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