grfspecial.c
changeset 452 520e4ed6945d
parent 451 1c72622b9701
child 453 5a269236cefa
equal deleted inserted replaced
451:1c72622b9701 452:520e4ed6945d
     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 #include "station.h"
       
    10 #include "sprite.h"
       
    11 
       
    12 /* TTDPatch extended GRF format codec
       
    13  * (c) Petr Baudis 2004 (GPL'd)
       
    14  * Changes by Florian octo Forster are (c) by the OpenTTD development team.
       
    15  *
       
    16  * Contains portions of documentation by TTDPatch team.
       
    17  * Thanks especially to Josef Drexler for the documentation as well as a lot
       
    18  * of help at #tycoon. Also thanks to Michael Blunck for is GRF files which
       
    19  * served as subject to the initial testing of this codec. */
       
    20 
       
    21 extern int _skip_sprites;
       
    22 extern int _replace_sprites_count[16];
       
    23 extern int _replace_sprites_offset[16];
       
    24 extern int _traininfo_vehicle_pitch;
       
    25 
       
    26 struct GRFFile {
       
    27 	char *filename;
       
    28 	uint32 grfid;
       
    29 	uint16 flags;
       
    30 	uint16 sprite_offset;
       
    31 	struct GRFFile *next;
       
    32 
       
    33 	/* A sprite group contains all sprites of a given vehicle (or multiple
       
    34 	 * vehicles) when carrying given cargo. It consists of several sprite
       
    35 	 * sets.  Group ids are refered as "cargo id"s by TTDPatch
       
    36 	 * documentation, contributing to the global confusion.
       
    37 	 *
       
    38 	 * A sprite set contains all sprites of a given vehicle carrying given
       
    39 	 * cargo at a given *stage* - that is usually its load stage. Ie. you
       
    40 	 * can have a spriteset for an empty wagon, wagon full of coal,
       
    41 	 * half-filled wagon etc.  Each spriteset contains eight sprites (one
       
    42 	 * per direction) or four sprites if the vehicle is symmetric. */
       
    43 
       
    44 	int spriteset_start;
       
    45 	int spriteset_numsets;
       
    46 	int spriteset_numents;
       
    47 	int spriteset_feature;
       
    48 
       
    49 	int spritegroups_count;
       
    50 	struct SpriteGroup *spritegroups;
       
    51 
       
    52 	struct StationSpec stations[256];
       
    53 };
       
    54 
       
    55 static struct GRFFile *_cur_grffile, *_first_grffile;
       
    56 static int _cur_spriteid;
       
    57 static int _cur_stage;
       
    58 extern int _custom_sprites_base;
       
    59 
       
    60 static int32 _paramlist[0x7f];
       
    61 static int _param_max;
       
    62 
       
    63 /* 32 * 8 = 256 flags. Apparently TTDPatch uses this many.. */
       
    64 static uint32 _ttdpatch_flags[8];
       
    65 
       
    66 
       
    67 enum grfspec_feature {
       
    68 	GSF_TRAIN,
       
    69 	GSF_ROAD,
       
    70 	GSF_SHIP,
       
    71 	GSF_AIRCRAFT,
       
    72 	GSF_STATION,
       
    73 	GSF_BRIDGE,
       
    74 	GSF_TOWNHOUSE,
       
    75 };
       
    76 
       
    77 
       
    78 typedef void (*SpecialSpriteHandler)(byte *buf, int len);
       
    79 
       
    80 static const int _vehcounts[4] = {
       
    81 	/* GSF_TRAIN */    NUM_TRAIN_ENGINES,
       
    82 	/* GSF_ROAD */     NUM_ROAD_ENGINES,
       
    83 	/* GSF_SHIP */     NUM_SHIP_ENGINES,
       
    84 	/* GSF_AIRCRAFT */ NUM_AIRCRAFT_ENGINES
       
    85 };
       
    86 
       
    87 static const int _vehshifts[4] = {
       
    88 	/* GSF_TRAIN */    0,
       
    89 	/* GSF_ROAD */     ROAD_ENGINES_INDEX,
       
    90 	/* GSF_SHIP */     SHIP_ENGINES_INDEX,
       
    91 	/* GSF_AIRCRAFT */ AIRCRAFT_ENGINES_INDEX,
       
    92 };
       
    93 
       
    94 
       
    95 /* Debugging messages policy:
       
    96  *
       
    97  * These should be the severities used for direct DEBUG() calls
       
    98  * (there is room for exceptions, but you have to have a good cause):
       
    99  *
       
   100  * 0..2 - dedicated to grfmsg()
       
   101  * 3
       
   102  * 4
       
   103  * 5
       
   104  * 6 - action handler entry reporting - one per action
       
   105  * 7 - basic action progress reporting - in loops, only single one allowed
       
   106  * 8 - more detailed progress reporting - less important stuff, in deep loops etc
       
   107  * 9 - extremely detailed progress reporting - detailed reports inside of deep loops and so
       
   108  */
       
   109 
       
   110 
       
   111 enum grfmsg_severity {
       
   112 	GMS_NOTICE,
       
   113 	GMS_WARN,
       
   114 	GMS_ERROR,
       
   115 	GMS_FATAL,
       
   116 };
       
   117 
       
   118 static void CDECL grfmsg(enum grfmsg_severity severity, const char *str, ...)
       
   119 {
       
   120 	static const char * const severitystr[4] = { "Notice", "Warning", "Error", "Fatal" };
       
   121 	int export_severity = 0;
       
   122 	char buf[1024];
       
   123 	va_list va;
       
   124 
       
   125 	va_start(va, str);
       
   126 	vsprintf(buf, str, va);
       
   127 	va_end(va);
       
   128 
       
   129 	export_severity = 2 - (severity == GMS_FATAL ? 2 : severity);
       
   130 	DEBUG(grf, export_severity) ("[%s][%s] %s", _cur_grffile->filename, severitystr[severity], buf);
       
   131 }
       
   132 
       
   133 
       
   134 #define check_length(real, wanted, where) \
       
   135 do { \
       
   136 	if (real < wanted) { \
       
   137 		grfmsg(GMS_ERROR, "%s: Invalid special sprite length %d (expected %d)!", where, real, wanted); \
       
   138 		return; \
       
   139 	} \
       
   140 } while (0)
       
   141 
       
   142 
       
   143 static byte INLINE grf_load_byte(byte **buf)
       
   144 {
       
   145 	return *(*buf)++;
       
   146 }
       
   147 
       
   148 static uint16 grf_load_word(byte **buf)
       
   149 {
       
   150 	uint16 val;
       
   151 	byte *p = *buf;
       
   152 	val = p[0];
       
   153 	val |= p[1] << 8;
       
   154 	*buf = p + 2;
       
   155 	return val;
       
   156 }
       
   157 
       
   158 static uint16 grf_load_dword(byte **buf)
       
   159 {
       
   160 	uint32 val;
       
   161 	byte *p = *buf;
       
   162 	val = p[0];
       
   163 	val |= p[1] << 8;
       
   164 	val |= p[2] << 16;
       
   165 	val |= p[3] << 24;
       
   166 	*buf = p + 4;
       
   167 	return val;
       
   168 }
       
   169 
       
   170 
       
   171 static struct GRFFile *GetFileByGRFID(uint32 grfid)
       
   172 {
       
   173 	struct GRFFile *file;
       
   174 
       
   175 	file = _first_grffile;
       
   176 	while ((file != NULL) && (file->grfid != grfid))
       
   177 		file = file->next;
       
   178 
       
   179 	return file;
       
   180 }
       
   181 
       
   182 static struct GRFFile *GetFileByFilename(const char *filename)
       
   183 {
       
   184 	struct GRFFile *file;
       
   185 
       
   186 	file = _first_grffile;
       
   187 	while ((file != NULL) && strcmp(file->filename, filename))
       
   188 		file = file->next;
       
   189 
       
   190 	return file;
       
   191 }
       
   192 
       
   193 
       
   194 typedef bool (*VCI_Handler)(uint engine, int numinfo, int prop, byte **buf, int len);
       
   195 
       
   196 #define FOR_EACH_OBJECT for (i = 0; i < numinfo; i++)
       
   197 
       
   198 static void dewagonize(int condition, int engine)
       
   199 {
       
   200 	EngineInfo *ei = &_engine_info[engine];
       
   201 	RailVehicleInfo *rvi = &_rail_vehicle_info[engine];
       
   202 
       
   203 	if (condition != 0) {
       
   204 		ei->unk2 &= ~0x80;
       
   205 		rvi->flags &= ~2;
       
   206 	} else {
       
   207 		ei->unk2 |= 0x80;
       
   208 		rvi->flags |= 2;
       
   209 	}
       
   210 }
       
   211 
       
   212 static bool RailVehicleChangeInfo(uint engine, int numinfo, int prop, byte **bufp, int len)
       
   213 {
       
   214 	EngineInfo *ei = &_engine_info[engine];
       
   215 	RailVehicleInfo *rvi = &_rail_vehicle_info[engine];
       
   216 	byte *buf = *bufp;
       
   217 	int i;
       
   218 	bool ret = false;
       
   219 
       
   220 	switch (prop) {
       
   221 		case 0x05: {	/* Track type */
       
   222 			FOR_EACH_OBJECT {
       
   223 				uint8 tracktype = grf_load_byte(&buf);
       
   224 
       
   225 				ei[i].railtype_climates &= 0xf;
       
   226 				ei[i].railtype_climates |= tracktype << 4;
       
   227 			}
       
   228 		}	break;
       
   229 		case 0x08: {	/* AI passenger service */
       
   230 			/* TODO */
       
   231 			FOR_EACH_OBJECT {
       
   232 				grf_load_byte(&buf);
       
   233 			}
       
   234 			ret = true;
       
   235 		}	break;
       
   236 		case 0x09: {	/* Speed */
       
   237 			FOR_EACH_OBJECT {
       
   238 				uint16 speed = grf_load_word(&buf);
       
   239 
       
   240 				rvi[i].max_speed = speed;
       
   241 				dewagonize(speed, engine + i);
       
   242 			}
       
   243 		}	break;
       
   244 		case 0x0B: {	/* Power */
       
   245 			FOR_EACH_OBJECT {
       
   246 				uint16 power = grf_load_word(&buf);
       
   247 
       
   248 				rvi[i].power = power;
       
   249 				dewagonize(power, engine + i);
       
   250 			}
       
   251 		}	break;
       
   252 		case 0x0D: {	/* Running cost factor */
       
   253 			FOR_EACH_OBJECT {
       
   254 				uint8 runcostfact = grf_load_byte(&buf);
       
   255 
       
   256 				rvi[i].running_cost_base = runcostfact;
       
   257 				dewagonize(runcostfact, engine + i);
       
   258 			}
       
   259 		}	break;
       
   260 		case 0x0E: {	/* Running cost base */
       
   261 			FOR_EACH_OBJECT {
       
   262 				uint32 base = grf_load_dword(&buf);
       
   263 
       
   264 				switch (base) {
       
   265 				case 0x4C30:
       
   266 					rvi[i].engclass = 0;
       
   267 					break;
       
   268 				case 0x4C36:
       
   269 					rvi[i].engclass = 1;
       
   270 					break;
       
   271 				case 0x4C3C:
       
   272 					rvi[i].engclass = 2;
       
   273 					break;
       
   274 				}
       
   275 				dewagonize(base, engine + i);
       
   276 			}
       
   277 		}	break;
       
   278 		case 0x12: {	/* Sprite ID */
       
   279 			FOR_EACH_OBJECT {
       
   280 				uint8 spriteid = grf_load_byte(&buf);
       
   281 
       
   282 				if (spriteid == 0xFD && rvi[i].image_index != 0xFD)
       
   283 					_engine_original_sprites[engine + i] = rvi[i].image_index;
       
   284 				rvi[i].image_index = spriteid;
       
   285 			}
       
   286 		}	break;
       
   287 		case 0x13: {	/* Dual-headed */
       
   288 			FOR_EACH_OBJECT {
       
   289 				uint8 dual = grf_load_byte(&buf);
       
   290 
       
   291 				if (dual != 0) {
       
   292 					rvi[i].flags |= 1;
       
   293 				} else {
       
   294 					rvi[i].flags &= ~1;
       
   295 				}
       
   296 			}
       
   297 		}	break;
       
   298 		case 0x14: {	/* Cargo capacity */
       
   299 			FOR_EACH_OBJECT {
       
   300 				uint8 capacity = grf_load_byte(&buf);
       
   301 
       
   302 				rvi[i].capacity = capacity;
       
   303 			}
       
   304 		}	break;
       
   305 		case 0x15: {	/* Cargo type */
       
   306 			FOR_EACH_OBJECT {
       
   307 				uint8 ctype = grf_load_byte(&buf);
       
   308 
       
   309 				rvi[i].cargo_type = ctype;
       
   310 			}
       
   311 		}	break;
       
   312 		case 0x16: {	/* Weight */
       
   313 			FOR_EACH_OBJECT {
       
   314 				uint8 weight = grf_load_byte(&buf);
       
   315 
       
   316 				rvi[i].weight = weight;
       
   317 			}
       
   318 		}	break;
       
   319 		case 0x17: {	/* Cost factor */
       
   320 			FOR_EACH_OBJECT {
       
   321 				uint8 cfactor = grf_load_byte(&buf);
       
   322 
       
   323 				rvi[i].base_cost = cfactor;
       
   324 			}
       
   325 		}	break;
       
   326 		case 0x18: {	/* AI rank */
       
   327 			/* TODO: _railveh_score should be merged to _rail_vehicle_info. */
       
   328 			FOR_EACH_OBJECT {
       
   329 				grf_load_byte(&buf);
       
   330 			}
       
   331 			ret = true;
       
   332 		}	break;
       
   333 		case 0x19: { /* Engine traction type */
       
   334 			/* TODO: What do the individual numbers mean?
       
   335 			 * XXX: And in what base are they, in fact? --pasky */
       
   336 			FOR_EACH_OBJECT {
       
   337 				uint8 traction = grf_load_byte(&buf);
       
   338 				int engclass;
       
   339 
       
   340 				if (traction <= 0x07)
       
   341 					engclass = 0;
       
   342 				else if (traction <= 0x27)
       
   343 					engclass = 1;
       
   344 				else if (traction <= 0x31)
       
   345 					engclass = 2;
       
   346 				else
       
   347 					break;
       
   348 
       
   349 				rvi[i].engclass = engclass;
       
   350 			}
       
   351 		}	break;
       
   352 		case 0x1D: {	/* Refit cargo */
       
   353 			FOR_EACH_OBJECT {
       
   354 				uint32 refit_mask = grf_load_dword(&buf);
       
   355 
       
   356 				_engine_refit_masks[engine + i] = refit_mask;
       
   357 			}
       
   358 		}	break;
       
   359 
       
   360 		/* TODO */
       
   361 		/* Fall-through for unimplemented two bytes long properties. */
       
   362 		case 0x1B:	/* Powered wagons power bonus */
       
   363 			FOR_EACH_OBJECT {
       
   364 				grf_load_byte(&buf);
       
   365 			}
       
   366 		/* Fall-through for unimplemented one byte long properties. */
       
   367 		case 0x1A:	/* Sort order */
       
   368 		case 0x1C:	/* Refit cost */
       
   369 		case 0x1E:	/* Callback */
       
   370 		case 0x1F:	/* Tractive effort */
       
   371 		case 0x21:	/* Shorter tenders */
       
   372 		case 0x22:	/* Visual */
       
   373 		case 0x23: {/* Powered wagons weight bonus */
       
   374 			/* TODO */
       
   375 			FOR_EACH_OBJECT {
       
   376 				grf_load_byte(&buf);
       
   377 			}
       
   378 			ret = true;
       
   379 		}	break;
       
   380 		default:
       
   381 			ret = true;
       
   382 	}
       
   383 	*bufp = buf;
       
   384 	return ret;
       
   385 }
       
   386 
       
   387 static bool RoadVehicleChangeInfo(uint engine, int numinfo, int prop, byte **bufp, int len)
       
   388 {
       
   389 	RoadVehicleInfo *rvi = &_road_vehicle_info[engine];
       
   390 	byte *buf = *bufp;
       
   391 	int i;
       
   392 	bool ret = false;
       
   393 
       
   394 	switch (prop) {
       
   395 		case 0x08: {	/* Speed */
       
   396 			FOR_EACH_OBJECT {
       
   397 				uint8 speed = grf_load_byte(&buf);
       
   398 
       
   399 				rvi[i].max_speed = speed; // ?? units
       
   400 			}
       
   401 		}	break;
       
   402 		case 0x09: {	/* Running cost factor */
       
   403 			FOR_EACH_OBJECT {
       
   404 				uint8 runcost = grf_load_byte(&buf);
       
   405 
       
   406 				rvi[i].running_cost = runcost;
       
   407 			}
       
   408 		}	break;
       
   409 		case 0x0A:	/* Running cost base */
       
   410 			/* TODO: I have no idea. --pasky */
       
   411 			FOR_EACH_OBJECT {
       
   412 				grf_load_byte(&buf);
       
   413 			}
       
   414 			ret = true;
       
   415 			break;
       
   416 		case 0x0E: {	/* Sprite ID */
       
   417 			FOR_EACH_OBJECT {
       
   418 				uint8 spriteid = grf_load_byte(&buf);
       
   419 
       
   420 				if (spriteid == 0xFF)
       
   421 					spriteid = 0xFD; // cars have different custom id in the GRF file
       
   422 
       
   423 				// This is currently not used but there's no reason
       
   424 				// in not having it here for the future.
       
   425 				if (spriteid == 0xFD && rvi[i].image_index != 0xFD)
       
   426 					_engine_original_sprites[ROAD_ENGINES_INDEX + engine + i] = rvi[i].image_index;
       
   427 
       
   428 				rvi[i].image_index = spriteid;
       
   429 			}
       
   430 		}	break;
       
   431 		case 0x0F: {	/* Cargo capacity */
       
   432 			FOR_EACH_OBJECT {
       
   433 				uint16 capacity = grf_load_word(&buf);
       
   434 
       
   435 				rvi[i].capacity = capacity;
       
   436 			}
       
   437 		}	break;
       
   438 		case 0x10: { /* Cargo type */
       
   439 			FOR_EACH_OBJECT {
       
   440 				uint8 cargo = grf_load_byte(&buf);
       
   441 
       
   442 				rvi[i].cargo_type = cargo;
       
   443 			}
       
   444 		}	break;
       
   445 		case 0x11: {	/* Cost factor */
       
   446 			FOR_EACH_OBJECT {
       
   447 				uint8 cost_factor = grf_load_byte(&buf);
       
   448 
       
   449 				rvi[i].base_cost = cost_factor; // ?? is it base_cost?
       
   450 			}
       
   451 		}	break;
       
   452 		case 0x12: {	/* SFX */
       
   453 			FOR_EACH_OBJECT {
       
   454 				uint8 sfx = grf_load_byte(&buf);
       
   455 
       
   456 				rvi[i].sfx = sfx;
       
   457 			}
       
   458 		}	break;
       
   459 		case 0x13:      /* Power in 10hp */
       
   460 		case 0x14:      /* Weight in 1/4 tons */
       
   461 		case 0x15:      /* Speed in mph*0.8 */
       
   462 			/* TODO: Support for road vehicles realistic power
       
   463 			 * computations (called rvpower in TTDPatch) is just
       
   464 			 * missing in OTTD yet. --pasky */
       
   465 			FOR_EACH_OBJECT {
       
   466 				grf_load_byte(&buf);
       
   467 			}
       
   468 			ret = true;
       
   469 			break;
       
   470 		case 0x16: {	/* Cargos available for refitting */
       
   471 			FOR_EACH_OBJECT {
       
   472 				uint32 refit_mask = grf_load_dword(&buf);
       
   473 
       
   474 				_engine_refit_masks[ROAD_ENGINES_INDEX + engine + i] = refit_mask;
       
   475 			}
       
   476 		}	break;
       
   477 		case 0x17:      /* Callback */
       
   478 		case 0x18:      /* Tractive effort */
       
   479 			/* TODO */
       
   480 			FOR_EACH_OBJECT {
       
   481 				grf_load_byte(&buf);
       
   482 			}
       
   483 			ret = true;
       
   484 			break;
       
   485 		default:
       
   486 			ret = true;
       
   487 	}
       
   488 
       
   489 	*bufp = buf;
       
   490 	return ret;
       
   491 }
       
   492 
       
   493 static bool ShipVehicleChangeInfo(uint engine, int numinfo, int prop, byte **bufp, int len)
       
   494 {
       
   495 	ShipVehicleInfo *svi = &_ship_vehicle_info[engine];
       
   496 	byte *buf = *bufp;
       
   497 	int i;
       
   498 	bool ret = false;
       
   499 
       
   500 	//printf("e %x prop %x?\n", engine, prop);
       
   501 	switch (prop) {
       
   502 		case 0x08: {	/* Sprite ID */
       
   503 			FOR_EACH_OBJECT {
       
   504 				uint8 spriteid = grf_load_byte(&buf);
       
   505 
       
   506 				if (spriteid == 0xFF)
       
   507 					spriteid = 0xFD; // ships have different custom id in the GRF file
       
   508 
       
   509 				// This is currently not used but there's no reason
       
   510 				// in not having it here for the future.
       
   511 				if (spriteid == 0xFD && svi[i].image_index != 0xFD)
       
   512 					_engine_original_sprites[SHIP_ENGINES_INDEX + engine + i] = svi[i].image_index;
       
   513 
       
   514 				svi[i].image_index = spriteid;
       
   515 			}
       
   516 		}	break;
       
   517 		case 0x09: {	/* Refittable */
       
   518 			FOR_EACH_OBJECT {
       
   519 				uint8 refittable = grf_load_byte(&buf);
       
   520 
       
   521 				svi[i].refittable = refittable;
       
   522 			}
       
   523 		}	break;
       
   524 		case 0x0A: {	/* Cost factor */
       
   525 			FOR_EACH_OBJECT {
       
   526 				uint8 cost_factor = grf_load_byte(&buf);
       
   527 
       
   528 				svi[i].base_cost = cost_factor; // ?? is it base_cost?
       
   529 			}
       
   530 		}	break;
       
   531 		case 0x0B: {	/* Speed */
       
   532 			FOR_EACH_OBJECT {
       
   533 				uint8 speed = grf_load_byte(&buf);
       
   534 
       
   535 				svi[i].max_speed = speed; // ?? units
       
   536 			}
       
   537 		}	break;
       
   538 		case 0x0C: { /* Cargo type */
       
   539 			FOR_EACH_OBJECT {
       
   540 				uint8 cargo = grf_load_byte(&buf);
       
   541 
       
   542 				// XXX: Need to consult this with patchman yet.
       
   543 #if 0
       
   544 				// Documentation claims this is already the
       
   545 				// per-landscape cargo type id, but newships.grf
       
   546 				// assume otherwise.
       
   547 				cargo = local_cargo_id_ctype[cargo];
       
   548 #endif
       
   549 				svi[i].cargo_type = cargo;
       
   550 			}
       
   551 		}	break;
       
   552 		case 0x0D: {	/* Cargo capacity */
       
   553 			FOR_EACH_OBJECT {
       
   554 				uint16 capacity = grf_load_word(&buf);
       
   555 
       
   556 				svi[i].capacity = capacity;
       
   557 			}
       
   558 		}	break;
       
   559 		case 0x0F: {	/* Running cost factor */
       
   560 			FOR_EACH_OBJECT {
       
   561 				uint8 runcost = grf_load_byte(&buf);
       
   562 
       
   563 				svi[i].running_cost = runcost;
       
   564 			}
       
   565 		} break;
       
   566 		case 0x10: {	/* SFX */
       
   567 			FOR_EACH_OBJECT {
       
   568 				uint8 sfx = grf_load_byte(&buf);
       
   569 
       
   570 				svi[i].sfx = sfx;
       
   571 			}
       
   572 		}	break;
       
   573 		case 0x11: {	/* Cargos available for refitting */
       
   574 			FOR_EACH_OBJECT {
       
   575 				uint32 refit_mask = grf_load_dword(&buf);
       
   576 
       
   577 				_engine_refit_masks[SHIP_ENGINES_INDEX + engine + i] = refit_mask;
       
   578 			}
       
   579 		}	break;
       
   580 		case 0x12: { /* Callback TODO */
       
   581 			ret = true;
       
   582 		}	break;
       
   583 		default:
       
   584 			ret = true;
       
   585 	}
       
   586 
       
   587 	*bufp = buf;
       
   588 	return ret;
       
   589 }
       
   590 
       
   591 static bool AircraftVehicleChangeInfo(uint engine, int numinfo, int prop, byte **bufp, int len)
       
   592 {
       
   593 	AircraftVehicleInfo *avi = &_aircraft_vehicle_info[engine];
       
   594 	byte *buf = *bufp;
       
   595 	int i;
       
   596 	bool ret = false;
       
   597 
       
   598 	//printf("e %x prop %x?\n", engine, prop);
       
   599 	switch (prop) {
       
   600 		case 0x08: {	/* Sprite ID */
       
   601 			FOR_EACH_OBJECT {
       
   602 				uint8 spriteid = grf_load_byte(&buf);
       
   603 
       
   604 				if (spriteid == 0xFF)
       
   605 					spriteid = 0xFD; // ships have different custom id in the GRF file
       
   606 
       
   607 				// This is currently not used but there's no reason
       
   608 				// in not having it here for the future.
       
   609 				if (spriteid == 0xFD && avi[i].image_index != 0xFD)
       
   610 					_engine_original_sprites[AIRCRAFT_ENGINES_INDEX + engine + i] = avi[i].image_index;
       
   611 
       
   612 				avi[i].image_index = spriteid;
       
   613 			}
       
   614 		}	break;
       
   615 		case 0x09: {	/* Helicopter */
       
   616 			FOR_EACH_OBJECT {
       
   617 				uint8 heli = grf_load_byte(&buf);
       
   618 
       
   619 				avi[i].subtype = (heli == 0) ? 0 : 1;
       
   620 			}
       
   621 		}	break;
       
   622 		case 0x0A: {	/* Large */
       
   623 			FOR_EACH_OBJECT {
       
   624 				uint8 large = grf_load_byte(&buf);
       
   625 
       
   626 				avi[i].subtype = (large == 1) ? 3 : 1;
       
   627 			}
       
   628 		}	break;
       
   629 		case 0x0B: {	/* Cost factor */
       
   630 			FOR_EACH_OBJECT {
       
   631 				uint8 cost_factor = grf_load_byte(&buf);
       
   632 
       
   633 				avi[i].base_cost = cost_factor; // ?? is it base_cost?
       
   634 			}
       
   635 		}	break;
       
   636 		case 0x0C: {	/* Speed */
       
   637 			FOR_EACH_OBJECT {
       
   638 				uint8 speed = grf_load_byte(&buf);
       
   639 
       
   640 				avi[i].max_speed = speed; // ?? units
       
   641 			}
       
   642 		}	break;
       
   643 		case 0x0D: {	/* Acceleration */
       
   644 			FOR_EACH_OBJECT {
       
   645 				uint8 accel = grf_load_byte(&buf);
       
   646 
       
   647 				avi[i].acceleration = accel;
       
   648 			}
       
   649 		} break;
       
   650 		case 0x0E: {	/* Running cost factor */
       
   651 			FOR_EACH_OBJECT {
       
   652 				uint8 runcost = grf_load_byte(&buf);
       
   653 
       
   654 				avi[i].running_cost = runcost;
       
   655 			}
       
   656 		} break;
       
   657 		case 0x0F: {	/* Passenger capacity */
       
   658 			FOR_EACH_OBJECT {
       
   659 				uint16 capacity = grf_load_word(&buf);
       
   660 
       
   661 				avi[i].passanger_capacity = capacity;
       
   662 			}
       
   663 		}	break;
       
   664 		case 0x11: {	/* Mail capacity */
       
   665 			FOR_EACH_OBJECT {
       
   666 				uint8 capacity = grf_load_byte(&buf);
       
   667 
       
   668 				avi[i].mail_capacity = capacity;
       
   669 			}
       
   670 		}	break;
       
   671 		case 0x12: {	/* SFX */
       
   672 			FOR_EACH_OBJECT {
       
   673 				uint8 sfx = grf_load_byte(&buf);
       
   674 
       
   675 				avi[i].sfx = sfx;
       
   676 			}
       
   677 		}	break;
       
   678 		case 0x13: {	/* Cargos available for refitting */
       
   679 			FOR_EACH_OBJECT {
       
   680 				uint32 refit_mask = grf_load_dword(&buf);
       
   681 
       
   682 				_engine_refit_masks[AIRCRAFT_ENGINES_INDEX + engine + i] = refit_mask;
       
   683 			}
       
   684 		}	break;
       
   685 		case 0x14: { /* Callback TODO */
       
   686 			ret = true;
       
   687 		}	break;
       
   688 		default:
       
   689 			ret = true;
       
   690 	}
       
   691 
       
   692 	*bufp = buf;
       
   693 	return ret;
       
   694 }
       
   695 
       
   696 static bool StationChangeInfo(uint stid, int numinfo, int prop, byte **bufp, int len)
       
   697 {
       
   698 	byte *buf = *bufp;
       
   699 	int i;
       
   700 	int ret = 0;
       
   701 
       
   702 	/* This is one single huge TODO. It doesn't handle anything more than
       
   703 	 * just waypoints for now. */
       
   704 
       
   705 	//printf("sci %d %d [0x%02x]\n", stid, numinfo, prop);
       
   706 	switch (prop) {
       
   707 		case 0x08:
       
   708 		{	/* Class ID */
       
   709 			FOR_EACH_OBJECT {
       
   710 				struct StationSpec *stat = &_cur_grffile->stations[stid + i];
       
   711 				uint32 classid;
       
   712 
       
   713 				/* classid, for a change, is always little-endian */
       
   714 				classid = *(buf++) << 24;
       
   715 				classid |= *(buf++) << 16;
       
   716 				classid |= *(buf++) << 8;
       
   717 				classid |= *(buf++);
       
   718 
       
   719 				switch (classid) {
       
   720 					case 'DFLT':
       
   721 						stat->sclass = STAT_CLASS_DFLT;
       
   722 						break;
       
   723 					case 'WAYP':
       
   724 						stat->sclass = STAT_CLASS_WAYP;
       
   725 						break;
       
   726 					default:
       
   727 						/* TODO: No support for custom
       
   728 						 * classes for now, so stuff
       
   729 						 * everything to the single
       
   730 						 * default one. --pasky */
       
   731 						stat->sclass = STAT_CLASS_DFLT;
       
   732 						//stat->sclass = STAT_CLASS_CUSTOM;
       
   733 						break;
       
   734 				}
       
   735 			}
       
   736 			break;
       
   737 		}
       
   738 		case 0x09:
       
   739 		{	/* Define sprite layout */
       
   740 			FOR_EACH_OBJECT {
       
   741 				struct StationSpec *stat = &_cur_grffile->stations[stid + i];
       
   742 				int t;
       
   743 
       
   744 				stat->tiles = grf_load_byte(&buf);
       
   745 				for (t = 0; t < stat->tiles; t++) {
       
   746 					DrawTileSprites *dts = &stat->renderdata[t];
       
   747 					int seq_count = 0;
       
   748 
       
   749 					if (t >= 8) {
       
   750 						grfmsg(GMS_WARN, "StationChangeInfo: Sprite %d>=8, skipping.", t);
       
   751 						grf_load_dword(&buf); // at least something
       
   752 						continue;
       
   753 					}
       
   754 
       
   755 					dts->ground_sprite = grf_load_dword(&buf);
       
   756 					if (!dts->ground_sprite) {
       
   757 						static const DrawTileSeqStruct empty = {0x80};
       
   758 						dts->seq = &empty;
       
   759 						continue;
       
   760 					}
       
   761 
       
   762 					dts->seq = NULL;
       
   763 					while (buf < *bufp + len) {
       
   764 						DrawTileSeqStruct *dtss;
       
   765 
       
   766 						// no relative bounding box support
       
   767 						dts->seq = realloc((void*)dts->seq, ++seq_count * sizeof(DrawTileSeqStruct));
       
   768 						dtss = (DrawTileSeqStruct*) &dts->seq[seq_count - 1];
       
   769 
       
   770 						dtss->delta_x = grf_load_byte(&buf);
       
   771 						if ((byte) dtss->delta_x == 0x80) break;
       
   772 						dtss->delta_y = grf_load_byte(&buf);
       
   773 						dtss->delta_z = grf_load_byte(&buf);
       
   774 						dtss->width = grf_load_byte(&buf);
       
   775 						dtss->height = grf_load_byte(&buf);
       
   776 						dtss->unk = grf_load_byte(&buf);
       
   777 						dtss->image = grf_load_dword(&buf) - 0x42d;
       
   778 					}
       
   779 				}
       
   780 			}
       
   781 			break;
       
   782 		}
       
   783 		case 0x0a:
       
   784 		{	/* Copy sprite layout */
       
   785 			FOR_EACH_OBJECT {
       
   786 				struct StationSpec *stat = &_cur_grffile->stations[stid + i];
       
   787 				byte srcid = grf_load_byte(&buf);
       
   788 				struct StationSpec *srcstat = &_cur_grffile->stations[srcid];
       
   789 				int t;
       
   790 
       
   791 				stat->tiles = srcstat->tiles;
       
   792 				for (t = 0; t < stat->tiles; t++) {
       
   793 					DrawTileSprites *dts = &stat->renderdata[t];
       
   794 					DrawTileSprites *sdts = &srcstat->renderdata[t];
       
   795 					DrawTileSeqStruct const *sdtss = sdts->seq;
       
   796 					int seq_count = 0;
       
   797 
       
   798 					dts->ground_sprite = sdts->ground_sprite;
       
   799 					if (!dts->ground_sprite) {
       
   800 						static const DrawTileSeqStruct empty = {0x80};
       
   801 						dts->seq = &empty;
       
   802 						continue;
       
   803 					}
       
   804 
       
   805 					dts->seq = NULL;
       
   806 					while (1) {
       
   807 						DrawTileSeqStruct *dtss;
       
   808 
       
   809 						// no relative bounding box support
       
   810 						dts->seq = realloc((void*)dts->seq, ++seq_count * sizeof(DrawTileSeqStruct));
       
   811 						dtss = (DrawTileSeqStruct*) &dts->seq[seq_count - 1];
       
   812 						*dtss = *sdtss;
       
   813 						if ((byte) dtss->delta_x == 0x80) break;
       
   814 						sdtss++;
       
   815 					}
       
   816 				}
       
   817 			}
       
   818 			break;
       
   819 		}
       
   820 		case 0x0b:
       
   821 		{	/* Callback */
       
   822 			/* TODO */
       
   823 			FOR_EACH_OBJECT {
       
   824 				grf_load_byte(&buf);
       
   825 			}
       
   826 			ret = 1;
       
   827 			break;
       
   828 		}
       
   829 		case 0x0C:
       
   830 		{	/* Platforms number */
       
   831 			FOR_EACH_OBJECT {
       
   832 				struct StationSpec *stat = &_cur_grffile->stations[stid + i];
       
   833 
       
   834 				stat->allowed_platforms = ~grf_load_byte(&buf);
       
   835 			}
       
   836 			break;
       
   837 		}
       
   838 		case 0x0D:
       
   839 		{	/* Platforms length */
       
   840 			FOR_EACH_OBJECT {
       
   841 				struct StationSpec *stat = &_cur_grffile->stations[stid + i];
       
   842 
       
   843 				stat->allowed_lengths = ~grf_load_byte(&buf);
       
   844 			}
       
   845 			break;
       
   846 		}
       
   847 		case 0x0e:
       
   848 		{	/* Define custom layout */
       
   849 			FOR_EACH_OBJECT {
       
   850 				struct StationSpec *stat = &_cur_grffile->stations[stid + i];
       
   851 
       
   852 				while (buf < *bufp + len) {
       
   853 					byte length = grf_load_byte(&buf);
       
   854 					byte number = grf_load_byte(&buf);
       
   855 					StationLayout layout;
       
   856 					int l, p;
       
   857 
       
   858 					if (length == 0 || number == 0) break;
       
   859 
       
   860 					//debug("l %d > %d ?", length, stat->lengths);
       
   861 					if (length > stat->lengths) {
       
   862 						stat->platforms = realloc(stat->platforms, length);
       
   863 						memset(stat->platforms + stat->lengths, 0, length - stat->lengths);
       
   864 
       
   865 						stat->layouts = realloc(stat->layouts, length * sizeof(*stat->layouts));
       
   866 						memset(stat->layouts + stat->lengths, 0,
       
   867 						       (length - stat->lengths) * sizeof(*stat->layouts));
       
   868 
       
   869 						stat->lengths = length;
       
   870 					}
       
   871 					l = length - 1; // index is zero-based
       
   872 
       
   873 					//debug("p %d > %d ?", number, stat->platforms[l]);
       
   874 					if (number > stat->platforms[l]) {
       
   875 						stat->layouts[l] = realloc(stat->layouts[l],
       
   876 						                               number * sizeof(**stat->layouts));
       
   877 						// We expect NULL being 0 here, but C99 guarantees that.
       
   878 						memset(stat->layouts[l] + stat->platforms[l], 0,
       
   879 						       (number - stat->platforms[l]) * sizeof(**stat->layouts));
       
   880 
       
   881 						stat->platforms[l] = number;
       
   882 					}
       
   883 
       
   884 					layout = malloc(length * number);
       
   885 					for (l = 0; l < length; l++)
       
   886 						for (p = 0; p < number; p++)
       
   887 							layout[l * number + p] = grf_load_byte(&buf);
       
   888 
       
   889 					l--;
       
   890 					p--;
       
   891 					free(stat->layouts[l][p]);
       
   892 					stat->layouts[l][p] = layout;
       
   893 				}
       
   894 			}
       
   895 			break;
       
   896 		}
       
   897 		case 0x0f:
       
   898 		{	/* Copy custom layout */
       
   899 			/* TODO */
       
   900 			FOR_EACH_OBJECT {
       
   901 				grf_load_byte(&buf);
       
   902 			}
       
   903 			ret = 1;
       
   904 			break;
       
   905 		}
       
   906 		case 0x10:
       
   907 		{	/* Little/lots cargo threshold */
       
   908 			/* TODO */
       
   909 			FOR_EACH_OBJECT {
       
   910 				grf_load_word(&buf);
       
   911 			}
       
   912 			ret = 1;
       
   913 			break;
       
   914 		}
       
   915 		case 0x11:
       
   916 		{	/* Pylon placement */
       
   917 			/* TODO; makes sense only for electrified tracks */
       
   918 			FOR_EACH_OBJECT {
       
   919 				grf_load_word(&buf);
       
   920 			}
       
   921 			ret = 1;
       
   922 			break;
       
   923 		}
       
   924 		case 0x12:
       
   925 		{	/* Cargo types for random triggers */
       
   926 			/* TODO */
       
   927 			FOR_EACH_OBJECT {
       
   928 				grf_load_dword(&buf);
       
   929 			}
       
   930 			ret = 1;
       
   931 			break;
       
   932 		}
       
   933 		default:
       
   934 			ret = 1;
       
   935 			break;
       
   936 	}
       
   937 
       
   938 	*bufp = buf;
       
   939 	return ret;
       
   940 }
       
   941 
       
   942 #undef shift_buf
       
   943 
       
   944 
       
   945 /* Action 0x00 */
       
   946 static void VehicleChangeInfo(byte *buf, int len)
       
   947 {
       
   948 	byte *bufend = buf + len;
       
   949 	int i;
       
   950 
       
   951 	/* <00> <feature> <num-props> <num-info> <id> (<property <new-info>)...
       
   952 	 *
       
   953 	 * B feature       0, 1, 2 or 3 for trains, road vehicles, ships or planes
       
   954 	 *                 4 for defining new train station sets
       
   955 	 * B num-props     how many properties to change per vehicle/station
       
   956 	 * B num-info      how many vehicles/stations to change
       
   957 	 * B id            ID of first vehicle/station to change, if num-info is
       
   958 	 *                 greater than one, this one and the following
       
   959 	 *                 vehicles/stations will be changed
       
   960 	 * B property      what property to change, depends on the feature
       
   961 	 * V new-info      new bytes of info (variable size; depends on properties) */
       
   962 	/* TODO: Bridges, town houses. */
       
   963 
       
   964 	static const VCI_Handler handler[7] = {
       
   965 		/* GSF_TRAIN */    RailVehicleChangeInfo,
       
   966 		/* GSF_ROAD */     RoadVehicleChangeInfo,
       
   967 		/* GSF_SHIP */     ShipVehicleChangeInfo,
       
   968 		/* GSF_AIRCRAFT */ AircraftVehicleChangeInfo,
       
   969 		/* GSF_STATION */  StationChangeInfo,
       
   970 		/* GSF_BRIDGE */   NULL,
       
   971 		/* GSF_TOWNHOUSE */NULL,
       
   972 	};
       
   973 
       
   974 	uint8 feature;
       
   975 	uint8 numprops;
       
   976 	uint8 numinfo;
       
   977 	byte engine;
       
   978 	EngineInfo *ei;
       
   979 
       
   980 	if (len == 1) {
       
   981 		DEBUG(grf, 8) ("Silently ignoring one-byte special sprite 0x00.");
       
   982 		return;
       
   983 	}
       
   984 
       
   985 	check_length(len, 6, "VehicleChangeInfo");
       
   986 	feature = buf[1];
       
   987 	numprops = buf[2];
       
   988 	numinfo = buf[3];
       
   989 	engine = buf[4];
       
   990 
       
   991 	DEBUG(grf, 6) ("VehicleChangeInfo: Feature %d, %d properties, to apply to %d+%d",
       
   992 	               feature, numprops, engine, numinfo);
       
   993 
       
   994 	if (feature > GSF_STATION) {
       
   995 		grfmsg(GMS_WARN, "VehicleChangeInfo: Unsupported feature %d, skipping.", feature);
       
   996 		return;
       
   997 	}
       
   998 
       
   999 	if (feature != GSF_STATION)
       
  1000 		ei = &_engine_info[engine + _vehshifts[feature]];
       
  1001 
       
  1002 	buf += 5;
       
  1003 
       
  1004 	while (numprops-- && buf < bufend) {
       
  1005 		uint8 prop = grf_load_byte(&buf);
       
  1006 
       
  1007 		if (feature == GSF_STATION)
       
  1008 			// stations don't share those common properties
       
  1009 			goto run_handler;
       
  1010 
       
  1011 		switch (prop) {
       
  1012 		case 0x00: { /* Introduction date */
       
  1013 			FOR_EACH_OBJECT {
       
  1014 				uint16 date = grf_load_word(&buf);
       
  1015 
       
  1016 				ei[i].base_intro = date;
       
  1017 			}
       
  1018 		}	break;
       
  1019 		case 0x02: { /* Decay speed */
       
  1020 			FOR_EACH_OBJECT {
       
  1021 				uint8 decay = grf_load_byte(&buf);
       
  1022 
       
  1023 				ei[i].unk2 &= 0x80;
       
  1024 				ei[i].unk2 |= decay & 0x7f;
       
  1025 			}
       
  1026 		}	break;
       
  1027 		case 0x03: { /* Vehicle life */
       
  1028 			FOR_EACH_OBJECT {
       
  1029 				uint8 life = grf_load_byte(&buf);
       
  1030 
       
  1031 				ei[i].lifelength = life;
       
  1032 			}
       
  1033 		}	break;
       
  1034 		case 0x04: { /* Model life */
       
  1035 			FOR_EACH_OBJECT {
       
  1036 				uint8 life = grf_load_byte(&buf);
       
  1037 
       
  1038 				ei[i].base_life = life;
       
  1039 			}
       
  1040 		}	break;
       
  1041 		case 0x06: { /* Climates available */
       
  1042 			FOR_EACH_OBJECT {
       
  1043 				uint8 climates = grf_load_byte(&buf);
       
  1044 
       
  1045 				ei[i].railtype_climates &= 0xf0;
       
  1046 				ei[i].railtype_climates |= climates;
       
  1047 			}
       
  1048 		}	break;
       
  1049 		case 0x07: { /* Loading speed */
       
  1050 			/* TODO */
       
  1051 			/* Hyronymus explained me what does
       
  1052 			 * this mean and insists on having a
       
  1053 			 * credit ;-). --pasky */
       
  1054 			/* TODO: This needs to be supported by
       
  1055 			 * LoadUnloadVehicle() first. */
       
  1056 			FOR_EACH_OBJECT {
       
  1057 				grf_load_byte(&buf);
       
  1058 			}
       
  1059 			goto ignoring;
       
  1060 		}
       
  1061 		default: {
       
  1062 run_handler:
       
  1063 			if (handler[feature](engine, numinfo, prop, &buf, bufend - buf)) {
       
  1064 ignoring:
       
  1065 				grfmsg(GMS_NOTICE, "VehicleChangeInfo: Ignoring property %x (not implemented).", prop);
       
  1066 			}
       
  1067 			break;
       
  1068 		}
       
  1069 		}
       
  1070 	}
       
  1071 #undef shift_buf
       
  1072 }
       
  1073 
       
  1074 #undef FOR_EACH_OBJECT
       
  1075 
       
  1076 
       
  1077 /* Action 0x01 */
       
  1078 static void NewSpriteSet(byte *buf, int len)
       
  1079 {
       
  1080 	/* <01> <feature> <num-sets> <num-ent>
       
  1081 	 *
       
  1082 	 * B feature       feature to define sprites for
       
  1083 	 *                 0, 1, 2, 3: veh-type, 4: train stations
       
  1084 	 * B num-sets      number of sprite sets
       
  1085 	 * B num-ent       how many entries per sprite set
       
  1086 	 *                 For vehicles, this is the number of different
       
  1087 	 *                         vehicle directions in each sprite set
       
  1088 	 *                         Set num-dirs=8, unless your sprites are symmetric.
       
  1089 	 *                         In that case, use num-dirs=4.
       
  1090 	 *                 For stations, must be 12 (hex) for the eighteen
       
  1091 	 *                         different sprites that make up a station */
       
  1092 	/* TODO: No stations support. */
       
  1093 	uint8 feature;
       
  1094 
       
  1095 	check_length(len, 4, "NewSpriteSet");
       
  1096 	feature = buf[1];
       
  1097 
       
  1098 	_cur_grffile->spriteset_start = _cur_spriteid + 1;
       
  1099 	_cur_grffile->spriteset_feature = feature;
       
  1100 	_cur_grffile->spriteset_numsets = buf[2];
       
  1101 	_cur_grffile->spriteset_numents = buf[3];
       
  1102 }
       
  1103 
       
  1104 /* Action 0x02 */
       
  1105 static void NewSpriteGroup(byte *buf, int len)
       
  1106 {
       
  1107 	byte *bufend = buf + len;
       
  1108 
       
  1109 	/* <02> <feature> <set-id> <type/num-entries> <feature-specific-data...>
       
  1110 	 *
       
  1111 	 * B feature       see action 1
       
  1112 	 * B set-id        ID of this particular definition
       
  1113 	 * B type/num-entries
       
  1114 	 *                 if 80 or greater, this is a randomized or variational
       
  1115 	 *                 list definition, see below
       
  1116 	 *                 otherwise it specifies a number of entries, the exact
       
  1117 	 *                 meaning depends on the feature
       
  1118 	 * V feature-specific-data (huge mess, don't even look it up --pasky) */
       
  1119 	/* TODO: No 0x80-types (ugh). */
       
  1120 	/* TODO: Also, empty sprites aren't handled for now. Need to investigate
       
  1121 	 * the "opacity" rules for these, that is which sprite to fall back to
       
  1122 	 * when. --pasky */
       
  1123 	uint8 feature;
       
  1124 	uint8 setid;
       
  1125 	/* XXX: For stations, these two are "little cargo" and "lotsa cargo" sets. */
       
  1126 	uint8 numloaded;
       
  1127 	uint8 numloading;
       
  1128 	struct SpriteGroup *group;
       
  1129 	struct RealSpriteGroup *rg;
       
  1130 	byte *loaded_ptr;
       
  1131 	byte *loading_ptr;
       
  1132 	int i;
       
  1133 
       
  1134 	check_length(len, 5, "NewSpriteGroup");
       
  1135 	feature = buf[1];
       
  1136 	setid = buf[2];
       
  1137 	numloaded = buf[3];
       
  1138 	numloading = buf[4];
       
  1139 
       
  1140 	if (numloaded == 0x81 || numloaded == 0x82) {
       
  1141 		struct DeterministicSpriteGroup *dg;
       
  1142 		uint16 groupid;
       
  1143 		int i;
       
  1144 
       
  1145 		// Ok, this is gonna get a little wild, so hold your breath...
       
  1146 
       
  1147 		/* This stuff is getting actually evaluated in
       
  1148 		 * EvalDeterministicSpriteGroup(). */
       
  1149 
       
  1150 		buf += 4; len -= 4;
       
  1151 		check_length(len, 6, "NewSpriteGroup 0x81/0x82");
       
  1152 
       
  1153 		if (setid >= _cur_grffile->spritegroups_count) {
       
  1154 			_cur_grffile->spritegroups_count = setid + 1;
       
  1155 			_cur_grffile->spritegroups = realloc(_cur_grffile->spritegroups, _cur_grffile->spritegroups_count * sizeof(struct SpriteGroup));
       
  1156 		}
       
  1157 
       
  1158 		group = &_cur_grffile->spritegroups[setid];
       
  1159 		memset(group, 0, sizeof(struct SpriteGroup));
       
  1160 		group->type = SGT_DETERMINISTIC;
       
  1161 		dg = &group->g.determ;
       
  1162 
       
  1163 		/* XXX: We don't free() anything, assuming that if there was
       
  1164 		 * some action here before, it got associated by action 3.
       
  1165 		 * We should perhaps keep some refcount? --pasky */
       
  1166 
       
  1167 		dg->var_scope = numloaded == 0x82 ? VSG_SCOPE_PARENT : VSG_SCOPE_SELF;
       
  1168 		dg->variable = grf_load_byte(&buf);
       
  1169 
       
  1170 		dg->shift_num = grf_load_byte(&buf);
       
  1171 		dg->and_mask = grf_load_byte(&buf);
       
  1172 		dg->operation = dg->shift_num >> 6; /* w00t */
       
  1173 		dg->shift_num &= 0x3F;
       
  1174 		if (dg->operation != DSG_OP_NONE) {
       
  1175 			dg->add_val = grf_load_byte(&buf);
       
  1176 			dg->divmod_val = grf_load_byte(&buf);
       
  1177 		}
       
  1178 
       
  1179 		/* (groupid & 0x8000) means this is callback result; we happily
       
  1180 		 * ignore that for now. */
       
  1181 
       
  1182 		dg->num_ranges = grf_load_byte(&buf);
       
  1183 		dg->ranges = calloc(dg->num_ranges, sizeof(*dg->ranges));
       
  1184 		for (i = 0; i < dg->num_ranges; i++) {
       
  1185 			groupid = grf_load_word(&buf);
       
  1186 			if (groupid & 0x8000 || groupid >= _cur_grffile->spritegroups_count) {
       
  1187 				/* This doesn't exist for us. */
       
  1188 				grf_load_word(&buf); // skip range
       
  1189 				i--; dg->num_ranges--;
       
  1190 				continue;
       
  1191 			}
       
  1192 			/* XXX: If multiple surreal sets attach a surreal
       
  1193 			 * set this way, we are in trouble. */
       
  1194 			dg->ranges[i].group = _cur_grffile->spritegroups[groupid];
       
  1195 			dg->ranges[i].low = grf_load_byte(&buf);
       
  1196 			dg->ranges[i].high = grf_load_byte(&buf);
       
  1197 		}
       
  1198 
       
  1199 		groupid = grf_load_word(&buf);
       
  1200 		if (groupid & 0x8000 || groupid >= _cur_grffile->spritegroups_count) {
       
  1201 			/* This spritegroup stinks. */
       
  1202 			free(dg->ranges), dg->ranges = NULL;
       
  1203 			grfmsg(GMS_WARN, "NewSpriteGroup(%02x:0x%x): Default groupid %04x is cargo callback or unknown, ignoring spritegroup.", setid, numloaded, groupid);
       
  1204 			return;
       
  1205 		}
       
  1206 
       
  1207 		dg->default_group = malloc(sizeof(*dg->default_group));
       
  1208 		memcpy(dg->default_group, &_cur_grffile->spritegroups[groupid], sizeof(*dg->default_group));
       
  1209 
       
  1210 		return;
       
  1211 
       
  1212 	} else if (numloaded == 0x80 || numloaded == 0x83) {
       
  1213 		struct RandomizedSpriteGroup *rg;
       
  1214 		int i;
       
  1215 
       
  1216 		/* This stuff is getting actually evaluated in
       
  1217 		 * EvalRandomizedSpriteGroup(). */
       
  1218 
       
  1219 		buf += 4;
       
  1220 		len -= 4;
       
  1221 		check_length(len, 6, "NewSpriteGroup 0x80/0x83");
       
  1222 
       
  1223 		if (setid >= _cur_grffile->spritegroups_count) {
       
  1224 			_cur_grffile->spritegroups_count = setid + 1;
       
  1225 			_cur_grffile->spritegroups = realloc(_cur_grffile->spritegroups, _cur_grffile->spritegroups_count * sizeof(struct SpriteGroup));
       
  1226 		}
       
  1227 
       
  1228 		group = &_cur_grffile->spritegroups[setid];
       
  1229 		memset(group, 0, sizeof(*group));
       
  1230 		group->type = SGT_RANDOMIZED;
       
  1231 		rg = &group->g.random;
       
  1232 
       
  1233 		/* XXX: We don't free() anything, assuming that if there was
       
  1234 		 * some action here before, it got associated by action 3.
       
  1235 		 * We should perhaps keep some refcount? --pasky */
       
  1236 
       
  1237 		rg->var_scope = numloaded == 0x83 ? VSG_SCOPE_PARENT : VSG_SCOPE_SELF;
       
  1238 
       
  1239 		rg->triggers = grf_load_byte(&buf);
       
  1240 		rg->cmp_mode = rg->triggers & 0x80;
       
  1241 		rg->triggers &= 0x7F;
       
  1242 
       
  1243 		rg->lowest_randbit = grf_load_byte(&buf);
       
  1244 		rg->num_groups = grf_load_byte(&buf);
       
  1245 
       
  1246 		rg->groups = calloc(rg->num_groups, sizeof(*rg->groups));
       
  1247 		for (i = 0; i < rg->num_groups; i++) {
       
  1248 			uint16 groupid = grf_load_word(&buf);
       
  1249 
       
  1250 			if (groupid & 0x8000 || groupid >= _cur_grffile->spritegroups_count) {
       
  1251 				/* This doesn't exist for us. */
       
  1252 				i--;
       
  1253 				rg->num_groups--;
       
  1254 				continue;
       
  1255 			}
       
  1256 			/* XXX: If multiple surreal sets attach a surreal
       
  1257 			 * set this way, we are in trouble. */
       
  1258 			rg->groups[i] = _cur_grffile->spritegroups[groupid];
       
  1259 		}
       
  1260 
       
  1261 		return;
       
  1262 	}
       
  1263 
       
  1264 	if (!_cur_grffile->spriteset_start) {
       
  1265 		grfmsg(GMS_ERROR, "NewSpriteGroup: No sprite set to work on! Skipping.");
       
  1266 		return;
       
  1267 	}
       
  1268 
       
  1269 	if (_cur_grffile->spriteset_feature != feature) {
       
  1270 		grfmsg(GMS_ERROR, "NewSpriteGroup: Group feature %x doesn't match set feature %x! Playing it risky and trying to use it anyway.", feature, _cur_grffile->spriteset_feature);
       
  1271 		// return; // XXX: we can't because of MB's newstats.grf --pasky
       
  1272 	}
       
  1273 
       
  1274 	check_length(bufend - buf, 5, "NewSpriteGroup");
       
  1275 	buf += 5;
       
  1276 	check_length(bufend - buf, 2 * numloaded, "NewSpriteGroup");
       
  1277 	loaded_ptr = buf;
       
  1278 	loading_ptr = buf + 2 * numloaded;
       
  1279 
       
  1280 	if (numloaded > 16) {
       
  1281 		grfmsg(GMS_WARN, "NewSpriteGroup: More than 16 sprites in group %x, skipping the rest.", setid);
       
  1282 		numloaded = 16;
       
  1283 	}
       
  1284 	if (numloading > 16) {
       
  1285 		grfmsg(GMS_WARN, "NewSpriteGroup: More than 16 sprites in group %x, skipping the rest.", setid);
       
  1286 		numloading = 16;
       
  1287 	}
       
  1288 
       
  1289 	if (setid >= _cur_grffile->spritegroups_count) {
       
  1290 		_cur_grffile->spritegroups_count = setid + 1;
       
  1291 		_cur_grffile->spritegroups = realloc(_cur_grffile->spritegroups, _cur_grffile->spritegroups_count * sizeof(struct SpriteGroup));
       
  1292 	}
       
  1293 	group = &_cur_grffile->spritegroups[setid];
       
  1294 	memset(group, 0, sizeof(struct SpriteGroup));
       
  1295 	group->type = SGT_REAL;
       
  1296 	rg = &group->g.real;
       
  1297 
       
  1298 	rg->sprites_per_set = _cur_grffile->spriteset_numents;
       
  1299 	rg->loaded_count  = numloaded;
       
  1300 	rg->loading_count = numloading;
       
  1301 
       
  1302 	DEBUG(grf, 6) ("NewSpriteGroup: New SpriteGroup 0x%02hhx, %u views, %u loaded, %u loading, sprites %u - %u",
       
  1303 			setid, rg->sprites_per_set, rg->loaded_count, rg->loading_count,
       
  1304 			_cur_grffile->spriteset_start - _cur_grffile->sprite_offset,
       
  1305 			_cur_grffile->spriteset_start + (_cur_grffile->spriteset_numents * (numloaded + numloading)) - _cur_grffile->sprite_offset);
       
  1306 
       
  1307 	for (i = 0; i < numloaded; i++) {
       
  1308 		uint16 spriteset_id = grf_load_word(&loaded_ptr);
       
  1309 		rg->loaded[i] = _cur_grffile->spriteset_start + spriteset_id * _cur_grffile->spriteset_numents;
       
  1310 		DEBUG(grf, 8) ("NewSpriteGroup: + rg->loaded[%i]  = %u (subset %u)", i, rg->loaded[i], spriteset_id);
       
  1311 	}
       
  1312 
       
  1313 	for (i = 0; i < numloading; i++) {
       
  1314 		uint16 spriteset_id = grf_load_word(&loading_ptr);
       
  1315 		rg->loading[i] = _cur_grffile->spriteset_start + spriteset_id * _cur_grffile->spriteset_numents;
       
  1316 		DEBUG(grf, 8) ("NewSpriteGroup: + rg->loading[%i] = %u (subset %u)", i, rg->loading[i], spriteset_id);
       
  1317 	}
       
  1318 }
       
  1319 
       
  1320 /* Action 0x03 */
       
  1321 static void NewVehicle_SpriteGroupMapping(byte *buf, int len)
       
  1322 {
       
  1323 	/* <03> <feature> <n-id> <ids>... <num-cid> [<cargo-type> <cid>]... <def-cid>
       
  1324 	 * id-list	:= [<id>] [id-list]
       
  1325 	 * cargo-list	:= <cargo-type> <cid> [cargo-list]
       
  1326 	 *
       
  1327 	 * B feature       see action 0
       
  1328 	 * B n-id          bits 0-6: how many IDs this definition applies to
       
  1329 	 *                 bit 7: if set, this is a wagon override definition (see below)
       
  1330 	 * B ids           the IDs for which this definition applies
       
  1331 	 * B num-cid       number of cargo IDs (sprite group IDs) in this definition
       
  1332 	 *                 can be zero, in that case the def-cid is used always
       
  1333 	 * B cargo-type    type of this cargo type (e.g. mail=2, wood=7, see below)
       
  1334 	 * W cid           cargo ID (sprite group ID) for this type of cargo
       
  1335 	 * W def-cid       default cargo ID (sprite group ID) */
       
  1336 	/* TODO: Bridges, town houses. */
       
  1337 	/* TODO: Multiple cargo support could be useful even for trains/cars -
       
  1338 	 * cargo id 0xff is used for showing images in the build train list. */
       
  1339 
       
  1340 	static byte *last_engines;
       
  1341 	static int last_engines_count;
       
  1342 	uint8 feature;
       
  1343 	uint8 idcount;
       
  1344 	bool wagover;
       
  1345 	uint8 cidcount;
       
  1346 	int c, i;
       
  1347 
       
  1348 	check_length(len, 7, "VehicleMapSpriteGroup");
       
  1349 	feature = buf[1];
       
  1350 	idcount = buf[2] & 0x7F;
       
  1351 	wagover = (buf[2] & 0x80) == 0x80;
       
  1352 	check_length(len, 3 + idcount, "VehicleMapSpriteGroup");
       
  1353 	cidcount = buf[3 + idcount];
       
  1354 	check_length(len, 4 + idcount + cidcount * 3, "VehicleMapSpriteGroup");
       
  1355 
       
  1356 	DEBUG(grf, 6) ("VehicleMapSpriteGroup: Feature %d, %d ids, %d cids, wagon override %d.",
       
  1357 			feature, idcount, cidcount, wagover);
       
  1358 
       
  1359 	if (feature > GSF_STATION) {
       
  1360 		grfmsg(GMS_WARN, "VehicleMapSpriteGroup: Unsupported feature %d, skipping.", feature);
       
  1361 		return;
       
  1362 	}
       
  1363 
       
  1364 
       
  1365 	if (feature == GSF_STATION) {
       
  1366 		// We do things differently for stations.
       
  1367 
       
  1368 		for (i = 0; i < idcount; i++) {
       
  1369 			uint8 stid = buf[3 + i];
       
  1370 			struct StationSpec *stat = &_cur_grffile->stations[stid];
       
  1371 			byte *bp = &buf[4 + idcount];
       
  1372 
       
  1373 			for (c = 0; c < cidcount; c++) {
       
  1374 				uint8 ctype = grf_load_byte(&bp);
       
  1375 				uint16 groupid = grf_load_word(&bp);
       
  1376 
       
  1377 				if (groupid >= _cur_grffile->spritegroups_count) {
       
  1378 					grfmsg(GMS_WARN, "VehicleMapSpriteGroup: Spriteset %x out of range %x, skipping.",
       
  1379 					       groupid, _cur_grffile->spritegroups_count);
       
  1380 					return;
       
  1381 				}
       
  1382 
       
  1383 				if (ctype != 0xFF) {
       
  1384 					/* TODO: No support for any other cargo. */
       
  1385 					continue;
       
  1386 				}
       
  1387 
       
  1388 				stat->spritegroup[1] = _cur_grffile->spritegroups[groupid];
       
  1389 			}
       
  1390 		}
       
  1391 
       
  1392 		{
       
  1393 			byte *bp = buf + 4 + idcount + cidcount * 3;
       
  1394 			uint16 groupid = grf_load_word(&bp);
       
  1395 
       
  1396 			if (groupid >= _cur_grffile->spritegroups_count) {
       
  1397 				grfmsg(GMS_WARN, "VehicleMapSpriteGroup: Spriteset %x out of range %x, skipping.",
       
  1398 				       groupid, _cur_grffile->spritegroups_count);
       
  1399 				return;
       
  1400 			}
       
  1401 
       
  1402 			for (i = 0; i < idcount; i++) {
       
  1403 				uint8 stid = buf[3 + i];
       
  1404 				struct StationSpec *stat = &_cur_grffile->stations[stid];
       
  1405 
       
  1406 				stat->spritegroup[0] = _cur_grffile->spritegroups[groupid];
       
  1407 				stat->grfid = _cur_grffile->grfid;
       
  1408 				SetCustomStation(stid, stat);
       
  1409 				stat->sclass = STAT_CLASS_NONE;
       
  1410 			}
       
  1411 		}
       
  1412 		return;
       
  1413 	}
       
  1414 
       
  1415 
       
  1416 	/* If ``n-id'' (or ``idcount'') is zero, this is a ``feature
       
  1417 	 * callback''. I have no idea how this works, so we will ignore it for
       
  1418 	 * now.  --octo */
       
  1419 	if (idcount == 0) {
       
  1420 		grfmsg(GMS_NOTICE, "NewMapping: Feature callbacks not implemented yet.");
       
  1421 		return;
       
  1422 	}
       
  1423 
       
  1424 	// FIXME: Tropicset contains things like:
       
  1425 	// 03 00 01 19 01 00 00 00 00 - this is missing one 00 at the end,
       
  1426 	// what should we exactly do with that? --pasky
       
  1427 
       
  1428 	if (!_cur_grffile->spriteset_start || !_cur_grffile->spritegroups) {
       
  1429 		grfmsg(GMS_WARN, "VehicleMapSpriteGroup: No sprite set to work on! Skipping.");
       
  1430 		return;
       
  1431 	}
       
  1432 
       
  1433 	if (!wagover && last_engines_count != idcount) {
       
  1434 		last_engines = realloc(last_engines, idcount);
       
  1435 		last_engines_count = idcount;
       
  1436 	}
       
  1437 
       
  1438 	if (wagover) {
       
  1439 		if (last_engines_count == 0) {
       
  1440 			grfmsg(GMS_ERROR, "VehicleMapSpriteGroup: WagonOverride: No engine to do override with.");
       
  1441 			return;
       
  1442 		}
       
  1443 		DEBUG(grf, 6) ("VehicleMapSpriteGroup: WagonOverride: %u engines, %u wagons.",
       
  1444 				last_engines_count, idcount);
       
  1445 	}
       
  1446 
       
  1447 
       
  1448 	for (i = 0; i < idcount; i++) {
       
  1449 		uint8 engine_id = buf[3 + i];
       
  1450 		uint8 engine = engine_id + _vehshifts[feature];
       
  1451 		byte *bp = &buf[4 + idcount];
       
  1452 
       
  1453 		if (engine_id > _vehcounts[feature]) {
       
  1454 			grfmsg(GMS_ERROR, "Id %u for feature %x is out of bounds.",
       
  1455 					engine_id, feature);
       
  1456 			return;
       
  1457 		}
       
  1458 
       
  1459 		DEBUG(grf, 7) ("VehicleMapSpriteGroup: [%d] Engine %d...", i, engine);
       
  1460 
       
  1461 		for (c = 0; c < cidcount; c++) {
       
  1462 			uint8 ctype = grf_load_byte(&bp);
       
  1463 			uint16 groupid = grf_load_word(&bp);
       
  1464 
       
  1465 			DEBUG(grf, 8) ("VehicleMapSpriteGroup: * [%d] Cargo type %x, group id %x", c, ctype, groupid);
       
  1466 
       
  1467 			if (groupid >= _cur_grffile->spritegroups_count) {
       
  1468 				grfmsg(GMS_WARN, "VehicleMapSpriteGroup: Spriteset %x out of range %x, skipping.", groupid, _cur_grffile->spritegroups_count);
       
  1469 				return;
       
  1470 			}
       
  1471 
       
  1472 			if (ctype == 0xFF)
       
  1473 				ctype = CID_PURCHASE;
       
  1474 
       
  1475 			if (wagover) {
       
  1476 				// TODO: No multiple cargo types per vehicle yet. --pasky
       
  1477 				SetWagonOverrideSprites(engine, &_cur_grffile->spritegroups[groupid], last_engines, last_engines_count);
       
  1478 			} else {
       
  1479 				SetCustomEngineSprites(engine, ctype, &_cur_grffile->spritegroups[groupid]);
       
  1480 				last_engines[i] = engine;
       
  1481 			}
       
  1482 		}
       
  1483 	}
       
  1484 
       
  1485 	{
       
  1486 		byte *bp = buf + 4 + idcount + cidcount * 3;
       
  1487 		uint16 groupid = grf_load_word(&bp);
       
  1488 
       
  1489 		DEBUG(grf, 8) ("-- Default group id %x", groupid);
       
  1490 
       
  1491 		for (i = 0; i < idcount; i++) {
       
  1492 			uint8 engine = buf[3 + i] + _vehshifts[feature];
       
  1493 
       
  1494 			// Don't tell me you don't love duplicated code!
       
  1495 			if (groupid >= _cur_grffile->spritegroups_count) {
       
  1496 				grfmsg(GMS_WARN, "VehicleMapSpriteGroup: Spriteset %x out of range %x, skipping.", groupid, _cur_grffile->spritegroups_count);
       
  1497 				return;
       
  1498 			}
       
  1499 
       
  1500 			if (wagover) {
       
  1501 				// TODO: No multiple cargo types per vehicle yet. --pasky
       
  1502 				SetWagonOverrideSprites(engine, &_cur_grffile->spritegroups[groupid], last_engines, last_engines_count);
       
  1503 			} else {
       
  1504 				SetCustomEngineSprites(engine, CID_DEFAULT, &_cur_grffile->spritegroups[groupid]);
       
  1505 				last_engines[i] = engine;
       
  1506 			}
       
  1507 		}
       
  1508 	}
       
  1509 }
       
  1510 
       
  1511 /* Action 0x04 */
       
  1512 static void VehicleNewName(byte *buf, int len)
       
  1513 {
       
  1514 	/* <04> <veh-type> <language-id> <num-veh> <offset> <data...>
       
  1515 	 *
       
  1516 	 * B veh-type      see action 0
       
  1517 	 * B language-id   language ID with bit 7 cleared (see below)
       
  1518 	 * B num-veh       number of vehicles which are getting a new name
       
  1519 	 * B offset        number of the first vehicle that gets a new name
       
  1520 	 * S data          new texts, each of them zero-terminated, after
       
  1521 	 *                 which the next name begins. */
       
  1522 	/* TODO: No support for changing non-vehicle text. Perhaps we shouldn't
       
  1523 	 * implement it at all, but it could be useful for some "modpacks"
       
  1524 	 * (completely new scenarios changing all graphics and logically also
       
  1525 	 * factory names etc). We should then also support all languages (by
       
  1526 	 * name), not only the original four ones. --pasky */
       
  1527 	/* TODO: Support for custom station class/type names. */
       
  1528 
       
  1529 	uint8 feature;
       
  1530 	uint8 lang;
       
  1531 	uint8 id;
       
  1532 	uint8 endid;
       
  1533 
       
  1534 	check_length(len, 6, "VehicleNewName");
       
  1535 	feature = buf[1];
       
  1536 	lang = buf[2];
       
  1537 	id = buf[4] + _vehshifts[feature];
       
  1538 	endid = id + buf[3];
       
  1539 
       
  1540 	DEBUG(grf, 6) ("VehicleNewName: About to rename engines %d..%d (feature %d) in language 0x%x.",
       
  1541 	               id, endid, feature, lang);
       
  1542 
       
  1543 	if (lang & 0x80) {
       
  1544 		grfmsg(GMS_WARN, "VehicleNewName: No support for changing in-game texts. Skipping.");
       
  1545 		return;
       
  1546 	}
       
  1547 
       
  1548 	if (!(lang & 3)) {
       
  1549 		/* XXX: If non-English name, silently skip it. */
       
  1550 		DEBUG(grf, 7) ("VehicleNewName: Skipping non-English name.");
       
  1551 		return;
       
  1552 	}
       
  1553 
       
  1554 	buf += 5, len -= 5;
       
  1555 	for (; id < endid && len > 0; id++) {
       
  1556 		int ofs = strlen(buf) + 1;
       
  1557 
       
  1558 		if (ofs < 128) {
       
  1559 			DEBUG(grf, 8) ("VehicleNewName: %d <- %s", id, buf);
       
  1560 			SetCustomEngineName(id, buf);
       
  1561 		} else {
       
  1562 			DEBUG(grf, 7) ("VehicleNewName: Too long a name (%d)", ofs);
       
  1563 		}
       
  1564 		buf += ofs, len -= ofs;
       
  1565 	}
       
  1566 }
       
  1567 
       
  1568 /* Action 0x05 */
       
  1569 static void GraphicsNew(byte *buf, int len)
       
  1570 {
       
  1571 	/* <05> <graphics-type> <num-sprites> <other data...>
       
  1572 	 *
       
  1573 	 * B graphics-type What set of graphics the sprites define.
       
  1574 	 * B num-sprites   How many sprites are in this set?
       
  1575 	 * V other data    Graphics type specific data.  Currently unused. */
       
  1576 	/* TODO */
       
  1577 
       
  1578 	uint8 type;
       
  1579 	uint8 num;
       
  1580 
       
  1581 	check_length(len, 2, "GraphicsNew");
       
  1582 	type = buf[0];
       
  1583 	num = buf[1];
       
  1584 
       
  1585 	grfmsg(GMS_NOTICE, "GraphicsNew: Custom graphics (type %x) sprite block of length %d (unimplemented, ignoring).\n",
       
  1586 	       type, num);
       
  1587 }
       
  1588 
       
  1589 /* Action 0x06 */
       
  1590 static void CfgApply(byte *buf, int len)
       
  1591 {
       
  1592 	/* <06> <param-num> <param-size> <offset> ... <FF>
       
  1593 	 *
       
  1594 	 * B param-num     Number of parameter to substitute (First = "zero")
       
  1595 	 *                 Ignored if that parameter was not specified in newgrf.cfg
       
  1596 	 * B param-size    How many bytes to replace.  If larger than 4, the
       
  1597 	 *                 bytes of the following parameter are used.  In that
       
  1598 	 *                 case, nothing is applied unless *all* parameters
       
  1599 	 *                 were specified.
       
  1600 	 * B offset        Offset into data from beginning of next sprite
       
  1601 	 *                 to place where parameter is to be stored. */
       
  1602 	/* TODO */
       
  1603 	grfmsg(GMS_NOTICE, "CfgApply: Ignoring (not implemented).\n");
       
  1604 }
       
  1605 
       
  1606 /* Action 0x07 */
       
  1607 /* Action 0x09 */
       
  1608 static void SkipIf(byte *buf, int len)
       
  1609 {
       
  1610 	/* <07/09> <param-num> <param-size> <condition-type> <value> <num-sprites>
       
  1611 	 *
       
  1612 	 * B param-num
       
  1613 	 * B param-size
       
  1614 	 * B condition-type
       
  1615 	 * V value
       
  1616 	 * B num-sprites */
       
  1617 	/* TODO: More params. More condition types. */
       
  1618 	uint8 param;
       
  1619 	uint8 paramsize;
       
  1620 	uint8 condtype;
       
  1621 	uint8 numsprites;
       
  1622 	int param_val = 0, cond_val = 0;
       
  1623 	bool result;
       
  1624 
       
  1625 	check_length(len, 6, "SkipIf");
       
  1626 	param = buf[1];
       
  1627 	paramsize = buf[2];
       
  1628 	condtype = buf[3];
       
  1629 
       
  1630 	if (condtype < 2) {
       
  1631 		/* Always 1 for bit tests, the given value should
       
  1632 		 * be ignored. */
       
  1633 		paramsize = 1;
       
  1634 	}
       
  1635 
       
  1636 	buf += 4;
       
  1637 	if (paramsize == 4)
       
  1638 		cond_val = grf_load_dword(&buf);
       
  1639 	else if (paramsize == 2)
       
  1640 		cond_val = grf_load_word(&buf);
       
  1641 	else if (paramsize == 1)
       
  1642 		cond_val = grf_load_byte(&buf);
       
  1643 
       
  1644 	switch (param) {
       
  1645 		case 0x83:    /* current climate, 0=temp, 1=arctic, 2=trop, 3=toyland */
       
  1646 			param_val = _opt.landscape;
       
  1647 			break;
       
  1648 		case 0x84:    /* .grf loading stage, 0=initialization, 1=activation */
       
  1649 			param_val = _cur_stage;
       
  1650 			break;
       
  1651 		case 0x85:    /* TTDPatch flags, only for bit tests */
       
  1652 			param_val = _ttdpatch_flags[cond_val / 0x20];
       
  1653 			cond_val %= 0x20;
       
  1654 			break;
       
  1655 		case 0x86:    /* road traffic side, bit 4 clear=left, set=right */
       
  1656 			param_val = _opt.road_side << 4;
       
  1657 			break;
       
  1658 		case 0x88: {  /* see if specified GRFID is active */
       
  1659 			struct GRFFile *file;
       
  1660 
       
  1661 			file = GetFileByGRFID(cond_val);
       
  1662 			param_val = (file != NULL);
       
  1663 		}	break;
       
  1664 		case 0x8B:    /* TTDPatch version */
       
  1665 			param_val = 0xFFFF;
       
  1666 			break;
       
  1667 		case 0x8D:    /* TTD Version, 00=DOS, 01=Windows */
       
  1668 			param_val = 1;
       
  1669 			break;
       
  1670 		case 0x8E:
       
  1671 			param_val = _traininfo_vehicle_pitch;
       
  1672 			break;
       
  1673 		/* TODO */
       
  1674 		case 0x8F:    /* Track type cost multipliers */
       
  1675 		default:
       
  1676 			if (param >= 0x80) {
       
  1677 				/* In-game variable. */
       
  1678 				grfmsg(GMS_WARN, "Unsupported in-game variable %x. Ignoring test.", param);
       
  1679 			} else {
       
  1680 				/* Parameter. */
       
  1681 				param_val = _paramlist[param];
       
  1682 			}
       
  1683 			return;
       
  1684 	}
       
  1685 
       
  1686 	switch (condtype) {
       
  1687 		case 0: result = (param_val & (1 << cond_val));
       
  1688 			break;
       
  1689 		case 1: result = !(param_val & (1 << cond_val));
       
  1690 			break;
       
  1691 		/* TODO: For the following, make it to work with paramsize>1. */
       
  1692 		case 2: result = (param_val == cond_val);
       
  1693 			break;
       
  1694 		case 3: result = (param_val != cond_val);
       
  1695 			break;
       
  1696 		case 4: result = (param_val < cond_val);
       
  1697 			break;
       
  1698 		case 5: result = (param_val > cond_val);
       
  1699 			break;
       
  1700 		case 6: result = param_val; /* GRFID is active (only for param-num=88) */
       
  1701 			break;
       
  1702 		case 7: result = !param_val; /* GRFID is not active (only for param-num=88) */
       
  1703 			break;
       
  1704 		default:
       
  1705 			grfmsg(GMS_WARN, "Unsupported test %d. Ignoring.", condtype);
       
  1706 			return;
       
  1707 	}
       
  1708 
       
  1709 	if (result == 0) {
       
  1710 		grfmsg(GMS_NOTICE, "Not skipping sprites, test was false.");
       
  1711 		return;
       
  1712 	}
       
  1713 
       
  1714 	numsprites = grf_load_byte(&buf);
       
  1715 	grfmsg(GMS_NOTICE, "Skipping %d->+%d sprites, test was true.", _cur_spriteid - _custom_sprites_base, numsprites);
       
  1716 	_skip_sprites = numsprites;
       
  1717 	if (_skip_sprites == 0) {
       
  1718 		/* Zero means there are no sprites to skip, so
       
  1719 		 * we use -1 to indicate that all further
       
  1720 		 * sprites should be skipped. */
       
  1721 		_skip_sprites = -1;
       
  1722 	}
       
  1723 }
       
  1724 
       
  1725 static void GRFInfo(byte *buf, int len)
       
  1726 {
       
  1727 	/* <08> <version> <grf-id> <name> <info>
       
  1728 	 *
       
  1729 	 * B version       newgrf version, currently 06
       
  1730 	 * 4*B grf-id      globally unique ID of this .grf file
       
  1731 	 * S name          name of this .grf set
       
  1732 	 * S info          string describing the set, and e.g. author and copyright */
       
  1733 	/* TODO: Check version. (We should have own versioning done somehow.) */
       
  1734 	uint8 version;
       
  1735 	uint32 grfid;
       
  1736 	char *name;
       
  1737 	char *info;
       
  1738 
       
  1739 	check_length(len, 9, "GRFInfo");
       
  1740 	version = buf[1];
       
  1741 	/* this is de facto big endian - grf_load_dword() unsuitable */
       
  1742 	grfid = buf[2] << 24 | buf[3] << 16 | buf[4] << 8 | buf[5];
       
  1743 	name = buf + 6;
       
  1744 	info = name + strlen(name) + 1;
       
  1745 
       
  1746 	_cur_grffile->grfid = grfid;
       
  1747 	_cur_grffile->flags |= 0x0001; /* set active flag */
       
  1748 
       
  1749 	DEBUG(grf, 1) ("[%s] Loaded GRFv%d set %08lx - %s:\n%s\n",
       
  1750 	               _cur_grffile->filename, version, grfid, name, info);
       
  1751 }
       
  1752 
       
  1753 static void SpriteReplace(byte *buf, int len)
       
  1754 {
       
  1755 	/* <0A> <num-sets> <set1> [<set2> ...]
       
  1756 	 * <set>: <num-sprites> <first-sprite>
       
  1757 	 *
       
  1758 	 * B num-sets      How many sets of sprites to replace.
       
  1759 	 * Each set:
       
  1760 	 * B num-sprites   How many sprites are in this set
       
  1761 	 * W first-sprite  First sprite number to replace */
       
  1762 	uint8 num_sets;
       
  1763 	int i;
       
  1764 
       
  1765 	buf++; /* skip action byte */
       
  1766 	num_sets = grf_load_byte(&buf);
       
  1767 
       
  1768 	if (num_sets > 16) {
       
  1769 		grfmsg(GMS_ERROR, "SpriteReplace: Too many sets (%d), taking only the first 16!", num_sets);
       
  1770 	}
       
  1771 
       
  1772 	for (i = 0; i < 16; i++) {
       
  1773 		if (i < num_sets) {
       
  1774 			uint8 num_sprites = grf_load_byte(&buf);
       
  1775 			uint16 first_sprite = grf_load_word(&buf);
       
  1776 
       
  1777 			_replace_sprites_count[i] = num_sprites;
       
  1778 			_replace_sprites_offset[i] = first_sprite;
       
  1779 			grfmsg(GMS_NOTICE, "SpriteReplace: [Set %d] Changing %d sprites, beginning with %d",
       
  1780 					i, num_sprites, first_sprite);
       
  1781 		} else {
       
  1782 			_replace_sprites_count[i] = 0;
       
  1783 			_replace_sprites_offset[i] = 0;
       
  1784 		}
       
  1785 	}
       
  1786 }
       
  1787 
       
  1788 static void GRFError(byte *buf, int len)
       
  1789 {
       
  1790 	/* <0B> <severity> <language-id> <message-id> [<message...> 00] [<data...>] 00 [<parnum>]
       
  1791 	 *
       
  1792 	 * B severity      00: notice, contine loading grf file
       
  1793 	 *                 01: warning, continue loading grf file
       
  1794 	 *                 02: error, but continue loading grf file, and attempt
       
  1795 	 *                     loading grf again when loading or starting next game
       
  1796 	 *                 03: error, abort loading and prevent loading again in
       
  1797 	 *                     the future (only when restarting the patch)
       
  1798 	 * B language-id   see action 4, use 1F for built-in error messages
       
  1799 	 * B message-id    message to show, see below
       
  1800 	 * S message       for custom messages (message-id FF), text of the message
       
  1801 	 *                 not present for built-in messages.
       
  1802 	 * V data          additional data for built-in (or custom) messages
       
  1803 	 * B parnum        see action 6, only used with built-in message 03 */
       
  1804 	/* TODO: For now we just show the message, sometimes incomplete and never translated. */
       
  1805 
       
  1806 	static const char * const msgstr[4] = {
       
  1807 		"Requires at least pseudo-TTDPatch version %s.",
       
  1808 		"This file is for %s version of TTD.",
       
  1809 		"Designed to be used with %s.",
       
  1810 		"Invalid parameter %s.",
       
  1811 	};
       
  1812 	uint8 severity;
       
  1813 	uint8 msgid;
       
  1814 
       
  1815 	check_length(len, 6, "GRFError");
       
  1816 	severity = buf[1];
       
  1817 	msgid = buf[3];
       
  1818 
       
  1819 	// Undocumented TTDPatch feature.
       
  1820 	if ((severity & 0x80) == 0 && _cur_stage == 0)
       
  1821 		return;
       
  1822 	severity &= 0x7F;
       
  1823 
       
  1824 	if (msgid == 0xFF) {
       
  1825 		grfmsg(severity, "%s", buf+4);
       
  1826 	} else {
       
  1827 		grfmsg(severity, msgstr[msgid], buf+4);
       
  1828 	}
       
  1829 }
       
  1830 
       
  1831 static void GRFComment(byte *buf, int len)
       
  1832 {
       
  1833 	/* <0C> [<ignored...>]
       
  1834 	 *
       
  1835 	 * V ignored       Anything following the 0C is ignored */
       
  1836 }
       
  1837 
       
  1838 /* Action 0x0D */
       
  1839 static void ParamSet(byte *buf, int len)
       
  1840 {
       
  1841 	/* <0D> <target> <operation> <source1> <source2> [<data>]
       
  1842 	 *
       
  1843 	 * B target        parameter number where result is stored
       
  1844 	 * B operation     operation to perform, see below
       
  1845 	 * B source1       first source operand
       
  1846 	 * B source2       second source operand
       
  1847 	 * D data          data to use in the calculation, not necessary
       
  1848 	 *                 if both source1 and source2 refer to actual parameters
       
  1849 	 *
       
  1850 	 * Operations
       
  1851 	 * 00      Set parameter equal to source1
       
  1852 	 * 01      Addition, source1 + source2
       
  1853 	 * 02      Subtraction, source1 - source2
       
  1854 	 * 03      Unsigned multiplication, source1 * source2 (both unsigned)
       
  1855 	 * 04      Signed multiplication, source1 * source2 (both signed)
       
  1856 	 * 05      Unsigned bit shift, source1 by source2 (source2 taken to be a
       
  1857 	 *         signed quantity; left shift if positive and right shift if
       
  1858 	 *         negative, source1 is unsigned)
       
  1859 	 * 06      Signed bit shift, source1 by source2
       
  1860 	 *         (source2 like in 05, and source1 as well)
       
  1861 	 */
       
  1862 
       
  1863 	byte target;
       
  1864 	byte oper;
       
  1865 	uint16 src1;
       
  1866 	uint16 src2;
       
  1867 	uint16 data = 0;
       
  1868 	int32 *dest;
       
  1869 
       
  1870 	check_length(len, 5, "ParamSet");
       
  1871 	buf++;
       
  1872 	target = grf_load_byte(&buf);
       
  1873 	oper = grf_load_byte(&buf);
       
  1874 	src1 = grf_load_byte(&buf);
       
  1875 	src2 = grf_load_byte(&buf);
       
  1876 
       
  1877 	if (len >= 8)
       
  1878 		data = grf_load_dword(&buf);
       
  1879 
       
  1880 	/* You can add 80 to the operation to make it apply only if the target
       
  1881 	 * is not defined yet.  In this respect, a parameter is taken to be
       
  1882 	 * defined if any of the following applies:
       
  1883 	 * - it has been set to any value in the newgrf(w).cfg parameter list
       
  1884 	 * - it OR A PARAMETER WITH HIGHER NUMBER has been set to any value by
       
  1885 	 *   an earlier action D */
       
  1886 	if (oper & 0x80) {
       
  1887 		if (_param_max < target)
       
  1888 			oper &= 0x7F;
       
  1889 		else
       
  1890 			return;
       
  1891 	}
       
  1892 
       
  1893 	/* The source1 and source2 operands refer to the grf parameter number
       
  1894 	 * like in action 6 and 7.  In addition, they can refer to the special
       
  1895 	 * variables available in action 7, or they can be FF to use the value
       
  1896 	 * of <data>.  If referring to parameters that are undefined, a value
       
  1897 	 * of 0 is used instead.  */
       
  1898 	if (src1 == 0xFF) {
       
  1899 		src1 = data;
       
  1900 	} else {
       
  1901 		src1 = _param_max >= src1 ? _paramlist[src1] : 0;
       
  1902 	}
       
  1903 
       
  1904 	if (src2 == 0xFF) {
       
  1905 		src2 = data;
       
  1906 	} else {
       
  1907 		src2 = _param_max >= src2 ? _paramlist[src2] : 0;
       
  1908 	}
       
  1909 
       
  1910 	/* TODO: You can access the parameters of another GRF file by using
       
  1911 	 * source2=FE, source1=the other GRF's parameter number and data=GRF
       
  1912 	 * ID.  This is only valid with operation 00 (set).  If the GRF ID
       
  1913 	 * cannot be found, a value of 0 is used for the parameter value
       
  1914 	 * instead. */
       
  1915 
       
  1916 	/* TODO: The target operand can also refer to the special variables
       
  1917 	 * from action 7, but at the moment the only variable that is valid to
       
  1918 	 * write is 8E. */
       
  1919 
       
  1920 	if (target == 0x8E) {
       
  1921 		dest = &_traininfo_vehicle_pitch;
       
  1922 	} else {
       
  1923 		if (_param_max < target)
       
  1924 			_param_max = target;
       
  1925 		dest = &_paramlist[target];
       
  1926 	}
       
  1927 
       
  1928 	/* FIXME: No checking for overflows. */
       
  1929 	switch (oper) {
       
  1930 		case 0x00:
       
  1931 			*dest = src1;
       
  1932 			break;
       
  1933 		case 0x01:
       
  1934 			*dest = src1 + src2;
       
  1935 			break;
       
  1936 		case 0x02:
       
  1937 			*dest = src1 - src2;
       
  1938 			break;
       
  1939 		case 0x03:
       
  1940 			*dest = ((uint32) src1) * ((uint32) src2);
       
  1941 			break;
       
  1942 		case 0x04:
       
  1943 			*dest = ((int32) src1) * ((int32) src2);
       
  1944 			break;
       
  1945 		case 0x05:
       
  1946 			if (src2 & 0x8000) /* src2 is "negative" */
       
  1947 				*dest = src1 >> -((int16) src2);
       
  1948 			else
       
  1949 				*dest = src1 << src2;
       
  1950 			break;
       
  1951 		case 0x06:
       
  1952 			if (src2 & 0x8000) /* src2 is "negative" */
       
  1953 				*dest = ((int16) src1) >> -((int16) src2);
       
  1954 			else
       
  1955 				*dest = ((int16) src1) << src2;
       
  1956 			break;
       
  1957 		default:
       
  1958 			grfmsg(GMS_ERROR, "ParamSet: Unknown operation %d, skipping.", oper);
       
  1959 	}
       
  1960 }
       
  1961 
       
  1962 static void GRFInhibit(byte *buf, int len)
       
  1963 {
       
  1964 	/* <0E> <num> <grfids...>
       
  1965 	 *
       
  1966 	 * B num           Number of GRFIDs that follow
       
  1967 	 * D grfids        GRFIDs of the files to deactivate */
       
  1968 
       
  1969 	byte num;
       
  1970 	int i;
       
  1971 
       
  1972 	check_length(len, 1, "GRFInhibit");
       
  1973 	buf++, len--;
       
  1974 	num = grf_load_byte(&buf); len--;
       
  1975 	check_length(len, 4 * num, "GRFInhibit");
       
  1976 
       
  1977 	for (i = 0; i < num; i++) {
       
  1978 		uint32 grfid = grf_load_dword(&buf);
       
  1979 		struct GRFFile *file = GetFileByGRFID(grfid);
       
  1980 
       
  1981 		/* Unset activation flag */
       
  1982 		if (file != NULL) {
       
  1983 			grfmsg(GMS_NOTICE, "GRFInhibit: Deactivating file ``%s''", file->filename);
       
  1984 			file->flags &= 0xFFFE;
       
  1985 		}
       
  1986 	}
       
  1987 }
       
  1988 
       
  1989 
       
  1990 static void InitializeGRFSpecial(void)
       
  1991 {
       
  1992 	/* FIXME: We should rather reflect reality in _ttdpatch_flags[]. */
       
  1993 
       
  1994 	_ttdpatch_flags[1] = (1 << 0x08)  /* mammothtrains */
       
  1995 	                   | (1 << 0x0B)  /* subsidiaries */
       
  1996 	                   | (1 << 0x14)  /* bridgespeedlimits */
       
  1997 	                   | (1 << 0x16)  /* eternalgame */
       
  1998 	                   | (1 << 0x17)  /* newtrains */
       
  1999 	                   | (1 << 0x18)  /* newrvs */
       
  2000 	                   | (1 << 0x19)  /* newships */
       
  2001 	                   | (1 << 0x1A)  /* newplanes */
       
  2002 			   | (1 << 0x1B); /* signalsontrafficside */
       
  2003 	                   /* Uncomment following if you want to fool the GRF file.
       
  2004 	                    * Some GRF files will refuse to load without this
       
  2005 	                    * but you can still squeeze something from them even
       
  2006 	                    * without the support - i.e. USSet. --pasky */
       
  2007 			   //| (1 << 0x1C); /* electrifiedrailway */
       
  2008 
       
  2009 	_ttdpatch_flags[2] = (1 << 0x0D)  /* buildonslopes */
       
  2010 	                   | (1 << 0x16)  /* canals */
       
  2011 	                   | (1 << 0x17); /* newstartyear */
       
  2012 }
       
  2013 
       
  2014 void InitNewGRFFile(const char *filename, int sprite_offset)
       
  2015 {
       
  2016 	struct GRFFile *newfile;
       
  2017 
       
  2018 	newfile = calloc(1, sizeof(struct GRFFile));
       
  2019 
       
  2020 	if (newfile == NULL)
       
  2021 		error ("Out of memory");
       
  2022 
       
  2023 	newfile->filename = strdup(filename);
       
  2024 	newfile->sprite_offset = sprite_offset;
       
  2025 
       
  2026 	if (_first_grffile == NULL) {
       
  2027 		_cur_grffile = newfile;
       
  2028 		_first_grffile = newfile;
       
  2029 	} else {
       
  2030 		_cur_grffile->next = newfile;
       
  2031 		_cur_grffile = newfile;
       
  2032 	}
       
  2033 }
       
  2034 
       
  2035 
       
  2036 /* Here we perform initial decoding of some special sprites (as are they
       
  2037  * described at http://www.ttdpatch.net/src/newgrf.txt, but this is only a very
       
  2038  * partial implementation yet). */
       
  2039 /* XXX: We consider GRF files trusted. It would be trivial to exploit OTTD by
       
  2040  * a crafted invalid GRF file. We should tell that to the user somehow, or
       
  2041  * better make this more robust in the future. */
       
  2042 
       
  2043 void DecodeSpecialSprite(const char *filename, int num, int spriteid, int stage)
       
  2044 {
       
  2045 #define NUM_ACTIONS 0xF
       
  2046 	static const SpecialSpriteHandler handlers[NUM_ACTIONS] = {
       
  2047 		/* 0x0 */ VehicleChangeInfo,
       
  2048 		/* 0x1 */ NewSpriteSet,
       
  2049 		/* 0x2 */ NewSpriteGroup,
       
  2050 		/* 0x3 */ NewVehicle_SpriteGroupMapping,
       
  2051 		/* 0x4 */ VehicleNewName,
       
  2052 		/* 0x5 */ GraphicsNew,
       
  2053 		/* 0x6 */ CfgApply,
       
  2054 		/* 0x7 */ SkipIf,
       
  2055 		/* 0x8 */ GRFInfo,
       
  2056 		/* 0x9 */ SkipIf,
       
  2057 		/* 0xa */ SpriteReplace,
       
  2058 		/* 0xb */ GRFError,
       
  2059 		/* 0xc */ GRFComment,
       
  2060 		/* 0xd */ ParamSet,
       
  2061 		/* 0xe */ GRFInhibit,
       
  2062 	};
       
  2063 	static int initialized;
       
  2064 	byte action;
       
  2065 	byte *buf = malloc(num);
       
  2066 	int i;
       
  2067 
       
  2068 	if (buf == NULL) error("DecodeSpecialSprite: Could not allocate memory");
       
  2069 
       
  2070 	if (initialized == 0) {
       
  2071 		InitializeGRFSpecial();
       
  2072 		initialized = 1;
       
  2073 	}
       
  2074 
       
  2075 	_cur_stage = stage;
       
  2076 	_cur_spriteid = spriteid;
       
  2077 
       
  2078 	for (i = 0; i != num; i++)
       
  2079 		buf[i] = FioReadByte();
       
  2080 
       
  2081 	action = buf[0];
       
  2082 
       
  2083 	/* XXX: There is a difference between staged loading in TTDPatch and
       
  2084 	 * here.  In TTDPatch, for some reason actions 1 and 2 are carried out
       
  2085 	 * during stage 0, whilst action 3 is carried out during stage 1 (to
       
  2086 	 * "resolve" cargo IDs... wtf). This is a little problem, because cargo
       
  2087 	 * IDs are valid only within a given set (action 1) block, and may be
       
  2088 	 * overwritten after action 3 associates them. But overwriting happens
       
  2089 	 * in an earlier stage than associating, so...  We just process actions
       
  2090 	 * 1 and 2 in stage 1 now, let's hope that won't get us into problems.
       
  2091 	 * --pasky */
       
  2092 
       
  2093 	if (stage == 0) {
       
  2094 		/* During initialization, actions 0, 1, 2, 3, 4, 5 and 7 are ignored. */
       
  2095 
       
  2096 		if ((action == 0x00) || (action == 0x01) || (action == 0x02) || (action == 0x03)
       
  2097 		    || (action == 0x04) || (action == 0x05) || (action == 0x07)) {
       
  2098 			DEBUG (grf, 7) ("DecodeSpecialSprite: Action: %x, Stage 0, Skipped", action);
       
  2099 			/* Do nothing. */
       
  2100 
       
  2101 		} else if (action < NUM_ACTIONS) {
       
  2102 			DEBUG (grf, 7) ("DecodeSpecialSprite: Action: %x, Stage 0", action);
       
  2103  			handlers[action](buf, num);
       
  2104 
       
  2105 		} else {
       
  2106 			grfmsg(GMS_WARN, "Unknown special sprite action %x, skipping.", action);
       
  2107 		}
       
  2108 
       
  2109 	} else if (stage == 1) {
       
  2110 		/* A .grf file is activated only if it was active when the game was
       
  2111 		 * started.  If a game is loaded, only its active .grfs will be
       
  2112 		 * reactivated, unless "loadallgraphics on" is used.  A .grf file is
       
  2113 		 * considered active if its action 8 has been processed, i.e. its
       
  2114 		 * action 8 hasn't been skipped using an action 7.
       
  2115 		 *
       
  2116 		 * During activation, only actions 0, 1, 2, 3, 4, 5, 7, 8, 9, 0A and 0B are
       
  2117 		 * carried out.  All others are ignored, because they only need to be
       
  2118 		 * processed once at initialization.  */
       
  2119 
       
  2120 		if ((_cur_grffile == NULL) || strcmp(_cur_grffile->filename, filename))
       
  2121 			_cur_grffile = GetFileByFilename(filename);
       
  2122 
       
  2123 		if (_cur_grffile == NULL)
       
  2124 			error("File ``%s'' lost in cache.\n", filename);
       
  2125 
       
  2126 		if (!(_cur_grffile->flags & 0x0001)) {
       
  2127 			DEBUG (grf, 7) ("DecodeSpecialSprite: Action: %x, Stage 1, Not activated", action);
       
  2128 			/* Do nothing. */
       
  2129 
       
  2130 		} else if ((action == 0x00) || (action == 0x01) || (action == 0x02) || (action == 0x03)
       
  2131 			   || (action == 0x04) || (action == 0x05) || (action == 0x07) || (action == 0x08)
       
  2132 			   || (action == 0x09) || (action == 0x0A) || (action == 0x0B)) {
       
  2133 			DEBUG (grf, 7) ("DecodeSpecialSprite: Action: %x, Stage 1", action);
       
  2134 			handlers[action](buf, num);
       
  2135 
       
  2136 		} else if (action < NUM_ACTIONS) {
       
  2137 			DEBUG (grf, 7) ("DecodeSpecialSprite: Action: %x, Stage 1, Skipped", action);
       
  2138 			/* Do nothing. */
       
  2139 
       
  2140 		} else {
       
  2141 			grfmsg(GMS_WARN, "Unknown special sprite action %x, skipping.", action);
       
  2142 		}
       
  2143 
       
  2144 	} else {
       
  2145 		error("Invalid stage %d", stage);
       
  2146 	}
       
  2147 
       
  2148 	free(buf);
       
  2149 #undef NUM_ACTIONS
       
  2150 }