summaryrefslogtreecommitdiff
path: root/src/autoreplace_cmd.cpp
diff options
context:
space:
mode:
authorfrosch <frosch@openttd.org>2008-08-21 20:42:45 +0000
committerfrosch <frosch@openttd.org>2008-08-21 20:42:45 +0000
commit447d0930f702a5a82d05a3f1ec94fcb59d10048c (patch)
tree16f2904c7d14bac747cb9d6ee38794d57d0d6c3d /src/autoreplace_cmd.cpp
parentf520a702e0279fffaa893496452d2864b32ca705 (diff)
downloadopenttd-447d0930f702a5a82d05a3f1ec94fcb59d10048c.tar.xz
(svn r14125) -Fix [FS#2237]: Segfault when autoreplace failed very early.
Note: Proper indenting in next commit.
Diffstat (limited to 'src/autoreplace_cmd.cpp')
-rw-r--r--src/autoreplace_cmd.cpp23
1 files changed, 13 insertions, 10 deletions
diff --git a/src/autoreplace_cmd.cpp b/src/autoreplace_cmd.cpp
index 5aa088b0c..2fd087a24 100644
--- a/src/autoreplace_cmd.cpp
+++ b/src/autoreplace_cmd.cpp
@@ -367,13 +367,13 @@ static CommandCost ReplaceChain(Vehicle **chain, uint32 flags, bool wagon_remova
}
Vehicle *new_head = (new_vehs[0] != NULL ? new_vehs[0] : old_vehs[0]);
- /* Separate the head, so we can start constructing the new chain */
+ /* Note: When autoreplace has already failed here, old_vehs[] is not completely initialized. But it is also not needed. */
if (cost.Succeeded()) {
+ /* Separate the head, so we can start constructing the new chain */
Vehicle *second = GetNextUnit(old_head);
if (second != NULL) cost.AddCost(MoveVehicle(second, NULL, DC_EXEC | DC_AUTOREPLACE, true));
assert(GetNextUnit(new_head) == NULL);
- }
/* Append engines to the new chain
* We do this from back to front, so that the head of the temporary vehicle chain does not change all the time.
@@ -468,7 +468,9 @@ static CommandCost ReplaceChain(Vehicle **chain, uint32 flags, bool wagon_remova
}
}
- /* If we are not in DC_EXEC undo everything */
+ /* If we are not in DC_EXEC undo everything, i.e. rearrange old vehicles.
+ * We do this from back to front, so that the head of the temporary vehicle chain does not change all the time.
+ * Note: The vehicle attach callback is disabled here :) */
if ((flags & DC_EXEC) == 0) {
/* Separate the head, so we can reattach the old vehicles */
Vehicle *second = GetNextUnit(old_head);
@@ -476,15 +478,16 @@ static CommandCost ReplaceChain(Vehicle **chain, uint32 flags, bool wagon_remova
assert(GetNextUnit(old_head) == NULL);
- /* Rearrange old vehicles and sell new
- * We do this from back to front, so that the head of the temporary vehicle chain does not change all the time.
- * Note: The vehicle attach callback is disabled here :) */
+ for (int i = num_units - 1; i > 0; i--) {
+ CommandCost ret = MoveVehicle(old_vehs[i], old_head, DC_EXEC | DC_AUTOREPLACE, false);
+ assert(ret.Succeeded());
+ }
+ }
+ }
+ /* Finally undo buying of new vehicles */
+ if ((flags & DC_EXEC) == 0) {
for (int i = num_units - 1; i >= 0; i--) {
- if (i > 0) {
- CommandCost ret = MoveVehicle(old_vehs[i], old_head, DC_EXEC | DC_AUTOREPLACE, false);
- assert(ret.Succeeded());
- }
if (new_vehs[i] != NULL) {
DoCommand(0, new_vehs[i]->index, 0, DC_EXEC, GetCmdSellVeh(new_vehs[i]));
new_vehs[i] = NULL;