summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPatric Stout <truebrain@openttd.org>2021-01-11 20:58:00 +0100
committerGitHub <noreply@github.com>2021-01-11 20:58:00 +0100
commitcd36e1716022dc86b128901604ebb0960d6dc35a (patch)
treecd76c589b1b06ba3b3af2ca906c89eeabc2f8061 /src
parent760b0cdc114864e2987a1e9c2b8369cea603a04e (diff)
downloadopenttd-cd36e1716022dc86b128901604ebb0960d6dc35a.tar.xz
Fix: don't allow free-wagon-chains to exceed max-train-length (#8533)
This makes no sense, that a free-wagon-chain could be larger than the maximum length of a train, as you cannot put an engine in front of that anyway. And it prevents run-away AIs making very silly long free-wagon-chains.
Diffstat (limited to 'src')
-rw-r--r--src/train_cmd.cpp20
1 files changed, 17 insertions, 3 deletions
diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp
index ddd8cafc7..3b8230e5f 100644
--- a/src/train_cmd.cpp
+++ b/src/train_cmd.cpp
@@ -646,8 +646,9 @@ static CommandCost CmdBuildRailWagon(TileIndex tile, DoCommandFlag flags, const
w->engine_type == e->index && ///< Same type
w->First() != v && ///< Don't connect to ourself
!(w->vehstatus & VS_CRASHED)) { ///< Not crashed/flooded
- DoCommand(0, v->index | 1 << 20, w->Last()->index, DC_EXEC, CMD_MOVE_RAIL_VEHICLE);
- break;
+ if (DoCommand(0, v->index | 1 << 20, w->Last()->index, DC_EXEC, CMD_MOVE_RAIL_VEHICLE).Succeeded()) {
+ break;
+ }
}
}
}
@@ -959,12 +960,25 @@ static CommandCost CheckNewTrain(Train *original_dst, Train *dst, Train *origina
static CommandCost CheckTrainAttachment(Train *t)
{
/* No multi-part train, no need to check. */
- if (t == nullptr || t->Next() == nullptr || !t->IsEngine()) return CommandCost();
+ if (t == nullptr || t->Next() == nullptr) return CommandCost();
/* The maximum length for a train. For each part we decrease this by one
* and if the result is negative the train is simply too long. */
int allowed_len = _settings_game.vehicle.max_train_length * TILE_SIZE - t->gcache.cached_veh_length;
+ /* For free-wagon chains, check if they are within the max_train_length limit. */
+ if (!t->IsEngine()) {
+ t = t->Next();
+ while (t != nullptr) {
+ allowed_len -= t->gcache.cached_veh_length;
+
+ t = t->Next();
+ }
+
+ if (allowed_len < 0) return_cmd_error(STR_ERROR_TRAIN_TOO_LONG);
+ return CommandCost();
+ }
+
Train *head = t;
Train *prev = t;