2005-07-24 14:12:37 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2008-05-06 15:11:33 +00:00
|
|
|
/** @file engine.cpp Base for all engine handling. */
|
2007-02-23 18:55:07 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "stdafx.h"
|
2005-06-02 19:30:21 +00:00
|
|
|
#include "openttd.h"
|
2005-02-05 15:58:59 +00:00
|
|
|
#include "debug.h"
|
2008-01-12 14:10:35 +00:00
|
|
|
#include "player_base.h"
|
|
|
|
#include "player_func.h"
|
2007-12-21 21:50:46 +00:00
|
|
|
#include "command_func.h"
|
2008-03-28 08:53:36 +00:00
|
|
|
#include "news_func.h"
|
2004-08-09 17:04:08 +00:00
|
|
|
#include "saveload.h"
|
2005-07-21 22:15:02 +00:00
|
|
|
#include "variables.h"
|
2005-12-02 14:55:23 +00:00
|
|
|
#include "train.h"
|
2007-01-27 12:29:55 +00:00
|
|
|
#include "aircraft.h"
|
2006-05-18 02:43:23 +00:00
|
|
|
#include "newgrf_cargo.h"
|
2008-04-29 21:31:29 +00:00
|
|
|
#include "newgrf_engine.h"
|
2007-05-19 09:40:18 +00:00
|
|
|
#include "group.h"
|
2007-12-21 19:49:27 +00:00
|
|
|
#include "strings_func.h"
|
2008-01-09 09:57:48 +00:00
|
|
|
#include "gfx_func.h"
|
2007-12-25 11:26:07 +00:00
|
|
|
#include "functions.h"
|
|
|
|
#include "window_func.h"
|
2007-12-26 13:50:40 +00:00
|
|
|
#include "date_func.h"
|
2008-01-07 09:19:53 +00:00
|
|
|
#include "autoreplace_base.h"
|
|
|
|
#include "autoreplace_gui.h"
|
2008-01-07 14:23:25 +00:00
|
|
|
#include "string_func.h"
|
2008-01-13 14:37:30 +00:00
|
|
|
#include "settings_type.h"
|
2008-04-06 23:49:45 +00:00
|
|
|
#include "oldpool_func.h"
|
2008-04-29 21:31:29 +00:00
|
|
|
#include "core/alloc_func.hpp"
|
2008-05-13 21:36:09 +00:00
|
|
|
#include "vehicle_func.h"
|
2008-04-29 21:31:29 +00:00
|
|
|
#include "map"
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-01-13 01:21:35 +00:00
|
|
|
#include "table/strings.h"
|
|
|
|
#include "table/engines.h"
|
|
|
|
|
2008-04-29 21:31:29 +00:00
|
|
|
DEFINE_OLD_POOL_GENERIC(Engine, Engine)
|
2005-12-15 17:55:59 +00:00
|
|
|
|
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 10:00:30 +00:00
|
|
|
enum {
|
|
|
|
YEAR_ENGINE_AGING_STOPS = 2050,
|
|
|
|
};
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-29 21:31:29 +00:00
|
|
|
/** Number of engines of each vehicle type in original engine data */
|
|
|
|
const uint8 _engine_counts[4] = {
|
|
|
|
lengthof(_orig_rail_vehicle_info),
|
|
|
|
lengthof(_orig_road_vehicle_info),
|
|
|
|
lengthof(_orig_ship_vehicle_info),
|
|
|
|
lengthof(_orig_aircraft_vehicle_info),
|
|
|
|
};
|
|
|
|
|
|
|
|
/** Offset of the first engine of each vehicle type in original engine data */
|
|
|
|
const uint8 _engine_offsets[4] = {
|
|
|
|
0,
|
|
|
|
lengthof(_orig_rail_vehicle_info),
|
|
|
|
lengthof(_orig_rail_vehicle_info) + lengthof(_orig_road_vehicle_info),
|
|
|
|
lengthof(_orig_rail_vehicle_info) + lengthof(_orig_road_vehicle_info) + lengthof(_orig_ship_vehicle_info),
|
|
|
|
};
|
|
|
|
|
|
|
|
Engine::Engine() :
|
|
|
|
name(NULL),
|
|
|
|
overrides_count(0),
|
|
|
|
overrides(NULL)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Engine::Engine(VehicleType type, EngineID base)
|
2008-01-08 18:25:51 +00:00
|
|
|
{
|
2008-04-29 21:31:29 +00:00
|
|
|
this->type = type;
|
|
|
|
this->internal_id = base;
|
|
|
|
this->list_position = base;
|
|
|
|
|
|
|
|
/* Check if this base engine is within the original engine data range */
|
|
|
|
if (base >= _engine_counts[type]) {
|
|
|
|
/* Mark engine as valid anyway */
|
|
|
|
this->info.climates = 0x80;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the original engine info for this slot */
|
|
|
|
this->info = _orig_engine_info[_engine_offsets[type] + base];
|
|
|
|
|
|
|
|
/* Copy the original engine data for this slot */
|
|
|
|
switch (type) {
|
|
|
|
default: NOT_REACHED();
|
|
|
|
|
|
|
|
case VEH_TRAIN:
|
|
|
|
this->u.rail = _orig_rail_vehicle_info[base];
|
|
|
|
this->image_index = this->u.rail.image_index;
|
|
|
|
this->info.string_id = STR_8000_KIRBY_PAUL_TANK_STEAM + base;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VEH_ROAD:
|
|
|
|
this->u.road = _orig_road_vehicle_info[base];
|
|
|
|
this->image_index = this->u.road.image_index;
|
|
|
|
this->info.string_id = STR_8074_MPS_REGAL_BUS + base;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VEH_SHIP:
|
|
|
|
this->u.ship = _orig_ship_vehicle_info[base];
|
|
|
|
this->image_index = this->u.ship.image_index;
|
|
|
|
this->info.string_id = STR_80CC_MPS_OIL_TANKER + base;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VEH_AIRCRAFT:
|
|
|
|
this->u.air = _orig_aircraft_vehicle_info[base];
|
|
|
|
this->image_index = this->u.air.image_index;
|
|
|
|
this->info.string_id = STR_80D7_SAMPSON_U52 + base;
|
|
|
|
break;
|
2008-01-12 19:58:06 +00:00
|
|
|
}
|
2008-01-08 18:25:51 +00:00
|
|
|
}
|
|
|
|
|
2008-04-29 21:31:29 +00:00
|
|
|
Engine::~Engine()
|
|
|
|
{
|
|
|
|
UnloadWagonOverrides(this);
|
|
|
|
free(this->name);
|
|
|
|
}
|
|
|
|
|
2008-05-07 18:31:29 +00:00
|
|
|
/** Sort all items using qsort() and given 'CompareItems' function
|
|
|
|
* @param el list to be sorted
|
|
|
|
* @param compare function for evaluation of the quicksort
|
|
|
|
*/
|
|
|
|
void EngList_Sort(EngineList *el, EngList_SortTypeFunction compare)
|
|
|
|
{
|
2008-05-13 18:39:15 +00:00
|
|
|
size_t size = el->size();
|
|
|
|
/* out-of-bounds access at the next line for size == 0 (even with operator[] at some systems)
|
|
|
|
* generally, do not sort if there are less than 2 items */
|
|
|
|
if (size < 2) return;
|
|
|
|
qsort(&(el->at(0)), size, sizeof(EngineID), compare);
|
2008-05-07 18:31:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Sort selected range of items (on indices @ <begin, begin+num_items-1>)
|
|
|
|
* @param el list to be sorted
|
|
|
|
* @param compare function for evaluation of the quicksort
|
|
|
|
* @param begin start of sorting
|
|
|
|
* @param num_items count of items to be sorted
|
|
|
|
*/
|
|
|
|
void EngList_SortPartial(EngineList *el, EngList_SortTypeFunction compare, uint begin, uint num_items)
|
|
|
|
{
|
|
|
|
assert(begin <= (uint)el->size());
|
|
|
|
assert(begin + num_items <= (uint)el->size());
|
|
|
|
qsort(&((*el)[begin]), num_items, sizeof(EngineID), compare);
|
|
|
|
}
|
|
|
|
|
2008-05-14 15:29:48 +00:00
|
|
|
|
|
|
|
/** Sets cached values in Player::num_vehicles and Group::num_vehicles
|
|
|
|
*/
|
2008-05-13 21:36:09 +00:00
|
|
|
void SetCachedEngineCounts()
|
|
|
|
{
|
|
|
|
uint engines = GetEnginePoolSize();
|
|
|
|
|
|
|
|
/* Set up the engine count for all players */
|
|
|
|
Player *p;
|
|
|
|
FOR_ALL_PLAYERS(p) {
|
|
|
|
free(p->num_engines);
|
|
|
|
p->num_engines = CallocT<EngineID>(engines);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Recalculate */
|
|
|
|
Group *g;
|
|
|
|
FOR_ALL_GROUPS(g) {
|
|
|
|
free(g->num_engines);
|
|
|
|
g->num_engines = CallocT<EngineID>(engines);
|
|
|
|
}
|
|
|
|
|
|
|
|
const Vehicle *v;
|
|
|
|
FOR_ALL_VEHICLES(v) {
|
|
|
|
if (!IsEngineCountable(v)) continue;
|
|
|
|
|
|
|
|
assert(v->engine_type < engines);
|
|
|
|
|
|
|
|
GetPlayer(v->owner)->num_engines[v->engine_type]++;
|
|
|
|
|
|
|
|
if (v->group_id == DEFAULT_GROUP) continue;
|
|
|
|
|
|
|
|
g = GetGroup(v->group_id);
|
|
|
|
assert(v->type == g->vehicle_type);
|
|
|
|
assert(v->owner == g->owner);
|
|
|
|
|
|
|
|
g->num_engines[v->engine_type]++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-29 21:31:29 +00:00
|
|
|
void SetupEngines()
|
|
|
|
{
|
|
|
|
_Engine_pool.CleanPool();
|
|
|
|
_Engine_pool.AddBlockToPool();
|
|
|
|
|
|
|
|
for (uint i = 0; i < lengthof(_orig_rail_vehicle_info); i++) new Engine(VEH_TRAIN, i);
|
|
|
|
for (uint i = 0; i < lengthof(_orig_road_vehicle_info); i++) new Engine(VEH_ROAD, i);
|
|
|
|
for (uint i = 0; i < lengthof(_orig_ship_vehicle_info); i++) new Engine(VEH_SHIP, i);
|
|
|
|
for (uint i = 0; i < lengthof(_orig_aircraft_vehicle_info); i++) new Engine(VEH_AIRCRAFT, i);
|
|
|
|
}
|
|
|
|
|
2008-01-08 18:25:51 +00:00
|
|
|
|
2005-10-01 12:43:34 +00:00
|
|
|
void ShowEnginePreviewWindow(EngineID engine);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void DeleteCustomEngineNames()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-01-12 19:58:06 +00:00
|
|
|
Engine *e;
|
|
|
|
FOR_ALL_ENGINES(e) {
|
|
|
|
free(e->name);
|
|
|
|
e->name = NULL;
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_vehicle_design_names &= ~1;
|
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void LoadCustomEngineNames()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2006-08-28 10:14:37 +00:00
|
|
|
/* XXX: not done */
|
2006-12-26 17:36:18 +00:00
|
|
|
DEBUG(misc, 1, "LoadCustomEngineNames: not done");
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void CalcEngineReliability(Engine *e)
|
|
|
|
{
|
|
|
|
uint age = e->age;
|
|
|
|
|
2007-05-10 23:10:23 +00:00
|
|
|
/* Check for early retirement */
|
|
|
|
if (e->player_avail != 0 && !_patches.never_expire_vehicles) {
|
2008-04-29 21:31:29 +00:00
|
|
|
int retire_early = e->info.retire_early;
|
2008-02-08 15:34:54 +00:00
|
|
|
uint retire_early_max_age = max(0, e->duration_phase_1 + e->duration_phase_2 - retire_early * 12);
|
|
|
|
if (retire_early != 0 && age >= retire_early_max_age) {
|
2007-05-10 23:10:23 +00:00
|
|
|
/* Early retirement is enabled and we're past the date... */
|
|
|
|
e->player_avail = 0;
|
|
|
|
AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (age < e->duration_phase_1) {
|
|
|
|
uint start = e->reliability_start;
|
|
|
|
e->reliability = age * (e->reliability_max - start) / e->duration_phase_1 + start;
|
2006-10-07 15:04:22 +00:00
|
|
|
} else if ((age -= e->duration_phase_1) < e->duration_phase_2 || _patches.never_expire_vehicles) {
|
|
|
|
/* We are at the peak of this engines life. It will have max reliability.
|
|
|
|
* This is also true if the engines never expire. They will not go bad over time */
|
2004-08-09 17:04:08 +00:00
|
|
|
e->reliability = e->reliability_max;
|
|
|
|
} else if ((age -= e->duration_phase_2) < e->duration_phase_3) {
|
|
|
|
uint max = e->reliability_max;
|
|
|
|
e->reliability = (int)age * (int)(e->reliability_final - max) / e->duration_phase_3 + max;
|
|
|
|
} else {
|
2006-10-07 15:04:22 +00:00
|
|
|
/* time's up for this engine.
|
|
|
|
* We will now completely retire this design */
|
|
|
|
e->player_avail = 0;
|
2004-08-09 17:04:08 +00:00
|
|
|
e->reliability = e->reliability_final;
|
2007-02-06 11:11:12 +00:00
|
|
|
/* Kick this engine out of the lists */
|
|
|
|
AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
2006-10-07 15:04:22 +00:00
|
|
|
InvalidateWindowClasses(WC_BUILD_VEHICLE); // Update to show the new reliability
|
2007-02-06 11:11:12 +00:00
|
|
|
InvalidateWindowClasses(WC_REPLACE_VEHICLE);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void StartupEngines()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Engine *e;
|
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 10:00:30 +00:00
|
|
|
/* Aging of vehicles stops, so account for that when starting late */
|
2006-08-20 12:39:17 +00:00
|
|
|
const Date aging_date = min(_date, ConvertYMDToDate(YEAR_ENGINE_AGING_STOPS, 0, 1));
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-29 21:31:29 +00:00
|
|
|
FOR_ALL_ENGINES(e) {
|
|
|
|
const EngineInfo *ei = &e->info;
|
2005-07-30 18:04:49 +00:00
|
|
|
uint32 r;
|
2005-01-02 17:23:04 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
e->age = 0;
|
|
|
|
e->flags = 0;
|
|
|
|
e->player_avail = 0;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2007-02-23 18:55:07 +00:00
|
|
|
/* The magic value of 729 days below comes from the NewGRF spec. If the
|
|
|
|
* base intro date is before 1922 then the random number of days is not
|
|
|
|
* added. */
|
2004-08-09 17:04:08 +00:00
|
|
|
r = Random();
|
2006-08-17 20:22:35 +00:00
|
|
|
e->intro_date = ei->base_intro <= ConvertYMDToDate(1922, 0, 1) ? ei->base_intro : (Date)GB(r, 0, 9) + ei->base_intro;
|
2004-08-09 17:04:08 +00:00
|
|
|
if (e->intro_date <= _date) {
|
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 10:00:30 +00:00
|
|
|
e->age = (aging_date - e->intro_date) >> 5;
|
2004-08-09 17:04:08 +00:00
|
|
|
e->player_avail = (byte)-1;
|
|
|
|
e->flags |= ENGINE_AVAILABLE;
|
|
|
|
}
|
|
|
|
|
2005-07-20 15:29:28 +00:00
|
|
|
e->reliability_start = GB(r, 16, 14) + 0x7AE0;
|
2004-08-09 17:04:08 +00:00
|
|
|
r = Random();
|
2005-07-20 15:29:28 +00:00
|
|
|
e->reliability_max = GB(r, 0, 14) + 0xBFFF;
|
|
|
|
e->reliability_final = GB(r, 16, 14) + 0x3FFF;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
r = Random();
|
2005-07-20 15:29:28 +00:00
|
|
|
e->duration_phase_1 = GB(r, 0, 5) + 7;
|
|
|
|
e->duration_phase_2 = GB(r, 5, 4) + ei->base_life * 12 - 96;
|
|
|
|
e->duration_phase_3 = GB(r, 9, 7) + 120;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
e->reliability_spd_dec = (ei->unk2&0x7F) << 2;
|
|
|
|
|
|
|
|
/* my invented flag for something that is a wagon */
|
|
|
|
if (ei->unk2 & 0x80) {
|
|
|
|
e->age = 0xFFFF;
|
|
|
|
} else {
|
|
|
|
CalcEngineReliability(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
e->lifelength = ei->lifelength + _patches.extend_vehicle_life;
|
|
|
|
|
2007-02-23 18:55:07 +00:00
|
|
|
/* prevent certain engines from ever appearing. */
|
2007-11-19 21:02:30 +00:00
|
|
|
if (!HasBit(ei->climates, _opt.landscape)) {
|
2004-08-09 17:04:08 +00:00
|
|
|
e->flags |= ENGINE_AVAILABLE;
|
|
|
|
e->player_avail = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-01-24 07:14:09 +00:00
|
|
|
static void AcceptEnginePreview(EngineID eid, PlayerID player)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-01-24 07:14:09 +00:00
|
|
|
Engine *e = GetEngine(eid);
|
2007-05-25 08:13:01 +00:00
|
|
|
Player *p = GetPlayer(player);
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-11-20 13:35:54 +00:00
|
|
|
SetBit(e->player_avail, player);
|
2007-03-08 16:27:54 +00:00
|
|
|
if (e->type == VEH_TRAIN) {
|
2007-01-24 07:14:09 +00:00
|
|
|
const RailVehicleInfo *rvi = RailVehInfo(eid);
|
|
|
|
|
|
|
|
assert(rvi->railtype < RAILTYPE_END);
|
2007-11-20 13:35:54 +00:00
|
|
|
SetBit(p->avail_railtypes, rvi->railtype);
|
2007-05-25 08:13:01 +00:00
|
|
|
} else if (e->type == VEH_ROAD) {
|
2007-11-20 13:35:54 +00:00
|
|
|
SetBit(p->avail_roadtypes, HasBit(EngInfo(eid)->misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD);
|
2007-01-24 07:14:09 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-02-15 11:02:50 +00:00
|
|
|
e->preview_player_rank = 0xFF;
|
2006-10-07 14:30:13 +00:00
|
|
|
if (player == _local_player) {
|
2007-02-06 11:11:12 +00:00
|
|
|
AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
|
2006-10-07 14:30:13 +00:00
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2008-02-15 11:02:50 +00:00
|
|
|
static PlayerID GetBestPlayer(uint8 pp)
|
2005-05-11 00:00:27 +00:00
|
|
|
{
|
|
|
|
const Player *p;
|
|
|
|
int32 best_hist;
|
|
|
|
PlayerID best_player;
|
|
|
|
uint mask = 0;
|
|
|
|
|
2008-02-15 21:05:46 +00:00
|
|
|
do {
|
2005-05-11 00:00:27 +00:00
|
|
|
best_hist = -1;
|
2006-10-14 15:49:43 +00:00
|
|
|
best_player = PLAYER_SPECTATOR;
|
2005-05-11 00:00:27 +00:00
|
|
|
FOR_ALL_PLAYERS(p) {
|
2007-11-19 21:02:30 +00:00
|
|
|
if (p->is_active && p->block_preview == 0 && !HasBit(mask, p->index) &&
|
2005-05-11 00:00:27 +00:00
|
|
|
p->old_economy[0].performance_history > best_hist) {
|
|
|
|
best_hist = p->old_economy[0].performance_history;
|
|
|
|
best_player = p->index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-10-14 15:49:43 +00:00
|
|
|
if (best_player == PLAYER_SPECTATOR) return PLAYER_SPECTATOR;
|
2005-05-11 00:00:27 +00:00
|
|
|
|
2007-11-20 13:35:54 +00:00
|
|
|
SetBit(mask, best_player);
|
2008-02-25 02:09:04 +00:00
|
|
|
} while (--pp != 0);
|
2005-05-11 00:00:27 +00:00
|
|
|
|
|
|
|
return best_player;
|
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void EnginesDailyLoop()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 10:00:30 +00:00
|
|
|
if (_cur_year >= YEAR_ENGINE_AGING_STOPS) return;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2008-04-29 21:31:29 +00:00
|
|
|
Engine *e;
|
|
|
|
FOR_ALL_ENGINES(e) {
|
|
|
|
EngineID i = e->index;
|
2007-02-28 17:06:22 +00:00
|
|
|
if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) {
|
|
|
|
if (e->flags & ENGINE_OFFER_WINDOW_OPEN) {
|
2008-02-15 11:02:50 +00:00
|
|
|
if (e->preview_player_rank != 0xFF && !--e->preview_wait) {
|
2007-02-28 17:06:22 +00:00
|
|
|
e->flags &= ~ENGINE_OFFER_WINDOW_OPEN;
|
2004-08-09 17:04:08 +00:00
|
|
|
DeleteWindowById(WC_ENGINE_PREVIEW, i);
|
2008-02-15 11:02:50 +00:00
|
|
|
e->preview_player_rank++;
|
2004-09-10 19:02:27 +00:00
|
|
|
}
|
2008-02-15 11:02:50 +00:00
|
|
|
} else if (e->preview_player_rank != 0xFF) {
|
|
|
|
PlayerID best_player = GetBestPlayer(e->preview_player_rank);
|
2005-05-11 00:00:27 +00:00
|
|
|
|
2006-10-14 15:49:43 +00:00
|
|
|
if (best_player == PLAYER_SPECTATOR) {
|
2008-02-15 11:02:50 +00:00
|
|
|
e->preview_player_rank = 0xFF;
|
2005-05-11 00:00:27 +00:00
|
|
|
continue;
|
|
|
|
}
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2006-10-14 15:15:56 +00:00
|
|
|
if (!IsHumanPlayer(best_player)) {
|
2005-05-11 00:00:27 +00:00
|
|
|
/* XXX - TTDBUG: TTD has a bug here ???? */
|
2007-01-24 07:14:09 +00:00
|
|
|
AcceptEnginePreview(i, best_player);
|
2004-08-09 17:04:08 +00:00
|
|
|
} else {
|
2007-02-28 17:06:22 +00:00
|
|
|
e->flags |= ENGINE_OFFER_WINDOW_OPEN;
|
2004-08-09 17:04:08 +00:00
|
|
|
e->preview_wait = 20;
|
2006-10-14 15:15:56 +00:00
|
|
|
if (IsInteractivePlayer(best_player)) ShowEnginePreviewWindow(i);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-11 00:00:27 +00:00
|
|
|
/** Accept an engine prototype. XXX - it is possible that the top-player
|
|
|
|
* changes while you are waiting to accept the offer? Then it becomes invalid
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile unused
|
2007-04-17 20:23:13 +00:00
|
|
|
* @param flags operation to perfom
|
2005-05-11 00:00:27 +00:00
|
|
|
* @param p1 engine-prototype offered
|
|
|
|
* @param p2 unused
|
|
|
|
*/
|
2007-06-18 10:48:15 +00:00
|
|
|
CommandCost CmdWantEnginePreview(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2005-05-11 00:00:27 +00:00
|
|
|
Engine *e;
|
|
|
|
|
2006-02-01 06:32:03 +00:00
|
|
|
if (!IsEngineIndex(p1)) return CMD_ERROR;
|
2005-06-07 18:13:49 +00:00
|
|
|
e = GetEngine(p1);
|
2008-02-15 11:02:50 +00:00
|
|
|
if (GetBestPlayer(e->preview_player_rank) != _current_player) return CMD_ERROR;
|
2005-05-11 00:00:27 +00:00
|
|
|
|
2007-01-24 07:14:09 +00:00
|
|
|
if (flags & DC_EXEC) AcceptEnginePreview(p1, _current_player);
|
2005-05-11 00:00:27 +00:00
|
|
|
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-02-23 18:55:07 +00:00
|
|
|
/* Determine if an engine type is a wagon (and not a loco) */
|
2005-10-01 12:43:34 +00:00
|
|
|
static bool IsWagon(EngineID index)
|
2004-09-15 06:51:23 +00:00
|
|
|
{
|
2008-04-29 21:31:29 +00:00
|
|
|
const Engine *e = GetEngine(index);
|
|
|
|
return e->type == VEH_TRAIN && e->u.rail.railveh_type == RAILVEH_WAGON;
|
2004-09-15 06:51:23 +00:00
|
|
|
}
|
|
|
|
|
2008-05-13 10:17:04 +00:00
|
|
|
StringID GetEngineCategoryName(EngineID engine);
|
|
|
|
|
2004-11-14 19:44:06 +00:00
|
|
|
static void NewVehicleAvailable(Engine *e)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
|
|
|
Vehicle *v;
|
|
|
|
Player *p;
|
2008-04-29 21:31:29 +00:00
|
|
|
EngineID index = e->index;
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-23 18:55:07 +00:00
|
|
|
/* In case the player didn't build the vehicle during the intro period,
|
|
|
|
* prevent that player from getting future intro periods for a while. */
|
2007-02-28 17:06:22 +00:00
|
|
|
if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) {
|
2004-08-09 17:04:08 +00:00
|
|
|
FOR_ALL_PLAYERS(p) {
|
2005-01-06 22:31:58 +00:00
|
|
|
uint block_preview = p->block_preview;
|
|
|
|
|
2007-11-19 21:02:30 +00:00
|
|
|
if (!HasBit(e->player_avail, p->index)) continue;
|
2004-09-10 19:02:27 +00:00
|
|
|
|
2005-01-06 22:31:58 +00:00
|
|
|
/* We assume the user did NOT build it.. prove me wrong ;) */
|
|
|
|
p->block_preview = 20;
|
|
|
|
|
|
|
|
FOR_ALL_VEHICLES(v) {
|
2007-03-08 16:27:54 +00:00
|
|
|
if (v->type == VEH_TRAIN || v->type == VEH_ROAD || v->type == VEH_SHIP ||
|
|
|
|
(v->type == VEH_AIRCRAFT && IsNormalAircraft(v))) {
|
2005-01-06 22:31:58 +00:00
|
|
|
if (v->owner == p->index && v->engine_type == index) {
|
|
|
|
/* The user did prove me wrong, so restore old value */
|
|
|
|
p->block_preview = block_preview;
|
|
|
|
break;
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-28 17:06:22 +00:00
|
|
|
e->flags = (e->flags & ~ENGINE_EXCLUSIVE_PREVIEW) | ENGINE_AVAILABLE;
|
2007-02-06 11:11:12 +00:00
|
|
|
AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
|
2004-08-23 07:50:01 +00:00
|
|
|
|
2007-02-23 18:55:07 +00:00
|
|
|
/* Now available for all players */
|
2004-08-09 17:04:08 +00:00
|
|
|
e->player_avail = (byte)-1;
|
2004-08-23 07:50:01 +00:00
|
|
|
|
2007-02-23 18:55:07 +00:00
|
|
|
/* Do not introduce new rail wagons */
|
2005-11-14 19:48:04 +00:00
|
|
|
if (IsWagon(index)) return;
|
2004-08-23 07:50:01 +00:00
|
|
|
|
2008-01-08 18:16:26 +00:00
|
|
|
if (e->type == VEH_TRAIN) {
|
2007-02-23 18:55:07 +00:00
|
|
|
/* maybe make another rail type available */
|
2008-04-29 21:31:29 +00:00
|
|
|
RailType railtype = e->u.rail.railtype;
|
2007-01-24 07:14:09 +00:00
|
|
|
assert(railtype < RAILTYPE_END);
|
|
|
|
FOR_ALL_PLAYERS(p) {
|
2007-11-20 13:35:54 +00:00
|
|
|
if (p->is_active) SetBit(p->avail_railtypes, railtype);
|
2005-07-20 22:02:58 +00:00
|
|
|
}
|
2008-01-08 18:16:26 +00:00
|
|
|
} else if (e->type == VEH_ROAD) {
|
2007-05-25 08:47:40 +00:00
|
|
|
/* maybe make another road type available */
|
|
|
|
FOR_ALL_PLAYERS(p) {
|
2008-04-29 21:31:29 +00:00
|
|
|
if (p->is_active) SetBit(p->avail_roadtypes, HasBit(e->info.misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD);
|
2007-05-25 08:47:40 +00:00
|
|
|
}
|
|
|
|
}
|
2008-05-13 10:17:04 +00:00
|
|
|
|
|
|
|
SetDParam(0, GetEngineCategoryName(index));
|
|
|
|
SetDParam(1, index);
|
|
|
|
AddNewsItem(STR_NEW_VEHICLE_NOW_AVAILABLE_WITH_TYPE, NM_CALLBACK, NF_NONE, NT_NEW_VEHICLES, DNC_VEHICLEAVAIL, index, 0);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void EnginesMonthlyLoop()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
(svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
- New optional landscape generator (TerraGenesis Perlin)
- Load heightmaps (either BMP or PNG)
- Progress dialog while generating worlds (no longer a 'hanging' screen)
- New dialogs for NewGame, Create Scenario and Play Heightmap
- Easier to configure your landscape
- More things to configure (tree-placer, ..)
- Speedup of world generation
- New console command 'restart': restart the map EXACTLY as it was when you
first started it (needs a game made after or with this commit)
- New console command 'getseed': get the seed of your map and share it with
others (of course only works with generated maps)
- Many new, world generation related, things
- Many internal cleanups and rewrites
Many tnx to those people who helped making this:
Belugas, DaleStan, glx, KUDr, RichK67, Rubidium, and TrueLight (alfabetic)
Many tnx to those who helped testing:
Arnau, Bjarni, and tokai (alfabetic)
And to all other people who helped testing and sending comments / bugs
Stats: 673 lines changed, 3534 new lines, 79 new strings
2006-08-19 10:00:30 +00:00
|
|
|
if (_cur_year < YEAR_ENGINE_AGING_STOPS) {
|
2008-01-24 10:47:44 +00:00
|
|
|
Engine *e;
|
|
|
|
FOR_ALL_ENGINES(e) {
|
2007-02-23 18:55:07 +00:00
|
|
|
/* Age the vehicle */
|
2005-07-30 18:04:49 +00:00
|
|
|
if (e->flags & ENGINE_AVAILABLE && e->age != 0xFFFF) {
|
2004-08-09 17:04:08 +00:00
|
|
|
e->age++;
|
|
|
|
CalcEngineReliability(e);
|
|
|
|
}
|
|
|
|
|
2006-02-06 10:05:41 +00:00
|
|
|
if (!(e->flags & ENGINE_AVAILABLE) && _date >= (e->intro_date + 365)) {
|
2007-02-23 18:55:07 +00:00
|
|
|
/* Introduce it to all players */
|
2004-08-09 17:04:08 +00:00
|
|
|
NewVehicleAvailable(e);
|
2007-02-28 17:06:22 +00:00
|
|
|
} else if (!(e->flags & (ENGINE_AVAILABLE|ENGINE_EXCLUSIVE_PREVIEW)) && _date >= e->intro_date) {
|
2007-02-23 18:55:07 +00:00
|
|
|
/* Introduction date has passed.. show introducing dialog to one player. */
|
2007-02-28 17:06:22 +00:00
|
|
|
e->flags |= ENGINE_EXCLUSIVE_PREVIEW;
|
2004-09-15 06:51:23 +00:00
|
|
|
|
2007-02-23 18:55:07 +00:00
|
|
|
/* Do not introduce new rail wagons */
|
2008-04-29 21:31:29 +00:00
|
|
|
if (!IsWagon(e->index))
|
2008-02-15 11:02:50 +00:00
|
|
|
e->preview_player_rank = 1; // Give to the player with the highest rating.
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-27 20:53:25 +00:00
|
|
|
static bool IsUniqueEngineName(const char *name)
|
|
|
|
{
|
|
|
|
char buf[512];
|
|
|
|
|
2008-04-29 21:31:29 +00:00
|
|
|
const Engine *e;
|
|
|
|
FOR_ALL_ENGINES(e) {
|
|
|
|
SetDParam(0, e->index);
|
2007-06-27 20:53:25 +00:00
|
|
|
GetString(buf, STR_ENGINE_NAME, lastof(buf));
|
|
|
|
if (strcmp(buf, name) == 0) return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2005-05-11 00:00:27 +00:00
|
|
|
/** Rename an engine.
|
2006-04-10 07:15:58 +00:00
|
|
|
* @param tile unused
|
2007-04-17 20:23:13 +00:00
|
|
|
* @param flags operation to perfom
|
2005-05-11 00:00:27 +00:00
|
|
|
* @param p1 engine ID to rename
|
|
|
|
* @param p2 unused
|
|
|
|
*/
|
2007-06-18 10:48:15 +00:00
|
|
|
CommandCost CmdRenameEngine(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2007-06-27 20:53:25 +00:00
|
|
|
if (!IsEngineIndex(p1) || StrEmpty(_cmd_text)) return CMD_ERROR;
|
|
|
|
|
|
|
|
if (!IsUniqueEngineName(_cmd_text)) return_cmd_error(STR_NAME_MUST_BE_UNIQUE);
|
2005-05-11 00:00:27 +00:00
|
|
|
|
2004-08-09 17:04:08 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2008-01-12 19:58:06 +00:00
|
|
|
Engine *e = GetEngine(p1);
|
|
|
|
free(e->name);
|
|
|
|
e->name = strdup(_cmd_text);
|
2004-08-09 17:04:08 +00:00
|
|
|
_vehicle_design_names |= 3;
|
|
|
|
MarkWholeScreenDirty();
|
|
|
|
}
|
|
|
|
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost();
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-02-04 11:28:12 +00:00
|
|
|
/** Check if an engine is buildable.
|
|
|
|
* @param engine index of the engine to check.
|
|
|
|
* @param type the type the engine should be.
|
|
|
|
* @param player index of the player.
|
|
|
|
* @return True if an engine is valid, of the specified type, and buildable by
|
|
|
|
* the given player.
|
2006-01-12 15:52:18 +00:00
|
|
|
*/
|
2008-02-04 11:28:12 +00:00
|
|
|
bool IsEngineBuildable(EngineID engine, VehicleType type, PlayerID player)
|
2006-01-12 15:52:18 +00:00
|
|
|
{
|
2007-02-23 18:55:07 +00:00
|
|
|
/* check if it's an engine that is in the engine array */
|
2006-01-12 15:52:18 +00:00
|
|
|
if (!IsEngineIndex(engine)) return false;
|
|
|
|
|
2008-02-04 11:28:12 +00:00
|
|
|
const Engine *e = GetEngine(engine);
|
2006-01-12 15:52:18 +00:00
|
|
|
|
2007-02-23 18:55:07 +00:00
|
|
|
/* check if it's an engine of specified type */
|
2006-01-12 15:52:18 +00:00
|
|
|
if (e->type != type) return false;
|
|
|
|
|
2007-02-23 18:55:07 +00:00
|
|
|
/* check if it's available */
|
2007-11-19 21:02:30 +00:00
|
|
|
if (!HasBit(e->player_avail, player)) return false;
|
2006-01-12 15:52:18 +00:00
|
|
|
|
2007-04-26 07:24:19 +00:00
|
|
|
if (type == VEH_TRAIN) {
|
|
|
|
/* Check if the rail type is available to this player */
|
|
|
|
const Player *p = GetPlayer(player);
|
2007-11-19 21:02:30 +00:00
|
|
|
if (!HasBit(p->avail_railtypes, RailVehInfo(engine)->railtype)) return false;
|
2007-04-26 07:24:19 +00:00
|
|
|
}
|
|
|
|
|
2006-01-12 15:52:18 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-04-20 16:56:55 +00:00
|
|
|
/** Get the default cargo type for a certain engine type
|
|
|
|
* @param engine The ID to get the cargo for
|
|
|
|
* @return The cargo type. CT_INVALID means no cargo capacity
|
|
|
|
*/
|
|
|
|
CargoID GetEngineCargoType(EngineID engine)
|
|
|
|
{
|
|
|
|
assert(IsEngineIndex(engine));
|
|
|
|
|
|
|
|
switch (GetEngine(engine)->type) {
|
|
|
|
case VEH_TRAIN:
|
|
|
|
if (RailVehInfo(engine)->capacity == 0) return CT_INVALID;
|
|
|
|
return RailVehInfo(engine)->cargo_type;
|
|
|
|
|
|
|
|
case VEH_ROAD:
|
|
|
|
if (RoadVehInfo(engine)->capacity == 0) return CT_INVALID;
|
|
|
|
return RoadVehInfo(engine)->cargo_type;
|
|
|
|
|
|
|
|
case VEH_SHIP:
|
|
|
|
if (ShipVehInfo(engine)->capacity == 0) return CT_INVALID;
|
|
|
|
return ShipVehInfo(engine)->cargo_type;
|
|
|
|
|
|
|
|
case VEH_AIRCRAFT:
|
|
|
|
/* all aircraft starts as passenger planes with cargo capacity */
|
|
|
|
return CT_PASSENGERS;
|
|
|
|
|
|
|
|
default: NOT_REACHED(); return CT_INVALID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-01-12 15:52:18 +00:00
|
|
|
/************************************************************************
|
|
|
|
* Engine Replacement stuff
|
|
|
|
************************************************************************/
|
|
|
|
|
2007-08-02 22:32:47 +00:00
|
|
|
DEFINE_OLD_POOL_GENERIC(EngineRenew, EngineRenew)
|
2006-01-12 15:52:18 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves the EngineRenew that specifies the replacement of the given
|
|
|
|
* engine type from the given renewlist */
|
2007-05-19 09:40:18 +00:00
|
|
|
static EngineRenew *GetEngineReplacement(EngineRenewList erl, EngineID engine, GroupID group)
|
2006-01-12 15:52:18 +00:00
|
|
|
{
|
2006-08-22 16:22:07 +00:00
|
|
|
EngineRenew *er = (EngineRenew *)erl;
|
|
|
|
|
2006-01-12 15:52:18 +00:00
|
|
|
while (er) {
|
2007-05-19 09:40:18 +00:00
|
|
|
if (er->from == engine && er->group_id == group) return er;
|
2006-01-12 15:52:18 +00:00
|
|
|
er = er->next;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2006-07-26 03:33:12 +00:00
|
|
|
void RemoveAllEngineReplacement(EngineRenewList *erl)
|
2006-01-12 15:52:18 +00:00
|
|
|
{
|
2006-08-22 16:22:07 +00:00
|
|
|
EngineRenew *er = (EngineRenew *)(*erl);
|
2006-08-26 14:22:54 +00:00
|
|
|
EngineRenew *next;
|
2006-08-22 16:22:07 +00:00
|
|
|
|
2007-08-02 22:32:47 +00:00
|
|
|
while (er != NULL) {
|
2006-08-26 14:22:54 +00:00
|
|
|
next = er->next;
|
2007-08-02 22:32:47 +00:00
|
|
|
delete er;
|
2006-08-26 14:22:54 +00:00
|
|
|
er = next;
|
2006-01-12 15:52:18 +00:00
|
|
|
}
|
2006-08-22 16:22:07 +00:00
|
|
|
*erl = NULL; // Empty list
|
2006-01-12 15:52:18 +00:00
|
|
|
}
|
|
|
|
|
2007-05-19 09:40:18 +00:00
|
|
|
EngineID EngineReplacement(EngineRenewList erl, EngineID engine, GroupID group)
|
2006-01-12 15:52:18 +00:00
|
|
|
{
|
2007-05-19 09:40:18 +00:00
|
|
|
const EngineRenew *er = GetEngineReplacement(erl, engine, group);
|
2008-01-16 01:27:24 +00:00
|
|
|
if (er == NULL && (group == DEFAULT_GROUP || (IsValidGroupID(group) && !GetGroup(group)->replace_protection))) {
|
|
|
|
/* We didn't find anything useful in the vehicle's own group so we will try ALL_GROUP */
|
|
|
|
er = GetEngineReplacement(erl, engine, ALL_GROUP);
|
|
|
|
}
|
2006-01-12 15:52:18 +00:00
|
|
|
return er == NULL ? INVALID_ENGINE : er->to;
|
|
|
|
}
|
|
|
|
|
2007-06-18 10:48:15 +00:00
|
|
|
CommandCost AddEngineReplacement(EngineRenewList *erl, EngineID old_engine, EngineID new_engine, GroupID group, uint32 flags)
|
2006-01-12 15:52:18 +00:00
|
|
|
{
|
|
|
|
EngineRenew *er;
|
|
|
|
|
2006-08-22 16:22:07 +00:00
|
|
|
/* Check if the old vehicle is already in the list */
|
2007-05-19 09:40:18 +00:00
|
|
|
er = GetEngineReplacement(*erl, old_engine, group);
|
2006-01-12 15:52:18 +00:00
|
|
|
if (er != NULL) {
|
|
|
|
if (flags & DC_EXEC) er->to = new_engine;
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost();
|
2006-01-12 15:52:18 +00:00
|
|
|
}
|
|
|
|
|
2008-04-23 20:56:08 +00:00
|
|
|
if (!EngineRenew::CanAllocateItem()) return CMD_ERROR;
|
2006-01-12 15:52:18 +00:00
|
|
|
|
|
|
|
if (flags & DC_EXEC) {
|
2008-04-23 20:56:08 +00:00
|
|
|
er = new EngineRenew(old_engine, new_engine);
|
2007-05-19 09:40:18 +00:00
|
|
|
er->group_id = group;
|
2006-01-12 15:52:18 +00:00
|
|
|
|
2006-08-22 16:22:07 +00:00
|
|
|
/* Insert before the first element */
|
|
|
|
er->next = (EngineRenew *)(*erl);
|
|
|
|
*erl = (EngineRenewList)er;
|
2006-01-12 15:52:18 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost();
|
2006-01-12 15:52:18 +00:00
|
|
|
}
|
|
|
|
|
2007-06-18 10:48:15 +00:00
|
|
|
CommandCost RemoveEngineReplacement(EngineRenewList *erl, EngineID engine, GroupID group, uint32 flags)
|
2006-01-12 15:52:18 +00:00
|
|
|
{
|
2006-08-22 16:22:07 +00:00
|
|
|
EngineRenew *er = (EngineRenew *)(*erl);
|
2006-07-26 03:33:12 +00:00
|
|
|
EngineRenew *prev = NULL;
|
2006-01-12 15:52:18 +00:00
|
|
|
|
|
|
|
while (er)
|
|
|
|
{
|
2007-05-19 09:40:18 +00:00
|
|
|
if (er->from == engine && er->group_id == group) {
|
2006-01-12 15:52:18 +00:00
|
|
|
if (flags & DC_EXEC) {
|
2006-08-22 16:22:07 +00:00
|
|
|
if (prev == NULL) { // First element
|
|
|
|
/* The second becomes the new first element */
|
|
|
|
*erl = (EngineRenewList)er->next;
|
2006-01-12 15:52:18 +00:00
|
|
|
} else {
|
2006-08-22 16:22:07 +00:00
|
|
|
/* Cut this element out */
|
|
|
|
prev->next = er->next;
|
2006-01-12 15:52:18 +00:00
|
|
|
}
|
2007-08-02 22:32:47 +00:00
|
|
|
delete er;
|
2006-01-12 15:52:18 +00:00
|
|
|
}
|
2007-06-18 19:53:50 +00:00
|
|
|
return CommandCost();
|
2006-01-12 15:52:18 +00:00
|
|
|
}
|
|
|
|
prev = er;
|
2006-08-22 16:22:07 +00:00
|
|
|
er = er->next;
|
2006-01-12 15:52:18 +00:00
|
|
|
}
|
|
|
|
|
2006-08-22 16:22:07 +00:00
|
|
|
return CMD_ERROR;
|
2006-01-12 15:52:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const SaveLoad _engine_renew_desc[] = {
|
2007-05-19 09:40:18 +00:00
|
|
|
SLE_VAR(EngineRenew, from, SLE_UINT16),
|
|
|
|
SLE_VAR(EngineRenew, to, SLE_UINT16),
|
2006-01-12 15:52:18 +00:00
|
|
|
|
2007-05-19 09:40:18 +00:00
|
|
|
SLE_REF(EngineRenew, next, REF_ENGINE_RENEWS),
|
|
|
|
SLE_CONDVAR(EngineRenew, group_id, SLE_UINT16, 60, SL_MAX_VERSION),
|
2006-01-12 15:52:18 +00:00
|
|
|
SLE_END()
|
|
|
|
};
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
static void Save_ERNW()
|
2006-01-12 15:52:18 +00:00
|
|
|
{
|
|
|
|
EngineRenew *er;
|
|
|
|
|
|
|
|
FOR_ALL_ENGINE_RENEWS(er) {
|
2006-08-22 16:22:07 +00:00
|
|
|
SlSetArrayIndex(er->index);
|
|
|
|
SlObject(er, _engine_renew_desc);
|
2006-01-12 15:52:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
static void Load_ERNW()
|
2006-01-12 15:52:18 +00:00
|
|
|
{
|
|
|
|
int index;
|
|
|
|
|
|
|
|
while ((index = SlIterateArray()) != -1) {
|
2007-08-02 22:32:47 +00:00
|
|
|
EngineRenew *er = new (index) EngineRenew();
|
2006-01-12 15:52:18 +00:00
|
|
|
SlObject(er, _engine_renew_desc);
|
2007-05-19 09:40:18 +00:00
|
|
|
|
2007-07-14 23:10:27 +00:00
|
|
|
/* Advanced vehicle lists, ungrouped vehicles got added */
|
|
|
|
if (CheckSavegameVersion(60)) {
|
|
|
|
er->group_id = ALL_GROUP;
|
|
|
|
} else if (CheckSavegameVersion(71)) {
|
|
|
|
if (er->group_id == DEFAULT_GROUP) er->group_id = ALL_GROUP;
|
|
|
|
}
|
2006-01-12 15:52:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-05-30 22:16:05 +00:00
|
|
|
static const SaveLoad _engine_desc[] = {
|
2006-08-22 14:38:37 +00:00
|
|
|
SLE_CONDVAR(Engine, intro_date, SLE_FILE_U16 | SLE_VAR_I32, 0, 30),
|
|
|
|
SLE_CONDVAR(Engine, intro_date, SLE_INT32, 31, SL_MAX_VERSION),
|
|
|
|
SLE_CONDVAR(Engine, age, SLE_FILE_U16 | SLE_VAR_I32, 0, 30),
|
|
|
|
SLE_CONDVAR(Engine, age, SLE_INT32, 31, SL_MAX_VERSION),
|
|
|
|
SLE_VAR(Engine, reliability, SLE_UINT16),
|
|
|
|
SLE_VAR(Engine, reliability_spd_dec, SLE_UINT16),
|
|
|
|
SLE_VAR(Engine, reliability_start, SLE_UINT16),
|
|
|
|
SLE_VAR(Engine, reliability_max, SLE_UINT16),
|
|
|
|
SLE_VAR(Engine, reliability_final, SLE_UINT16),
|
|
|
|
SLE_VAR(Engine, duration_phase_1, SLE_UINT16),
|
|
|
|
SLE_VAR(Engine, duration_phase_2, SLE_UINT16),
|
|
|
|
SLE_VAR(Engine, duration_phase_3, SLE_UINT16),
|
|
|
|
|
|
|
|
SLE_VAR(Engine, lifelength, SLE_UINT8),
|
|
|
|
SLE_VAR(Engine, flags, SLE_UINT8),
|
2008-02-15 11:02:50 +00:00
|
|
|
SLE_VAR(Engine, preview_player_rank, SLE_UINT8),
|
2006-08-22 14:38:37 +00:00
|
|
|
SLE_VAR(Engine, preview_wait, SLE_UINT8),
|
2007-01-24 07:14:09 +00:00
|
|
|
SLE_CONDNULL(1, 0, 44),
|
2006-08-22 14:38:37 +00:00
|
|
|
SLE_VAR(Engine, player_avail, SLE_UINT8),
|
2008-01-12 19:58:06 +00:00
|
|
|
SLE_CONDSTR(Engine, name, SLE_STR, 0, 84, SL_MAX_VERSION),
|
2004-08-09 17:04:08 +00:00
|
|
|
|
2007-02-23 18:55:07 +00:00
|
|
|
/* reserve extra space in savegame here. (currently 16 bytes) */
|
2006-03-16 00:20:33 +00:00
|
|
|
SLE_CONDNULL(16, 2, SL_MAX_VERSION),
|
2004-08-09 17:04:08 +00:00
|
|
|
|
|
|
|
SLE_END()
|
|
|
|
};
|
|
|
|
|
2008-04-29 21:31:29 +00:00
|
|
|
static std::map<EngineID, Engine> _temp_engine;
|
|
|
|
|
|
|
|
Engine *GetTempDataEngine(EngineID index)
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-04-29 21:31:29 +00:00
|
|
|
return &_temp_engine[index];
|
|
|
|
}
|
2005-07-30 18:04:49 +00:00
|
|
|
|
2008-04-29 21:31:29 +00:00
|
|
|
static void Save_ENGN()
|
|
|
|
{
|
|
|
|
Engine *e;
|
|
|
|
FOR_ALL_ENGINES(e) {
|
|
|
|
SlSetArrayIndex(e->index);
|
|
|
|
SlObject(e, _engine_desc);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
static void Load_ENGN()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-04-29 21:31:29 +00:00
|
|
|
/* As engine data is loaded before engines are initialized we need to load
|
|
|
|
* this information into a temporary array. This is then copied into the
|
|
|
|
* engine pool after processing NewGRFs by CopyTempEngineData(). */
|
2004-08-09 17:04:08 +00:00
|
|
|
int index;
|
|
|
|
while ((index = SlIterateArray()) != -1) {
|
2008-04-29 21:31:29 +00:00
|
|
|
Engine *e = GetTempDataEngine(index);
|
|
|
|
SlObject(e, _engine_desc);
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-29 21:31:29 +00:00
|
|
|
/**
|
|
|
|
* Copy data from temporary engine array into the real engine pool.
|
|
|
|
*/
|
|
|
|
void CopyTempEngineData()
|
|
|
|
{
|
|
|
|
Engine *e;
|
|
|
|
FOR_ALL_ENGINES(e) {
|
|
|
|
if (e->index >= _temp_engine.size()) break;
|
|
|
|
|
|
|
|
const Engine *se = GetTempDataEngine(e->index);
|
|
|
|
e->intro_date = se->intro_date;
|
|
|
|
e->age = se->age;
|
|
|
|
e->reliability = se->reliability;
|
|
|
|
e->reliability_spd_dec = se->reliability_spd_dec;
|
|
|
|
e->reliability_start = se->reliability_start;
|
|
|
|
e->reliability_max = se->reliability_max;
|
|
|
|
e->reliability_final = se->reliability_final;
|
|
|
|
e->duration_phase_1 = se->duration_phase_1;
|
|
|
|
e->duration_phase_2 = se->duration_phase_2;
|
|
|
|
e->duration_phase_3 = se->duration_phase_3;
|
|
|
|
e->lifelength = se->lifelength;
|
|
|
|
e->flags = se->flags;
|
|
|
|
e->preview_player_rank = se->preview_player_rank;
|
|
|
|
e->preview_wait = se->preview_wait;
|
|
|
|
e->player_avail = se->player_avail;
|
|
|
|
if (se->name != NULL) e->name = strdup(se->name);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get rid of temporary data */
|
|
|
|
_temp_engine.clear();
|
|
|
|
}
|
|
|
|
|
2008-01-12 19:58:06 +00:00
|
|
|
static void Load_ENGS()
|
2004-08-09 17:04:08 +00:00
|
|
|
{
|
2008-04-29 21:31:29 +00:00
|
|
|
/* Load old separate String ID list into a temporary array. This
|
|
|
|
* was always 256 entries. */
|
|
|
|
StringID names[256];
|
2008-01-12 19:58:06 +00:00
|
|
|
|
|
|
|
SlArray(names, lengthof(names), SLE_STRINGID);
|
|
|
|
|
2008-04-29 21:31:29 +00:00
|
|
|
/* Copy each string into the temporary engine array. */
|
2008-01-12 19:58:06 +00:00
|
|
|
for (EngineID engine = 0; engine < lengthof(names); engine++) {
|
2008-04-29 21:31:29 +00:00
|
|
|
Engine *e = GetTempDataEngine(engine);
|
2008-01-12 19:58:06 +00:00
|
|
|
e->name = CopyFromOldName(names[engine]);
|
|
|
|
}
|
2004-08-09 17:04:08 +00:00
|
|
|
}
|
|
|
|
|
2007-01-10 18:56:51 +00:00
|
|
|
extern const ChunkHandler _engine_chunk_handlers[] = {
|
2006-01-12 15:52:18 +00:00
|
|
|
{ 'ENGN', Save_ENGN, Load_ENGN, CH_ARRAY },
|
2008-01-12 19:58:06 +00:00
|
|
|
{ 'ENGS', NULL, Load_ENGS, CH_RIFF },
|
2006-01-12 15:52:18 +00:00
|
|
|
{ 'ERNW', Save_ERNW, Load_ERNW, CH_ARRAY | CH_LAST},
|
2004-08-09 17:04:08 +00:00
|
|
|
};
|
|
|
|
|
2007-03-07 11:47:46 +00:00
|
|
|
void InitializeEngines()
|
2005-01-27 21:18:03 +00:00
|
|
|
{
|
2006-01-12 15:52:18 +00:00
|
|
|
/* Clean the engine renew pool and create 1 block in it */
|
2007-08-02 22:32:47 +00:00
|
|
|
_EngineRenew_pool.CleanPool();
|
|
|
|
_EngineRenew_pool.AddBlockToPool();
|
2005-01-27 21:00:05 +00:00
|
|
|
}
|