src/engine.cpp
branchnoai
changeset 9732 f8eb3e208514
parent 9724 b39bc69bb2f2
child 9800 ab08ca2a2018
equal deleted inserted replaced
9731:9b1552d0fd9b 9732:f8eb3e208514
    89 {
    89 {
    90 	uint age = e->age;
    90 	uint age = e->age;
    91 
    91 
    92 	/* Check for early retirement */
    92 	/* Check for early retirement */
    93 	if (e->player_avail != 0 && !_patches.never_expire_vehicles) {
    93 	if (e->player_avail != 0 && !_patches.never_expire_vehicles) {
    94 		uint retire_early = EngInfo(e - _engines)->retire_early;
    94 		int retire_early = EngInfo(e - _engines)->retire_early;
    95 		if (retire_early > 0 && age >= e->duration_phase_1 + e->duration_phase_2 - retire_early * 12) {
    95 		uint retire_early_max_age = max(0, e->duration_phase_1 + e->duration_phase_2 - retire_early * 12);
       
    96 		if (retire_early != 0 && age >= retire_early_max_age) {
    96 			/* Early retirement is enabled and we're past the date... */
    97 			/* Early retirement is enabled and we're past the date... */
    97 			e->player_avail = 0;
    98 			e->player_avail = 0;
    98 			AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
    99 			AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
    99 		}
   100 		}
   100 	}
   101 	}
   188 		SetBit(p->avail_railtypes, rvi->railtype);
   189 		SetBit(p->avail_railtypes, rvi->railtype);
   189 	} else if (e->type == VEH_ROAD) {
   190 	} else if (e->type == VEH_ROAD) {
   190 		SetBit(p->avail_roadtypes, HasBit(EngInfo(eid)->misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD);
   191 		SetBit(p->avail_roadtypes, HasBit(EngInfo(eid)->misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD);
   191 	}
   192 	}
   192 
   193 
   193 	e->preview_player = INVALID_PLAYER;
   194 	e->preview_player_rank = 0xFF;
   194 	if (player == _local_player) {
   195 	if (player == _local_player) {
   195 		AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
   196 		AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
   196 	}
   197 	}
   197 }
   198 }
   198 
   199 
   199 static PlayerID GetBestPlayer(PlayerID pp)
   200 static PlayerID GetBestPlayer(uint8 pp)
   200 {
   201 {
   201 	const Player *p;
   202 	const Player *p;
   202 	int32 best_hist;
   203 	int32 best_hist;
   203 	PlayerID best_player;
   204 	PlayerID best_player;
   204 	uint mask = 0;
   205 	uint mask = 0;
   215 		}
   216 		}
   216 
   217 
   217 		if (best_player == PLAYER_SPECTATOR) return PLAYER_SPECTATOR;
   218 		if (best_player == PLAYER_SPECTATOR) return PLAYER_SPECTATOR;
   218 
   219 
   219 		SetBit(mask, best_player);
   220 		SetBit(mask, best_player);
   220 	} while (pp--, pp != 0);
   221 	} while (--p != 0);
   221 
   222 
   222 	return best_player;
   223 	return best_player;
   223 }
   224 }
   224 
   225 
   225 void EnginesDailyLoop()
   226 void EnginesDailyLoop()
   231 	for (i = 0; i != lengthof(_engines); i++) {
   232 	for (i = 0; i != lengthof(_engines); i++) {
   232 		Engine *e = &_engines[i];
   233 		Engine *e = &_engines[i];
   233 
   234 
   234 		if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) {
   235 		if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) {
   235 			if (e->flags & ENGINE_OFFER_WINDOW_OPEN) {
   236 			if (e->flags & ENGINE_OFFER_WINDOW_OPEN) {
   236 				if (e->preview_player != 0xFF && !--e->preview_wait) {
   237 				if (e->preview_player_rank != 0xFF && !--e->preview_wait) {
   237 					e->flags &= ~ENGINE_OFFER_WINDOW_OPEN;
   238 					e->flags &= ~ENGINE_OFFER_WINDOW_OPEN;
   238 					DeleteWindowById(WC_ENGINE_PREVIEW, i);
   239 					DeleteWindowById(WC_ENGINE_PREVIEW, i);
   239 					e->preview_player++;
   240 					e->preview_player_rank++;
   240 				}
   241 				}
   241 			} else if (e->preview_player != 0xFF) {
   242 			} else if (e->preview_player_rank != 0xFF) {
   242 				PlayerID best_player = GetBestPlayer(e->preview_player);
   243 				PlayerID best_player = GetBestPlayer(e->preview_player_rank);
   243 
   244 
   244 				if (best_player == PLAYER_SPECTATOR) {
   245 				if (best_player == PLAYER_SPECTATOR) {
   245 					e->preview_player = INVALID_PLAYER;
   246 					e->preview_player_rank = 0xFF;
   246 					continue;
   247 					continue;
   247 				}
   248 				}
   248 
   249 
   249 				if (!IsHumanPlayer(best_player)) {
   250 				if (!IsHumanPlayer(best_player)) {
   250 					/* XXX - TTDBUG: TTD has a bug here ???? */
   251 					/* XXX - TTDBUG: TTD has a bug here ???? */
   270 {
   271 {
   271 	Engine *e;
   272 	Engine *e;
   272 
   273 
   273 	if (!IsEngineIndex(p1)) return CMD_ERROR;
   274 	if (!IsEngineIndex(p1)) return CMD_ERROR;
   274 	e = GetEngine(p1);
   275 	e = GetEngine(p1);
   275 	if (GetBestPlayer(e->preview_player) != _current_player) return CMD_ERROR;
   276 	if (GetBestPlayer(e->preview_player_rank) != _current_player) return CMD_ERROR;
   276 
   277 
   277 	if (flags & DC_EXEC) AcceptEnginePreview(p1, _current_player);
   278 	if (flags & DC_EXEC) AcceptEnginePreview(p1, _current_player);
   278 
   279 
   279 	return CommandCost();
   280 	return CommandCost();
   280 }
   281 }
   358 				/* Introduction date has passed.. show introducing dialog to one player. */
   359 				/* Introduction date has passed.. show introducing dialog to one player. */
   359 				e->flags |= ENGINE_EXCLUSIVE_PREVIEW;
   360 				e->flags |= ENGINE_EXCLUSIVE_PREVIEW;
   360 
   361 
   361 				/* Do not introduce new rail wagons */
   362 				/* Do not introduce new rail wagons */
   362 				if (!IsWagon(e - _engines))
   363 				if (!IsWagon(e - _engines))
   363 					e->preview_player = (PlayerID)1; // Give to the player with the highest rating.
   364 					e->preview_player_rank = 1; // Give to the player with the highest rating.
   364 			}
   365 			}
   365 		}
   366 		}
   366 	}
   367 	}
   367 }
   368 }
   368 
   369 
   401 
   402 
   402 	return CommandCost();
   403 	return CommandCost();
   403 }
   404 }
   404 
   405 
   405 
   406 
   406 /*
   407 /** Check if an engine is buildable.
   407  * returns true if an engine is valid, of the specified type, and buildable by
   408  * @param engine index of the engine to check.
   408  * the given player, false otherwise
   409  * @param type   the type the engine should be.
   409  *
   410  * @param player index of the player.
   410  * engine = index of the engine to check
   411  * @return True if an engine is valid, of the specified type, and buildable by
   411  * type   = the type the engine should be of (VEH_xxx)
   412  *              the given player.
   412  * player = index of the player
       
   413  */
   413  */
   414 bool IsEngineBuildable(EngineID engine, byte type, PlayerID player)
   414 bool IsEngineBuildable(EngineID engine, VehicleType type, PlayerID player)
   415 {
   415 {
   416 	const Engine *e;
       
   417 
       
   418 	/* check if it's an engine that is in the engine array */
   416 	/* check if it's an engine that is in the engine array */
   419 	if (!IsEngineIndex(engine)) return false;
   417 	if (!IsEngineIndex(engine)) return false;
   420 
   418 
   421 	e = GetEngine(engine);
   419 	const Engine *e = GetEngine(engine);
   422 
   420 
   423 	/* check if it's an engine of specified type */
   421 	/* check if it's an engine of specified type */
   424 	if (e->type != type) return false;
   422 	if (e->type != type) return false;
   425 
   423 
   426 	/* check if it's available */
   424 	/* check if it's available */
   613 	    SLE_VAR(Engine, duration_phase_2,    SLE_UINT16),
   611 	    SLE_VAR(Engine, duration_phase_2,    SLE_UINT16),
   614 	    SLE_VAR(Engine, duration_phase_3,    SLE_UINT16),
   612 	    SLE_VAR(Engine, duration_phase_3,    SLE_UINT16),
   615 
   613 
   616 	    SLE_VAR(Engine, lifelength,          SLE_UINT8),
   614 	    SLE_VAR(Engine, lifelength,          SLE_UINT8),
   617 	    SLE_VAR(Engine, flags,               SLE_UINT8),
   615 	    SLE_VAR(Engine, flags,               SLE_UINT8),
   618 	    SLE_VAR(Engine, preview_player,      SLE_UINT8),
   616 	    SLE_VAR(Engine, preview_player_rank, SLE_UINT8),
   619 	    SLE_VAR(Engine, preview_wait,        SLE_UINT8),
   617 	    SLE_VAR(Engine, preview_wait,        SLE_UINT8),
   620 	SLE_CONDNULL(1, 0, 44),
   618 	SLE_CONDNULL(1, 0, 44),
   621 	    SLE_VAR(Engine, player_avail,        SLE_UINT8),
   619 	    SLE_VAR(Engine, player_avail,        SLE_UINT8),
   622 	SLE_CONDSTR(Engine, name,                SLE_STR, 0,                 84, SL_MAX_VERSION),
   620 	SLE_CONDSTR(Engine, name,                SLE_STR, 0,                 84, SL_MAX_VERSION),
   623 
   621