changeset 11913:f67b6243e05a draft

(svn r16315) -Codechange: move the autorenew settings to a new CompanySettings struct
author yexo <yexo@openttd.org>
date Fri, 15 May 2009 23:55:06 +0000
parents 9106ff52ea5c
children 08d3a74525b2
files src/ai/api/ai_company.cpp src/ai/api/ai_group.cpp src/aircraft_cmd.cpp src/autoreplace_cmd.cpp src/autoreplace_gui.cpp src/company_base.h src/company_cmd.cpp src/openttd.cpp src/saveload/afterload.cpp src/saveload/company_sl.cpp src/settings.cpp src/settings_gui.cpp src/settings_type.h src/table/settings.h src/vehicle.cpp
diffstat 15 files changed, 68 insertions(+), 69 deletions(-) [+]
line wrap: on
line diff
--- a/src/ai/api/ai_company.cpp
+++ b/src/ai/api/ai_company.cpp
@@ -156,7 +156,7 @@
 	company = ResolveCompanyID(company);
 	if (company == COMPANY_INVALID) return false;
 
-	return ::GetCompany((CompanyID)company)->engine_renew;
+	return ::GetCompany((CompanyID)company)->settings.engine_renew;
 }
 
 /* static */ bool AICompany::SetAutoRenewMonths(int16 months)
@@ -169,7 +169,7 @@
 	company = ResolveCompanyID(company);
 	if (company == COMPANY_INVALID) return 0;
 
-	return ::GetCompany((CompanyID)company)->engine_renew_months;
+	return ::GetCompany((CompanyID)company)->settings.engine_renew_months;
 }
 
 /* static */ bool AICompany::SetAutoRenewMoney(uint32 money)
@@ -182,5 +182,5 @@
 	company = ResolveCompanyID(company);
 	if (company == COMPANY_INVALID) return 0;
 
-	return ::GetCompany((CompanyID)company)->engine_renew_money;
+	return ::GetCompany((CompanyID)company)->settings.engine_renew_money;
 }
--- a/src/ai/api/ai_group.cpp
+++ b/src/ai/api/ai_group.cpp
@@ -100,7 +100,7 @@
 
 /* static */ bool AIGroup::HasWagonRemoval()
 {
-	return ::GetCompany(_current_company)->renew_keep_length;
+	return ::GetCompany(_current_company)->settings.renew_keep_length;
 }
 
 /* static */ bool AIGroup::SetAutoReplace(GroupID group_id, EngineID engine_id_old, EngineID engine_id_new)
--- a/src/aircraft_cmd.cpp
+++ b/src/aircraft_cmd.cpp
@@ -1448,7 +1448,7 @@
 		return false;
 	}
 
