(svn r3239) - Codechange: Introduce and use helper functions for engine replacement code.
This commit is contained in:
		@@ -1469,7 +1469,7 @@ static void AircraftEventHandler_HeliTakeOff(Vehicle *v, const AirportFTAClass *
 | 
			
		||||
 | 
			
		||||
	// 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 @@ static void AircraftEventHandler_Landing(Vehicle *v, const AirportFTAClass *Airp
 | 
			
		||||
	// 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;
 | 
			
		||||
 
 | 
			
		||||
@@ -1277,11 +1277,7 @@ bool AfterLoadGame(uint version)
 | 
			
		||||
	 *  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;
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										6
									
								
								player.h
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								player.h
									
									
									
									
									
								
							@@ -263,4 +263,10 @@ void LoadFromHighScore(void);
 | 
			
		||||
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 */
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										68
									
								
								players.c
									
									
									
									
									
								
							
							
						
						
									
										68
									
								
								players.c
									
									
									
									
									
								
							@@ -465,7 +465,6 @@ static Player *AllocatePlayer(void)
 | 
			
		||||
Player *DoStartupNewPlayer(bool is_ai)
 | 
			
		||||
{
 | 
			
		||||
	Player *p;
 | 
			
		||||
	int i;
 | 
			
		||||
 | 
			
		||||
	p = AllocatePlayer();
 | 
			
		||||
	if (p == NULL)
 | 
			
		||||
@@ -488,9 +487,7 @@ Player *DoStartupNewPlayer(bool is_ai)
 | 
			
		||||
	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 @@ int32 CmdReplaceVehicle(int x, int y, uint32 flags, uint32 p1, uint32 p2)
 | 
			
		||||
				// 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 @@ void LoadFromHighScore(void)
 | 
			
		||||
	 _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),
 | 
			
		||||
 
 | 
			
		||||
@@ -1618,7 +1618,8 @@ static int32 ReplaceVehicle(Vehicle **w, byte flags)
 | 
			
		||||
	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 @@ static void MaybeReplaceVehicle(Vehicle *v)
 | 
			
		||||
			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;
 | 
			
		||||
			}
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -421,7 +421,7 @@ static void train_engine_drawing_loop(int *x, int *y, int *pos, int *sel, int *s
 | 
			
		||||
		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 @@ static void SetupScrollStuffForReplaceWindow(Window *w)
 | 
			
		||||
				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 @@ static void SetupScrollStuffForReplaceWindow(Window *w)
 | 
			
		||||
 | 
			
		||||
			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 @@ static void SetupScrollStuffForReplaceWindow(Window *w)
 | 
			
		||||
 | 
			
		||||
			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 @@ static void SetupScrollStuffForReplaceWindow(Window *w)
 | 
			
		||||
 | 
			
		||||
			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 @@ static void DrawEngineArrayInReplaceWindow(Window *w, int x, int y, int x2, int
 | 
			
		||||
 | 
			
		||||
				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 @@ static void DrawEngineArrayInReplaceWindow(Window *w, int x, int y, int x2, int
 | 
			
		||||
 | 
			
		||||
				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 @@ static void DrawEngineArrayInReplaceWindow(Window *w, int x, int y, int x2, int
 | 
			
		||||
 | 
			
		||||
				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 @@ static void ReplaceVehicleWndProc(Window *w, WindowEvent *e)
 | 
			
		||||
				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 @@ static void ReplaceVehicleWndProc(Window *w, WindowEvent *e)
 | 
			
		||||
				//    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 @@ static void ReplaceVehicleWndProc(Window *w, WindowEvent *e)
 | 
			
		||||
 | 
			
		||||
				// 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);
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user