diff --git a/src/autoreplace.cpp b/src/autoreplace.cpp index 59980546ee..e1b73448dd 100644 --- a/src/autoreplace.cpp +++ b/src/autoreplace.cpp @@ -11,6 +11,7 @@ #include "command_func.h" #include "group.h" #include "autoreplace_base.h" +#include "core/bitmath_func.hpp" #include "core/pool_func.hpp" #include "safeguards.h" @@ -64,7 +65,7 @@ void RemoveAllEngineReplacement(EngineRenewList *erl) EngineID EngineReplacement(EngineRenewList erl, EngineID engine, GroupID group, bool *replace_when_old) { const EngineRenew *er = GetEngineReplacement(erl, engine, group); - if (er == nullptr && (group == DEFAULT_GROUP || (Group::IsValidID(group) && !Group::Get(group)->replace_protection))) { + if (er == nullptr && (group == DEFAULT_GROUP || (Group::IsValidID(group) && !HasBit(Group::Get(group)->flags, GroupFlags::GF_REPLACE_PROTECTION)))) { /* We didn't find anything useful in the vehicle's own group so we will try ALL_GROUP */ er = GetEngineReplacement(erl, engine, ALL_GROUP); } diff --git a/src/group.h b/src/group.h index 1be8d8b01f..57d15b448d 100644 --- a/src/group.h +++ b/src/group.h @@ -62,13 +62,17 @@ struct GroupStatistics { static void UpdateAutoreplace(CompanyID company); }; +enum GroupFlags : uint8 { + GF_REPLACE_PROTECTION, ///< If set to true, the global autoreplace has no effect on the group +}; + /** Group data. */ struct Group : GroupPool::PoolItem<&_group_pool> { std::string name; ///< Group Name Owner owner; ///< Group Owner VehicleType vehicle_type; ///< Vehicle type of the group - bool replace_protection; ///< If set to true, the global autoreplace have no effect on the group + uint8 flags; ///< Group flags Livery livery; ///< Custom colour scheme for vehicles in this group GroupStatistics statistics; ///< NOSAVE: Statistics and caches on the vehicles in the group. diff --git a/src/group_cmd.cpp b/src/group_cmd.cpp index bf25dae862..219470c207 100644 --- a/src/group_cmd.cpp +++ b/src/group_cmd.cpp @@ -315,7 +315,7 @@ CommandCost CmdCreateGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint3 if (flags & DC_EXEC) { Group *g = new Group(_current_company); - g->replace_protection = false; + ClrBit(g->flags, GroupFlags::GF_REPLACE_PROTECTION); g->vehicle_type = vt; g->parent = INVALID_GROUP; @@ -668,12 +668,18 @@ CommandCost CmdSetGroupLivery(TileIndex tile, DoCommandFlag flags, uint32 p1, ui * @param g initial group. * @param protect 1 to set or 0 to clear protection. */ -static void SetGroupReplaceProtection(Group *g, bool protect) +static void SetGroupReplaceProtection(Group *g, bool protect, bool children) { - g->replace_protection = protect; + if (protect) { + SetBit(g->flags, GroupFlags::GF_REPLACE_PROTECTION); + } else { + ClrBit(g->flags, GroupFlags::GF_REPLACE_PROTECTION); + } + + if (!children) return; for (Group *pg : Group::Iterate()) { - if (pg->parent == g->index) SetGroupReplaceProtection(pg, protect); + if (pg->parent == g->index) SetGroupReplaceProtection(pg, protect, true); } } @@ -695,11 +701,7 @@ CommandCost CmdSetGroupReplaceProtection(TileIndex tile, DoCommandFlag flags, ui if (g == nullptr || g->owner != _current_company) return CMD_ERROR; if (flags & DC_EXEC) { - if (HasBit(p2, 1)) { - SetGroupReplaceProtection(g, HasBit(p2, 0)); - } else { - g->replace_protection = HasBit(p2, 0); - } + SetGroupReplaceProtection(g, HasBit(p2, 0), HasBit(p2, 1)); SetWindowDirty(GetWindowClassForVehicleType(g->vehicle_type), VehicleListIdentifier(VL_GROUP_LIST, g->vehicle_type, _current_company).Pack()); InvalidateWindowData(WC_REPLACE_VEHICLE, g->vehicle_type); diff --git a/src/group_gui.cpp b/src/group_gui.cpp index 3720533043..0f463e030f 100644 --- a/src/group_gui.cpp +++ b/src/group_gui.cpp @@ -533,7 +533,7 @@ public: /* If not a default group and the group has replace protection, show an enabled replace sprite. */ uint16 protect_sprite = SPR_GROUP_REPLACE_OFF_TRAIN; - if (!IsDefaultGroupID(this->vli.index) && !IsAllGroupID(this->vli.index) && Group::Get(this->vli.index)->replace_protection) protect_sprite = SPR_GROUP_REPLACE_ON_TRAIN; + if (!IsDefaultGroupID(this->vli.index) && !IsAllGroupID(this->vli.index) && HasBit(Group::Get(this->vli.index)->flags, GroupFlags::GF_REPLACE_PROTECTION)) protect_sprite = SPR_GROUP_REPLACE_ON_TRAIN; this->GetWidget(WID_GL_REPLACE_PROTECTION)->widget_data = protect_sprite + this->vli.vtype; /* Set text of "group by" dropdown widget. */ @@ -601,7 +601,7 @@ public: assert(g->owner == this->owner); - DrawGroupInfo(y1, r.left, r.right, g->index, this->indents[i] * LEVEL_WIDTH, g->replace_protection, g->folded || (i + 1 < (int)this->groups.size() && indents[i + 1] > this->indents[i])); + DrawGroupInfo(y1, r.left, r.right, g->index, this->indents[i] * LEVEL_WIDTH, HasBit(g->flags, GroupFlags::GF_REPLACE_PROTECTION), g->folded || (i + 1 < (int)this->groups.size() && indents[i + 1] > this->indents[i])); y1 += this->tiny_step_height; } @@ -798,7 +798,7 @@ public: case WID_GL_REPLACE_PROTECTION: { const Group *g = Group::GetIfValid(this->vli.index); if (g != nullptr) { - DoCommandP(0, this->vli.index, (g->replace_protection ? 0 : 1) | (_ctrl_pressed << 1), CMD_SET_GROUP_REPLACE_PROTECTION); + DoCommandP(0, this->vli.index, (HasBit(g->flags, GroupFlags::GF_REPLACE_PROTECTION) ? 0 : 1) | (_ctrl_pressed << 1), CMD_SET_GROUP_REPLACE_PROTECTION); } break; } diff --git a/src/saveload/group_sl.cpp b/src/saveload/group_sl.cpp index cae313ff83..c5f7e2b507 100644 --- a/src/saveload/group_sl.cpp +++ b/src/saveload/group_sl.cpp @@ -21,7 +21,7 @@ static const SaveLoad _group_desc[] = { SLE_CONDNULL(2, SL_MIN_VERSION, SLV_164), // num_vehicle SLE_VAR(Group, owner, SLE_UINT8), SLE_VAR(Group, vehicle_type, SLE_UINT8), - SLE_VAR(Group, replace_protection, SLE_BOOL), + SLE_VAR(Group, flags, SLE_UINT8), SLE_CONDVAR(Group, livery.in_use, SLE_UINT8, SLV_GROUP_LIVERIES, SL_MAX_VERSION), SLE_CONDVAR(Group, livery.colour1, SLE_UINT8, SLV_GROUP_LIVERIES, SL_MAX_VERSION), SLE_CONDVAR(Group, livery.colour2, SLE_UINT8, SLV_GROUP_LIVERIES, SL_MAX_VERSION), diff --git a/src/script/api/script_group.cpp b/src/script/api/script_group.cpp index 7f80815864..ae2e4137b9 100644 --- a/src/script/api/script_group.cpp +++ b/src/script/api/script_group.cpp @@ -96,7 +96,7 @@ { if (!IsValidGroup(group_id)) return false; - return ::Group::Get(group_id)->replace_protection; + return HasBit(::Group::Get(group_id)->flags, GroupFlags::GF_REPLACE_PROTECTION); } /* static */ int32 ScriptGroup::GetNumEngines(GroupID group_id, EngineID engine_id)