changeset 18160:9fd018e89254 draft

(svn r22985) -Feature: Display autoreplace status in group GUI.
author frosch <frosch@openttd.org>
date Mon, 03 Oct 2011 17:26:37 +0000
parents de0749262352
children c9c87f3f8e9f
files src/autoreplace_cmd.cpp src/economy.cpp src/group.h src/group_cmd.cpp src/group_gui.cpp src/table/sprites.h src/vehicle.cpp src/vehicle_cmd.cpp
diffstat 8 files changed, 78 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/src/autoreplace_cmd.cpp
+++ b/src/autoreplace_cmd.cpp
@@ -754,6 +754,7 @@
 		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;
--- a/src/economy.cpp
+++ b/src/economy.cpp
@@ -419,6 +419,8 @@
 				}
 			}
 		}
+
+		if (new_owner != INVALID_OWNER) GroupStatistics::UpdateAutoreplace(new_owner);
 	}
 
 	/*  Change ownership of tiles */
--- a/src/group.h
+++ b/src/group.h
@@ -26,6 +26,9 @@
 	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 @@
 		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 @@
 
 	static void UpdateProfits();
 	static void UpdateAfterLoad();
+	static void UpdateAutoreplace(CompanyID company);
 };
 
 /** Group data. */
--- a/src/group_cmd.cpp
+++ b/src/group_cmd.cpp
@@ -125,6 +125,10 @@
 		GroupStatistics::CountEngine(v, 1);
 		if (v->IsPrimaryVehicle()) GroupStatistics::CountVehicle(v, 1);
 	}
+
+	FOR_ALL_COMPANIES(c) {
+		GroupStatistics::UpdateAutoreplace(c->index);
+	}
 }
 
 /**
@@ -203,6 +207,37 @@
 }
 
 /**
+ * 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
  * @param v     Vehicle we have to update
@@ -400,6 +435,7 @@
 		}
 
 		GroupStatistics::CountVehicle(v, 1);
+		GroupStatistics::UpdateAutoreplace(v->owner);
 
 		/* Update the Replace Vehicle Windows */
 		SetWindowDirty(WC_REPLACE_VEHICLE, v->type);
@@ -544,6 +580,7 @@
 	}
 
 	/* Update the Replace Vehicle Windows */
+	GroupStatistics::UpdateAutoreplace(v->owner);
 	SetWindowDirty(WC_REPLACE_VEHICLE, VEH_TRAIN);
 }
 
@@ -567,6 +604,7 @@
 	}
 
 	/* Update the Replace Vehicle Windows */
+	GroupStatistics::UpdateAutoreplace(v->owner);
 	SetWindowDirty(WC_REPLACE_VEHICLE, VEH_TRAIN);
 }
 
--- a/src/group_gui.cpp
+++ b/src/group_gui.cpp
@@ -117,6 +117,8 @@
 	/* 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 @@
 		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 @@
 
 		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 @@
 	 * @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 @@
 		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 @@
 
 					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;
 				}
--- a/src/table/sprites.h
+++ b/src/table/sprites.h
@@ -56,7 +56,7 @@
 
 /** 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_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;
--- a/src/vehicle.cpp
+++ b/src/vehicle.cpp
@@ -680,6 +680,7 @@
 	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);
--- a/src/vehicle_cmd.cpp
+++ b/src/vehicle_cmd.cpp
@@ -143,6 +143,7 @@
 		}
 
 		GroupStatistics::CountEngine(v, 1);
+		GroupStatistics::UpdateAutoreplace(_current_company);
 
 		if (v->IsPrimaryVehicle()) {
 			GroupStatistics::CountVehicle(v, 1);