author | truelight |
Fri, 10 Sep 2004 19:02:27 +0000 | |
changeset 193 | 0a7025304867 |
parent 74 | d23a80ef6361 |
child 356 | e3721e481b38 |
permissions | -rw-r--r-- |
0 | 1 |
#include "stdafx.h" |
2 |
||
3 |
#include <stdarg.h> |
|
4 |
||
5 |
#include "ttd.h" |
|
6 |
#include "gfx.h" |
|
7 |
#include "fileio.h" |
|
8 |
#include "engine.h" |
|
9 |
||
10 |
/* TTDPatch extended GRF format codec |
|
11 |
* (c) Petr Baudis 2004 (GPL'd) |
|
12 |
* Contains portions of documentation by TTDPatch team. |
|
13 |
* Thanks especially to Josef Drexler for the documentation as well as a lot |
|
14 |
* of help at #tycoon. Also thanks to Michael Blunck for is GRF files which |
|
15 |
* served as subject to the initial testing of this codec. */ |
|
16 |
||
17 |
extern int _skip_sprites; |
|
18 |
||
19 |
static const char *_cur_grffile; |
|
20 |
static int _cur_spriteid; |
|
21 |
||
22 |
typedef void (*SpecialSpriteHandler)(byte *buf, int len); |
|
23 |
||
24 |
static const int _vehshifts[4] = { |
|
25 |
0, |
|
26 |
ROAD_ENGINES_INDEX, |
|
27 |
SHIP_ENGINES_INDEX, |
|
28 |
AIRCRAFT_ENGINES_INDEX, |
|
29 |
}; |
|
30 |
||
31 |
||
32 |
enum grfmsg_severity { |
|
33 |
GMS_NOTICE, |
|
34 |
GMS_WARN, |
|
35 |
GMS_ERROR, |
|
36 |
GMS_FATAL, |
|
37 |
}; |
|
38 |
||
39 |
static void CDECL grfmsg(enum grfmsg_severity severity, const char *str, ...) |
|
40 |
{ |
|
41 |
static const char * const severitystr[4] = { "Notice", "Warning", "Error", "Fatal" }; |
|
65
f9f866bc609c
(svn r66) -Fix Station list updated on station deletion/station rename
darkvater
parents:
51
diff
changeset
|
42 |
char buf[1024]; |
0 | 43 |
va_list va; |
44 |
||
45 |
va_start(va, str); |
|
65
f9f866bc609c
(svn r66) -Fix Station list updated on station deletion/station rename
darkvater
parents:
51
diff
changeset
|
46 |
vsprintf(buf, str, va); |
0 | 47 |
va_end(va); |
74
d23a80ef6361
(svn r75) -Add proper crediting to graphics artists to about box.
darkvater
parents:
66
diff
changeset
|
48 |
DEBUG(grf, 2) ("[%s][%s] %s", _cur_grffile, severitystr[severity], buf); |
0 | 49 |
} |
50 |
||
51 |
static byte INLINE grf_load_byte(byte **buf) { |
|
52 |
return *(*buf)++; |
|
53 |
} |
|
54 |
||
55 |
||
56 |
static uint16 grf_load_word(byte **buf) |
|
57 |
{ |
|
58 |
uint16 val; |
|
59 |
byte *p = *buf; |
|
60 |
val = p[0]; |
|
61 |
val |= p[1] << 8; |
|
62 |
*buf = p + 2; |
|
63 |
return val; |
|
64 |
} |
|
65 |
||
66 |
static uint16 grf_load_dword(byte **buf) |
|
67 |
{ |
|
68 |
uint32 val; |
|
69 |
byte *p = *buf; |
|
70 |
val = p[0]; |
|
71 |
val |= p[1] << 8; |
|
72 |
val |= p[2] << 16; |
|
73 |
val |= p[3] << 24; |
|
74 |
*buf = p + 4; |
|
75 |
return val; |
|
76 |
} |
|
77 |
||
78 |
typedef int (*VCI_Handler)(uint engine, int numinfo, int prop, byte **buf, int len); |
|
79 |
||
80 |
#define foreach_engine for (i = 0; i < numinfo; i++) |
|
81 |
||
82 |
static void dewagonize(int condition, int engine) |
|
83 |
{ |
|
84 |
EngineInfo *ei = &_engine_info[engine]; |
|
85 |
RailVehicleInfo *rvi = &_rail_vehicle_info[engine]; |
|
86 |
||
87 |
if (condition) { |
|
88 |
ei->unk2 &= ~0x80; |
|
89 |
rvi->flags &= ~2; |
|
90 |
} else { |
|
91 |
ei->unk2 |= 0x80; |
|
92 |
rvi->flags |= 2; |
|
93 |
} |
|
94 |
} |
|
95 |
||
96 |
static int RailVehicleChangeInfo(uint engine, int numinfo, int prop, byte **bufp, int len) |
|
97 |
{ |
|
98 |
EngineInfo *ei = &_engine_info[engine]; |
|
99 |
RailVehicleInfo *rvi = &_rail_vehicle_info[engine]; |
|
100 |
byte *buf = *bufp; |
|
101 |
int i; |
|
102 |
int ret = 0; |
|
103 |
||
104 |
switch (prop) { |
|
105 |
case 0x05: |
|
106 |
{ /* Track type */ |
|
107 |
foreach_engine { |
|
108 |
uint8 tracktype = grf_load_byte(&buf); |
|
109 |
||
110 |
ei[i].railtype_climates &= 0xf; |
|
111 |
ei[i].railtype_climates |= tracktype << 4; |
|
112 |
} |
|
113 |
break; |
|
114 |
} |
|
115 |
case 0x08: |
|
116 |
{ /* AI passenger service */ |
|
117 |
/* TODO */ |
|
118 |
foreach_engine { |
|
119 |
grf_load_byte(&buf); |
|
120 |
} |
|
121 |
ret = 1; |
|
122 |
break; |
|
123 |
} |
|
124 |
case 0x09: |
|
125 |
{ /* Speed */ |
|
126 |
foreach_engine { |
|
127 |
uint16 speed = grf_load_word(&buf); |
|
128 |
||
129 |
rvi[i].max_speed = speed; |
|
130 |
dewagonize(speed, engine + i); |
|
131 |
} |
|
132 |
break; |
|
133 |
} |
|
134 |
case 0x0b: |
|
135 |
{ /* Power */ |
|
136 |
foreach_engine { |
|
137 |
uint16 power = grf_load_word(&buf); |
|
138 |
||
139 |
rvi[i].power = power; |
|
140 |
dewagonize(power, engine + i); |
|
141 |
} |
|
142 |
break; |
|
143 |
} |
|
144 |
case 0x0d: |
|
145 |
{ /* Running cost factor */ |
|
146 |
foreach_engine { |
|
147 |
uint8 runcostfact = grf_load_byte(&buf); |
|
148 |
||
149 |
rvi[i].running_cost_base = runcostfact; |
|
150 |
dewagonize(runcostfact, engine + i); |
|
151 |
} |
|
152 |
break; |
|
153 |
} |
|
154 |
case 0x0e: |
|
155 |
{ /* Running cost base */ |
|
156 |
foreach_engine { |
|
157 |
uint32 base = grf_load_dword(&buf); |
|
158 |
||
159 |
switch (base) { |
|
160 |
case 0x4c30: |
|
161 |
rvi[i].engclass = 0; |
|
162 |
break; |
|
163 |
case 0x4c36: |
|
164 |
rvi[i].engclass = 1; |
|
165 |
break; |
|
166 |
case 0x4c3c: |
|
167 |
rvi[i].engclass = 2; |
|
168 |
break; |
|
169 |
} |
|
170 |
dewagonize(base, engine + i); |
|
171 |
} |
|
172 |
break; |
|
173 |
} |
|
174 |
case 0x12: |
|
175 |
{ /* Sprite ID */ |
|
176 |
foreach_engine { |
|
177 |
uint8 spriteid = grf_load_byte(&buf); |
|
178 |
||
179 |
if (spriteid == 0xfd && rvi[i].image_index != 0xfd) |
|
180 |
_engine_original_sprites[engine + i] = rvi[i].image_index; |
|
181 |
rvi[i].image_index = spriteid; |
|
182 |
} |
|
183 |
break; |
|
184 |
} |
|
185 |
case 0x13: |
|
186 |
{ /* Dual-headed */ |
|
187 |
foreach_engine { |
|
188 |
uint8 dual = grf_load_byte(&buf); |
|
189 |
||
190 |
if (dual) { |
|
191 |
rvi[i].flags |= 1; |
|
192 |
} else { |
|
193 |
rvi[i].flags &= ~1; |
|
194 |
} |
|
195 |
} |
|
196 |
break; |
|
197 |
} |
|
198 |
case 0x14: |
|
199 |
{ /* Cargo capacity */ |
|
200 |
foreach_engine { |
|
201 |
uint8 capacity = grf_load_byte(&buf); |
|
202 |
||
203 |
rvi[i].capacity = capacity; |
|
204 |
} |
|
205 |
break; |
|
206 |
} |
|
207 |
case 0x15: |
|
208 |
{ /* Cargo type */ |
|
209 |
foreach_engine { |
|
210 |
uint8 ctype = grf_load_byte(&buf); |
|
211 |
||
212 |
rvi[i].cargo_type = ctype; |
|
213 |
} |
|
214 |
break; |
|
215 |
} |
|
216 |
case 0x16: |
|
217 |
{ /* Weight */ |
|
218 |
foreach_engine { |
|
219 |
uint8 weight = grf_load_byte(&buf); |
|
220 |
||
221 |
rvi[i].weight = weight; |
|
222 |
} |
|
223 |
break; |
|
224 |
} |
|
225 |
case 0x17: |
|
226 |
{ /* Cost factor */ |
|
227 |
foreach_engine { |
|
228 |
uint8 cfactor = grf_load_byte(&buf); |
|
229 |
||
230 |
rvi[i].base_cost = cfactor; |
|
231 |
} |
|
232 |
break; |
|
233 |
} |
|
234 |
case 0x18: |
|
235 |
{ /* AI rank */ |
|
236 |
/* TODO: _railveh_score should be merged to _rail_vehicle_info. */ |
|
237 |
foreach_engine { |
|
238 |
grf_load_byte(&buf); |
|
239 |
} |
|
240 |
ret = 1; |
|
241 |
break; |
|
242 |
} |
|
243 |
case 0x19: |
|
244 |
{ /* Engine traction type */ |
|
245 |
/* TODO: What do the individual numbers mean? |
|
246 |
* XXX: And in what base are they, in fact? --pasky */ |
|
247 |
foreach_engine { |
|
248 |
uint8 traction = grf_load_byte(&buf); |
|
249 |
int engclass; |
|
250 |
||
251 |
if (traction <= 0x07) |
|
252 |
engclass = 0; |
|
253 |
else if (traction <= 0x27) |
|
254 |
engclass = 1; |
|
255 |
else if (traction <= 0x31) |
|
256 |
engclass = 2; |
|
257 |
else |
|
258 |
break; |
|
259 |
||
260 |
rvi[i].engclass = engclass; |
|
261 |
} |
|
262 |
break; |
|
263 |
} |
|
264 |
/* TODO */ |
|
265 |
/* Fall-through for unimplemented four bytes long properties. */ |
|
266 |
case 0x1d: /* Refit cargo */ |
|
267 |
foreach_engine { |
|
268 |
grf_load_word(&buf); |
|
269 |
} |
|
270 |
/* Fall-through for unimplemented two bytes long properties. */ |
|
271 |
case 0x1b: /* Powered wagons power bonus */ |
|
272 |
foreach_engine { |
|
273 |
grf_load_byte(&buf); |
|
274 |
} |
|
275 |
/* Fall-through for unimplemented one byte long properties. */ |
|
276 |
case 0x1a: /* Sort order */ |
|
277 |
case 0x1c: /* Refit cost */ |
|
278 |
case 0x1e: /* Callback */ |
|
279 |
case 0x1f: /* Tractive effort */ |
|
280 |
case 0x21: /* Shorter tenders */ |
|
281 |
case 0x22: /* Visual */ |
|
282 |
case 0x23: /* Powered wagons weight bonus */ |
|
283 |
/* TODO */ |
|
284 |
foreach_engine { |
|
285 |
grf_load_byte(&buf); |
|
286 |
} |
|
287 |
ret = 1; |
|
288 |
break; |
|
289 |
default: |
|
290 |
ret = 1; |
|
291 |
break; |
|
292 |
} |
|
293 |
*bufp = buf; |
|
294 |
return ret; |
|
295 |
} |
|
296 |
||
297 |
static int ShipVehicleChangeInfo(uint engine, int numinfo, int prop, byte **bufp, int len) |
|
298 |
{ |
|
299 |
ShipVehicleInfo *svi = &_ship_vehicle_info[engine]; |
|
300 |
byte *buf = *bufp; |
|
301 |
int i; |
|
302 |
int ret = 0; |
|
303 |
||
304 |
//printf("e %x prop %x?\n", engine, prop); |
|
305 |
switch (prop) { |
|
306 |
case 0x08: |
|
307 |
{ /* Sprite ID */ |
|
308 |
foreach_engine { |
|
309 |
uint8 spriteid = grf_load_byte(&buf); |
|
310 |
||
311 |
if (spriteid == 0xff) |
|
312 |
spriteid = 0xfd; // ships have different custom id in the GRF file |
|
313 |
||
314 |
// This is currently not used but there's no reason |
|
315 |
// in not having it here for the future. |
|
316 |
if (spriteid == 0xfd |
|
317 |
&& svi[i].image_index != 0xfd) |
|
318 |
_engine_original_sprites[SHIP_ENGINES_INDEX |
|
319 |
+ engine + i] |
|
320 |
= svi[i].image_index; |
|
321 |
svi[i].image_index = spriteid; |
|
322 |
} |
|
323 |
break; |
|
324 |
} |
|
325 |
case 0x09: |
|
326 |
{ /* Refittable */ |
|
327 |
foreach_engine { |
|
328 |
uint8 refittable = grf_load_byte(&buf); |
|
329 |
||
330 |
svi[i].refittable = refittable; |
|
331 |
} |
|
332 |
break; |
|
333 |
} |
|
334 |
case 0x0a: |
|
335 |
{ /* Cost factor */ |
|
336 |
foreach_engine { |
|
337 |
uint8 cost_factor = grf_load_byte(&buf); |
|
338 |
||
339 |
svi[i].base_cost = cost_factor; // ?? is it base_cost? |
|
340 |
} |
|
341 |
break; |
|
342 |
} |
|
343 |
case 0x0b: |
|
344 |
{ /* Speed */ |
|
345 |
foreach_engine { |
|
346 |
uint8 speed = grf_load_byte(&buf); |
|
347 |
||
348 |
svi[i].max_speed = speed; // ?? units |
|
349 |
} |
|
350 |
break; |
|
351 |
} |
|
352 |
case 0x0c: |
|
353 |
{ /* Cargo type */ |
|
354 |
||
355 |
foreach_engine { |
|
356 |
uint8 cargo = grf_load_byte(&buf); |
|
357 |
||
358 |
// XXX: Need to consult this with patchman yet. |
|
359 |
#if 0 |
|
360 |
// Documentation claims this is already the |
|
361 |
// per-landscape cargo type id, but newships.grf |
|
362 |
// assume otherwise. |
|
363 |
cargo = local_cargo_id_ctype[cargo]; |
|
364 |
#endif |
|
365 |
svi[i].cargo_type = cargo; |
|
366 |
} |
|
367 |
break; |
|
368 |
} |
|
369 |
case 0x0d: |
|
370 |
{ /* Cargo capacity */ |
|
371 |
foreach_engine { |
|
372 |
uint16 capacity = grf_load_word(&buf); |
|
373 |
||
374 |
svi[i].capacity = capacity; |
|
375 |
} |
|
376 |
break; |
|
377 |
} |
|
378 |
case 0x0f: |
|
379 |
{ /* Running cost factor */ |
|
380 |
foreach_engine { |
|
381 |
uint8 runcost = grf_load_byte(&buf); |
|
382 |
||
383 |
svi[i].running_cost = runcost; |
|
384 |
} |
|
385 |
break; |
|
386 |
} |
|
387 |
case 0x10: |
|
388 |
{ /* SFX */ |
|
389 |
foreach_engine { |
|
390 |
uint8 sfx = grf_load_byte(&buf); |
|
391 |
||
392 |
svi[i].sfx = sfx; |
|
393 |
} |
|
394 |
break; |
|
395 |
} |
|
396 |
case 0x11: |
|
397 |
{ /* Cargos available for refitting */ |
|
398 |
foreach_engine { |
|
399 |
uint32 refit_mask = grf_load_dword(&buf); |
|
400 |
||
401 |
_engine_refit_masks[SHIP_ENGINES_INDEX + engine + i] = refit_mask; |
|
402 |
} |
|
403 |
break; |
|
404 |
} |
|
405 |
case 0x12: |
|
406 |
{ /* Callback */ |
|
407 |
/* TODO */ |
|
408 |
ret = 1; |
|
409 |
break; |
|
410 |
} |
|
411 |
default: |
|
412 |
ret = 1; |
|
413 |
break; |
|
414 |
} |
|
415 |
||
416 |
*bufp = buf; |
|
417 |
return ret; |
|
418 |
} |
|
419 |
||
420 |
#undef shift_buf |
|
421 |
||
422 |
static void VehicleChangeInfo(byte *buf, int len) |
|
423 |
{ |
|
424 |
byte *bufend = buf + len; |
|
425 |
int i; |
|
426 |
||
427 |
/* <00> <feature> <num-props> <num-info> <id> (<property <new-info>)... |
|
428 |
* |
|
429 |
* B feature 0, 1, 2 or 3 for trains, road vehicles, ships or planes |
|
430 |
* 4 for defining new train station sets |
|
431 |
* B num-props how many properties to change per vehicle/station |
|
432 |
* B num-info how many vehicles/stations to change |
|
433 |
* B id ID of first vehicle/station to change, if num-info is |
|
434 |
* greater than one, this one and the following |
|
435 |
* vehicles/stations will be changed |
|
436 |
* B property what property to change, depends on the feature |
|
437 |
* V new-info new bytes of info (variable size; depends on properties) */ |
|
438 |
/* TODO: Only trains and ships are supported for now. */ |
|
439 |
||
440 |
static const VCI_Handler handler[5] = { |
|
441 |
RailVehicleChangeInfo, |
|
442 |
NULL, |
|
443 |
ShipVehicleChangeInfo, |
|
444 |
NULL, |
|
445 |
NULL, |
|
446 |
}; |
|
447 |
||
448 |
if (len > 5) { |
|
449 |
uint8 feature = buf[1]; |
|
450 |
uint8 numprops = buf[2]; |
|
451 |
uint8 numinfo = buf[3]; |
|
452 |
byte engine = buf[4]; |
|
453 |
EngineInfo *ei; |
|
454 |
||
455 |
if (feature != 0 && feature != 2) { |
|
456 |
grfmsg(GMS_WARN, "VehicleChangeInfo: Unsupported vehicle type %x, skipping.", feature); |
|
457 |
return; |
|
458 |
} |
|
459 |
||
460 |
ei = &_engine_info[engine + _vehshifts[feature]]; |
|
461 |
||
462 |
buf += 5; |
|
463 |
||
464 |
while (numprops-- && buf < bufend) { |
|
465 |
uint8 prop = grf_load_byte(&buf); |
|
466 |
||
467 |
switch (prop) { |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
74
diff
changeset
|
468 |
case 0x00: { |
0 | 469 |
/* Introduction date */ |
470 |
foreach_engine { |
|
471 |
uint16 date = grf_load_word(&buf); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
74
diff
changeset
|
472 |
|
0 | 473 |
ei[i].base_intro = date; |
474 |
} |
|
475 |
break; |
|
476 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
74
diff
changeset
|
477 |
case 0x02: { |
0 | 478 |
/* Decay speed */ |
479 |
foreach_engine { |
|
480 |
uint8 decay = grf_load_byte(&buf); |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
74
diff
changeset
|
481 |
|
0 | 482 |
ei[i].unk2 &= 0x80; |
483 |
ei[i].unk2 |= decay & 0x7f; |
|
484 |
} |
|
485 |
break; |
|
486 |
} |
|
487 |
case 0x03: { |
|
488 |
/* Vehicle life */ |
|
489 |
foreach_engine { |
|
490 |
uint8 life = grf_load_byte(&buf); |
|
491 |
||
492 |
ei[i].lifelength = life; |
|
493 |
} |
|
494 |
break; |
|
495 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
74
diff
changeset
|
496 |
case 0x04: { |
0 | 497 |
/* Model life */ |
498 |
foreach_engine { |
|
499 |
uint8 life = grf_load_byte(&buf); |
|
500 |
||
501 |
ei[i].base_life = life; |
|
502 |
} |
|
503 |
break; |
|
504 |
} |
|
505 |
case 0x06: { |
|
506 |
/* Climates available */ |
|
507 |
foreach_engine { |
|
508 |
uint8 climates = grf_load_byte(&buf); |
|
509 |
||
510 |
ei[i].railtype_climates &= 0xf0; |
|
511 |
ei[i].railtype_climates |= climates; |
|
512 |
} |
|
513 |
break; |
|
514 |
} |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
74
diff
changeset
|
515 |
case 0x07: { |
0 | 516 |
/* Loading speed */ |
517 |
/* Hyronymus explained me what does |
|
518 |
* this mean and insists on having a |
|
519 |
* credit ;-). --pasky */ |
|
520 |
/* TODO: This needs to be supported by |
|
521 |
* LoadUnloadVehicle() first. */ |
|
522 |
foreach_engine { |
|
523 |
grf_load_byte(&buf); |
|
524 |
} |
|
525 |
goto ignoring; |
|
526 |
} |
|
527 |
default: |
|
528 |
{ |
|
529 |
if (handler[feature](engine, numinfo, prop, &buf, bufend - buf)) { |
|
530 |
ignoring: |
|
531 |
grfmsg(GMS_WARN, "VehicleChangeInfo: Ignoring property %x.", prop); |
|
532 |
} |
|
533 |
break; |
|
534 |
} |
|
535 |
} |
|
536 |
} |
|
537 |
} |
|
538 |
#undef shift_buf |
|
539 |
} |
|
540 |
||
541 |
||
542 |
/* A sprite superset contains all sprites of a given vehicle (or multiple |
|
543 |
* vehicles) when carrying given cargo. It consists of several sprite sets. |
|
544 |
* Superset ids are refered as "cargo id"s by TTDPatch documentation, |
|
545 |
* contributing to the global confusion. |
|
546 |
* |
|
547 |
* A sprite set contains all sprites of a given vehicle carrying given cargo at |
|
548 |
* a given *stage* - that is usually its load stage. Ie. you can have a |
|
549 |
* spriteset for an empty wagon, wagon full of coal, half-filled wagon etc. |
|
550 |
* Each spriteset contains eight sprites (one per direction) or four sprites if |
|
551 |
* the vehicle is symmetric. */ |
|
552 |
||
553 |
static int _spriteset_start; |
|
554 |
static int _spriteset_numsets; |
|
555 |
static int _spriteset_numents; |
|
556 |
static int _spriteset_feature; |
|
557 |
||
558 |
static int _spritesset_count; |
|
559 |
static struct SpriteSuperSet *_spritesset; |
|
560 |
||
561 |
static void SpriteNewSet(byte *buf, int len) |
|
562 |
{ |
|
563 |
/* <01> <feature> <num-sets> <num-ent> |
|
564 |
* |
|
565 |
* B feature feature to define sprites for |
|
566 |
* 0, 1, 2, 3: veh-type, 4: train stations |
|
567 |
* B num-sets number of sprite sets |
|
568 |
* B num-ent how many entries per sprite set |
|
569 |
* For vehicles, this is the number of different |
|
570 |
* vehicle directions in each sprite set |
|
571 |
* Set num-dirs=8, unless your sprites are symmetric. |
|
572 |
* In that case, use num-dirs=4. |
|
573 |
* For stations, must be 12 (hex) for the eighteen |
|
574 |
* different sprites that make up a station */ |
|
575 |
/* TODO: No stations support. */ |
|
576 |
||
577 |
if (len == 4) { |
|
578 |
uint8 feature = buf[1]; |
|
579 |
||
580 |
if (feature == 4) { |
|
581 |
_spriteset_start = 0; |
|
582 |
grfmsg(GMS_WARN, "SpriteNewSet: Stations unsupported, skipping."); |
|
583 |
return; |
|
584 |
} |
|
585 |
||
586 |
_spriteset_start = _cur_spriteid + 1; |
|
587 |
_spriteset_numsets = buf[2]; |
|
588 |
_spriteset_numents = buf[3]; |
|
589 |
_spriteset_feature = feature; |
|
590 |
} |
|
591 |
} |
|
592 |
||
593 |
static void SpriteNewSuperset(byte *buf, int len) |
|
594 |
{ |
|
595 |
byte *bufend = buf + len; |
|
596 |
||
597 |
/* <02> <feature> <set-id> <type/num-entries> <feature-specific-data...> |
|
598 |
* |
|
599 |
* B feature see action 1 |
|
600 |
* B set-id ID of this particular definition |
|
601 |
* B type/num-entries |
|
602 |
* if 80 or greater, this is a randomized or variational |
|
603 |
* list definition, see below |
|
604 |
* otherwise it specifies a number of entries, the exact |
|
605 |
* meaning depends on the feature |
|
606 |
* V feature-specific-data (huge mess, don't even look it up --pasky) */ |
|
607 |
/* TODO: Only trains supported now. No 0x80-types (ugh). */ |
|
608 |
/* TODO: Also, empty sprites aren't handled for now. Need to investigate |
|
609 |
* the "opacity" rules for these, that is which sprite to fall back to |
|
610 |
* when. --pasky */ |
|
611 |
||
612 |
if (bufend - buf > 4) { |
|
613 |
uint8 feature = buf[1]; |
|
614 |
uint8 setid = buf[2]; |
|
615 |
uint8 numloaded = buf[3]; |
|
616 |
uint8 numloading = buf[4]; |
|
617 |
struct SpriteSuperSet *superset; |
|
618 |
int i; |
|
619 |
||
620 |
if (feature == 4) { |
|
621 |
grfmsg(GMS_WARN, "SpriteNewSuperset: Stations unsupported, skipping."); |
|
622 |
return; |
|
623 |
} |
|
624 |
||
625 |
if (numloaded == 0x81) { |
|
626 |
// XXX: This is _VERY_ ad hoc just to handle Dm3. And that is |
|
627 |
// a semi-futile ask because the great Patchman himself says |
|
628 |
// this is just buggy. It dereferences last (first) byte of |
|
629 |
// a schedule list pointer of the vehicle and if it's 0xff |
|
630 |
// it uses superset 01, otherwise it uses superset 00. Now |
|
631 |
// if _you_ understand _that_... We just assume it is never |
|
632 |
// 0xff and therefore go for superset 00. --pasky |
|
633 |
uint8 var = buf[4]; |
|
634 |
//uint8 shiftnum = buf[5]; |
|
635 |
//uint8 andmask = buf[6]; |
|
636 |
uint8 nvar = buf[7]; |
|
637 |
//uint32 val; |
|
638 |
uint16 def; |
|
193
0a7025304867
(svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents:
74
diff
changeset
|
639 |
|
0 | 640 |
grfmsg(GMS_WARN, "SpriteNewSuperset(0x81): Unsupported variable %x. Using default cid.", var); |
641 |
||
642 |
//val = (0xff << shiftnum) & andmask; |
|
643 |
||
644 |
//Go for the default. |
|
645 |
if (setid >= _spritesset_count) { |
|
646 |
_spritesset_count = setid + 1; |
|
647 |
_spritesset = realloc(_spritesset, _spritesset_count * sizeof(struct SpriteSuperSet)); |
|
648 |
} |
|
649 |
buf += 8 + nvar * 4; |
|
650 |
def = grf_load_word(&buf); |
|
651 |
_spritesset[setid] = _spritesset[def]; |
|
652 |
return; |
|
653 |
||
654 |
} else if (numloaded & 0x80) { |
|
655 |
grfmsg(GMS_WARN, "SpriteNewSuperset(0x%x): Unsupported special superset.", numloaded); |
|
656 |
return; |
|
657 |
} |
|
658 |
||
659 |
if (!_spriteset_start) { |
|
660 |
grfmsg(GMS_WARN, "SpriteNewSuperset: No sprite set to work on! Skipping."); |
|
661 |
return; |
|
662 |
} |
|
663 |
||
664 |
if (_spriteset_feature != feature) { |
|
665 |
grfmsg(GMS_WARN, "SpriteNewSuperset: Superset feature %x doesn't match set feature %x! Skipping.", feature, _spriteset_feature); |
|
666 |
return; |
|
667 |
} |
|
668 |
||
669 |
if (setid >= _spritesset_count) { |
|
670 |
_spritesset_count = setid + 1; |
|
671 |
_spritesset = realloc(_spritesset, _spritesset_count * sizeof(struct SpriteSuperSet)); |
|
672 |
} |
|
673 |
superset = &_spritesset[setid]; |
|
674 |
memset(superset, 0, sizeof(struct SpriteSuperSet)); |
|
675 |
superset->sprites_per_set = _spriteset_numents; |
|
676 |
||
677 |
buf += 5; |
|
678 |
||
679 |
for (i = 0; buf < bufend && i < numloaded; i++) { |
|
680 |
uint16 spriteset_id = grf_load_word(&buf); |
|
681 |
||
682 |
if (_spritesset[setid].loaded_count > 16) { |
|
683 |
grfmsg(GMS_WARN, "SpriteNewSuperset: More than 16 sprites in superset %x, skipping.", setid); |
|
684 |
return; |
|
685 |
} |
|
686 |
superset->loaded[superset->loaded_count++] |
|
687 |
= _spriteset_start + spriteset_id * _spriteset_numents; |
|
688 |
} |
|
689 |
||
690 |
for (i = 0; buf < bufend && i < numloading; i++) { |
|
691 |
uint16 spriteset_id = grf_load_word(&buf); |
|
692 |
||
693 |
if (_spritesset[setid].loading_count > 16) { |
|
694 |
grfmsg(GMS_WARN, "SpriteNewSuperset: More than 16 sprites in superset %x, skipping.", setid); |
|
695 |
return; |
|
696 |
} |
|
697 |
superset->loading[superset->loading_count++] = _spriteset_start + spriteset_id * _spriteset_numents; |
|
698 |
} |
|
699 |
} |
|
700 |
} |
|
701 |
||
702 |
static void VehicleMapSpriteSuperset(byte *buf, int len) |
|
703 |
{ |
|
704 |
byte *bufend = buf + len; |
|
705 |
/* <03> <feature> <n-id> <ids>... <num-cid> [<cargo-type> <cid>]... <def-cid> |
|
706 |
* |
|
707 |
* B feature see action 0 |
|
708 |
* B n-id bits 0-6: how many IDs this definition applies to |
|
709 |
* bit 7: if set, this is a wagon override definition (see below) |
|
710 |
* B ids the IDs for which this definition applies |
|
711 |
* B num-cid number of cargo IDs in this definition |
|
712 |
* can be zero, in that case the def-cid is used always |
|
713 |
* B cargo-type type of this cargo type (e.g. mail=2, wood=7, see below) |
|
714 |
* W cid cargo ID for this type of cargo |
|
715 |
* W def-cid default cargo ID */ |
|
716 |
/* TODO: Only trains supported now. */ |
|
717 |
/* TODO: Multiple cargo support could be useful even for trains/cars - |
|
718 |
* cargo id 0xff is used for showing images in the build train list. */ |
|
719 |
||
720 |
static byte *last_engines; |
|
721 |
static int last_engines_count; |
|
722 |
||
723 |
if (bufend - buf > 6) { |
|
724 |
uint8 feature = buf[1]; |
|
725 |
uint8 idcount = buf[2] & 0x7f; |
|
726 |
int wagover = buf[2] & 0x80; |
|
727 |
uint8 cidcount = buf[3 + idcount]; |
|
728 |
int c, i; |
|
729 |
||
730 |
if (feature == 4) { |
|
731 |
grfmsg(GMS_WARN, "VehicleMapSpriteSuperset: Stations unsupported, skipping."); |
|
732 |
return; |
|
733 |
} |
|
734 |
||
735 |
// FIXME: Tropicset contains things like: |
|
736 |
// 03 00 01 19 01 00 00 00 00 - this is missing one 00 at the end, |
|
737 |
// what should we exactly do with that? --pasky |
|
738 |
||
739 |
if (!_spriteset_start || !_spritesset) { |
|
740 |
grfmsg(GMS_WARN, "VehicleMapSpriteSuperset: No sprite set to work on! Skipping."); |
|
741 |
return; |
|
742 |
} |
|
743 |
||
744 |
if (!wagover && last_engines_count != idcount) { |
|
745 |
last_engines = realloc(last_engines, idcount); |
|
746 |
last_engines_count = idcount; |
|
747 |
} |
|
748 |
||
749 |
for (i = 0; i < idcount; i++) { |
|
750 |
uint8 engine = buf[3 + i] + _vehshifts[feature]; |
|
751 |
byte *bp = &buf[4 + idcount]; |
|
752 |
||
753 |
for (c = 0; c < cidcount; c++) { |
|
754 |
uint8 ctype = grf_load_byte(&bp); |
|
755 |
uint16 supersetid = grf_load_word(&bp); |
|
756 |
||
757 |
if (supersetid >= _spritesset_count) { |
|
758 |
grfmsg(GMS_WARN, "VehicleMapSpriteSuperset: Spriteset %x out of range %x, skipping.", supersetid, _spritesset_count); |
|
759 |
return; |
|
760 |
} |
|
761 |
||
762 |
if (ctype == 0xff) |
|
763 |
ctype = CID_PURCHASE; |
|
764 |
||
765 |
if (wagover) { |
|
766 |
// TODO: No multiple cargo types per vehicle yet. --pasky |
|
767 |
SetWagonOverrideSprites(engine, &_spritesset[supersetid], last_engines, last_engines_count); |
|
768 |
} else { |
|
769 |
SetCustomEngineSprites(engine, ctype, &_spritesset[supersetid]); |
|
770 |
last_engines[i] = engine; |
|
771 |
} |
|
772 |
} |
|
773 |
} |
|
774 |
||
775 |
{ |
|
776 |
byte *bp = buf + 4 + idcount + cidcount * 3; |
|
777 |
uint16 supersetid = grf_load_word(&bp); |
|
778 |
||
779 |
for (i = 0; i < idcount; i++) { |
|
780 |
uint8 engine = buf[3 + i] + _vehshifts[feature]; |
|
781 |
||
782 |
// Don't tell me you don't love duplicated code! |
|
783 |
if (supersetid >= _spritesset_count) { |
|
784 |
grfmsg(GMS_WARN, "VehicleMapSpriteSuperset: Spriteset %x out of range %x, skipping.", supersetid, _spritesset_count); |
|
785 |
return; |
|
786 |
} |
|
787 |
||
788 |
if (wagover) { |
|
789 |
// TODO: No multiple cargo types per vehicle yet. --pasky |
|
790 |
SetWagonOverrideSprites(engine, &_spritesset[supersetid], last_engines, last_engines_count); |
|
791 |
} else { |
|
792 |
SetCustomEngineSprites(engine, CID_DEFAULT, &_spritesset[supersetid]); |
|
793 |
last_engines[i] = engine; |
|
794 |
} |
|
795 |
} |
|
796 |
} |
|
797 |
} |
|
798 |
} |
|
799 |
||
800 |
static void VehicleNewName(byte *buf, int len) |
|
801 |
{ |
|
802 |
/* <04> <veh-type> <language-id> <num-veh> <offset> <data...> |
|
803 |
* |
|
804 |
* B veh-type see action 0 |
|
805 |
* B language-id language ID with bit 7 cleared (see below) |
|
806 |
* B num-veh number of vehicles which are getting a new name |
|
807 |
* B offset number of the first vehicle that gets a new name |
|
808 |
* S data new texts, each of them zero-terminated, after |
|
809 |
* which the next name begins. */ |
|
810 |
/* TODO: No support for changing non-vehicle text. Perhaps we shouldn't |
|
811 |
* implement it at all, but it could be useful for some "modpacks" |
|
812 |
* (completely new scenarios changing all graphics and logically also |
|
813 |
* factory names etc). We should then also support all languages (by |
|
814 |
* name), not only the original four ones. --pasky */ |
|
815 |
||
816 |
if (len > 5) { |
|
817 |
uint8 feature = buf[1]; |
|
818 |
uint8 lang = buf[2]; |
|
819 |
uint8 id = buf[4] + _vehshifts[feature]; |
|
820 |
uint8 endid = id + buf[3]; |
|
821 |
||
822 |
if (lang & 0x80) { |
|
823 |
grfmsg(GMS_WARN, "VehicleNewName: No support for changing in-game texts. Skipping."); |
|
824 |
return; |
|
825 |
} |
|
826 |
||
827 |
if (!(lang & 3)) { |
|
828 |
/* XXX: If non-English name, silently skip it. */ |
|
829 |
return; |
|
830 |
} |
|
831 |
||
832 |
buf += 5, len -= 5; |
|
833 |
for (; id < endid && len > 0; id++) { |
|
834 |
int ofs = strlen(buf) + 1; |
|
835 |
||
836 |
if (ofs < 128) |
|
837 |
SetCustomEngineName(id, buf); |
|
838 |
buf += ofs, len -= ofs; |
|
839 |
} |
|
840 |
} |
|
841 |
} |
|
842 |
||
843 |
static void GraphicsNew(byte *buf, int len) |
|
844 |
{ |
|
845 |
/* <05> <graphics-type> <num-sprites> <other data...> |
|
846 |
* |
|
847 |
* B graphics-type What set of graphics the sprites define. |
|
848 |
* B num-sprites How many sprites are in this set? |
|
849 |
* V other data Graphics type specific data. Currently unused. */ |
|
850 |
/* TODO */ |
|
851 |
} |
|
852 |
||
853 |
static void CfgApply(byte *buf, int len) |
|
854 |
{ |
|
855 |
/* <06> <param-num> <param-size> <offset> ... <FF> |
|
856 |
* |
|
857 |
* B param-num Number of parameter to substitute (First = "zero") |
|
858 |
* Ignored if that parameter was not specified in newgrf.cfg |
|
859 |
* B param-size How many bytes to replace. If larger than 4, the |
|
860 |
* bytes of the following parameter are used. In that |
|
861 |
* case, nothing is applied unless *all* parameters |
|
862 |
* were specified. |
|
863 |
* B offset Offset into data from beginning of next sprite |
|
864 |
* to place where parameter is to be stored. */ |
|
865 |
/* TODO */ |
|
866 |
} |
|
867 |
||
868 |
static void SkipIf(byte *buf, int len) |
|
869 |
{ |
|
870 |
/* <07/09> <param-num> <param-size> <condition-type> <value> <num-sprites> |
|
871 |
* |
|
872 |
* B param-num |
|
873 |
* B param-size |
|
874 |
* B condition-type |
|
875 |
* V value |
|
876 |
* B num-sprites */ |
|
877 |
/* TODO: We only support few tests. */ |
|
878 |
/* TODO: More params. More condition types. */ |
|
879 |
||
880 |
if (len > 5) { |
|
881 |
uint8 param = buf[1]; |
|
882 |
uint8 paramsize = buf[2]; |
|
883 |
uint8 condtype = buf[3]; |
|
884 |
uint8 numsprites = buf[4 + paramsize]; |
|
885 |
int val, result; |
|
886 |
||
887 |
if (param == 0x83) { |
|
888 |
val = _opt.landscape; |
|
889 |
} else { |
|
890 |
grfmsg(GMS_WARN, "Unsupported param %x. Ignoring test.", param); |
|
891 |
return; |
|
892 |
} |
|
893 |
||
894 |
switch (condtype) { |
|
895 |
case 2: result = (buf[4] == val); |
|
896 |
break; |
|
897 |
case 3: result = (buf[4] != val); |
|
898 |
break; |
|
899 |
default: |
|
900 |
grfmsg(GMS_WARN, "Unsupported test %d. Ignoring.", condtype); |
|
901 |
return; |
|
902 |
} |
|
903 |
||
904 |
if (!result) |
|
905 |
return; |
|
906 |
||
907 |
_skip_sprites = numsprites; |
|
908 |
if (_skip_sprites == 0) { |
|
909 |
/* Zero means there are no sprites to skip, so |
|
910 |
* we use -1 to indicate that all further |
|
911 |
* sprites should be skipped. */ |
|
912 |
_skip_sprites = -1; |
|
913 |
} |
|
914 |
} |
|
915 |
} |
|
916 |
||
917 |
static void GRFInfo(byte *buf, int len) |
|
918 |
{ |
|
919 |
/* <08> <version> <grf-id> <name> <info> |
|
920 |
* |
|
921 |
* B version newgrf version, currently 06 |
|
922 |
* 4*B grf-id globally unique ID of this .grf file |
|
923 |
* S name name of this .grf set |
|
924 |
* S info string describing the set, and e.g. author and copyright */ |
|
925 |
/* TODO: Check version. (We should have own versioning done somehow.) */ |
|
926 |
||
927 |
if (len > 8) { |
|
928 |
uint8 version = buf[1]; |
|
929 |
// this is de facto big endian - grf_load_dword() unsuitable |
|
930 |
uint32 grfid = buf[2] << 24 | buf[3] << 16 | buf[4] << 8 | buf[5]; |
|
66
d0f669221657
(svn r67) -Forget to change debug message for grfspecial.c
darkvater
parents:
65
diff
changeset
|
931 |
DEBUG(grf, 1) ("[%s] Loaded GRFv%d set %08lx - %s:\n%s\n", _cur_grffile, version, grfid, buf+6, buf+6+strlen(buf+6)+1); |
0 | 932 |
} |
933 |
} |
|
934 |
||
935 |
static void SpriteReplace(byte *buf, int len) |
|
936 |
{ |
|
937 |
/* <0A> <num-sets> <set1> [<set2> ...] |
|
938 |
* <set>: <num-sprites> <first-sprite> |
|
939 |
* |
|
940 |
* B num-sets How many sets of sprites to replace. |
|
941 |
* Each set: |
|
942 |
* B num-sprites How many sprites are in this set |
|
943 |
* W first-sprite First sprite number to replace */ |
|
944 |
/* TODO */ |
|
945 |
} |
|
946 |
||
947 |
static void GRFError(byte *buf, int len) |
|
948 |
{ |
|
949 |
/* <0B> <severity> <language-id> <message-id> [<message...> 00] [<data...>] 00 [<parnum>] |
|
950 |
* |
|
951 |
* B severity 00: notice, contine loading grf file |
|
952 |
* 01: warning, continue loading grf file |
|
953 |
* 02: error, but continue loading grf file, and attempt |
|
954 |
* loading grf again when loading or starting next game |
|
955 |
* 03: error, abort loading and prevent loading again in |
|
956 |
* the future (only when restarting the patch) |
|
957 |
* B language-id see action 4, use 1F for built-in error messages |
|
958 |
* B message-id message to show, see below |
|
959 |
* S message for custom messages (message-id FF), text of the message |
|
960 |
* not present for built-in messages. |
|
961 |
* V data additional data for built-in (or custom) messages |
|
962 |
* B parnum see action 6, only used with built-in message 03 */ |
|
963 |
/* TODO: For now we just show the message, sometimes incomplete and never translated. */ |
|
964 |
||
965 |
static const char * const msgstr[4] = { |
|
966 |
"Requires at least pseudo-TTDPatch version %s.", |
|
967 |
"This file is for %s version of TTD.", |
|
968 |
"Designed to be used with %s.", |
|
969 |
"Invalid parameter %s.", |
|
970 |
}; |
|
971 |
||
972 |
if (len > 5) { |
|
973 |
uint8 severity = buf[1]; |
|
974 |
uint8 msgid = buf[3]; |
|
975 |
||
976 |
if (msgid == 0xff) { |
|
977 |
grfmsg(severity, "%s", buf+4); |
|
978 |
} else { |
|
979 |
grfmsg(severity, msgstr[msgid], buf+4); |
|
980 |
} |
|
981 |
} |
|
982 |
} |
|
983 |
||
984 |
static void GRFComment(byte *buf, int len) |
|
985 |
{ |
|
986 |
/* <0C> [<ignored...>] |
|
987 |
* |
|
988 |
* V ignored Anything following the 0C is ignored */ |
|
989 |
} |
|
990 |
||
991 |
static void ParamSet(byte *buf, int len) |
|
992 |
{ |
|
993 |
/* <0D> <target> <operation> <source1> <source2> [<data>] |
|
994 |
* |
|
995 |
* B target parameter number where result is stored |
|
996 |
* B operation operation to perform, see below |
|
997 |
* B source1 first source operand |
|
998 |
* B source2 second source operand |
|
999 |
* D data data to use in the calculation, not necessary |
|
1000 |
* if both source1 and source2 refer to actual parameters |
|
1001 |
* |
|
1002 |
* Operations |
|
1003 |
* 00 Set parameter equal to source1 |
|
1004 |
* 01 Addition, source1 + source2 |
|
1005 |
* 02 Subtraction, source1 - source2 |
|
1006 |
* 03 Unsigned multiplication, source1 * source2 (both unsigned) |
|
1007 |
* 04 Signed multiplication, source1 * source2 (both signed) |
|
1008 |
* 05 Unsigned bit shift, source1 by source2 (source2 taken to be a |
|
1009 |
* signed quantity; left shift if positive and right shift if |
|
1010 |
* negative, source1 is unsigned) |
|
1011 |
* 06 Signed bit shift, source1 by source2 |
|
1012 |
* (source2 like in 05, and source1 as well) |
|
1013 |
*/ |
|
1014 |
/* TODO */ |
|
1015 |
} |
|
1016 |
||
1017 |
static void GRFInhibit(byte *buf, int len) |
|
1018 |
{ |
|
1019 |
/* <0E> <num> <grfids...> |
|
1020 |
* |
|
1021 |
* B num Number of GRFIDs that follow |
|
1022 |
* D grfids GRFIDs of the files to deactivate */ |
|
1023 |
/* TODO */ |
|
1024 |
} |
|
1025 |
||
1026 |
/* Here we perform initial decoding of some special sprites (as are they |
|
1027 |
* described at http://www.ttdpatch.net/src/newgrf.txt, but this is only a very |
|
1028 |
* partial implementation yet; also, we ignore the stages stuff). */ |
|
1029 |
/* XXX: We consider GRF files trusted. It would be trivial to exploit OTTD by |
|
1030 |
* a crafted invalid GRF file. We should tell that to the user somehow, or |
|
1031 |
* better make this more robust in the future. */ |
|
1032 |
||
1033 |
void DecodeSpecialSprite(const char *filename, int num, int spriteid) |
|
1034 |
{ |
|
1035 |
#define NUM_ACTIONS 0xf |
|
1036 |
static const SpecialSpriteHandler handlers[NUM_ACTIONS] = { |
|
1037 |
/* 0x0 */ VehicleChangeInfo, |
|
1038 |
/* 0x1 */ SpriteNewSet, |
|
1039 |
/* 0x2 */ SpriteNewSuperset, |
|
1040 |
/* 0x3 */ VehicleMapSpriteSuperset, |
|
1041 |
/* 0x4 */ VehicleNewName, |
|
1042 |
/* 0x5 */ GraphicsNew, |
|
1043 |
/* 0x6 */ CfgApply, |
|
1044 |
/* 0x7 */ SkipIf, |
|
1045 |
/* 0x8 */ GRFInfo, |
|
1046 |
/* 0x9 */ SkipIf, |
|
1047 |
/* 0xa */ SpriteReplace, |
|
1048 |
/* 0xb */ GRFError, |
|
1049 |
/* 0xc */ GRFComment, |
|
1050 |
/* 0xd */ ParamSet, |
|
1051 |
/* 0xe */ GRFInhibit, |
|
1052 |
}; |
|
1053 |
byte action; |
|
1054 |
byte *buf = malloc(num); |
|
1055 |
int i; |
|
1056 |
||
1057 |
_cur_grffile = filename; |
|
1058 |
_cur_spriteid = spriteid; |
|
1059 |
||
1060 |
for (i = 0; i != num; i++) |
|
1061 |
buf[i] = FioReadByte(); |
|
1062 |
||
1063 |
action = buf[0]; |
|
1064 |
if (action < NUM_ACTIONS) { |
|
1065 |
handlers[action](buf, num); |
|
1066 |
} else { |
|
1067 |
grfmsg(GMS_WARN, "Unknown special sprite action %x, skipping.", action); |
|
1068 |
} |
|
1069 |
||
1070 |
free(buf); |
|
1071 |
#undef NUM_ACTIONS |
|
1072 |
} |