(svn r22985) -Feature: Display autoreplace status in group GUI.

This commit is contained in:
frosch 2011-10-03 17:26:37 +00:00
parent f9f257ce4a
commit 37301ea23d
8 changed files with 78 additions and 4 deletions

View File

@ -754,6 +754,7 @@ CommandCost CmdSetAutoReplace(TileIndex tile, DoCommandFlag flags, uint32 p1, ui
cost = RemoveEngineReplacementForCompany(c, old_engine_type, id_g, flags);
}
if (flags & DC_EXEC) GroupStatistics::UpdateAutoreplace(_current_company);
if ((flags & DC_EXEC) && IsLocalCompany()) InvalidateAutoreplaceWindow(old_engine_type, id_g);
return cost;

View File

@ -419,6 +419,8 @@ void ChangeOwnershipOfCompanyItems(Owner old_owner, Owner new_owner)
}
}
}
if (new_owner != INVALID_OWNER) GroupStatistics::UpdateAutoreplace(new_owner);
}
/* Change ownership of tiles */

View File

@ -26,6 +26,9 @@ struct GroupStatistics {
uint16 num_vehicle; ///< Number of vehicles.
uint16 *num_engines; ///< Caches the number of engines of each type the company owns.
bool autoreplace_defined; ///< Are any autoreplace rules set?
bool autoreplace_finished; ///< Have all autoreplacement finished?
uint16 num_profit_vehicle; ///< Number of vehicles considered for profit statistics;
Money profit_last_year; ///< Sum of profits for all vehicles.
@ -40,6 +43,12 @@ struct GroupStatistics {
this->profit_last_year = 0;
}
void ClearAutoreplace()
{
this->autoreplace_defined = false;
this->autoreplace_finished = false;
}
static GroupStatistics &Get(CompanyID company, GroupID id_g, VehicleType type);
static GroupStatistics &Get(const Vehicle *v);
static GroupStatistics &GetAllGroup(const Vehicle *v);
@ -50,6 +59,7 @@ struct GroupStatistics {
static void UpdateProfits();
static void UpdateAfterLoad();
static void UpdateAutoreplace(CompanyID company);
};
/** Group data. */

View File

@ -125,6 +125,10 @@ void GroupStatistics::Clear()
GroupStatistics::CountEngine(v, 1);
if (v->IsPrimaryVehicle()) GroupStatistics::CountVehicle(v, 1);
}
FOR_ALL_COMPANIES(c) {
GroupStatistics::UpdateAutoreplace(c->index);
}
}
/**
@ -202,6 +206,37 @@ void GroupStatistics::Clear()
}
}
/**
* Update autoreplace_defined and autoreplace_finished of all statistics of a company.
* @param company Company to update statistics for.
*/
/* static */ void GroupStatistics::UpdateAutoreplace(CompanyID company)
{
/* Set up the engine count for all companies */
Company *c = Company::Get(company);
for (VehicleType type = VEH_BEGIN; type < VEH_COMPANY_END; type++) {
c->group_all[type].ClearAutoreplace();
c->group_default[type].ClearAutoreplace();
}
/* Recalculate */
Group *g;
FOR_ALL_GROUPS(g) {
if (g->owner != company) continue;
g->statistics.ClearAutoreplace();
}
for (EngineRenewList erl = c->engine_renew_list; erl != NULL; erl = erl->next) {
const Engine *e = Engine::Get(erl->from);
GroupStatistics &stats = GroupStatistics::Get(company, erl->group_id, e->type);
if (!stats.autoreplace_defined) {
stats.autoreplace_defined = true;
stats.autoreplace_finished = true;
}
if (stats.num_engines[erl->from] > 0) stats.autoreplace_finished = false;
}
}
/**
* Update the num engines of a groupID. Decrease the old one and increase the new one
* @note called in SetTrainGroupID and UpdateTrainGroupID
@ -400,6 +435,7 @@ CommandCost CmdAddVehicleGroup(TileIndex tile, DoCommandFlag flags, uint32 p1, u
}
GroupStatistics::CountVehicle(v, 1);
GroupStatistics::UpdateAutoreplace(v->owner);
/* Update the Replace Vehicle Windows */
SetWindowDirty(WC_REPLACE_VEHICLE, v->type);
@ -544,6 +580,7 @@ void SetTrainGroupID(Train *v, GroupID new_g)
}
/* Update the Replace Vehicle Windows */
GroupStatistics::UpdateAutoreplace(v->owner);
SetWindowDirty(WC_REPLACE_VEHICLE, VEH_TRAIN);
}
@ -567,6 +604,7 @@ void UpdateTrainGroupID(Train *v)
}
/* Update the Replace Vehicle Windows */
GroupStatistics::UpdateAutoreplace(v->owner);
SetWindowDirty(WC_REPLACE_VEHICLE, VEH_TRAIN);
}

View File

