engine.c
author tron
Sat, 30 Jul 2005 09:29:20 +0000
changeset 2238 33361a216301
parent 2204 c7ce008b5775
child 2242 512eae2cf006
permissions -rw-r--r--
(svn r2758) Add the AB() macro to add a value to a bit range and use it in a few places, also make use of GB and SB nearby
2186
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     1
/* $Id$ */
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2163
diff changeset
     2
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     3
#include "stdafx.h"
1891
862800791170 (svn r2397) - CodeChange: rename all "ttd" files to "openttd" files.
Darkvater
parents: 1883
diff changeset
     4
#include "openttd.h"
1299
39c06aba09aa (svn r1803) Move debugging stuff into files of it's own
tron
parents: 1197
diff changeset
     5
#include "debug.h"
2163
b17b313113a0 (svn r2673) Include functions.h directly, not globally via openttd.h
tron
parents: 2159
diff changeset
     6
#include "functions.h"
2201
f240b3c7e2ec (svn r2717) Move _userstring to strings.[ch]
tron
parents: 2186
diff changeset
     7
#include "string.h"
f240b3c7e2ec (svn r2717) Move _userstring to strings.[ch]
tron
parents: 2186
diff changeset
     8
#include "strings.h"
507
04b5403aaf6b (svn r815) Include strings.h only in the files which need it.
tron
parents: 485
diff changeset
     9
