(svn r3239) - Codechange: Introduce and use helper functions for engine replacement code.
authorpeter1138
Sat, 26 Nov 2005 16:41:14 +0000
changeset 2697 c7b1a950c4cf
parent 2696 309dedc426e5
child 2698 9a99d68de3ca
(svn r3239) - Codechange: Introduce and use helper functions for engine replacement code.
aircraft_cmd.c
openttd.c
player.h
players.c
vehicle.c
vehicle_gui.c
--- a/aircraft_cmd.c	Sat Nov 26 16:18:15 2005 +0000
+++ b/aircraft_cmd.c	Sat Nov 26 16:41:14 2005 +0000
@@ -1469,7 +1469,7 @@
 
 	// check if the aircraft needs to be replaced or renewed and send it to a hangar if needed
 	if (v->owner == _local_player && (
-				p->engine_replacement[v->engine_type] != INVALID_ENGINE ||
+				EngineHasReplacement(p, v->engine_type) ||
 				(p->engine_renew && v->age - v->max_age > p->engine_renew_months * 30)
 			)) {
 		_current_player = _local_player;
@@ -1533,7 +1533,7 @@
 	// check if the aircraft needs to be replaced or renewed and send it to a hangar if needed
 	if (v->current_order.type != OT_GOTO_DEPOT && v->owner == _local_player) {
 		// only the vehicle owner needs to calculate the rest (locally)
-		if ((p->engine_replacement[v->engine_type] != INVALID_ENGINE) ||
+		if (EngineHasReplacement(p, v->engine_type) ||
 			(p->engine_renew && v->age - v->max_age > (p->engine_renew_months * 30))) {
 			// send the aircraft to the hangar at next airport (bit 17 set)
 			_current_player = _local_player;
--- a/openttd.c	Sat Nov 26 16:18:15 2005 +0000
+++ b/openttd.c	Sat Nov 26 16:41:14 2005 +0000
@@ -1277,11 +1277,7 @@
 	 *  of course, we do need to initialize them for older savegames. */
 	if (CheckSavegameVersion(16)) {
 		FOR_ALL_PLAYERS(p) {
-			EngineID i;
-
-			for (i = 0; i < TOTAL_NUM_ENGINES; i++) {
-				p->engine_replacement[i] = INVALID_ENGINE;
-			}
+			InitialiseEngineReplacement(p);
 			p->engine_renew = false;
 			p->engine_renew_months = -6;
 			p->engine_renew_money = 100000;
--- a/player.h	Sat Nov 26 16:18:15 2005 +0000
+++ b/player.h	Sat Nov 26 16:41:14 2005 +0000
@@ -263,4 +263,10 @@
 int8 SaveHighScoreValue(const Player *p);
 int8 SaveHighScoreValueNetwork(void);
 
+void InitialiseEngineReplacement(Player *p);
+EngineID EngineReplacement(const Player *p, EngineID engine);
+bool EngineHasReplacement(const Player *p, EngineID engine);
+int32 AddEngineReplacement(Player *p, EngineID old_engine, EngineID new_engine, uint32 flags);
+int32 RemoveEngineReplacement(Player *p, EngineID engine, uint32 flags);
+
 #endif /* PLAYER_H */
--- a/players.c	Sat Nov 26 16:18:15 2005 +0000
+++ b/players.c	Sat Nov 26 16:41:14 2005 +0000
@@ -465,7 +465,6 @@
 Player *DoStartupNewPlayer(bool is_ai)
 {
 	Player *p;
-	int i;
 
 	p = AllocatePlayer();
 	if (p == NULL)
@@ -488,9 +487,7 @@
 	p->face = Random();
 
 	/* Engine renewal settings */
-	for (i = 0; i < TOTAL_NUM_ENGINES; i++)
-		p->engine_replacement[i] = INVALID_ENGINE;
-
+	InitialiseEngineReplacement(p);
 	p->renew_keep_length = false;
 	p->engine_renew = false;
 	p->engine_renew_months = -6;
@@ -731,10 +728,10 @@
 				// make sure that the player can actually buy the new engine
 				if (!HASBIT(GetEngine(new_engine_type)->player_avail, _current_player))
 					return CMD_ERROR;
-			}
 
-			if (flags & DC_EXEC) {
-				p->engine_replacement[old_engine_type] = new_engine_type;
+				return AddEngineReplacement(p, old_engine_type, new_engine_type, flags);
+			} else {
+				return RemoveEngineReplacement(p, old_engine_type, flags);
 			}
 		} break;
 		case 4:
@@ -1100,6 +1097,63 @@
 	 _patches.ending_date = 2051;
 }
 
+void InitialiseEngineReplacement(Player *p)
+{
+	EngineID engine;
+
+	for (engine = 0; engine < TOTAL_NUM_ENGINES; engine++)
+		p->engine_replacement[engine] = INVALID_ENGINE;
+}
+
+/**
+ * Retrieve the engine replacement for the given player and original engine type.
+ * @param p Player.
+ * @param engine Engine type.
+ * @return Assigned replacement engine.
+ */
+EngineID EngineReplacement(const Player *p, EngineID engine)
+{
+	return p->engine_replacement[engine];
+}
+
+/**
+ * Check if an engine has a replacement set up.
+ * @param p Player.
+ * @param engine Engine type.
+ * @return True if there is a replacement for the original engine type.
+ */
+bool EngineHasReplacement(const Player *p, EngineID engine)
+{
+	return EngineReplacement(p, engine) != INVALID_ENGINE;
+}
+
+/**
+ * Add an engine replacement for the player.
+ * @param p Player.
+ * @param old_engine The original engine type.
+ * @param new_engine The replacement engine type.
+ * @param flags The calling command flags.
+ * @return 0 on success, CMD_ERROR on failure.
+ */
+int32 AddEngineReplacement(Player *p, EngineID old_engine, EngineID new_engine, uint32 flags)
+{
+	if (flags & DC_EXEC) p->engine_replacement[old_engine] = new_engine;
+	return 0;
+}
+
+/**
+ * Remove an engine replacement for the player.
+ * @param p Player.
+ * @param engine The original engine type.
+ * @param flags The calling command flags.
+ * @return 0 on success, CMD_ERROR on failure.
+ */
+int32 RemoveEngineReplacement(Player *p, EngineID engine, uint32 flags)
+{
+	if (flags & DC_EXEC) p->engine_replacement[engine] = INVALID_ENGINE;
+	return 0;
+}
+
 // Save/load of players
 static const SaveLoad _player_desc[] = {
 	SLE_VAR(Player,name_2,					SLE_UINT32),
--- a/vehicle.c	Sat Nov 26 16:18:15 2005 +0000
+++ b/vehicle.c	Sat Nov 26 16:41:14 2005 +0000
@@ -1618,7 +1618,8 @@
 	bool new_front = false;
 	Vehicle *new_v = NULL;
 
-	new_engine_type = p->engine_replacement[old_v->engine_type] == INVALID_ENGINE ? old_v->engine_type : p->engine_replacement[old_v->engine_type];
+	new_engine_type = EngineReplacement(p, old_v->engine_type);
+	if (new_engine_type == INVALID_ENGINE) new_engine_type = old_v->engine_type;
 
 	cost = DoCommand(old_v->x_pos, old_v->y_pos, new_engine_type, 1, flags, CMD_BUILD_VEH(old_v->type));
 	if (CmdFailed(cost)) return cost;
@@ -1722,7 +1723,7 @@
 			if (!p->engine_renew ||
 					w->age - w->max_age < (p->engine_renew_months * 30) || // replace if engine is too old
 					w->max_age == 0) { // rail cars got a max age of 0
-				if (p->engine_replacement[w->engine_type] == INVALID_ENGINE) // updates to a new model
+				if (!EngineHasReplacement(p, w->engine_type)) // updates to a new model
 					continue;
 			}
 
--- a/vehicle_gui.c	Sat Nov 26 16:18:15 2005 +0000
+++ b/vehicle_gui.c	Sat Nov 26 16:41:14 2005 +0000
@@ -421,7 +421,7 @@
 		const RailVehicleInfo *rvi = RailVehInfo(i);
 		const EngineInfo *info = &_engine_info[i];
 
-		if (p->engine_replacement[i] == INVALID_ENGINE && _player_num_engines[i] == 0 && show_outdated) continue;
+		if (!EngineHasReplacement(p, i) && _player_num_engines[i] == 0 && show_outdated) continue;
 
 		if (rvi->power == 0 && !show_cars)   // disables display of cars (works since they do not have power)
 			continue;
@@ -480,7 +480,7 @@
 				const EngineInfo *info = &_engine_info[engine_id];
 
 				if (ENGINE_AVAILABLE && RailVehInfo(engine_id)->power && e->railtype == railtype) {
-					if (_player_num_engines[engine_id] > 0 || p->engine_replacement[engine_id] != INVALID_ENGINE) {
+					if (_player_num_engines[engine_id] > 0 || EngineHasReplacement(p, engine_id)) {
 						if (sel[0] == 0) selected_id[0] = engine_id;
 						count++;
 						sel[0]--;
@@ -503,7 +503,7 @@
 
 			do {
 				info = &_engine_info[engine_id];
-				if (_player_num_engines[engine_id] > 0 || p->engine_replacement[engine_id] != INVALID_ENGINE) {
+				if (_player_num_engines[engine_id] > 0 || EngineHasReplacement(p, engine_id)) {
 					if (sel[0] == 0) selected_id[0] = engine_id;
 					count++;
 					sel[0]--;
@@ -536,7 +536,7 @@
 
 			do {
 				info = &_engine_info[engine_id];
-				if (_player_num_engines[engine_id] > 0 || p->engine_replacement[engine_id] != INVALID_ENGINE) {
+				if (_player_num_engines[engine_id] > 0 || EngineHasReplacement(p, engine_id)) {
 					if (sel[0] == 0) selected_id[0] = engine_id;
 					count++;
 					sel[0]--;
@@ -571,7 +571,7 @@
 
 			do {
 				info = &_engine_info[engine_id];
-				if (_player_num_engines[engine_id] > 0 || p->engine_replacement[engine_id] != INVALID_ENGINE) {
+				if (_player_num_engines[engine_id] > 0 || EngineHasReplacement(p, engine_id)) {
 					count++;
 					if (sel[0] == 0) selected_id[0] = engine_id;
 					sel[0]--;
@@ -650,7 +650,7 @@
 
 				do {
 					info = &_engine_info[engine_id];
-					if (_player_num_engines[engine_id] > 0 || p->engine_replacement[engine_id] != INVALID_ENGINE) {
+					if (_player_num_engines[engine_id] > 0 || EngineHasReplacement(p, engine_id)) {
 						if (IS_INT_INSIDE(--pos, -w->vscroll.cap, 0)) {
 							DrawString(x+59, y+2, GetCustomEngineName(engine_id), sel[0]==0 ? 0xC : 0x10);
 							DrawRoadVehEngine(x+29, y+6, engine_id, _player_num_engines[engine_id] > 0 ? SPRITE_PALETTE(PLAYER_SPRITE_COLOR(_local_player)) : PALETTE_CRASH);
@@ -687,7 +687,7 @@
 
 				do {
 					info = &_engine_info[engine_id];
-					if (_player_num_engines[engine_id] > 0 || p->engine_replacement[engine_id] != INVALID_ENGINE) {
+					if (_player_num_engines[engine_id] > 0 || EngineHasReplacement(p, engine_id)) {
 						if (IS_INT_INSIDE(--pos, -w->vscroll.cap, 0)) {
 							DrawString(x+75, y+7, GetCustomEngineName(engine_id), sel[0]==0 ? 0xC : 0x10);
 							DrawShipEngine(x+35, y+10, engine_id, _player_num_engines[engine_id] > 0 ? SPRITE_PALETTE(PLAYER_SPRITE_COLOR(_local_player)) : PALETTE_CRASH);
@@ -722,7 +722,7 @@
 
 				do {
 					info = &_engine_info[engine_id];
-					if (_player_num_engines[engine_id] > 0 || p->engine_replacement[engine_id] != INVALID_ENGINE) {
+					if (_player_num_engines[engine_id] > 0 || EngineHasReplacement(p, engine_id)) {
 						if (sel[0] == 0) selected_id[0] = engine_id;
 						if (IS_INT_INSIDE(--pos, -w->vscroll.cap, 0)) {
 							DrawString(x+62, y+7, GetCustomEngineName(engine_id), sel[0]==0 ? 0xC : 0x10);
@@ -835,7 +835,7 @@
 				if (selected_id[0] == -1 ||
 						selected_id[1] == -1 ||
 						selected_id[0] == selected_id[1] ||
-						p->engine_replacement[selected_id[0]] == selected_id[1]) {
+						EngineReplacement(p, selected_id[0]) == selected_id[1]) {
 					SETBIT(w->disabled_state, 4);
 				} else {
 					CLRBIT(w->disabled_state, 4);
@@ -845,7 +845,7 @@
 				//    The left list (existing vehicle) is empty
 				// or The selected vehicle has no replacement set up
 				if (selected_id[0] == -1 ||
-						p->engine_replacement[selected_id[0]] == INVALID_ENGINE) {
+						!EngineHasReplacement(p, selected_id[0])) {
 					SETBIT(w->disabled_state, 6);
 				} else {
 					CLRBIT(w->disabled_state, 6);
@@ -863,10 +863,10 @@
 
 				// sets up the string for the vehicle that is being replaced to
 				if (selected_id[0] != -1) {
-					if (p->engine_replacement[selected_id[0]] == INVALID_ENGINE) {
+					if (!EngineHasReplacement(p, selected_id[0])) {
 						SetDParam(0, STR_NOT_REPLACING);
 					} else {
-						SetDParam(0, GetCustomEngineName(p->engine_replacement[selected_id[0]]));
+						SetDParam(0, GetCustomEngineName(EngineReplacement(p, selected_id[0])));
 					}
 				} else {
 					SetDParam(0, STR_NOT_REPLACING_VEHICLE_SELECTED);