engine.c
author matthijs
Wed, 22 Mar 2006 22:26:16 +0000
branch0.4.5
changeset 9958 bed516c67d61
parent 9922 3d7645168508
permissions -rw-r--r--
(svn r4041) [Debian] Change next version number to 0.4.6 instead of 0.4.5.1.
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"
2201
3300b6ce8bce (svn r2717) Move _userstring to strings.[ch]
tron
parents: 2186
diff changeset
     7
#include "string.h"
3300b6ce8bce (svn r2717) Move _userstring to strings.[ch]
tron
parents: 2186
diff changeset
     8
#include "strings.h"
507
8aa8100b0b22 (svn r815) Include strings.h only in the files which need it.
tron
parents: 485
diff changeset
     9
#include "table/strings.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    10
#include "engine.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    11
#include "table/engines.h"
1009
a7ccb8e39cbc (svn r1508) Remove duplicate declarations and include proper headers where necessary
tron
parents: 964
diff changeset
    12
#include "gfx.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    13
#include "player.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    14
#include "command.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    15
#include "vehicle.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    16
#include "news.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    17
#include "saveload.h"
405
6830ae7a0d5d (svn r602) -newgrf: Move DrawTileSeqStruct & co and struct SpriteGroup to sprite.h (pasky)
darkvater
parents: 369
diff changeset
    18
#include "sprite.h"
2159
3b634157c3b2 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2147
diff changeset
    19
#include "variables.h"
2708
9d1a8d5dc3ea (svn r3252) - NewGRF: Map new train subtypes to old types for rail vehicles.
peter1138
parents: 2639
diff changeset
    20
#include "train.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    21
2763
d579caf58b59 (svn r3308) BAD
tron
parents: 2708
diff changeset
    22
EngineInfo _engine_info[TOTAL_NUM_ENGINES];
d579caf58b59 (svn r3308) BAD
tron
parents: 2708
diff changeset
    23
RailVehicleInfo _rail_vehicle_info[NUM_TRAIN_ENGINES];
d579caf58b59 (svn r3308) BAD
tron
parents: 2708
diff changeset
    24
ShipVehicleInfo _ship_vehicle_info[NUM_SHIP_ENGINES];
d579caf58b59 (svn r3308) BAD
tron
parents: 2708
diff changeset
    25
AircraftVehicleInfo _aircraft_vehicle_info[NUM_AIRCRAFT_ENGINES];
d579caf58b59 (svn r3308) BAD
tron
parents: 2708
diff changeset
    26
RoadVehicleInfo _road_vehicle_info[NUM_ROAD_ENGINES];
d579caf58b59 (svn r3308) BAD
tron
parents: 2708
diff changeset
    27
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    28
enum {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    29
	ENGINE_AVAILABLE = 1,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    30
	ENGINE_INTRODUCING = 2,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    31
	ENGINE_PREVIEWING = 4,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    32
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    33
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
    34
/** TRANSLATE FROM LOCAL CARGO TO GLOBAL CARGO ID'S.
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
    35
 * This maps the per-landscape cargo ID's to globally unique cargo ID's usable ie. in
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
    36
 * the custom GRF  files. It is basically just a transcribed table from TTDPatch's newgrf.txt.
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
 */
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
const CargoID _global_cargo_id[NUM_LANDSCAPE][NUM_CARGO] = {
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
	/* LT_NORMAL */ {GC_PASSENGERS, GC_COAL,  GC_MAIL, GC_OIL, GC_LIVESTOCK, GC_GOODS, GC_GRAIN, GC_WOOD, GC_IRON_ORE,    GC_STEEL,  GC_VALUABLES, GC_PAPER_TEMP},
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
    40
	/* LT_HILLY */  {GC_PASSENGERS, GC_COAL,  GC_MAIL, GC_OIL, GC_LIVESTOCK, GC_GOODS, GC_GRAIN, GC_WOOD, GC_INVALID,     GC_PAPER,  GC_VALUABLES, GC_FOOD },
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
	/* LT_DESERT */ {GC_PASSENGERS, GC_RUBBER,GC_MAIL, GC_OIL, GC_FRUIT,     GC_GOODS, GC_GRAIN, GC_WOOD, GC_COPPER_ORE,  GC_WATER,  GC_VALUABLES, GC_FOOD },
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
	/* LT_CANDY */  {GC_PASSENGERS, GC_SUGAR, GC_MAIL, GC_TOYS,GC_BATTERIES, GC_CANDY, GC_TOFFEE,GC_COLA, GC_COTTON_CANDY,GC_BUBBLES,GC_PLASTIC,   GC_FIZZY_DRINKS },
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
    43
	/**
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
    44
	 * - GC_INVALID (255) means that  cargo is not available for that climate
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
    45
	 * - GC_PAPER_TEMP (27) is paper in  temperate climate in TTDPatch
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
	 * Following can  be renumbered:
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
	 * - GC_DEFAULT (29) is the defa ult cargo for the purpose of spritesets
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
	 * - GC_PURCHASE (30) is the purchase list image (the equivalent of 0xff) for the purpose of spritesets
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
    49
	 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    50
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    51
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
/** BEGIN --- TRANSLATE FROM GLOBAL CARGO TO LOCAL CARGO ID'S **/
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
    53
/** Map global cargo ID's to local-cargo ID's */
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
    54
const CargoID _local_cargo_id_ctype[NUM_GLOBAL_CID] = {
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
	CT_PASSENGERS,CT_COAL,   CT_MAIL,        CT_OIL,      CT_LIVESTOCK,CT_GOODS,  CT_GRAIN,      CT_WOOD,         /*  0- 7 */
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
	CT_IRON_ORE,  CT_STEEL,  CT_VALUABLES,   CT_PAPER,    CT_FOOD,     CT_FRUIT,  CT_COPPER_ORE, CT_WATER,        /*  8-15 */
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
	CT_RUBBER,    CT_SUGAR,  CT_TOYS,        CT_BATTERIES,CT_CANDY,    CT_TOFFEE, CT_COLA,       CT_COTTON_CANDY, /* 16-23 */
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
	CT_BUBBLES,   CT_PLASTIC,CT_FIZZY_DRINKS,CT_PAPER     /* unsup. */,CT_HILLY_UNUSED,                           /* 24-28 */
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
    59
	CT_INVALID,   CT_INVALID                                                                                      /* 29-30 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    60
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    61
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
    62
#define MC(cargo) (1 << cargo)
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
    63
/** Bitmasked value where the global cargo ID is available in landscape
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
    64
 * 0: LT_NORMAL, 1: LT_HILLY, 2: LT_DESERT, 3: LT_CANDY */
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
    65
const uint32 _landscape_global_cargo_mask[NUM_LANDSCAPE] =
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
    66
{ /* LT_NORMAL: temperate */
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
    67
	MC(GC_PASSENGERS)|MC(GC_COAL)|MC(GC_MAIL)|MC(GC_OIL)|MC(GC_LIVESTOCK)|MC(GC_GOODS)|MC(GC_GRAIN)|MC(GC_WOOD)|
2611
65b5313bc525 (svn r3148) -NewGRF, Feature: Add support for cargo refitting specification by cargo classes.
peter1138
parents: 2602
diff changeset
    68
	MC(GC_IRON_ORE)|MC(GC_STEEL)|MC(GC_VALUABLES),
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
    69
	/* LT_HILLY: arctic */
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
    70
	MC(GC_PASSENGERS)|MC(GC_COAL)|MC(GC_MAIL)|MC(GC_OIL)|MC(GC_LIVESTOCK)|MC(GC_GOODS)|
2611
65b5313bc525 (svn r3148) -NewGRF, Feature: Add support for cargo refitting specification by cargo classes.
peter1138
parents: 2602
diff changeset
    71
	MC(GC_GRAIN)|MC(GC_WOOD)|MC(GC_VALUABLES)|MC(GC_PAPER)|MC(GC_FOOD),
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
    72
	/* LT_DESERT: rainforest/desert */
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
    73
	MC(GC_PASSENGERS)|MC(GC_MAIL)|MC(GC_OIL)|MC(GC_GOODS)|MC(GC_GRAIN)|MC(GC_WOOD)|
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
    74
	MC(GC_VALUABLES)|MC(GC_FOOD)|MC(GC_FRUIT)|MC(GC_COPPER_ORE)|MC(GC_WATER)|MC(GC_RUBBER),
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
    75
	/* LT_CANDY: toyland */
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
    76
	MC(GC_PASSENGERS)|MC(GC_MAIL)|MC(GC_SUGAR)|MC(GC_TOYS)|MC(GC_BATTERIES)|MC(GC_CANDY)|
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
    77
	MC(GC_TOFFEE)|MC(GC_COLA)|MC(GC_COTTON_CANDY)|MC(GC_BUBBLES)|MC(GC_PLASTIC)|MC(GC_FIZZY_DRINKS)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    78
};
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
    79
/** END   --- TRANSLATE FROM GLOBAL CARGO TO LOCAL CARGO ID'S **/
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
    80
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
    81
/** 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
    82
 * 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
    83
 * us exactly what is refittable and what is not */
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
    84
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
    85
	/* Trains */
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
    86
	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)|
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
    87
	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)|
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
    88
	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),
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
    89
	/* 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
    90
	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
    91
	/* Ships */
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
    92
	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)|
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
    93
	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)|
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
    94
	MC(GC_CANDY)|MC(GC_TOFFEE)|MC(GC_COLA)|MC(GC_COTTON_CANDY)|MC(GC_BUBBLES)|MC(GC_PLASTIC)|MC(GC_FIZZY_DRINKS),
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
    95
	/* Aircraft */
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
    96
	MC(GC_PASSENGERS)|MC(GC_MAIL)|MC(GC_GOODS)|MC(GC_VALUABLES)|MC(GC_FOOD)|MC(GC_FRUIT)|MC(GC_SUGAR)|MC(GC_TOYS)|
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
    97
	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),
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
    98
	/* 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
    99
	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
   100
};
2611
65b5313bc525 (svn r3148) -NewGRF, Feature: Add support for cargo refitting specification by cargo classes.
peter1138
parents: 2602
diff changeset
   101
65b5313bc525 (svn r3148) -NewGRF, Feature: Add support for cargo refitting specification by cargo classes.
peter1138
parents: 2602
diff changeset
   102
/**
65b5313bc525 (svn r3148) -NewGRF, Feature: Add support for cargo refitting specification by cargo classes.
peter1138
parents: 2602
diff changeset
   103
 * Bitmask of classes for cargo types.
65b5313bc525 (svn r3148) -NewGRF, Feature: Add support for cargo refitting specification by cargo classes.
peter1138
parents: 2602
diff changeset
   104
 */
65b5313bc525 (svn r3148) -NewGRF, Feature: Add support for cargo refitting specification by cargo classes.
peter1138
parents: 2602
diff changeset
   105
