engine.c
author rubidium
Mon, 28 Aug 2006 10:14:37 +0000
changeset 4432 33631ac88c40
parent 4384 293c0d26294c
child 4549 60410aa1aa88
permissions -rw-r--r--
(svn r6195) -Codechange: do not use '//' in '/* ... */' style comments and '/*' or '*/' in '//' style comments.
2186
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     1
/* $Id$ */
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     2
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     3
#include "stdafx.h"
1891
92a3b0aa0946 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1883
diff changeset
     4
#include "openttd.h"
1299
0a6510cc889b (svn r1803) Move debugging stuff into files of it's own
tron
parents: 1197
diff changeset
     5
#include "debug.h"
2163
637ec3c361f5 (svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents: 2159
diff changeset
     6
#include "functions.h"
507
8aa8100b0b22 (svn r815) Include strings.h only in the files which need it.
tron
parents: 485
diff changeset
     7
#include "table/strings.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     8
#include "engine.h"
1009
a7ccb8e39cbc (svn r1508) Remove duplicate declarations and include proper headers where necessary
tron
parents: 964
diff changeset
     9
#include "gfx.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    10
#include "player.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    11
#include "command.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    12
#include "vehicle.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    13
#include "news.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    14
#include "saveload.h"
2159
3b634157c3b2 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2147
diff changeset
    15
#include "variables.h"
2708
9d1a8d5dc3ea (svn r3252) - NewGRF: Map new train subtypes to old types for rail vehicles.
peter1138
parents: 2639
diff changeset
    16
#include "train.h"
3866
113de2b20059 (svn r4902) Newgrf : Introduction of the newgrf_cargo files.
belugas
parents: 3865
diff changeset
    17
#include "newgrf_cargo.h"
4261
2ec8f5a9747b (svn r5887) -Cleanup: move date related functions, defines and variables to date.[ch]
rubidium
parents: 4171
diff changeset
    18
#include "date.h"
4322
0bee6d9bc452 (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
    19
#include "table/engines.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    20
2763
d579caf58b59 (svn r3308) BAD
tron
parents: 2708
diff changeset
    21
EngineInfo _engine_info[TOTAL_NUM_ENGINES];
d579caf58b59 (svn r3308) BAD
tron
parents: 2708
diff changeset
    22
RailVehicleInfo _rail_vehicle_info[NUM_TRAIN_ENGINES];
d579caf58b59 (svn r3308) BAD
tron
parents: 2708
diff changeset
    23
ShipVehicleInfo _ship_vehicle_info[NUM_SHIP_ENGINES];
d579caf58b59 (svn r3308) BAD
tron
parents: 2708
diff changeset
    24
AircraftVehicleInfo _aircraft_vehicle_info[NUM_AIRCRAFT_ENGINES];
d579caf58b59 (svn r3308) BAD
tron
parents: 2708
diff changeset
    25
RoadVehicleInfo _road_vehicle_info[NUM_ROAD_ENGINES];
d579caf58b59 (svn r3308) BAD
tron
parents: 2708
diff changeset
    26
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    27
enum {
4344
5d0e40cd67b9 (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
    28
	ENGINE_AVAILABLE   = 1,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    29
	ENGINE_INTRODUCING = 2,
4344
5d0e40cd67b9 (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
    30
	ENGINE_PREVIEWING  = 4,
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    31
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    32
4300
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4297
diff changeset
    33
enum {
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4297
diff changeset
    34
	YEAR_ENGINE_AGING_STOPS = 2050,
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4297
diff changeset
    35
};
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4297
diff changeset
    36
1802
448f187042d3 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1786
diff changeset
    37
/** Bitmasked values of what type of cargo is refittable for the given vehicle-type.
448f187042d3 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1786
diff changeset
    38
 * This coupled with the landscape information (_landscape_global_cargo_mask) gives
448f187042d3 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1786
diff changeset
    39
 * us exactly what is refittable and what is not */
3866
113de2b20059 (svn r4902) Newgrf : Introduction of the newgrf_cargo files.
belugas
parents: 3865
diff changeset
    40
#define MC(cargo) (1 << cargo)
1802
448f187042d3 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1786
diff changeset
    41
const uint32 _default_refitmasks[NUM_VEHICLE_TYPES] = {
448f187042d3 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1786
diff changeset
    42
	/* Trains */
4344
5d0e40cd67b9 (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
    43
	MC(GC_PASSENGERS) | MC(GC_COAL)      | MC(GC_MAIL)   | MC(GC_LIVESTOCK) | MC(GC_GOODS)        | MC(GC_GRAIN)      | MC(GC_WOOD)    | MC(GC_IRON_ORE)    |
5d0e40cd67b9 (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
    44
	MC(GC_STEEL)      | MC(GC_VALUABLES) | MC(GC_PAPER)  | MC(GC_FOOD)      | MC(GC_FRUIT)        | MC(GC_COPPER_ORE) | MC(GC_WATER)   | MC(GC_SUGAR)       |
5d0e40cd67b9 (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
    45
	MC(GC_TOYS)       | MC(GC_CANDY)     | MC(GC_TOFFEE) | MC(GC_COLA)      | MC(GC_COTTON_CANDY) | MC(GC_BUBBLES)    | MC(GC_PLASTIC) | MC(GC_FIZZY_DRINKS),
1802
448f187042d3 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1786
diff changeset
    46
	/* Road vehicles (not refittable by default) */
448f187042d3 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1786
diff changeset
    47
	0,
448f187042d3 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1786
diff changeset
    48
	/* Ships */
4344
5d0e40cd67b9 (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
    49
	MC(GC_COAL)  | MC(GC_MAIL)   | MC(GC_LIVESTOCK) | MC(GC_GOODS)        | MC(GC_GRAIN)   | MC(GC_WOOD)    | MC(GC_IRON_ORE) | MC(GC_STEEL) | MC(GC_VALUABLES) |
5d0e40cd67b9 (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
    50
	MC(GC_PAPER) | MC(GC_FOOD)   | MC(GC_FRUIT)     | MC(GC_COPPER_ORE)   | MC(GC_WATER)   | MC(GC_RUBBER)  | MC(GC_SUGAR)    | MC(GC_TOYS)  | MC(GC_BATTERIES) |
5d0e40cd67b9 (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
    51
	MC(GC_CANDY) | MC(GC_TOFFEE) | MC(GC_COLA)      | MC(GC_COTTON_CANDY) | MC(GC_BUBBLES) | MC(GC_PLASTIC) | MC(GC_FIZZY_DRINKS),
1802
448f187042d3 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1786
diff changeset
    52
	/* Aircraft */
4344
5d0e40cd67b9 (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
    53
	MC(GC_PASSENGERS) | MC(GC_MAIL)  | MC(GC_GOODS)  | MC(GC_VALUABLES) | MC(GC_FOOD)         | MC(GC_FRUIT)   | MC(GC_SUGAR)   | MC(GC_TOYS) |
5d0e40cd67b9 (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
    54
	MC(GC_BATTERIES)  | MC(GC_CANDY) | MC(GC_TOFFEE) | MC(GC_COLA)      | MC(GC_COTTON_CANDY) | MC(GC_BUBBLES) | MC(GC_PLASTIC) | MC(GC_FIZZY_DRINKS),
1802
448f187042d3 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1786
diff changeset
    55
	/* Special/Disaster */
448f187042d3 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1786
diff changeset
    56
	0,0
448f187042d3 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1786
diff changeset
    57
};
448f187042d3 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1786
diff changeset
    58
#undef MC
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    59
2477
d454d5152386 (svn r3003) Change int, uint, uin16 and byte to EngineID where appropriate, plus some related changes (mostly casts)
tron
parents: 2469
diff changeset
    60
void ShowEnginePreviewWindow(EngineID engine);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    61
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1009
diff changeset
    62
void DeleteCustomEngineNames(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    63
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    64
	uint i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    65
	StringID old;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    66
1474
d83520edcda6 (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
    67
	for (i = 0; i != TOTAL_NUM_ENGINES; i++) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    68
		old = _engine_name_strings[i];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    69
		_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
    70
		DeleteName(old);
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
	_vehicle_design_names &= ~1;
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
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1009
diff changeset
    76
void LoadCustomEngineNames(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    77
{
4432
33631ac88c40 (svn r6195) -Codechange: do not use '//' in '/* ... */' style comments and '/*' or '*/' in '//' style comments.
rubidium
parents: 4384
diff changeset
    78
	/* XXX: not done */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    79
	DEBUG(misc, 1) ("LoadCustomEngineNames: not done");
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    80
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    81
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1009
diff changeset
    82
static void SetupEngineNames(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    83
{
1474
d83520edcda6 (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
    84
	StringID *name;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    85
1474
d83520edcda6 (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
    86
	for (name = _engine_name_strings; name != endof(_engine_name_strings); name++)
d83520edcda6 (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
    87
		*name = STR_SV_EMPTY;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    88
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    89
	DeleteCustomEngineNames();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    90
	LoadCustomEngineNames();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    91
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    92
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1009
diff changeset
    93
static void AdjustAvailAircraft(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    94
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    95
	byte avail = 0;
4297
47ce9665b4af (svn r5934) -Cleanup: forgot some conversions to Year and to Date
rubidium
parents: 4293
diff changeset
    96
	if (_cur_year >= 1955) avail |= 2; // big airport
47ce9665b4af (svn r5934) -Cleanup: forgot some conversions to Year and to Date
rubidium
parents: 4293
diff changeset
    97
	if (_cur_year <  1960 || _patches.always_small_airport) avail |= 1;  // small airport
47ce9665b4af (svn r5934) -Cleanup: forgot some conversions to Year and to Date
rubidium
parents: 4293
diff changeset
    98
	if (_cur_year >= 1963) avail |= 4; // enable heliport
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    99
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   100
	if (avail != _avail_aircraft) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   101
		_avail_aircraft = avail;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   102
		InvalidateWindow(WC_BUILD_STATION, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   103
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   104
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   105
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   106
static void CalcEngineReliability(Engine *e)
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
	uint age = e->age;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   109
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   110
	if (age < e->duration_phase_1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   111
		uint start = e->reliability_start;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   112
		e->reliability = age * (e->reliability_max - start) / e->duration_phase_1 + start;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   113
	} else if ((age -= e->duration_phase_1) < e->duration_phase_2) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   114
		e->reliability = e->reliability_max;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   115
	} 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
   116
		uint max = e->reliability_max;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   117
		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
   118
	} else {
292
9b0a6d544593 (svn r298) Fix: Engines from other climates do not appear any more when never_expire_vehicles is enabled
dominik
parents: 257
diff changeset
   119
		// time's up for this engine
9b0a6d544593 (svn r298) Fix: Engines from other climates do not appear any more when never_expire_vehicles is enabled
dominik
parents: 257
diff changeset
   120
		// make it either available to all players (if never_expire_vehicles is enabled and if it was available earlier)
9b0a6d544593 (svn r298) Fix: Engines from other climates do not appear any more when never_expire_vehicles is enabled
dominik
parents: 257
diff changeset
   121
		// or disable this engine completely
9b0a6d544593 (svn r298) Fix: Engines from other climates do not appear any more when never_expire_vehicles is enabled
dominik
parents: 257
diff changeset
   122
		e->player_avail = (_patches.never_expire_vehicles && e->player_avail)? -1 : 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   123
		e->reliability = e->reliability_final;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   124
	}
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
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1009
diff changeset
   127
void AddTypeToEngines(void)
842
ebfd36603ab9 (svn r1323) Adding autoreplace feature
bjarni
parents: 819
diff changeset
   128
{
2242
27fa4807cd61 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   129
	Engine* e = _engines;
842
ebfd36603ab9 (svn r1323) Adding autoreplace feature
bjarni
parents: 819
diff changeset
   130
2242
27fa4807cd61 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   131
	do e->type = VEH_Train;    while (++e < &_engines[ROAD_ENGINES_INDEX]);
27fa4807cd61 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   132
	do e->type = VEH_Road;     while (++e < &_engines[SHIP_ENGINES_INDEX]);
27fa4807cd61 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   133
	do e->type = VEH_Ship;     while (++e < &_engines[AIRCRAFT_ENGINES_INDEX]);
27fa4807cd61 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   134
	do e->type = VEH_Aircraft; while (++e < &_engines[TOTAL_NUM_ENGINES]);
27fa4807cd61 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   135
	do e->type = VEH_Special;  while (++e < endof(_engines));
842
ebfd36603ab9 (svn r1323) Adding autoreplace feature
bjarni
parents: 819
diff changeset
   136
}
ebfd36603ab9 (svn r1323) Adding autoreplace feature
bjarni
parents: 819
diff changeset
   137
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1009
diff changeset
   138
void StartupEngines(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   139
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   140
	Engine *e;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   141
	const EngineInfo *ei;
4300
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4297
diff changeset
   142
	/* Aging of vehicles stops, so account for that when starting late */
4322
0bee6d9bc452 (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
   143
	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
   144
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   145
	SetupEngineNames();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   146
2242
27fa4807cd61 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   147
	for (e = _engines, ei = _engine_info; e != endof(_engines); e++, ei++) {
27fa4807cd61 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   148
		uint32 r;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   149
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   150
		e->age = 0;
2530
b8d5a5284024 (svn r3059) Use bitfields to encode railtype and climates of engines instead of manual shifting/anding
tron
parents: 2491
diff changeset
   151
		e->railtype = ei->railtype;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   152
		e->flags = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   153
		e->player_avail = 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 114
diff changeset
   154
2875
905a06cc99c3 (svn r3423) - NewGRF: Update the base_intro age at which an engine will have a randomized intro date, as per (the now updated) spec.
peter1138
parents: 2862
diff changeset
   155
		// The magic value of 729 days below comes from the NewGRF spec. If the
905a06cc99c3 (svn r3423) - NewGRF: Update the base_intro age at which an engine will have a randomized intro date, as per (the now updated) spec.
peter1138
parents: 2862
diff changeset
   156
		// base intro date is before 1922 then the random number of days is not
905a06cc99c3 (svn r3423) - NewGRF: Update the base_intro age at which an engine will have a randomized intro date, as per (the now updated) spec.
peter1138
parents: 2862
diff changeset
   157
		// added.
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   158
		r = Random();
4297
47ce9665b4af (svn r5934) -Cleanup: forgot some conversions to Year and to Date
rubidium
parents: 4293
diff changeset
   159
		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
   160
		if (e->intro_date <= _date) {
4300
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4297
diff changeset
   161
			e->age = (aging_date - e->intro_date) >> 5;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   162
			e->player_avail = (byte)-1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   163
			e->flags |= ENGINE_AVAILABLE;
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
2140
d708eb80ab8b (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
   166
		e->reliability_start = GB(r, 16, 14) + 0x7AE0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   167
		r = Random();
2140
d708eb80ab8b (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
   168
		e->reliability_max   = GB(r,  0, 14) + 0xBFFF;
d708eb80ab8b (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
   169
		e->reliability_final = GB(r, 16, 14) + 0x3FFF;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   170
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   171
		r = Random();
2140
d708eb80ab8b (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
   172
		e->duration_phase_1 = GB(r, 0, 5) + 7;
d708eb80ab8b (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
   173
		e->duration_phase_2 = GB(r, 5, 4) + ei->base_life * 12 - 96;
d708eb80ab8b (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
   174
		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
   175
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   176
		e->reliability_spd_dec = (ei->unk2&0x7F) << 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   177
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   178
		/* my invented flag for something that is a wagon */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   179
		if (ei->unk2 & 0x80) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   180
			e->age = 0xFFFF;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   181
		} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   182
			CalcEngineReliability(e);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   183
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   184
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   185
		e->lifelength = ei->lifelength + _patches.extend_vehicle_life;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   186
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   187
		// prevent certain engines from ever appearing.
2530
b8d5a5284024 (svn r3059) Use bitfields to encode railtype and climates of engines instead of manual shifting/anding
tron
parents: 2491
diff changeset
   188
		if (!HASBIT(ei->climates, _opt.landscape)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   189
			e->flags |= ENGINE_AVAILABLE;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   190
			e->player_avail = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   191
		}
915
013cb2d74800 (svn r1402) Trim trailing whitespace
tron
parents: 842
diff changeset
   192
819
02e9296ccdca (svn r1290) Added type to typedef struct Engine and filled in the same data as in type in vehicle
bjarni
parents: 740
diff changeset
   193
		/* This sets up type for the engine
02e9296ccdca (svn r1290) Added type to typedef struct Engine and filled in the same data as in type in vehicle
bjarni
parents: 740
diff changeset
   194
		   It is needed if you want to ask the engine what type it is
02e9296ccdca (svn r1290) Added type to typedef struct Engine and filled in the same data as in type in vehicle
bjarni
parents: 740
diff changeset
   195
		   It should hopefully be the same as when you ask a vehicle what it is
02e9296ccdca (svn r1290) Added type to typedef struct Engine and filled in the same data as in type in vehicle
bjarni
parents: 740
diff changeset
   196
		   but using this, you can ask what type an engine number is
02e9296ccdca (svn r1290) Added type to typedef struct Engine and filled in the same data as in type in vehicle
bjarni
parents: 740
diff changeset
   197
		   even if it is not a vehicle (yet)*/
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   198
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   199
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   200
	AdjustAvailAircraft();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   201
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   202
2817
58dcead3f545 (svn r3365) Staticise 36 functions
tron
parents: 2769
diff changeset
   203
static void AcceptEnginePreview(Engine *e, PlayerID player)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   204
{
1962
51ee4f459268 (svn r2468) -Codechange: Got rid of DEREF_PLAYER and replaced it by GetPlayer
celestar
parents: 1926
diff changeset
   205
	Player *p = GetPlayer(player);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   206
2147
42efdf5020d1 (svn r2657) -Codechange: The available railtypes per player are now a bitmask, so
celestar
parents: 2140
diff changeset
   207
	assert(e->railtype < RAILTYPE_END);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   208
	SETBIT(e->player_avail, player);
2147
42efdf5020d1 (svn r2657) -Codechange: The available railtypes per player are now a bitmask, so
celestar
parents: 2140
diff changeset
   209
	SETBIT(p->avail_railtypes, e->railtype);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   210
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   211
	e->preview_player = 0xFF;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   212
	InvalidateWindowClasses(WC_BUILD_VEHICLE);
1096
ce698f7e4621 (svn r1597) fix: autoreplace vehicle lists are now redrawn when a new vehicle becomes available (thanks LordOfThePigs for pointing this one out)
bjarni
parents: 1093
diff changeset
   213
	InvalidateWindowClasses(WC_REPLACE_VEHICLE);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   214
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   215
1786
a54634efeb98 (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
static PlayerID GetBestPlayer(PlayerID pp)
a54634efeb98 (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
{
a54634efeb98 (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
	const Player *p;
a54634efeb98 (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
	int32 best_hist;
a54634efeb98 (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
   220
	PlayerID best_player;
a54634efeb98 (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
	uint mask = 0;
a54634efeb98 (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
   222
a54634efeb98 (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
   223
	do {
a54634efeb98 (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
		best_hist = -1;
2469
1bfbb0dc0b75 (svn r2995) Replace 0xFF/0xFFFF with CT_INVALID/OWNER_SPECTATOR/INVALID_STATION where appropriate
tron
parents: 2242
diff changeset
   225
		best_player = OWNER_SPECTATOR;
1786
a54634efeb98 (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
		FOR_ALL_PLAYERS(p) {
a54634efeb98 (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
			if (p->is_active && p->block_preview == 0 && !HASBIT(mask, p->index) &&
a54634efeb98 (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
   228
					p->old_economy[0].performance_history > best_hist) {
a54634efeb98 (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
   229
				best_hist = p->old_economy[0].performance_history;
a54634efeb98 (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
   230
				best_player = p->index;
a54634efeb98 (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
   231
			}
a54634efeb98 (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
   232
		}
a54634efeb98 (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
   233
2469
1bfbb0dc0b75 (svn r2995) Replace 0xFF/0xFFFF with CT_INVALID/OWNER_SPECTATOR/INVALID_STATION where appropriate
tron
parents: 2242
diff changeset
   234
		if (best_player == OWNER_SPECTATOR) return OWNER_SPECTATOR;
1786
a54634efeb98 (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
   235
a54634efeb98 (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
   236
		SETBIT(mask, best_player);
a54634efeb98 (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
   237
	} while (--pp != 0);
a54634efeb98 (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
   238
a54634efeb98 (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
   239
	return best_player;
a54634efeb98 (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
   240
}
a54634efeb98 (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
   241
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1009
diff changeset
   242
void EnginesDailyLoop(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   243
{
2477
d454d5152386 (svn r3003) Change int, uint, uin16 and byte to EngineID where appropriate, plus some related changes (mostly casts)
tron
parents: 2469
diff changeset
   244
	EngineID i;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   245
4300
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4297
diff changeset
   246
	if (_cur_year >= YEAR_ENGINE_AGING_STOPS) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   247
2242
27fa4807cd61 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   248
	for (i = 0; i != lengthof(_engines); i++) {
4171
3fadda3afe70 (svn r5609) CodeChange : Apply coding style
belugas
parents: 4077
diff changeset
   249
		Engine *e = &_engines[i];
2242
27fa4807cd61 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   250
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   251
		if (e->flags & ENGINE_INTRODUCING) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   252
			if (e->flags & ENGINE_PREVIEWING) {
740
c1d06749846b (svn r1196) -Fix: Preview Vehicle had a small glitch. Fixed now.
truelight
parents: 555
diff changeset
   253
				if (e->preview_player != 0xFF && !--e->preview_wait) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   254
					e->flags &= ~ENGINE_PREVIEWING;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   255
					DeleteWindowById(WC_ENGINE_PREVIEW, i);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   256
					e->preview_player++;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 114
diff changeset
   257
				}
4077
d3022f976946 (svn r5391) Miscellaneous, mostly bracing and whitespace, nothing spectacular
tron
parents: 3866
diff changeset
   258
			} else if (e->preview_player != 0xFF) {
1786
a54634efeb98 (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
   259
				PlayerID best_player = GetBestPlayer(e->preview_player);
a54634efeb98 (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
   260
2469
1bfbb0dc0b75 (svn r2995) Replace 0xFF/0xFFFF with CT_INVALID/OWNER_SPECTATOR/INVALID_STATION where appropriate
tron
parents: 2242
diff changeset
   261
				if (best_player == OWNER_SPECTATOR) {
1786
a54634efeb98 (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
   262
					e->preview_player = 0xFF;
a54634efeb98 (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
   263
					continue;
a54634efeb98 (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
   264
				}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 114
diff changeset
   265
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   266
				if (!IS_HUMAN_PLAYER(best_player)) {
1786
a54634efeb98 (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
   267
					/* XXX - TTDBUG: TTD has a bug here ???? */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   268
					AcceptEnginePreview(e, best_player);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   269
				} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   270
					e->flags |= ENGINE_PREVIEWING;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   271
					e->preview_wait = 20;
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2611
diff changeset
   272
					if (IS_INTERACTIVE_PLAYER(best_player)) ShowEnginePreviewWindow(i);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   273
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   274
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   275
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   276
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   277
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   278
1786
a54634efeb98 (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
/** Accept an engine prototype. XXX - it is possible that the top-player
a54634efeb98 (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
   280
 * changes while you are waiting to accept the offer? Then it becomes invalid
3491
4c8427796c64 (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
   281
 * @param tile unused
1786
a54634efeb98 (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
   282
 * @param p1 engine-prototype offered
a54634efeb98 (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
   283
 * @param p2 unused
a54634efeb98 (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
   284
 */
3491
4c8427796c64 (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
   285
int32 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
   286
{
1786
a54634efeb98 (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
   287
	Engine *e;
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2875
diff changeset
   288
1786
a54634efeb98 (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
   289
	if (!IsEngineIndex(p1)) return CMD_ERROR;
1926
68d60188a22f (svn r2432) Use GetEngine() instead of DEREF_ENGINE() or even _engines[]
tron
parents: 1908
diff changeset
   290
	e = GetEngine(p1);
1786
a54634efeb98 (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
   291
	if (GetBestPlayer(e->preview_player) != _current_player) return CMD_ERROR;
a54634efeb98 (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
   292
2951
2db3adee7736 (svn r3510) Fiddle with whitespace and parentheses
tron
parents: 2875
diff changeset
   293
	if (flags & DC_EXEC) AcceptEnginePreview(e, _current_player);
1786
a54634efeb98 (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
   294
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   295
	return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   296
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   297
257
8d83db6716ea (svn r262) Fix: [ 1028234 ] Monorail and MagLev infrastructure not available in 1920s any more
dominik
parents: 220
diff changeset
   298
// Determine if an engine type is a wagon (and not a loco)
2477
d454d5152386 (svn r3003) Change int, uint, uin16 and byte to EngineID where appropriate, plus some related changes (mostly casts)
tron
parents: 2469
diff changeset
   299
static bool IsWagon(EngineID index)
257
8d83db6716ea (svn r262) Fix: [ 1028234 ] Monorail and MagLev infrastructure not available in 1920s any more
dominik
parents: 220
diff changeset
   300
{
964
4bd472539a4e (svn r1456) Simplify isWagon(), make it static and give it a canonical name (IsWagon)
tron
parents: 938
diff changeset
   301
	return index < NUM_TRAIN_ENGINES && RailVehInfo(index)->flags & RVI_WAGON;
257
8d83db6716ea (svn r262) Fix: [ 1028234 ] Monorail and MagLev infrastructure not available in 1920s any more
dominik
parents: 220
diff changeset
   302
}
8d83db6716ea (svn r262) Fix: [ 1028234 ] Monorail and MagLev infrastructure not available in 1920s any more
dominik
parents: 220
diff changeset
   303
410
8de2aaf20800 (svn r607) -Patch: [ 985102 ] static cleanup
tron
parents: 408
diff changeset
   304
static void NewVehicleAvailable(Engine *e)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   305
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   306
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   307
	Player *p;
2477
d454d5152386 (svn r3003) Change int, uint, uin16 and byte to EngineID where appropriate, plus some related changes (mostly casts)
tron
parents: 2469
diff changeset
   308
	EngineID index = e - _engines;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   309
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   310
	// In case the player didn't build the vehicle during the intro period,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   311
	// prevent that player from getting future intro periods for a while.
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2611
diff changeset
   312
	if (e->flags & ENGINE_INTRODUCING) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   313
		FOR_ALL_PLAYERS(p) {
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   314
			uint block_preview = p->block_preview;
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   315
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2611
diff changeset
   316
			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
   317
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   318
			/* We assume the user did NOT build it.. prove me wrong ;) */
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   319
			p->block_preview = 20;
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   320
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   321
			FOR_ALL_VEHICLES(v) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   322
				if (v->type == VEH_Train || v->type == VEH_Road || v->type == VEH_Ship ||
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   323
						(v->type == VEH_Aircraft && v->subtype <= 2)) {
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   324
					if (v->owner == p->index && v->engine_type == index) {
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   325
						/* The user did prove me wrong, so restore old value */
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   326
						p->block_preview = block_preview;
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   327
						break;
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   328
					}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   329
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   330
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   331
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   332
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   333
114
6a2af0c2d0db (svn r115) Fix: monorail/maglev became available around 1927
dominik
parents: 0
diff changeset
   334
	e->flags = (e->flags & ~ENGINE_INTRODUCING) | ENGINE_AVAILABLE;
6a2af0c2d0db (svn r115) Fix: monorail/maglev became available around 1927
dominik
parents: 0
diff changeset
   335
	InvalidateWindowClasses(WC_BUILD_VEHICLE);
1096
ce698f7e4621 (svn r1597) fix: autoreplace vehicle lists are now redrawn when a new vehicle becomes available (thanks LordOfThePigs for pointing this one out)
bjarni
parents: 1093
diff changeset
   336
	InvalidateWindowClasses(WC_REPLACE_VEHICLE);
114
6a2af0c2d0db (svn r115) Fix: monorail/maglev became available around 1927
dominik
parents: 0
diff changeset
   337
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   338
	// Now available for all players
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   339
	e->player_avail = (byte)-1;
114
6a2af0c2d0db (svn r115) Fix: monorail/maglev became available around 1927
dominik
parents: 0
diff changeset
   340
6a2af0c2d0db (svn r115) Fix: monorail/maglev became available around 1927
dominik
parents: 0
diff changeset
   341
	// Do not introduce new rail wagons
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2611
diff changeset
   342
	if (IsWagon(index)) return;
114
6a2af0c2d0db (svn r115) Fix: monorail/maglev became available around 1927
dominik
parents: 0
diff changeset
   343
6a2af0c2d0db (svn r115) Fix: monorail/maglev became available around 1927
dominik
parents: 0
diff changeset
   344
	// make maglev / monorail available
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   345
	FOR_ALL_PLAYERS(p) {
2147
42efdf5020d1 (svn r2657) -Codechange: The available railtypes per player are now a bitmask, so
celestar
parents: 2140
diff changeset
   346
		if (p->is_active) {
42efdf5020d1 (svn r2657) -Codechange: The available railtypes per player are now a bitmask, so
celestar
parents: 2140
diff changeset
   347
			assert(e->railtype < RAILTYPE_END);
42efdf5020d1 (svn r2657) -Codechange: The available railtypes per player are now a bitmask, so
celestar
parents: 2140
diff changeset
   348
			SETBIT(p->avail_railtypes, e->railtype);
42efdf5020d1 (svn r2657) -Codechange: The available railtypes per player are now a bitmask, so
celestar
parents: 2140
diff changeset
   349
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   350
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   351
2477
d454d5152386 (svn r3003) Change int, uint, uin16 and byte to EngineID where appropriate, plus some related changes (mostly casts)
tron
parents: 2469
diff changeset
   352
	if (index < NUM_TRAIN_ENGINES) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   353
		AddNewsItem(index, NEWS_FLAGS(NM_CALLBACK, 0, NT_NEW_VEHICLES, DNC_TRAINAVAIL), 0, 0);
2477
d454d5152386 (svn r3003) Change int, uint, uin16 and byte to EngineID where appropriate, plus some related changes (mostly casts)
tron
parents: 2469
diff changeset
   354
	} else if (index < NUM_TRAIN_ENGINES + NUM_ROAD_ENGINES) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   355
		AddNewsItem(index, NEWS_FLAGS(NM_CALLBACK, 0, NT_NEW_VEHICLES, DNC_ROADAVAIL), 0, 0);
2477
d454d5152386 (svn r3003) Change int, uint, uin16 and byte to EngineID where appropriate, plus some related changes (mostly casts)
tron
parents: 2469
diff changeset
   356
	} else if (index < NUM_TRAIN_ENGINES + NUM_ROAD_ENGINES + NUM_SHIP_ENGINES) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   357
		AddNewsItem(index, NEWS_FLAGS(NM_CALLBACK, 0, NT_NEW_VEHICLES, DNC_SHIPAVAIL), 0, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   358
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   359
		AddNewsItem(index, NEWS_FLAGS(NM_CALLBACK, 0, NT_NEW_VEHICLES, DNC_AIRCRAFTAVAIL), 0, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   360
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   361
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   362
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1009
diff changeset
   363
void EnginesMonthlyLoop(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   364
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   365
	Engine *e;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   366
4300
687a17c9c557 (svn r5946) -Add: merged the TGP branch to mainline. TGP adds:
truelight
parents: 4297
diff changeset
   367
	if (_cur_year < YEAR_ENGINE_AGING_STOPS) {
2242
27fa4807cd61 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   368
		for (e = _engines; e != endof(_engines); e++) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   369
			// Age the vehicle
2242
27fa4807cd61 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   370
			if (e->flags & ENGINE_AVAILABLE && e->age != 0xFFFF) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   371
				e->age++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   372
				CalcEngineReliability(e);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   373
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   374
2990
352b4590ee1c (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
   375
			if (!(e->flags & ENGINE_AVAILABLE) && _date >= (e->intro_date + 365)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   376
				// Introduce it to all players
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   377
				NewVehicleAvailable(e);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   378
			} else if (!(e->flags & (ENGINE_AVAILABLE|ENGINE_INTRODUCING)) && _date >= e->intro_date) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   379
				// Introduction date has passed.. show introducing dialog to one player.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   380
				e->flags |= ENGINE_INTRODUCING;
257
8d83db6716ea (svn r262) Fix: [ 1028234 ] Monorail and MagLev infrastructure not available in 1920s any more
dominik
parents: 220
diff changeset
   381
8d83db6716ea (svn r262) Fix: [ 1028234 ] Monorail and MagLev infrastructure not available in 1920s any more
dominik
parents: 220
diff changeset
   382
				// Do not introduce new rail wagons
964
4bd472539a4e (svn r1456) Simplify isWagon(), make it static and give it a canonical name (IsWagon)
tron
parents: 938
diff changeset
   383
				if (!IsWagon(e - _engines))
257
8d83db6716ea (svn r262) Fix: [ 1028234 ] Monorail and MagLev infrastructure not available in 1920s any more
dominik
parents: 220
diff changeset
   384
					e->preview_player = 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
   385
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   386
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   387
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   388
	AdjustAvailAircraft();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   389
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   390
1786
a54634efeb98 (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
/** Rename an engine.
3491
4c8427796c64 (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
   392
 * @param tile unused
1786
a54634efeb98 (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
   393
 * @param p1 engine ID to rename
a54634efeb98 (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
   394
 * @param p2 unused
a54634efeb98 (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
   395
 */
3491
4c8427796c64 (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
   396
int32 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
   397
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   398
	StringID str;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   399
1840
c181d5cb4ffe (svn r2345) - Fix: Don't allow stuff to be renamed to nothing if we don't support it. Only valid ones are signs (delete) and waypoints (rename to default).
Darkvater
parents: 1820
diff changeset
   400
	if (!IsEngineIndex(p1) || _cmd_text[0] == '\0') return CMD_ERROR;
1786
a54634efeb98 (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
   401
1820
9b6458526480 (svn r2324) Introduce _cmd_text for passing strings with a command instead of abusing _decode_parameters as text buffer. This should prevent several possible buffer overruns and is a bit cleaner to use. As bonus it reduces the size of most command packets by 79 bytes.
tron
parents: 1802
diff changeset
   402
	str = AllocateNameUnique(_cmd_text, 0);
1786
a54634efeb98 (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
   403
	if (str == 0) return CMD_ERROR;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 114
diff changeset
   404
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   405
	if (flags & DC_EXEC) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   406
		StringID old_str = _engine_name_strings[p1];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   407
		_engine_name_strings[p1] = str;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   408
		DeleteName(old_str);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   409
		_vehicle_design_names |= 3;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   410
		MarkWholeScreenDirty();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   411
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   412
		DeleteName(str);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   413
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   414
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   415
	return 0;
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   418
2848
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   419
/*
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   420
 * returns true if an engine is valid, of the specified type, and buildable by
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   421
 * the current player, false otherwise
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   422
 *
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   423
 * engine = index of the engine to check
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   424
 * type   = the type the engine should be of (VEH_xxx)
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   425
 */
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   426
bool IsEngineBuildable(uint engine, byte type)
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   427
{
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   428
	const Engine *e;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   429
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   430
	// check if it's an engine that is in the engine array
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   431
	if (!IsEngineIndex(engine)) return false;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   432
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   433
	e = GetEngine(engine);
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   434
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   435
	// check if it's an engine of specified type
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   436
	if (e->type != type) return false;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   437
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   438
	// check if it's available
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   439
	if (!HASBIT(e->player_avail, _current_player)) return false;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   440
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   441
	return true;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   442
}
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   443
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   444
/************************************************************************
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   445
 * Engine Replacement stuff
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   446
 ************************************************************************/
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   447
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   448
static void EngineRenewPoolNewBlock(uint start_item); /* Forward declare for initializer of _engine_renew_pool */
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   449
enum {
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   450
	ENGINE_RENEW_POOL_BLOCK_SIZE_BITS = 3,
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   451
	ENGINE_RENEW_POOL_MAX_BLOCKS      = 8000,
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   452
};
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   453
3585
18a23b37e99a (svn r4471) - Pools: Add a facility for calling a custom function during pool block clean up.
peter1138
parents: 3491
diff changeset
   454
MemoryPool _engine_renew_pool = { "EngineRe", ENGINE_RENEW_POOL_MAX_BLOCKS, ENGINE_RENEW_POOL_BLOCK_SIZE_BITS, sizeof(EngineRenew), &EngineRenewPoolNewBlock, NULL, 0, 0, NULL };
2848
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   455
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   456
static void EngineRenewPoolNewBlock(uint start_item)
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   457
{
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   458
	EngineRenew *er;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   459
4348
95ba9ff1612e (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   460
	/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
95ba9ff1612e (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   461
	 *  TODO - This is just a temporary stage, this will be removed. */
4358
a0b557926540 (svn r6059) -Fix(r6049): FOR_ALL was not use to not skip invalid items, so really don't skip invalid items :)
glx
parents: 4348
diff changeset
   462
	for (er = GetEngineRenew(start_item); er != NULL; er = (er->index + 1 < GetEngineRenewPoolSize()) ? GetEngineRenew(er->index + 1) : NULL) {
2848
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   463
		er->index = start_item++;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   464
		er->from = INVALID_ENGINE;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   465
	}
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   466
}
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   467
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   468
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   469
static EngineRenew *AllocateEngineRenew(void)
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   470
{
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   471
	EngineRenew *er;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   472
4348
95ba9ff1612e (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   473
	/* We don't use FOR_ALL here, because FOR_ALL skips invalid items.
95ba9ff1612e (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   474
	 *  TODO - This is just a temporary stage, this will be removed. */
4358
a0b557926540 (svn r6059) -Fix(r6049): FOR_ALL was not use to not skip invalid items, so really don't skip invalid items :)
glx
parents: 4348
diff changeset
   475
	for (er = GetEngineRenew(0); er != NULL; er = (er->index + 1 < GetEngineRenewPoolSize()) ? GetEngineRenew(er->index + 1) : NULL) {
4348
95ba9ff1612e (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   476
		if (IsValidEngineRenew(er)) continue;
95ba9ff1612e (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   477
95ba9ff1612e (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   478
		er->to = INVALID_ENGINE;
95ba9ff1612e (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   479
		er->next = NULL;
95ba9ff1612e (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   480
		return er;
2848
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   481
	}
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   482
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   483
	/* Check if we can add a block to the pool */
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   484
	if (AddBlockToPool(&_engine_renew_pool)) return AllocateEngineRenew();
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   485
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   486
	return NULL;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   487
}
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   488
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   489
/**
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   490
 * Retrieves the EngineRenew that specifies the replacement of the given
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   491
 * engine type from the given renewlist */
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   492
static EngineRenew *GetEngineReplacement(EngineRenewList erl, EngineID engine)
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   493
{
4348
95ba9ff1612e (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   494
	EngineRenew *er = (EngineRenew *)erl;
95ba9ff1612e (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   495
2848
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   496
	while (er) {
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   497
		if (er->from == engine) return er;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   498
		er = er->next;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   499
	}
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   500
	return NULL;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   501
}
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   502
4171
3fadda3afe70 (svn r5609) CodeChange : Apply coding style
belugas
parents: 4077
diff changeset
   503
void RemoveAllEngineReplacement(EngineRenewList *erl)
2848
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   504
{
4348
95ba9ff1612e (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   505
	EngineRenew *er = (EngineRenew *)(*erl);
4384
293c0d26294c (svn r6137) -Codechange: some very minor cleanups:
truelight
parents: 4358
diff changeset
   506
	EngineRenew *next;
4348
95ba9ff1612e (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   507
2848
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   508
	while (er) {
4384
293c0d26294c (svn r6137) -Codechange: some very minor cleanups:
truelight
parents: 4358
diff changeset
   509
		next = er->next;
293c0d26294c (svn r6137) -Codechange: some very minor cleanups:
truelight
parents: 4358
diff changeset
   510
		DeleteEngineRenew(er);
293c0d26294c (svn r6137) -Codechange: some very minor cleanups:
truelight
parents: 4358
diff changeset
   511
		er = next;
2848
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   512
	}
4348
95ba9ff1612e (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   513
	*erl = NULL; // Empty list
2848
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   514
}
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   515
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   516
EngineID EngineReplacement(EngineRenewList erl, EngineID engine)
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   517
{
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   518
	const EngineRenew *er = GetEngineReplacement(erl, engine);
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   519
	return er == NULL ? INVALID_ENGINE : er->to;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   520
}
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   521
4171
3fadda3afe70 (svn r5609) CodeChange : Apply coding style
belugas
parents: 4077
diff changeset
   522
int32 AddEngineReplacement(EngineRenewList *erl, EngineID old_engine, EngineID new_engine, uint32 flags)
2848
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   523
{
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   524
	EngineRenew *er;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   525
4348
95ba9ff1612e (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   526
	/* Check if the old vehicle is already in the list */
2848
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   527
	er = GetEngineReplacement(*erl, old_engine);
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   528
	if (er != NULL) {
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   529
		if (flags & DC_EXEC) er->to = new_engine;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   530
		return 0;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   531
	}
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   532
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   533
	er = AllocateEngineRenew();
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   534
	if (er == NULL) return CMD_ERROR;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   535
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   536
	if (flags & DC_EXEC) {
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   537
		er->from = old_engine;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   538
		er->to = new_engine;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   539
4348
95ba9ff1612e (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   540
		/* Insert before the first element */
95ba9ff1612e (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   541
		er->next = (EngineRenew *)(*erl);
95ba9ff1612e (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   542
		*erl = (EngineRenewList)er;
2848
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   543
	}
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   544
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   545
	return 0;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   546
}
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   547
4171
3fadda3afe70 (svn r5609) CodeChange : Apply coding style
belugas
parents: 4077
diff changeset
   548
int32 RemoveEngineReplacement(EngineRenewList *erl, EngineID engine, uint32 flags)
2848
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   549
{
4348
95ba9ff1612e (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   550
	EngineRenew *er = (EngineRenew *)(*erl);
4171
3fadda3afe70 (svn r5609) CodeChange : Apply coding style
belugas
parents: 4077
diff changeset
   551
	EngineRenew *prev = NULL;
2848
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   552
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   553
	while (er)
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   554
	{
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   555
		if (er->from == engine) {
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   556
			if (flags & DC_EXEC) {
4348
95ba9ff1612e (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   557
				if (prev == NULL) { // First element
95ba9ff1612e (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   558
					/* The second becomes the new first element */
95ba9ff1612e (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   559
					*erl = (EngineRenewList)er->next;
2848
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   560
				} else {
4348
95ba9ff1612e (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   561
					/* Cut this element out */
95ba9ff1612e (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   562
					prev->next = er->next;
2848
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   563
				}
4384
293c0d26294c (svn r6137) -Codechange: some very minor cleanups:
truelight
parents: 4358
diff changeset
   564
				DeleteEngineRenew(er);
2848
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   565
			}
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   566
			return 0;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   567
		}
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   568
		prev = er;
4348
95ba9ff1612e (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   569
		er = er->next;
2848
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   570
	}
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   571
4348
95ba9ff1612e (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   572
	return CMD_ERROR;
2848
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   573
}
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   574
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   575
static const SaveLoad _engine_renew_desc[] = {
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   576
	SLE_VAR(EngineRenew, from, SLE_UINT16),
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   577
	SLE_VAR(EngineRenew, to,   SLE_UINT16),
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   578
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   579
	SLE_REF(EngineRenew, next, REF_ENGINE_RENEWS),
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   580
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   581
	SLE_END()
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   582
};
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   583
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   584
static void Save_ERNW(void)
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   585
{
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   586
	EngineRenew *er;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   587
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   588
	FOR_ALL_ENGINE_RENEWS(er) {
4348
95ba9ff1612e (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   589
		SlSetArrayIndex(er->index);
95ba9ff1612e (svn r6049) -Codechange: forgot EngineRenew in r6047
truelight
parents: 4346
diff changeset
   590
		SlObject(er, _engine_renew_desc);
2848
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   591
	}
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   592
}
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   593
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   594
static void Load_ERNW(void)
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   595
{
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   596
	int index;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   597
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   598
	while ((index = SlIterateArray()) != -1) {
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   599
		EngineRenew *er;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   600
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   601
		if (!AddBlockIfNeeded(&_engine_renew_pool, index))
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   602
			error("EngineRenews: failed loading savegame: too many EngineRenews");
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   603
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   604
		er = GetEngineRenew(index);
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   605
		SlObject(er, _engine_renew_desc);
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   606
	}
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   607
}
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   608
1881
023a134a4b12 (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
   609
static const SaveLoad _engine_desc[] = {
4344
5d0e40cd67b9 (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
   610
	SLE_CONDVAR(Engine, intro_date,          SLE_FILE_U16 | SLE_VAR_I32,  0,  30),
5d0e40cd67b9 (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_INT32,                  31, SL_MAX_VERSION),
5d0e40cd67b9 (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, age,                 SLE_FILE_U16 | SLE_VAR_I32,  0,  30),
5d0e40cd67b9 (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_INT32,                  31, SL_MAX_VERSION),
5d0e40cd67b9 (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_VAR(Engine, reliability,         SLE_UINT16),
5d0e40cd67b9 (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_spd_dec, SLE_UINT16),
5d0e40cd67b9 (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_start,   SLE_UINT16),
5d0e40cd67b9 (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_max,     SLE_UINT16),
5d0e40cd67b9 (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_final,   SLE_UINT16),
5d0e40cd67b9 (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, duration_phase_1,    SLE_UINT16),
5d0e40cd67b9 (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_2,    SLE_UINT16),
5d0e40cd67b9 (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_3,    SLE_UINT16),
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   622
4344
5d0e40cd67b9 (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
   623
	    SLE_VAR(Engine, lifelength,          SLE_UINT8),
5d0e40cd67b9 (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, flags,               SLE_UINT8),
5d0e40cd67b9 (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, preview_player,      SLE_UINT8),
5d0e40cd67b9 (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_wait,        SLE_UINT8),
5d0e40cd67b9 (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, railtype,            SLE_UINT8),
5d0e40cd67b9 (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
   628
	    SLE_VAR(Engine, player_avail,        SLE_UINT8),
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   629
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   630
	// reserve extra space in savegame here. (currently 16 bytes)
3222
8bfed4267cba (svn r3895) - Add proper SLE(G)_CONDNULL macros for the empty space reservation in savegames and update where used
Darkvater
parents: 3047
diff changeset
   631
	SLE_CONDNULL(16, 2, SL_MAX_VERSION),
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   632
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   633
	SLE_END()
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   634
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   635
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1009
diff changeset
   636
static void Save_ENGN(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   637
{
2242
27fa4807cd61 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   638
	uint i;
27fa4807cd61 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   639
27fa4807cd61 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   640
	for (i = 0; i != lengthof(_engines); i++) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   641
		SlSetArrayIndex(i);
2242
27fa4807cd61 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   642
		SlObject(&_engines[i], _engine_desc);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   643
	}
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
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1009
diff changeset
   646
static void Load_ENGN(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   647
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   648
	int index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   649
	while ((index = SlIterateArray()) != -1) {
1926
68d60188a22f (svn r2432) Use GetEngine() instead of DEREF_ENGINE() or even _engines[]
tron
parents: 1908
diff changeset
   650
		SlObject(GetEngine(index), _engine_desc);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   651
	}
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
1093
e8d26c7dc42f (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1009
diff changeset
   654
static void LoadSave_ENGS(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   655
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   656
	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
   657
}
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
const ChunkHandler _engine_chunk_handlers[] = {
2848
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   660
	{ 'ENGN', Save_ENGN,     Load_ENGN,     CH_ARRAY          },
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   661
	{ 'ENGS', LoadSave_ENGS, LoadSave_ENGS, CH_RIFF           },
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   662
	{ '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
   663
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   664
2848
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   665
void InitializeEngines(void)
1197
4322cf8d6ae7 (svn r1701) Style police ^^
tron
parents: 1196
diff changeset
   666
{
2848
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   667
	/* Clean the engine renew pool and create 1 block in it */
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   668
	CleanPool(&_engine_renew_pool);
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
   669
	AddBlockToPool(&_engine_renew_pool);
1196
115f46e3807d (svn r1700) - Fix: Hacked clients can no longer be used to build vehicles that are not available yet (Hackykid)
bjarni
parents: 1096
diff changeset
   670
}