@ -117,6 +117,8 @@ private:
/* Columns in the group list */
enum ListColumns {
VGC_NAME, ///< Group name.
VGC_PROTECT, ///< Autoreplace protect icon.
VGC_AUTOREPLACE, ///< Autoreplace active icon.
VGC_PROFIT, ///< Profit icon.
VGC_NUMBER, ///< Number of vehicles in the group.
@ -186,6 +188,12 @@ private:
this->column_size[VGC_NAME].width = max(170u, this->column_size[VGC_NAME].width);
this->tiny_step_height = this->column_size[VGC_NAME].height;
this->column_size[VGC_PROTECT] = GetSpriteSize(SPR_GROUP_REPLACE_PROTECT);
this->tiny_step_height = max(this->tiny_step_height, this->column_size[VGC_PROTECT].height);
this->column_size[VGC_AUTOREPLACE] = GetSpriteSize(SPR_GROUP_REPLACE_ACTIVE);
this->tiny_step_height = max(this->tiny_step_height, this->column_size[VGC_AUTOREPLACE].height);
this->column_size[VGC_PROFIT].width = 0;
this->column_size[VGC_PROFIT].height = 0;
static const SpriteID profit_sprites[] = {SPR_PROFIT_NA, SPR_PROFIT_NEGATIVE, SPR_PROFIT_SOME, SPR_PROFIT_LOT};
@ -203,6 +211,8 @@ private:
return WD_FRAMERECT_LEFT + 8 +
this->column_size[VGC_NAME].width + 8 +
this->column_size[VGC_PROTECT].width + 2 +
this->column_size[VGC_AUTOREPLACE].width + 2 +
this->column_size[VGC_PROFIT].width + 2 +
this->column_size[VGC_NUMBER].width + 2 +
WD_FRAMERECT_RIGHT;
@ -214,8 +224,9 @@ private:
* @param left Left of the row.
* @param right Right of the row.
* @param g_id Group to list.
* @param protection Whether autoreplace protection is set.
*/
void DrawGroupInfo(int y, int left, int right, GroupID g_id) const
void DrawGroupInfo(int y, int left, int right, GroupID g_id, bool protection = false) const
{
/* draw the selected group in white, else we draw it in black */
TextColour colour = g_id == this->vli.index ? TC_WHITE : TC_BLACK;
@ -235,8 +246,16 @@ private:
int x = rtl ? right - WD_FRAMERECT_RIGHT - 8 - this->column_size[VGC_NAME].width + 1 : left + WD_FRAMERECT_LEFT + 8;
DrawString(x, x + this->column_size[VGC_NAME].width - 1, y + (this->tiny_step_height - this->column_size[VGC_NAME].height) / 2, str, colour);
/* draw autoreplace protection */
x = rtl ? x - 8 - this->column_size[VGC_PROTECT].width : x + 8 + this->column_size[VGC_NAME].width;
if (protection) DrawSprite(SPR_GROUP_REPLACE_PROTECT, PAL_NONE, x, y + (this->tiny_step_height - this->column_size[VGC_PROTECT].height) / 2);
/* draw autoreplace status */
x = rtl ? x - 2 - this->column_size[VGC_AUTOREPLACE].width : x + 2 + this->column_size[VGC_PROTECT].width;
if (stats.autoreplace_defined) DrawSprite(SPR_GROUP_REPLACE_ACTIVE, stats.autoreplace_finished ? PALETTE_CRASH : PAL_NONE, x, y + (this->tiny_step_height - this->column_size[VGC_AUTOREPLACE].height) / 2);
/* draw the profit icon */
x = rtl ? x - 8 - this->column_size[VGC_PROFIT].width : x + 8 + this->column_size[VGC_NAME].width;
x = rtl ? x - 2 - this->column_size[VGC_PROFIT].width : x + 2 + this->column_size[VGC_AUTOREPLACE].width;
SpriteID spr;
if (stats.num_profit_vehicle == 0) {
spr = SPR_PROFIT_NA;
@ -489,7 +508,7 @@ public:
assert(g->owner == this->owner);
DrawGroupInfo(y1, r.left, r.right, g->index);
DrawGroupInfo(y1, r.left, r.right, g->index, g->replace_protection);
y1 += this->tiny_step_height;
}

View File

@ -56,7 +56,7 @@ static const SpriteID SPR_LARGE_SMALL_WINDOW = 682;
/** Extra graphic spritenumbers */
static const SpriteID SPR_OPENTTD_BASE = 4896;
static const uint16 OPENTTD_SPRITE_COUNT = 160;
static const uint16 OPENTTD_SPRITE_COUNT = 162;
/* Halftile-selection sprites */
static const SpriteID SPR_HALFTILE_SELECTION_FLAT = SPR_OPENTTD_BASE;
@ -123,6 +123,8 @@ static const SpriteID SPR_PROFIT_LOT = SPR_OPENTTD_BASE + 157;
static const SpriteID SPR_UNREAD_NEWS = SPR_OPENTTD_BASE + 158;
static const SpriteID SPR_EXCLUSIVE_TRANSPORT = SPR_OPENTTD_BASE + 159;
static const SpriteID SPR_GROUP_REPLACE_PROTECT = SPR_OPENTTD_BASE + 160;
static const SpriteID SPR_GROUP_REPLACE_ACTIVE = SPR_OPENTTD_BASE + 161;
static const SpriteID SPR_GROUP_CREATE_TRAIN = SPR_OPENTTD_BASE + 114;
static const SpriteID SPR_GROUP_CREATE_ROADVEH = SPR_OPENTTD_BASE + 115;

View File

@ -680,6 +680,7 @@ void Vehicle::PreDestructor()
if (this->IsEngineCountable()) {
GroupStatistics::CountEngine(this, -1);
if (this->IsPrimaryVehicle()) GroupStatistics::CountVehicle(this, -1);
GroupStatistics::UpdateAutoreplace(this->owner);
if (this->owner == _local_company) InvalidateAutoreplaceWindow(this->engine_type, this->group_id);
DeleteGroupHighlightOfVehicle(this);

View File

@ -143,6 +143,7 @@ CommandCost CmdBuildVehicle(TileIndex tile, DoCommandFlag flags, uint32 p1, uint
}
GroupStatistics::CountEngine(v, 1);
GroupStatistics::UpdateAutoreplace(_current_company);
if (v->IsPrimaryVehicle()) {
GroupStatistics::CountVehicle(v, 1);