Changeset - r25224:92a124f03ad6
[Not reviewed]
master
0 6 0
peter1138 - 5 years ago 2019-03-29 23:24:40
peter1138@openttd.org
Codechange: Replace Group::replace_protection with Group::flags
6 files changed with 23 insertions and 16 deletions:
0 comments (0 inline, 0 general)
src/autoreplace.cpp
Show inline comments
 
@@ -8,12 +8,13 @@
 
/** @file autoreplace.cpp Management of replacement lists. */
 

	
 
#include "stdafx.h"
 
#include "command_func.h"
 
#include "group.h"
 
#include "autoreplace_base.h"
 
#include "core/bitmath_func.hpp"
 
#include "core/pool_func.hpp"
 

	
 
#include "safeguards.h"
 

	
 
/** The pool of autoreplace "orders". */
 
EngineRenewPool _enginerenew_pool("EngineRenew");
 
@@ -61,13 +62,13 @@ void RemoveAllEngineReplacement(EngineRe
 
 * @return The engine type to replace with, or INVALID_ENGINE if no
 
 * replacement is in the list.
 
 */
 
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);
 
	}
 
	if (replace_when_old != nullptr) *replace_when_old = er == nullptr ? false : er->replace_when_old;
 
	return er == nullptr ? INVALID_ENGINE : er->to;
 
}
src/group.h
Show inline comments
 
@@ -59,19 +59,23 @@ struct GroupStatistics {
 

	
 
	static void UpdateProfits();
 
	static void UpdateAfterLoad();
 
	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.
 

	
 
	bool folded;                ///< NOSAVE: Is this group folded in the group view?
 

	
 
	GroupID parent;             ///< Parent group
src/group_cmd.cpp
Show inline comments
 
@@ -312,13 +312,13 @@ CommandCost CmdCreateGroup(TileIndex til
 
		if (pg->owner != _current_company) return CMD_ERROR;
 
		if (pg->vehicle_type != vt) return CMD_ERROR;
 
	}
 

	
 
	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;
 

	
 
		if (pg == nullptr) {
 
			const Company *c = Company::Get(_current_company);
 
			g->livery.colour1 = c->livery[LS_DEFAULT].colour1;
 
@@ -665,18 +665,24 @@ CommandCost CmdSetGroupLivery(TileIndex 
 

	
 
/**
 
 * Set replace protection for a group and its sub-groups.
 
 * @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);
 
	}
 
}
 

	
 
/**
 
 * (Un)set global replace protection from a group
 
 * @param tile unused
 
@@ -692,17 +698,13 @@ static void SetGroupReplaceProtection(Gr
 
CommandCost CmdSetGroupReplaceProtection(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
 
{
 
	Group *g = Group::GetIfValid(p1);
 
	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);
 
	}
 

	
 
	return CommandCost();
src/group_gui.cpp
Show inline comments
 
@@ -530,13 +530,13 @@ public:
 
				WID_GL_CREATE_GROUP,
 
				WID_GL_AVAILABLE_VEHICLES,
 
				WIDGET_LIST_END);
 

	
 
		/* 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<NWidgetCore>(WID_GL_REPLACE_PROTECTION)->widget_data = protect_sprite + this->vli.vtype;
 

	
 
		/* Set text of "group by" dropdown widget. */
 
		this->GetWidget<NWidgetCore>(WID_GL_GROUP_BY_DROPDOWN)->widget_data = this->vehicle_group_by_names[this->grouping];
 

	
 
		/* Set text of "sort by" dropdown widget. */
 
@@ -598,13 +598,13 @@ public:
 
				int max = std::min<size_t>(this->group_sb->GetPosition() + this->group_sb->GetCapacity(), this->groups.size());
 
				for (int i = this->group_sb->GetPosition(); i < max; ++i) {
 
					const Group *g = this->groups[i];
 

	
 
					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;
 
				}
 
				if ((uint)this->group_sb->GetPosition() + this->group_sb->GetCapacity() > this->groups.size()) {
 
					DrawGroupInfo(y1, r.left, r.right, NEW_GROUP);
 
				}
 
@@ -795,13 +795,13 @@ public:
 
				break;
 
			}
 

	
 
			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;
 
			}
 
		}
 
	}
 

	
src/saveload/group_sl.cpp
Show inline comments
 
@@ -18,13 +18,13 @@
 
static const SaveLoad _group_desc[] = {
 
	 SLE_CONDVAR(Group, name,               SLE_NAME,                       SL_MIN_VERSION,  SLV_84),
 
	SLE_CONDSSTR(Group, name,               SLE_STR | SLF_ALLOW_CONTROL,    SLV_84, SL_MAX_VERSION),
 
	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),
 
	 SLE_CONDVAR(Group, parent,             SLE_UINT16,                    SLV_189, SL_MAX_VERSION),
 
	     SLE_END()
 
};
src/script/api/script_group.cpp
Show inline comments
 
@@ -93,13 +93,13 @@
 
}
 

	
 
/* static */ bool ScriptGroup::GetAutoReplaceProtection(GroupID group_id)
 
{
 
	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)
 
{
 
	if (!IsValidGroup(group_id) && group_id != GROUP_DEFAULT && group_id != GROUP_ALL) return -1;
 

	
0 comments (0 inline, 0 general)