newgrf_engine.c
author truelight
Tue, 21 Feb 2006 19:42:42 +0000
changeset 3062 2c96a73da934
parent 3014 1a60d4541d3c
child 3157 3f35e2d9c8e3
permissions -rw-r--r--
(svn r3651) -Fix: r3646 fixed some lame compiler, but broke others... this fixes it for some more compilers
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;
2490
0eac698c33cf (svn r3016) -NewGRF: Implement the start of reference counting for sprite groups. This will allow unloading.
peter1138
parents: 2489
diff changeset
    47
	group->ref_count++;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    48
	wo->trains = trains;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    49
	wo->train_id = malloc(trains);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    50
	memcpy(wo->train_id, train_id, trains);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    51
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    52
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
    53
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
    54
{
2242
512eae2cf006 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
    55
	const WagonOverrides *wos = &_engine_wagon_overrides[engine];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    56
	int i;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    57
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
    58
	// 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
    59
	// 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
    60
	// 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
    61
	// that. --pasky
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    62
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    63
	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
    64
		const WagonOverride *wo = &wos->overrides[i];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    65
		int j;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    66
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    67
		for (j = 0; j < wo->trains; j++) {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    68
			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
    69
				return wo->group;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    70
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    71
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    72
	return NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    73
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    74
2491
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
    75
/**
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
    76
 * Unload all wagon override sprite groups.
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
    77
 */
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
    78
void UnloadWagonOverrides(void)
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
    79
{
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
    80
	WagonOverrides *wos;
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
    81
	WagonOverride *wo;
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
    82
	EngineID engine;
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
    83
	int i;
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
    84
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
    85
	for (engine = 0; engine < TOTAL_NUM_ENGINES; engine++) {
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
    86
		wos = &_engine_wagon_overrides[engine];
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
    87
		for (i = 0; i < wos->overrides_count; i++) {
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
    88
			wo = &wos->overrides[i];
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
    89
			UnloadSpriteGroup(&wo->group);
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
    90
			free(wo->train_id);
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
    91
		}
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
    92
		free(wos->overrides);
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
    93
		wos->overrides_count = 0;
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
    94
		wos->overrides = NULL;
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
    95
	}
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
    96
}
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    97
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    98
// 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
    99
// (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
   100
// 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
   101
// 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
   102
static SpriteGroup *engine_custom_sprites[TOTAL_NUM_ENGINES][NUM_GLOBAL_CID];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   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
{
2491
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   106
	if (engine_custom_sprites[engine][cargo] != NULL) {
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   107
		DEBUG(grf, 6)("SetCustomEngineSprites: engine `%d' cargo `%d' already has group -- removing.", engine, cargo);
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   108
		UnloadSpriteGroup(&engine_custom_sprites[engine][cargo]);
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;
2490
0eac698c33cf (svn r3016) -NewGRF: Implement the start of reference counting for sprite groups. This will allow unloading.
peter1138
parents: 2489
diff changeset
   111
	group->ref_count++;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   112
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   113
2491
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   114
/**
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   115
 * Unload all engine sprite groups.
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   116
 */
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   117
void UnloadCustomEngineSprites(void)
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   118
{
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   119
	EngineID engine;
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   120
	CargoID cargo;
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   121
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   122
	for (engine = 0; engine < TOTAL_NUM_ENGINES; engine++) {
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   123
		for (cargo = 0; cargo < NUM_GLOBAL_CID; cargo++) {
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   124
			if (engine_custom_sprites[engine][cargo] != NULL) {
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   125
				DEBUG(grf, 6)("UnloadCustomEngineSprites: Unloading group for engine `%d' cargo `%d'.", engine, cargo);
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   126
				UnloadSpriteGroup(&engine_custom_sprites[engine][cargo]);
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
		}
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   129
	}
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   130
}
15a117630a4f (svn r3017) -NewGRF: Implement sprite group unreferencing and unloading.
peter1138
parents: 2490
diff changeset
   131
2708
48714bad9499 (svn r3252) - NewGRF: Map new train subtypes to old types for rail vehicles.
peter1138
parents: 2639
diff changeset
   132
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
   133
{
48714bad9499 (svn r3252) - NewGRF: Map new train subtypes to old types for rail vehicles.
peter1138
parents: 2639
diff changeset
   134
	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
   135
	if (IsTrainEngine(v)) return 0;
48714bad9499 (svn r3252) - NewGRF: Map new train subtypes to old types for rail vehicles.
peter1138
parents: 2639
diff changeset
   136
	if (IsFreeWagon(v)) return 4;
48714bad9499 (svn r3252) - NewGRF: Map new train subtypes to old types for rail vehicles.
peter1138
parents: 2639
diff changeset
   137
	return 2;
48714bad9499 (svn r3252) - NewGRF: Map new train subtypes to old types for rail vehicles.
peter1138
parents: 2639
diff changeset
   138
}
48714bad9499 (svn r3252) - NewGRF: Map new train subtypes to old types for rail vehicles.
peter1138
parents: 2639
diff changeset
   139
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
   140
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
   141
	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
   142
		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
   143
			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
   144
				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
   145
				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
   146
				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
   147
				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
   148
				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
   149
				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
   150
				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
   151
				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
   152
				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
   153
			}
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
			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
   155
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 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
   157
			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
   158
				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
   159
				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
   160
				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
   161
				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
   162
				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
   163
				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
   164
				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
   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
			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
   167
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 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
   169
			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
   170
				// 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
   171
				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
   172
				// 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
   173
			}
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
			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
   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
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
	DEBUG(grf, 1)("Unhandled vehicle property 0x%x (var 0x%x), type 0x%x", var, var + 0x80, 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
   178
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
	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
   180
}
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
2242
512eae2cf006 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   182
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
   183
	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
   184
2242
512eae2cf006 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   185
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
   186
	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
   187
{
2488
1e98c71e5f6a (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   188
	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
   189
		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
   190
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
   191
	//debug("spgt %d", spritegroup->type);
426
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   192
	switch (spritegroup->type) {
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   193
		case SGT_REAL:
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   194
		case SGT_CALLBACK:
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   195
			return spritegroup;
426
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   196
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   197
		case SGT_DETERMINISTIC: {
2242
512eae2cf006 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   198
			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
   199
			const SpriteGroup *target;
426
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   200
			int value = -1;
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   201
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
   202
			//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
   203
			if (dsg->variable == 0x0C) {
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   204
				/* Callback ID */
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   205
				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
   206
			} 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
   207
				value = (callback_info >> 8) & 0xFF;
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   208
			} else if ((dsg->variable >> 6) == 0) {
426
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   209
				/* General property */
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   210
				value = GetDeterministicSpriteValue(dsg->variable);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   211
			} 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
   212
				/* 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
   213
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
   214
				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
   215
					/* 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
   216
					 * 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
   217
					 * 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
   218
					 * (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
   219
					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
   220
						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
   221
					} 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
   222
						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
   223
					}
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   224
					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
   225
				}
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
426
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   227
				if (dsg->var_scope == VSG_SCOPE_PARENT) {
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   228
					/* 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
   229
					if (veh->type == VEH_Train)
426
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   230
						veh = GetFirstVehicleInChain(veh);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   231
				}
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   232
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
   233
				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
   234
					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
   235
						const Vehicle *u = GetFirstVehicleInChain(veh);
426
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   236
						byte chain_before = 0, chain_after = 0;
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   237
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   238
						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
   239
							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
   240
							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
   241
								chain_before = 0;
426
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   242
							u = u->next;
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   243
						}
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
   244
						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
   245
							chain_after++;
426
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   246
							u = u->next;
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
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
   249
						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
   250
						        | (chain_before + chain_after) << 16;
426
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   251
					} else {
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   252
						value = 1; /* 1 vehicle in the chain */
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   253
					}
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   254
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   255
				} else {
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   256
					// TTDPatch runs on little-endian arch;
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   257
					// 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
   258
					switch (dsg->variable - 0x80) {
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 543
diff changeset
   259
#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
   260
						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
   261
						veh_prop(0x01, MapOldSubType(veh));
426
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   262
						veh_prop(0x04, veh->index);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   263
						veh_prop(0x05, veh->index & 0xFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   264
						/* XXX? Is THIS right? */
555
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 543
diff changeset
   265
						veh_prop(0x0A, PackOrder(&veh->current_order));
02df8a1b7f33 (svn r955) Replace uint16 for orders with struct Order
tron
parents: 543
diff changeset
   266
						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
   267
						veh_prop(0x0C, veh->num_orders);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   268
						veh_prop(0x0D, veh->cur_order_index);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   269
						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
   270
						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
   271
						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
   272
						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
   273
						veh_prop(0x14, veh->service_interval);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   274
						veh_prop(0x15, veh->service_interval & 0xFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   275
						veh_prop(0x16, veh->last_station_visited);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   276
						veh_prop(0x17, veh->tick_counter);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   277
						veh_prop(0x18, veh->max_speed);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   278
						veh_prop(0x19, veh->max_speed & 0xFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   279
						veh_prop(0x1F, veh->direction);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   280
						veh_prop(0x28, veh->cur_image);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   281
						veh_prop(0x29, veh->cur_image & 0xFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   282
						veh_prop(0x32, veh->vehstatus);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   283
						veh_prop(0x33, veh->vehstatus);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   284
						veh_prop(0x34, veh->cur_speed);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   285
						veh_prop(0x35, veh->cur_speed & 0xFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   286
						veh_prop(0x36, veh->subspeed);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   287
						veh_prop(0x37, veh->acceleration);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   288
						veh_prop(0x39, veh->cargo_type);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   289
						veh_prop(0x3A, veh->cargo_cap);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   290
						veh_prop(0x3B, veh->cargo_cap & 0xFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   291
						veh_prop(0x3C, veh->cargo_count);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   292
						veh_prop(0x3D, veh->cargo_count & 0xFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   293
						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
   294
						veh_prop(0x3F, veh->cargo_days);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   295
						veh_prop(0x40, veh->age);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   296
						veh_prop(0x41, veh->age & 0xFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   297
						veh_prop(0x42, veh->max_age);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   298
						veh_prop(0x43, veh->max_age & 0xFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   299
						veh_prop(0x44, veh->build_year);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   300
						veh_prop(0x45, veh->unitnumber);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   301
						veh_prop(0x46, veh->engine_type);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   302
						veh_prop(0x47, veh->engine_type & 0xFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   303
						veh_prop(0x48, veh->spritenum);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   304
						veh_prop(0x49, veh->day_counter);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   305
						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
   306
						veh_prop(0x4B, veh->breakdown_ctr);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   307
						veh_prop(0x4C, veh->breakdown_delay);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   308
						veh_prop(0x4D, veh->breakdown_chance);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   309
						veh_prop(0x4E, veh->reliability);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   310
						veh_prop(0x4F, veh->reliability & 0xFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   311
						veh_prop(0x50, veh->reliability_spd_dec);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   312
						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
   313
						veh_prop(0x52, veh->profit_this_year);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   314
						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
   315
						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
   316
						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
   317
						veh_prop(0x56, veh->profit_last_year);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   318
						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
   319
						veh_prop(0x58, veh->profit_last_year);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   320
						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
   321
						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
   322
						veh_prop(0x5C, veh->value);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   323
						veh_prop(0x5D, veh->value & 0xFFFFFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   324
						veh_prop(0x5E, veh->value & 0xFFFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   325
						veh_prop(0x5F, veh->value & 0xFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   326
						veh_prop(0x60, veh->string_id);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   327
						veh_prop(0x61, veh->string_id & 0xFF);
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   328
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
   329
						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
   330
						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
   331
						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
   332
#undef veh_prop
426
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   333
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
   334
						// 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
   335
						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
   336
					}
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   337
				}
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   338
			}
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   339
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   340
			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
   341
			//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
   342
			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
   343
		}
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
   344
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
   345
		case SGT_RANDOMIZED: {
2242
512eae2cf006 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   346
			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
   347
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
   348
			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
   349
				/* 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
   350
				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
   351
				//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
   352
				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
   353
			}
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
   354
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
   355
			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
   356
				/* 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
   357
				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
   358
					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
   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
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   361
			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
   362
		}
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   363
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   364
		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
   365
			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
   366
			return NULL;
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   367
	}
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   368
}
381172276dc6 (svn r625) Almost complete support for deterministic variational vehicle spritegroups. (pasky)
tron
parents: 414
diff changeset
   369
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
   370
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
   371
{
2242
512eae2cf006 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   372
	const SpriteGroup *group;
1802
da61740cc1e7 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1786
diff changeset
   373
	byte cargo = GC_PURCHASE;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   374
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
   375
	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
   376
		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
   377
		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
   378
	}
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
   379
2488
1e98c71e5f6a (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   380
	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
   381
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
   382
	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
   383
		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
   384
1477
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   385
		if (overset != NULL) group = overset;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   386
	}
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 114
diff changeset
   387
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
   388
	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
   389
}
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
   390
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
   391
int GetCustomEngineSprite(EngineID engine, const Vehicle *v, byte direction)
445
beafc0fb8f12 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   392
{
2242
512eae2cf006 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   393
	const SpriteGroup *group;
512eae2cf006 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   394
	const RealSpriteGroup *rsg;
1802
da61740cc1e7 (svn r2306) - CodeChange: Check the last commands; refits. This needed an extensive rewrite and global/local-cargo ID juggling and bitmasking. However with this done it looks better as well and is compatible with newgrf handling. Big thanks to HackyKid for doing most of the work. This also closes patch "[ 1199277 ] Command checks"
Darkvater
parents: 1786
diff changeset
   395
	byte cargo = GC_PURCHASE;
445
beafc0fb8f12 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   396
	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
   397
	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
   398
	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
   399
	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
   400
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
   401
	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
   402
		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
   403
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
		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
   405
		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
   406
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
		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
   408
		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
   409
655a34aef536 (svn r3378) - NewGRF Fix: Use order status to determine whether a vehicle is loading instead
peter1138
parents: 2817
diff changeset
   410
		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
   411
			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
   412
		} else {
655a34aef536 (svn r3378) - NewGRF Fix: Use order status to determine whether a vehicle is loading instead
peter1138
parents: 2817
diff changeset
   413
			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
   414
		}
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
   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
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
	group = GetVehicleSpriteGroup(engine, v);
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   418
	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
   419
2488
1e98c71e5f6a (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   420
	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
   421
		// 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
   422
		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
   423
		                                (resolve_callback) ResolveVehicleSpriteGroup);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   424
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   425
2488
1e98c71e5f6a (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   426
	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
   427
		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
   428
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   429
	assert(group->type == SGT_REAL);
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   430
	rsg = &group->g.real;
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   431
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
   432
	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
   433
		// 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
   434
		// look up the sprite number in _engine_original_sprites.
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   435
		return 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   436
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   437
1988
c23f5c7139bb (svn r2494) - Fix: [newgrf] Dont assume a spriteset can only have 4 or 8 sprites.
hackykid
parents: 1962
diff changeset
   438
	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
   439
	direction %= rsg->sprites_per_set;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   440
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
   441
	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
   442
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   443
	// 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
   444
	// empty and totally full vehicles. --pasky
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   445
	if (loaded == 100 || totalsets == 1) { // full
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   446
		spriteset = totalsets - 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   447
	} else if (loaded == 0 || totalsets == 2) { // empty
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   448
		spriteset = 0;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   449
	} else { // something inbetween
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   450
		spriteset = loaded * (totalsets - 2) / 100 + 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   451
		// correct possible rounding errors
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   452
		if (!spriteset)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   453
			spriteset = 1;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   454
		else if (spriteset == totalsets - 1)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   455
			spriteset--;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   456
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   457
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
   458
	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
   459
	return r;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   460
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   461
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   462
/**
1908
2fa391fed79a (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1904
diff changeset
   463
 * 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
   464
 * @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
   465
 * @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
   466
 */
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
   467
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
   468
{
1908
2fa391fed79a (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1904
diff changeset
   469
	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
   470
	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
   471
}
2fa391fed79a (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1904
diff changeset
   472
2fa391fed79a (svn r2414) - Feature: [newgrf] Implement powered wagons, and the callback that goes with it.
hackykid
parents: 1904
diff changeset
   473
/**
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   474
 * Evaluates a newgrf callback
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   475
 * @param callback_info info about which callback to evaluate
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   476
 *  (bit 0-7)  = CallBack id of the callback to use, see CallBackId enum
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   477
 *  (bit 8-15) = Other info some callbacks need to have, callback specific, see CallBackId enum, not used yet
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   478
 * @param engine Engine type of the vehicle to evaluate the callback for
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   479
 * @param vehicle The vehicle to evaluate the callback for, NULL if it doesnt exist (yet)
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   480
 * @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
   481
 */
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
   482
uint16 GetCallBackResult(uint16 callback_info, EngineID engine, const Vehicle *v)
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   483
{
2242
512eae2cf006 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   484
	const SpriteGroup *group;
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   485
	byte cargo = GC_DEFAULT;
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   486
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   487
	if (v != NULL)
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   488
		cargo = _global_cargo_id[_opt.landscape][v->cargo_type];
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   489
2488
1e98c71e5f6a (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   490
	group = engine_custom_sprites[engine][cargo];
1904
6459332eded4 (svn r2410) - Fix: [newgrf] When resolving callbacks, dont ignore wagon overrides.
hackykid
parents: 1891
diff changeset
   491
6459332eded4 (svn r2410) - Fix: [newgrf] When resolving callbacks, dont ignore wagon overrides.
hackykid
parents: 1891
diff changeset
   492
	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
   493
		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
   494
6459332eded4 (svn r2410) - Fix: [newgrf] When resolving callbacks, dont ignore wagon overrides.
hackykid
parents: 1891
diff changeset
   495
		if (overset != NULL) group = overset;
6459332eded4 (svn r2410) - Fix: [newgrf] When resolving callbacks, dont ignore wagon overrides.
hackykid
parents: 1891
diff changeset
   496
	}
6459332eded4 (svn r2410) - Fix: [newgrf] When resolving callbacks, dont ignore wagon overrides.
hackykid
parents: 1891
diff changeset
   497
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   498
	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
   499
2488
1e98c71e5f6a (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   500
	if (group == NULL && cargo != GC_DEFAULT) {
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   501
		// 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
   502
		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
   503
		                                (resolve_callback) ResolveVehicleSpriteGroup);
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   504
	}
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   505
2488
1e98c71e5f6a (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   506
	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
   507
		return CALLBACK_FAILED;
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   508
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   509
	return group->g.callback.result;
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   510
}
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   511
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   512
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   513
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
   514
// 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
   515
// 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
   516
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
   517
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
   518
2242
512eae2cf006 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   519
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
   520
	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
   521
{
2488
1e98c71e5f6a (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   522
	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
   523
		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
   524
1477
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   525
	if (spritegroup->type == SGT_RANDOMIZED) {
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   526
		_vsg_bits_to_reseed |= RandomizedSpriteGroupTriggeredBits(
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   527
			&spritegroup->g.random,
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   528
			_vsg_random_triggers,
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   529
			&veh->waiting_triggers
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   530
		);
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   531
	}
445
beafc0fb8f12 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   532
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   533
	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
   534
}
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
   535
1477
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   536
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
   537
{
2242
512eae2cf006 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   538
	const SpriteGroup *group;
512eae2cf006 (svn r2762) Simplify a loop, remove an unused variable and add many consts
tron
parents: 2204
diff changeset
   539
	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
   540
	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
   541
beafc0fb8f12 (svn r654) Hopefully complete support for randomized variational spritegroups (i.e. the cars transporter in DBSetXL gets different cars each time) (pasky)
tron
parents: 433
diff changeset
   542
	_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
   543
	_vsg_bits_to_reseed = 0;
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   544
	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
   545
	                                  (resolve_callback) TriggerVehicleSpriteGroup);
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   546
2488
1e98c71e5f6a (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   547
	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
   548
		// 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
   549
		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
   550
		                                  (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
   551
	}
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   552
2488
1e98c71e5f6a (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   553
	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
   554
		return;
1e98c71e5f6a (svn r3014) -NewGRF, Codechange: Make all sprite group references be pointers instead of copying the data around.
peter1138
parents: 2477
diff changeset
   555
1883
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   556
	assert(group->type == SGT_REAL);
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   557
	rsg = &group->g.real;
ad68cd0a0a25 (svn r2389) - Feature: [newgrf] Implement the mechanism for handling newgrf callbacks.
hackykid
parents: 1881
diff changeset
   558
542
de27e74b11bd (svn r939) -Fix: Fixed compiler errors
truelight
parents: 507
diff changeset
   559
	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
   560
	veh->random_bits &= ~_vsg_bits_to_reseed;
542
de27e74b11bd (svn r939) -Fix: Fixed compiler errors
truelight
parents: 507
diff changeset
   561
	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
   562
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
	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
   564
		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
   565
			/* 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
   566
			 * 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
   567
			/* 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
   568
			 * 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
   569
			 * 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
   570
			 * 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
   571
			 * 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
   572
			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
   573
			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
   574
			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
   575
		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
   576
			/* 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
   577
			 * 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
   578
			 * 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
   579
			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
   580
				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
   581
			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
   582
		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
   583
			/* 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
   584
			 * 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
   585
			 * 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
   586
			 * 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
   587
			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
   588
				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
   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_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
   591
			/* 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
   592
			 * 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
   593
			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
   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, 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
   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
	}
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
}
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
1477
9389baf2bf3c (svn r1981) Typedef some structs and enums
tron
parents: 1475
diff changeset
   600
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
   601
{
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
   602
	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
   603
		// 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
   604
		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
   605
	}
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
   606
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
   607
	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
   608
}
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
1474
a26a21fa10ef (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
   610
static char *_engine_custom_names[TOTAL_NUM_ENGINES];
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   611
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
   612
void SetCustomEngineName(EngineID engine, const char *name)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   613
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   614
	_engine_custom_names[engine] = strdup(name);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   615
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   616
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
   617
void UnloadCustomEngineNames(void)
1474
a26a21fa10ef (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
   618
{
a26a21fa10ef (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
   619
	char **i;
a26a21fa10ef (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
   620
	for (i = _engine_custom_names; i != endof(_engine_custom_names); i++) {
a26a21fa10ef (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
   621
		free(*i);
a26a21fa10ef (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
   622
		*i = NULL;
a26a21fa10ef (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
   623
	}
a26a21fa10ef (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
   624
}
a26a21fa10ef (svn r1978) - Fix: Plug some memleaks; thanks Valgrind
Darkvater
parents: 1329
diff changeset
   625
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
   626
StringID GetCustomEngineName(EngineID engine)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   627
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   628
	if (!_engine_custom_names[engine])
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   629
		return _engine_name_strings[engine];
2201
f240b3c7e2ec (svn r2717) Move _userstring to strings.[ch]
tron
parents: 2186
diff changeset
   630
	ttd_strlcpy(_userstring, _engine_custom_names[engine], lengthof(_userstring));
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   631
	return STR_SPEC_USERSTRING;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   632
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
   633
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
   634
// 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
   635
// 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
   636
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
   637
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
   638
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
   639
{
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
   640
	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
   641
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
   642
	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
   643
		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
   644
}
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
   645
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
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
   647
{
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
	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
   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
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
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
   652
{
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
	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
   654
	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
   655
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
	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
   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
	// 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
   659
	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
   660
		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
   661
			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
   662
		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
   663
			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
   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
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
	// 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
   667
	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
   668
		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
   669
		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
   670
			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
   671
			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
   672
		}
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
	}
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
}