#include "table/strings.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    10
#include "engine.h"
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    11
#include "table/engines.h"
1009
75140dc68759 (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
415546028e8d (svn r602) -newgrf: Move DrawTileSeqStruct & co and struct SpriteGroup to sprite.h (pasky)
darkvater
parents: 369
diff changeset
    18
#include "sprite.h"
2159
f6284cf5fab0 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2147
diff changeset
    19
#include "variables.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    20
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    21
enum {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    22
	ENGINE_AVAILABLE = 1,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    23
	ENGINE_INTRODUCING = 2,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    24
	ENGINE_PREVIEWING = 4,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    25
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    26
1802
da61740cc1e7 (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
    27
/** TRANSLATE FROM LOCAL CARGO TO GLOBAL CARGO ID'S.
da61740cc1e7 (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
    28
 * This maps the per-landscape cargo ID's to globally unique cargo ID's usable ie. in
da61740cc1e7 (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
    29
 * the custom GRF  files. It is basically just a transcribed table from TTDPatch's newgrf.txt.
da61740cc1e7 (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
    30
 */
da61740cc1e7 (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
    31
const CargoID _global_cargo_id[NUM_LANDSCAPE][NUM_CARGO] = {
da61740cc1e7 (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
    32
	/* 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},
da61740cc1e7 (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
    33
	/* 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 },
da61740cc1e7 (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
	/* 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 },
da61740cc1e7 (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
	/* 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 },
da61740cc1e7 (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
	/**
da61740cc1e7 (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
	 * - GC_INVALID (255) means that  cargo is not available for that climate
da61740cc1e7 (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
	 * - GC_PAPER_TEMP (27) is paper in  temperate climate in TTDPatch
da61740cc1e7 (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
	 * Following can  be renumbered:
da61740cc1e7 (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
	 * - GC_DEFAULT (29) is the defa ult cargo for the purpose of spritesets
da61740cc1e7 (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
	 * - GC_PURCHASE (30) is the purchase list image (the equivalent of 0xff) for the purpose of spritesets
da61740cc1e7 (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
	 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    43
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    44
1802
da61740cc1e7 (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
/** BEGIN --- TRANSLATE FROM GLOBAL CARGO TO LOCAL CARGO ID'S **/
da61740cc1e7 (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
/** Map global cargo ID's to local-cargo ID's */
da61740cc1e7 (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
const CargoID _local_cargo_id_ctype[NUM_GLOBAL_CID] = {
da61740cc1e7 (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
	CT_PASSENGERS,CT_COAL,   CT_MAIL,        CT_OIL,      CT_LIVESTOCK,CT_GOODS,  CT_GRAIN,      CT_WOOD,         /*  0- 7 */
da61740cc1e7 (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
	CT_IRON_ORE,  CT_STEEL,  CT_VALUABLES,   CT_PAPER,    CT_FOOD,     CT_FRUIT,  CT_COPPER_ORE, CT_WATER,        /*  8-15 */
da61740cc1e7 (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
    50
	CT_RUBBER,    CT_SUGAR,  CT_TOYS,        CT_BATTERIES,CT_CANDY,    CT_TOFFEE, CT_COLA,       CT_COTTON_CANDY, /* 16-23 */
da61740cc1e7 (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
    51
	CT_BUBBLES,   CT_PLASTIC,CT_FIZZY_DRINKS,CT_PAPER     /* unsup. */,CT_HILLY_UNUSED,                           /* 24-28 */
da61740cc1e7 (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
	CT_INVALID,   CT_INVALID                                                                                      /* 29-30 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    53
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    54
1802
da61740cc1e7 (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
#define MC(cargo) (1 << cargo)
da61740cc1e7 (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
/** Bitmasked value where the global cargo ID is available in landscape
da61740cc1e7 (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
 * 0: LT_NORMAL, 1: LT_HILLY, 2: LT_DESERT, 3: LT_CANDY */
da61740cc1e7 (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
const uint32 _landscape_global_cargo_mask[NUM_LANDSCAPE] =
da61740cc1e7 (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
{ /* LT_NORMAL: temperate */
da61740cc1e7 (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
    60
	MC(GC_PASSENGERS)|MC(GC_COAL)|MC(GC_MAIL)|MC(GC_OIL)|MC(GC_LIVESTOCK)|MC(GC_GOODS)|MC(GC_GRAIN)|MC(GC_WOOD)|
da61740cc1e7 (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
    61
	MC(GC_IRON_ORE)|MC(GC_STEEL)|MC(GC_VALUABLES)|MC(GC_FOOD)|MC(GC_UNDEFINED),
da61740cc1e7 (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
	/* LT_HILLY: arctic */
da61740cc1e7 (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
	MC(GC_PASSENGERS)|MC(GC_COAL)|MC(GC_MAIL)|MC(GC_OIL)|MC(GC_LIVESTOCK)|MC(GC_GOODS)|
da61740cc1e7 (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
	MC(GC_GRAIN)|MC(GC_WOOD)|MC(GC_VALUABLES)|MC(GC_PAPER)|MC(GC_FOOD)|MC(GC_UNDEFINED),
da61740cc1e7 (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
	/* LT_DESERT: rainforest/desert */
da61740cc1e7 (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
	MC(GC_PASSENGERS)|MC(GC_MAIL)|MC(GC_OIL)|MC(GC_GOODS)|MC(GC_GRAIN)|MC(GC_WOOD)|
da61740cc1e7 (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_VALUABLES)|MC(GC_FOOD)|MC(GC_FRUIT)|MC(GC_COPPER_ORE)|MC(GC_WATER)|MC(GC_RUBBER),
da61740cc1e7 (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
    68
	/* LT_CANDY: toyland */
da61740cc1e7 (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
	MC(GC_PASSENGERS)|MC(GC_MAIL)|MC(GC_SUGAR)|MC(GC_TOYS)|MC(GC_BATTERIES)|MC(GC_CANDY)|
da61740cc1e7 (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_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
    71
};
1802
da61740cc1e7 (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
/** END   --- TRANSLATE FROM GLOBAL CARGO TO LOCAL CARGO ID'S **/
da61740cc1e7 (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
da61740cc1e7 (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
/** Bitmasked values of what type of cargo is refittable for the given vehicle-type.
da61740cc1e7 (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
 * This coupled with the landscape information (_landscape_global_cargo_mask) gives
da61740cc1e7 (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
 * us exactly what is refittable and what is not */
da61740cc1e7 (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
const uint32 _default_refitmasks[NUM_VEHICLE_TYPES] = {
da61740cc1e7 (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
    78
	/* Trains */
da61740cc1e7 (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
	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)|
da61740cc1e7 (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
	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)|
da61740cc1e7 (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
	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),
da61740cc1e7 (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
	/* Road vehicles (not refittable by default) */
da61740cc1e7 (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
	0,
da61740cc1e7 (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
	/* Ships */
da61740cc1e7 (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
	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)|
da61740cc1e7 (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_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)|
da61740cc1e7 (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_CANDY)|MC(GC_TOFFEE)|MC(GC_COLA)|MC(GC_COTTON_CANDY)|MC(GC_BUBBLES)|MC(GC_PLASTIC)|MC(GC_FIZZY_DRINKS),
da61740cc1e7 (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
	/* Aircraft */
da61740cc1e7 (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
	MC(GC_PASSENGERS)|MC(GC_MAIL)|MC(GC_GOODS)|MC(GC_VALUABLES)|MC(GC_FOOD)|MC(GC_FRUIT)|MC(GC_SUGAR)|MC(GC_TOYS)|
da61740cc1e7 (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
	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),
da61740cc1e7 (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
	/* Special/Disaster */
da61740cc1e7 (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
	0,0
da61740cc1e7 (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
};
da61740cc1e7 (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
#undef MC
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    95
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    96
void ShowEnginePreviewWindow(int engine);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    97
1093
4fdc46eaf423 (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1009
diff changeset
    98
void DeleteCustomEngineNames(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    99
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   100
	uint i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   101
	StringID old;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   102
1474
a26a21fa10ef (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
   103
	for (i = 0; i != TOTAL_NUM_ENGINES; i++) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   104
		old = _engine_name_strings[i];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   105
		_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
   106
		DeleteName(old);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   107
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   108
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   109
	_vehicle_design_names &= ~1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   110
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   111
1093
4fdc46eaf423 (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1009
diff changeset
   112
void LoadCustomEngineNames(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   113
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   114
	// XXX: not done */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   115
	DEBUG(misc, 1) ("LoadCustomEngineNames: not done");
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   116
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   117
1093
4fdc46eaf423 (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1009
diff changeset
   118
static void SetupEngineNames(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   119
{
1474
a26a21fa10ef (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
   120
	StringID *name;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   121
1474
a26a21fa10ef (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
   122
	for (name = _engine_name_strings; name != endof(_engine_name_strings); name++)
a26a21fa10ef (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
   123
		*name = STR_SV_EMPTY;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   124
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   125
	DeleteCustomEngineNames();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   126
	LoadCustomEngineNames();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   127
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   128
1093
4fdc46eaf423 (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1009
diff changeset
   129
static void AdjustAvailAircraft(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   130
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   131
	uint16 date = _date;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   132
	byte avail = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   133
	if (date >= 12784) avail |= 2; // big airport
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   134
	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
   135
	if (date >= 15706) avail |= 4; // enable heliport
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   136
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   137
	if (avail != _avail_aircraft) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   138
		_avail_aircraft = avail;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   139
		InvalidateWindow(WC_BUILD_STATION, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   140
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   141
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   142
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   143
static void CalcEngineReliability(Engine *e)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   144
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   145
	uint age = e->age;
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
	if (age < e->duration_phase_1) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   148
		uint start = e->reliability_start;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   149
		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
   150
	} 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
   151
		e->reliability = e->reliability_max;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   152
	} 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
   153
		uint max = e->reliability_max;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   154
		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
   155
	} else {
292
16c136eaffcb (svn r298) Fix: Engines from other climates do not appear any more when never_expire_vehicles is enabled
dominik
parents: 257
diff changeset
   156
		// time's up for this engine
16c136eaffcb (svn r298) Fix: Engines from other climates do not appear any more when never_expire_vehicles is enabled
dominik
parents: 257
diff changeset
   157
		// make it either available to all players (if never_expire_vehicles is enabled and if it was available earlier)
16c136eaffcb (svn r298) Fix: Engines from other climates do not appear any more when never_expire_vehicles is enabled
dominik
parents: 257
diff changeset
   158
		// or disable this engine completely
16c136eaffcb (svn r298) Fix: Engines from other climates do not appear any more when never_expire_vehicles is enabled
dominik
parents: 257
diff changeset
   159
		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
   160
		e->reliability = e->reliability_final;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   161
	}
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
1093
4fdc46eaf423 (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1009
diff changeset
   164
void AddTypeToEngines(void)
842
efc3546bc313 (svn r1323) Adding autoreplace feature
bjarni
parents: 819
diff changeset
   165
{
efc3546bc313 (svn r1323) Adding autoreplace feature
bjarni
parents: 819
diff changeset
   166
	Engine *e;
efc3546bc313 (svn r1323) Adding autoreplace feature
bjarni
parents: 819
diff changeset
   167
	uint32 counter = 0;
915
d845fe7cf6f2 (svn r1402) Trim trailing whitespace
tron
parents: 842
diff changeset
   168
842
efc3546bc313 (svn r1323) Adding autoreplace feature
bjarni
parents: 819
diff changeset
   169
	for(e=_engines; e != endof(_engines); e++, counter++) {
efc3546bc313 (svn r1323) Adding autoreplace feature
bjarni
parents: 819
diff changeset
   170
efc3546bc313 (svn r1323) Adding autoreplace feature
bjarni
parents: 819
diff changeset
   171
		e->type = VEH_Train;
efc3546bc313 (svn r1323) Adding autoreplace feature
bjarni
parents: 819
diff changeset
   172
		if 	(counter >= ROAD_ENGINES_INDEX) {
efc3546bc313 (svn r1323) Adding autoreplace feature
bjarni
parents: 819
diff changeset
   173
			e->type = VEH_Road;
efc3546bc313 (svn r1323) Adding autoreplace feature
bjarni
parents: 819
diff changeset
   174
			if 	(counter >= SHIP_ENGINES_INDEX) {
efc3546bc313 (svn r1323) Adding autoreplace feature
bjarni
parents: 819
diff changeset
   175
				e->type = VEH_Ship;
efc3546bc313 (svn r1323) Adding autoreplace feature
bjarni
parents: 819
diff changeset
   176
				if 	(counter >= AIRCRAFT_ENGINES_INDEX) {
efc3546bc313 (svn r1323) Adding autoreplace feature
bjarni
parents: 819
diff changeset
   177
					e->type = VEH_Aircraft;
efc3546bc313 (svn r1323) Adding autoreplace feature
bjarni
parents: 819
diff changeset
   178
					if 	(counter >= TOTAL_NUM_ENGINES) {
efc3546bc313 (svn r1323) Adding autoreplace feature
bjarni
parents: 819
diff changeset
   179
						e->type = VEH_Special;
efc3546bc313 (svn r1323) Adding autoreplace feature
bjarni
parents: 819
diff changeset
   180
					}
efc3546bc313 (svn r1323) Adding autoreplace feature
bjarni
parents: 819
diff changeset
   181
				}
efc3546bc313 (svn r1323) Adding autoreplace feature
bjarni
parents: 819
diff changeset
   182
			}
efc3546bc313 (svn r1323) Adding autoreplace feature
bjarni
parents: 819
diff changeset
   183
		}
efc3546bc313 (svn r1323) Adding autoreplace feature
bjarni
parents: 819
diff changeset
   184
	}
efc3546bc313 (svn r1323) Adding autoreplace feature
bjarni
parents: 819
diff changeset
   185
}
efc3546bc313 (svn r1323) Adding autoreplace feature
bjarni
parents: 819
diff changeset
   186
1093
4fdc46eaf423 (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1009
diff changeset
   187
void StartupEngines(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   188
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   189
	Engine *e;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   190
	const EngineInfo *ei;
819
c425b7e22f6a (svn r1290) Added type to typedef struct Engine and filled in the same data as in type in vehicle
bjarni
parents: 740
diff changeset
   191
	uint32 r, counter = 0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   192
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   193
	SetupEngineNames();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   194
842
efc3546bc313 (svn r1323) Adding autoreplace feature
bjarni
parents: 819
diff changeset
   195
	for(e=_engines, ei=_engine_info; e != endof(_engines); e++, ei++, counter++) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   196
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   197
		e->age = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   198
		e->railtype = ei->railtype_climates >> 4;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   199
		e->flags = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   200
		e->player_avail = 0;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 114
diff changeset
   201
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   202
		r = Random();
2140
a04d0142ad65 (svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents: 1988
diff changeset
   203
		e->intro_date = GB(r, 0, 9) + ei->base_intro;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   204
		if (e->intro_date <= _date) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   205
			e->age = (_date - e->intro_date) >> 5;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   206
			e->player_avail = (byte)-1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   207
			e->flags |= ENGINE_AVAILABLE;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   208
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   209
2140
a04d0142ad65 (svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents: 1988
diff changeset
   210
		e->reliability_start = GB(r, 16, 14) + 0x7AE0;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   211
		r = Random();
2140
a04d0142ad65 (svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents: 1988
diff changeset
   212
		e->reliability_max   = GB(r,  0, 14) + 0xBFFF;
a04d0142ad65 (svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents: 1988
diff changeset
   213
		e->reliability_final = GB(r, 16, 14) + 0x3FFF;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   214
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   215
		r = Random();
2140
a04d0142ad65 (svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents: 1988
diff changeset
   216
		e->duration_phase_1 = GB(r, 0, 5) + 7;
a04d0142ad65 (svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents: 1988
diff changeset
   217
		e->duration_phase_2 = GB(r, 5, 4) + ei->base_life * 12 - 96;
a04d0142ad65 (svn r2650) Convert many explicit shifts+ands to extract bits to invocations of GB - should be a bit nicer to read
tron
parents: 1988
diff changeset
   218
		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
   219
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   220
		e->reliability_spd_dec = (ei->unk2&0x7F) << 2;
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
		/* my invented flag for something that is a wagon */
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   223
		if (ei->unk2 & 0x80) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   224
			e->age = 0xFFFF;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   225
		} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   226
			CalcEngineReliability(e);
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   229
		e->lifelength = ei->lifelength + _patches.extend_vehicle_life;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   230
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   231
		// prevent certain engines from ever appearing.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   232
		if (!HASBIT(ei->railtype_climates, _opt.landscape)) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   233
			e->flags |= ENGINE_AVAILABLE;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   234
			e->player_avail = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   235
		}
915
d845fe7cf6f2 (svn r1402) Trim trailing whitespace
tron
parents: 842
diff changeset
   236
819
c425b7e22f6a (svn r1290) Added type to typedef struct Engine and filled in the same data as in type in vehicle
bjarni
parents: 740
diff changeset
   237
		/* This sets up type for the engine
c425b7e22f6a (svn r1290) Added type to typedef struct Engine and filled in the same data as in type in vehicle
bjarni
parents: 740
diff changeset
   238
		   It is needed if you want to ask the engine what type it is
c425b7e22f6a (svn r1290) Added type to typedef struct Engine and filled in the same data as in type in vehicle
bjarni
parents: 740
diff changeset
   239
		   It should hopefully be the same as when you ask a vehicle what it is
c425b7e22f6a (svn r1290) Added type to typedef struct Engine and filled in the same data as in type in vehicle
bjarni
parents: 740
diff changeset
   240
		   but using this, you can ask what type an engine number is
c425b7e22f6a (svn r1290) Added type to typedef struct Engine and filled in the same data as in type in vehicle
bjarni
parents: 740
diff changeset
   241
		   even if it is not a vehicle (yet)*/
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   242
	}
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
	AdjustAvailAircraft();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   245
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   246
1474
a26a21fa10ef (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
   247
uint32 _engine_refit_masks[TOTAL_NUM_ENGINES];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   248
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   249
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   250
// 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
   251
1477
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   252
typedef struct WagonOverride {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   253
	byte *train_id;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   254
	int trains;
1477
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   255
	SpriteGroup group;
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   256
} WagonOverride;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   257
1477
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   258
typedef struct WagonOverrides {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   259
	int overrides_count;
1477
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   260
	WagonOverride *overrides;
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   261
} WagonOverrides;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   262
1477
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   263
static WagonOverrides _engine_wagon_overrides[TOTAL_NUM_ENGINES];
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   264
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   265
void SetWagonOverrideSprites(byte engine, SpriteGroup *group, byte *train_id,
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   266
	int trains)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   267
{
1477
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   268
	WagonOverrides *wos;
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   269
	WagonOverride *wo;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   270
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   271
	wos = &_engine_wagon_overrides[engine];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   272
	wos->overrides_count++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   273
	wos->overrides = realloc(wos->overrides,
1477
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   274
		wos->overrides_count * sizeof(*wos->overrides));
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 114
diff changeset
   275
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   276
	wo = &wos->overrides[wos->overrides_count - 1];
408
48da21eb9ff2 (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
   277
	/* FIXME: If we are replacing an override, release original SpriteGroup
48da21eb9ff2 (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
   278
	 * to prevent leaks. But first we need to refcount the SpriteGroup.
48da21eb9ff2 (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
   279
	 * --pasky */
369
3742b39b6cca (svn r557) -newgrf: Rename all 'superset' tokens to 'group' and some other small renamings (pasky and octo).
darkvater
parents: 292
diff changeset
   280
	wo->group = *group;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   281
	wo->trains = trains;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   282
	wo->train_id = malloc(trains);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   283
	memcpy(wo->train_id, train_id, trains);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   284
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   285
1477
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   286
static SpriteGroup *GetWagonOverrideSpriteSet(byte engine, byte overriding_engine)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   287
{
1477
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   288
	WagonOverrides *wos = &_engine_wagon_overrides[engine];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   289
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   290
408
48da21eb9ff2 (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
   291
	// XXX: This could turn out to be a timesink on profiles. We could
48da21eb9ff2 (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
   292
	// always just dedicate 65535 bytes for an [engine][train] trampoline
48da21eb9ff2 (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
   293
	// for O(1). Or O(logMlogN) and searching binary tree or smt. like
48da21eb9ff2 (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
   294
	// that. --pasky
0
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
	for (i = 0; i < wos->overrides_count; i++) {
1477
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   297
		WagonOverride *wo = &wos->overrides[i];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   298
		int j;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   299
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   300
		for (j = 0; j < wo->trains; j++) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   301
			if (wo->train_id[j] == overriding_engine)
369
3742b39b6cca (svn r557) -newgrf: Rename all 'superset' tokens to 'group' and some other small renamings (pasky and octo).
darkvater
parents: 292
diff changeset
   302
				return &wo->group;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   303
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   304
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   305
	return NULL;
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
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   308
1474
a26a21fa10ef (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
   309
byte _engine_original_sprites[TOTAL_NUM_ENGINES];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   310
// 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
   311
// (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
   312
// 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
   313
// memory. --pasky)
1802
da61740cc1e7 (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
   314
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
   315
1477
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   316
void SetCustomEngineSprites(byte engine, byte cargo, SpriteGroup *group)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   317
{
408
48da21eb9ff2 (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
   318
	/* FIXME: If we are replacing an override, release original SpriteGroup
48da21eb9ff2 (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
   319
	 * to prevent leaks. But first we need to refcount the SpriteGroup.
48da21eb9ff2 (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
   320
	 * --pasky */
369
3742b39b6cca (svn r557) -newgrf: Rename all 'superset' tokens to 'group' and some other small renamings (pasky and octo).
darkvater
parents: 292
diff changeset
   321
	_engine_custom_sprites[engine][cargo] = *group;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   322
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   323
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   324
typedef SpriteGroup *(*resolve_callback)(SpriteGroup *spritegroup,
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   325
	const Vehicle *veh, uint16 callback_info, void *resolve_func); /* XXX data pointer used as function pointer */
445
beafc0fb8f12 (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
   326
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   327
static SpriteGroup* ResolveVehicleSpriteGroup(SpriteGroup *spritegroup,
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   328
	const Vehicle *veh, uint16 callback_info, resolve_callback resolve_func)
426
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   329
{
445
beafc0fb8f12 (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
   330
	//debug("spgt %d", spritegroup->type);
426
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   331
	switch (spritegroup->type) {
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   332
		case SGT_REAL:
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   333
		case SGT_CALLBACK:
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   334
			return spritegroup;
426
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   335
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   336
		case SGT_DETERMINISTIC: {
1477
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   337
			DeterministicSpriteGroup *dsg = &spritegroup->g.determ;
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   338
			SpriteGroup *target;
426
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   339
			int value = -1;
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   340
433
a2cbd6cf3f37 (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
   341
			//debug("[%p] Having fun resolving variable %x", veh, dsg->variable);
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   342
			if (dsg->variable == 0x0C) {
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   343
				/* Callback ID */
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   344
				value = callback_info & 0xFF;
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   345
			} else if ((dsg->variable >> 6) == 0) {
426
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   346
				/* General property */
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   347
				value = GetDeterministicSpriteValue(dsg->variable);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   348
			} else {
433
a2cbd6cf3f37 (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
   349
				/* Vehicle-specific property. */
a2cbd6cf3f37 (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
   350
a2cbd6cf3f37 (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
   351
				if (veh == NULL) {
a2cbd6cf3f37 (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
   352
					/* We are in a purchase list of something,
a2cbd6cf3f37 (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
   353
					 * and we are checking for something undefined.
a2cbd6cf3f37 (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
   354
					 * That means we should get the first target
a2cbd6cf3f37 (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
   355
					 * (NOT the default one). */
a2cbd6cf3f37 (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
   356
					if (dsg->num_ranges > 0) {
a2cbd6cf3f37 (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
   357
						target = &dsg->ranges[0].group;
a2cbd6cf3f37 (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
   358
					} else {
a2cbd6cf3f37 (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
   359
						target = dsg->default_group;
a2cbd6cf3f37 (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
   360
					}
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   361
					return resolve_func(target, NULL, callback_info, resolve_func);
433
a2cbd6cf3f37 (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
   362
				}
a2cbd6cf3f37 (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
   363
426
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   364
				if (dsg->var_scope == VSG_SCOPE_PARENT) {
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   365
					/* First engine in the vehicle chain */
433
a2cbd6cf3f37 (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
   366
					if (veh->type == VEH_Train)
426
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   367
						veh = GetFirstVehicleInChain(veh);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   368
				}
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   369
1855
6c7ade86caa7 (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
   370
				if (dsg->variable == 0x40 || dsg->variable == 0x41) {
433
a2cbd6cf3f37 (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
   371
					if (veh->type == VEH_Train) {
426
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   372
						Vehicle *u = GetFirstVehicleInChain(veh);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   373
						byte chain_before = 0, chain_after = 0;
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   374
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   375
						while (u != veh) {
1855
6c7ade86caa7 (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
   376
							chain_before++;
6c7ade86caa7 (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
   377
							if (dsg->variable == 0x41 && u->engine_type != veh->engine_type)
6c7ade86caa7 (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
   378
								chain_before = 0;
426
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   379
							u = u->next;
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   380
						}
1855
6c7ade86caa7 (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
   381
						while (u->next != NULL && (dsg->variable == 0x40 || u->next->engine_type == veh->engine_type)) {
6c7ade86caa7 (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
   382
							chain_after++;
426
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   383
							u = u->next;
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   384
						};
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   385
1559
c0d5d35b25db (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
   386
						value = chain_before | chain_after << 8
c0d5d35b25db (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
   387
						        | (chain_before + chain_after) << 16;
426
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   388
					} else {
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   389
						value = 1; /* 1 vehicle in the chain */
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   390
					}
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   391
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   392
				} else {
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   393
					// TTDPatch runs on little-endian arch;
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   394
					// Variable is 0x80 + offset in TTD's vehicle structure
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   395
					switch (dsg->variable - 0x80) {
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 543
diff changeset
   396
#define veh_prop(id_, value_) case (id_): value = (value_); break
426
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   397
						veh_prop(0x00, veh->type);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   398
						veh_prop(0x01, veh->subtype);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   399
						veh_prop(0x04, veh->index);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   400
						veh_prop(0x05, veh->index & 0xFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   401
						/* XXX? Is THIS right? */
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 543
diff changeset
   402
						veh_prop(0x0A, PackOrder(&veh->current_order));
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 543
diff changeset
   403
						veh_prop(0x0B, PackOrder(&veh->current_order) & 0xff);
426
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   404
						veh_prop(0x0C, veh->num_orders);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   405
						veh_prop(0x0D, veh->cur_order_index);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   406
						veh_prop(0x10, veh->load_unload_time_rem);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   407
						veh_prop(0x11, veh->load_unload_time_rem & 0xFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   408
						veh_prop(0x12, veh->date_of_last_service);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   409
						veh_prop(0x13, veh->date_of_last_service & 0xFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   410
						veh_prop(0x14, veh->service_interval);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   411
						veh_prop(0x15, veh->service_interval & 0xFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   412
						veh_prop(0x16, veh->last_station_visited);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   413
						veh_prop(0x17, veh->tick_counter);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   414
						veh_prop(0x18, veh->max_speed);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   415
						veh_prop(0x19, veh->max_speed & 0xFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   416
						veh_prop(0x1F, veh->direction);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   417
						veh_prop(0x28, veh->cur_image);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   418
						veh_prop(0x29, veh->cur_image & 0xFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   419
						veh_prop(0x32, veh->vehstatus);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   420
						veh_prop(0x33, veh->vehstatus);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   421
						veh_prop(0x34, veh->cur_speed);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   422
						veh_prop(0x35, veh->cur_speed & 0xFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   423
						veh_prop(0x36, veh->subspeed);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   424
						veh_prop(0x37, veh->acceleration);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   425
						veh_prop(0x39, veh->cargo_type);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   426
						veh_prop(0x3A, veh->cargo_cap);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   427
						veh_prop(0x3B, veh->cargo_cap & 0xFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   428
						veh_prop(0x3C, veh->cargo_count);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   429
						veh_prop(0x3D, veh->cargo_count & 0xFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   430
						veh_prop(0x3E, veh->cargo_source); // Probably useless; so what
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   431
						veh_prop(0x3F, veh->cargo_days);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   432
						veh_prop(0x40, veh->age);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   433
						veh_prop(0x41, veh->age & 0xFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   434
						veh_prop(0x42, veh->max_age);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   435
						veh_prop(0x43, veh->max_age & 0xFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   436
						veh_prop(0x44, veh->build_year);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   437
						veh_prop(0x45, veh->unitnumber);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   438
						veh_prop(0x46, veh->engine_type);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   439
						veh_prop(0x47, veh->engine_type & 0xFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   440
						veh_prop(0x48, veh->spritenum);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   441
						veh_prop(0x49, veh->day_counter);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   442
						veh_prop(0x4A, veh->breakdowns_since_last_service);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   443
						veh_prop(0x4B, veh->breakdown_ctr);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   444
						veh_prop(0x4C, veh->breakdown_delay);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   445
						veh_prop(0x4D, veh->breakdown_chance);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   446
						veh_prop(0x4E, veh->reliability);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   447
						veh_prop(0x4F, veh->reliability & 0xFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   448
						veh_prop(0x50, veh->reliability_spd_dec);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   449
						veh_prop(0x51, veh->reliability_spd_dec & 0xFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   450
						veh_prop(0x52, veh->profit_this_year);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   451
						veh_prop(0x53, veh->profit_this_year & 0xFFFFFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   452
						veh_prop(0x54, veh->profit_this_year & 0xFFFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   453
						veh_prop(0x55, veh->profit_this_year & 0xFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   454
						veh_prop(0x56, veh->profit_last_year);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   455
						veh_prop(0x57, veh->profit_last_year & 0xFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   456
						veh_prop(0x58, veh->profit_last_year);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   457
						veh_prop(0x59, veh->profit_last_year & 0xFF);
1855
6c7ade86caa7 (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
   458
						veh_prop(0x5A, veh->next == NULL ? INVALID_VEHICLE : veh->next->index);
426
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   459
						veh_prop(0x5C, veh->value);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   460
						veh_prop(0x5D, veh->value & 0xFFFFFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   461
						veh_prop(0x5E, veh->value & 0xFFFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   462
						veh_prop(0x5F, veh->value & 0xFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   463
						veh_prop(0x60, veh->string_id);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   464
						veh_prop(0x61, veh->string_id & 0xFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   465
						/* 00h..07h=sub image? 40h=in tunnel; actually some kind of status
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   466
						 * aircraft: >=13h when in flight
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   467
						 * train, ship: 80h=in depot
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   468
						 * rv: 0feh=in depot */
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   469
						/* TODO veh_prop(0x62, veh->???); */
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   470
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   471
						/* TODO: The rest is per-vehicle, I hope no GRF file looks so far.
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   472
						 * But they won't let us have an easy ride so surely *some* GRF
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   473
						 * file does. So someone needs to do this too. --pasky */
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   474
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   475
#undef veh_prop
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   476
					}
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   477
				}
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   478
			}
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   479
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   480
			target = value != -1 ? EvalDeterministicSpriteGroup(dsg, value) : dsg->default_group;
445
beafc0fb8f12 (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
   481
			//debug("Resolved variable %x: %d, %p", dsg->variable, value, callback);
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   482
			return resolve_func(target, veh, callback_info, resolve_func);
445
beafc0fb8f12 (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
   483
		}
beafc0fb8f12 (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
   484
beafc0fb8f12 (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
   485
		case SGT_RANDOMIZED: {
1477
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   486
			RandomizedSpriteGroup *rsg = &spritegroup->g.random;
445
beafc0fb8f12 (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
   487
beafc0fb8f12 (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
   488
			if (veh == NULL) {
beafc0fb8f12 (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
   489
				/* Purchase list of something. Show the first one. */
beafc0fb8f12 (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
   490
				assert(rsg->num_groups > 0);
beafc0fb8f12 (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
   491
				//debug("going for %p: %d", rsg->groups[0], rsg->groups[0].type);
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   492
				return resolve_func(&rsg->groups[0], NULL, callback_info, resolve_func);
445
beafc0fb8f12 (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
   493
			}
beafc0fb8f12 (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
   494
beafc0fb8f12 (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
   495
			if (rsg->var_scope == VSG_SCOPE_PARENT) {
beafc0fb8f12 (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
   496
				/* First engine in the vehicle chain */
beafc0fb8f12 (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
   497
				if (veh->type == VEH_Train)
beafc0fb8f12 (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
   498
					veh = GetFirstVehicleInChain(veh);
beafc0fb8f12 (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
   499
			}
beafc0fb8f12 (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
   500
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   501
			return resolve_func(EvalRandomizedSpriteGroup(rsg, veh->random_bits), veh, callback_info, resolve_func);
426
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   502
		}
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   503
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   504
		default:
445
beafc0fb8f12 (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
   505
			error("I don't know how to handle such a spritegroup %d!", spritegroup->type);
426
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   506
			return NULL;
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   507
	}
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   508
}
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   509
1477
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   510
static SpriteGroup *GetVehicleSpriteGroup(byte engine, const Vehicle *v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   511
{
1477
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   512
	SpriteGroup *group;
1802
da61740cc1e7 (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
   513
	byte cargo = GC_PURCHASE;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   514
414
4629d4bf7f14 (svn r611) -newgrf: Change GetCustomEngineSprite() calling convention (invisible to users of GetCustomVehicle*() wrappers). Needed for deterministic spritegroups support (pasky).
darkvater
parents: 410
diff changeset
   515
	if (v != NULL) {
4629d4bf7f14 (svn r611) -newgrf: Change GetCustomEngineSprite() calling convention (invisible to users of GetCustomVehicle*() wrappers). Needed for deterministic spritegroups support (pasky).
darkvater
parents: 410
diff changeset
   516
		cargo = _global_cargo_id[_opt.landscape][v->cargo_type];
1802
da61740cc1e7 (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
   517
		assert(cargo != GC_INVALID);
414
4629d4bf7f14 (svn r611) -newgrf: Change GetCustomEngineSprite() calling convention (invisible to users of GetCustomVehicle*() wrappers). Needed for deterministic spritegroups support (pasky).
darkvater
parents: 410
diff changeset
   518
	}
4629d4bf7f14 (svn r611) -newgrf: Change GetCustomEngineSprite() calling convention (invisible to users of GetCustomVehicle*() wrappers). Needed for deterministic spritegroups support (pasky).
darkvater
parents: 410
diff changeset
   519
4629d4bf7f14 (svn r611) -newgrf: Change GetCustomEngineSprite() calling convention (invisible to users of GetCustomVehicle*() wrappers). Needed for deterministic spritegroups support (pasky).
darkvater
parents: 410
diff changeset
   520
	group = &_engine_custom_sprites[engine][cargo];
4629d4bf7f14 (svn r611) -newgrf: Change GetCustomEngineSprite() calling convention (invisible to users of GetCustomVehicle*() wrappers). Needed for deterministic spritegroups support (pasky).
darkvater
parents: 410
diff changeset
   521
1560
d040e4763f45 (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
   522
	if (v != NULL && v->type == VEH_Train) {
d040e4763f45 (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
   523
		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
   524
1477
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   525
		if (overset != NULL) group = overset;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   526
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 114
diff changeset
   527
445
beafc0fb8f12 (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
   528
	return group;
beafc0fb8f12 (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
   529
}
beafc0fb8f12 (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
   530
1475
c21a6481b735 (svn r1979) Const correctness
tron
parents: 1474
diff changeset
   531
int GetCustomEngineSprite(byte engine, const Vehicle *v, byte direction)
445
beafc0fb8f12 (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
   532
{
1477
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   533
	SpriteGroup *group;
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   534
	RealSpriteGroup *rsg;
1802
da61740cc1e7 (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
   535
	byte cargo = GC_PURCHASE;
445
beafc0fb8f12 (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
   536
	byte loaded = 0;
beafc0fb8f12 (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
   537
	bool in_motion = 0;
beafc0fb8f12 (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
   538
	int totalsets, spriteset;
beafc0fb8f12 (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
   539
	int r;
beafc0fb8f12 (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
   540
beafc0fb8f12 (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
   541
	if (v != NULL) {
beafc0fb8f12 (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
   542
		int capacity = v->cargo_cap;
beafc0fb8f12 (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
   543
beafc0fb8f12 (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
   544
		cargo = _global_cargo_id[_opt.landscape][v->cargo_type];
1802
da61740cc1e7 (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
   545
		assert(cargo != GC_INVALID);
da61740cc1e7 (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
   546
445
beafc0fb8f12 (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
   547
		if (capacity == 0) capacity = 1;
beafc0fb8f12 (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
		loaded = (v->cargo_count * 100) / capacity;
beafc0fb8f12 (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
		in_motion = (v->cur_speed != 0);
beafc0fb8f12 (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
	}
beafc0fb8f12 (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
   551
beafc0fb8f12 (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
	group = GetVehicleSpriteGroup(engine, v);
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   553
	group = ResolveVehicleSpriteGroup(group, v, 0, (resolve_callback) ResolveVehicleSpriteGroup);
408
48da21eb9ff2 (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
   554
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   555
	if (group->type == SGT_REAL && group->g.real.sprites_per_set == 0 && cargo != GC_DEFAULT) {
369
3742b39b6cca (svn r557) -newgrf: Rename all 'superset' tokens to 'group' and some other small renamings (pasky and octo).
darkvater
parents: 292
diff changeset
   556
		// This group is empty but perhaps there'll be a default one.
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   557
		group = ResolveVehicleSpriteGroup(&_engine_custom_sprites[engine][GC_DEFAULT], v, 0,
445
beafc0fb8f12 (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
		                                (resolve_callback) ResolveVehicleSpriteGroup);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   559
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   560
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   561
	assert(group->type == SGT_REAL);
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   562
	rsg = &group->g.real;
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   563
408
48da21eb9ff2 (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
   564
	if (!rsg->sprites_per_set) {
369
3742b39b6cca (svn r557) -newgrf: Rename all 'superset' tokens to 'group' and some other small renamings (pasky and octo).
darkvater
parents: 292
diff changeset
   565
		// 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
   566
		// look up the sprite number in _engine_original_sprites.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   567
		return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   568
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   569
1988
c23f5c7139bb (svn r2494) - Fix: [newgrf] Dont assume a spriteset can only have 4 or 8 sprites.
hackykid
parents: 1962
diff changeset
   570
	assert(rsg->sprites_per_set <= 8);
c23f5c7139bb (svn r2494) - Fix: [newgrf] Dont assume a spriteset can only have 4 or 8 sprites.
hackykid
parents: 1962
diff changeset
   571
	direction %= rsg->sprites_per_set;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   572
408
48da21eb9ff2 (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
   573
	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
   574
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   575
	// 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
   576
	// empty and totally full vehicles. --pasky
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   577
	if (loaded == 100 || totalsets == 1) { // full
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   578
		spriteset = totalsets - 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   579
	} else if (loaded == 0 || totalsets == 2) { // empty
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   580
		spriteset = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   581
	} else { // something inbetween
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   582
		spriteset = loaded * (totalsets - 2) / 100 + 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   583
		// correct possible rounding errors
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   584
		if (!spriteset)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   585
			spriteset = 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   586
		else if (spriteset == totalsets - 1)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   587
			spriteset--;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   588
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   589
408
48da21eb9ff2 (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
   590
	r = (in_motion ? rsg->loaded[spriteset] : rsg->loading[spriteset]) + direction;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   591
	return r;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   592
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   593
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   594
/**
1908
2fa391fed79a (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1904
diff changeset
   595
 * Check if a wagon is currently using a wagon override
2fa391fed79a (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1904
diff changeset
   596
 * @param v The wagon to check
2fa391fed79a (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1904
diff changeset
   597
 * @return true if it is using an override, false otherwise
2fa391fed79a (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1904
diff changeset
   598
 */
2fa391fed79a (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1904
diff changeset
   599
bool UsesWagonOverride(const Vehicle *v) {
2fa391fed79a (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1904
diff changeset
   600
	assert(v->type == VEH_Train);
2fa391fed79a (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1904
diff changeset
   601
	return (GetWagonOverrideSpriteSet(v->engine_type, v->u.rail.first_engine) != NULL);
2fa391fed79a (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1904
diff changeset
   602
}
2fa391fed79a (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1904
diff changeset
   603
2fa391fed79a (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1904
diff changeset
   604
/**
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   605
 * Evaluates a newgrf callback
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   606
 * @param callback_info info about which callback to evaluate
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   607
 *  (bit 0-7)  = CallBack id of the callback to use, see CallBackId enum
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   608
 *  (bit 8-15) = Other info some callbacks need to have, callback specific, see CallBackId enum, not used yet
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   609
 * @param engine Engine type of the vehicle to evaluate the callback for
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   610
 * @param vehicle The vehicle to evaluate the callback for, NULL if it doesnt exist (yet)
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   611
 * @return The value the callback returned, or CALLBACK_FAILED if it failed
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   612
 */
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   613
uint16 GetCallBackResult(uint16 callback_info, byte engine, const Vehicle *v)
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   614
{
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   615
	SpriteGroup *group;
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   616
	byte cargo = GC_DEFAULT;
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   617
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   618
	if (v != NULL)
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   619
		cargo = _global_cargo_id[_opt.landscape][v->cargo_type];
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   620
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   621
	group = &_engine_custom_sprites[engine][cargo];
1904
6459332eded4 (svn r2410) - Fix: [newgrf] When resolving callbacks, dont ignore wagon overrides.
hackykid
parents: 1891
diff changeset
   622
6459332eded4 (svn r2410) - Fix: [newgrf] When resolving callbacks, dont ignore wagon overrides.
hackykid
parents: 1891
diff changeset
   623
	if (v != NULL && v->type == VEH_Train) {
6459332eded4 (svn r2410) - Fix: [newgrf] When resolving callbacks, dont ignore wagon overrides.
hackykid
parents: 1891
diff changeset
   624
		SpriteGroup *overset = GetWagonOverrideSpriteSet(engine, v->u.rail.first_engine);
6459332eded4 (svn r2410) - Fix: [newgrf] When resolving callbacks, dont ignore wagon overrides.
hackykid
parents: 1891
diff changeset
   625
6459332eded4 (svn r2410) - Fix: [newgrf] When resolving callbacks, dont ignore wagon overrides.
hackykid
parents: 1891
diff changeset
   626
		if (overset != NULL) group = overset;
6459332eded4 (svn r2410) - Fix: [newgrf] When resolving callbacks, dont ignore wagon overrides.
hackykid
parents: 1891
diff changeset
   627
	}
6459332eded4 (svn r2410) - Fix: [newgrf] When resolving callbacks, dont ignore wagon overrides.
hackykid
parents: 1891
diff changeset
   628
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   629
	group = ResolveVehicleSpriteGroup(group, v, callback_info, (resolve_callback) ResolveVehicleSpriteGroup);
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   630
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   631
	if (group->type == SGT_REAL && group->g.real.sprites_per_set == 0 && cargo != GC_DEFAULT) {
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   632
		// This group is empty but perhaps there'll be a default one.
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   633
		group = ResolveVehicleSpriteGroup(&_engine_custom_sprites[engine][GC_DEFAULT], v, callback_info,
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   634
		                                (resolve_callback) ResolveVehicleSpriteGroup);
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   635
	}
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   636
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   637
	if (group->type != SGT_CALLBACK)
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   638
		return CALLBACK_FAILED;
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   639
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   640
	return group->g.callback.result;
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   641
}
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   642
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   643
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   644
445
beafc0fb8f12 (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
   645
// Global variables are evil, yes, but we would end up with horribly overblown
beafc0fb8f12 (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
   646
// calling convention otherwise and this should be 100% reentrant.
beafc0fb8f12 (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
   647
static byte _vsg_random_triggers;
beafc0fb8f12 (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
   648
static byte _vsg_bits_to_reseed;
beafc0fb8f12 (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
   649
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   650
static SpriteGroup *TriggerVehicleSpriteGroup(SpriteGroup *spritegroup,
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   651
	Vehicle *veh, uint16 callback_info, resolve_callback resolve_func)
445
beafc0fb8f12 (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
   652
{
1477
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   653
	if (spritegroup->type == SGT_RANDOMIZED) {
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   654
		_vsg_bits_to_reseed |= RandomizedSpriteGroupTriggeredBits(
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   655
			&spritegroup->g.random,
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   656
			_vsg_random_triggers,
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   657
			&veh->waiting_triggers
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   658
		);
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   659
	}
445
beafc0fb8f12 (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
   660
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   661
	return ResolveVehicleSpriteGroup(spritegroup, veh, callback_info, resolve_func);
445
beafc0fb8f12 (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
   662
}
beafc0fb8f12 (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
   663
1477
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   664
static void DoTriggerVehicle(Vehicle *veh, VehicleTrigger trigger, byte base_random_bits, bool first)
445
beafc0fb8f12 (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
   665
{
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   666
	SpriteGroup *group;
1477
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   667
	RealSpriteGroup *rsg;
445
beafc0fb8f12 (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
   668
	byte new_random_bits;
beafc0fb8f12 (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
   669
beafc0fb8f12 (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
   670
	_vsg_random_triggers = trigger;
beafc0fb8f12 (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
   671
	_vsg_bits_to_reseed = 0;
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   672
	group = TriggerVehicleSpriteGroup(GetVehicleSpriteGroup(veh->engine_type, veh), veh, 0,
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   673
	                                  (resolve_callback) TriggerVehicleSpriteGroup);
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   674
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   675
	if (group->type == SGT_REAL && group->g.real.sprites_per_set == 0 && veh->cargo_type != GC_DEFAULT) {
445
beafc0fb8f12 (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
   676
		// This group turned out to be empty but perhaps there'll be a default one.
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   677
		group = TriggerVehicleSpriteGroup(&_engine_custom_sprites[veh->engine_type][GC_DEFAULT], veh, 0,
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   678
		                                  (resolve_callback) TriggerVehicleSpriteGroup);
445
beafc0fb8f12 (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
   679
	}
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   680
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   681
	assert(group->type == SGT_REAL);
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   682
	rsg = &group->g.real;
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   683
542
de27e74b11bd (svn r939) -Fix: Fixed compiler errors
truelight
parents: 507
diff changeset
   684
	new_random_bits = Random();
445
beafc0fb8f12 (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
   685
	veh->random_bits &= ~_vsg_bits_to_reseed;
542
de27e74b11bd (svn r939) -Fix: Fixed compiler errors
truelight
parents: 507
diff changeset
   686
	veh->random_bits |= (first ? new_random_bits : base_random_bits) & _vsg_bits_to_reseed;
445
beafc0fb8f12 (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
   687
beafc0fb8f12 (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
   688
	switch (trigger) {
beafc0fb8f12 (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
   689
		case VEHICLE_TRIGGER_NEW_CARGO:
beafc0fb8f12 (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
   690
			/* All vehicles in chain get ANY_NEW_CARGO trigger now.
beafc0fb8f12 (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
   691
			 * So we call it for the first one and they will recurse. */
beafc0fb8f12 (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
   692
			/* Indexing part of vehicle random bits needs to be
beafc0fb8f12 (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
   693
			 * same for all triggered vehicles in the chain (to get
beafc0fb8f12 (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
   694
			 * all the random-cargo wagons carry the same cargo,
beafc0fb8f12 (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
   695
			 * i.e.), so we give them all the NEW_CARGO triggered
beafc0fb8f12 (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
   696
			 * vehicle's portion of random bits. */
beafc0fb8f12 (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
   697
			assert(first);
beafc0fb8f12 (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
   698
			DoTriggerVehicle(GetFirstVehicleInChain(veh), VEHICLE_TRIGGER_ANY_NEW_CARGO, new_random_bits, false);
beafc0fb8f12 (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
   699
			break;
beafc0fb8f12 (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
   700
		case VEHICLE_TRIGGER_DEPOT:
beafc0fb8f12 (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
   701
			/* We now trigger the next vehicle in chain recursively.
beafc0fb8f12 (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
   702
			 * The random bits portions may be different for each
beafc0fb8f12 (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
   703
			 * vehicle in chain. */
beafc0fb8f12 (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
   704
			if (veh->next != NULL)
beafc0fb8f12 (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
   705
				DoTriggerVehicle(veh->next, trigger, 0, true);
beafc0fb8f12 (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
   706
			break;
beafc0fb8f12 (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
   707
		case VEHICLE_TRIGGER_EMPTY:
beafc0fb8f12 (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
   708
			/* We now trigger the next vehicle in chain
beafc0fb8f12 (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
   709
			 * recursively.  The random bits portions must be same
beafc0fb8f12 (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
   710
			 * for each vehicle in chain, so we give them all
beafc0fb8f12 (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
   711
			 * first chained vehicle's portion of random bits. */
beafc0fb8f12 (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
   712
			if (veh->next != NULL)
beafc0fb8f12 (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
   713
				DoTriggerVehicle(veh->next, trigger, first ? new_random_bits : base_random_bits, false);
beafc0fb8f12 (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
   714
			break;
beafc0fb8f12 (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
   715
		case VEHICLE_TRIGGER_ANY_NEW_CARGO:
beafc0fb8f12 (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
   716
			/* Now pass the trigger recursively to the next vehicle
beafc0fb8f12 (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
   717
			 * in chain. */
beafc0fb8f12 (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
   718
			assert(!first);
beafc0fb8f12 (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
			if (veh->next != NULL)
beafc0fb8f12 (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
				DoTriggerVehicle(veh->next, VEHICLE_TRIGGER_ANY_NEW_CARGO, base_random_bits, false);
beafc0fb8f12 (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
			break;
beafc0fb8f12 (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
	}
beafc0fb8f12 (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
}
beafc0fb8f12 (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
   724
1477
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   725
void TriggerVehicle(Vehicle *veh, VehicleTrigger trigger)
445
beafc0fb8f12 (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
{
beafc0fb8f12 (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
   727
	DoTriggerVehicle(veh, trigger, 0, true);
beafc0fb8f12 (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
   728
}
beafc0fb8f12 (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
   729
1474
a26a21fa10ef (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
   730
static char *_engine_custom_names[TOTAL_NUM_ENGINES];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   731
1329
6988419aa6f0 (svn r1833) byte -> char transition: the rest
tron
parents: 1328
diff changeset
   732
void SetCustomEngineName(int engine, const char *name)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   733
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   734
	_engine_custom_names[engine] = strdup(name);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   735
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   736
1474
a26a21fa10ef (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
   737
void UnInitNewgrEngines(void)
a26a21fa10ef (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
   738
{
a26a21fa10ef (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
   739
	char **i;
a26a21fa10ef (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
   740
	for (i = _engine_custom_names; i != endof(_engine_custom_names); i++) {
a26a21fa10ef (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
   741
		free(*i);
a26a21fa10ef (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
   742
		*i = NULL;
a26a21fa10ef (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
   743
	}
a26a21fa10ef (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
   744
}
a26a21fa10ef (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
   745
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   746
StringID GetCustomEngineName(int engine)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   747
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   748
	if (!_engine_custom_names[engine])
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   749
		return _engine_name_strings[engine];
2201
f240b3c7e2ec (svn r2717) Move _userstring to strings.[ch]
tron
parents: 2186
diff changeset
   750
	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
   751
	return STR_SPEC_USERSTRING;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   752
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   753
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   754
1786
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   755
void AcceptEnginePreview(Engine *e, PlayerID player)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   756
{
1962
8254df1b359b (svn r2468) -Codechange: Got rid of DEREF_PLAYER and replaced it by GetPlayer
celestar
parents: 1926
diff changeset
   757
	Player *p = GetPlayer(player);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   758
2147
eb6ba42fd216 (svn r2657) -Codechange: The available railtypes per player are now a bitmask, so
celestar
parents: 2140
diff changeset
   759
	assert(e->railtype < RAILTYPE_END);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   760
	SETBIT(e->player_avail, player);
2147
eb6ba42fd216 (svn r2657) -Codechange: The available railtypes per player are now a bitmask, so
celestar
parents: 2140
diff changeset
   761
	SETBIT(p->avail_railtypes, e->railtype);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   762
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   763
	e->preview_player = 0xFF;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   764
	InvalidateWindowClasses(WC_BUILD_VEHICLE);
1096
87415b0c6fdb (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
   765
	InvalidateWindowClasses(WC_REPLACE_VEHICLE);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   766
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   767
1786
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   768
static PlayerID GetBestPlayer(PlayerID pp)
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   769
{
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   770
	const Player *p;
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   771
	int32 best_hist;
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   772
	PlayerID best_player;
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   773
	uint mask = 0;
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   774
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   775
	do {
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   776
		best_hist = -1;
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   777
		best_player = -1;
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   778
		FOR_ALL_PLAYERS(p) {
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   779
			if (p->is_active && p->block_preview == 0 && !HASBIT(mask, p->index) &&
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   780
					p->old_economy[0].performance_history > best_hist) {
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   781
				best_hist = p->old_economy[0].performance_history;
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   782
				best_player = p->index;
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   783
			}
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   784
		}
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   785
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   786
		if (best_player == (PlayerID)-1) return -1;
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   787
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   788
		SETBIT(mask, best_player);
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   789
	} while (--pp != 0);
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   790
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   791
	return best_player;
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   792
}
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   793
1093
4fdc46eaf423 (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1009
diff changeset
   794
void EnginesDailyLoop(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   795
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   796
	Engine *e;
1786
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   797
	int i;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   798
1786
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   799
	if (_cur_year >= 130) return;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   800
1786
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   801
	for (e = _engines, i = 0; i != TOTAL_NUM_ENGINES; e++, i++) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   802
		if (e->flags & ENGINE_INTRODUCING) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   803
			if (e->flags & ENGINE_PREVIEWING) {
740
ab84ecdf86a2 (svn r1196) -Fix: Preview Vehicle had a small glitch. Fixed now.
truelight
parents: 555
diff changeset
   804
				if (e->preview_player != 0xFF && !--e->preview_wait) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   805
					e->flags &= ~ENGINE_PREVIEWING;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   806
					DeleteWindowById(WC_ENGINE_PREVIEW, i);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   807
					e->preview_player++;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 114
diff changeset
   808
				}
1786
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   809
 			} else if (e->preview_player != 0xFF) {
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   810
				PlayerID best_player = GetBestPlayer(e->preview_player);
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   811
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   812
				if (best_player == (PlayerID)-1) {
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   813
					e->preview_player = 0xFF;
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   814
					continue;
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   815
				}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 114
diff changeset
   816
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   817
				if (!IS_HUMAN_PLAYER(best_player)) {
1786
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   818
					/* XXX - TTDBUG: TTD has a bug here ???? */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   819
					AcceptEnginePreview(e, best_player);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   820
				} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   821
					e->flags |= ENGINE_PREVIEWING;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   822
					e->preview_wait = 20;
1786
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   823
					if (IS_INTERACTIVE_PLAYER(best_player))
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 114
diff changeset
   824
						ShowEnginePreviewWindow(i);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   825
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   826
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   827
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   828
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   829
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   830
1786
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   831
/** Accept an engine prototype. XXX - it is possible that the top-player
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   832
 * changes while you are waiting to accept the offer? Then it becomes invalid
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   833
 * @param x,y unused
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   834
 * @param p1 engine-prototype offered
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   835
 * @param p2 unused
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   836
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   837
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
   838
{
1786
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   839
	Engine *e;
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   840
	if (!IsEngineIndex(p1)) return CMD_ERROR;
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   841
1926
530480d14685 (svn r2432) Use GetEngine() instead of DEREF_ENGINE() or even _engines[]
tron
parents: 1908
diff changeset
   842
	e = GetEngine(p1);
1786
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   843
	if (GetBestPlayer(e->preview_player) != _current_player) return CMD_ERROR;
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   844
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   845
	if (flags & DC_EXEC)
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   846
		AcceptEnginePreview(e, _current_player);
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   847
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   848
	return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   849
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   850
257
0ff8a2e60a0d (svn r262) Fix: [ 1028234 ] Monorail and MagLev infrastructure not available in 1920s any more
dominik
parents: 220
diff changeset
   851
// Determine if an engine type is a wagon (and not a loco)
964
fe9e7b220b9f (svn r1456) Simplify isWagon(), make it static and give it a canonical name (IsWagon)
tron
parents: 938
diff changeset
   852
static bool IsWagon(byte index)
257
0ff8a2e60a0d (svn r262) Fix: [ 1028234 ] Monorail and MagLev infrastructure not available in 1920s any more
dominik
parents: 220
diff changeset
   853
{
964
fe9e7b220b9f (svn r1456) Simplify isWagon(), make it static and give it a canonical name (IsWagon)
tron
parents: 938
diff changeset
   854
	return index < NUM_TRAIN_ENGINES && RailVehInfo(index)->flags & RVI_WAGON;
257
0ff8a2e60a0d (svn r262) Fix: [ 1028234 ] Monorail and MagLev infrastructure not available in 1920s any more
dominik
parents: 220
diff changeset
   855
}
0ff8a2e60a0d (svn r262) Fix: [ 1028234 ] Monorail and MagLev infrastructure not available in 1920s any more
dominik
parents: 220
diff changeset
   856
410
0efd84450b01 (svn r607) -Patch: [ 985102 ] static cleanup
tron
parents: 408
diff changeset
   857
static void NewVehicleAvailable(Engine *e)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   858
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   859
	Vehicle *v;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   860
	Player *p;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   861
	int index = e - _engines;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   862
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   863
	// 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
   864
	// prevent that player from getting future intro periods for a while.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   865
	if (e->flags&ENGINE_INTRODUCING) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   866
		FOR_ALL_PLAYERS(p) {
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   867
			uint block_preview = p->block_preview;
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   868
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   869
			if (!HASBIT(e->player_avail,p->index))
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   870
				continue;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 114
diff changeset
   871
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   872
			/* We assume the user did NOT build it.. prove me wrong ;) */
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   873
			p->block_preview = 20;
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   874
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   875
			FOR_ALL_VEHICLES(v) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   876
				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
   877
						(v->type == VEH_Aircraft && v->subtype <= 2)) {
919
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   878
					if (v->owner == p->index && v->engine_type == index) {
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   879
						/* The user did prove me wrong, so restore old value */
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   880
						p->block_preview = block_preview;
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   881
						break;
544f374ee392 (svn r1407) -Codechange: changed a lot around _stations, _vehicles, _towns and _industries
truelight
parents: 915
diff changeset
   882
					}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   883
				}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   884
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   885
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   886
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   887
114
6a2af0c2d0db (svn r115) Fix: monorail/maglev became available around 1927
dominik
parents: 0
diff changeset
   888
	e->flags = (e->flags & ~ENGINE_INTRODUCING) | ENGINE_AVAILABLE;
6a2af0c2d0db (svn r115) Fix: monorail/maglev became available around 1927
dominik
parents: 0
diff changeset
   889
	InvalidateWindowClasses(WC_BUILD_VEHICLE);
1096
87415b0c6fdb (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
   890
	InvalidateWindowClasses(WC_REPLACE_VEHICLE);
114
6a2af0c2d0db (svn r115) Fix: monorail/maglev became available around 1927
dominik
parents: 0
diff changeset
   891
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   892
	// Now available for all players
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   893
	e->player_avail = (byte)-1;
114
6a2af0c2d0db (svn r115) Fix: monorail/maglev became available around 1927
dominik
parents: 0
diff changeset
   894
6a2af0c2d0db (svn r115) Fix: monorail/maglev became available around 1927
dominik
parents: 0
diff changeset
   895
	// Do not introduce new rail wagons
964
fe9e7b220b9f (svn r1456) Simplify isWagon(), make it static and give it a canonical name (IsWagon)
tron
parents: 938
diff changeset
   896
	if (IsWagon(index))
257
0ff8a2e60a0d (svn r262) Fix: [ 1028234 ] Monorail and MagLev infrastructure not available in 1920s any more
dominik
parents: 220
diff changeset
   897
		return;
114
6a2af0c2d0db (svn r115) Fix: monorail/maglev became available around 1927
dominik
parents: 0
diff changeset
   898
6a2af0c2d0db (svn r115) Fix: monorail/maglev became available around 1927
dominik
parents: 0
diff changeset
   899
	// make maglev / monorail available
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   900
	FOR_ALL_PLAYERS(p) {
2147
eb6ba42fd216 (svn r2657) -Codechange: The available railtypes per player are now a bitmask, so
celestar
parents: 2140
diff changeset
   901
		if (p->is_active) {
eb6ba42fd216 (svn r2657) -Codechange: The available railtypes per player are now a bitmask, so
celestar
parents: 2140
diff changeset
   902
			assert(e->railtype < RAILTYPE_END);
eb6ba42fd216 (svn r2657) -Codechange: The available railtypes per player are now a bitmask, so
celestar
parents: 2140
diff changeset
   903
			SETBIT(p->avail_railtypes, e->railtype);
eb6ba42fd216 (svn r2657) -Codechange: The available railtypes per player are now a bitmask, so
celestar
parents: 2140
diff changeset
   904
		}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   905
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   906
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   907
	if ((byte)index < NUM_TRAIN_ENGINES) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   908
		AddNewsItem(index, NEWS_FLAGS(NM_CALLBACK, 0, NT_NEW_VEHICLES, DNC_TRAINAVAIL), 0, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   909
	} else if ((byte)index < NUM_TRAIN_ENGINES + NUM_ROAD_ENGINES) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   910
		AddNewsItem(index, NEWS_FLAGS(NM_CALLBACK, 0, NT_NEW_VEHICLES, DNC_ROADAVAIL), 0, 0);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   911
	} else if ((byte)index < NUM_TRAIN_ENGINES + NUM_ROAD_ENGINES + NUM_SHIP_ENGINES) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   912
		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
   913
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   914
		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
   915
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   916
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   917
1093
4fdc46eaf423 (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1009
diff changeset
   918
void EnginesMonthlyLoop(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   919
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   920
	Engine *e;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   921
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   922
	if (_cur_year < 130) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   923
		for(e=_engines; e != endof(_engines); e++) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   924
			// Age the vehicle
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   925
			if (e->flags&ENGINE_AVAILABLE && e->age != 0xFFFF) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   926
				e->age++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   927
				CalcEngineReliability(e);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   928
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   929
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   930
			if (!(e->flags & ENGINE_AVAILABLE) && (uint16)(_date - min(_date, 365)) >= e->intro_date) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   931
				// Introduce it to all players
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   932
				NewVehicleAvailable(e);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   933
			} 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
   934
				// 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
   935
				e->flags |= ENGINE_INTRODUCING;
257
0ff8a2e60a0d (svn r262) Fix: [ 1028234 ] Monorail and MagLev infrastructure not available in 1920s any more
dominik
parents: 220
diff changeset
   936
0ff8a2e60a0d (svn r262) Fix: [ 1028234 ] Monorail and MagLev infrastructure not available in 1920s any more
dominik
parents: 220
diff changeset
   937
				// Do not introduce new rail wagons
964
fe9e7b220b9f (svn r1456) Simplify isWagon(), make it static and give it a canonical name (IsWagon)
tron
parents: 938
diff changeset
   938
				if (!IsWagon(e - _engines))
257
0ff8a2e60a0d (svn r262) Fix: [ 1028234 ] Monorail and MagLev infrastructure not available in 1920s any more
dominik
parents: 220
diff changeset
   939
					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
   940
			}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   941
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   942
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   943
	AdjustAvailAircraft();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   944
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   945
1786
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   946
/** Rename an engine.
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   947
 * @param x,y unused
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   948
 * @param p1 engine ID to rename
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   949
 * @param p2 unused
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   950
 */
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   951
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
   952
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   953
	StringID str;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   954
1840
ce40137a6ab4 (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
   955
	if (!IsEngineIndex(p1) || _cmd_text[0] == '\0') return CMD_ERROR;
1786
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   956
1820
d03c56850dc2 (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
   957
	str = AllocateNameUnique(_cmd_text, 0);
1786
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
   958
	if (str == 0) return CMD_ERROR;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 114
diff changeset
   959
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   960
	if (flags & DC_EXEC) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   961
		StringID old_str = _engine_name_strings[p1];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   962
		_engine_name_strings[p1] = str;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   963
		DeleteName(old_str);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   964
		_vehicle_design_names |= 3;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   965
		MarkWholeScreenDirty();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   966
	} else {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   967
		DeleteName(str);
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
	return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   971
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   972
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   973
1881
435d39bd6ee0 (svn r2387) - CodeChange: made the saveload code more readable and also removed the 'byte' saveload arrays which means you can save an array of more than 255 elements, or bigger structs than 255 bytes. This doesn't yet solve the problem that a chunk can be a maximum of 16384 big.
Darkvater
parents: 1855
diff changeset
   974
static const SaveLoad _engine_desc[] = {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   975
	SLE_VAR(Engine,intro_date,						SLE_UINT16),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   976
	SLE_VAR(Engine,age,										SLE_UINT16),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   977
	SLE_VAR(Engine,reliability,						SLE_UINT16),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   978
	SLE_VAR(Engine,reliability_spd_dec,		SLE_UINT16),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   979
	SLE_VAR(Engine,reliability_start,			SLE_UINT16),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   980
	SLE_VAR(Engine,reliability_max,				SLE_UINT16),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   981
	SLE_VAR(Engine,reliability_final,			SLE_UINT16),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   982
	SLE_VAR(Engine,duration_phase_1,			SLE_UINT16),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   983
	SLE_VAR(Engine,duration_phase_2,			SLE_UINT16),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   984
	SLE_VAR(Engine,duration_phase_3,			SLE_UINT16),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   985
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   986
	SLE_VAR(Engine,lifelength,						SLE_UINT8),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   987
	SLE_VAR(Engine,flags,									SLE_UINT8),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   988
	SLE_VAR(Engine,preview_player,				SLE_UINT8),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   989
	SLE_VAR(Engine,preview_wait,					SLE_UINT8),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   990
	SLE_VAR(Engine,railtype,							SLE_UINT8),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   991
	SLE_VAR(Engine,player_avail,					SLE_UINT8),
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   992
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   993
	// reserve extra space in savegame here. (currently 16 bytes)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   994
	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
   995
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   996
	SLE_END()
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   997
};
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   998
1093
4fdc46eaf423 (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1009
diff changeset
   999
static void Save_ENGN(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1000
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1001
	Engine *e;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1002
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1003
	for(i=0,e=_engines; i != lengthof(_engines); i++,e++) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1004
		SlSetArrayIndex(i);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1005
		SlObject(e, _engine_desc);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1006
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1007
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1008
1093
4fdc46eaf423 (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1009
diff changeset
  1009
static void Load_ENGN(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1010
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1011
	int index;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1012
	while ((index = SlIterateArray()) != -1) {
1926
530480d14685 (svn r2432) Use GetEngine() instead of DEREF_ENGINE() or even _engines[]
tron
parents: 1908
diff changeset
  1013
		SlObject(GetEngine(index), _engine_desc);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1014
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1015
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1016
1093
4fdc46eaf423 (svn r1594) Convert all undefined parameter lists to (void) and add the appropriate warning flags in the Makefile
tron
parents: 1009
diff changeset
  1017
static void LoadSave_ENGS(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1018
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1019
	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
  1020
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1021
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1022
const ChunkHandler _engine_chunk_handlers[] = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1023
	{ 'ENGN', Save_ENGN, Load_ENGN, CH_ARRAY},
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
  1024
	{ 'ENGS', LoadSave_ENGS, LoadSave_ENGS, CH_RIFF | CH_LAST},
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
1197
433b4a05fa3e (svn r1701) Style police ^^
tron
parents: 1196
diff changeset
  1027
1196
67f7f3017d99 (svn r1700) - Fix: Hacked clients can no longer be used to build vehicles that are not available yet (Hackykid)
bjarni
parents: 1096
diff changeset
  1028
/*
1197
433b4a05fa3e (svn r1701) Style police ^^
tron
parents: 1196
diff changeset
  1029
 * returns true if an engine is valid, of the specified type, and buildable by
433b4a05fa3e (svn r1701) Style police ^^
tron
parents: 1196
diff changeset
  1030
 * the current player, false otherwise
1196
67f7f3017d99 (svn r1700) - Fix: Hacked clients can no longer be used to build vehicles that are not available yet (Hackykid)
bjarni
parents: 1096
diff changeset
  1031
 *
67f7f3017d99 (svn r1700) - Fix: Hacked clients can no longer be used to build vehicles that are not available yet (Hackykid)
bjarni
parents: 1096
diff changeset
  1032
 * engine = index of the engine to check
67f7f3017d99 (svn r1700) - Fix: Hacked clients can no longer be used to build vehicles that are not available yet (Hackykid)
bjarni
parents: 1096
diff changeset
  1033
 * type   = the type the engine should be of (VEH_xxx)
67f7f3017d99 (svn r1700) - Fix: Hacked clients can no longer be used to build vehicles that are not available yet (Hackykid)
bjarni
parents: 1096
diff changeset
  1034
 */
1197
433b4a05fa3e (svn r1701) Style police ^^
tron
parents: 1196
diff changeset
  1035
bool IsEngineBuildable(uint engine, byte type)
433b4a05fa3e (svn r1701) Style police ^^
tron
parents: 1196
diff changeset
  1036
{
433b4a05fa3e (svn r1701) Style police ^^
tron
parents: 1196
diff changeset
  1037
	const Engine *e;
1196
67f7f3017d99 (svn r1700) - Fix: Hacked clients can no longer be used to build vehicles that are not available yet (Hackykid)
bjarni
parents: 1096
diff changeset
  1038
67f7f3017d99 (svn r1700) - Fix: Hacked clients can no longer be used to build vehicles that are not available yet (Hackykid)
bjarni
parents: 1096
diff changeset
  1039
	// check if it's an engine that is in the engine array
1786
7cfd46c3fcc4 (svn r2290) - CodeChange: protect the next batch of commands. This brings us to a total of 61, which is 53% :)
Darkvater
parents: 1560
diff changeset
  1040
	if (!IsEngineIndex(engine)) return false;
1196
67f7f3017d99 (svn r1700) - Fix: Hacked clients can no longer be used to build vehicles that are not available yet (Hackykid)
bjarni
parents: 1096
diff changeset
  1041
1926
530480d14685 (svn r2432) Use GetEngine() instead of DEREF_ENGINE() or even _engines[]
tron
parents: 1908
diff changeset
  1042
	e = GetEngine(engine);
1196
67f7f3017d99 (svn r1700) - Fix: Hacked clients can no longer be used to build vehicles that are not available yet (Hackykid)
bjarni
parents: 1096
diff changeset
  1043
67f7f3017d99 (svn r1700) - Fix: Hacked clients can no longer be used to build vehicles that are not available yet (Hackykid)
bjarni
parents: 1096
diff changeset
  1044
	// check if it's an engine of specified type
67f7f3017d99 (svn r1700) - Fix: Hacked clients can no longer be used to build vehicles that are not available yet (Hackykid)
bjarni
parents: 1096
diff changeset
  1045
	if (e->type != type) return false;
67f7f3017d99 (svn r1700) - Fix: Hacked clients can no longer be used to build vehicles that are not available yet (Hackykid)
bjarni
parents: 1096
diff changeset
  1046
67f7f3017d99 (svn r1700) - Fix: Hacked clients can no longer be used to build vehicles that are not available yet (Hackykid)
bjarni
parents: 1096
diff changeset
  1047
	// check if it's available
67f7f3017d99 (svn r1700) - Fix: Hacked clients can no longer be used to build vehicles that are not available yet (Hackykid)
bjarni
parents: 1096
diff changeset
  1048
	if (!HASBIT(e->player_avail, _current_player)) return false;
67f7f3017d99 (svn r1700) - Fix: Hacked clients can no longer be used to build vehicles that are not available yet (Hackykid)
bjarni
parents: 1096
diff changeset
  1049
67f7f3017d99 (svn r1700) - Fix: Hacked clients can no longer be used to build vehicles that are not available yet (Hackykid)
bjarni
parents: 1096
diff changeset
  1050
	return true;
67f7f3017d99 (svn r1700) - Fix: Hacked clients can no longer be used to build vehicles that are not available yet (Hackykid)
bjarni
parents: 1096
diff changeset
  1051
}