newgrf_engine.c
author peter1138
Sun, 23 Apr 2006 22:25:33 +0000
changeset 3648 d234c46bb37a
parent 3630 13278b8ed023
child 3668 42325f12e7d8
permissions -rw-r--r--
(svn r4559) - NewGRF: keep a record of which grf an engine is defined in. Will be used in the future.
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"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
     9
#include "engine.h"
2982
eb7f50dcb249 (svn r3557) - NewGRF: move callback enums to a new file as more than just engines will use them
peter1138
parents: 2971
diff changeset
    10
#include "newgrf_callbacks.h"
2962
f0a49b646c48 (svn r3524) - Split newgrf features from engine.[ch] into newgrf_engine.[ch], and add the new files to project files.
peter1138
parents: 2951
diff changeset
    11
#include "newgrf_engine.h"
405
415546028e8d (svn r602) -newgrf: Move DrawTileSeqStruct & co and struct SpriteGroup to sprite.h (pasky)
darkvater
parents: 369
diff changeset
    12
#include "sprite.h"
2159
f6284cf5fab0 (svn r2669) Shuffle some more stuff around to reduce dependencies
tron
parents: 2147
diff changeset
    13
#include "variables.h"
2708
48714bad9499 (svn r3252) - NewGRF: Map new train subtypes to old types for rail vehicles.
peter1138
parents: 2639
diff changeset
    14
#include "train.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    15
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    16
// 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
    17
1477
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
    18