-	if (c->money < (c->engine_renew_money + (2 * DoCommand(0, new_engine, 0, DC_QUERY_COST, CMD_BUILD_AIRCRAFT).GetCost()))) {
+	if (c->money < (c->settings.engine_renew_money + (2 * DoCommand(0, new_engine, 0, DC_QUERY_COST, CMD_BUILD_AIRCRAFT).GetCost()))) {
 		/* We lack enough money to request the replacement right away.
 		 * We want 2*(the price of the new vehicle) and not looking at the value of the vehicle we are going to sell.
 		 * The reason is that we don't want to send a whole lot of vehicles to the hangars when we only have enough money to replace a single one.
--- a/src/autoreplace_cmd.cpp
+++ b/src/autoreplace_cmd.cpp
@@ -624,7 +624,7 @@
 	}
 
 	const Company *c = GetCompany(_current_company);
-	bool wagon_removal = c->renew_keep_length;
+	bool wagon_removal = c->settings.renew_keep_length;
 
 	/* Test whether any replacement is set, before issuing a whole lot of commands that would end in nothing changed */
 	Vehicle *w = v;
--- a/src/autoreplace_gui.cpp
+++ b/src/autoreplace_gui.cpp
@@ -267,7 +267,7 @@
 
 		if (this->window_number == VEH_TRAIN) {
 			/* set on/off for renew_keep_length */
-			SetDParam(1, c->renew_keep_length ? STR_CONFIG_SETTING_ON : STR_CONFIG_SETTING_OFF);
+			SetDParam(1, c->settings.renew_keep_length ? STR_CONFIG_SETTING_ON : STR_CONFIG_SETTING_OFF);
 
 			/* set wagon/engine button */
 			SetDParam(2, this->wagon_btnstate ? STR_ENGINES : STR_WAGONS);
@@ -347,7 +347,7 @@
 			}
 
 			case RVW_WIDGET_TRAIN_WAGONREMOVE_TOGGLE: // toggle renew_keep_length
-				DoCommandP(0, 5, GetCompany(_local_company)->renew_keep_length ? 0 : 1, CMD_SET_AUTOREPLACE);
+				DoCommandP(0, 5, GetCompany(_local_company)->settings.renew_keep_length ? 0 : 1, CMD_SET_AUTOREPLACE);
 				break;
 
 			case RVW_WIDGET_START_REPLACE: { // Start replacing
--- a/src/company_base.h
+++ b/src/company_base.h
@@ -15,6 +15,7 @@
 #include "autoreplace_type.h"
 #include "economy_type.h"
 #include "tile_type.h"
+#include "settings_type.h"
 
 struct CompanyEconomyEntry {
 	Money income;
@@ -78,10 +79,7 @@
 	CompanyEconomyEntry cur_economy;
 	CompanyEconomyEntry old_economy[24];
 	EngineRenewList engine_renew_list; ///< Defined later
-	bool engine_renew;
-	bool renew_keep_length;
-	int16 engine_renew_months;
-	uint32 engine_renew_money;
+	CompanySettings settings;          ///< settings specific for each company
 	uint16 *num_engines; ///< caches the number of engines of each type the company owns (no need to save this)
 
 	inline bool IsValid() const { return this->name_1 != 0; }
--- a/src/company_cmd.cpp
+++ b/src/company_cmd.cpp
@@ -78,9 +78,7 @@
 	/* Do not update the settings if we are in the intro GUI */
 	if (IsValidCompanyID(new_company) && _game_mode != GM_MENU) {
 		const Company *c = GetCompany(new_company);
-		_settings_client.gui.autorenew        = c->engine_renew;
-		_settings_client.gui.autorenew_months = c->engine_renew_months;
-		_settings_client.gui.autorenew_money  = c->engine_renew_money;
+		_settings_client.company = c->settings;
 		InvalidateWindow(WC_GAME_OPTIONS, 0);
 	}
 
@@ -568,12 +566,12 @@
 	Company *c = GetCompany(_current_company);
 	switch (GB(p1, 0, 3)) {
 		case 0:
-			if (c->engine_renew == HasBit(p2, 0)) return CMD_ERROR;
+			if (c->settings.engine_renew == HasBit(p2, 0)) return CMD_ERROR;
 
 			if (flags & DC_EXEC) {
-				c->engine_renew = HasBit(p2, 0);
+				c->settings.engine_renew = HasBit(p2, 0);
 				if (IsLocalCompany()) {
-					_settings_client.gui.autorenew = c->engine_renew;
+					_settings_client.company.engine_renew = c->settings.engine_renew;
 					InvalidateWindow(WC_GAME_OPTIONS, 0);
 				}
 			}
@@ -581,12 +579,12 @@
 
 		case 1:
 			if (Clamp((int16)p2, -12, 12) != (int16)p2) return CMD_ERROR;
-			if (c->engine_renew_months == (int16)p2) return CMD_ERROR;
+			if (c->settings.engine_renew_months == (int16)p2) return CMD_ERROR;
 
 			if (flags & DC_EXEC) {
-				c->engine_renew_months = (int16)p2;
+				c->settings.engine_renew_months = (int16)p2;
 				if (IsLocalCompany()) {
-					_settings_client.gui.autorenew_months = c->engine_renew_months;
+					_settings_client.company.engine_renew_months = c->settings.engine_renew_months;
 					InvalidateWindow(WC_GAME_OPTIONS, 0);
 				}
 			}
@@ -594,12 +592,12 @@
 
 		case 2:
 			if (ClampU(p2, 0, 2000000) != p2) return CMD_ERROR;
-			if (c->engine_renew_money == p2) return CMD_ERROR;
+			if (c->settings.engine_renew_money == p2) return CMD_ERROR;
 
 			if (flags & DC_EXEC) {
-				c->engine_renew_money = p2;
+				c->settings.engine_renew_money = p2;
 				if (IsLocalCompany()) {
-					_settings_client.gui.autorenew_money = c->engine_renew_money;
+					_settings_client.company.engine_renew_money = c->settings.engine_renew_money;
 					InvalidateWindow(WC_GAME_OPTIONS, 0);
 				}
 			}
@@ -630,24 +628,24 @@
 			if (ClampU(p2, 0, 2000000) != p2) return CMD_ERROR;
 
 			if (flags & DC_EXEC) {
-				c->engine_renew = HasBit(p1, 15);
-				c->engine_renew_months = (int16)GB(p1, 16, 16);
-				c->engine_renew_money = p2;
+				c->settings.engine_renew = HasBit(p1, 15);
+				c->settings.engine_renew_months = (int16)GB(p1, 16, 16);
+				c->settings.engine_renew_money = p2;
 
 				if (IsLocalCompany()) {
-					_settings_client.gui.autorenew = c->engine_renew;
-					_settings_client.gui.autorenew_months = c->engine_renew_months;
-					_settings_client.gui.autorenew_money = c->engine_renew_money;
+					_settings_client.company.engine_renew = c->settings.engine_renew;
+					_settings_client.company.engine_renew_months = c->settings.engine_renew_months;
+					_settings_client.company.engine_renew_money = c->settings.engine_renew_money;
 					InvalidateWindow(WC_GAME_OPTIONS, 0);
 				}
 			}
 			break;
 
 		case 5:
-			if (c->renew_keep_length == HasBit(p2, 0)) return CMD_ERROR;
+			if (c->settings.renew_keep_length == HasBit(p2, 0)) return CMD_ERROR;
 
 			if (flags & DC_EXEC) {
-				c->renew_keep_length = HasBit(p2, 0);
+				c->settings.renew_keep_length = HasBit(p2, 0);
 				if (IsLocalCompany()) {
 					InvalidateWindow(WC_REPLACE_VEHICLE, VEH_TRAIN);
 				}
@@ -750,8 +748,8 @@
 			/* This is the client (or non-dedicated server) who wants a new company */
 			if (cid == _network_own_client_id) {
 				/* Create p1 and p2 here because SetLocalCompany resets the gui.autorenew* settings. */
-				uint32 p1 = (_settings_client.gui.autorenew << 15 ) | (_settings_client.gui.autorenew_months << 16) | 4;
-				uint32 p2 = _settings_client.gui.autorenew_money;
+				uint32 p1 = (_settings_client.company.engine_renew << 15 ) | (_settings_client.company.engine_renew_months << 16) | 4;
+				uint32 p2 = _settings_client.company.engine_renew_money;
 				assert(_local_company == COMPANY_SPECTATOR);
 				SetLocalCompany(c->index);
 				if (!StrEmpty(_settings_client.network.default_company_pass)) {
--- a/src/openttd.cpp
+++ b/src/openttd.cpp
@@ -755,15 +755,12 @@
 	DoStartupNewCompany(false);
 
 	Company *c = GetCompany(COMPANY_FIRST);
-	c->engine_renew = _settings_client.gui.autorenew;
-	c->engine_renew_months = _settings_client.gui.autorenew_months;
-	c->engine_renew_money = _settings_client.gui.autorenew_money;
+	c->settings = _settings_client.company;
 
 	IConsoleCmdExec("exec scripts/game_start.scr 0");
 
 	SetLocalCompany(COMPANY_FIRST);
 	_current_company = _local_company;
-	DoCommandP(0, (_settings_client.gui.autorenew << 15 ) | (_settings_client.gui.autorenew_months << 16) | 4, _settings_client.gui.autorenew_money, CMD_SET_AUTOREPLACE);
 
 	InitializeRailGUI();
 
@@ -854,7 +851,8 @@
 
 	SetLocalCompany(COMPANY_FIRST);
 	_current_company = _local_company;
-	DoCommandP(0, (_settings_client.gui.autorenew << 15 ) | (_settings_client.gui.autorenew_months << 16) | 4, _settings_client.gui.autorenew_money, CMD_SET_AUTOREPLACE);
+	Company *c = GetCompany(COMPANY_FIRST);
+	c->settings = _settings_client.company;
 
 	MarkWholeScreenDirty();
 }
--- a/src/saveload/afterload.cpp
+++ b/src/saveload/afterload.cpp
@@ -661,10 +661,10 @@
 	 *  of course, we do need to initialize them for older savegames. */
 	if (CheckSavegameVersion(16)) {
 		FOR_ALL_COMPANIES(c) {
-			c->engine_renew_list   = NULL;
-			c->engine_renew        = false;
-			c->engine_renew_months = -6;
-			c->engine_renew_money  = 100000;
+			c->engine_renew_list            = NULL;
+			c->settings.engine_renew        = false;
+			c->settings.engine_renew_months = 6;
+			c->settings.engine_renew_money  = 100000;
 		}
 
 		/* When loading a game, _local_company is not yet set to the correct value.
@@ -675,9 +675,7 @@
 		 */
 		if (!_network_dedicated && IsValidCompanyID(COMPANY_FIRST)) {
 			c = GetCompany(COMPANY_FIRST);
-			c->engine_renew        = _settings_client.gui.autorenew;
-			c->engine_renew_months = _settings_client.gui.autorenew_months;
-			c->engine_renew_money  = _settings_client.gui.autorenew_money;
+			c->settings = _settings_client.company;
 		}
 	}
 
@@ -950,7 +948,7 @@
 	 * replaced, shall keep their old length. In all prior versions, just default
 	 * to false */
 	if (CheckSavegameVersionOldStyle(16, 1)) {
-		FOR_ALL_COMPANIES(c) c->renew_keep_length = false;
+		FOR_ALL_COMPANIES(c) c->settings.renew_keep_length = false;
 	}
 
 	/* In version 17, ground type is moved from m2 to m4 for depots and
--- a/src/saveload/company_sl.cpp
+++ b/src/saveload/company_sl.cpp
@@ -6,6 +6,7 @@
 #include "../company_base.h"
 #include "../company_func.h"
 #include "../company_manager_face.h"
+#include "../settings_type.h"
 
 #include "saveload.h"
 
@@ -133,11 +134,11 @@
 
 	/* Engine renewal settings */
 	SLE_CONDNULL(512, 16, 18),
-	SLE_CONDREF(Company, engine_renew_list,     REF_ENGINE_RENEWS,          19, SL_MAX_VERSION),
-	SLE_CONDVAR(Company, engine_renew,          SLE_BOOL,                   16, SL_MAX_VERSION),
-	SLE_CONDVAR(Company, engine_renew_months,   SLE_INT16,                  16, SL_MAX_VERSION),
-	SLE_CONDVAR(Company, engine_renew_money,    SLE_UINT32,                 16, SL_MAX_VERSION),
-	SLE_CONDVAR(Company, renew_keep_length,     SLE_BOOL,                    2, SL_MAX_VERSION), // added with 16.1, but was blank since 2
+	SLE_CONDREF(Company, engine_renew_list,            REF_ENGINE_RENEWS,   19, SL_MAX_VERSION),
+	SLE_CONDVAR(Company, settings.engine_renew,        SLE_BOOL,            16, SL_MAX_VERSION),
+	SLE_CONDVAR(Company, settings.engine_renew_months, SLE_INT16,           16, SL_MAX_VERSION),
+	SLE_CONDVAR(Company, settings.engine_renew_money,  SLE_UINT32,          16, SL_MAX_VERSION),
+	SLE_CONDVAR(Company, settings.renew_keep_length,   SLE_BOOL,             2, SL_MAX_VERSION),
 
 	/* Reserve extra space in savegame here. (currently 63 bytes) */
 	SLE_CONDNULL(63, 2, SL_MAX_VERSION),
--- a/src/settings.cpp
+++ b/src/settings.cpp
@@ -726,19 +726,19 @@
 
 static bool EngineRenewUpdate(int32 p1)
 {
-	DoCommandP(0, 0, _settings_client.gui.autorenew, CMD_SET_AUTOREPLACE);
+	DoCommandP(0, 0, _settings_client.company.engine_renew, CMD_SET_AUTOREPLACE);
 	return true;
 }
 
 static bool EngineRenewMonthsUpdate(int32 p1)
 {
-	DoCommandP(0, 1, _settings_client.gui.autorenew_months, CMD_SET_AUTOREPLACE);
+	DoCommandP(0, 1, _settings_client.company.engine_renew_months, CMD_SET_AUTOREPLACE);
 	return true;
 }
 
 static bool EngineRenewMoneyUpdate(int32 p1)
 {
-	DoCommandP(0, 2, _settings_client.gui.autorenew_money, CMD_SET_AUTOREPLACE);
+	DoCommandP(0, 2, _settings_client.company.engine_renew_money, CMD_SET_AUTOREPLACE);
 	return true;
 }
 
--- a/src/settings_gui.cpp
+++ b/src/settings_gui.cpp
@@ -1254,9 +1254,9 @@
 static SettingsPage _settings_vehicles_routing_page = {_settings_vehicles_routing, lengthof(_settings_vehicles_routing)};
 
 static SettingEntry _settings_vehicles_autorenew[] = {
-	SettingEntry("gui.autorenew"),
-	SettingEntry("gui.autorenew_months"),
-	SettingEntry("gui.autorenew_money"),
+	SettingEntry("company.engine_renew"),
+	SettingEntry("company.engine_renew_months"),
+	SettingEntry("company.engine_renew_money"),
 };
 /** Autorenew sub-page */
 static SettingsPage _settings_vehicles_autorenew_page = {_settings_vehicles_autorenew, lengthof(_settings_vehicles_autorenew)};
--- a/src/settings_type.h
+++ b/src/settings_type.h
@@ -78,9 +78,6 @@
 	bool   auto_euro;                        ///< automatically switch to euro in 2002
 	byte   drag_signals_density;             ///< many signals density
 	Year   semaphore_build_before;           ///< build semaphore signals automatically before this year
-	bool   autorenew;                        ///< should autorenew be enabled for new companies?
-	int16  autorenew_months;                 ///< how many months from EOL of vehicles should autorenew trigger for new companies?
-	int32  autorenew_money;                  ///< how much money before autorenewing for new companies?
 	byte   news_message_timeout;             ///< how much longer than the news message "age" should we keep the message in the history
 	bool   show_track_reservation;           ///< highlight reserved tracks.
 	uint8  default_signal_type;              ///< the signal type to build by default.
@@ -341,6 +338,14 @@
 	byte   station_spread;                   ///< amount a station may spread
 };
 
+/** Settings that can be set per company. */
+struct CompanySettings {
+	bool engine_renew;                       ///< is autorenew enabled
+	int16 engine_renew_months;               ///< months before/after the maximum vehicle age a vehicle should be renewed
+	uint32 engine_renew_money;               ///< minimum amount of money before autorenew is used
+	bool renew_keep_length;                  ///< sell some wagons if after autoreplace the train is longer than before
+};
+
 /** All settings together for the game. */
 struct GameSettings {
 	DifficultySettings   difficulty;         ///< settings related to the difficulty
@@ -360,6 +365,7 @@
 struct ClientSettings {
 	GUISettings          gui;                ///< settings related to the GUI
 	NetworkSettings      network;            ///< settings related to the network
+	CompanySettings      company;            ///< default values for per-company settings
 };
 
 /** The current settings for this game. */
--- a/src/table/settings.h
+++ b/src/table/settings.h
@@ -547,9 +547,9 @@
 	 SDTC_BOOL(gui.vehicle_income_warn,                  S,  0,  true,                        STR_CONFIG_SETTING_WARN_INCOME_LESS,            NULL),
 	  SDTC_VAR(gui.order_review_system,       SLE_UINT8, S, MS,     2,        0,        2, 0, STR_CONFIG_SETTING_ORDER_REVIEW,                NULL),
 	 SDTC_BOOL(gui.lost_train_warn,                      S,  0,  true,                        STR_CONFIG_SETTING_WARN_LOST_TRAIN,             NULL),
-	 SDTC_BOOL(gui.autorenew,                            S,  0, false,                        STR_CONFIG_SETTING_AUTORENEW_VEHICLE,           EngineRenewUpdate),
-	  SDTC_VAR(gui.autorenew_months,          SLE_INT16, S,  0,     6,      -12,       12, 0, STR_CONFIG_SETTING_AUTORENEW_MONTHS,            EngineRenewMonthsUpdate),
-	  SDTC_VAR(gui.autorenew_money,            SLE_UINT, S, CR,100000,        0,  2000000, 0, STR_CONFIG_SETTING_AUTORENEW_MONEY,             EngineRenewMoneyUpdate),
+	 SDTC_BOOL(company.engine_renew,                     S,  0, false,                        STR_CONFIG_SETTING_AUTORENEW_VEHICLE,           EngineRenewUpdate),
+	  SDTC_VAR(company.engine_renew_months,   SLE_INT16, S,  0,     6,      -12,       12, 0, STR_CONFIG_SETTING_AUTORENEW_MONTHS,            EngineRenewMonthsUpdate),
+	  SDTC_VAR(company.engine_renew_money,     SLE_UINT, S, CR,100000,        0,  2000000, 0, STR_CONFIG_SETTING_AUTORENEW_MONEY,             EngineRenewMoneyUpdate),
 	 SDTC_BOOL(gui.always_build_infrastructure,          S,  0, false,                        STR_CONFIG_SETTING_ALWAYS_BUILD_INFRASTRUCTURE, RedrawScreen),
 	 SDTC_BOOL(gui.new_nonstop,                          S,  0, false,                        STR_CONFIG_SETTING_NONSTOP_BY_DEFAULT,          NULL),
 	  SDTC_VAR(gui.stop_location,             SLE_UINT8, S, MS,     2,        0,        2, 1, STR_CONFIG_SETTING_STOP_LOCATION,               NULL),
--- a/src/vehicle.cpp
+++ b/src/vehicle.cpp
@@ -64,8 +64,8 @@
 	 * argument rather than finding it again. */
 	assert(c == GetCompany(this->owner));
 
-	if (!c->engine_renew) return false;
-	if (this->age - this->max_age < (c->engine_renew_months * 30)) return false;
+	if (!c->settings.engine_renew) return false;
+	if (this->age - this->max_age < (c->settings.engine_renew_months * 30)) return false;
 	if (this->age == 0) return false; // rail cars don't age and lacks a max age
 
 	return true;
@@ -642,9 +642,9 @@
 		int z = v->z_pos;
 
 		const Company *c = GetCompany(_current_company);
-		SubtractMoneyFromCompany(CommandCost(EXPENSES_NEW_VEHICLES, (Money)c->engine_renew_money));
+		SubtractMoneyFromCompany(CommandCost(EXPENSES_NEW_VEHICLES, (Money)c->settings.engine_renew_money));
 		CommandCost res = DoCommand(0, v->index, 0, DC_EXEC, CMD_AUTOREPLACE_VEHICLE);
-		SubtractMoneyFromCompany(CommandCost(EXPENSES_NEW_VEHICLES, -(Money)c->engine_renew_money));
+		SubtractMoneyFromCompany(CommandCost(EXPENSES_NEW_VEHICLES, -(Money)c->settings.engine_renew_money));
 
 		if (!IsLocalCompany()) continue;
 
@@ -906,7 +906,7 @@
 	if (v->Previous() != NULL || v->owner != _local_company || (v->vehstatus & VS_CRASHED) != 0) return;
 
 	/* Don't warn if a renew is active */
-	if (GetCompany(v->owner)->engine_renew && GetEngine(v->engine_type)->company_avail != 0) return;
+	if (GetCompany(v->owner)->settings.engine_renew && GetEngine(v->engine_type)->company_avail != 0) return;
 
 	StringID str;
 	if (age == -DAYS_IN_LEAP_YEAR) {