author | darkvater |
Sun, 14 Nov 2004 18:18:28 +0000 | |
changeset 408 | 63a8c0505aab |
parent 405 | 6830ae7a0d5d |
child 410 | 8de2aaf20800 |
permissions | -rw-r--r-- |
0 | 1 |
#include "stdafx.h" |
2 |
#include "ttd.h" |
|
3 |
#include "engine.h" |
|
4 |
#include "table/engines.h" |
|
5 |
#include "player.h" |
|
6 |
#include "command.h" |
|
7 |
#include "vehicle.h" |
|
8 |
#include "news.h" |
|
9 |
#include "saveload.h" |
|
405
6830ae7a0d5d
(svn r602) -newgrf: Move DrawTileSeqStruct & co and struct SpriteGroup to sprite.h (pasky)
darkvater
parents:
369
diff
changeset
|
10 |
#include "sprite.h" |
0 | 11 |
|
12 |
#define UPDATE_PLAYER_RAILTYPE(e,p) if ((byte)(e->railtype + 1) > p->max_railtype) p->max_railtype = e->railtype + 1; |
|
13 |
||
14 |
enum { |
|
15 |
ENGINE_AVAILABLE = 1, |
|
16 |
ENGINE_INTRODUCING = 2, |
|
17 |
ENGINE_PREVIEWING = 4, |
|
18 |
}; |
|
19 |
||
20 |
/* This maps per-landscape cargo ids to globally unique cargo ids usable ie. in |
|
21 |
* the custom GRF files. It is basically just a transcribed table from |
|
22 |
* TTDPatch's newgrf.txt. */ |
|
23 |
byte _global_cargo_id[NUM_LANDSCAPE][NUM_CARGO] = { |
|
24 |
/* LT_NORMAL */ { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12 }, |
|
25 |
/* LT_HILLY */ { 0, 1, 2, 3, 4, 5, 6, 7, 28, 11, 10, 12 }, |
|
26 |
/* LT_DESERT */ { 0, 16, 2, 3, 13, 5, 6, 7, 14, 15, 10, 12 }, |
|
27 |
/* LT_CANDY */ { 0, 17, 2, 18, 19, 20, 21, 22, 23, 24, 25, 26 }, |
|
28 |
// 27 is paper in temperate climate in TTDPatch |
|
29 |
// Following can be renumbered: |
|
30 |
// 29 is the default cargo for the purpose of spritesets |
|
31 |
// 30 is the purchase list image (the equivalent of 0xff) for the purpose of spritesets |
|
32 |
}; |
|
33 |
||
34 |
/* These two arrays provide a reverse mapping. */ |
|
35 |
byte _local_cargo_id_ctype[NUM_CID] = { |
|
36 |
CT_PASSENGERS, CT_COAL, CT_MAIL, CT_OIL, CT_LIVESTOCK, CT_GOODS, CT_GRAIN, CT_WOOD, // 0-7 |
|
37 |
CT_IRON_ORE, CT_STEEL, CT_VALUABLES, CT_PAPER, CT_FOOD, CT_FRUIT, CT_COPPER_ORE, CT_WATER, // 8-15 |
|
38 |
CT_RUBBER, CT_SUGAR, CT_TOYS, CT_BATTERIES, CT_CANDY, CT_TOFFEE, CT_COLA, CT_COTTON_CANDY, // 16-23 |
|
39 |
CT_BUBBLES, CT_PLASTIC, CT_FIZZY_DRINKS, CT_PAPER /* unsup. */, CT_HILLY_UNUSED // 24-28 |
|
40 |
}; |
|
41 |
||
42 |
/* LT'th bit is set of the particular landscape if cargo available there. |
|
43 |
* 1: LT_NORMAL, 2: LT_HILLY, 4: LT_DESERT, 8: LT_CANDY */ |
|
44 |
byte _local_cargo_id_landscape[NUM_CID] = { |
|
45 |
15, 3, 15, 7, 3, 7, 7, 7, 1, 1, 7, 2, 7, // 0-12 |
|
46 |
4, 4, 4, 4, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 1, 2, // 13-28 |
|
47 |
}; |
|
48 |
||
49 |
void ShowEnginePreviewWindow(int engine); |
|
50 |
||
51 |
void DeleteCustomEngineNames() |
|
52 |
{ |
|
53 |
uint i; |
|
54 |
StringID old; |
|
55 |
||
56 |
for(i=0; i!=TOTAL_NUM_ENGINES; i++) { |
|
57 |
old = _engine_name_strings[i]; |
|
58 |
_engine_name_strings[i] = i + STR_8000_KIRBY_PAUL_TANK_STEAM; |
|
59 |
DeleteName(old); |
|
60 |
} |
|
61 |
||
62 |
_vehicle_design_names &= ~1; |
|
63 |
} |
|
64 |
||
65 |
void LoadCustomEngineNames() |
|
66 |
{ |
|
67 |
// XXX: not done */ |
|
68 |
DEBUG(misc, 1) ("LoadCustomEngineNames: not done"); |
|
69 |
} |
|
70 |
||
71 |
static void SetupEngineNames() |
|
72 |
{ |
|
73 |
uint i; |
|
74 |
||
75 |
for(i=0; i!=TOTAL_NUM_ENGINES; i++) |
|
76 |
_engine_name_strings[i] = STR_SV_EMPTY; |
|
77 |
||
78 |
DeleteCustomEngineNames(); |
|
79 |
LoadCustomEngineNames(); |
|
80 |
} |
|
81 |
||
82 |
static void AdjustAvailAircraft() |
|
83 |
{ |
|
84 |
uint16 date = _date; |
|
85 |
byte avail = 0; |
|
86 |
if (date >= 12784) avail |= 2; // big airport |
|
87 |
if (date < 14610 || _patches.always_small_airport) avail |= 1; // small airport |
|
88 |
if (date >= 15706) avail |= 4; // enable heliport |
|
89 |
||
90 |
if (avail != _avail_aircraft) { |
|
91 |
_avail_aircraft = avail; |
|
92 |
InvalidateWindow(WC_BUILD_STATION, 0); |
|
93 |
} |
|
94 |
} |
|
95 |
||
96 |
static void CalcEngineReliability(Engine *e) |
|
97 |
{ |
|
98 |
uint age = e->age; |
|
99 |
||
100 |
if (age < e->duration_phase_1) { |
|
101 |
uint start = e->reliability_start; |
|
102 |
e->reliability = age * (e->reliability_max - start) / e->duration_phase_1 + start; |
|
103 |
} else if ((age -= e->duration_phase_1) < e->duration_phase_2) { |
|
104 |
e->reliability = e->reliability_max; |
|
105 |
} else if ((age -= e->duration_phase_2) < e->duration_phase_3) { |
|
106 |
uint max = e->reliability_max; |
|
107 |
e->reliability = (int)age * (int)(e->reliability_final - max) / e->duration_phase_3 + max; |
|
108 |
} else { |
|
292
9b0a6d544593
(svn r298) Fix: Engines from other climates do not appear any more when never_expire_vehicles is enabled
dominik
parents:
257
diff
changeset
|
109 |
// time's up for this engine |
9b0a6d544593
(svn r298) Fix: Engines from other climates do not appear any more when never_expire_vehicles is enabled
dominik
parents:
257
diff
changeset
|
110 |
// make it either available to all players (if never_expire_vehicles is enabled and if it was available earlier) |
9b0a6d544593
(svn r298) Fix: Engines from other climates do not appear any more when never_expire_vehicles is enabled
dominik
parents:
257
diff
changeset
|
111 |
// or disable this engine completely |
9b0a6d544593
(svn r298) Fix: Engines from other climates do not appear any more when never_expire_vehicles is enabled
dominik
parents:
257
diff
changeset
|
112 |
e->player_avail = (_patches.never_expire_vehicles && e->player_avail)? -1 : 0; |
0 | 113 |
e->reliability = e->reliability_final; |
114 |
} |
|
115 |
} |
|
116 |
||
117 |
void StartupEngines() |
|
118 |
{ |
|
119 |
Engine *e; |
|
120 |
const EngineInfo *ei; |
|
121 |
uint32 r; |
|
122 |
||
123 |
SetupEngineNames(); |
|
124 |
||
125 |
||
126 |
for(e=_engines, ei=_engine_info; e != endof(_engines); e++,ei++) { |
|
127 |
e->age = 0; |
|
128 |
e->railtype = ei->railtype_climates >> 4; |
|
129 |
e->flags = 0; |
|
130 |
e->player_avail = 0; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
114
diff
changeset
|
131 |
|
0 | 132 |
r = Random(); |
133 |
e->intro_date = (uint16)((r & 0x1FF) + ei->base_intro); |
|
134 |
if (e->intro_date <= _date) { |
|
135 |
e->age = (_date - e->intro_date) >> 5; |
|
136 |
e->player_avail = (byte)-1; |
|
137 |
e->flags |= ENGINE_AVAILABLE; |
|
138 |
} |
|
139 |
||
140 |
e->reliability_start = (uint16)(((r >> 16) & 0x3fff) + 0x7AE0); |
|
141 |
r = Random(); |
|
142 |
e->reliability_max = (uint16)((r & 0x3fff) + 0xbfff); |
|
143 |
e->reliability_final = (uint16)(((r>>16) & 0x3fff) + 0x3fff); |
|
144 |
||
145 |
r = Random(); |
|
146 |
e->duration_phase_1 = (uint16)((r & 0x1F) + 7); |
|
147 |
e->duration_phase_2 = (uint16)(((r >> 5) & 0xF) + ei->base_life * 12 - 96); |
|
148 |
e->duration_phase_3 = (uint16)(((r >> 9) & 0x7F) + 120); |
|
149 |
||
150 |
e->reliability_spd_dec = (ei->unk2&0x7F) << 2; |
|
151 |
||
152 |
/* my invented flag for something that is a wagon */ |
|
153 |
if (ei->unk2 & 0x80) { |
|
154 |
e->age = 0xFFFF; |
|
155 |
} else { |
|
156 |
CalcEngineReliability(e); |
|
157 |
} |
|
158 |
||
159 |
e->lifelength = ei->lifelength + _patches.extend_vehicle_life; |
|
160 |
||
161 |
// prevent certain engines from ever appearing. |
|
162 |
if (!HASBIT(ei->railtype_climates, _opt.landscape)) { |
|
163 |
e->flags |= ENGINE_AVAILABLE; |
|
164 |
e->player_avail = 0; |
|
165 |
} |
|
166 |
} |
|
167 |
||
168 |
AdjustAvailAircraft(); |
|
169 |
} |
|
170 |
||
171 |
uint32 _engine_refit_masks[256]; |
|
172 |
||
173 |
||
174 |
// TODO: We don't support cargo-specific wagon overrides. Pretty exotic... ;-) --pasky |
|
175 |
||
176 |
struct WagonOverride { |
|
177 |
byte *train_id; |
|
178 |
int trains; |
|
369
ab2266938fa8
(svn r557) -newgrf: Rename all 'superset' tokens to 'group' and some other small renamings (pasky and octo).
darkvater
parents:
292
diff
changeset
|
179 |
struct SpriteGroup group; |
0 | 180 |
}; |
181 |
||
182 |
static struct WagonOverrides { |
|
183 |
int overrides_count; |
|
184 |
struct WagonOverride *overrides; |
|
185 |
} _engine_wagon_overrides[256]; |
|
186 |
||
369
ab2266938fa8
(svn r557) -newgrf: Rename all 'superset' tokens to 'group' and some other small renamings (pasky and octo).
darkvater
parents:
292
diff
changeset
|
187 |
void SetWagonOverrideSprites(byte engine, struct SpriteGroup *group, |
0 | 188 |
byte *train_id, int trains) |
189 |
{ |
|
190 |
struct WagonOverrides *wos; |
|
191 |
struct WagonOverride *wo; |
|
192 |
||
193 |
wos = &_engine_wagon_overrides[engine]; |
|
194 |
wos->overrides_count++; |
|
195 |
wos->overrides = realloc(wos->overrides, |
|
196 |
wos->overrides_count * sizeof(struct WagonOverride)); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
114
diff
changeset
|
197 |
|
0 | 198 |
wo = &wos->overrides[wos->overrides_count - 1]; |
408
63a8c0505aab
(svn r605) -newgrf: Framework for supporting variational spritegroups . Deterministic only at the moment, but random ones support shouldn't be that difficult now It doesn't do anything, but makes these actions actually possible (pasky).
darkvater
parents:
405
diff
changeset
|
199 |
/* FIXME: If we are replacing an override, release original SpriteGroup |
63a8c0505aab
(svn r605) -newgrf: Framework for supporting variational spritegroups . Deterministic only at the moment, but random ones support shouldn't be that difficult now It doesn't do anything, but makes these actions actually possible (pasky).
darkvater
parents:
405
diff
changeset
|
200 |
* to prevent leaks. But first we need to refcount the SpriteGroup. |
63a8c0505aab
(svn r605) -newgrf: Framework for supporting variational spritegroups . Deterministic only at the moment, but random ones support shouldn't be that difficult now It doesn't do anything, but makes these actions actually possible (pasky).
darkvater
parents:
405
diff
changeset
|
201 |
* --pasky */ |
369
ab2266938fa8
(svn r557) -newgrf: Rename all 'superset' tokens to 'group' and some other small renamings (pasky and octo).
darkvater
parents:
292
diff
changeset
|
202 |
wo->group = *group; |
0 | 203 |
wo->trains = trains; |
204 |
wo->train_id = malloc(trains); |
|
205 |
memcpy(wo->train_id, train_id, trains); |
|
206 |
} |
|
207 |
||
369
ab2266938fa8
(svn r557) -newgrf: Rename all 'superset' tokens to 'group' and some other small renamings (pasky and octo).
darkvater
parents:
292
diff
changeset
|
208 |
static struct SpriteGroup *GetWagonOverrideSpriteSet(byte engine, byte overriding_engine) |
0 | 209 |
{ |
210 |
struct WagonOverrides *wos = &_engine_wagon_overrides[engine]; |
|
211 |
int i; |
|
212 |
||
408
63a8c0505aab
(svn r605) -newgrf: Framework for supporting variational spritegroups . Deterministic only at the moment, but random ones support shouldn't be that difficult now It doesn't do anything, but makes these actions actually possible (pasky).
darkvater
parents:
405
diff
changeset
|
213 |
// XXX: This could turn out to be a timesink on profiles. We could |
63a8c0505aab
(svn r605) -newgrf: Framework for supporting variational spritegroups . Deterministic only at the moment, but random ones support shouldn't be that difficult now It doesn't do anything, but makes these actions actually possible (pasky).
darkvater
parents:
405
diff
changeset
|
214 |
// always just dedicate 65535 bytes for an [engine][train] trampoline |
63a8c0505aab
(svn r605) -newgrf: Framework for supporting variational spritegroups . Deterministic only at the moment, but random ones support shouldn't be that difficult now It doesn't do anything, but makes these actions actually possible (pasky).
darkvater
parents:
405
diff
changeset
|
215 |
// for O(1). Or O(logMlogN) and searching binary tree or smt. like |
63a8c0505aab
(svn r605) -newgrf: Framework for supporting variational spritegroups . Deterministic only at the moment, but random ones support shouldn't be that difficult now It doesn't do anything, but makes these actions actually possible (pasky).
darkvater
parents:
405
diff
changeset
|
216 |
// that. --pasky |
0 | 217 |
|
218 |
for (i = 0; i < wos->overrides_count; i++) { |
|
219 |
struct WagonOverride *wo = &wos->overrides[i]; |
|
220 |
int j; |
|
221 |
||
222 |
for (j = 0; j < wo->trains; j++) { |
|
223 |
if (wo->train_id[j] == overriding_engine) |
|
369
ab2266938fa8
(svn r557) -newgrf: Rename all 'superset' tokens to 'group' and some other small renamings (pasky and octo).
darkvater
parents:
292
diff
changeset
|
224 |
return &wo->group; |
0 | 225 |
} |
226 |
} |
|
227 |
return NULL; |
|
228 |
} |
|
229 |
||
230 |
||
231 |
byte _engine_original_sprites[256]; |
|
232 |
// 0 - 28 are cargos, 29 is default, 30 is the advert (purchase list) |
|
233 |
// (It isn't and shouldn't be like this in the GRF files since new cargo types |
|
234 |
// may appear in future - however it's more convenient to store it like this in |
|
235 |
// memory. --pasky) |
|
369
ab2266938fa8
(svn r557) -newgrf: Rename all 'superset' tokens to 'group' and some other small renamings (pasky and octo).
darkvater
parents:
292
diff
changeset
|
236 |
static struct SpriteGroup _engine_custom_sprites[256][NUM_CID]; |
0 | 237 |
|
369
ab2266938fa8
(svn r557) -newgrf: Rename all 'superset' tokens to 'group' and some other small renamings (pasky and octo).
darkvater
parents:
292
diff
changeset
|
238 |
void SetCustomEngineSprites(byte engine, byte cargo, struct SpriteGroup *group) |
0 | 239 |
{ |
408
63a8c0505aab
(svn r605) -newgrf: Framework for supporting variational spritegroups . Deterministic only at the moment, but random ones support shouldn't be that difficult now It doesn't do anything, but makes these actions actually possible (pasky).
darkvater
parents:
405
diff
changeset
|
240 |
/* FIXME: If we are replacing an override, release original SpriteGroup |
63a8c0505aab
(svn r605) -newgrf: Framework for supporting variational spritegroups . Deterministic only at the moment, but random ones support shouldn't be that difficult now It doesn't do anything, but makes these actions actually possible (pasky).
darkvater
parents:
405
diff
changeset
|
241 |
* to prevent leaks. But first we need to refcount the SpriteGroup. |
63a8c0505aab
(svn r605) -newgrf: Framework for supporting variational spritegroups . Deterministic only at the moment, but random ones support shouldn't be that difficult now It doesn't do anything, but makes these actions actually possible (pasky).
darkvater
parents:
405
diff
changeset
|
242 |
* --pasky */ |
369
ab2266938fa8
(svn r557) -newgrf: Rename all 'superset' tokens to 'group' and some other small renamings (pasky and octo).
darkvater
parents:
292
diff
changeset
|
243 |
_engine_custom_sprites[engine][cargo] = *group; |
0 | 244 |
} |
245 |
||
246 |
int GetCustomEngineSprite(byte engine, uint16 overriding_engine, byte cargo, |
|
247 |
byte loaded, byte in_motion, byte direction) |
|
248 |
{ |
|
369
ab2266938fa8
(svn r557) -newgrf: Rename all 'superset' tokens to 'group' and some other small renamings (pasky and octo).
darkvater
parents:
292
diff
changeset
|
249 |
struct SpriteGroup *group = &_engine_custom_sprites[engine][cargo]; |
408
63a8c0505aab
(svn r605) -newgrf: Framework for supporting variational spritegroups . Deterministic only at the moment, but random ones support shouldn't be that difficult now It doesn't do anything, but makes these actions actually possible (pasky).
darkvater
parents:
405
diff
changeset
|
250 |
struct RealSpriteGroup *rsg; |
0 | 251 |
int totalsets, spriteset; |
252 |
int r; |
|
253 |
||
254 |
if (overriding_engine != 0xffff) { |
|
369
ab2266938fa8
(svn r557) -newgrf: Rename all 'superset' tokens to 'group' and some other small renamings (pasky and octo).
darkvater
parents:
292
diff
changeset
|
255 |
struct SpriteGroup *overset; |
0 | 256 |
|
257 |
overset = GetWagonOverrideSpriteSet(engine, overriding_engine); |
|
369
ab2266938fa8
(svn r557) -newgrf: Rename all 'superset' tokens to 'group' and some other small renamings (pasky and octo).
darkvater
parents:
292
diff
changeset
|
258 |
if (overset) group = overset; |
0 | 259 |
} |
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
114
diff
changeset
|
260 |
|
408
63a8c0505aab
(svn r605) -newgrf: Framework for supporting variational spritegroups . Deterministic only at the moment, but random ones support shouldn't be that difficult now It doesn't do anything, but makes these actions actually possible (pasky).
darkvater
parents:
405
diff
changeset
|
261 |
/* TODO: Resolve surreal groups properly. --pasky */ |
63a8c0505aab
(svn r605) -newgrf: Framework for supporting variational spritegroups . Deterministic only at the moment, but random ones support shouldn't be that difficult now It doesn't do anything, but makes these actions actually possible (pasky).
darkvater
parents:
405
diff
changeset
|
262 |
rsg = TriviallyGetRSG(group); |
63a8c0505aab
(svn r605) -newgrf: Framework for supporting variational spritegroups . Deterministic only at the moment, but random ones support shouldn't be that difficult now It doesn't do anything, but makes these actions actually possible (pasky).
darkvater
parents:
405
diff
changeset
|
263 |
|
63a8c0505aab
(svn r605) -newgrf: Framework for supporting variational spritegroups . Deterministic only at the moment, but random ones support shouldn't be that difficult now It doesn't do anything, but makes these actions actually possible (pasky).
darkvater
parents:
405
diff
changeset
|
264 |
if (!rsg->sprites_per_set && cargo != 29) { |
369
ab2266938fa8
(svn r557) -newgrf: Rename all 'superset' tokens to 'group' and some other small renamings (pasky and octo).
darkvater
parents:
292
diff
changeset
|
265 |
// This group is empty but perhaps there'll be a default one. |
408
63a8c0505aab
(svn r605) -newgrf: Framework for supporting variational spritegroups . Deterministic only at the moment, but random ones support shouldn't be that difficult now It doesn't do anything, but makes these actions actually possible (pasky).
darkvater
parents:
405
diff
changeset
|
266 |
/* TODO: Resolve surreal groups properly. --pasky */ |
63a8c0505aab
(svn r605) -newgrf: Framework for supporting variational spritegroups . Deterministic only at the moment, but random ones support shouldn't be that difficult now It doesn't do anything, but makes these actions actually possible (pasky).
darkvater
parents:
405
diff
changeset
|
267 |
rsg = TriviallyGetRSG(&_engine_custom_sprites[engine][29]); |
0 | 268 |
} |
269 |
||
408
63a8c0505aab
(svn r605) -newgrf: Framework for supporting variational spritegroups . Deterministic only at the moment, but random ones support shouldn't be that difficult now It doesn't do anything, but makes these actions actually possible (pasky).
darkvater
parents:
405
diff
changeset
|
270 |
if (!rsg->sprites_per_set) { |
369
ab2266938fa8
(svn r557) -newgrf: Rename all 'superset' tokens to 'group' and some other small renamings (pasky and octo).
darkvater
parents:
292
diff
changeset
|
271 |
// This group is empty. This function users should therefore |
0 | 272 |
// look up the sprite number in _engine_original_sprites. |
273 |
return 0; |
|
274 |
} |
|
275 |
||
276 |
direction %= 8; |
|
408
63a8c0505aab
(svn r605) -newgrf: Framework for supporting variational spritegroups . Deterministic only at the moment, but random ones support shouldn't be that difficult now It doesn't do anything, but makes these actions actually possible (pasky).
darkvater
parents:
405
diff
changeset
|
277 |
if (rsg->sprites_per_set == 4) |
0 | 278 |
direction %= 4; |
279 |
||
408
63a8c0505aab
(svn r605) -newgrf: Framework for supporting variational spritegroups . Deterministic only at the moment, but random ones support shouldn't be that difficult now It doesn't do anything, but makes these actions actually possible (pasky).
darkvater
parents:
405
diff
changeset
|
280 |
totalsets = in_motion ? rsg->loaded_count : rsg->loading_count; |
0 | 281 |
|
282 |
// My aim here is to make it possible to visually determine absolutely |
|
283 |
// empty and totally full vehicles. --pasky |
|
284 |
if (loaded == 100 || totalsets == 1) { // full |
|
285 |
spriteset = totalsets - 1; |
|
286 |
} else if (loaded == 0 || totalsets == 2) { // empty |
|
287 |
spriteset = 0; |
|
288 |
} else { // something inbetween |
|
289 |
spriteset = loaded * (totalsets - 2) / 100 + 1; |
|
290 |
// correct possible rounding errors |
|
291 |
if (!spriteset) |
|
292 |
spriteset = 1; |
|
293 |
else if (spriteset == totalsets - 1) |
|
294 |
spriteset--; |
|
295 |
} |
|
296 |
||
408
63a8c0505aab
(svn r605) -newgrf: Framework for supporting variational spritegroups . Deterministic only at the moment, but random ones support shouldn't be that difficult now It doesn't do anything, but makes these actions actually possible (pasky).
darkvater
parents:
405
diff
changeset
|
297 |
r = (in_motion ? rsg->loaded[spriteset] : rsg->loading[spriteset]) + direction; |
0 | 298 |
return r; |
299 |
} |
|
300 |
||
301 |
||
302 |
static char *_engine_custom_names[256]; |
|
303 |
||
304 |
void SetCustomEngineName(int engine, char *name) |
|
305 |
{ |
|
306 |
_engine_custom_names[engine] = strdup(name); |
|
307 |
} |
|
308 |
||
309 |
StringID GetCustomEngineName(int engine) |
|
310 |
{ |
|
311 |
if (!_engine_custom_names[engine]) |
|
312 |
return _engine_name_strings[engine]; |
|
313 |
strcpy(_userstring, _engine_custom_names[engine]); |
|
314 |
return STR_SPEC_USERSTRING; |
|
315 |
} |
|
316 |
||
317 |
||
318 |
void AcceptEnginePreview(Engine *e, int player) |
|
319 |
{ |
|
320 |
Player *p; |
|
321 |
||
322 |
SETBIT(e->player_avail, player); |
|
323 |
||
324 |
p = DEREF_PLAYER(player); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
114
diff
changeset
|
325 |
|
0 | 326 |
UPDATE_PLAYER_RAILTYPE(e,p); |
327 |
||
328 |
e->preview_player = 0xFF; |
|
329 |
InvalidateWindowClasses(WC_BUILD_VEHICLE); |
|
330 |
} |
|
331 |
||
332 |
void EnginesDailyLoop() |
|
333 |
{ |
|
334 |
Engine *e; |
|
335 |
int i,num; |
|
336 |
Player *p; |
|
337 |
uint mask; |
|
338 |
int32 best_hist; |
|
339 |
int best_player; |
|
340 |
||
341 |
if (_cur_year >= 130) |
|
342 |
return; |
|
343 |
||
344 |
for(e=_engines,i=0; i!=TOTAL_NUM_ENGINES; e++,i++) { |
|
345 |
if (e->flags & ENGINE_INTRODUCING) { |
|
346 |
if (e->flags & ENGINE_PREVIEWING) { |
|
347 |
if (!--e->preview_wait) { |
|
348 |
e->flags &= ~ENGINE_PREVIEWING; |
|
349 |
DeleteWindowById(WC_ENGINE_PREVIEW, i); |
|
350 |
e->preview_player++; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
114
diff
changeset
|
351 |
} |
0 | 352 |
} else if (e->preview_player != 0xFF) { |
353 |
num = e->preview_player; |
|
354 |
mask = 0; |
|
355 |
do { |
|
356 |
best_hist = -1; |
|
357 |
best_player = -1; |
|
358 |
FOR_ALL_PLAYERS(p) { |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
114
diff
changeset
|
359 |
if (p->is_active && p->block_preview == 0 && !HASBIT(mask,p->index) && |
0 | 360 |
p->old_economy[0].performance_history > best_hist) { |
361 |
best_hist = p->old_economy[0].performance_history; |
|
362 |
best_player = p->index; |
|
363 |
} |
|
364 |
} |
|
365 |
if (best_player == -1) { |
|
366 |
e->preview_player = 0xFF; |
|
367 |
goto next_engine; |
|
368 |
} |
|
369 |
mask |= (1 << best_player); |
|
370 |
} while (--num != 0); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
114
diff
changeset
|
371 |
|
0 | 372 |
if (!IS_HUMAN_PLAYER(best_player)) { |
373 |
/* TTDBUG: TTD has a bug here */ |
|
374 |
AcceptEnginePreview(e, best_player); |
|
375 |
} else { |
|
376 |
e->flags |= ENGINE_PREVIEWING; |
|
377 |
e->preview_wait = 20; |
|
378 |
if (IS_INTERACTIVE_PLAYER(best_player)) { |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
114
diff
changeset
|
379 |
ShowEnginePreviewWindow(i); |
0 | 380 |
} |
381 |
} |
|
382 |
} |
|
383 |
} |
|
384 |
next_engine:; |
|
385 |
} |
|
386 |
} |
|
387 |
||
388 |
int32 CmdWantEnginePreview(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
389 |
{ |
|
390 |
if (flags & DC_EXEC) { |
|
391 |
AcceptEnginePreview(&_engines[p1], _current_player); |
|
392 |
} |
|
393 |
return 0; |
|
394 |
} |
|
395 |
||
257
8d83db6716ea
(svn r262) Fix: [ 1028234 ] Monorail and MagLev infrastructure not available in 1920s any more
dominik
parents:
220
diff
changeset
|
396 |
// Determine if an engine type is a wagon (and not a loco) |
8d83db6716ea
(svn r262) Fix: [ 1028234 ] Monorail and MagLev infrastructure not available in 1920s any more
dominik
parents:
220
diff
changeset
|
397 |
bool isWagon(byte index) |
8d83db6716ea
(svn r262) Fix: [ 1028234 ] Monorail and MagLev infrastructure not available in 1920s any more
dominik
parents:
220
diff
changeset
|
398 |
{ |
8d83db6716ea
(svn r262) Fix: [ 1028234 ] Monorail and MagLev infrastructure not available in 1920s any more
dominik
parents:
220
diff
changeset
|
399 |
if (index < NUM_TRAIN_ENGINES) { |
8d83db6716ea
(svn r262) Fix: [ 1028234 ] Monorail and MagLev infrastructure not available in 1920s any more
dominik
parents:
220
diff
changeset
|
400 |
const RailVehicleInfo *rvi = &_rail_vehicle_info[index]; |
8d83db6716ea
(svn r262) Fix: [ 1028234 ] Monorail and MagLev infrastructure not available in 1920s any more
dominik
parents:
220
diff
changeset
|
401 |
if(rvi->flags & RVI_WAGON) |
8d83db6716ea
(svn r262) Fix: [ 1028234 ] Monorail and MagLev infrastructure not available in 1920s any more
dominik
parents:
220
diff
changeset
|
402 |
return true; |
8d83db6716ea
(svn r262) Fix: [ 1028234 ] Monorail and MagLev infrastructure not available in 1920s any more
dominik
parents:
220
diff
changeset
|
403 |
} |
8d83db6716ea
(svn r262) Fix: [ 1028234 ] Monorail and MagLev infrastructure not available in 1920s any more
dominik
parents:
220
diff
changeset
|
404 |
return false; |
8d83db6716ea
(svn r262) Fix: [ 1028234 ] Monorail and MagLev infrastructure not available in 1920s any more
dominik
parents:
220
diff
changeset
|
405 |
} |
8d83db6716ea
(svn r262) Fix: [ 1028234 ] Monorail and MagLev infrastructure not available in 1920s any more
dominik
parents:
220
diff
changeset
|
406 |
|
0 | 407 |
void NewVehicleAvailable(Engine *e) |
408 |
{ |
|
409 |
Vehicle *v; |
|
410 |
Player *p; |
|
411 |
int index = e - _engines; |
|
412 |
||
413 |
// In case the player didn't build the vehicle during the intro period, |
|
414 |
// prevent that player from getting future intro periods for a while. |
|
415 |
if (e->flags&ENGINE_INTRODUCING) { |
|
416 |
FOR_ALL_PLAYERS(p) { |
|
417 |
if (!HASBIT(e->player_avail,p->index)) |
|
418 |
continue; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
114
diff
changeset
|
419 |
|
0 | 420 |
for(v=_vehicles;;) { |
421 |
if (v->type == VEH_Train || v->type == VEH_Road || v->type == VEH_Ship || |
|
422 |
(v->type == VEH_Aircraft && v->subtype <= 2)) { |
|
423 |
if (v->owner == p->index && v->engine_type == index) break; |
|
424 |
} |
|
425 |
if (++v == endof(_vehicles)) { |
|
426 |
p->block_preview = 20; |
|
427 |
break; |
|
428 |
} |
|
429 |
} |
|
430 |
} |
|
431 |
} |
|
432 |
||
114
6a2af0c2d0db
(svn r115) Fix: monorail/maglev became available around 1927
dominik
parents:
0
diff
changeset
|
433 |
e->flags = (e->flags & ~ENGINE_INTRODUCING) | ENGINE_AVAILABLE; |
6a2af0c2d0db
(svn r115) Fix: monorail/maglev became available around 1927
dominik
parents:
0
diff
changeset
|
434 |
InvalidateWindowClasses(WC_BUILD_VEHICLE); |
6a2af0c2d0db
(svn r115) Fix: monorail/maglev became available around 1927
dominik
parents:
0
diff
changeset
|
435 |
|
0 | 436 |
// Now available for all players |
437 |
e->player_avail = (byte)-1; |
|
114
6a2af0c2d0db
(svn r115) Fix: monorail/maglev became available around 1927
dominik
parents:
0
diff
changeset
|
438 |
|
6a2af0c2d0db
(svn r115) Fix: monorail/maglev became available around 1927
dominik
parents:
0
diff
changeset
|
439 |
// Do not introduce new rail wagons |
257
8d83db6716ea
(svn r262) Fix: [ 1028234 ] Monorail and MagLev infrastructure not available in 1920s any more
dominik
parents:
220
diff
changeset
|
440 |
if(isWagon(index)) |
8d83db6716ea
(svn r262) Fix: [ 1028234 ] Monorail and MagLev infrastructure not available in 1920s any more
dominik
parents:
220
diff
changeset
|
441 |
return; |
114
6a2af0c2d0db
(svn r115) Fix: monorail/maglev became available around 1927
dominik
parents:
0
diff
changeset
|
442 |
|
6a2af0c2d0db
(svn r115) Fix: monorail/maglev became available around 1927
dominik
parents:
0
diff
changeset
|
443 |
// make maglev / monorail available |
0 | 444 |
FOR_ALL_PLAYERS(p) { |
445 |
if (p->is_active) |
|
446 |
UPDATE_PLAYER_RAILTYPE(e,p); |
|
447 |
} |
|
448 |
||
449 |
if ((byte)index < NUM_TRAIN_ENGINES) { |
|
450 |
AddNewsItem(index, NEWS_FLAGS(NM_CALLBACK, 0, NT_NEW_VEHICLES, DNC_TRAINAVAIL), 0, 0); |
|
451 |
} else if ((byte)index < NUM_TRAIN_ENGINES + NUM_ROAD_ENGINES) { |
|
452 |
AddNewsItem(index, NEWS_FLAGS(NM_CALLBACK, 0, NT_NEW_VEHICLES, DNC_ROADAVAIL), 0, 0); |
|
453 |
} else if ((byte)index < NUM_TRAIN_ENGINES + NUM_ROAD_ENGINES + NUM_SHIP_ENGINES) { |
|
454 |
AddNewsItem(index, NEWS_FLAGS(NM_CALLBACK, 0, NT_NEW_VEHICLES, DNC_SHIPAVAIL), 0, 0); |
|
455 |
} else { |
|
456 |
AddNewsItem(index, NEWS_FLAGS(NM_CALLBACK, 0, NT_NEW_VEHICLES, DNC_AIRCRAFTAVAIL), 0, 0); |
|
457 |
} |
|
458 |
} |
|
459 |
||
460 |
void EnginesMonthlyLoop() |
|
461 |
{ |
|
462 |
Engine *e; |
|
463 |
||
464 |
if (_cur_year < 130) { |
|
465 |
for(e=_engines; e != endof(_engines); e++) { |
|
466 |
// Age the vehicle |
|
467 |
if (e->flags&ENGINE_AVAILABLE && e->age != 0xFFFF) { |
|
468 |
e->age++; |
|
469 |
CalcEngineReliability(e); |
|
470 |
} |
|
471 |
||
472 |
if (!(e->flags & ENGINE_AVAILABLE) && (uint16)(_date - min(_date, 365)) >= e->intro_date) { |
|
473 |
// Introduce it to all players |
|
474 |
NewVehicleAvailable(e); |
|
475 |
} else if (!(e->flags & (ENGINE_AVAILABLE|ENGINE_INTRODUCING)) && _date >= e->intro_date) { |
|
476 |
// Introduction date has passed.. show introducing dialog to one player. |
|
477 |
e->flags |= ENGINE_INTRODUCING; |
|
257
8d83db6716ea
(svn r262) Fix: [ 1028234 ] Monorail and MagLev infrastructure not available in 1920s any more
dominik
parents:
220
diff
changeset
|
478 |
|
8d83db6716ea
(svn r262) Fix: [ 1028234 ] Monorail and MagLev infrastructure not available in 1920s any more
dominik
parents:
220
diff
changeset
|
479 |
// Do not introduce new rail wagons |
8d83db6716ea
(svn r262) Fix: [ 1028234 ] Monorail and MagLev infrastructure not available in 1920s any more
dominik
parents:
220
diff
changeset
|
480 |
if(!isWagon(e - _engines)) |
8d83db6716ea
(svn r262) Fix: [ 1028234 ] Monorail and MagLev infrastructure not available in 1920s any more
dominik
parents:
220
diff
changeset
|
481 |
e->preview_player = 1; // Give to the player with the highest rating. |
0 | 482 |
} |
483 |
} |
|
484 |
} |
|
485 |
AdjustAvailAircraft(); |
|
486 |
} |
|
487 |
||
488 |
int32 CmdRenameEngine(int x, int y, uint32 flags, uint32 p1, uint32 p2) |
|
489 |
{ |
|
490 |
StringID str; |
|
491 |
||
492 |
str = AllocateName((byte*)_decode_parameters, 0); |
|
493 |
if (str == 0) |
|
494 |
return CMD_ERROR; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
114
diff
changeset
|
495 |
|
0 | 496 |
if (flags & DC_EXEC) { |
497 |
StringID old_str = _engine_name_strings[p1]; |
|
498 |
_engine_name_strings[p1] = str; |
|
499 |
DeleteName(old_str); |
|
500 |
_vehicle_design_names |= 3; |
|
501 |
MarkWholeScreenDirty(); |
|
502 |
} else { |
|
503 |
DeleteName(str); |
|
504 |
} |
|
505 |
||
506 |
return 0; |
|
507 |
} |
|
508 |
||
509 |
int GetPlayerMaxRailtype(int p) |
|
510 |
{ |
|
511 |
Engine *e; |
|
512 |
int rt = 0; |
|
513 |
int i; |
|
514 |
||
515 |
for(e=_engines,i=0; i!=lengthof(_engines); e++,i++) { |
|
516 |
if (!HASBIT(e->player_avail, p)) |
|
517 |
continue; |
|
518 |
||
519 |
if ((i >= 27 && i < 54) || (i >= 57 && i < 84) || (i >= 89 && i < 116)) |
|
520 |
continue; |
|
521 |
||
522 |
if (rt < e->railtype) |
|
523 |
rt = e->railtype; |
|
524 |
} |
|
525 |
||
526 |
return rt + 1; |
|
527 |
} |
|
528 |
||
529 |
||
530 |
static const byte _engine_desc[] = { |
|
531 |
SLE_VAR(Engine,intro_date, SLE_UINT16), |
|
532 |
SLE_VAR(Engine,age, SLE_UINT16), |
|
533 |
SLE_VAR(Engine,reliability, SLE_UINT16), |
|
534 |
SLE_VAR(Engine,reliability_spd_dec, SLE_UINT16), |
|
535 |
SLE_VAR(Engine,reliability_start, SLE_UINT16), |
|
536 |
SLE_VAR(Engine,reliability_max, SLE_UINT16), |
|
537 |
SLE_VAR(Engine,reliability_final, SLE_UINT16), |
|
538 |
SLE_VAR(Engine,duration_phase_1, SLE_UINT16), |
|
539 |
SLE_VAR(Engine,duration_phase_2, SLE_UINT16), |
|
540 |
SLE_VAR(Engine,duration_phase_3, SLE_UINT16), |
|
541 |
||
542 |
SLE_VAR(Engine,lifelength, SLE_UINT8), |
|
543 |
SLE_VAR(Engine,flags, SLE_UINT8), |
|
544 |
SLE_VAR(Engine,preview_player, SLE_UINT8), |
|
545 |
SLE_VAR(Engine,preview_wait, SLE_UINT8), |
|
546 |
SLE_VAR(Engine,railtype, SLE_UINT8), |
|
547 |
SLE_VAR(Engine,player_avail, SLE_UINT8), |
|
548 |
||
549 |
// reserve extra space in savegame here. (currently 16 bytes) |
|
550 |
SLE_CONDARR(NullStruct,null,SLE_FILE_U64 | SLE_VAR_NULL, 2, 2, 255), |
|
551 |
||
552 |
SLE_END() |
|
553 |
}; |
|
554 |
||
555 |
static void Save_ENGN() |
|
556 |
{ |
|
557 |
Engine *e; |
|
558 |
int i; |
|
559 |
for(i=0,e=_engines; i != lengthof(_engines); i++,e++) { |
|
560 |
SlSetArrayIndex(i); |
|
561 |
SlObject(e, _engine_desc); |
|
562 |
} |
|
563 |
} |
|
564 |
||
565 |
static void Load_ENGN() |
|
566 |
{ |
|
567 |
int index; |
|
568 |
while ((index = SlIterateArray()) != -1) { |
|
569 |
SlObject(&_engines[index], _engine_desc); |
|
570 |
} |
|
571 |
} |
|
572 |
||
573 |
static void LoadSave_ENGS() |
|
574 |
{ |
|
575 |
SlArray(_engine_name_strings, lengthof(_engine_name_strings), SLE_STRINGID); |
|
576 |
} |
|
577 |
||
578 |
const ChunkHandler _engine_chunk_handlers[] = { |
|
579 |
{ 'ENGN', Save_ENGN, Load_ENGN, CH_ARRAY}, |
|
580 |
{ 'ENGS', LoadSave_ENGS, LoadSave_ENGS, CH_RIFF | CH_LAST}, |
|
581 |
}; |
|
582 |
||
583 |
||
584 |