typedef struct WagonOverride {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    19
	byte *train_id;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    20
	int trains;
2488
1e98c71e5f6a (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
    21
	SpriteGroup *group;
1477
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
    22
} WagonOverride;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    23
1477
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
    24
typedef struct WagonOverrides {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    25
	int overrides_count;
1477
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
    26
	WagonOverride *overrides;
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
    27
} WagonOverrides;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    28
1477
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
    29
static WagonOverrides _engine_wagon_overrides[TOTAL_NUM_ENGINES];
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
    30
2477
225b2916fe2a (svn r3003) Change int, uint, uin16 and byte to EngineID where appropriate, plus some related changes (mostly casts)
tron
parents: 2469
diff changeset
    31
void SetWagonOverrideSprites(EngineID engine, SpriteGroup *group, byte *train_id,
1477
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
    32
	int trains)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    33
{
1477
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
    34
	WagonOverrides *wos;
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
    35
	WagonOverride *wo;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    36
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    37
	wos = &_engine_wagon_overrides[engine];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    38
	wos->overrides_count++;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    39
	wos->overrides = realloc(wos->overrides,
1477
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
    40
		wos->overrides_count * sizeof(*wos->overrides));
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 114
diff changeset
    41
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    42
	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
    43
	/* 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
    44
	 * 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
    45
	 * --pasky */
2488
1e98c71e5f6a (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
    46
	wo->group = group;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    47
	wo->trains = trains;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    48
	wo->train_id = malloc(trains);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    49
	memcpy(wo->train_id, train_id, trains);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    50
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    51
2477
225b2916fe2a (svn r3003) Change int, uint, uin16 and byte to EngineID where appropriate, plus some related changes (mostly casts)
tron
parents: 2469
diff changeset
    52
static const SpriteGroup *GetWagonOverrideSpriteSet(EngineID engine, byte overriding_engine)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    53
{
2242
512eae2cf006 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
    54
	const WagonOverrides *wos = &_engine_wagon_overrides[engine];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    55
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    56
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
    57
	// 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
    58
	// 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
    59
	// 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
    60
	// that. --pasky
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    61
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    62
	for (i = 0; i < wos->overrides_count; i++) {
2242
512eae2cf006 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
    63
		const WagonOverride *wo = &wos->overrides[i];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    64
		int j;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    65
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    66
		for (j = 0; j < wo->trains; j++) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    67
			if (wo->train_id[j] == overriding_engine)
2488
1e98c71e5f6a (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
    68
				return wo->group;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    69
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    70
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    71
	return NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    72
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    73
2491
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
    74
/**
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
    75
 * Unload all wagon override sprite groups.
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
    76
 */
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
    77
void UnloadWagonOverrides(void)
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
    78
{
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
    79
	WagonOverrides *wos;
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
    80
	WagonOverride *wo;
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
    81
	EngineID engine;
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
    82
	int i;
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
    83
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
    84
	for (engine = 0; engine < TOTAL_NUM_ENGINES; engine++) {
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
    85
		wos = &_engine_wagon_overrides[engine];
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
    86
		for (i = 0; i < wos->overrides_count; i++) {
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
    87
			wo = &wos->overrides[i];
3595
a0acdb23e662 (svn r4486) - NewGRF: Create and use a memory pool to manage sprite groups. This
peter1138
parents: 3390
diff changeset
    88
			wo->group = NULL;
2491
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
    89
			free(wo->train_id);
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
    90
		}
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
    91
		free(wos->overrides);
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
    92
		wos->overrides_count = 0;
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
    93
		wos->overrides = NULL;
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
    94
	}
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
    95
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    96
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    97
// 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
    98
// (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
    99
// 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
   100
// memory. --pasky)
2488
1e98c71e5f6a (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   101
static SpriteGroup *engine_custom_sprites[TOTAL_NUM_ENGINES][NUM_GLOBAL_CID];
3648
d234c46bb37a (svn r4559) - NewGRF: keep a record of which grf an engine is defined in. Will be used in the future.
peter1138
parents: 3630
diff changeset
   102
static uint32 _engine_grf[TOTAL_NUM_ENGINES];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   103
2477
225b2916fe2a (svn r3003) Change int, uint, uin16 and byte to EngineID where appropriate, plus some related changes (mostly casts)
tron
parents: 2469
diff changeset
   104
void SetCustomEngineSprites(EngineID engine, byte cargo, SpriteGroup *group)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   105
{
3614
c697c69b6363 (svn r4510) - NewGRF bounds checking:
peter1138
parents: 3607
diff changeset
   106
	assert(engine < TOTAL_NUM_ENGINES);
2491
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   107
	if (engine_custom_sprites[engine][cargo] != NULL) {
3595
a0acdb23e662 (svn r4486) - NewGRF: Create and use a memory pool to manage sprite groups. This
peter1138
parents: 3390
diff changeset
   108
		DEBUG(grf, 6)("SetCustomEngineSprites: engine `%d' cargo `%d' already has group -- replacing.", engine, cargo);
2491
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   109
	}
2488
1e98c71e5f6a (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   110
	engine_custom_sprites[engine][cargo] = group;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   111
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   112
2491
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   113
/**
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   114
 * Unload all engine sprite groups.
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   115
 */
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   116
void UnloadCustomEngineSprites(void)
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   117
{
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   118
	EngineID engine;
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   119
	CargoID cargo;
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   120
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   121
	for (engine = 0; engine < TOTAL_NUM_ENGINES; engine++) {
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   122
		for (cargo = 0; cargo < NUM_GLOBAL_CID; cargo++) {
3595
a0acdb23e662 (svn r4486) - NewGRF: Create and use a memory pool to manage sprite groups. This
peter1138
parents: 3390
diff changeset
   123
			engine_custom_sprites[engine][cargo] = NULL;
2491
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   124
		}
3648
d234c46bb37a (svn r4559) - NewGRF: keep a record of which grf an engine is defined in. Will be used in the future.
peter1138
parents: 3630
diff changeset
   125
		_engine_grf[engine] = 0;
2491
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   126
	}
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   127
}
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   128
3648
d234c46bb37a (svn r4559) - NewGRF: keep a record of which grf an engine is defined in. Will be used in the future.
peter1138
parents: 3630
diff changeset
   129
void SetEngineGRF(EngineID engine, uint32 grfid)
d234c46bb37a (svn r4559) - NewGRF: keep a record of which grf an engine is defined in. Will be used in the future.
peter1138
parents: 3630
diff changeset
   130
{
d234c46bb37a (svn r4559) - NewGRF: keep a record of which grf an engine is defined in. Will be used in the future.
peter1138
parents: 3630
diff changeset
   131
	assert(engine < TOTAL_NUM_ENGINES);
d234c46bb37a (svn r4559) - NewGRF: keep a record of which grf an engine is defined in. Will be used in the future.
peter1138
parents: 3630
diff changeset
   132
	_engine_grf[engine] = grfid;
d234c46bb37a (svn r4559) - NewGRF: keep a record of which grf an engine is defined in. Will be used in the future.
peter1138
parents: 3630
diff changeset
   133
}
d234c46bb37a (svn r4559) - NewGRF: keep a record of which grf an engine is defined in. Will be used in the future.
peter1138
parents: 3630
diff changeset
   134
d234c46bb37a (svn r4559) - NewGRF: keep a record of which grf an engine is defined in. Will be used in the future.
peter1138
parents: 3630
diff changeset
   135
uint32 GetEngineGRFID(EngineID engine)
d234c46bb37a (svn r4559) - NewGRF: keep a record of which grf an engine is defined in. Will be used in the future.
peter1138
parents: 3630
diff changeset
   136
{
d234c46bb37a (svn r4559) - NewGRF: keep a record of which grf an engine is defined in. Will be used in the future.
peter1138
parents: 3630
diff changeset
   137
	assert(engine < TOTAL_NUM_ENGINES);
d234c46bb37a (svn r4559) - NewGRF: keep a record of which grf an engine is defined in. Will be used in the future.
peter1138
parents: 3630
diff changeset
   138
	return _engine_grf[engine];
d234c46bb37a (svn r4559) - NewGRF: keep a record of which grf an engine is defined in. Will be used in the future.
peter1138
parents: 3630
diff changeset
   139
}
d234c46bb37a (svn r4559) - NewGRF: keep a record of which grf an engine is defined in. Will be used in the future.
peter1138
parents: 3630
diff changeset
   140
d234c46bb37a (svn r4559) - NewGRF: keep a record of which grf an engine is defined in. Will be used in the future.
peter1138
parents: 3630
diff changeset
   141
2708
48714bad9499 (svn r3252) - NewGRF: Map new train subtypes to old types for rail vehicles.
peter1138
parents: 2639
diff changeset
   142
static int MapOldSubType(const Vehicle *v)
48714bad9499 (svn r3252) - NewGRF: Map new train subtypes to old types for rail vehicles.
peter1138
parents: 2639
diff changeset
   143
{
48714bad9499 (svn r3252) - NewGRF: Map new train subtypes to old types for rail vehicles.
peter1138
parents: 2639
diff changeset
   144
	if (v->type != VEH_Train) return v->subtype;
48714bad9499 (svn r3252) - NewGRF: Map new train subtypes to old types for rail vehicles.
peter1138
parents: 2639
diff changeset
   145
	if (IsTrainEngine(v)) return 0;
48714bad9499 (svn r3252) - NewGRF: Map new train subtypes to old types for rail vehicles.
peter1138
parents: 2639
diff changeset
   146
	if (IsFreeWagon(v)) return 4;
48714bad9499 (svn r3252) - NewGRF: Map new train subtypes to old types for rail vehicles.
peter1138
parents: 2639
diff changeset
   147
	return 2;
48714bad9499 (svn r3252) - NewGRF: Map new train subtypes to old types for rail vehicles.
peter1138
parents: 2639
diff changeset
   148
}
48714bad9499 (svn r3252) - NewGRF: Map new train subtypes to old types for rail vehicles.
peter1138
parents: 2639
diff changeset
   149
3014
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   150
static int VehicleSpecificProperty(const Vehicle *v, byte var) {
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   151
	switch (v->type) {
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   152
		case VEH_Train:
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   153
			switch (var) {
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   154
				case 0x62: return v->u.rail.track;
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   155
				case 0x66: return v->u.rail.railtype;
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   156
				case 0x73: return v->u.rail.cached_veh_length;
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   157
				case 0x74: return v->u.rail.cached_power;
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   158
				case 0x75: return v->u.rail.cached_power & 0xFFFFFF;
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   159
				case 0x76: return v->u.rail.cached_power & 0xFFFF;
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   160
				case 0x77: return v->u.rail.cached_power & 0xFF;
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   161
				case 0x7C: return v->first->index;
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   162
				case 0x7D: return v->first->index & 0xFF;
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   163
			}
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   164
			break;
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   165
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   166
		case VEH_Road:
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   167
			switch (var) {
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   168
				case 0x62: return v->u.road.state;
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   169
				case 0x64: return v->u.road.blocked_ctr;
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   170
				case 0x65: return v->u.road.blocked_ctr & 0xFF;
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   171
				case 0x66: return v->u.road.overtaking;
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   172
				case 0x67: return v->u.road.overtaking_ctr;
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   173
				case 0x68: return v->u.road.crashed_ctr;
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   174
				case 0x69: return v->u.road.crashed_ctr & 0xFF;
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   175
			}
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   176
			break;
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   177
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   178
		case VEH_Aircraft:
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   179
			switch (var) {
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   180
				// case 0x62: XXX Need to convert from ottd to ttdp state
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   181
				case 0x63: return v->u.air.targetairport;
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   182
				// case 0x66: XXX
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   183
			}
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   184
			break;
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   185
	}
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   186
3630
13278b8ed023 (svn r4529) - Codechange: Use proper naming for hex numbers in debug prints eg. 0xF3A6. Use fixed lengths where applicable (newgrf). Unfortunately '%#X' is unusable since it gives 0XFF3 and '%#x' gives 0xff3 while we want 0xFF3 :P
Darkvater
parents: 3614
diff changeset
   187
	DEBUG(grf, 1)("Unhandled vehicle property 0x%02X (var 0x%02X), type 0x%02X", var, var + 0x80, v->type);
3014
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   188
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   189
	return -1;
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   190
}
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   191
2242
512eae2cf006 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   192
typedef SpriteGroup *(*resolve_callback)(const SpriteGroup *spritegroup,
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   193
	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
   194
2242
512eae2cf006 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   195
static const SpriteGroup* ResolveVehicleSpriteGroup(const SpriteGroup *spritegroup,
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   196
	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
   197
{
2488
1e98c71e5f6a (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   198
	if (spritegroup == NULL)
1e98c71e5f6a (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   199
		return NULL;
1e98c71e5f6a (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   200
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
   201
	//debug("spgt %d", spritegroup->type);
426
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   202
	switch (spritegroup->type) {
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   203
		case SGT_REAL:
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   204
		case SGT_CALLBACK:
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   205
			return spritegroup;
426
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   206
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   207
		case SGT_DETERMINISTIC: {
2242
512eae2cf006 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   208
			const DeterministicSpriteGroup *dsg = &spritegroup->g.determ;
512eae2cf006 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   209
			const SpriteGroup *target;
426
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   210
			int value = -1;
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   211
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
   212
			//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
   213
			if (dsg->variable == 0x0C) {
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   214
				/* Callback ID */
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   215
				value = callback_info & 0xFF;
2602
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2589
diff changeset
   216
			} else if (dsg->variable == 0x10) {
1a6561c9a408 (svn r3139) -NewGRF, Feature: support for articulated rail vehicles. This is used, for example, by coal tenders.
peter1138
parents: 2589
diff changeset
   217
				value = (callback_info >> 8) & 0xFF;
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   218
			} else if ((dsg->variable >> 6) == 0) {
426
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   219
				/* General property */
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   220
				value = GetDeterministicSpriteValue(dsg->variable);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   221
			} 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
   222
				/* 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
   223
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
   224
				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
   225
					/* 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
   226
					 * 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
   227
					 * 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
   228
					 * (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
   229
					if (dsg->num_ranges > 0) {
2488
1e98c71e5f6a (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   230
						target = dsg->ranges[0].group;
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
   231
					} 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
   232
						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
   233
					}
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   234
					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
   235
				}
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
   236
426
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   237
				if (dsg->var_scope == VSG_SCOPE_PARENT) {
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   238
					/* 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
   239
					if (veh->type == VEH_Train)
426
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   240
						veh = GetFirstVehicleInChain(veh);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   241
				}
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   242
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
   243
				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
   244
					if (veh->type == VEH_Train) {
2242
512eae2cf006 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   245
						const Vehicle *u = GetFirstVehicleInChain(veh);
426
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   246
						byte chain_before = 0, chain_after = 0;
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   247
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   248
						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
   249
							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
   250
							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
   251
								chain_before = 0;
426
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   252
							u = u->next;
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   253
						}
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
   254
						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
   255
							chain_after++;
426
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   256
							u = u->next;
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   257
						};
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   258
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
   259
						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
   260
						        | (chain_before + chain_after) << 16;
426
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   261
					} else {
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   262
						value = 1; /* 1 vehicle in the chain */
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   263
					}
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   264
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   265
				} else {
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   266
					// TTDPatch runs on little-endian arch;
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   267
					// 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
   268
					switch (dsg->variable - 0x80) {
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 543
diff changeset
   269
#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
   270
						veh_prop(0x00, veh->type);
2708
48714bad9499 (svn r3252) - NewGRF: Map new train subtypes to old types for rail vehicles.
peter1138
parents: 2639
diff changeset
   271
						veh_prop(0x01, MapOldSubType(veh));
426
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   272
						veh_prop(0x04, veh->index);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   273
						veh_prop(0x05, veh->index & 0xFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   274
						/* XXX? Is THIS right? */
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 543
diff changeset
   275
						veh_prop(0x0A, PackOrder(&veh->current_order));
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 543
diff changeset
   276
						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
   277
						veh_prop(0x0C, veh->num_orders);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   278
						veh_prop(0x0D, veh->cur_order_index);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   279
						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
   280
						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
   281
						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
   282
						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
   283
						veh_prop(0x14, veh->service_interval);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   284
						veh_prop(0x15, veh->service_interval & 0xFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   285
						veh_prop(0x16, veh->last_station_visited);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   286
						veh_prop(0x17, veh->tick_counter);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   287
						veh_prop(0x18, veh->max_speed);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   288
						veh_prop(0x19, veh->max_speed & 0xFF);
3300
073fe603d5ba (svn r4039) - NewGRF: Add support for a few more vehicle variables. These are sometimes used for animation of sprites.
peter1138
parents: 3157
diff changeset
   289
						veh_prop(0x1A, veh->x_pos);
073fe603d5ba (svn r4039) - NewGRF: Add support for a few more vehicle variables. These are sometimes used for animation of sprites.
peter1138
parents: 3157
diff changeset
   290
						veh_prop(0x1B, veh->x_pos & 0xFF);
073fe603d5ba (svn r4039) - NewGRF: Add support for a few more vehicle variables. These are sometimes used for animation of sprites.
peter1138
parents: 3157
diff changeset
   291
						veh_prop(0x1C, veh->y_pos);
073fe603d5ba (svn r4039) - NewGRF: Add support for a few more vehicle variables. These are sometimes used for animation of sprites.
peter1138
parents: 3157
diff changeset
   292
						veh_prop(0x1D, veh->y_pos & 0xFF);
073fe603d5ba (svn r4039) - NewGRF: Add support for a few more vehicle variables. These are sometimes used for animation of sprites.
peter1138
parents: 3157
diff changeset
   293
						veh_prop(0x1E, veh->z_pos);
426
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   294
						veh_prop(0x1F, veh->direction);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   295
						veh_prop(0x28, veh->cur_image);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   296
						veh_prop(0x29, veh->cur_image & 0xFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   297
						veh_prop(0x32, veh->vehstatus);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   298
						veh_prop(0x33, veh->vehstatus);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   299
						veh_prop(0x34, veh->cur_speed);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   300
						veh_prop(0x35, veh->cur_speed & 0xFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   301
						veh_prop(0x36, veh->subspeed);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   302
						veh_prop(0x37, veh->acceleration);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   303
						veh_prop(0x39, veh->cargo_type);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   304
						veh_prop(0x3A, veh->cargo_cap);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   305
						veh_prop(0x3B, veh->cargo_cap & 0xFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   306
						veh_prop(0x3C, veh->cargo_count);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   307
						veh_prop(0x3D, veh->cargo_count & 0xFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   308
						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
   309
						veh_prop(0x3F, veh->cargo_days);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   310
						veh_prop(0x40, veh->age);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   311
						veh_prop(0x41, veh->age & 0xFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   312
						veh_prop(0x42, veh->max_age);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   313
						veh_prop(0x43, veh->max_age & 0xFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   314
						veh_prop(0x44, veh->build_year);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   315
						veh_prop(0x45, veh->unitnumber);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   316
						veh_prop(0x46, veh->engine_type);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   317
						veh_prop(0x47, veh->engine_type & 0xFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   318
						veh_prop(0x48, veh->spritenum);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   319
						veh_prop(0x49, veh->day_counter);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   320
						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
   321
						veh_prop(0x4B, veh->breakdown_ctr);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   322
						veh_prop(0x4C, veh->breakdown_delay);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   323
						veh_prop(0x4D, veh->breakdown_chance);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   324
						veh_prop(0x4E, veh->reliability);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   325
						veh_prop(0x4F, veh->reliability & 0xFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   326
						veh_prop(0x50, veh->reliability_spd_dec);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   327
						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
   328
						veh_prop(0x52, veh->profit_this_year);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   329
						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
   330
						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
   331
						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
   332
						veh_prop(0x56, veh->profit_last_year);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   333
						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
   334
						veh_prop(0x58, veh->profit_last_year);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   335
						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
   336
						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
   337
						veh_prop(0x5C, veh->value);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   338
						veh_prop(0x5D, veh->value & 0xFFFFFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   339
						veh_prop(0x5E, veh->value & 0xFFFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   340
						veh_prop(0x5F, veh->value & 0xFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   341
						veh_prop(0x60, veh->string_id);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   342
						veh_prop(0x61, veh->string_id & 0xFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   343
3014
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   344
						veh_prop(0x72, 0); // XXX Refit cycle currently unsupported
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   345
						veh_prop(0x7A, veh->random_bits);
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   346
						veh_prop(0x7B, veh->waiting_triggers);
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   347
#undef veh_prop
426
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   348
3014
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   349
						// Handle vehicle specific properties.
1a60d4541d3c (svn r3594) - NewGRF: Add support for some vehicle specific properties and a give a debug message for unknown variables.
peter1138
parents: 2982
diff changeset
   350
						default: value = VehicleSpecificProperty(veh, dsg->variable - 0x80); break;
426
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   351
					}
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   352
				}
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   353
			}
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   354
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   355
			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
   356
			//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
   357
			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
   358
		}
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
   359
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
   360
		case SGT_RANDOMIZED: {
2242
512eae2cf006 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   361
			const 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
   362
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
   363
			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
   364
				/* 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
   365
				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
   366
				//debug("going for %p: %d", rsg->groups[0], rsg->groups[0].type);
2488
1e98c71e5f6a (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   367
				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
   368
			}
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
   369
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
   370
			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
   371
				/* 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
   372
				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
   373
					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
   374
			}
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
   375
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   376
			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
   377
		}
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   378
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   379
		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
   380
			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
   381
			return NULL;
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   382
	}
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   383
}
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   384
2477
225b2916fe2a (svn r3003) Change int, uint, uin16 and byte to EngineID where appropriate, plus some related changes (mostly casts)
tron
parents: 2469
diff changeset
   385
static const SpriteGroup *GetVehicleSpriteGroup(EngineID engine, const Vehicle *v)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   386
{
2242
512eae2cf006 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   387
	const SpriteGroup *group;
3344
fc86351d4641 (svn r4128) - CodeChange: Add proper semantics for CargoID for such variables instead of using the general byte-type.
Darkvater
parents: 3300
diff changeset
   388
	CargoID cargo = GC_PURCHASE;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   389
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
   390
	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
   391
		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
   392
		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
   393
	}
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
   394
2488
1e98c71e5f6a (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   395
	group = engine_custom_sprites[engine][cargo];
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
   396
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
   397
	if (v != NULL && v->type == VEH_Train) {
2242
512eae2cf006 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   398
		const SpriteGroup *overset = GetWagonOverrideSpriteSet(engine, v->u.rail.first_engine);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   399
1477
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   400
		if (overset != NULL) group = overset;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   401
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 114
diff changeset
   402
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
   403
	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
   404
}
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
   405
3157
3f35e2d9c8e3 (svn r3783) Replace further ints and magic numbers by Direction, DiagDirection and friends
tron
parents: 3014
diff changeset
   406
int GetCustomEngineSprite(EngineID engine, const Vehicle* v, Direction 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
   407
{
2242
512eae2cf006 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   408
	const SpriteGroup *group;
512eae2cf006 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   409
	const RealSpriteGroup *rsg;
3348
0d0442be01d4 (svn r4132) - Missed 3 CargoID's
Darkvater
parents: 3344
diff changeset
   410
	CargoID 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
   411
	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
   412
	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
   413
	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
   414
	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
   415
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
   416
	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
   417
		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
   418
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
   419
		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
   420
		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
   421
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
   422
		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
   423
		loaded = (v->cargo_count * 100) / capacity;
2830
655a34aef536 (svn r3378) - NewGRF Fix: Use order status to determine whether a vehicle is loading instead
peter1138
parents: 2817
diff changeset
   424
655a34aef536 (svn r3378) - NewGRF Fix: Use order status to determine whether a vehicle is loading instead
peter1138
parents: 2817
diff changeset
   425
		if (v->type == VEH_Train) {
655a34aef536 (svn r3378) - NewGRF Fix: Use order status to determine whether a vehicle is loading instead
peter1138
parents: 2817
diff changeset
   426
			in_motion = GetFirstVehicleInChain(v)->current_order.type != OT_LOADING;
655a34aef536 (svn r3378) - NewGRF Fix: Use order status to determine whether a vehicle is loading instead
peter1138
parents: 2817
diff changeset
   427
		} else {
655a34aef536 (svn r3378) - NewGRF Fix: Use order status to determine whether a vehicle is loading instead
peter1138
parents: 2817
diff changeset
   428
			in_motion = v->current_order.type != OT_LOADING;
655a34aef536 (svn r3378) - NewGRF Fix: Use order status to determine whether a vehicle is loading instead
peter1138
parents: 2817
diff changeset
   429
		}
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
   430
	}
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
   431
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
   432
	group = GetVehicleSpriteGroup(engine, v);
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   433
	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
   434
2488
1e98c71e5f6a (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   435
	if (group == NULL && 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
   436
		// This group is empty but perhaps there'll be a default one.
2488
1e98c71e5f6a (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   437
		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
   438
		                                (resolve_callback) ResolveVehicleSpriteGroup);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   439
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   440
2488
1e98c71e5f6a (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   441
	if (group == NULL)
1e98c71e5f6a (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   442
		return 0;
1e98c71e5f6a (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   443
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   444
	assert(group->type == SGT_REAL);
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   445
	rsg = &group->g.real;
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   446
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
   447
	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
   448
		// 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
   449
		// look up the sprite number in _engine_original_sprites.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   450
		return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   451
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   452
1988
c23f5c7139bb (svn r2494) - Fix: [newgrf] Dont assume a spriteset can only have 4 or 8 sprites.
hackykid
parents: 1962
diff changeset
   453
	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
   454
	direction %= rsg->sprites_per_set;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   455
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
   456
	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
   457
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   458
	// 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
   459
	// empty and totally full vehicles. --pasky
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   460
	if (loaded == 100 || totalsets == 1) { // full
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   461
		spriteset = totalsets - 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   462
	} else if (loaded == 0 || totalsets == 2) { // empty
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   463
		spriteset = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   464
	} else { // something inbetween
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   465
		spriteset = loaded * (totalsets - 2) / 100 + 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   466
		// correct possible rounding errors
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   467
		if (!spriteset)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   468
			spriteset = 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   469
		else if (spriteset == totalsets - 1)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   470
			spriteset--;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   471
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   472
2489
6fbb72b64e07 (svn r3015) -NewGRF: Add a new sprite group result type, and support loading of callbacks results within random and "real" groups. (Not fully supported yet.)
peter1138
parents: 2488
diff changeset
   473
	r = (in_motion ? rsg->loaded[spriteset]->g.result.result : rsg->loading[spriteset]->g.result.result) + direction;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   474
	return r;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   475
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   476
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   477
/**
1908
2fa391fed79a (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1904
diff changeset
   478
 * 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
   479
 * @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
   480
 * @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
   481
 */
2548
49c8a096033f (svn r3077) static, const, bracing, indentation, 0 -> '\0'/NULL, typos in comments, excess empty lines, minor other changes
tron
parents: 2530
diff changeset
   482
bool UsesWagonOverride(const Vehicle* v)
49c8a096033f (svn r3077) static, const, bracing, indentation, 0 -> '\0'/NULL, typos in comments, excess empty lines, minor other changes
tron
parents: 2530
diff changeset
   483
{
1908
2fa391fed79a (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1904
diff changeset
   484
	assert(v->type == VEH_Train);
2242
512eae2cf006 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   485
	return GetWagonOverrideSpriteSet(v->engine_type, v->u.rail.first_engine) != NULL;
1908
2fa391fed79a (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1904
diff changeset
   486
}
2fa391fed79a (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1904
diff changeset
   487
2fa391fed79a (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1904
diff changeset
   488
/**
3390
ae4b0872dc78 (svn r4198) - NewGRF: Rename GetCallBackResult() to GetVehicleCallback(), as other types will exist later, and use separate parameters instead of bitshifting.
peter1138
parents: 3348
diff changeset
   489
 * Evaluate a newgrf callback for vehicles
ae4b0872dc78 (svn r4198) - NewGRF: Rename GetCallBackResult() to GetVehicleCallback(), as other types will exist later, and use separate parameters instead of bitshifting.
peter1138
parents: 3348
diff changeset
   490
 * @param callback The callback to evalute
ae4b0872dc78 (svn r4198) - NewGRF: Rename GetCallBackResult() to GetVehicleCallback(), as other types will exist later, and use separate parameters instead of bitshifting.
peter1138
parents: 3348
diff changeset
   491
 * @param param1   First parameter of the callback
ae4b0872dc78 (svn r4198) - NewGRF: Rename GetCallBackResult() to GetVehicleCallback(), as other types will exist later, and use separate parameters instead of bitshifting.
peter1138
parents: 3348
diff changeset
   492
 * @param param2   Second parameter of the callback
ae4b0872dc78 (svn r4198) - NewGRF: Rename GetCallBackResult() to GetVehicleCallback(), as other types will exist later, and use separate parameters instead of bitshifting.
peter1138
parents: 3348
diff changeset
   493
 * @param engine   Engine type of the vehicle to evaluate the callback for
ae4b0872dc78 (svn r4198) - NewGRF: Rename GetCallBackResult() to GetVehicleCallback(), as other types will exist later, and use separate parameters instead of bitshifting.
peter1138
parents: 3348
diff changeset
   494
 * @param vehicle  The vehicle to evaluate the callback for, or NULL if it doesnt exist yet
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   495
 * @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
   496
 */
3390
ae4b0872dc78 (svn r4198) - NewGRF: Rename GetCallBackResult() to GetVehicleCallback(), as other types will exist later, and use separate parameters instead of bitshifting.
peter1138
parents: 3348
diff changeset
   497
uint16 GetVehicleCallback(byte callback, uint32 param1, uint32 param2, EngineID engine, const Vehicle *v)
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   498
{
2242
512eae2cf006 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   499
	const SpriteGroup *group;
3607
45157f043dec (svn r4500) - NewGRF: When running a callback with no vehicle, use the purchase list 'cargo' type first, and then fallback to the default if needed.
peter1138
parents: 3604
diff changeset
   500
	CargoID cargo;
3390
ae4b0872dc78 (svn r4198) - NewGRF: Rename GetCallBackResult() to GetVehicleCallback(), as other types will exist later, and use separate parameters instead of bitshifting.
peter1138
parents: 3348
diff changeset
   501
	uint16 callback_info = callback | (param1 << 8); // XXX Temporary conversion between new and old format.
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   502
3607
45157f043dec (svn r4500) - NewGRF: When running a callback with no vehicle, use the purchase list 'cargo' type first, and then fallback to the default if needed.
peter1138
parents: 3604
diff changeset
   503
	cargo = (v == NULL) ? GC_PURCHASE : _global_cargo_id[_opt.landscape][v->cargo_type];
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   504
2488
1e98c71e5f6a (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   505
	group = engine_custom_sprites[engine][cargo];
1904
6459332eded4 (svn r2410) - Fix: [newgrf] When resolving callbacks, dont ignore wagon overrides.
hackykid
parents: 1891
diff changeset
   506
6459332eded4 (svn r2410) - Fix: [newgrf] When resolving callbacks, dont ignore wagon overrides.
hackykid
parents: 1891
diff changeset
   507
	if (v != NULL && v->type == VEH_Train) {
2242
512eae2cf006 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   508
		const SpriteGroup *overset = GetWagonOverrideSpriteSet(engine, v->u.rail.first_engine);
1904
6459332eded4 (svn r2410) - Fix: [newgrf] When resolving callbacks, dont ignore wagon overrides.
hackykid
parents: 1891
diff changeset
   509
6459332eded4 (svn r2410) - Fix: [newgrf] When resolving callbacks, dont ignore wagon overrides.
hackykid
parents: 1891
diff changeset
   510
		if (overset != NULL) group = overset;
6459332eded4 (svn r2410) - Fix: [newgrf] When resolving callbacks, dont ignore wagon overrides.
hackykid
parents: 1891
diff changeset
   511
	}
6459332eded4 (svn r2410) - Fix: [newgrf] When resolving callbacks, dont ignore wagon overrides.
hackykid
parents: 1891
diff changeset
   512
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   513
	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
   514
3607
45157f043dec (svn r4500) - NewGRF: When running a callback with no vehicle, use the purchase list 'cargo' type first, and then fallback to the default if needed.
peter1138
parents: 3604
diff changeset
   515
	if ((group == NULL || group->type != SGT_CALLBACK) && cargo != GC_DEFAULT) {
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   516
		// This group is empty but perhaps there'll be a default one.
2488
1e98c71e5f6a (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   517
		group = ResolveVehicleSpriteGroup(engine_custom_sprites[engine][GC_DEFAULT], v, callback_info,
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   518
		                                (resolve_callback) ResolveVehicleSpriteGroup);
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   519
	}
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   520
2488
1e98c71e5f6a (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   521
	if (group == NULL || group->type != SGT_CALLBACK)
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   522
		return CALLBACK_FAILED;
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   523
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   524
	return group->g.callback.result;
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   525
}
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   526
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   527
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   528
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
   529
// 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
   530
// 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
   531
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
   532
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
   533
2242
512eae2cf006 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   534
static const SpriteGroup *TriggerVehicleSpriteGroup(const SpriteGroup *spritegroup,
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   535
	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
   536
{
2488
1e98c71e5f6a (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   537
	if (spritegroup == NULL)
1e98c71e5f6a (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   538
		return NULL;
1e98c71e5f6a (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   539
1477
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   540
	if (spritegroup->type == SGT_RANDOMIZED) {
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   541
		_vsg_bits_to_reseed |= RandomizedSpriteGroupTriggeredBits(
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   542
			&spritegroup->g.random,
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   543
			_vsg_random_triggers,
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   544
			&veh->waiting_triggers
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   545
		);
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
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
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   548
	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
   549
}
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
1477
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   551
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
   552
{
2242
512eae2cf006 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   553
	const SpriteGroup *group;
512eae2cf006 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   554
	const 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
   555
	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
   556
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
   557
	_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
   558
	_vsg_bits_to_reseed = 0;
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   559
	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
   560
	                                  (resolve_callback) TriggerVehicleSpriteGroup);
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   561
2488
1e98c71e5f6a (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   562
	if (group == NULL && 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
   563
		// This group turned out to be empty but perhaps there'll be a default one.
2488
1e98c71e5f6a (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   564
		group = TriggerVehicleSpriteGroup(engine_custom_sprites[veh->engine_type][GC_DEFAULT], veh, 0,
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   565
		                                  (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
   566
	}
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   567
2488
1e98c71e5f6a (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   568
	if (group == NULL)
1e98c71e5f6a (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   569
		return;
1e98c71e5f6a (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   570
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   571
	assert(group->type == SGT_REAL);
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   572
	rsg = &group->g.real;
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   573
542
de27e74b11bd (svn r939) -Fix: Fixed compiler errors
truelight
parents: 507
diff changeset
   574
	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
   575
	veh->random_bits &= ~_vsg_bits_to_reseed;
542
de27e74b11bd (svn r939) -Fix: Fixed compiler errors
truelight
parents: 507
diff changeset
   576
	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
   577
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
   578
	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
   579
		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
   580
			/* 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
   581
			 * 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
   582
			/* 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
   583
			 * 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
   584
			 * 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
   585
			 * 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
   586
			 * 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
   587
			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
   588
			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
   589
			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
   590
		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
   591
			/* 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
   592
			 * 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
   593
			 * 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
   594
			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
   595
				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
   596
			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
   597
		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
   598
			/* 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
   599
			 * 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
   600
			 * 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
   601
			 * 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
   602
			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
   603
				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
   604
			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
   605
		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
   606
			/* 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
   607
			 * 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
   608
			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
   609
			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
   610
				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
   611
			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
   612
	}
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
   613
}
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
   614
1477
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   615
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
   616
{
2589
3640c75148f2 (svn r3126) Autoreplace: Only the first vehicle in a chain should be marked as entering a depot, so call moved out of DoVehicleTrigger loop.
peter1138
parents: 2574
diff changeset
   617
	if (trigger == VEHICLE_TRIGGER_DEPOT) {
3640c75148f2 (svn r3126) Autoreplace: Only the first vehicle in a chain should be marked as entering a depot, so call moved out of DoVehicleTrigger loop.
peter1138
parents: 2574
diff changeset
   618
		// store that the vehicle entered a depot this tick
3640c75148f2 (svn r3126) Autoreplace: Only the first vehicle in a chain should be marked as entering a depot, so call moved out of DoVehicleTrigger loop.
peter1138
parents: 2574
diff changeset
   619
		VehicleEnteredDepotThisTick(veh);
3640c75148f2 (svn r3126) Autoreplace: Only the first vehicle in a chain should be marked as entering a depot, so call moved out of DoVehicleTrigger loop.
peter1138
parents: 2574
diff changeset
   620
	}
3640c75148f2 (svn r3126) Autoreplace: Only the first vehicle in a chain should be marked as entering a depot, so call moved out of DoVehicleTrigger loop.
peter1138
parents: 2574
diff changeset
   621
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
   622
	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
   623
}
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
   624
3604
1e7d9f584f93 (svn r4496) - NewGRF: switch custom engine names from storing a char* to using the new StringID based text system. Vehicle name
peter1138
parents: 3595
diff changeset
   625
StringID _engine_custom_names[TOTAL_NUM_ENGINES];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   626
3604
1e7d9f584f93 (svn r4496) - NewGRF: switch custom engine names from storing a char* to using the new StringID based text system. Vehicle name
peter1138
parents: 3595
diff changeset
   627
void SetCustomEngineName(EngineID engine, StringID name)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   628
{
3614
c697c69b6363 (svn r4510) - NewGRF bounds checking:
peter1138
parents: 3607
diff changeset
   629
	assert(engine < lengthof(_engine_custom_names));
3604
1e7d9f584f93 (svn r4496) - NewGRF: switch custom engine names from storing a char* to using the new StringID based text system. Vehicle name
peter1138
parents: 3595
diff changeset
   630
	_engine_custom_names[engine] = name;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   631
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   632
2769
eab49a86038e (svn r3316) - NewGRF: Unload engine names before loading grf files. This fixes names in climates where the engines don't get loaded. Renamed function to reflect its purpose.
peter1138
parents: 2763
diff changeset
   633
void UnloadCustomEngineNames(void)
1474
a26a21fa10ef (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
   634
{
3604
1e7d9f584f93 (svn r4496) - NewGRF: switch custom engine names from storing a char* to using the new StringID based text system. Vehicle name
peter1138
parents: 3595
diff changeset
   635
	EngineID i;
1e7d9f584f93 (svn r4496) - NewGRF: switch custom engine names from storing a char* to using the new StringID based text system. Vehicle name
peter1138
parents: 3595
diff changeset
   636
	for (i = 0; i < TOTAL_NUM_ENGINES; i++) {
1e7d9f584f93 (svn r4496) - NewGRF: switch custom engine names from storing a char* to using the new StringID based text system. Vehicle name
peter1138
parents: 3595
diff changeset
   637
		_engine_custom_names[i] = 0;
1474
a26a21fa10ef (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
   638
	}
a26a21fa10ef (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
   639
}
a26a21fa10ef (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
   640
2477
225b2916fe2a (svn r3003) Change int, uint, uin16 and byte to EngineID where appropriate, plus some related changes (mostly casts)
tron
parents: 2469
diff changeset
   641
StringID GetCustomEngineName(EngineID engine)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   642
{
3604
1e7d9f584f93 (svn r4496) - NewGRF: switch custom engine names from storing a char* to using the new StringID based text system. Vehicle name
peter1138
parents: 3595
diff changeset
   643
	return _engine_custom_names[engine] == 0 ? _engine_name_strings[engine] : _engine_custom_names[engine];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   644
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   645
2971
cae3a81e8674 (svn r3546) - NewGRF feature: Implement rail vehicle 'property' 0x1A: allows shuffling order of rail vehicle purchase list (and replace vehicle list)
peter1138
parents: 2962
diff changeset
   646
// Functions for changing the order of vehicle purchase lists
cae3a81e8674 (svn r3546) - NewGRF feature: Implement rail vehicle 'property' 0x1A: allows shuffling order of rail vehicle purchase list (and replace vehicle list)
peter1138
parents: 2962
diff changeset
   647
// This is currently only implemented for rail vehicles.
cae3a81e8674 (svn r3546) - NewGRF feature: Implement rail vehicle 'property' 0x1A: allows shuffling order of rail vehicle purchase list (and replace vehicle list)
peter1138
parents: 2962
diff changeset
   648
static EngineID engine_list_order[NUM_TRAIN_ENGINES];
cae3a81e8674 (svn r3546) - NewGRF feature: Implement rail vehicle 'property' 0x1A: allows shuffling order of rail vehicle purchase list (and replace vehicle list)
peter1138
parents: 2962
diff changeset
   649
cae3a81e8674 (svn r3546) - NewGRF feature: Implement rail vehicle 'property' 0x1A: allows shuffling order of rail vehicle purchase list (and replace vehicle list)
peter1138
parents: 2962
diff changeset
   650
void ResetEngineListOrder(void)
cae3a81e8674 (svn r3546) - NewGRF feature: Implement rail vehicle 'property' 0x1A: allows shuffling order of rail vehicle purchase list (and replace vehicle list)
peter1138
parents: 2962
diff changeset
   651
{
cae3a81e8674 (svn r3546) - NewGRF feature: Implement rail vehicle 'property' 0x1A: allows shuffling order of rail vehicle purchase list (and replace vehicle list)
peter1138
parents: 2962
diff changeset
   652
	EngineID i;
cae3a81e8674 (svn r3546) - NewGRF feature: Implement rail vehicle 'property' 0x1A: allows shuffling order of rail vehicle purchase list (and replace vehicle list)
peter1138
parents: 2962
diff changeset
   653
cae3a81e8674 (svn r3546) - NewGRF feature: Implement rail vehicle 'property' 0x1A: allows shuffling order of rail vehicle purchase list (and replace vehicle list)
peter1138
parents: 2962
diff changeset
   654
	for (i = 0; i < NUM_TRAIN_ENGINES; i++)
cae3a81e8674 (svn r3546) - NewGRF feature: Implement rail vehicle 'property' 0x1A: allows shuffling order of rail vehicle purchase list (and replace vehicle list)
peter1138
parents: 2962
diff changeset
   655
		engine_list_order[i] = i;
cae3a81e8674 (svn r3546) - NewGRF feature: Implement rail vehicle 'property' 0x1A: allows shuffling order of rail vehicle purchase list (and replace vehicle list)
peter1138
parents: 2962
diff changeset
   656
}
cae3a81e8674 (svn r3546) - NewGRF feature: Implement rail vehicle 'property' 0x1A: allows shuffling order of rail vehicle purchase list (and replace vehicle list)
peter1138
parents: 2962
diff changeset
   657
cae3a81e8674 (svn r3546) - NewGRF feature: Implement rail vehicle 'property' 0x1A: allows shuffling order of rail vehicle purchase list (and replace vehicle list)
peter1138
parents: 2962
diff changeset
   658
EngineID GetRailVehAtPosition(EngineID pos)
cae3a81e8674 (svn r3546) - NewGRF feature: Implement rail vehicle 'property' 0x1A: allows shuffling order of rail vehicle purchase list (and replace vehicle list)
peter1138
parents: 2962
diff changeset
   659
{
cae3a81e8674 (svn r3546) - NewGRF feature: Implement rail vehicle 'property' 0x1A: allows shuffling order of rail vehicle purchase list (and replace vehicle list)
peter1138
parents: 2962
diff changeset
   660
	return engine_list_order[pos];
cae3a81e8674 (svn r3546) - NewGRF feature: Implement rail vehicle 'property' 0x1A: allows shuffling order of rail vehicle purchase list (and replace vehicle list)
peter1138
parents: 2962
diff changeset
   661
}
cae3a81e8674 (svn r3546) - NewGRF feature: Implement rail vehicle 'property' 0x1A: allows shuffling order of rail vehicle purchase list (and replace vehicle list)
peter1138
parents: 2962
diff changeset
   662
cae3a81e8674 (svn r3546) - NewGRF feature: Implement rail vehicle 'property' 0x1A: allows shuffling order of rail vehicle purchase list (and replace vehicle list)
peter1138
parents: 2962
diff changeset
   663
void AlterRailVehListOrder(EngineID engine, EngineID target)
cae3a81e8674 (svn r3546) - NewGRF feature: Implement rail vehicle 'property' 0x1A: allows shuffling order of rail vehicle purchase list (and replace vehicle list)
peter1138
parents: 2962
diff changeset
   664
{
cae3a81e8674 (svn r3546) - NewGRF feature: Implement rail vehicle 'property' 0x1A: allows shuffling order of rail vehicle purchase list (and replace vehicle list)
peter1138
parents: 2962
diff changeset
   665
	EngineID i;
cae3a81e8674 (svn r3546) - NewGRF feature: Implement rail vehicle 'property' 0x1A: allows shuffling order of rail vehicle purchase list (and replace vehicle list)
peter1138
parents: 2962
diff changeset
   666
	bool moving = false;
cae3a81e8674 (svn r3546) - NewGRF feature: Implement rail vehicle 'property' 0x1A: allows shuffling order of rail vehicle purchase list (and replace vehicle list)
peter1138
parents: 2962
diff changeset
   667
cae3a81e8674 (svn r3546) - NewGRF feature: Implement rail vehicle 'property' 0x1A: allows shuffling order of rail vehicle purchase list (and replace vehicle list)
peter1138
parents: 2962
diff changeset
   668
	if (engine == target) return;
cae3a81e8674 (svn r3546) - NewGRF feature: Implement rail vehicle 'property' 0x1A: allows shuffling order of rail vehicle purchase list (and replace vehicle list)
peter1138
parents: 2962
diff changeset
   669
cae3a81e8674 (svn r3546) - NewGRF feature: Implement rail vehicle 'property' 0x1A: allows shuffling order of rail vehicle purchase list (and replace vehicle list)
peter1138
parents: 2962
diff changeset
   670
	// First, remove our ID from the list.
cae3a81e8674 (svn r3546) - NewGRF feature: Implement rail vehicle 'property' 0x1A: allows shuffling order of rail vehicle purchase list (and replace vehicle list)
peter1138
parents: 2962
diff changeset
   671
	for (i = 0; i < NUM_TRAIN_ENGINES - 1; i++) {
cae3a81e8674 (svn r3546) - NewGRF feature: Implement rail vehicle 'property' 0x1A: allows shuffling order of rail vehicle purchase list (and replace vehicle list)
peter1138
parents: 2962
diff changeset
   672
		if (engine_list_order[i] == engine)
cae3a81e8674 (svn r3546) - NewGRF feature: Implement rail vehicle 'property' 0x1A: allows shuffling order of rail vehicle purchase list (and replace vehicle list)
peter1138
parents: 2962
diff changeset
   673
			moving = true;
cae3a81e8674 (svn r3546) - NewGRF feature: Implement rail vehicle 'property' 0x1A: allows shuffling order of rail vehicle purchase list (and replace vehicle list)
peter1138
parents: 2962
diff changeset
   674
		if (moving)
cae3a81e8674 (svn r3546) - NewGRF feature: Implement rail vehicle 'property' 0x1A: allows shuffling order of rail vehicle purchase list (and replace vehicle list)
peter1138
parents: 2962
diff changeset
   675
			engine_list_order[i] = engine_list_order[i + 1];
cae3a81e8674 (svn r3546) - NewGRF feature: Implement rail vehicle 'property' 0x1A: allows shuffling order of rail vehicle purchase list (and replace vehicle list)
peter1138
parents: 2962
diff changeset
   676
	}
cae3a81e8674 (svn r3546) - NewGRF feature: Implement rail vehicle 'property' 0x1A: allows shuffling order of rail vehicle purchase list (and replace vehicle list)
peter1138
parents: 2962
diff changeset
   677
cae3a81e8674 (svn r3546) - NewGRF feature: Implement rail vehicle 'property' 0x1A: allows shuffling order of rail vehicle purchase list (and replace vehicle list)
peter1138
parents: 2962
diff changeset
   678
	// Now, insert it again, before the target engine.
cae3a81e8674 (svn r3546) - NewGRF feature: Implement rail vehicle 'property' 0x1A: allows shuffling order of rail vehicle purchase list (and replace vehicle list)
peter1138
parents: 2962
diff changeset
   679
	for (i = NUM_TRAIN_ENGINES - 1; i > 0; i--) {
cae3a81e8674 (svn r3546) - NewGRF feature: Implement rail vehicle 'property' 0x1A: allows shuffling order of rail vehicle purchase list (and replace vehicle list)
peter1138
parents: 2962
diff changeset
   680
		engine_list_order[i] = engine_list_order[i - 1];
cae3a81e8674 (svn r3546) - NewGRF feature: Implement rail vehicle 'property' 0x1A: allows shuffling order of rail vehicle purchase list (and replace vehicle list)
peter1138
parents: 2962
diff changeset
   681
		if (engine_list_order[i] == target) {
cae3a81e8674 (svn r3546) - NewGRF feature: Implement rail vehicle 'property' 0x1A: allows shuffling order of rail vehicle purchase list (and replace vehicle list)
peter1138
parents: 2962
diff changeset
   682
			engine_list_order[i - 1] = engine;
cae3a81e8674 (svn r3546) - NewGRF feature: Implement rail vehicle 'property' 0x1A: allows shuffling order of rail vehicle purchase list (and replace vehicle list)
peter1138
parents: 2962
diff changeset
   683
			break;
cae3a81e8674 (svn r3546) - NewGRF feature: Implement rail vehicle 'property' 0x1A: allows shuffling order of rail vehicle purchase list (and replace vehicle list)
peter1138
parents: 2962
diff changeset
   684
		}
cae3a81e8674 (svn r3546) - NewGRF feature: Implement rail vehicle 'property' 0x1A: allows shuffling order of rail vehicle purchase list (and replace vehicle list)
peter1138
parents: 2962
diff changeset
   685
	}
cae3a81e8674 (svn r3546) - NewGRF feature: Implement rail vehicle 'property' 0x1A: allows shuffling order of rail vehicle purchase list (and replace vehicle list)
peter1138
parents: 2962
diff changeset
   686
}