Skip to content

Commit

Permalink
Feature: Per-group wagon removal flag.
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterN committed Mar 30, 2019
1 parent 0b9707c commit 481ed27
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/autoreplace_cmd.cpp
Expand Up @@ -687,6 +687,9 @@ CommandCost CmdAutoreplaceVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1
const Company *c = Company::Get(_current_company);
bool wagon_removal = c->settings.renew_keep_length;

const Group *g = Group::GetIfValid(v->group_id);
if (g != NULL) wagon_removal = HasBit(g->flags, GroupFlags::GF_REPLACE_WAGON_REMOVAL);

/* Test whether any replacement is set, before issuing a whole lot of commands that would end in nothing changed */
Vehicle *w = v;
bool any_replacements = false;
Expand Down
22 changes: 18 additions & 4 deletions src/autoreplace_gui.cpp
Expand Up @@ -342,8 +342,15 @@ class ReplaceVehicleWindow : public Window {
break;

case WID_RV_TRAIN_WAGONREMOVE_TOGGLE: {
const Company *c = Company::Get(_local_company);
SetDParam(0, c->settings.renew_keep_length ? STR_CONFIG_SETTING_ON : STR_CONFIG_SETTING_OFF);
bool remove_wagon;
const Group *g = Group::GetIfValid(this->sel_group);
if (g != NULL) {
remove_wagon = HasBit(g->flags, GroupFlags::GF_REPLACE_WAGON_REMOVAL);
} else {
const Company *c = Company::Get(_local_company);
remove_wagon = c->settings.renew_keep_length;
}
SetDParam(0, remove_wagon ? STR_CONFIG_SETTING_ON : STR_CONFIG_SETTING_OFF);
break;
}

Expand Down Expand Up @@ -478,9 +485,16 @@ class ReplaceVehicleWindow : public Window {
ShowDropDownList(this, GetRailTypeDropDownList(true, true), sel_railtype, WID_RV_TRAIN_RAILTYPE_DROPDOWN);
break;

case WID_RV_TRAIN_WAGONREMOVE_TOGGLE: // toggle renew_keep_length
DoCommandP(0, GetCompanySettingIndex("company.renew_keep_length"), Company::Get(_local_company)->settings.renew_keep_length ? 0 : 1, CMD_CHANGE_COMPANY_SETTING);
case WID_RV_TRAIN_WAGONREMOVE_TOGGLE: {
const Group *g = Group::GetIfValid(this->sel_group);
if (g != NULL) {
DoCommandP(0, this->sel_group | (GroupFlags::GF_REPLACE_WAGON_REMOVAL << 16), (HasBit(g->flags, GroupFlags::GF_REPLACE_WAGON_REMOVAL) ? 0 : 1) | (_ctrl_pressed << 1), CMD_SET_GROUP_FLAG);
} else {
// toggle renew_keep_length
DoCommandP(0, GetCompanySettingIndex("company.renew_keep_length"), Company::Get(_local_company)->settings.renew_keep_length ? 0 : 1, CMD_CHANGE_COMPANY_SETTING);
}
break;
}

case WID_RV_START_REPLACE: { // Start replacing
if (this->GetWidget<NWidgetLeaf>(widget)->ButtonHit(pt)) {
Expand Down
3 changes: 2 additions & 1 deletion src/group.h
Expand Up @@ -64,7 +64,8 @@ struct GroupStatistics {
};

enum GroupFlags : uint8 {
GF_REPLACE_PROTECTION, ///< If set to true, the global autoreplace has no effect on the group
GF_REPLACE_PROTECTION, ///< If set to true, the global autoreplace has no effect on the group
GF_REPLACE_WAGON_REMOVAL, ///< If set, autoreplace will perform wagon removal on vehicles in this group.
GF_END,
};

Expand Down
3 changes: 2 additions & 1 deletion src/group_cmd.cpp
Expand Up @@ -331,18 +331,19 @@ CommandCost CmdCreateGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, uint3

if (flags & DC_EXEC) {
Group *g = new Group(_current_company);
ClrBit(g->flags, GroupFlags::GF_REPLACE_PROTECTION);
g->vehicle_type = vt;
g->parent = INVALID_GROUP;

if (pg == NULL) {
const Company *c = Company::Get(_current_company);
g->livery.colour1 = c->livery[LS_DEFAULT].colour1;
g->livery.colour2 = c->livery[LS_DEFAULT].colour2;
if (c->settings.renew_keep_length) SetBit(g->flags, GroupFlags::GF_REPLACE_WAGON_REMOVAL);
} else {
g->parent = pg->index;
g->livery.colour1 = pg->livery.colour1;
g->livery.colour2 = pg->livery.colour2;
g->flags = pg->flags;
}

_new_group_id = g->index;
Expand Down
14 changes: 14 additions & 0 deletions src/saveload/afterload.cpp
Expand Up @@ -3121,6 +3121,20 @@ bool AfterLoadGame()
}
}

if (IsSavegameVersionBefore(SLV_GROUP_REPLACE_WAGON_REMOVAL)) {
/* Propagate wagon removal flag for compatibility */
/* Temporary bitmask of company wagon removal setting */
uint16 wagon_removal;
const Company *c;
FOR_ALL_COMPANIES(c) {
if (c->settings.renew_keep_length) SetBit(wagon_removal, c->index);
}
Group *g;
FOR_ALL_GROUPS(g) {
if (HasBit(wagon_removal, g->owner)) SetBit(g->flags, GroupFlags::GF_REPLACE_WAGON_REMOVAL);
}
}

/* Compute station catchment areas. This is needed here in case UpdateStationAcceptance is called below. */
Station::RecomputeCatchmentForAll();

Expand Down
1 change: 1 addition & 0 deletions src/saveload/saveload.h
Expand Up @@ -298,6 +298,7 @@ enum SaveLoadVersion : uint16 {
SLV_ROADVEH_PATH_CACHE, ///< 211 PR#7261 Add path cache for road vehicles.
SLV_REMOVE_OPF, ///< 212 PR#7245 Remove OPF.
SLV_TREES_WATER_CLASS, ///< 213 PR#7405 WaterClass update for tree tiles.
SLV_GROUP_REPLACE_WAGON_REMOVAL, ///< 214 PR#.... Per-group wagon removal flag.

SL_MAX_VERSION, ///< Highest possible saveload version
};
Expand Down

0 comments on commit 481ed27

Please sign in to comment.