Changeset - r2697:3bb79206c43f
[Not reviewed]
master
0 6 0
peter1138 - 18 years ago 2005-11-26 16:41:14
peter1138@openttd.org
(svn r3239) - Codechange: Introduce and use helper functions for engine replacement code.
6 files changed with 85 insertions and 28 deletions:
0 comments (0 inline, 0 general)
aircraft_cmd.c
Show inline comments
 
@@ -1469,7 +1469,7 @@ static void AircraftEventHandler_HeliTak
 

	
 
	// 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
 
	// 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;
openttd.c
Show inline comments
 
@@ -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;
player.h
Show inline comments
 
@@ -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 */
players.c
Show inline comments
 
@@ -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, ui
 
				// 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),
vehicle.c
Show inline comments
 
@@ -1618,7 +1618,8 @@ static int32 ReplaceVehicle(Vehicle **w,
 
	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 
 
			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;
 
			}
 

	
vehicle_gui.c
Show inline comments
 
@@ -421,7 +421,7 @@ static void train_engine_drawing_loop(in
 
		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 SetupScrollStuffForReplaceWi
 
				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 SetupScrollStuffForReplaceWi
 

	
 
			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 SetupScrollStuffForReplaceWi
 

	
 
			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 SetupScrollStuffForReplaceWi
 

	
 
			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 DrawEngineArrayInReplaceWind
 

	
 
				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 DrawEngineArrayInReplaceWind
 

	
 
				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 DrawEngineArrayInReplaceWind
 

	
 
				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
 
				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
 
				//    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
 

	
 
				// 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);
0 comments (0 inline, 0 general)