From 233b0f8e9ec844948d671346d5a0f1cdc9a68f3b Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Mon, 29 Jan 2018 18:47:31 +0000 Subject: [PATCH 1/2] TBTR: Add sanity check assertions --- src/tbtr_template_gui_create.cpp | 4 ++++ src/train_cmd.cpp | 3 +++ src/vehicle.cpp | 2 ++ 3 files changed, 9 insertions(+) diff --git a/src/tbtr_template_gui_create.cpp b/src/tbtr_template_gui_create.cpp index a21c168636..42dd41c647 100644 --- a/src/tbtr_template_gui_create.cpp +++ b/src/tbtr_template_gui_create.cpp @@ -188,6 +188,9 @@ public: } virtual_train = train; + if (virtual_train != NULL) { + assert(HasBit(virtual_train->subtype, GVSF_VIRTUAL)); + } UpdateButtonState(); } @@ -542,6 +545,7 @@ public: void RearrangeVirtualTrain() { virtual_train = virtual_train->First(); + assert(HasBit(virtual_train->subtype, GVSF_VIRTUAL)); } diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp index 677401bca1..4f92b0ad5d 100644 --- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -1211,6 +1211,7 @@ CommandCost CmdMoveRailVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, u src = src->GetFirstEnginePart(); if (dst != NULL) { dst = dst->GetFirstEnginePart(); + assert(HasBit(dst->subtype, GVSF_VIRTUAL) == HasBit(src->subtype, GVSF_VIRTUAL)); } /* don't move the same vehicle.. */ @@ -1218,9 +1219,11 @@ CommandCost CmdMoveRailVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, u /* locate the head of the two chains */ Train *src_head = src->First(); + assert(HasBit(src_head->subtype, GVSF_VIRTUAL) == HasBit(src->subtype, GVSF_VIRTUAL)); Train *dst_head; if (dst != NULL) { dst_head = dst->First(); + assert(HasBit(dst_head->subtype, GVSF_VIRTUAL) == HasBit(dst->subtype, GVSF_VIRTUAL)); if (dst_head->tile != src_head->tile) return CMD_ERROR; /* Now deal with articulated part of destination wagon */ dst = dst->GetLastEnginePart(); diff --git a/src/vehicle.cpp b/src/vehicle.cpp index 5774aa7c0f..bab123360d 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -1072,6 +1072,8 @@ void CallVehicleTicks() for (TemplateReplacementMap::iterator it = _vehicles_to_templatereplace.Begin(); it != _vehicles_to_templatereplace.End(); it++) { Train *t = it->first; + assert(!_vehicles_to_autoreplace.Contains(t)); + /* Store the position of the effect as the vehicle pointer will become invalid later */ int x = t->x_pos; int y = t->y_pos; From 64d9f8880211650a49e4adc8d40e2b4c91fc94dc Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Mon, 29 Jan 2018 22:03:11 +0000 Subject: [PATCH 2/2] TBTR: Fix bugs when both autoreplace and template replacement active on same vehicle --- src/vehicle.cpp | 68 ++++++++++++++++++++++++++----------------------- 1 file changed, 36 insertions(+), 32 deletions(-) diff --git a/src/vehicle.cpp b/src/vehicle.cpp index bab123360d..e87661093c 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -1034,6 +1034,38 @@ void CallVehicleTicks() } } + /* do Template Replacement */ + Backup tmpl_cur_company(_current_company, FILE_LINE); + for (TemplateReplacementMap::iterator it = _vehicles_to_templatereplace.Begin(); it != _vehicles_to_templatereplace.End(); it++) { + Train *t = it->first; + + _vehicles_to_autoreplace.Erase(t); + + /* Store the position of the effect as the vehicle pointer will become invalid later */ + int x = t->x_pos; + int y = t->y_pos; + int z = t->z_pos; + + tmpl_cur_company.Change(t->owner); + + bool stayInDepot = it->second; + + it->first->vehstatus |= VS_STOPPED; + CommandCost res = DoCommand(t->tile, t->index, stayInDepot ? 1 : 0, DC_EXEC, CMD_TEMPLATE_REPLACE_VEHICLE); + + if (!IsLocalCompany()) continue; + + if (res.Succeeded()) { + if (res.GetCost() != 0) { + ShowCostOrIncomeAnimation(x, y, z, res.GetCost()); + } + continue; + } + + ShowAutoReplaceAdviceMessage(res, t); + } + tmpl_cur_company.Restore(); + /* do Auto Replacement */ Backup cur_company(_current_company, FILE_LINE); for (AutoreplaceMap::iterator it = _vehicles_to_autoreplace.Begin(); it != _vehicles_to_autoreplace.End(); it++) { @@ -1041,6 +1073,10 @@ void CallVehicleTicks() /* Autoreplace needs the current company set as the vehicle owner */ cur_company.Change(v->owner); + if (v->type == VEH_TRAIN) { + assert(!_vehicles_to_templatereplace.Contains(Train::From(v))); + } + /* Start vehicle if we stopped them in VehicleEnteredDepotThisTick() * We need to stop them between VehicleEnteredDepotThisTick() and here or we risk that * they are already leaving the depot again before being replaced. */ @@ -1066,38 +1102,6 @@ void CallVehicleTicks() ShowAutoReplaceAdviceMessage(res, v); } cur_company.Restore(); - - /* do Template Replacement */ - Backup tmpl_cur_company(_current_company, FILE_LINE); - for (TemplateReplacementMap::iterator it = _vehicles_to_templatereplace.Begin(); it != _vehicles_to_templatereplace.End(); it++) { - Train *t = it->first; - - assert(!_vehicles_to_autoreplace.Contains(t)); - - /* Store the position of the effect as the vehicle pointer will become invalid later */ - int x = t->x_pos; - int y = t->y_pos; - int z = t->z_pos; - - tmpl_cur_company.Change(t->owner); - - bool stayInDepot = it->second; - - it->first->vehstatus |= VS_STOPPED; - CommandCost res = DoCommand(t->tile, t->index, stayInDepot ? 1 : 0, DC_EXEC, CMD_TEMPLATE_REPLACE_VEHICLE); - - if (!IsLocalCompany()) continue; - - if (res.Succeeded()) { - if (res.GetCost() != 0) { - ShowCostOrIncomeAnimation(x, y, z, res.GetCost()); - } - continue; - } - - ShowAutoReplaceAdviceMessage(res, t); - } - tmpl_cur_company.Restore(); } /**