const uint32 cargo_classes[16] = {
65b5313bc525 (svn r3148) -NewGRF, Feature: Add support for cargo refitting specification by cargo classes.
peter1138
parents: 2602
diff changeset
   106
	/* Passengers */ MC(GC_PASSENGERS),
65b5313bc525 (svn r3148) -NewGRF, Feature: Add support for cargo refitting specification by cargo classes.
peter1138
parents: 2602
diff changeset
   107
	/* Mail       */ MC(GC_MAIL),
65b5313bc525 (svn r3148) -NewGRF, Feature: Add support for cargo refitting specification by cargo classes.
peter1138
parents: 2602
diff changeset
   108
	/* Express    */ MC(GC_GOODS)|MC(GC_FOOD)|MC(GC_CANDY),
65b5313bc525 (svn r3148) -NewGRF, Feature: Add support for cargo refitting specification by cargo classes.
peter1138
parents: 2602
diff changeset
   109
	/* Armoured   */ MC(GC_VALUABLES),
65b5313bc525 (svn r3148) -NewGRF, Feature: Add support for cargo refitting specification by cargo classes.
peter1138
parents: 2602
diff changeset
   110
	/* Bulk       */ MC(GC_COAL)|MC(GC_GRAIN)|MC(GC_IRON_ORE)|MC(GC_COPPER_ORE)|MC(GC_FRUIT)|MC(GC_SUGAR)|MC(GC_TOFFEE)|MC(GC_COTTON_CANDY),
65b5313bc525 (svn r3148) -NewGRF, Feature: Add support for cargo refitting specification by cargo classes.
peter1138
parents: 2602
diff changeset
   111
	/* Piece      */ MC(GC_LIVESTOCK)|MC(GC_WOOD)|MC(GC_STEEL)|MC(GC_PAPER)|MC(GC_TOYS)|MC(GC_BATTERIES)|MC(GC_BUBBLES)|MC(GC_FIZZY_DRINKS),
65b5313bc525 (svn r3148) -NewGRF, Feature: Add support for cargo refitting specification by cargo classes.
peter1138
parents: 2602
diff changeset
   112
	/* Liquids    */ MC(GC_OIL)|MC(GC_WATER)|MC(GC_RUBBER)|MC(GC_COLA)|MC(GC_PLASTIC),
65b5313bc525 (svn r3148) -NewGRF, Feature: Add support for cargo refitting specification by cargo classes.
peter1138
parents: 2602
diff changeset
   113
	/* Chilled    */ MC(GC_FOOD)|MC(GC_FRUIT),
65b5313bc525 (svn r3148) -NewGRF, Feature: Add support for cargo refitting specification by cargo classes.
peter1138
parents: 2602
diff changeset
   114
	/* Undefined  */ 0, 0, 0, 0, 0, 0, 0, 0
65b5313bc525 (svn r3148) -NewGRF, Feature: Add support for cargo refitting specification by cargo classes.
peter1138
parents: 2602
diff changeset
   115
};
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
   116
#undef MC
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   117
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
   118
void ShowEnginePreviewWindow(EngineID engine);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   119
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
   120
