src/engine.cpp
author skidd13
Sun, 02 Dec 2007 21:43:16 +0000
changeset 8004 1c54bc6f4bdf
parent 7931 b0a46cd92225
child 8114 dd6d21dc99c1
permissions -rw-r--r--
(svn r11563) -Codechange: Align the preprocessor code in stdafx.h with tabs
2186
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     1
/* $Id$ */
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     2
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5971
diff changeset
     3
/** @file engine.cpp */
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5971
diff changeset
     4
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     5
#include "stdafx.h"
1891
862800791170 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1883
diff changeset
     6
#include "openttd.h"
1299
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents: 1197
diff changeset
     7
#include "debug.h"
2163
b17b313113a0 (svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents: 2159
diff changeset
     8
#include "functions.h"
507
04b5403aaf6b (svn r815) Include strings.h only in the files which need it.
tron
parents: 485
diff changeset
     9
#include "table/strings.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    10
#include "engine.h"
1009
75140dc68759 (svn r1508) Remove duplicate declarations and include proper headers where necessary
tron
parents: 964
diff changeset
    11
#include "gfx.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    12
#include "player.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    13
#include "command.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    14
#include "vehicle.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    15
#include "news.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    16
#include "saveload.h"
2159
f6284cf5fab0 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2147
diff changeset
    17
#include "variables.h"
2708
48714bad9499 (svn r3252) - NewGRF: Map new train subtypes to old types for rail vehicles.
peter1138
parents: 2639
diff changeset
    18
#include "train.h"
5854
9eee280920f0 (svn r8428) -Codechange: Add proper names to aircraft subtypes instead of magic numbers and add a function IsNormalAircraft() which tells us whether the aircraft is in fact some flying device or a rotor/shadow.
Darkvater
parents: 5840
diff changeset
    19
#include "aircraft.h"
3866
b13553406a0a (svn r4902) Newgrf : Introduction of the newgrf_cargo files.
belugas
parents: 3865
diff changeset
    20
#include "newgrf_cargo.h"
4261
28670f743746 (svn r5887) -Cleanup: move date related functions, defines and variables to date.[ch]
rubidium
parents: 4171
diff changeset
    21
#include "date.h"
4322
f40bbdf476b3 (svn r5975) -Cleanup: use ORIGINAL_BASE_YEAR & ORIGINAL_MAX_YEAR where the functions really depend on the original date format.
rubidium
parents: 4300
diff changeset
    22
#include "table/engines.h"
6643
f81bee57bc09 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 6606
diff changeset
    23
#include "group.h"
7097
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 6950
diff changeset
    24
#include "string.h"
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 6950
diff changeset
    25
#include "strings.h"
7388
0a3a9cc1a98b (svn r10757) -Codechange: make the engine renew struct use the pool item class as super class.
rubidium
parents: 7258
diff changeset
    26
#include "misc/autoptr.hpp"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    27
2763
a4d065e3261b (svn r3308) BAD
tron
parents: 2708
diff changeset
    28
EngineInfo _engine_info[TOTAL_NUM_ENGINES];
a4d065e3261b (svn r3308) BAD
tron
parents: 2708
diff changeset
    29
RailVehicleInfo _rail_vehicle_info[NUM_TRAIN_ENGINES];
a4d065e3261b (svn r3308) BAD
tron
parents: 2708
diff changeset
    30
ShipVehicleInfo _ship_vehicle_info[NUM_SHIP_ENGINES];
a4d065e3261b (svn r3308) BAD
tron
parents: 2708
diff changeset
    31
AircraftVehicleInfo _aircraft_vehicle_info[NUM_AIRCRAFT_ENGINES];
a4d065e3261b (svn r3308) BAD
tron
parents: 2708
diff changeset
    32
RoadVehicleInfo _road_vehicle_info[NUM_ROAD_ENGINES];
a4d065e3261b (svn r3308) BAD
tron
parents: 2708
diff changeset
    33
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    34
enum {
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4297
diff changeset
    35
	YEAR_ENGINE_AGING_STOPS = 2050,
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4297
diff changeset
    36
};
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4297
diff changeset
    37
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    38
2477
225b2916fe2a (svn r3003) Change int, uint, uin16 and byte to EngineID where appropriate, plus some related changes (mostly casts)
tron
parents: 2469
diff changeset
    39
void ShowEnginePreviewWindow(EngineID engine);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    40
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6209
diff changeset
    41
void DeleteCustomEngineNames()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    42
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    43
	uint i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    44
	StringID old;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    45
1474
a26a21fa10ef (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
    46
	for (i = 0; i != TOTAL_NUM_ENGINES; i++) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    47
		old = _engine_name_strings[i];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    48
		_engine_name_strings[i] = i + STR_8000_KIRBY_PAUL_TANK_STEAM;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    49
		DeleteName(old);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    50
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    51
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    52
	_vehicle_design_names &= ~1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    53
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    54
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6209
diff changeset
    55
void LoadCustomEngineNames()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    56
{
4432
372316ca7924 (svn r6195) -Codechange: do not use '//' in '/* ... */' style comments and '/*' or '*/' in '//' style comments.
rubidium
parents: 4384
diff changeset
    57
	/* XXX: not done */
5380
8ea58542b6e0 (svn r7565) -Codechange: Rework DEBUG functionality. Look for appropiate debugging levels to
Darkvater
parents: 5216
diff changeset
    58
	DEBUG(misc, 1, "LoadCustomEngineNames: not done");
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    59
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    60
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6209
diff changeset
    61
static void SetupEngineNames()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    62
{
1474
a26a21fa10ef (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
    63
	StringID *name;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    64
1474
a26a21fa10ef (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
    65
	for (name = _engine_name_strings; name != endof(_engine_name_strings); name++)
a26a21fa10ef (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
    66
		*name = STR_SV_EMPTY;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    67
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    68
	DeleteCustomEngineNames();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    69
	LoadCustomEngineNames();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    70
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    71
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    72
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    73
static void CalcEngineReliability(Engine *e)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    74
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    75
	uint age = e->age;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    76
6606
cc9f292f7223 (svn r9825) -Codechange: [NewGRF] Add support for early vehicle retirement.
peter1138
parents: 6530
diff changeset
    77
	/* Check for early retirement */
cc9f292f7223 (svn r9825) -Codechange: [NewGRF] Add support for early vehicle retirement.
peter1138
parents: 6530
diff changeset
    78
	if (e->player_avail != 0 && !_patches.never_expire_vehicles) {
cc9f292f7223 (svn r9825) -Codechange: [NewGRF] Add support for early vehicle retirement.
peter1138
parents: 6530
diff changeset
    79
		uint retire_early = EngInfo(e - _engines)->retire_early;
cc9f292f7223 (svn r9825) -Codechange: [NewGRF] Add support for early vehicle retirement.
peter1138
parents: 6530
diff changeset
    80
		if (retire_early > 0 && age >= e->duration_phase_1 + e->duration_phase_2 - retire_early * 12) {
cc9f292f7223 (svn r9825) -Codechange: [NewGRF] Add support for early vehicle retirement.
peter1138
parents: 6530
diff changeset
    81
			/* Early retirement is enabled and we're past the date... */
cc9f292f7223 (svn r9825) -Codechange: [NewGRF] Add support for early vehicle retirement.
peter1138
parents: 6530
diff changeset
    82
			e->player_avail = 0;
cc9f292f7223 (svn r9825) -Codechange: [NewGRF] Add support for early vehicle retirement.
peter1138
parents: 6530
diff changeset
    83
			AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
cc9f292f7223 (svn r9825) -Codechange: [NewGRF] Add support for early vehicle retirement.
peter1138
parents: 6530
diff changeset
    84
		}
cc9f292f7223 (svn r9825) -Codechange: [NewGRF] Add support for early vehicle retirement.
peter1138
parents: 6530
diff changeset
    85
	}
cc9f292f7223 (svn r9825) -Codechange: [NewGRF] Add support for early vehicle retirement.
peter1138
parents: 6530
diff changeset
    86
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    87
	if (age < e->duration_phase_1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    88
		uint start = e->reliability_start;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    89
		e->reliability = age * (e->reliability_max - start) / e->duration_phase_1 + start;
4767
df84b7f59e5b (svn r6681) -Fix: when vehicles never expire they will stay at peak reliability instead of the lowest to make them useful even when old
bjarni
parents: 4766
diff changeset
    90
	} else if ((age -= e->duration_phase_1) < e->duration_phase_2 || _patches.never_expire_vehicles) {
df84b7f59e5b (svn r6681) -Fix: when vehicles never expire they will stay at peak reliability instead of the lowest to make them useful even when old
bjarni
parents: 4766
diff changeset
    91
		/* We are at the peak of this engines life. It will have max reliability.
df84b7f59e5b (svn r6681) -Fix: when vehicles never expire they will stay at peak reliability instead of the lowest to make them useful even when old
bjarni
parents: 4766
diff changeset
    92
		 * This is also true if the engines never expire. They will not go bad over time */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    93
		e->reliability = e->reliability_max;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    94
	} else if ((age -= e->duration_phase_2) < e->duration_phase_3) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    95
		uint max = e->reliability_max;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    96
		e->reliability = (int)age * (int)(e->reliability_final - max) / e->duration_phase_3 + max;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    97
	} else {
4767
df84b7f59e5b (svn r6681) -Fix: when vehicles never expire they will stay at peak reliability instead of the lowest to make them useful even when old
bjarni
parents: 4766
diff changeset
    98
		/* time's up for this engine.
df84b7f59e5b (svn r6681) -Fix: when vehicles never expire they will stay at peak reliability instead of the lowest to make them useful even when old
bjarni
parents: 4766
diff changeset
    99
		 * We will now completely retire this design */
df84b7f59e5b (svn r6681) -Fix: when vehicles never expire they will stay at peak reliability instead of the lowest to make them useful even when old
bjarni
parents: 4766
diff changeset
   100
		e->player_avail = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   101
		e->reliability = e->reliability_final;
5944
6d059cc8662b (svn r8610) -Codechange/Feature: rewrote the list handling in the autoreplace window
bjarni
parents: 5868
diff changeset
   102
		/* Kick this engine out of the lists */
6d059cc8662b (svn r8610) -Codechange/Feature: rewrote the list handling in the autoreplace window
bjarni
parents: 5868
diff changeset
   103
		AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   104
	}
4767
df84b7f59e5b (svn r6681) -Fix: when vehicles never expire they will stay at peak reliability instead of the lowest to make them useful even when old
bjarni
parents: 4766
diff changeset
   105
	InvalidateWindowClasses(WC_BUILD_VEHICLE); // Update to show the new reliability
5944
6d059cc8662b (svn r8610) -Codechange/Feature: rewrote the list handling in the autoreplace window
bjarni
parents: 5868
diff changeset
   106
	InvalidateWindowClasses(WC_REPLACE_VEHICLE);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   107
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   108
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6209
diff changeset
   109
void AddTypeToEngines()
842
efc3546bc313 (svn r1323) Adding autoreplace feature
bjarni
parents: 819
diff changeset
   110
{
2242
512eae2cf006 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   111
	Engine* e = _engines;
842
efc3546bc313 (svn r1323) Adding autoreplace feature
bjarni
parents: 819
diff changeset
   112
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6247
diff changeset
   113
	do e->type = VEH_TRAIN;    while (++e < &_engines[ROAD_ENGINES_INDEX]);
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6247
diff changeset
   114
	do e->type = VEH_ROAD;     while (++e < &_engines[SHIP_ENGINES_INDEX]);
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6247
diff changeset
   115
	do e->type = VEH_SHIP;     while (++e < &_engines[AIRCRAFT_ENGINES_INDEX]);
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6247
diff changeset
   116
	do e->type = VEH_AIRCRAFT; while (++e < &_engines[TOTAL_NUM_ENGINES]);
842
efc3546bc313 (svn r1323) Adding autoreplace feature
bjarni
parents: 819
diff changeset
   117
}
efc3546bc313 (svn r1323) Adding autoreplace feature
bjarni
parents: 819
diff changeset
   118
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6209
diff changeset
   119
void StartupEngines()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   120
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   121
	Engine *e;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   122
	const EngineInfo *ei;
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4297
diff changeset
   123
	/* Aging of vehicles stops, so account for that when starting late */
4322
f40bbdf476b3 (svn r5975) -Cleanup: use ORIGINAL_BASE_YEAR & ORIGINAL_MAX_YEAR where the functions really depend on the original date format.
rubidium
parents: 4300
diff changeset
   124
	const Date aging_date = min(_date, ConvertYMDToDate(YEAR_ENGINE_AGING_STOPS, 0, 1));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   125
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   126
	SetupEngineNames();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   127
2242
512eae2cf006 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   128
	for (e = _engines, ei = _engine_info; e != endof(_engines); e++, ei++) {
512eae2cf006 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   129
		uint32 r;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   130
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   131
		e->age = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   132
		e->flags = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   133
		e->player_avail = 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 114
diff changeset
   134
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5971
diff changeset
   135
		/* The magic value of 729 days below comes from the NewGRF spec. If the
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5971
diff changeset
   136
		 * base intro date is before 1922 then the random number of days is not
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5971
diff changeset
   137
		 * added. */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   138
		r = Random();
4297
d0311b62255c (svn r5934) -Cleanup: forgot some conversions to Year and to Date
rubidium
parents: 4293
diff changeset
   139
		e->intro_date = ei->base_intro <= ConvertYMDToDate(1922, 0, 1) ? ei->base_intro : (Date)GB(r, 0, 9) + ei->base_intro;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   140
		if (e->intro_date <= _date) {
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4297
diff changeset
   141
			e->age = (aging_date - e->intro_date) >> 5;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   142
			e->player_avail = (byte)-1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   143
			e->flags |= ENGINE_AVAILABLE;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   144
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   145
2140
a04d0142ad65 (svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents: 1988
diff changeset
   146
		e->reliability_start = GB(r, 16, 14) + 0x7AE0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   147
		r = Random();
2140
a04d0142ad65 (svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents: 1988
diff changeset
   148
		e->reliability_max   = GB(r,  0, 14) + 0xBFFF;
a04d0142ad65 (svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents: 1988
diff changeset
   149
		e->reliability_final = GB(r, 16, 14) + 0x3FFF;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   150
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   151
		r = Random();
2140
a04d0142ad65 (svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents: 1988
diff changeset
   152
		e->duration_phase_1 = GB(r, 0, 5) + 7;
a04d0142ad65 (svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents: 1988
diff changeset
   153
		e->duration_phase_2 = GB(r, 5, 4) + ei->base_life * 12 - 96;
a04d0142ad65 (svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents: 1988
diff changeset
   154
		e->duration_phase_3 = GB(r, 9, 7) + 120;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   155
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   156
		e->reliability_spd_dec = (ei->unk2&0x7F) << 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   157
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   158
		/* my invented flag for something that is a wagon */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   159
		if (ei->unk2 & 0x80) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   160
			e->age = 0xFFFF;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   161
		} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   162
			CalcEngineReliability(e);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   163
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   164
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   165
		e->lifelength = ei->lifelength + _patches.extend_vehicle_life;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   166
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5971
diff changeset
   167
		/* prevent certain engines from ever appearing. */
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7388
diff changeset
   168
		if (!HasBit(ei->climates, _opt.landscape)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   169
			e->flags |= ENGINE_AVAILABLE;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   170
			e->player_avail = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   171
		}
915
d845fe7cf6f2 (svn r1402) Trim trailing whitespace
tron
parents: 842
diff changeset
   172
819
c425b7e22f6a (svn r1290) Added type to typedef struct Engine and filled in the same data as in type in vehicle
bjarni
parents: 740
diff changeset
   173
		/* This sets up type for the engine
4549
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4432
diff changeset
   174
		 * It is needed if you want to ask the engine what type it is
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4432
diff changeset
   175
		 * It should hopefully be the same as when you ask a vehicle what it is
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4432
diff changeset
   176
		 * but using this, you can ask what type an engine number is
106ed18a7675 (svn r6381) -Cleanup: make the '/* */' comments that span multiple lines more uniform.
rubidium
parents: 4432
diff changeset
   177
		 * even if it is not a vehicle (yet)*/
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   178
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   179
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   180
5823
7aa8c2312103 (svn r8385) -Fix
tron
parents: 5763
diff changeset
   181
static void AcceptEnginePreview(EngineID eid, PlayerID player)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   182
{
5823
7aa8c2312103 (svn r8385) -Fix
tron
parents: 5763
diff changeset
   183
	Engine *e = GetEngine(eid);
6685
00490513d7b1 (svn r9917) -Codechange: prepare some more areas for more road types.
rubidium
parents: 6643
diff changeset
   184
	Player *p = GetPlayer(player);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   185
7931
b0a46cd92225 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 7928
diff changeset
   186
	SetBit(e->player_avail, player);
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6247
diff changeset
   187
	if (e->type == VEH_TRAIN) {
5823
7aa8c2312103 (svn r8385) -Fix
tron
parents: 5763
diff changeset
   188
		const RailVehicleInfo *rvi = RailVehInfo(eid);
7aa8c2312103 (svn r8385) -Fix
tron
parents: 5763
diff changeset
   189
7aa8c2312103 (svn r8385) -Fix
tron
parents: 5763
diff changeset
   190
		assert(rvi->railtype < RAILTYPE_END);
7931
b0a46cd92225 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 7928
diff changeset
   191
		SetBit(p->avail_railtypes, rvi->railtype);
6685
00490513d7b1 (svn r9917) -Codechange: prepare some more areas for more road types.
rubidium
parents: 6643
diff changeset
   192
	} else if (e->type == VEH_ROAD) {
7931
b0a46cd92225 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 7928
diff changeset
   193
		SetBit(p->avail_roadtypes, HasBit(EngInfo(eid)->misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD);
5823
7aa8c2312103 (svn r8385) -Fix
tron
parents: 5763
diff changeset
   194
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   195
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   196
	e->preview_player = INVALID_PLAYER;
4766
aa276d0ec2eb (svn r6680) -Codechange r6679: [train build window] only generate the list when the window data is invalidated or the window is generated, not on each redraw
bjarni
parents: 4549
diff changeset
   197
	if (player == _local_player) {
5944
6d059cc8662b (svn r8610) -Codechange/Feature: rewrote the list handling in the autoreplace window
bjarni
parents: 5868
diff changeset
   198
		AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
4766
aa276d0ec2eb (svn r6680) -Codechange r6679: [train build window] only generate the list when the window data is invalidated or the window is generated, not on each redraw
bjarni
parents: 4549
diff changeset
   199
	}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   200
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   201
1786
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   202
static PlayerID GetBestPlayer(PlayerID pp)
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   203
{
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   204
	const Player *p;
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   205
	int32 best_hist;
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   206
	PlayerID best_player;
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   207
	uint mask = 0;
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   208
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   209
	do {
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   210
		best_hist = -1;
4848
45f848b46222 (svn r6774) -Codechange: Rename the badly named OWNER_SPECTATOR to PLAYER_SPECTATOR and
Darkvater
parents: 4845
diff changeset
   211
		best_player = PLAYER_SPECTATOR;
1786
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   212
		FOR_ALL_PLAYERS(p) {
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7388
diff changeset
   213
			if (p->is_active && p->block_preview == 0 && !HasBit(mask, p->index) &&
1786
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   214
					p->old_economy[0].performance_history > best_hist) {
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   215
				best_hist = p->old_economy[0].performance_history;
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   216
				best_player = p->index;
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   217
			}
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   218
		}
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   219
4848
45f848b46222 (svn r6774) -Codechange: Rename the badly named OWNER_SPECTATOR to PLAYER_SPECTATOR and
Darkvater
parents: 4845
diff changeset
   220
		if (best_player == PLAYER_SPECTATOR) return PLAYER_SPECTATOR;
1786
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   221
7931
b0a46cd92225 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 7928
diff changeset
   222
		SetBit(mask, best_player);
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   223
	} while (pp--, pp != 0);
1786
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   224
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   225
	return best_player;
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   226
}
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   227
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6209
diff changeset
   228
void EnginesDailyLoop()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   229
{
2477
225b2916fe2a (svn r3003) Change int, uint, uin16 and byte to EngineID where appropriate, plus some related changes (mostly casts)
tron
parents: 2469
diff changeset
   230
	EngineID i;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   231
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4297
diff changeset
   232
	if (_cur_year >= YEAR_ENGINE_AGING_STOPS) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   233
2242
512eae2cf006 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   234
	for (i = 0; i != lengthof(_engines); i++) {
4171
5c6e60c392c3 (svn r5609) CodeChange : Apply coding style
belugas
parents: 4077
diff changeset
   235
		Engine *e = &_engines[i];
2242
512eae2cf006 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   236
6174
0c743995213f (svn r8944) -Codechange: Move the enum describing the bitmask of Engine.flags to engine.h and give the enum values better names.
maedhros
parents: 6125
diff changeset
   237
		if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) {
0c743995213f (svn r8944) -Codechange: Move the enum describing the bitmask of Engine.flags to engine.h and give the enum values better names.
maedhros
parents: 6125
diff changeset
   238
			if (e->flags & ENGINE_OFFER_WINDOW_OPEN) {
740
ab84ecdf86a2 (svn r1196) -Fix: Preview Vehicle had a small glitch. Fixed now.
truelight
parents: 555
diff changeset
   239
				if (e->preview_player != 0xFF && !--e->preview_wait) {
6174
0c743995213f (svn r8944) -Codechange: Move the enum describing the bitmask of Engine.flags to engine.h and give the enum values better names.
maedhros
parents: 6125
diff changeset
   240
					e->flags &= ~ENGINE_OFFER_WINDOW_OPEN;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   241
					DeleteWindowById(WC_ENGINE_PREVIEW, i);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   242
					e->preview_player++;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 114
diff changeset
   243
				}
4077
d4d440dd8925 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3866
diff changeset
   244
			} else if (e->preview_player != 0xFF) {
1786
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   245
				PlayerID best_player = GetBestPlayer(e->preview_player);
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   246
4848
45f848b46222 (svn r6774) -Codechange: Rename the badly named OWNER_SPECTATOR to PLAYER_SPECTATOR and
Darkvater
parents: 4845
diff changeset
   247
				if (best_player == PLAYER_SPECTATOR) {
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   248
					e->preview_player = INVALID_PLAYER;
1786
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   249
					continue;
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   250
				}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 114
diff changeset
   251
4845
72bbb6e2694e (svn r6771) -Codechange: Replace two macros with functions. IS_HUMAN_PLAYER and
Darkvater
parents: 4792
diff changeset
   252
				if (!IsHumanPlayer(best_player)) {
1786
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   253
					/* XXX - TTDBUG: TTD has a bug here ???? */
5823
7aa8c2312103 (svn r8385) -Fix
tron
parents: 5763
diff changeset
   254
					AcceptEnginePreview(i, best_player);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   255
				} else {
6174
0c743995213f (svn r8944) -Codechange: Move the enum describing the bitmask of Engine.flags to engine.h and give the enum values better names.
maedhros
parents: 6125
diff changeset
   256
					e->flags |= ENGINE_OFFER_WINDOW_OPEN;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   257
					e->preview_wait = 20;
4845
72bbb6e2694e (svn r6771) -Codechange: Replace two macros with functions. IS_HUMAN_PLAYER and
Darkvater
parents: 4792
diff changeset
   258
					if (IsInteractivePlayer(best_player)) ShowEnginePreviewWindow(i);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   259
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   260
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   261
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   262
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   263
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   264
1786
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   265
/** Accept an engine prototype. XXX - it is possible that the top-player
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   266
 * changes while you are waiting to accept the offer? Then it becomes invalid
3491
35d747bb5e82 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3222
diff changeset
   267
 * @param tile unused
6481
85ad87daf4b0 (svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents: 6259
diff changeset
   268
 * @param flags operation to perfom
1786
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   269
 * @param p1 engine-prototype offered
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   270
 * @param p2 unused
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   271
 */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6686
diff changeset
   272
CommandCost CmdWantEnginePreview(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   273
{
1786
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   274
	Engine *e;
2951
2de6d3a59743 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2875
diff changeset
   275
1786
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   276
	if (!IsEngineIndex(p1)) return CMD_ERROR;
1926
530480d14685 (svn r2432) Use GetEngine() instead of DEREF_ENGINE() or even _engines[]
tron
parents: 1908
diff changeset
   277
	e = GetEngine(p1);
1786
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   278
	if (GetBestPlayer(e->preview_player) != _current_player) return CMD_ERROR;
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   279
5823
7aa8c2312103 (svn r8385) -Fix
tron
parents: 5763
diff changeset
   280
	if (flags & DC_EXEC) AcceptEnginePreview(p1, _current_player);
1786
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   281
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
   282
	return CommandCost();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   283
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   284
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5971
diff changeset
   285
/* Determine if an engine type is a wagon (and not a loco) */
2477
225b2916fe2a (svn r3003) Change int, uint, uin16 and byte to EngineID where appropriate, plus some related changes (mostly casts)
tron
parents: 2469
diff changeset
   286
static bool IsWagon(EngineID index)
257
0ff8a2e60a0d (svn r262) Fix: [ 1028234 ] Monorail and MagLev infrastructure not available in 1920s any more
dominik
parents: 220
diff changeset
   287
{
5868
94430141c189 (svn r8455) -Codechange: Give a more meaningful name (railveh_type)to member flags of RailVehInfo, as well as changing the code to reflect the fact that it was not a flag but rather a one value only variable. Doing so, some evaluations have been simplified.
belugas
parents: 5854
diff changeset
   288
	return index < NUM_TRAIN_ENGINES && RailVehInfo(index)->railveh_type == RAILVEH_WAGON;
257
0ff8a2e60a0d (svn r262) Fix: [ 1028234 ] Monorail and MagLev infrastructure not available in 1920s any more
dominik
parents: 220
diff changeset
   289
}
0ff8a2e60a0d (svn r262) Fix: [ 1028234 ] Monorail and MagLev infrastructure not available in 1920s any more
dominik
parents: 220
diff changeset
   290
410
0efd84450b01 (svn r607) -Patch: [ 985102 ] static cleanup
tron
parents: 408
diff changeset
   291
static void NewVehicleAvailable(Engine *e)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   292
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   293
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   294
	Player *p;
2477
225b2916fe2a (svn r3003) Change int, uint, uin16 and byte to EngineID where appropriate, plus some related changes (mostly casts)
tron
parents: 2469
diff changeset
   295
	EngineID index = e - _engines;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   296
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5971
diff changeset
   297
	/* In case the player didn't build the vehicle during the intro period,
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5971
diff changeset
   298
	 * prevent that player from getting future intro periods for a while. */
6174
0c743995213f (svn r8944) -Codechange: Move the enum describing the bitmask of Engine.flags to engine.h and give the enum values better names.
maedhros
parents: 6125
diff changeset
   299
	if (e->flags & ENGINE_EXCLUSIVE_PREVIEW) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   300
		FOR_ALL_PLAYERS(p) {
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   301
			uint block_preview = p->block_preview;
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   302
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7388
diff changeset
   303
			if (!HasBit(e->player_avail, p->index)) continue;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 114
diff changeset
   304
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   305
			/* We assume the user did NOT build it.. prove me wrong ;) */
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   306
			p->block_preview = 20;
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   307
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   308
			FOR_ALL_VEHICLES(v) {
6259
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6247
diff changeset
   309
				if (v->type == VEH_TRAIN || v->type == VEH_ROAD || v->type == VEH_SHIP ||
471b91a4b1d8 (svn r9068) -Codechange: capitalize the VEH_Train etc. enums to match the coding style (and rest of the code).
rubidium
parents: 6247
diff changeset
   310
						(v->type == VEH_AIRCRAFT && IsNormalAircraft(v))) {
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   311
					if (v->owner == p->index && v->engine_type == index) {
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   312
						/* The user did prove me wrong, so restore old value */
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   313
						p->block_preview = block_preview;
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   314
						break;
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   315
					}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   316
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   317
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   318
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   319
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   320
6174
0c743995213f (svn r8944) -Codechange: Move the enum describing the bitmask of Engine.flags to engine.h and give the enum values better names.
maedhros
parents: 6125
diff changeset
   321
	e->flags = (e->flags & ~ENGINE_EXCLUSIVE_PREVIEW) | ENGINE_AVAILABLE;
5944
6d059cc8662b (svn r8610) -Codechange/Feature: rewrote the list handling in the autoreplace window
bjarni
parents: 5868
diff changeset
   322
	AddRemoveEngineFromAutoreplaceAndBuildWindows(e->type);
114
6a2af0c2d0db (svn r115) Fix: monorail/maglev became available around 1927
dominik
parents: 0
diff changeset
   323
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5971
diff changeset
   324
	/* Now available for all players */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   325
	e->player_avail = (byte)-1;
114
6a2af0c2d0db (svn r115) Fix: monorail/maglev became available around 1927
dominik
parents: 0
diff changeset
   326
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5971
diff changeset
   327
	/* Do not introduce new rail wagons */
2639
eeaefdabfdfd (svn r3181) -Bracing
tron
parents: 2611
diff changeset
   328
	if (IsWagon(index)) return;
114
6a2af0c2d0db (svn r115) Fix: monorail/maglev became available around 1927
dominik
parents: 0
diff changeset
   329
5823
7aa8c2312103 (svn r8385) -Fix
tron
parents: 5763
diff changeset
   330
	if (index < NUM_TRAIN_ENGINES) {
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5971
diff changeset
   331
		/* maybe make another rail type available */
5823
7aa8c2312103 (svn r8385) -Fix
tron
parents: 5763
diff changeset
   332
		RailType railtype = RailVehInfo(index)->railtype;
7aa8c2312103 (svn r8385) -Fix
tron
parents: 5763
diff changeset
   333
		assert(railtype < RAILTYPE_END);
7aa8c2312103 (svn r8385) -Fix
tron
parents: 5763
diff changeset
   334
		FOR_ALL_PLAYERS(p) {
7931
b0a46cd92225 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 7928
diff changeset
   335
			if (p->is_active) SetBit(p->avail_railtypes, railtype);
2147
eb6ba42fd216 (svn r2657) -Codechange: The available railtypes per player are now a bitmask, so
celestar
parents: 2140
diff changeset
   336
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   337
	}
6686
9e8b32366bd6 (svn r9918) -Fix: the available roadtypes/railtypes were not updated on GRF updates.
rubidium
parents: 6685
diff changeset
   338
	if ((index - NUM_TRAIN_ENGINES) < NUM_ROAD_ENGINES) {
9e8b32366bd6 (svn r9918) -Fix: the available roadtypes/railtypes were not updated on GRF updates.
rubidium
parents: 6685
diff changeset
   339
		/* maybe make another road type available */
9e8b32366bd6 (svn r9918) -Fix: the available roadtypes/railtypes were not updated on GRF updates.
rubidium
parents: 6685
diff changeset
   340
		FOR_ALL_PLAYERS(p) {
7931
b0a46cd92225 (svn r11484) -Codechange: Remove the doubled function SetBitT and rename the remaining to fit with the naming style
skidd13
parents: 7928
diff changeset
   341
			if (p->is_active) SetBit(p->avail_roadtypes, HasBit(EngInfo(index)->misc_flags, EF_ROAD_TRAM) ? ROADTYPE_TRAM : ROADTYPE_ROAD);
6686
9e8b32366bd6 (svn r9918) -Fix: the available roadtypes/railtypes were not updated on GRF updates.
rubidium
parents: 6685
diff changeset
   342
		}
9e8b32366bd6 (svn r9918) -Fix: the available roadtypes/railtypes were not updated on GRF updates.
rubidium
parents: 6685
diff changeset
   343
	}
6209
3a1a90cd67f3 (svn r9001) -Codechange: remove duplication of functions and strings with respect to the news of new vehicles.
rubidium
parents: 6174
diff changeset
   344
	AddNewsItem(index, NEWS_FLAGS(NM_CALLBACK, 0, NT_NEW_VEHICLES, DNC_VEHICLEAVAIL), 0, 0);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   345
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   346
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6209
diff changeset
   347
void EnginesMonthlyLoop()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   348
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   349
	Engine *e;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   350
4300
c7e43c47a2b9 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4297
diff changeset
   351
	if (_cur_year < YEAR_ENGINE_AGING_STOPS) {
2242
512eae2cf006 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   352
		for (e = _engines; e != endof(_engines); e++) {
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5971
diff changeset
   353
			/* Age the vehicle */
2242
512eae2cf006 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   354
			if (e->flags & ENGINE_AVAILABLE && e->age != 0xFFFF) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   355
				e->age++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   356
				CalcEngineReliability(e);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   357
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   358
2990
2bbe7769ebd4 (svn r3565) - Fix: On loading a game, GetPlayerRailtypes() didn't account for the fact that vehicles are introduced a year after their introduction date. This will also relieve possible (rare) network desyncs.
peter1138
parents: 2962
diff changeset
   359
			if (!(e->flags & ENGINE_AVAILABLE) && _date >= (e->intro_date + 365)) {
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5971
diff changeset
   360
				/* Introduce it to all players */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   361
				NewVehicleAvailable(e);
6174
0c743995213f (svn r8944) -Codechange: Move the enum describing the bitmask of Engine.flags to engine.h and give the enum values better names.
maedhros
parents: 6125
diff changeset
   362
			} else if (!(e->flags & (ENGINE_AVAILABLE|ENGINE_EXCLUSIVE_PREVIEW)) && _date >= e->intro_date) {
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5971
diff changeset
   363
				/* Introduction date has passed.. show introducing dialog to one player. */
6174
0c743995213f (svn r8944) -Codechange: Move the enum describing the bitmask of Engine.flags to engine.h and give the enum values better names.
maedhros
parents: 6125
diff changeset
   364
				e->flags |= ENGINE_EXCLUSIVE_PREVIEW;
257
0ff8a2e60a0d (svn r262) Fix: [ 1028234 ] Monorail and MagLev infrastructure not available in 1920s any more
dominik
parents: 220
diff changeset
   365
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5971
diff changeset
   366
				/* Do not introduce new rail wagons */
964
fe9e7b220b9f (svn r1456) Simplify isWagon(), make it static and give it a canonical name (IsWagon)
tron
parents: 938
diff changeset
   367
				if (!IsWagon(e - _engines))
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   368
					e->preview_player = (PlayerID)1; // Give to the player with the highest rating.
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   369
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   370
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   371
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   372
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   373
7097
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 6950
diff changeset
   374
static bool IsUniqueEngineName(const char *name)
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 6950
diff changeset
   375
{
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 6950
diff changeset
   376
	char buf[512];
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 6950
diff changeset
   377
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 6950
diff changeset
   378
	for (EngineID i = 0; i < TOTAL_NUM_ENGINES; i++) {
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 6950
diff changeset
   379
		SetDParam(0, i);
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 6950
diff changeset
   380
		GetString(buf, STR_ENGINE_NAME, lastof(buf));
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 6950
diff changeset
   381
		if (strcmp(buf, name) == 0) return false;
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 6950
diff changeset
   382
	}
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 6950
diff changeset
   383
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 6950
diff changeset
   384
	return true;
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 6950
diff changeset
   385
}
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 6950
diff changeset
   386
1786
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   387
/** Rename an engine.
3491
35d747bb5e82 (svn r4342) Change the first two parameters of commands - virtual pixel coordinates of the tile to operate on - to a TileIndex
tron
parents: 3222
diff changeset
   388
 * @param tile unused
6481
85ad87daf4b0 (svn r9662) -Documentation: Doxygen corrections and @file omissions
belugas
parents: 6259
diff changeset
   389
 * @param flags operation to perfom
1786
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   390
 * @param p1 engine ID to rename
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   391
 * @param p2 unused
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   392
 */
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6686
diff changeset
   393
CommandCost CmdRenameEngine(TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   394
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   395
	StringID str;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   396
7097
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 6950
diff changeset
   397
	if (!IsEngineIndex(p1) || StrEmpty(_cmd_text)) return CMD_ERROR;
1786
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   398
7097
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 6950
diff changeset
   399
	if (!IsUniqueEngineName(_cmd_text)) return_cmd_error(STR_NAME_MUST_BE_UNIQUE);
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 6950
diff changeset
   400
bc497a49fb45 (svn r10364) -Fix [FS#706]: checking for duplicate custom names was inconsistent, and tested all 'namespaces'. now only check names of the same type.
peter1138
parents: 6950
diff changeset
   401
	str = AllocateName(_cmd_text, 0);
1786
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   402
	if (str == 0) return CMD_ERROR;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 114
diff changeset
   403
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   404
	if (flags & DC_EXEC) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   405
		StringID old_str = _engine_name_strings[p1];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   406
		_engine_name_strings[p1] = str;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   407
		DeleteName(old_str);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   408
		_vehicle_design_names |= 3;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   409
		MarkWholeScreenDirty();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   410
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   411
		DeleteName(str);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   412
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   413
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
   414
	return CommandCost();
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   415
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   416
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   417
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   418
/*
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   419
 * returns true if an engine is valid, of the specified type, and buildable by
5215
b8fb685df4c5 (svn r7330) -Fix (r7304): Data invalidation doesn't always happen as the local
peter1138
parents: 5037
diff changeset
   420
 * the given player, false otherwise
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   421
 *
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   422
 * engine = index of the engine to check
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   423
 * type   = the type the engine should be of (VEH_xxx)
5215
b8fb685df4c5 (svn r7330) -Fix (r7304): Data invalidation doesn't always happen as the local
peter1138
parents: 5037
diff changeset
   424
 * player = index of the player
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   425
 */
5215
b8fb685df4c5 (svn r7330) -Fix (r7304): Data invalidation doesn't always happen as the local
peter1138
parents: 5037
diff changeset
   426
bool IsEngineBuildable(EngineID engine, byte type, PlayerID player)
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   427
{
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   428
	const Engine *e;
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   429
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5971
diff changeset
   430
	/* check if it's an engine that is in the engine array */
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   431
	if (!IsEngineIndex(engine)) return false;
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   432
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   433
	e = GetEngine(engine);
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   434
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5971
diff changeset
   435
	/* check if it's an engine of specified type */
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   436
	if (e->type != type) return false;
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   437
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5971
diff changeset
   438
	/* check if it's available */
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7388
diff changeset
   439
	if (!HasBit(e->player_avail, player)) return false;
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   440
6530
06785f74984c (svn r9718) -Fix: Don't allow building of rail vehicles whose railtype isn't available yet. This also affects the rail purchase window.
peter1138
parents: 6505
diff changeset
   441
	if (type == VEH_TRAIN) {
06785f74984c (svn r9718) -Fix: Don't allow building of rail vehicles whose railtype isn't available yet. This also affects the rail purchase window.
peter1138
parents: 6505
diff changeset
   442
		/* Check if the rail type is available to this player */
06785f74984c (svn r9718) -Fix: Don't allow building of rail vehicles whose railtype isn't available yet. This also affects the rail purchase window.
peter1138
parents: 6505
diff changeset
   443
		const Player *p = GetPlayer(player);
7928
63e18de69e50 (svn r11481) -Codechange: Rename the HASBIT function to fit with the naming style
skidd13
parents: 7388
diff changeset
   444
		if (!HasBit(p->avail_railtypes, RailVehInfo(engine)->railtype)) return false;
6530
06785f74984c (svn r9718) -Fix: Don't allow building of rail vehicles whose railtype isn't available yet. This also affects the rail purchase window.
peter1138
parents: 6505
diff changeset
   445
	}
06785f74984c (svn r9718) -Fix: Don't allow building of rail vehicles whose railtype isn't available yet. This also affects the rail purchase window.
peter1138
parents: 6505
diff changeset
   446
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   447
	return true;
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   448
}
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   449
6505
8b4c20ddacd3 (svn r9688) -Codechange: Created a function to get default cargo type for a cargo type
bjarni
parents: 6481
diff changeset
   450
/** Get the default cargo type for a certain engine type
8b4c20ddacd3 (svn r9688) -Codechange: Created a function to get default cargo type for a cargo type
bjarni
parents: 6481
diff changeset
   451
 * @param engine The ID to get the cargo for
8b4c20ddacd3 (svn r9688) -Codechange: Created a function to get default cargo type for a cargo type
bjarni
parents: 6481
diff changeset
   452
 * @return The cargo type. CT_INVALID means no cargo capacity
8b4c20ddacd3 (svn r9688) -Codechange: Created a function to get default cargo type for a cargo type
bjarni
parents: 6481
diff changeset
   453
 */
8b4c20ddacd3 (svn r9688) -Codechange: Created a function to get default cargo type for a cargo type
bjarni
parents: 6481
diff changeset
   454
CargoID GetEngineCargoType(EngineID engine)
8b4c20ddacd3 (svn r9688) -Codechange: Created a function to get default cargo type for a cargo type
bjarni
parents: 6481
diff changeset
   455
{
8b4c20ddacd3 (svn r9688) -Codechange: Created a function to get default cargo type for a cargo type
bjarni
parents: 6481
diff changeset
   456
	assert(IsEngineIndex(engine));
8b4c20ddacd3 (svn r9688) -Codechange: Created a function to get default cargo type for a cargo type
bjarni
parents: 6481
diff changeset
   457
8b4c20ddacd3 (svn r9688) -Codechange: Created a function to get default cargo type for a cargo type
bjarni
parents: 6481
diff changeset
   458
	switch (GetEngine(engine)->type) {
8b4c20ddacd3 (svn r9688) -Codechange: Created a function to get default cargo type for a cargo type
bjarni
parents: 6481
diff changeset
   459
		case VEH_TRAIN:
8b4c20ddacd3 (svn r9688) -Codechange: Created a function to get default cargo type for a cargo type
bjarni
parents: 6481
diff changeset
   460
			if (RailVehInfo(engine)->capacity == 0) return CT_INVALID;
8b4c20ddacd3 (svn r9688) -Codechange: Created a function to get default cargo type for a cargo type
bjarni
parents: 6481
diff changeset
   461
			return RailVehInfo(engine)->cargo_type;
8b4c20ddacd3 (svn r9688) -Codechange: Created a function to get default cargo type for a cargo type
bjarni
parents: 6481
diff changeset
   462
8b4c20ddacd3 (svn r9688) -Codechange: Created a function to get default cargo type for a cargo type
bjarni
parents: 6481
diff changeset
   463
		case VEH_ROAD:
8b4c20ddacd3 (svn r9688) -Codechange: Created a function to get default cargo type for a cargo type
bjarni
parents: 6481
diff changeset
   464
			if (RoadVehInfo(engine)->capacity == 0) return CT_INVALID;
8b4c20ddacd3 (svn r9688) -Codechange: Created a function to get default cargo type for a cargo type
bjarni
parents: 6481
diff changeset
   465
			return RoadVehInfo(engine)->cargo_type;
8b4c20ddacd3 (svn r9688) -Codechange: Created a function to get default cargo type for a cargo type
bjarni
parents: 6481
diff changeset
   466
8b4c20ddacd3 (svn r9688) -Codechange: Created a function to get default cargo type for a cargo type
bjarni
parents: 6481
diff changeset
   467
		case VEH_SHIP:
8b4c20ddacd3 (svn r9688) -Codechange: Created a function to get default cargo type for a cargo type
bjarni
parents: 6481
diff changeset
   468
			if (ShipVehInfo(engine)->capacity == 0) return CT_INVALID;
8b4c20ddacd3 (svn r9688) -Codechange: Created a function to get default cargo type for a cargo type
bjarni
parents: 6481
diff changeset
   469
			return ShipVehInfo(engine)->cargo_type;
8b4c20ddacd3 (svn r9688) -Codechange: Created a function to get default cargo type for a cargo type
bjarni
parents: 6481
diff changeset
   470
8b4c20ddacd3 (svn r9688) -Codechange: Created a function to get default cargo type for a cargo type
bjarni
parents: 6481
diff changeset
   471
		case VEH_AIRCRAFT:
8b4c20ddacd3 (svn r9688) -Codechange: Created a function to get default cargo type for a cargo type
bjarni
parents: 6481
diff changeset
   472
			/* all aircraft starts as passenger planes with cargo capacity */
8b4c20ddacd3 (svn r9688) -Codechange: Created a function to get default cargo type for a cargo type
bjarni
parents: 6481
diff changeset
   473
			return CT_PASSENGERS;
8b4c20ddacd3 (svn r9688) -Codechange: Created a function to get default cargo type for a cargo type
bjarni
parents: 6481
diff changeset
   474
8b4c20ddacd3 (svn r9688) -Codechange: Created a function to get default cargo type for a cargo type
bjarni
parents: 6481
diff changeset
   475
		default: NOT_REACHED(); return CT_INVALID;
8b4c20ddacd3 (svn r9688) -Codechange: Created a function to get default cargo type for a cargo type
bjarni
parents: 6481
diff changeset
   476
	}
8b4c20ddacd3 (svn r9688) -Codechange: Created a function to get default cargo type for a cargo type
bjarni
parents: 6481
diff changeset
   477
}
8b4c20ddacd3 (svn r9688) -Codechange: Created a function to get default cargo type for a cargo type
bjarni
parents: 6481
diff changeset
   478
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   479
/************************************************************************
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   480
 * Engine Replacement stuff
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   481
 ************************************************************************/
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   482
7388
0a3a9cc1a98b (svn r10757) -Codechange: make the engine renew struct use the pool item class as super class.
rubidium
parents: 7258
diff changeset
   483
DEFINE_OLD_POOL_GENERIC(EngineRenew, EngineRenew)
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   484
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   485
/**
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   486
 * Retrieves the EngineRenew that specifies the replacement of the given
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   487
 * engine type from the given renewlist */
6643
f81bee57bc09 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 6606
diff changeset
   488
static EngineRenew *GetEngineReplacement(EngineRenewList erl, EngineID engine, GroupID group)
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   489
{
4348
d9ddb0558a48 (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   490
	EngineRenew *er = (EngineRenew *)erl;
d9ddb0558a48 (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   491
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   492
	while (er) {
6643
f81bee57bc09 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 6606
diff changeset
   493
		if (er->from == engine && er->group_id == group) return er;
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   494
		er = er->next;
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   495
	}
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   496
	return NULL;
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   497
}
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   498
4171
5c6e60c392c3 (svn r5609) CodeChange : Apply coding style
belugas
parents: 4077
diff changeset
   499
void RemoveAllEngineReplacement(EngineRenewList *erl)
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   500
{
4348
d9ddb0558a48 (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   501
	EngineRenew *er = (EngineRenew *)(*erl);
4384
7e0d3ed719d9 (svn r6137) -Codechange: some very minor cleanups:
truelight
parents: 4358
diff changeset
   502
	EngineRenew *next;
4348
d9ddb0558a48 (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   503
7388
0a3a9cc1a98b (svn r10757) -Codechange: make the engine renew struct use the pool item class as super class.
rubidium
parents: 7258
diff changeset
   504
	while (er != NULL) {
4384
7e0d3ed719d9 (svn r6137) -Codechange: some very minor cleanups:
truelight
parents: 4358
diff changeset
   505
		next = er->next;
7388
0a3a9cc1a98b (svn r10757) -Codechange: make the engine renew struct use the pool item class as super class.
rubidium
parents: 7258
diff changeset
   506
		delete er;
4384
7e0d3ed719d9 (svn r6137) -Codechange: some very minor cleanups:
truelight
parents: 4358
diff changeset
   507
		er = next;
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   508
	}
4348
d9ddb0558a48 (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   509
	*erl = NULL; // Empty list
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   510
}
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   511
6643
f81bee57bc09 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 6606
diff changeset
   512
EngineID EngineReplacement(EngineRenewList erl, EngineID engine, GroupID group)
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   513
{
6643
f81bee57bc09 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 6606
diff changeset
   514
	const EngineRenew *er = GetEngineReplacement(erl, engine, group);
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   515
	return er == NULL ? INVALID_ENGINE : er->to;
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   516
}
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   517
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6686
diff changeset
   518
CommandCost AddEngineReplacement(EngineRenewList *erl, EngineID old_engine, EngineID new_engine, GroupID group, uint32 flags)
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   519
{
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   520
	EngineRenew *er;
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   521
4348
d9ddb0558a48 (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   522
	/* Check if the old vehicle is already in the list */
6643
f81bee57bc09 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 6606
diff changeset
   523
	er = GetEngineReplacement(*erl, old_engine, group);
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   524
	if (er != NULL) {
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   525
		if (flags & DC_EXEC) er->to = new_engine;
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
   526
		return CommandCost();
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   527
	}
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   528
7388
0a3a9cc1a98b (svn r10757) -Codechange: make the engine renew struct use the pool item class as super class.
rubidium
parents: 7258
diff changeset
   529
	er = new EngineRenew(old_engine, new_engine);
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   530
	if (er == NULL) return CMD_ERROR;
7388
0a3a9cc1a98b (svn r10757) -Codechange: make the engine renew struct use the pool item class as super class.
rubidium
parents: 7258
diff changeset
   531
	AutoPtrT<EngineRenew> er_auto_delete = er;
0a3a9cc1a98b (svn r10757) -Codechange: make the engine renew struct use the pool item class as super class.
rubidium
parents: 7258
diff changeset
   532
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   533
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   534
	if (flags & DC_EXEC) {
6643
f81bee57bc09 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 6606
diff changeset
   535
		er->group_id = group;
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   536
4348
d9ddb0558a48 (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   537
		/* Insert before the first element */
d9ddb0558a48 (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   538
		er->next = (EngineRenew *)(*erl);
d9ddb0558a48 (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   539
		*erl = (EngineRenewList)er;
7388
0a3a9cc1a98b (svn r10757) -Codechange: make the engine renew struct use the pool item class as super class.
rubidium
parents: 7258
diff changeset
   540
0a3a9cc1a98b (svn r10757) -Codechange: make the engine renew struct use the pool item class as super class.
rubidium
parents: 7258
diff changeset
   541
		er_auto_delete.Detach();
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   542
	}
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   543
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
   544
	return CommandCost();
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   545
}
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   546
6943
1914f26aee04 (svn r10197) -Codechange: replace int32 with CommandCost where appropriate.
rubidium
parents: 6686
diff changeset
   547
CommandCost RemoveEngineReplacement(EngineRenewList *erl, EngineID engine, GroupID group, uint32 flags)
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   548
{
4348
d9ddb0558a48 (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   549
	EngineRenew *er = (EngineRenew *)(*erl);
4171
5c6e60c392c3 (svn r5609) CodeChange : Apply coding style
belugas
parents: 4077
diff changeset
   550
	EngineRenew *prev = NULL;
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   551
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   552
	while (er)
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   553
	{
6643
f81bee57bc09 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 6606
diff changeset
   554
		if (er->from == engine && er->group_id == group) {
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   555
			if (flags & DC_EXEC) {
4348
d9ddb0558a48 (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   556
				if (prev == NULL) { // First element
d9ddb0558a48 (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   557
					/* The second becomes the new first element */
d9ddb0558a48 (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   558
					*erl = (EngineRenewList)er->next;
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   559
				} else {
4348
d9ddb0558a48 (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   560
					/* Cut this element out */
d9ddb0558a48 (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   561
					prev->next = er->next;
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   562
				}
7388
0a3a9cc1a98b (svn r10757) -Codechange: make the engine renew struct use the pool item class as super class.
rubidium
parents: 7258
diff changeset
   563
				delete er;
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   564
			}
6950
14ecb0acdfb4 (svn r10205) -Codechange: refactor returning of cost, so it can be more easily modified.
rubidium
parents: 6943
diff changeset
   565
			return CommandCost();
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   566
		}
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   567
		prev = er;
4348
d9ddb0558a48 (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   568
		er = er->next;
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   569
	}
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   570
4348
d9ddb0558a48 (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   571
	return CMD_ERROR;
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   572
}
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   573
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   574
static const SaveLoad _engine_renew_desc[] = {
6643
f81bee57bc09 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 6606
diff changeset
   575
	    SLE_VAR(EngineRenew, from,     SLE_UINT16),
f81bee57bc09 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 6606
diff changeset
   576
	    SLE_VAR(EngineRenew, to,       SLE_UINT16),
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   577
6643
f81bee57bc09 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 6606
diff changeset
   578
	    SLE_REF(EngineRenew, next,     REF_ENGINE_RENEWS),
f81bee57bc09 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 6606
diff changeset
   579
	SLE_CONDVAR(EngineRenew, group_id, SLE_UINT16, 60, SL_MAX_VERSION),
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   580
	SLE_END()
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   581
};
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   582
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6209
diff changeset
   583
static void Save_ERNW()
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   584
{
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   585
	EngineRenew *er;
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   586
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   587
	FOR_ALL_ENGINE_RENEWS(er) {
4348
d9ddb0558a48 (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   588
		SlSetArrayIndex(er->index);
d9ddb0558a48 (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   589
		SlObject(er, _engine_renew_desc);
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   590
	}
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   591
}
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   592
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6209
diff changeset
   593
static void Load_ERNW()
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   594
{
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   595
	int index;
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   596
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   597
	while ((index = SlIterateArray()) != -1) {
7388
0a3a9cc1a98b (svn r10757) -Codechange: make the engine renew struct use the pool item class as super class.
rubidium
parents: 7258
diff changeset
   598
		EngineRenew *er = new (index) EngineRenew();
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   599
		SlObject(er, _engine_renew_desc);
6643
f81bee57bc09 (svn r9874) -Feature: advanced vehicle lists a.k.a. group interface. Now you can make groups of vehicles and perform all kinds of tasks on that given group. Original code by nycom and graphics by skidd13.
rubidium
parents: 6606
diff changeset
   600
7258
8ea218277d00 (svn r10567) -Add [FS#915]: a "group" with ungrouped vehicles. Patch by Matthias Wolf.
rubidium
parents: 7097
diff changeset
   601
		/* Advanced vehicle lists, ungrouped vehicles got added */
8ea218277d00 (svn r10567) -Add [FS#915]: a "group" with ungrouped vehicles. Patch by Matthias Wolf.
rubidium
parents: 7097
diff changeset
   602
		if (CheckSavegameVersion(60)) {
8ea218277d00 (svn r10567) -Add [FS#915]: a "group" with ungrouped vehicles. Patch by Matthias Wolf.
rubidium
parents: 7097
diff changeset
   603
			er->group_id = ALL_GROUP;
8ea218277d00 (svn r10567) -Add [FS#915]: a "group" with ungrouped vehicles. Patch by Matthias Wolf.
rubidium
parents: 7097
diff changeset
   604
		} else if (CheckSavegameVersion(71)) {
8ea218277d00 (svn r10567) -Add [FS#915]: a "group" with ungrouped vehicles. Patch by Matthias Wolf.
rubidium
parents: 7097
diff changeset
   605
			if (er->group_id == DEFAULT_GROUP) er->group_id = ALL_GROUP;
8ea218277d00 (svn r10567) -Add [FS#915]: a "group" with ungrouped vehicles. Patch by Matthias Wolf.
rubidium
parents: 7097
diff changeset
   606
		}
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   607
	}
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   608
}
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   609
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1855
diff changeset
   610
static const SaveLoad _engine_desc[] = {
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
   611
	SLE_CONDVAR(Engine, intro_date,          SLE_FILE_U16 | SLE_VAR_I32,  0,  30),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
   612
	SLE_CONDVAR(Engine, intro_date,          SLE_INT32,                  31, SL_MAX_VERSION),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
   613
	SLE_CONDVAR(Engine, age,                 SLE_FILE_U16 | SLE_VAR_I32,  0,  30),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
   614
	SLE_CONDVAR(Engine, age,                 SLE_INT32,                  31, SL_MAX_VERSION),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
   615
	    SLE_VAR(Engine, reliability,         SLE_UINT16),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
   616
	    SLE_VAR(Engine, reliability_spd_dec, SLE_UINT16),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
   617
	    SLE_VAR(Engine, reliability_start,   SLE_UINT16),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
   618
	    SLE_VAR(Engine, reliability_max,     SLE_UINT16),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
   619
	    SLE_VAR(Engine, reliability_final,   SLE_UINT16),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
   620
	    SLE_VAR(Engine, duration_phase_1,    SLE_UINT16),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
   621
	    SLE_VAR(Engine, duration_phase_2,    SLE_UINT16),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
   622
	    SLE_VAR(Engine, duration_phase_3,    SLE_UINT16),
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   623
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
   624
	    SLE_VAR(Engine, lifelength,          SLE_UINT8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
   625
	    SLE_VAR(Engine, flags,               SLE_UINT8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
   626
	    SLE_VAR(Engine, preview_player,      SLE_UINT8),
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
   627
	    SLE_VAR(Engine, preview_wait,        SLE_UINT8),
5823
7aa8c2312103 (svn r8385) -Fix
tron
parents: 5763
diff changeset
   628
	SLE_CONDNULL(1, 0, 44),
4344
7e123fec5b0b (svn r6045) -Cleanup: align all table-like structures using spaces, i.e. whitespace fixes only except for a few comments to make them uniform for the whole enum/struct.
rubidium
parents: 4326
diff changeset
   629
	    SLE_VAR(Engine, player_avail,        SLE_UINT8),
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   630
6125
a6fff965707c (svn r8862) -Cleanup: doxygen changes, again. Mostly @files missing tags and a few comments style.
belugas
parents: 5971
diff changeset
   631
	/* reserve extra space in savegame here. (currently 16 bytes) */
3222
6de22e06a1e9 (svn r3895) - Add proper SLE(G)_CONDNULL macros for the empty space reservation in savegames and update where used
Darkvater
parents: 3047
diff changeset
   632
	SLE_CONDNULL(16, 2, SL_MAX_VERSION),
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   633
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   634
	SLE_END()
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   635
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   636
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6209
diff changeset
   637
static void Save_ENGN()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   638
{
2242
512eae2cf006 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   639
	uint i;
512eae2cf006 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   640
512eae2cf006 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   641
	for (i = 0; i != lengthof(_engines); i++) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   642
		SlSetArrayIndex(i);
2242
512eae2cf006 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   643
		SlObject(&_engines[i], _engine_desc);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   644
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   645
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   646
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6209
diff changeset
   647
static void Load_ENGN()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   648
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   649
	int index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   650
	while ((index = SlIterateArray()) != -1) {
1926
530480d14685 (svn r2432) Use GetEngine() instead of DEREF_ENGINE() or even _engines[]
tron
parents: 1908
diff changeset
   651
		SlObject(GetEngine(index), _engine_desc);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   652
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   653
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   654
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6209
diff changeset
   655
static void LoadSave_ENGS()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   656
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   657
	SlArray(_engine_name_strings, lengthof(_engine_name_strings), SLE_STRINGID);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   658
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   659
5587
167d9a91ef02 (svn r8038) -Merge: the cpp branch. Effort of KUDr, Celestar, glx, Smoovius, stillunknown and pv2b.
rubidium
parents: 5584
diff changeset
   660
extern const ChunkHandler _engine_chunk_handlers[] = {
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   661
	{ 'ENGN', Save_ENGN,     Load_ENGN,     CH_ARRAY          },
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   662
	{ 'ENGS', LoadSave_ENGS, LoadSave_ENGS, CH_RIFF           },
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   663
	{ 'ERNW', Save_ERNW,     Load_ERNW,     CH_ARRAY | CH_LAST},
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   664
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   665
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6209
diff changeset
   666
void InitializeEngines()
1197
433b4a05fa3e (svn r1701) Style police ^^
tron
parents: 1196
diff changeset
   667
{
2848
8f57563f8ddf (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   668
	/* Clean the engine renew pool and create 1 block in it */
7388
0a3a9cc1a98b (svn r10757) -Codechange: make the engine renew struct use the pool item class as super class.
rubidium
parents: 7258
diff changeset
   669
	_EngineRenew_pool.CleanPool();
0a3a9cc1a98b (svn r10757) -Codechange: make the engine renew struct use the pool item class as super class.
rubidium
parents: 7258
diff changeset
   670
	_EngineRenew_pool.AddBlockToPool();
1196
67f7f3017d99 (svn r1700) - Fix: Hacked clients can no longer be used to build vehicles that are not available yet (Hackykid)
bjarni
parents: 1096
diff changeset
   671
}