diff --git a/src/group_cmd.cpp b/src/group_cmd.cpp index 55873fb8d4..951c74f959 100644 --- a/src/group_cmd.cpp +++ b/src/group_cmd.cpp @@ -139,7 +139,7 @@ void GroupStatistics::Clear() /* static */ void GroupStatistics::CountVehicle(const Vehicle *v, int delta) { /* make virtual trains group-neutral */ - if ( HasBit(v->subtype, GVSF_VIRTUAL) ) return; + if (HasBit(v->subtype, GVSF_VIRTUAL)) return; assert(delta == 1 || delta == -1); @@ -164,6 +164,9 @@ void GroupStatistics::Clear() */ /* static */ void GroupStatistics::CountEngine(const Vehicle *v, int delta) { + /* make virtual trains group-neutral */ + if (HasBit(v->subtype, GVSF_VIRTUAL)) return; + assert(delta == 1 || delta == -1); GroupStatistics::GetAllGroup(v).num_engines[v->engine_type] += delta; GroupStatistics::Get(v).num_engines[v->engine_type] += delta; diff --git a/src/saveload/tbtr_template_veh_sl.cpp b/src/saveload/tbtr_template_veh_sl.cpp index 60c8b6080b..054eb45971 100644 --- a/src/saveload/tbtr_template_veh_sl.cpp +++ b/src/saveload/tbtr_template_veh_sl.cpp @@ -3,6 +3,7 @@ #include "../tbtr_template_vehicle.h" #include "../tbtr_template_vehicle_func.h" #include "../train.h" +#include "../core/backup_type.hpp" #include "saveload.h" @@ -102,6 +103,7 @@ void AfterLoadTemplateVehiclesUpdateImage() FOR_ALL_TEMPLATES(tv) { if (tv->Prev() == NULL) { + Backup cur_company(_current_company, tv->owner, FILE_LINE); StringID err; Train* t = VirtualTrainFromTemplateVehicle(tv, err); if (t != NULL) { @@ -123,6 +125,7 @@ void AfterLoadTemplateVehiclesUpdateImage() } delete t; } + cur_company.Restore(); } } } diff --git a/src/vehicle.cpp b/src/vehicle.cpp index 9f17876dfe..ba22eb2058 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -653,6 +653,7 @@ uint CountVehiclesInChain(const Vehicle *v) */ bool Vehicle::IsEngineCountable() const { + if (HasBit(this->subtype, GVSF_VIRTUAL)) return false; switch (this->type) { case VEH_AIRCRAFT: return Aircraft::From(this)->IsNormalAircraft(); // don't count plane shadows and helicopter rotors case VEH_TRAIN: diff --git a/src/vehicle_cmd.cpp b/src/vehicle_cmd.cpp index 3622dbd287..668e2316b7 100644 --- a/src/vehicle_cmd.cpp +++ b/src/vehicle_cmd.cpp @@ -919,6 +919,10 @@ CommandCost CmdVirtualTrainFromTemplateVehicle(TileIndex tile, DoCommandFlag fla return CMD_ERROR; } + if (tv->owner != _current_company) { + return CMD_ERROR; + } + bool should_execute = (flags & DC_EXEC) != 0; if (should_execute) { @@ -940,6 +944,8 @@ Train* VirtualTrainFromTemplateVehicle(TemplateVehicle* tv, StringID &err) CommandCost c; Train *tmp, *head, *tail; + assert(tv->owner == _current_company); + head = CmdBuildVirtualRailVehicle(tv->engine_type, true, err); if (!head) return NULL;