diff --git a/src/lang/english.txt b/src/lang/english.txt index d48e5e892f..fb2e703822 100644 --- a/src/lang/english.txt +++ b/src/lang/english.txt @@ -6152,6 +6152,7 @@ STR_TMPL_WARNING_FREE_WAGON :{RED}Free Chain STR_TMPL_WARNING_VEH_UNAVAILABLE :{RED}Train not buildable: vehicle unavailable! STR_TMPL_GROUP_USES_TEMPLATE :{BLACK}Template in use: {NUM} STR_TMP_TEMPLATE_IN_USE :Template is in use +STR_TMP_TEMPLATE_FROM_PARENT_GROUP :Using template from parent group STR_TMPL_GROUP_NUM_TRAINS :{BLACK}{NUM} STR_TMPL_CREATEGUI_TITLE :{WHITE}Create/Edit Template Vehicle STR_TMPL_MAINGUI_DEFINEDGROUPS :{BLACK}Defined Groups for Company diff --git a/src/tbtr_template_gui_main.cpp b/src/tbtr_template_gui_main.cpp index 741da77c3c..a80f038b76 100644 --- a/src/tbtr_template_gui_main.cpp +++ b/src/tbtr_template_gui_main.cpp @@ -602,11 +602,14 @@ public: StringID str = STR_GROUP_NAME; DrawString(left + ScaleGUITrad(30 + this->indents[i] * 10), right, text_y, str, TC_BLACK); - const TemplateID tid = GetTemplateIDByGroupID(g_id); + const TemplateID tid = GetTemplateIDByGroupIDRecursive(g_id); + const TemplateID tid_self = GetTemplateIDByGroupID(g_id); /* Draw the template in use for this group, if there is one */ short template_in_use = FindTemplateIndex(tid); - if (template_in_use >= 0) { + if (tid != INVALID_TEMPLATE && tid_self == INVALID_TEMPLATE) { + DrawString (left, right, text_y, STR_TMP_TEMPLATE_FROM_PARENT_GROUP, TC_SILVER, SA_HOR_CENTER); + } else if (template_in_use >= 0) { SetDParam(0, template_in_use); DrawString (left, right, text_y, STR_TMPL_GROUP_USES_TEMPLATE, TC_BLACK, SA_HOR_CENTER); } else if (tid != INVALID_TEMPLATE) { /* If there isn't a template applied from the current group, check if there is one for another rail type */ diff --git a/src/tbtr_template_vehicle.cpp b/src/tbtr_template_vehicle.cpp index 74867507ae..5357bc3fb5 100644 --- a/src/tbtr_template_vehicle.cpp +++ b/src/tbtr_template_vehicle.cpp @@ -159,6 +159,18 @@ TemplateID GetTemplateIDByGroupID(GroupID gid) return iter->second; } +TemplateID GetTemplateIDByGroupIDRecursive(GroupID gid) +{ + while (gid != INVALID_GROUP) { + auto iter = _template_replacement_index.find(gid); + if (iter != _template_replacement_index.end()) return iter->second; + const Group *g = Group::GetIfValid(gid); + if (g == nullptr) break; + gid = Group::Get(gid)->parent; + } + return INVALID_TEMPLATE; +} + bool IssueTemplateReplacement(GroupID gid, TemplateID tid) { TemplateReplacement *tr = GetTemplateReplacementByGroupID(gid); diff --git a/src/tbtr_template_vehicle.h b/src/tbtr_template_vehicle.h index 6178137ce5..c245cd2533 100644 --- a/src/tbtr_template_vehicle.h +++ b/src/tbtr_template_vehicle.h @@ -201,6 +201,7 @@ struct TemplateReplacement : TemplateReplacementPool::PoolItem<&_template_replac TemplateReplacement* GetTemplateReplacementByGroupID(GroupID); TemplateID GetTemplateIDByGroupID(GroupID); +TemplateID GetTemplateIDByGroupIDRecursive(GroupID); bool IssueTemplateReplacement(GroupID, TemplateID); short DeleteTemplateReplacementsByGroupID(GroupID); diff --git a/src/tbtr_template_vehicle_func.cpp b/src/tbtr_template_vehicle_func.cpp index 86d84bb155..c0c08a5f56 100644 --- a/src/tbtr_template_vehicle_func.cpp +++ b/src/tbtr_template_vehicle_func.cpp @@ -208,6 +208,12 @@ TemplateVehicle* GetTemplateVehicleByGroupID(GroupID gid) { return tid != INVALID_TEMPLATE ? TemplateVehicle::GetIfValid(tid) : nullptr; } +TemplateVehicle* GetTemplateVehicleByGroupIDRecursive(GroupID gid) { + if (gid >= NEW_GROUP) return nullptr; + const TemplateID tid = GetTemplateIDByGroupIDRecursive(gid); + return tid != INVALID_TEMPLATE ? TemplateVehicle::GetIfValid(tid) : nullptr; +} + /** * Check a template consist whether it contains any engine of the given railtype */ diff --git a/src/tbtr_template_vehicle_func.h b/src/tbtr_template_vehicle_func.h index 72f9e385a8..744ca5d48c 100644 --- a/src/tbtr_template_vehicle_func.h +++ b/src/tbtr_template_vehicle_func.h @@ -38,6 +38,7 @@ void tbtr_debug_pvt(const Train*); #endif TemplateVehicle* GetTemplateVehicleByGroupID(GroupID); +TemplateVehicle* GetTemplateVehicleByGroupIDRecursive(GroupID); bool ChainContainsVehicle(Train*, Train*); Train* ChainContainsEngine(EngineID, Train*); Train* DepotContainsEngine(TileIndex, EngineID, Train*); diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp index 194fba4e37..7916fca456 100644 --- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -5494,7 +5494,7 @@ CommandCost CmdTemplateReplaceVehicle(TileIndex tile, DoCommandFlag flags, uint3 Train *new_chain = nullptr; Train *remainder_chain = nullptr; Train *tmp_chain = nullptr; - TemplateVehicle *tv = GetTemplateVehicleByGroupID(incoming->group_id); + TemplateVehicle *tv = GetTemplateVehicleByGroupIDRecursive(incoming->group_id); if (tv == nullptr) { if (leaveDepot) incoming->vehstatus &= ~VS_STOPPED; return CMD_ERROR; diff --git a/src/vehicle.cpp b/src/vehicle.cpp index cbe196f068..2ba1ff9ab4 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -248,7 +248,7 @@ bool Vehicle::NeedsServicing() const } if (this->type == VEH_TRAIN) { - TemplateVehicle *tv = GetTemplateVehicleByGroupID(this->group_id); + TemplateVehicle *tv = GetTemplateVehicleByGroupIDRecursive(this->group_id); if (tv != nullptr) { if (tv->IsReplaceOldOnly() && !this->NeedsAutorenewing(c, false)) return false; Money needed_money = c->settings.engine_renew_money; @@ -1092,7 +1092,7 @@ void Vehicle::PreCleanPool() void VehicleEnteredDepotThisTick(Vehicle *v) { /* Template Replacement Setup stuff */ - if (GetTemplateIDByGroupID(v->group_id) != INVALID_TEMPLATE) { + if (GetTemplateIDByGroupIDRecursive(v->group_id) != INVALID_TEMPLATE) { /* Vehicle should stop in the depot if it was in 'stopping' state */ _vehicles_to_templatereplace.insert(v->index); }