void DeleteCustomEngineNames(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   121
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   122
	uint i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   123
	StringID old;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   124
1474
d83520edcda6 (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
   125
	for (i = 0; i != TOTAL_NUM_ENGINES; i++) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   126
		old = _engine_name_strings[i];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   127
		_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
   128
		DeleteName(old);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   129
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   130
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   131
	_vehicle_design_names &= ~1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   132
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   133
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
   134
void LoadCustomEngineNames(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   135
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   136
	// XXX: not done */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   137
	DEBUG(misc, 1) ("LoadCustomEngineNames: not done");
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   138
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   139
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
   140
static void SetupEngineNames(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   141
{
1474
d83520edcda6 (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
   142
	StringID *name;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   143
1474
d83520edcda6 (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
   144
	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
   145
		*name = STR_SV_EMPTY;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   146
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   147
	DeleteCustomEngineNames();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   148
	LoadCustomEngineNames();
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
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
   151
static void AdjustAvailAircraft(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   152
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   153
	uint16 date = _date;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   154
	byte avail = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   155
	if (date >= 12784) avail |= 2; // big airport
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   156
	if (date < 14610 || _patches.always_small_airport) avail |= 1;  // small airport
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   157
	if (date >= 15706) avail |= 4; // enable heliport
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   158
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   159
	if (avail != _avail_aircraft) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   160
		_avail_aircraft = avail;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   161
		InvalidateWindow(WC_BUILD_STATION, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   162
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   163
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   164
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   165
static void CalcEngineReliability(Engine *e)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   166
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   167
	uint age = e->age;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   168
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   169
	if (age < e->duration_phase_1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   170
		uint start = e->reliability_start;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   171
		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
   172
	} 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
   173
		e->reliability = e->reliability_max;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   174
	} 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
   175
		uint max = e->reliability_max;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   176
		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
   177
	} 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
   178
		// 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
   179
		// 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
   180
		// 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
   181
		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
   182
		e->reliability = e->reliability_final;
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
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
   186
void AddTypeToEngines(void)
842
ebfd36603ab9 (svn r1323) Adding autoreplace feature
bjarni
parents: 819
diff changeset
   187
{
2242
27fa4807cd61 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   188
	Engine* e = _engines;
842
ebfd36603ab9 (svn r1323) Adding autoreplace feature
bjarni
parents: 819
diff changeset
   189
2242
27fa4807cd61 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   190
	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
   191
	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
   192
	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
   193
	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
   194
	do e->type = VEH_Special;  while (++e < endof(_engines));
842
ebfd36603ab9 (svn r1323) Adding autoreplace feature
bjarni
parents: 819
diff changeset
   195
}
ebfd36603ab9 (svn r1323) Adding autoreplace feature
bjarni
parents: 819
diff changeset
   196
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
   197
void StartupEngines(void)
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
	Engine *e;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   200
	const EngineInfo *ei;
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
	SetupEngineNames();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   203
2242
27fa4807cd61 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   204
	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
   205
		uint32 r;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   206
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   207
		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
   208
		e->railtype = ei->railtype;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   209
		e->flags = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   210
		e->player_avail = 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 114
diff changeset
   211
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
   212
		// 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
   213
		// 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
   214
		// added.
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   215
		r = Random();
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
   216
		e->intro_date = ei->base_intro <= 729 ? ei->base_intro : GB(r, 0, 9) + ei->base_intro;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   217
		if (e->intro_date <= _date) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   218
			e->age = (_date - e->intro_date) >> 5;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   219
			e->player_avail = (byte)-1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   220
			e->flags |= ENGINE_AVAILABLE;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   221
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   222
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
   223
		e->reliability_start = GB(r, 16, 14) + 0x7AE0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   224
		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
   225
		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
   226
		e->reliability_final = GB(r, 16, 14) + 0x3FFF;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   227
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   228
		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
   229
		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
   230
		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
   231
		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
   232
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   233
		e->reliability_spd_dec = (ei->unk2&0x7F) << 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   234
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   235
		/* my invented flag for something that is a wagon */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   236
		if (ei->unk2 & 0x80) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   237
			e->age = 0xFFFF;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   238
		} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   239
			CalcEngineReliability(e);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   240
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   241
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   242
		e->lifelength = ei->lifelength + _patches.extend_vehicle_life;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   243
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   244
		// 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
   245
		if (!HASBIT(ei->climates, _opt.landscape)) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   246
			e->flags |= ENGINE_AVAILABLE;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   247
			e->player_avail = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   248
		}
915
013cb2d74800 (svn r1402) Trim trailing whitespace
tron
parents: 842
diff changeset
   249
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
   250
		/* 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
   251
		   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
   252
		   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
   253
		   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
   254
		   even if it is not a vehicle (yet)*/
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   255
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   256
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   257
	AdjustAvailAircraft();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   258
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   259
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   260
// TODO: We don't support cargo-specific wagon overrides. Pretty exotic... ;-) --pasky
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   261
1477
2174a73b11c9 (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   262
typedef struct WagonOverride {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   263
	byte *train_id;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   264
	int trains;
2488
cbe3d6b60c27 (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   265
	SpriteGroup *group;
1477
2174a73b11c9 (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   266
} WagonOverride;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   267
1477
2174a73b11c9 (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   268
typedef struct WagonOverrides {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   269
	int overrides_count;
1477
2174a73b11c9 (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   270
	WagonOverride *overrides;
2174a73b11c9 (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   271
} WagonOverrides;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   272
1477
2174a73b11c9 (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   273
static WagonOverrides _engine_wagon_overrides[TOTAL_NUM_ENGINES];
2174a73b11c9 (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   274
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
   275
void SetWagonOverrideSprites(EngineID engine, SpriteGroup *group, byte *train_id,
1477
2174a73b11c9 (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   276
	int trains)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   277
{
1477
2174a73b11c9 (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   278
	WagonOverrides *wos;
2174a73b11c9 (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   279
	WagonOverride *wo;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   280
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   281
	wos = &_engine_wagon_overrides[engine];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   282
	wos->overrides_count++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   283
	wos->overrides = realloc(wos->overrides,
1477
2174a73b11c9 (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   284
		wos->overrides_count * sizeof(*wos->overrides));
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 114
diff changeset
   285
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   286
	wo = &wos->overrides[wos->overrides_count - 1];
408
63a8c0505aab (svn r605) -newgrf: Framework for supporting variational spritegroups . Deterministic only at the moment, but random ones support shouldn't be that difficult now It doesn't do anything, but makes these actions actually possible (pasky).
darkvater
parents: 405
diff changeset
   287
	/* FIXME: If we are replacing an override, release original SpriteGroup
63a8c0505aab (svn r605) -newgrf: Framework for supporting variational spritegroups . Deterministic only at the moment, but random ones support shouldn't be that difficult now It doesn't do anything, but makes these actions actually possible (pasky).
darkvater
parents: 405
diff changeset
   288
	 * to prevent leaks. But first we need to refcount the SpriteGroup.
63a8c0505aab (svn r605) -newgrf: Framework for supporting variational spritegroups . Deterministic only at the moment, but random ones support shouldn't be that difficult now It doesn't do anything, but makes these actions actually possible (pasky).
darkvater
parents: 405
diff changeset
   289
	 * --pasky */
2488
cbe3d6b60c27 (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   290
	wo->group = group;
2490
a0db300b6da6 (svn r3016) -NewGRF: Implement the start of reference counting for sprite groups. This will allow unloading.
peter1138
parents: 2489
diff changeset
   291
	group->ref_count++;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   292
	wo->trains = trains;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   293
	wo->train_id = malloc(trains);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   294
	memcpy(wo->train_id, train_id, trains);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   295
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   296
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
   297
static const SpriteGroup *GetWagonOverrideSpriteSet(EngineID engine, byte overriding_engine)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   298
{
2242
27fa4807cd61 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   299
	const WagonOverrides *wos = &_engine_wagon_overrides[engine];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   300
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   301
408
63a8c0505aab (svn r605) -newgrf: Framework for supporting variational spritegroups . Deterministic only at the moment, but random ones support shouldn't be that difficult now It doesn't do anything, but makes these actions actually possible (pasky).
darkvater
parents: 405
diff changeset
   302
	// XXX: This could turn out to be a timesink on profiles. We could
63a8c0505aab (svn r605) -newgrf: Framework for supporting variational spritegroups . Deterministic only at the moment, but random ones support shouldn't be that difficult now It doesn't do anything, but makes these actions actually possible (pasky).
darkvater
parents: 405
diff changeset
   303
	// always just dedicate 65535 bytes for an [engine][train] trampoline
63a8c0505aab (svn r605) -newgrf: Framework for supporting variational spritegroups . Deterministic only at the moment, but random ones support shouldn't be that difficult now It doesn't do anything, but makes these actions actually possible (pasky).
darkvater
parents: 405
diff changeset
   304
	// for O(1). Or O(logMlogN) and searching binary tree or smt. like
63a8c0505aab (svn r605) -newgrf: Framework for supporting variational spritegroups . Deterministic only at the moment, but random ones support shouldn't be that difficult now It doesn't do anything, but makes these actions actually possible (pasky).
darkvater
parents: 405
diff changeset
   305
	// that. --pasky
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   306
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   307
	for (i = 0; i < wos->overrides_count; i++) {
2242
27fa4807cd61 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   308
		const WagonOverride *wo = &wos->overrides[i];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   309
		int j;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   310
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   311
		for (j = 0; j < wo->trains; j++) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   312
			if (wo->train_id[j] == overriding_engine)
2488
cbe3d6b60c27 (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   313
				return wo->group;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   314
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   315
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   316
	return NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   317
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   318
2491
dfe9542f783e (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   319
/**
dfe9542f783e (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   320
 * Unload all wagon override sprite groups.
dfe9542f783e (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   321
 */
dfe9542f783e (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   322
void UnloadWagonOverrides(void)
dfe9542f783e (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   323
{
dfe9542f783e (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   324
	WagonOverrides *wos;
dfe9542f783e (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   325
	WagonOverride *wo;
dfe9542f783e (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   326
	EngineID engine;
dfe9542f783e (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   327
	int i;
dfe9542f783e (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   328
dfe9542f783e (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   329
	for (engine = 0; engine < TOTAL_NUM_ENGINES; engine++) {
dfe9542f783e (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   330
		wos = &_engine_wagon_overrides[engine];
dfe9542f783e (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   331
		for (i = 0; i < wos->overrides_count; i++) {
dfe9542f783e (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   332
			wo = &wos->overrides[i];
dfe9542f783e (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   333
			UnloadSpriteGroup(&wo->group);
dfe9542f783e (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   334
			free(wo->train_id);
dfe9542f783e (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   335
		}
dfe9542f783e (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   336
		free(wos->overrides);
dfe9542f783e (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   337
		wos->overrides_count = 0;
dfe9542f783e (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   338
		wos->overrides = NULL;
dfe9542f783e (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   339
	}
dfe9542f783e (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   340
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   341
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   342
// 0 - 28 are cargos, 29 is default, 30 is the advert (purchase list)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   343
// (It isn't and shouldn't be like this in the GRF files since new cargo types
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   344
// may appear in future - however it's more convenient to store it like this in
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   345
// memory. --pasky)
2488
cbe3d6b60c27 (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   346
static SpriteGroup *engine_custom_sprites[TOTAL_NUM_ENGINES][NUM_GLOBAL_CID];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   347
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
   348
void SetCustomEngineSprites(EngineID engine, byte cargo, SpriteGroup *group)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   349
{
2491
dfe9542f783e (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   350
	if (engine_custom_sprites[engine][cargo] != NULL) {
dfe9542f783e (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   351
		DEBUG(grf, 6)("SetCustomEngineSprites: engine `%d' cargo `%d' already has group -- removing.", engine, cargo);
dfe9542f783e (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   352
		UnloadSpriteGroup(&engine_custom_sprites[engine][cargo]);
dfe9542f783e (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   353
	}
2488
cbe3d6b60c27 (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   354
	engine_custom_sprites[engine][cargo] = group;
2490
a0db300b6da6 (svn r3016) -NewGRF: Implement the start of reference counting for sprite groups. This will allow unloading.
peter1138
parents: 2489
diff changeset
   355
	group->ref_count++;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   356
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   357
2491
dfe9542f783e (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   358
/**
dfe9542f783e (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   359
 * Unload all engine sprite groups.
dfe9542f783e (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   360
 */
dfe9542f783e (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   361
void UnloadCustomEngineSprites(void)
dfe9542f783e (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   362
{
dfe9542f783e (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   363
	EngineID engine;
dfe9542f783e (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   364
	CargoID cargo;
dfe9542f783e (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   365
dfe9542f783e (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   366
	for (engine = 0; engine < TOTAL_NUM_ENGINES; engine++) {
dfe9542f783e (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   367
		for (cargo = 0; cargo < NUM_GLOBAL_CID; cargo++) {
dfe9542f783e (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   368
			if (engine_custom_sprites[engine][cargo] != NULL) {
dfe9542f783e (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   369
				DEBUG(grf, 6)("UnloadCustomEngineSprites: Unloading group for engine `%d' cargo `%d'.", engine, cargo);
dfe9542f783e (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   370
				UnloadSpriteGroup(&engine_custom_sprites[engine][cargo]);
dfe9542f783e (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   371
			}
dfe9542f783e (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   372
		}
dfe9542f783e (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   373
	}
dfe9542f783e (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   374
}
dfe9542f783e (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   375
2708
9d1a8d5dc3ea (svn r3252) - NewGRF: Map new train subtypes to old types for rail vehicles.
peter1138
parents: 2639
diff changeset
   376
static int MapOldSubType(const Vehicle *v)
9d1a8d5dc3ea (svn r3252) - NewGRF: Map new train subtypes to old types for rail vehicles.
peter1138
parents: 2639
diff changeset
   377
{
9d1a8d5dc3ea (svn r3252) - NewGRF: Map new train subtypes to old types for rail vehicles.
peter1138
parents: 2639
diff changeset
   378
	if (v->type != VEH_Train) return v->subtype;
9d1a8d5dc3ea (svn r3252) - NewGRF: Map new train subtypes to old types for rail vehicles.
peter1138
parents: 2639
diff changeset
   379
	if (IsTrainEngine(v)) return 0;
9d1a8d5dc3ea (svn r3252) - NewGRF: Map new train subtypes to old types for rail vehicles.
peter1138
parents: 2639
diff changeset
   380
	if (IsFreeWagon(v)) return 4;
9d1a8d5dc3ea (svn r3252) - NewGRF: Map new train subtypes to old types for rail vehicles.
peter1138
parents: 2639
diff changeset
   381
	return 2;
9d1a8d5dc3ea (svn r3252) - NewGRF: Map new train subtypes to old types for rail vehicles.
peter1138
parents: 2639
diff changeset
   382
}
9d1a8d5dc3ea (svn r3252) - NewGRF: Map new train subtypes to old types for rail vehicles.
peter1138
parents: 2639
diff changeset
   383
2242
27fa4807cd61 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   384
typedef SpriteGroup *(*resolve_callback)(const SpriteGroup *spritegroup,
1883
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   385
	const Vehicle *veh, uint16 callback_info, void *resolve_func); /* XXX data pointer used as function pointer */
445
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   386
2242
27fa4807cd61 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   387
static const SpriteGroup* ResolveVehicleSpriteGroup(const SpriteGroup *spritegroup,
1883
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   388
	const Vehicle *veh, uint16 callback_info, resolve_callback resolve_func)
426
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   389
{
2488
cbe3d6b60c27 (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   390
	if (spritegroup == NULL)
cbe3d6b60c27 (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   391
		return NULL;
cbe3d6b60c27 (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   392
445
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   393
	//debug("spgt %d", spritegroup->type);
426
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   394
	switch (spritegroup->type) {
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   395
		case SGT_REAL:
1883
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   396
		case SGT_CALLBACK:
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   397
			return spritegroup;
426
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   398
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   399
		case SGT_DETERMINISTIC: {
2242
27fa4807cd61 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   400
			const DeterministicSpriteGroup *dsg = &spritegroup->g.determ;
27fa4807cd61 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   401
			const SpriteGroup *target;
426
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   402
			int value = -1;
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   403
433
00c1c7a58672 (svn r635) Fix choosing a spritegroup from deterministic variational spritegroups if there is no structure to search (i.e. in purchase lists) (pasky)
tron
parents: 426
diff changeset
   404
			//debug("[%p] Having fun resolving variable %x", veh, dsg->variable);
1883
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   405
			if (dsg->variable == 0x0C) {
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   406
				/* Callback ID */
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   407
				value = callback_info & 0xFF;
2602
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2589
diff changeset
   408
			} else if (dsg->variable == 0x10) {
f0e2dcce3695 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2589
diff changeset
   409
				value = (callback_info >> 8) & 0xFF;
1883
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   410
			} else if ((dsg->variable >> 6) == 0) {
426
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   411
				/* General property */
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   412
				value = GetDeterministicSpriteValue(dsg->variable);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   413
			} else {
433
00c1c7a58672 (svn r635) Fix choosing a spritegroup from deterministic variational spritegroups if there is no structure to search (i.e. in purchase lists) (pasky)
tron
parents: 426
diff changeset
   414
				/* Vehicle-specific property. */
00c1c7a58672 (svn r635) Fix choosing a spritegroup from deterministic variational spritegroups if there is no structure to search (i.e. in purchase lists) (pasky)
tron
parents: 426
diff changeset
   415
00c1c7a58672 (svn r635) Fix choosing a spritegroup from deterministic variational spritegroups if there is no structure to search (i.e. in purchase lists) (pasky)
tron
parents: 426
diff changeset
   416
				if (veh == NULL) {
00c1c7a58672 (svn r635) Fix choosing a spritegroup from deterministic variational spritegroups if there is no structure to search (i.e. in purchase lists) (pasky)
tron
parents: 426
diff changeset
   417
					/* We are in a purchase list of something,
00c1c7a58672 (svn r635) Fix choosing a spritegroup from deterministic variational spritegroups if there is no structure to search (i.e. in purchase lists) (pasky)
tron
parents: 426
diff changeset
   418
					 * and we are checking for something undefined.
00c1c7a58672 (svn r635) Fix choosing a spritegroup from deterministic variational spritegroups if there is no structure to search (i.e. in purchase lists) (pasky)
tron
parents: 426
diff changeset
   419
					 * That means we should get the first target
00c1c7a58672 (svn r635) Fix choosing a spritegroup from deterministic variational spritegroups if there is no structure to search (i.e. in purchase lists) (pasky)
tron
parents: 426
diff changeset
   420
					 * (NOT the default one). */
00c1c7a58672 (svn r635) Fix choosing a spritegroup from deterministic variational spritegroups if there is no structure to search (i.e. in purchase lists) (pasky)
tron
parents: 426
diff changeset
   421
					if (dsg->num_ranges > 0) {
2488
cbe3d6b60c27 (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   422
						target = dsg->ranges[0].group;
433
00c1c7a58672 (svn r635) Fix choosing a spritegroup from deterministic variational spritegroups if there is no structure to search (i.e. in purchase lists) (pasky)
tron
parents: 426
diff changeset
   423
					} else {
00c1c7a58672 (svn r635) Fix choosing a spritegroup from deterministic variational spritegroups if there is no structure to search (i.e. in purchase lists) (pasky)
tron
parents: 426
diff changeset
   424
						target = dsg->default_group;
00c1c7a58672 (svn r635) Fix choosing a spritegroup from deterministic variational spritegroups if there is no structure to search (i.e. in purchase lists) (pasky)
tron
parents: 426
diff changeset
   425
					}
1883
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   426
					return resolve_func(target, NULL, callback_info, resolve_func);
433
00c1c7a58672 (svn r635) Fix choosing a spritegroup from deterministic variational spritegroups if there is no structure to search (i.e. in purchase lists) (pasky)
tron
parents: 426
diff changeset
   427
				}
00c1c7a58672 (svn r635) Fix choosing a spritegroup from deterministic variational spritegroups if there is no structure to search (i.e. in purchase lists) (pasky)
tron
parents: 426
diff changeset
   428
426
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   429
				if (dsg->var_scope == VSG_SCOPE_PARENT) {
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   430
					/* First engine in the vehicle chain */
433
00c1c7a58672 (svn r635) Fix choosing a spritegroup from deterministic variational spritegroups if there is no structure to search (i.e. in purchase lists) (pasky)
tron
parents: 426
diff changeset
   431
					if (veh->type == VEH_Train)
426
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   432
						veh = GetFirstVehicleInChain(veh);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   433
				}
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   434
1855
caf6d1396112 (svn r2361) - Feature: [newgrf] Implement varaction2 property 0x41 and 0xDA. These are the position in and length of chain of consecutive vehicles with the same type, and index of the next wagon in the chain (INVALID_VEHICLE if last), resp. Improves displaying of some engines in the dbsetxl.
hackykid
parents: 1840
diff changeset
   435
				if (dsg->variable == 0x40 || dsg->variable == 0x41) {
433
00c1c7a58672 (svn r635) Fix choosing a spritegroup from deterministic variational spritegroups if there is no structure to search (i.e. in purchase lists) (pasky)
tron
parents: 426
diff changeset
   436
					if (veh->type == VEH_Train) {
2242
27fa4807cd61 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   437
						const Vehicle *u = GetFirstVehicleInChain(veh);
426
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   438
						byte chain_before = 0, chain_after = 0;
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   439
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   440
						while (u != veh) {
1855
caf6d1396112 (svn r2361) - Feature: [newgrf] Implement varaction2 property 0x41 and 0xDA. These are the position in and length of chain of consecutive vehicles with the same type, and index of the next wagon in the chain (INVALID_VEHICLE if last), resp. Improves displaying of some engines in the dbsetxl.
hackykid
parents: 1840
diff changeset
   441
							chain_before++;
caf6d1396112 (svn r2361) - Feature: [newgrf] Implement varaction2 property 0x41 and 0xDA. These are the position in and length of chain of consecutive vehicles with the same type, and index of the next wagon in the chain (INVALID_VEHICLE if last), resp. Improves displaying of some engines in the dbsetxl.
hackykid
parents: 1840
diff changeset
   442
							if (dsg->variable == 0x41 && u->engine_type != veh->engine_type)
caf6d1396112 (svn r2361) - Feature: [newgrf] Implement varaction2 property 0x41 and 0xDA. These are the position in and length of chain of consecutive vehicles with the same type, and index of the next wagon in the chain (INVALID_VEHICLE if last), resp. Improves displaying of some engines in the dbsetxl.
hackykid
parents: 1840
diff changeset
   443
								chain_before = 0;
426
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   444
							u = u->next;
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   445
						}
1855
caf6d1396112 (svn r2361) - Feature: [newgrf] Implement varaction2 property 0x41 and 0xDA. These are the position in and length of chain of consecutive vehicles with the same type, and index of the next wagon in the chain (INVALID_VEHICLE if last), resp. Improves displaying of some engines in the dbsetxl.
hackykid
parents: 1840
diff changeset
   446
						while (u->next != NULL && (dsg->variable == 0x40 || u->next->engine_type == veh->engine_type)) {
caf6d1396112 (svn r2361) - Feature: [newgrf] Implement varaction2 property 0x41 and 0xDA. These are the position in and length of chain of consecutive vehicles with the same type, and index of the next wagon in the chain (INVALID_VEHICLE if last), resp. Improves displaying of some engines in the dbsetxl.
hackykid
parents: 1840
diff changeset
   447
							chain_after++;
426
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   448
							u = u->next;
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   449
						};
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   450
1559
616d0c6c5f3d (svn r2063) Fix: the 0x40 deterministic spritegroup for vehicles was wrong, modified to match the wiki again. (The wiki was partially wrong, too! ;) (Patch by HackyKid.)
pasky
parents: 1539
diff changeset
   451
						value = chain_before | chain_after << 8
616d0c6c5f3d (svn r2063) Fix: the 0x40 deterministic spritegroup for vehicles was wrong, modified to match the wiki again. (The wiki was partially wrong, too! ;) (Patch by HackyKid.)
pasky
parents: 1539
diff changeset
   452
						        | (chain_before + chain_after) << 16;
426
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   453
					} else {
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   454
						value = 1; /* 1 vehicle in the chain */
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   455
					}
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   456
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   457
				} else {
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   458
					// TTDPatch runs on little-endian arch;
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   459
					// Variable is 0x80 + offset in TTD's vehicle structure
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   460
					switch (dsg->variable - 0x80) {
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 543
diff changeset
   461
#define veh_prop(id_, value_) case (id_): value = (value_); break
426
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   462
						veh_prop(0x00, veh->type);
2708
9d1a8d5dc3ea (svn r3252) - NewGRF: Map new train subtypes to old types for rail vehicles.
peter1138
parents: 2639
diff changeset
   463
						veh_prop(0x01, MapOldSubType(veh));
426
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   464
						veh_prop(0x04, veh->index);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   465
						veh_prop(0x05, veh->index & 0xFF);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   466
						/* XXX? Is THIS right? */
555
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 543
diff changeset
   467
						veh_prop(0x0A, PackOrder(&veh->current_order));
eec6c0294435 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 543
diff changeset
   468
						veh_prop(0x0B, PackOrder(&veh->current_order) & 0xff);
426
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   469
						veh_prop(0x0C, veh->num_orders);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   470
						veh_prop(0x0D, veh->cur_order_index);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   471
						veh_prop(0x10, veh->load_unload_time_rem);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   472
						veh_prop(0x11, veh->load_unload_time_rem & 0xFF);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   473
						veh_prop(0x12, veh->date_of_last_service);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   474
						veh_prop(0x13, veh->date_of_last_service & 0xFF);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   475
						veh_prop(0x14, veh->service_interval);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   476
						veh_prop(0x15, veh->service_interval & 0xFF);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   477
						veh_prop(0x16, veh->last_station_visited);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   478
						veh_prop(0x17, veh->tick_counter);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   479
						veh_prop(0x18, veh->max_speed);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   480
						veh_prop(0x19, veh->max_speed & 0xFF);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   481
						veh_prop(0x1F, veh->direction);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   482
						veh_prop(0x28, veh->cur_image);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   483
						veh_prop(0x29, veh->cur_image & 0xFF);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   484
						veh_prop(0x32, veh->vehstatus);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   485
						veh_prop(0x33, veh->vehstatus);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   486
						veh_prop(0x34, veh->cur_speed);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   487
						veh_prop(0x35, veh->cur_speed & 0xFF);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   488
						veh_prop(0x36, veh->subspeed);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   489
						veh_prop(0x37, veh->acceleration);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   490
						veh_prop(0x39, veh->cargo_type);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   491
						veh_prop(0x3A, veh->cargo_cap);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   492
						veh_prop(0x3B, veh->cargo_cap & 0xFF);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   493
						veh_prop(0x3C, veh->cargo_count);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   494
						veh_prop(0x3D, veh->cargo_count & 0xFF);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   495
						veh_prop(0x3E, veh->cargo_source); // Probably useless; so what
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   496
						veh_prop(0x3F, veh->cargo_days);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   497
						veh_prop(0x40, veh->age);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   498
						veh_prop(0x41, veh->age & 0xFF);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   499
						veh_prop(0x42, veh->max_age);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   500
						veh_prop(0x43, veh->max_age & 0xFF);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   501
						veh_prop(0x44, veh->build_year);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   502
						veh_prop(0x45, veh->unitnumber);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   503
						veh_prop(0x46, veh->engine_type);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   504
						veh_prop(0x47, veh->engine_type & 0xFF);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   505
						veh_prop(0x48, veh->spritenum);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   506
						veh_prop(0x49, veh->day_counter);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   507
						veh_prop(0x4A, veh->breakdowns_since_last_service);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   508
						veh_prop(0x4B, veh->breakdown_ctr);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   509
						veh_prop(0x4C, veh->breakdown_delay);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   510
						veh_prop(0x4D, veh->breakdown_chance);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   511
						veh_prop(0x4E, veh->reliability);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   512
						veh_prop(0x4F, veh->reliability & 0xFF);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   513
						veh_prop(0x50, veh->reliability_spd_dec);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   514
						veh_prop(0x51, veh->reliability_spd_dec & 0xFF);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   515
						veh_prop(0x52, veh->profit_this_year);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   516
						veh_prop(0x53, veh->profit_this_year & 0xFFFFFF);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   517
						veh_prop(0x54, veh->profit_this_year & 0xFFFF);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   518
						veh_prop(0x55, veh->profit_this_year & 0xFF);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   519
						veh_prop(0x56, veh->profit_last_year);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   520
						veh_prop(0x57, veh->profit_last_year & 0xFF);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   521
						veh_prop(0x58, veh->profit_last_year);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   522
						veh_prop(0x59, veh->profit_last_year & 0xFF);
1855
caf6d1396112 (svn r2361) - Feature: [newgrf] Implement varaction2 property 0x41 and 0xDA. These are the position in and length of chain of consecutive vehicles with the same type, and index of the next wagon in the chain (INVALID_VEHICLE if last), resp. Improves displaying of some engines in the dbsetxl.
hackykid
parents: 1840
diff changeset
   523
						veh_prop(0x5A, veh->next == NULL ? INVALID_VEHICLE : veh->next->index);
426
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   524
						veh_prop(0x5C, veh->value);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   525
						veh_prop(0x5D, veh->value & 0xFFFFFF);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   526
						veh_prop(0x5E, veh->value & 0xFFFF);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   527
						veh_prop(0x5F, veh->value & 0xFF);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   528
						veh_prop(0x60, veh->string_id);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   529
						veh_prop(0x61, veh->string_id & 0xFF);
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   530
						/* 00h..07h=sub image? 40h=in tunnel; actually some kind of status
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   531
						 * aircraft: >=13h when in flight
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   532
						 * train, ship: 80h=in depot
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   533
						 * rv: 0feh=in depot */
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   534
						/* TODO veh_prop(0x62, veh->???); */
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   535
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   536
						/* TODO: The rest is per-vehicle, I hope no GRF file looks so far.
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   537
						 * But they won't let us have an easy ride so surely *some* GRF
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   538
						 * file does. So someone needs to do this too. --pasky */
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   539
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   540
#undef veh_prop
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   541
					}
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   542
				}
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   543
			}
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   544
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   545
			target = value != -1 ? EvalDeterministicSpriteGroup(dsg, value) : dsg->default_group;
445
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   546
			//debug("Resolved variable %x: %d, %p", dsg->variable, value, callback);
1883
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   547
			return resolve_func(target, veh, callback_info, resolve_func);
445
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   548
		}
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   549
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   550
		case SGT_RANDOMIZED: {
2242
27fa4807cd61 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   551
			const RandomizedSpriteGroup *rsg = &spritegroup->g.random;
445
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   552
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   553
			if (veh == NULL) {
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   554
				/* Purchase list of something. Show the first one. */
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   555
				assert(rsg->num_groups > 0);
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   556
				//debug("going for %p: %d", rsg->groups[0], rsg->groups[0].type);
2488
cbe3d6b60c27 (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   557
				return resolve_func(rsg->groups[0], NULL, callback_info, resolve_func);
445
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   558
			}
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   559
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   560
			if (rsg->var_scope == VSG_SCOPE_PARENT) {
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   561
				/* First engine in the vehicle chain */
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   562
				if (veh->type == VEH_Train)
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   563
					veh = GetFirstVehicleInChain(veh);
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   564
			}
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   565
1883
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   566
			return resolve_func(EvalRandomizedSpriteGroup(rsg, veh->random_bits), veh, callback_info, resolve_func);
426
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   567
		}
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   568
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   569
		default:
445
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   570
			error("I don't know how to handle such a spritegroup %d!", spritegroup->type);
426
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   571
			return NULL;
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   572
	}
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   573
}
bd6f3c2cad9f (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   574
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
   575
static const SpriteGroup *GetVehicleSpriteGroup(EngineID engine, const Vehicle *v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   576
{
2242
27fa4807cd61 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   577
	const SpriteGroup *group;
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
   578
	byte cargo = GC_PURCHASE;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   579
414
e18cc252e2ea (svn r611) -newgrf: Change GetCustomEngineSprite() calling convention (invisible to users of GetCustomVehicle*() wrappers). Needed for deterministic spritegroups support (pasky).
darkvater
parents: 410
diff changeset
   580
	if (v != NULL) {
e18cc252e2ea (svn r611) -newgrf: Change GetCustomEngineSprite() calling convention (invisible to users of GetCustomVehicle*() wrappers). Needed for deterministic spritegroups support (pasky).
darkvater
parents: 410
diff changeset
   581
		cargo = _global_cargo_id[_opt.landscape][v->cargo_type];
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
   582
		assert(cargo != GC_INVALID);
414
e18cc252e2ea (svn r611) -newgrf: Change GetCustomEngineSprite() calling convention (invisible to users of GetCustomVehicle*() wrappers). Needed for deterministic spritegroups support (pasky).
darkvater
parents: 410
diff changeset
   583
	}
e18cc252e2ea (svn r611) -newgrf: Change GetCustomEngineSprite() calling convention (invisible to users of GetCustomVehicle*() wrappers). Needed for deterministic spritegroups support (pasky).
darkvater
parents: 410
diff changeset
   584
2488
cbe3d6b60c27 (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   585
	group = engine_custom_sprites[engine][cargo];
414
e18cc252e2ea (svn r611) -newgrf: Change GetCustomEngineSprite() calling convention (invisible to users of GetCustomVehicle*() wrappers). Needed for deterministic spritegroups support (pasky).
darkvater
parents: 410
diff changeset
   586
1560
50acca78ff8d (svn r2064) - Codechange: GetVehicleSpriteGroup() cleanup - drop overriding_engine, which was around probably only for historical reasons. (Paralellily developed by HackyKid.)
pasky
parents: 1559
diff changeset
   587
	if (v != NULL && v->type == VEH_Train) {
2242
27fa4807cd61 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   588
		const SpriteGroup *overset = GetWagonOverrideSpriteSet(engine, v->u.rail.first_engine);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   589
1477
2174a73b11c9 (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   590
		if (overset != NULL) group = overset;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   591
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 114
diff changeset
   592
445
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   593
	return group;
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   594
}
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   595
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
   596
int GetCustomEngineSprite(EngineID engine, const Vehicle *v, byte direction)
445
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   597
{
2242
27fa4807cd61 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   598
	const SpriteGroup *group;
27fa4807cd61 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   599
	const RealSpriteGroup *rsg;
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
   600
	byte cargo = GC_PURCHASE;
445
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   601
	byte loaded = 0;
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   602
	bool in_motion = 0;
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   603
	int totalsets, spriteset;
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   604
	int r;
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   605
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   606
	if (v != NULL) {
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   607
		int capacity = v->cargo_cap;
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   608
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   609
		cargo = _global_cargo_id[_opt.landscape][v->cargo_type];
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
   610
		assert(cargo != GC_INVALID);
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
   611
445
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   612
		if (capacity == 0) capacity = 1;
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   613
		loaded = (v->cargo_count * 100) / capacity;
2830
873da5589c15 (svn r3378) - NewGRF Fix: Use order status to determine whether a vehicle is loading instead
peter1138
parents: 2817
diff changeset
   614
873da5589c15 (svn r3378) - NewGRF Fix: Use order status to determine whether a vehicle is loading instead
peter1138
parents: 2817
diff changeset
   615
		if (v->type == VEH_Train) {
873da5589c15 (svn r3378) - NewGRF Fix: Use order status to determine whether a vehicle is loading instead
peter1138
parents: 2817
diff changeset
   616
			in_motion = GetFirstVehicleInChain(v)->current_order.type != OT_LOADING;
873da5589c15 (svn r3378) - NewGRF Fix: Use order status to determine whether a vehicle is loading instead
peter1138
parents: 2817
diff changeset
   617
		} else {
873da5589c15 (svn r3378) - NewGRF Fix: Use order status to determine whether a vehicle is loading instead
peter1138
parents: 2817
diff changeset
   618
			in_motion = v->current_order.type != OT_LOADING;
873da5589c15 (svn r3378) - NewGRF Fix: Use order status to determine whether a vehicle is loading instead
peter1138
parents: 2817
diff changeset
   619
		}
445
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   620
	}
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   621
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   622
	group = GetVehicleSpriteGroup(engine, v);
1883
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   623
	group = ResolveVehicleSpriteGroup(group, v, 0, (resolve_callback) ResolveVehicleSpriteGroup);
408
63a8c0505aab (svn r605) -newgrf: Framework for supporting variational spritegroups . Deterministic only at the moment, but random ones support shouldn't be that difficult now It doesn't do anything, but makes these actions actually possible (pasky).
darkvater
parents: 405
diff changeset
   624
2488
cbe3d6b60c27 (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   625
	if (group == NULL && cargo != GC_DEFAULT) {
369
ab2266938fa8 (svn r557) -newgrf: Rename all 'superset' tokens to 'group' and some other small renamings (pasky and octo).
darkvater
parents: 292
diff changeset
   626
		// This group is empty but perhaps there'll be a default one.
2488
cbe3d6b60c27 (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   627
		group = ResolveVehicleSpriteGroup(engine_custom_sprites[engine][GC_DEFAULT], v, 0,
445
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   628
		                                (resolve_callback) ResolveVehicleSpriteGroup);
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
2488
cbe3d6b60c27 (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   631
	if (group == NULL)
cbe3d6b60c27 (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   632
		return 0;
cbe3d6b60c27 (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   633
1883
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   634
	assert(group->type == SGT_REAL);
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   635
	rsg = &group->g.real;
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   636
408
63a8c0505aab (svn r605) -newgrf: Framework for supporting variational spritegroups . Deterministic only at the moment, but random ones support shouldn't be that difficult now It doesn't do anything, but makes these actions actually possible (pasky).
darkvater
parents: 405
diff changeset
   637
	if (!rsg->sprites_per_set) {
369
ab2266938fa8 (svn r557) -newgrf: Rename all 'superset' tokens to 'group' and some other small renamings (pasky and octo).
darkvater
parents: 292
diff changeset
   638
		// This group is empty. This function users should therefore
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   639
		// look up the sprite number in _engine_original_sprites.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   640
		return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   641
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   642
1988
a8db3e705778 (svn r2494) - Fix: [newgrf] Dont assume a spriteset can only have 4 or 8 sprites.
hackykid
parents: 1962
diff changeset
   643
	assert(rsg->sprites_per_set <= 8);
a8db3e705778 (svn r2494) - Fix: [newgrf] Dont assume a spriteset can only have 4 or 8 sprites.
hackykid
parents: 1962
diff changeset
   644
	direction %= rsg->sprites_per_set;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   645
408
63a8c0505aab (svn r605) -newgrf: Framework for supporting variational spritegroups . Deterministic only at the moment, but random ones support shouldn't be that difficult now It doesn't do anything, but makes these actions actually possible (pasky).
darkvater
parents: 405
diff changeset
   646
	totalsets = in_motion ? rsg->loaded_count : rsg->loading_count;
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
	// My aim here is to make it possible to visually determine absolutely
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   649
	// empty and totally full vehicles. --pasky
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   650
	if (loaded == 100 || totalsets == 1) { // full
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   651
		spriteset = totalsets - 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   652
	} else if (loaded == 0 || totalsets == 2) { // empty
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   653
		spriteset = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   654
	} else { // something inbetween
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   655
		spriteset = loaded * (totalsets - 2) / 100 + 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   656
		// correct possible rounding errors
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   657
		if (!spriteset)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   658
			spriteset = 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   659
		else if (spriteset == totalsets - 1)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   660
			spriteset--;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   661
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   662
2489
88827d8ae517 (svn r3015) -NewGRF: Add a new sprite group result type, and support loading of callbacks results within random and "real" groups. (Not fully supported yet.)
peter1138
parents: 2488
diff changeset
   663
	r = (in_motion ? rsg->loaded[spriteset]->g.result.result : rsg->loading[spriteset]->g.result.result) + direction;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   664
	return r;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   665
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   666
1883
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   667
/**
1908
bc48a30aba72 (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1904
diff changeset
   668
 * Check if a wagon is currently using a wagon override
bc48a30aba72 (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1904
diff changeset
   669
 * @param v The wagon to check
bc48a30aba72 (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1904
diff changeset
   670
 * @return true if it is using an override, false otherwise
bc48a30aba72 (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1904
diff changeset
   671
 */
2548
97ada3bd2702 (svn r3077) static, const, bracing, indentation, 0 -> '\0'/NULL, typos in comments, excess empty lines, minor other changes
tron
parents: 2530
diff changeset
   672
bool UsesWagonOverride(const Vehicle* v)
97ada3bd2702 (svn r3077) static, const, bracing, indentation, 0 -> '\0'/NULL, typos in comments, excess empty lines, minor other changes
tron
parents: 2530
diff changeset
   673
{
1908
bc48a30aba72 (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1904
diff changeset
   674
	assert(v->type == VEH_Train);
2242
27fa4807cd61 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   675
	return GetWagonOverrideSpriteSet(v->engine_type, v->u.rail.first_engine) != NULL;
1908
bc48a30aba72 (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1904
diff changeset
   676
}
bc48a30aba72 (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1904
diff changeset
   677
bc48a30aba72 (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1904
diff changeset
   678
/**
1883
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   679
 * Evaluates a newgrf callback
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   680
 * @param callback_info info about which callback to evaluate
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   681
 *  (bit 0-7)  = CallBack id of the callback to use, see CallBackId enum
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   682
 *  (bit 8-15) = Other info some callbacks need to have, callback specific, see CallBackId enum, not used yet
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   683
 * @param engine Engine type of the vehicle to evaluate the callback for
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   684
 * @param vehicle The vehicle to evaluate the callback for, NULL if it doesnt exist (yet)
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   685
 * @return The value the callback returned, or CALLBACK_FAILED if it failed
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   686
 */
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
   687
uint16 GetCallBackResult(uint16 callback_info, EngineID engine, const Vehicle *v)
1883
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   688
{
2242
27fa4807cd61 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   689
	const SpriteGroup *group;
1883
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   690
	byte cargo = GC_DEFAULT;
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   691
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   692
	if (v != NULL)
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   693
		cargo = _global_cargo_id[_opt.landscape][v->cargo_type];
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   694
2488
cbe3d6b60c27 (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   695
	group = engine_custom_sprites[engine][cargo];
1904
cf8a8adc33c9 (svn r2410) - Fix: [newgrf] When resolving callbacks, dont ignore wagon overrides.
hackykid
parents: 1891
diff changeset
   696
cf8a8adc33c9 (svn r2410) - Fix: [newgrf] When resolving callbacks, dont ignore wagon overrides.
hackykid
parents: 1891
diff changeset
   697
	if (v != NULL && v->type == VEH_Train) {
2242
27fa4807cd61 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   698
		const SpriteGroup *overset = GetWagonOverrideSpriteSet(engine, v->u.rail.first_engine);
1904
cf8a8adc33c9 (svn r2410) - Fix: [newgrf] When resolving callbacks, dont ignore wagon overrides.
hackykid
parents: 1891
diff changeset
   699
cf8a8adc33c9 (svn r2410) - Fix: [newgrf] When resolving callbacks, dont ignore wagon overrides.
hackykid
parents: 1891
diff changeset
   700
		if (overset != NULL) group = overset;
cf8a8adc33c9 (svn r2410) - Fix: [newgrf] When resolving callbacks, dont ignore wagon overrides.
hackykid
parents: 1891
diff changeset
   701
	}
cf8a8adc33c9 (svn r2410) - Fix: [newgrf] When resolving callbacks, dont ignore wagon overrides.
hackykid
parents: 1891
diff changeset
   702
1883
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   703
	group = ResolveVehicleSpriteGroup(group, v, callback_info, (resolve_callback) ResolveVehicleSpriteGroup);
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   704
2488
cbe3d6b60c27 (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   705
	if (group == NULL && cargo != GC_DEFAULT) {
1883
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   706
		// This group is empty but perhaps there'll be a default one.
2488
cbe3d6b60c27 (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   707
		group = ResolveVehicleSpriteGroup(engine_custom_sprites[engine][GC_DEFAULT], v, callback_info,
1883
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   708
		                                (resolve_callback) ResolveVehicleSpriteGroup);
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   709
	}
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   710
2488
cbe3d6b60c27 (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   711
	if (group == NULL || group->type != SGT_CALLBACK)
1883
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   712
		return CALLBACK_FAILED;
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   713
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   714
	return group->g.callback.result;
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   715
}
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   716
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   717
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   718
445
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   719
// Global variables are evil, yes, but we would end up with horribly overblown
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   720
// calling convention otherwise and this should be 100% reentrant.
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   721
static byte _vsg_random_triggers;
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   722
static byte _vsg_bits_to_reseed;
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   723
2242
27fa4807cd61 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   724
static const SpriteGroup *TriggerVehicleSpriteGroup(const SpriteGroup *spritegroup,
1883
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   725
	Vehicle *veh, uint16 callback_info, resolve_callback resolve_func)
445
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   726
{
2488
cbe3d6b60c27 (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   727
	if (spritegroup == NULL)
cbe3d6b60c27 (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   728
		return NULL;
cbe3d6b60c27 (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   729
1477
2174a73b11c9 (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   730
	if (spritegroup->type == SGT_RANDOMIZED) {
2174a73b11c9 (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   731
		_vsg_bits_to_reseed |= RandomizedSpriteGroupTriggeredBits(
2174a73b11c9 (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   732
			&spritegroup->g.random,
2174a73b11c9 (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   733
			_vsg_random_triggers,
2174a73b11c9 (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   734
			&veh->waiting_triggers
2174a73b11c9 (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   735
		);
2174a73b11c9 (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   736
	}
445
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   737
1883
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   738
	return ResolveVehicleSpriteGroup(spritegroup, veh, callback_info, resolve_func);
445
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   739
}
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   740
1477
2174a73b11c9 (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   741
static void DoTriggerVehicle(Vehicle *veh, VehicleTrigger trigger, byte base_random_bits, bool first)
445
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   742
{
2242
27fa4807cd61 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   743
	const SpriteGroup *group;
27fa4807cd61 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   744
	const RealSpriteGroup *rsg;
445
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   745
	byte new_random_bits;
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   746
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   747
	_vsg_random_triggers = trigger;
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   748
	_vsg_bits_to_reseed = 0;
1883
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   749
	group = TriggerVehicleSpriteGroup(GetVehicleSpriteGroup(veh->engine_type, veh), veh, 0,
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   750
	                                  (resolve_callback) TriggerVehicleSpriteGroup);
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   751
2488
cbe3d6b60c27 (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   752
	if (group == NULL && veh->cargo_type != GC_DEFAULT) {
445
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   753
		// This group turned out to be empty but perhaps there'll be a default one.
2488
cbe3d6b60c27 (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   754
		group = TriggerVehicleSpriteGroup(engine_custom_sprites[veh->engine_type][GC_DEFAULT], veh, 0,
1883
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   755
		                                  (resolve_callback) TriggerVehicleSpriteGroup);
445
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   756
	}
1883
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   757
2488
cbe3d6b60c27 (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   758
	if (group == NULL)
cbe3d6b60c27 (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   759
		return;
cbe3d6b60c27 (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   760
1883
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   761
	assert(group->type == SGT_REAL);
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   762
	rsg = &group->g.real;
3b02000cfbe0 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   763
542
2f35c4770b06 (svn r939) -Fix: Fixed compiler errors
truelight
parents: 507
diff changeset
   764
	new_random_bits = Random();
445
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   765
	veh->random_bits &= ~_vsg_bits_to_reseed;
542
2f35c4770b06 (svn r939) -Fix: Fixed compiler errors
truelight
parents: 507
diff changeset
   766
	veh->random_bits |= (first ? new_random_bits : base_random_bits) & _vsg_bits_to_reseed;
445
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   767
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   768
	switch (trigger) {
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   769
		case VEHICLE_TRIGGER_NEW_CARGO:
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   770
			/* All vehicles in chain get ANY_NEW_CARGO trigger now.
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   771
			 * So we call it for the first one and they will recurse. */
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   772
			/* Indexing part of vehicle random bits needs to be
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   773
			 * same for all triggered vehicles in the chain (to get
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   774
			 * all the random-cargo wagons carry the same cargo,
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   775
			 * i.e.), so we give them all the NEW_CARGO triggered
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   776
			 * vehicle's portion of random bits. */
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   777
			assert(first);
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   778
			DoTriggerVehicle(GetFirstVehicleInChain(veh), VEHICLE_TRIGGER_ANY_NEW_CARGO, new_random_bits, false);
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   779
			break;
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   780
		case VEHICLE_TRIGGER_DEPOT:
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   781
			/* We now trigger the next vehicle in chain recursively.
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   782
			 * The random bits portions may be different for each
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   783
			 * vehicle in chain. */
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   784
			if (veh->next != NULL)
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   785
				DoTriggerVehicle(veh->next, trigger, 0, true);
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   786
			break;
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   787
		case VEHICLE_TRIGGER_EMPTY:
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   788
			/* We now trigger the next vehicle in chain
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   789
			 * recursively.  The random bits portions must be same
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   790
			 * for each vehicle in chain, so we give them all
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   791
			 * first chained vehicle's portion of random bits. */
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   792
			if (veh->next != NULL)
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   793
				DoTriggerVehicle(veh->next, trigger, first ? new_random_bits : base_random_bits, false);
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   794
			break;
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   795
		case VEHICLE_TRIGGER_ANY_NEW_CARGO:
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   796
			/* Now pass the trigger recursively to the next vehicle
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   797
			 * in chain. */
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   798
			assert(!first);
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   799
			if (veh->next != NULL)
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   800
				DoTriggerVehicle(veh->next, VEHICLE_TRIGGER_ANY_NEW_CARGO, base_random_bits, false);
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   801
			break;
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   802
	}
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   803
}
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   804
1477
2174a73b11c9 (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   805
void TriggerVehicle(Vehicle *veh, VehicleTrigger trigger)
445
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   806
{
2589
28c7b9b212af (svn r3126) Autoreplace: Only the first vehicle in a chain should be marked as entering a depot, so call moved out of DoVehicleTrigger loop.
peter1138
parents: 2574
diff changeset
   807
	if (trigger == VEHICLE_TRIGGER_DEPOT) {
28c7b9b212af (svn r3126) Autoreplace: Only the first vehicle in a chain should be marked as entering a depot, so call moved out of DoVehicleTrigger loop.
peter1138
parents: 2574
diff changeset
   808
		// store that the vehicle entered a depot this tick
28c7b9b212af (svn r3126) Autoreplace: Only the first vehicle in a chain should be marked as entering a depot, so call moved out of DoVehicleTrigger loop.
peter1138
parents: 2574
diff changeset
   809
		VehicleEnteredDepotThisTick(veh);
28c7b9b212af (svn r3126) Autoreplace: Only the first vehicle in a chain should be marked as entering a depot, so call moved out of DoVehicleTrigger loop.
peter1138
parents: 2574
diff changeset
   810
	}
28c7b9b212af (svn r3126) Autoreplace: Only the first vehicle in a chain should be marked as entering a depot, so call moved out of DoVehicleTrigger loop.
peter1138
parents: 2574
diff changeset
   811
445
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   812
	DoTriggerVehicle(veh, trigger, 0, true);
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   813
}
0e3fa3da3899 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   814
1474
d83520edcda6 (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
   815
static char *_engine_custom_names[TOTAL_NUM_ENGINES];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   816
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
   817
void SetCustomEngineName(EngineID engine, const char *name)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   818
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   819
	_engine_custom_names[engine] = strdup(name);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   820
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   821
2769
acea0abd2195 (svn r3316) - NewGRF: Unload engine names before loading grf files. This fixes names in climates where the engines don't get loaded. Renamed function to reflect its purpose.
peter1138
parents: 2763
diff changeset
   822
void UnloadCustomEngineNames(void)
1474
d83520edcda6 (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
   823
{
d83520edcda6 (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
   824
	char **i;
d83520edcda6 (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
   825
	for (i = _engine_custom_names; i != endof(_engine_custom_names); i++) {
d83520edcda6 (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
   826
		free(*i);
d83520edcda6 (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
   827
		*i = NULL;
d83520edcda6 (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
   828
	}
d83520edcda6 (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
   829
}
d83520edcda6 (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
   830
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
   831
StringID GetCustomEngineName(EngineID engine)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   832
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   833
	if (!_engine_custom_names[engine])
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   834
		return _engine_name_strings[engine];
2201
3300b6ce8bce (svn r2717) Move _userstring to strings.[ch]
tron
parents: 2186
diff changeset
   835
	ttd_strlcpy(_userstring, _engine_custom_names[engine], lengthof(_userstring));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   836
	return STR_SPEC_USERSTRING;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   837
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   838
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   839
2817
58dcead3f545 (svn r3365) Staticise 36 functions
tron
parents: 2769
diff changeset
   840
static void AcceptEnginePreview(Engine *e, PlayerID player)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   841
{
1962
51ee4f459268 (svn r2468) -Codechange: Got rid of DEREF_PLAYER and replaced it by GetPlayer
celestar
parents: 1926
diff changeset
   842
	Player *p = GetPlayer(player);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   843
2147
42efdf5020d1 (svn r2657) -Codechange: The available railtypes per player are now a bitmask, so
celestar
parents: 2140
diff changeset
   844
	assert(e->railtype < RAILTYPE_END);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   845
	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
   846
	SETBIT(p->avail_railtypes, e->railtype);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   847
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   848
	e->preview_player = 0xFF;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   849
	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
   850
	InvalidateWindowClasses(WC_REPLACE_VEHICLE);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   851
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   852
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
   853
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
   854
{
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
   855
	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
   856
	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
   857
	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
   858
	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
   859
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
   860
	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
   861
		best_hist = -1;
2469
1bfbb0dc0b75 (svn r2995) Replace 0xFF/0xFFFF with CT_INVALID/OWNER_SPECTATOR/INVALID_STATION where appropriate
tron
parents: 2242
diff changeset
   862
		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
   863
		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
   864
			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
   865
					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
   866
				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
   867
				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
   868
			}
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
   869
		}
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
   870
2469
1bfbb0dc0b75 (svn r2995) Replace 0xFF/0xFFFF with CT_INVALID/OWNER_SPECTATOR/INVALID_STATION where appropriate
tron
parents: 2242
diff changeset
   871
		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
   872
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
   873
		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
   874
	} 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
   875
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
   876
	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
   877
}
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
   878
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
   879
void EnginesDailyLoop(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   880
{
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
   881
	EngineID i;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   882
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
   883
	if (_cur_year >= 130) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   884
2242
27fa4807cd61 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   885
	for (i = 0; i != lengthof(_engines); i++) {
27fa4807cd61 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   886
		Engine* e = &_engines[i];
27fa4807cd61 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   887
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   888
		if (e->flags & ENGINE_INTRODUCING) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   889
			if (e->flags & ENGINE_PREVIEWING) {
740
c1d06749846b (svn r1196) -Fix: Preview Vehicle had a small glitch. Fixed now.
truelight
parents: 555
diff changeset
   890
				if (e->preview_player != 0xFF && !--e->preview_wait) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   891
					e->flags &= ~ENGINE_PREVIEWING;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   892
					DeleteWindowById(WC_ENGINE_PREVIEW, i);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   893
					e->preview_player++;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 114
diff changeset
   894
				}
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
   895
 			} else if (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
   896
				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
   897
2469
1bfbb0dc0b75 (svn r2995) Replace 0xFF/0xFFFF with CT_INVALID/OWNER_SPECTATOR/INVALID_STATION where appropriate
tron
parents: 2242
diff changeset
   898
				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
   899
					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
   900
					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
   901
				}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 114
diff changeset
   902
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   903
				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
   904
					/* XXX - TTDBUG: TTD has a bug here ???? */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   905
					AcceptEnginePreview(e, best_player);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   906
				} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   907
					e->flags |= ENGINE_PREVIEWING;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   908
					e->preview_wait = 20;
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2611
diff changeset
   909
					if (IS_INTERACTIVE_PLAYER(best_player)) ShowEnginePreviewWindow(i);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   910
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   911
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   912
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   913
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   914
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   915
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
   916
/** 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
   917
 * changes while you are waiting to accept the offer? Then it becomes invalid
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
   918
 * @param x,y 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
   919
 * @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
   920
 * @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
   921
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   922
int32 CmdWantEnginePreview(int x, int y, uint32 flags, uint32 p1, uint32 p2)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   923
{
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
   924
	Engine *e;
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
   925
	if (!IsEngineIndex(p1)) 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
   926
1926
68d60188a22f (svn r2432) Use GetEngine() instead of DEREF_ENGINE() or even _engines[]
tron
parents: 1908
diff changeset
   927
	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
   928
	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
   929
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
   930
	if (flags & DC_EXEC)
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
   931
		AcceptEnginePreview(e, _current_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
   932
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   933
	return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   934
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   935
257
8d83db6716ea (svn r262) Fix: [ 1028234 ] Monorail and MagLev infrastructure not available in 1920s any more
dominik
parents: 220
diff changeset
   936
// 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
   937
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
   938
{
964
4bd472539a4e (svn r1456) Simplify isWagon(), make it static and give it a canonical name (IsWagon)
tron
parents: 938
diff changeset
   939
	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
   940
}
8d83db6716ea (svn r262) Fix: [ 1028234 ] Monorail and MagLev infrastructure not available in 1920s any more
dominik
parents: 220
diff changeset
   941
410
8de2aaf20800 (svn r607) -Patch: [ 985102 ] static cleanup
tron
parents: 408
diff changeset
   942
static void NewVehicleAvailable(Engine *e)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   943
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   944
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   945
	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
   946
	EngineID index = e - _engines;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   947
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   948
	// 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
   949
	// prevent that player from getting future intro periods for a while.
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2611
diff changeset
   950
	if (e->flags & ENGINE_INTRODUCING) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   951
		FOR_ALL_PLAYERS(p) {
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   952
			uint block_preview = p->block_preview;
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   953
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2611
diff changeset
   954
			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
   955
919
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   956
			/* 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
   957
			p->block_preview = 20;
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   958
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   959
			FOR_ALL_VEHICLES(v) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   960
				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
   961
						(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
   962
					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
   963
						/* 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
   964
						p->block_preview = block_preview;
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   965
						break;
b0d6c7642f99 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   966
					}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   967
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   968
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   969
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   970
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   971
114
6a2af0c2d0db (svn r115) Fix: monorail/maglev became available around 1927
dominik
parents: 0
diff changeset
   972
	e->flags = (e->flags & ~ENGINE_INTRODUCING) | ENGINE_AVAILABLE;
6a2af0c2d0db (svn r115) Fix: monorail/maglev became available around 1927
dominik
parents: 0
diff changeset
   973
	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
   974
	InvalidateWindowClasses(WC_REPLACE_VEHICLE);
114
6a2af0c2d0db (svn r115) Fix: monorail/maglev became available around 1927
dominik
parents: 0
diff changeset
   975
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   976
	// Now available for all players
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   977
	e->player_avail = (byte)-1;
114
6a2af0c2d0db (svn r115) Fix: monorail/maglev became available around 1927
dominik
parents: 0
diff changeset
   978
6a2af0c2d0db (svn r115) Fix: monorail/maglev became available around 1927
dominik
parents: 0
diff changeset
   979
	// Do not introduce new rail wagons
2639
8a7342eb3a78 (svn r3181) -Bracing
tron
parents: 2611
diff changeset
   980
	if (IsWagon(index)) return;
114
6a2af0c2d0db (svn r115) Fix: monorail/maglev became available around 1927
dominik
parents: 0
diff changeset
   981
6a2af0c2d0db (svn r115) Fix: monorail/maglev became available around 1927
dominik
parents: 0
diff changeset
   982
	// make maglev / monorail available
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   983
	FOR_ALL_PLAYERS(p) {
2147
42efdf5020d1 (svn r2657) -Codechange: The available railtypes per player are now a bitmask, so
celestar
parents: 2140
diff changeset
   984
		if (p->is_active) {
42efdf5020d1 (svn r2657) -Codechange: The available railtypes per player are now a bitmask, so
celestar
parents: 2140
diff changeset
   985
			assert(e->railtype < RAILTYPE_END);
42efdf5020d1 (svn r2657) -Codechange: The available railtypes per player are now a bitmask, so
celestar
parents: 2140
diff changeset
   986
			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
   987
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   988
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   989
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
   990
	if (index < NUM_TRAIN_ENGINES) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   991
		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
   992
	} 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
   993
		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
   994
	} 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
   995
		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
   996
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   997
		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
   998
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   999
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1000
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
  1001
void EnginesMonthlyLoop(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1002
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1003
	Engine *e;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1004
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1005
	if (_cur_year < 130) {
2242
27fa4807cd61 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
  1006
		for (e = _engines; e != endof(_engines); e++) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1007
			// Age the vehicle
2242
27fa4807cd61 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
  1008
			if (e->flags & ENGINE_AVAILABLE && e->age != 0xFFFF) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1009
				e->age++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1010
				CalcEngineReliability(e);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1011
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1012
9922
3d7645168508 (svn r3952) - 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. Backport of r3565 from trunk
Darkvater
parents: 2875
diff changeset
  1013
			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
  1014
				// Introduce it to all players
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1015
				NewVehicleAvailable(e);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1016
			} 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
  1017
				// 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
  1018
				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
  1019
8d83db6716ea (svn r262) Fix: [ 1028234 ] Monorail and MagLev infrastructure not available in 1920s any more
dominik
parents: 220
diff changeset
  1020
				// 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
  1021
				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
  1022
					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
  1023
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1024
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1025
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1026
	AdjustAvailAircraft();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1027
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1028
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
  1029
/** Rename an engine.
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
  1030
 * @param x,y 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
  1031
 * @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
  1032
 * @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
  1033
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1034
int32 CmdRenameEngine(int x, int y, uint32 flags, uint32 p1, uint32 p2)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1035
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1036
	StringID str;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1037
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
  1038
	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
  1039
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
  1040
	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
  1041
	if (str == 0) return CMD_ERROR;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 114
diff changeset
  1042
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1043
	if (flags & DC_EXEC) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1044
		StringID old_str = _engine_name_strings[p1];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1045
		_engine_name_strings[p1] = str;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1046
		DeleteName(old_str);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1047
		_vehicle_design_names |= 3;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1048
		MarkWholeScreenDirty();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1049
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1050
		DeleteName(str);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1051
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1052
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1053
	return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1054
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1055
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1056
2848
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1057
/*
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1058
 * returns true if an engine is valid, of the specified type, and buildable by
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1059
 * the current player, false otherwise
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1060
 *
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1061
 * engine = index of the engine to check
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1062
 * type   = the type the engine should be of (VEH_xxx)
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1063
 */
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1064
bool IsEngineBuildable(uint engine, byte type)
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1065
{
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1066
	const Engine *e;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1067
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1068
	// check if it's an engine that is in the engine array
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1069
	if (!IsEngineIndex(engine)) return false;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1070
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1071
	e = GetEngine(engine);
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1072
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1073
	// check if it's an engine of specified type
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1074
	if (e->type != type) return false;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1075
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1076
	// check if it's available
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1077
	if (!HASBIT(e->player_avail, _current_player)) return false;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1078
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1079
	return true;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1080
}
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1081
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1082
/************************************************************************
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1083
 * Engine Replacement stuff
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1084
 ************************************************************************/
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1085
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1086
static void EngineRenewPoolNewBlock(uint start_item); /* Forward declare for initializer of _engine_renew_pool */
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1087
enum {
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1088
	ENGINE_RENEW_POOL_BLOCK_SIZE_BITS = 3,
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1089
	ENGINE_RENEW_POOL_MAX_BLOCKS      = 8000,
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1090
};
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1091
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1092
MemoryPool _engine_renew_pool = { "EngineRe", ENGINE_RENEW_POOL_MAX_BLOCKS, ENGINE_RENEW_POOL_BLOCK_SIZE_BITS, sizeof(EngineRenew), &EngineRenewPoolNewBlock, 0, 0, NULL };
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1093
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1094
static inline uint16 GetEngineRenewPoolSize(void)
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1095
{
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1096
	return _engine_renew_pool.total_items;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1097
}
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1098
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1099
#define FOR_ALL_ENGINE_RENEWS_FROM(er, start) for (er = GetEngineRenew(start); er != NULL; er = (er->index + 1 < GetEngineRenewPoolSize()) ? GetEngineRenew(er->index + 1) : NULL)
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1100
#define FOR_ALL_ENGINE_RENEWS(er) FOR_ALL_ENGINE_RENEWS_FROM(er, 0)
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1101
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1102
static void EngineRenewPoolNewBlock(uint start_item)
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1103
{
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1104
	EngineRenew *er;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1105
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1106
	FOR_ALL_ENGINE_RENEWS_FROM(er, start_item) {
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1107
		er->index = start_item++;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1108
		er->from = INVALID_ENGINE;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1109
	}
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1110
}
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1111
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1112
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1113
static EngineRenew *AllocateEngineRenew(void)
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1114
{
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1115
	EngineRenew *er;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1116
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1117
	FOR_ALL_ENGINE_RENEWS(er) {
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1118
		if (er->from == INVALID_ENGINE) {
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1119
			er->to = INVALID_ENGINE;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1120
			er->next = NULL;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1121
			return er;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1122
		}
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1123
	}
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1124
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1125
	/* Check if we can add a block to the pool */
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1126
	if (AddBlockToPool(&_engine_renew_pool)) return AllocateEngineRenew();
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1127
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1128
	return NULL;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1129
}
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1130
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1131
/**
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1132
 * Retrieves the EngineRenew that specifies the replacement of the given
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1133
 * engine type from the given renewlist */
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1134
static EngineRenew *GetEngineReplacement(EngineRenewList erl, EngineID engine)
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1135
{
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1136
	EngineRenew* er = (EngineRenew*)erl; /* Fetch first element */
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1137
	while (er) {
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1138
		if (er->from == engine) return er;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1139
		er = er->next;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1140
	}
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1141
	return NULL;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1142
}
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1143
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1144
void RemoveAllEngineReplacement(EngineRenewList* erl)
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1145
{
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1146
	EngineRenew* er = (EngineRenew*)(*erl); /* Fetch first element */
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1147
	while (er) {
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1148
		er->from = INVALID_ENGINE; /* "Deallocate" all elements */
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1149
		er = er->next;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1150
	}
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1151
	*erl = NULL; /* Empty list */
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1152
}
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1153
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1154
EngineID EngineReplacement(EngineRenewList erl, EngineID engine)
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1155
{
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1156
	const EngineRenew *er = GetEngineReplacement(erl, engine);
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1157
	return er == NULL ? INVALID_ENGINE : er->to;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1158
}
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1159
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1160
int32 AddEngineReplacement(EngineRenewList* erl, EngineID old_engine, EngineID new_engine, uint32 flags)
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1161
{
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1162
	EngineRenew *er;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1163
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1164
	// Check if the old vehicle is already in the list
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1165
	er = GetEngineReplacement(*erl, old_engine);
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1166
	if (er != NULL) {
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1167
		if (flags & DC_EXEC) er->to = new_engine;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1168
		return 0;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1169
	}
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1170
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1171
	er = AllocateEngineRenew();
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1172
	if (er == NULL) return CMD_ERROR;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1173
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1174
	if (flags & DC_EXEC) {
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1175
		er->from = old_engine;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1176
		er->to = new_engine;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1177
		er->next = (EngineRenew*)(*erl); /* Resolve the first element in the list */
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1178
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1179
		*erl = (EngineRenewList)er; /* Insert before the first element */
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1180
	}
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1181
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1182
	return 0;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1183
}
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1184
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1185
int32 RemoveEngineReplacement(EngineRenewList* erl, EngineID engine, uint32 flags)
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1186
{
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1187
	EngineRenew* er = (EngineRenew*)(*erl); /* Start at the first element */
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1188
	EngineRenew* prev = NULL;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1189
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1190
	while (er)
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1191
	{
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1192
		if (er->from == engine) {
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1193
			if (flags & DC_EXEC) {
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1194
				if (prev == NULL) { /* First element */
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1195
					(*erl) = (EngineRenewList)er->next; /* The second becomes the new first element */
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1196
				} else {
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1197
					prev->next = er->next; /* Cut this element out */
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1198
				}
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1199
				er->from = INVALID_ENGINE; /* Deallocate */
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1200
			}
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1201
			return 0;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1202
		}
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1203
		prev = er;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1204
		er = er->next; /* Look at next element */
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1205
	}
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1206
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1207
	return CMD_ERROR; /* Not found? */
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1208
}
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1209
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1210
static const SaveLoad _engine_renew_desc[] = {
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1211
	SLE_VAR(EngineRenew, from, SLE_UINT16),
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1212
	SLE_VAR(EngineRenew, to,   SLE_UINT16),
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1213
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1214
	SLE_REF(EngineRenew, next, REF_ENGINE_RENEWS),
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1215
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1216
	SLE_END()
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1217
};
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1218
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1219
static void Save_ERNW(void)
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1220
{
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1221
	EngineRenew *er;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1222
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1223
	FOR_ALL_ENGINE_RENEWS(er) {
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1224
		if (er->from != INVALID_ENGINE) {
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1225
			SlSetArrayIndex(er->index);
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1226
			SlObject(er, _engine_renew_desc);
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1227
		}
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1228
	}
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1229
}
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1230
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1231
static void Load_ERNW(void)
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1232
{
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1233
	int index;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1234
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1235
	while ((index = SlIterateArray()) != -1) {
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1236
		EngineRenew *er;
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1237
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1238
		if (!AddBlockIfNeeded(&_engine_renew_pool, index))
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1239
			error("EngineRenews: failed loading savegame: too many EngineRenews");
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1240
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1241
		er = GetEngineRenew(index);
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1242
		SlObject(er, _engine_renew_desc);
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1243
	}
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1244
}
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1245
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
  1246
static const SaveLoad _engine_desc[] = {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1247
	SLE_VAR(Engine,intro_date,						SLE_UINT16),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1248
	SLE_VAR(Engine,age,										SLE_UINT16),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1249
	SLE_VAR(Engine,reliability,						SLE_UINT16),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1250
	SLE_VAR(Engine,reliability_spd_dec,		SLE_UINT16),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1251
	SLE_VAR(Engine,reliability_start,			SLE_UINT16),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1252
	SLE_VAR(Engine,reliability_max,				SLE_UINT16),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1253
	SLE_VAR(Engine,reliability_final,			SLE_UINT16),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1254
	SLE_VAR(Engine,duration_phase_1,			SLE_UINT16),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1255
	SLE_VAR(Engine,duration_phase_2,			SLE_UINT16),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1256
	SLE_VAR(Engine,duration_phase_3,			SLE_UINT16),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1257
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1258
	SLE_VAR(Engine,lifelength,						SLE_UINT8),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1259
	SLE_VAR(Engine,flags,									SLE_UINT8),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1260
	SLE_VAR(Engine,preview_player,				SLE_UINT8),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1261
	SLE_VAR(Engine,preview_wait,					SLE_UINT8),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1262
	SLE_VAR(Engine,railtype,							SLE_UINT8),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1263
	SLE_VAR(Engine,player_avail,					SLE_UINT8),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1264
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1265
	// reserve extra space in savegame here. (currently 16 bytes)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1266
	SLE_CONDARR(NullStruct,null,SLE_FILE_U64 | SLE_VAR_NULL, 2, 2, 255),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1267
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1268
	SLE_END()
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1269
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1270
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
  1271
static void Save_ENGN(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1272
{
2242
27fa4807cd61 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
  1273
	uint i;
27fa4807cd61 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
  1274
27fa4807cd61 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
  1275
	for (i = 0; i != lengthof(_engines); i++) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1276
		SlSetArrayIndex(i);
2242
27fa4807cd61 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
  1277
		SlObject(&_engines[i], _engine_desc);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1278
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1279
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1280
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
  1281
static void Load_ENGN(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1282
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1283
	int index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1284
	while ((index = SlIterateArray()) != -1) {
1926
68d60188a22f (svn r2432) Use GetEngine() instead of DEREF_ENGINE() or even _engines[]
tron
parents: 1908
diff changeset
  1285
		SlObject(GetEngine(index), _engine_desc);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1286
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1287
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1288
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
  1289
static void LoadSave_ENGS(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1290
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1291
	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
  1292
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1293
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1294
const ChunkHandler _engine_chunk_handlers[] = {
2848
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1295
	{ 'ENGN', Save_ENGN,     Load_ENGN,     CH_ARRAY          },
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1296
	{ 'ENGS', LoadSave_ENGS, LoadSave_ENGS, CH_RIFF           },
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1297
	{ '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
  1298
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1299
2848
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1300
void InitializeEngines(void)
1197
4322cf8d6ae7 (svn r1701) Style police ^^
tron
parents: 1196
diff changeset
  1301
{
2848
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1302
	/* Clean the engine renew pool and create 1 block in it */
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1303
	CleanPool(&_engine_renew_pool);
cb4b1cc0bf65 (svn r3396) - Autoreplace changes:
peter1138
parents: 2830
diff changeset
  1304
	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
  1305
}