truelight@0: #include "stdafx.h" truelight@0: truelight@0: #include truelight@0: truelight@0: #include "ttd.h" truelight@0: #include "gfx.h" truelight@0: #include "fileio.h" truelight@0: #include "engine.h" truelight@0: truelight@0: /* TTDPatch extended GRF format codec truelight@0: * (c) Petr Baudis 2004 (GPL'd) truelight@0: * Contains portions of documentation by TTDPatch team. truelight@0: * Thanks especially to Josef Drexler for the documentation as well as a lot truelight@0: * of help at #tycoon. Also thanks to Michael Blunck for is GRF files which truelight@0: * served as subject to the initial testing of this codec. */ truelight@0: truelight@0: extern int _skip_sprites; truelight@0: truelight@0: static const char *_cur_grffile; truelight@0: static int _cur_spriteid; truelight@0: truelight@0: typedef void (*SpecialSpriteHandler)(byte *buf, int len); truelight@0: truelight@0: static const int _vehshifts[4] = { truelight@0: 0, truelight@0: ROAD_ENGINES_INDEX, truelight@0: SHIP_ENGINES_INDEX, truelight@0: AIRCRAFT_ENGINES_INDEX, truelight@0: }; truelight@0: truelight@0: truelight@0: enum grfmsg_severity { truelight@0: GMS_NOTICE, truelight@0: GMS_WARN, truelight@0: GMS_ERROR, truelight@0: GMS_FATAL, truelight@0: }; truelight@0: truelight@0: static void CDECL grfmsg(enum grfmsg_severity severity, const char *str, ...) truelight@0: { truelight@0: static const char * const severitystr[4] = { "Notice", "Warning", "Error", "Fatal" }; truelight@0: va_list va; truelight@0: truelight@0: va_start(va, str); truelight@0: fprintf(stderr, "[%s][%s] ", _cur_grffile, severitystr[severity]); truelight@0: vfprintf(stderr, str, va); truelight@0: va_end(va); truelight@0: fprintf(stderr, "\n"); truelight@0: } truelight@0: truelight@0: static byte INLINE grf_load_byte(byte **buf) { truelight@0: return *(*buf)++; truelight@0: } truelight@0: truelight@0: truelight@0: static uint16 grf_load_word(byte **buf) truelight@0: { truelight@0: uint16 val; truelight@0: byte *p = *buf; truelight@0: val = p[0]; truelight@0: val |= p[1] << 8; truelight@0: *buf = p + 2; truelight@0: return val; truelight@0: } truelight@0: truelight@0: static uint16 grf_load_dword(byte **buf) truelight@0: { truelight@0: uint32 val; truelight@0: byte *p = *buf; truelight@0: val = p[0]; truelight@0: val |= p[1] << 8; truelight@0: val |= p[2] << 16; truelight@0: val |= p[3] << 24; truelight@0: *buf = p + 4; truelight@0: return val; truelight@0: } truelight@0: truelight@0: typedef int (*VCI_Handler)(uint engine, int numinfo, int prop, byte **buf, int len); truelight@0: truelight@0: #define foreach_engine for (i = 0; i < numinfo; i++) truelight@0: truelight@0: static void dewagonize(int condition, int engine) truelight@0: { truelight@0: EngineInfo *ei = &_engine_info[engine]; truelight@0: RailVehicleInfo *rvi = &_rail_vehicle_info[engine]; truelight@0: truelight@0: if (condition) { truelight@0: ei->unk2 &= ~0x80; truelight@0: rvi->flags &= ~2; truelight@0: } else { truelight@0: ei->unk2 |= 0x80; truelight@0: rvi->flags |= 2; truelight@0: } truelight@0: } truelight@0: truelight@0: static int RailVehicleChangeInfo(uint engine, int numinfo, int prop, byte **bufp, int len) truelight@0: { truelight@0: EngineInfo *ei = &_engine_info[engine]; truelight@0: RailVehicleInfo *rvi = &_rail_vehicle_info[engine]; truelight@0: byte *buf = *bufp; truelight@0: int i; truelight@0: int ret = 0; truelight@0: truelight@0: switch (prop) { truelight@0: case 0x05: truelight@0: { /* Track type */ truelight@0: foreach_engine { truelight@0: uint8 tracktype = grf_load_byte(&buf); truelight@0: truelight@0: ei[i].railtype_climates &= 0xf; truelight@0: ei[i].railtype_climates |= tracktype << 4; truelight@0: } truelight@0: break; truelight@0: } truelight@0: case 0x08: truelight@0: { /* AI passenger service */ truelight@0: /* TODO */ truelight@0: foreach_engine { truelight@0: grf_load_byte(&buf); truelight@0: } truelight@0: ret = 1; truelight@0: break; truelight@0: } truelight@0: case 0x09: truelight@0: { /* Speed */ truelight@0: foreach_engine { truelight@0: uint16 speed = grf_load_word(&buf); truelight@0: truelight@0: rvi[i].max_speed = speed; truelight@0: dewagonize(speed, engine + i); truelight@0: } truelight@0: break; truelight@0: } truelight@0: case 0x0b: truelight@0: { /* Power */ truelight@0: foreach_engine { truelight@0: uint16 power = grf_load_word(&buf); truelight@0: truelight@0: rvi[i].power = power; truelight@0: dewagonize(power, engine + i); truelight@0: } truelight@0: break; truelight@0: } truelight@0: case 0x0d: truelight@0: { /* Running cost factor */ truelight@0: foreach_engine { truelight@0: uint8 runcostfact = grf_load_byte(&buf); truelight@0: truelight@0: rvi[i].running_cost_base = runcostfact; truelight@0: dewagonize(runcostfact, engine + i); truelight@0: } truelight@0: break; truelight@0: } truelight@0: case 0x0e: truelight@0: { /* Running cost base */ truelight@0: foreach_engine { truelight@0: uint32 base = grf_load_dword(&buf); truelight@0: truelight@0: switch (base) { truelight@0: case 0x4c30: truelight@0: rvi[i].engclass = 0; truelight@0: break; truelight@0: case 0x4c36: truelight@0: rvi[i].engclass = 1; truelight@0: break; truelight@0: case 0x4c3c: truelight@0: rvi[i].engclass = 2; truelight@0: break; truelight@0: } truelight@0: dewagonize(base, engine + i); truelight@0: } truelight@0: break; truelight@0: } truelight@0: case 0x12: truelight@0: { /* Sprite ID */ truelight@0: foreach_engine { truelight@0: uint8 spriteid = grf_load_byte(&buf); truelight@0: truelight@0: if (spriteid == 0xfd && rvi[i].image_index != 0xfd) truelight@0: _engine_original_sprites[engine + i] = rvi[i].image_index; truelight@0: rvi[i].image_index = spriteid; truelight@0: } truelight@0: break; truelight@0: } truelight@0: case 0x13: truelight@0: { /* Dual-headed */ truelight@0: foreach_engine { truelight@0: uint8 dual = grf_load_byte(&buf); truelight@0: truelight@0: if (dual) { truelight@0: rvi[i].flags |= 1; truelight@0: } else { truelight@0: rvi[i].flags &= ~1; truelight@0: } truelight@0: } truelight@0: break; truelight@0: } truelight@0: case 0x14: truelight@0: { /* Cargo capacity */ truelight@0: foreach_engine { truelight@0: uint8 capacity = grf_load_byte(&buf); truelight@0: truelight@0: rvi[i].capacity = capacity; truelight@0: } truelight@0: break; truelight@0: } truelight@0: case 0x15: truelight@0: { /* Cargo type */ truelight@0: foreach_engine { truelight@0: uint8 ctype = grf_load_byte(&buf); truelight@0: truelight@0: rvi[i].cargo_type = ctype; truelight@0: } truelight@0: break; truelight@0: } truelight@0: case 0x16: truelight@0: { /* Weight */ truelight@0: foreach_engine { truelight@0: uint8 weight = grf_load_byte(&buf); truelight@0: truelight@0: rvi[i].weight = weight; truelight@0: } truelight@0: break; truelight@0: } truelight@0: case 0x17: truelight@0: { /* Cost factor */ truelight@0: foreach_engine { truelight@0: uint8 cfactor = grf_load_byte(&buf); truelight@0: truelight@0: rvi[i].base_cost = cfactor; truelight@0: } truelight@0: break; truelight@0: } truelight@0: case 0x18: truelight@0: { /* AI rank */ truelight@0: /* TODO: _railveh_score should be merged to _rail_vehicle_info. */ truelight@0: foreach_engine { truelight@0: grf_load_byte(&buf); truelight@0: } truelight@0: ret = 1; truelight@0: break; truelight@0: } truelight@0: case 0x19: truelight@0: { /* Engine traction type */ truelight@0: /* TODO: What do the individual numbers mean? truelight@0: * XXX: And in what base are they, in fact? --pasky */ truelight@0: foreach_engine { truelight@0: uint8 traction = grf_load_byte(&buf); truelight@0: int engclass; truelight@0: truelight@0: if (traction <= 0x07) truelight@0: engclass = 0; truelight@0: else if (traction <= 0x27) truelight@0: engclass = 1; truelight@0: else if (traction <= 0x31) truelight@0: engclass = 2; truelight@0: else truelight@0: break; truelight@0: truelight@0: rvi[i].engclass = engclass; truelight@0: } truelight@0: break; truelight@0: } truelight@0: /* TODO */ truelight@0: /* Fall-through for unimplemented four bytes long properties. */ truelight@0: case 0x1d: /* Refit cargo */ truelight@0: foreach_engine { truelight@0: grf_load_word(&buf); truelight@0: } truelight@0: /* Fall-through for unimplemented two bytes long properties. */ truelight@0: case 0x1b: /* Powered wagons power bonus */ truelight@0: foreach_engine { truelight@0: grf_load_byte(&buf); truelight@0: } truelight@0: /* Fall-through for unimplemented one byte long properties. */ truelight@0: case 0x1a: /* Sort order */ truelight@0: case 0x1c: /* Refit cost */ truelight@0: case 0x1e: /* Callback */ truelight@0: case 0x1f: /* Tractive effort */ truelight@0: case 0x21: /* Shorter tenders */ truelight@0: case 0x22: /* Visual */ truelight@0: case 0x23: /* Powered wagons weight bonus */ truelight@0: /* TODO */ truelight@0: foreach_engine { truelight@0: grf_load_byte(&buf); truelight@0: } truelight@0: ret = 1; truelight@0: break; truelight@0: default: truelight@0: ret = 1; truelight@0: break; truelight@0: } truelight@0: *bufp = buf; truelight@0: return ret; truelight@0: } truelight@0: truelight@0: static int ShipVehicleChangeInfo(uint engine, int numinfo, int prop, byte **bufp, int len) truelight@0: { truelight@0: ShipVehicleInfo *svi = &_ship_vehicle_info[engine]; truelight@0: byte *buf = *bufp; truelight@0: int i; truelight@0: int ret = 0; truelight@0: truelight@0: //printf("e %x prop %x?\n", engine, prop); truelight@0: switch (prop) { truelight@0: case 0x08: truelight@0: { /* Sprite ID */ truelight@0: foreach_engine { truelight@0: uint8 spriteid = grf_load_byte(&buf); truelight@0: truelight@0: if (spriteid == 0xff) truelight@0: spriteid = 0xfd; // ships have different custom id in the GRF file truelight@0: truelight@0: // This is currently not used but there's no reason truelight@0: // in not having it here for the future. truelight@0: if (spriteid == 0xfd truelight@0: && svi[i].image_index != 0xfd) truelight@0: _engine_original_sprites[SHIP_ENGINES_INDEX truelight@0: + engine + i] truelight@0: = svi[i].image_index; truelight@0: svi[i].image_index = spriteid; truelight@0: } truelight@0: break; truelight@0: } truelight@0: case 0x09: truelight@0: { /* Refittable */ truelight@0: foreach_engine { truelight@0: uint8 refittable = grf_load_byte(&buf); truelight@0: truelight@0: svi[i].refittable = refittable; truelight@0: } truelight@0: break; truelight@0: } truelight@0: case 0x0a: truelight@0: { /* Cost factor */ truelight@0: foreach_engine { truelight@0: uint8 cost_factor = grf_load_byte(&buf); truelight@0: truelight@0: svi[i].base_cost = cost_factor; // ?? is it base_cost? truelight@0: } truelight@0: break; truelight@0: } truelight@0: case 0x0b: truelight@0: { /* Speed */ truelight@0: foreach_engine { truelight@0: uint8 speed = grf_load_byte(&buf); truelight@0: truelight@0: svi[i].max_speed = speed; // ?? units truelight@0: } truelight@0: break; truelight@0: } truelight@0: case 0x0c: truelight@0: { /* Cargo type */ truelight@0: truelight@0: foreach_engine { truelight@0: uint8 cargo = grf_load_byte(&buf); truelight@0: truelight@0: // XXX: Need to consult this with patchman yet. truelight@0: #if 0 truelight@0: // Documentation claims this is already the truelight@0: // per-landscape cargo type id, but newships.grf truelight@0: // assume otherwise. truelight@0: cargo = local_cargo_id_ctype[cargo]; truelight@0: #endif truelight@0: svi[i].cargo_type = cargo; truelight@0: } truelight@0: break; truelight@0: } truelight@0: case 0x0d: truelight@0: { /* Cargo capacity */ truelight@0: foreach_engine { truelight@0: uint16 capacity = grf_load_word(&buf); truelight@0: truelight@0: svi[i].capacity = capacity; truelight@0: } truelight@0: break; truelight@0: } truelight@0: case 0x0f: truelight@0: { /* Running cost factor */ truelight@0: foreach_engine { truelight@0: uint8 runcost = grf_load_byte(&buf); truelight@0: truelight@0: svi[i].running_cost = runcost; truelight@0: } truelight@0: break; truelight@0: } truelight@0: case 0x10: truelight@0: { /* SFX */ truelight@0: foreach_engine { truelight@0: uint8 sfx = grf_load_byte(&buf); truelight@0: truelight@0: svi[i].sfx = sfx; truelight@0: } truelight@0: break; truelight@0: } truelight@0: case 0x11: truelight@0: { /* Cargos available for refitting */ truelight@0: foreach_engine { truelight@0: uint32 refit_mask = grf_load_dword(&buf); truelight@0: truelight@0: _engine_refit_masks[SHIP_ENGINES_INDEX + engine + i] = refit_mask; truelight@0: } truelight@0: break; truelight@0: } truelight@0: case 0x12: truelight@0: { /* Callback */ truelight@0: /* TODO */ truelight@0: ret = 1; truelight@0: break; truelight@0: } truelight@0: default: truelight@0: ret = 1; truelight@0: break; truelight@0: } truelight@0: truelight@0: *bufp = buf; truelight@0: return ret; truelight@0: } truelight@0: truelight@0: #undef shift_buf truelight@0: truelight@0: static void VehicleChangeInfo(byte *buf, int len) truelight@0: { truelight@0: byte *bufend = buf + len; truelight@0: int i; truelight@0: truelight@0: /* <00> ()... truelight@0: * truelight@0: * B feature 0, 1, 2 or 3 for trains, road vehicles, ships or planes truelight@0: * 4 for defining new train station sets truelight@0: * B num-props how many properties to change per vehicle/station truelight@0: * B num-info how many vehicles/stations to change truelight@0: * B id ID of first vehicle/station to change, if num-info is truelight@0: * greater than one, this one and the following truelight@0: * vehicles/stations will be changed truelight@0: * B property what property to change, depends on the feature truelight@0: * V new-info new bytes of info (variable size; depends on properties) */ truelight@0: /* TODO: Only trains and ships are supported for now. */ truelight@0: truelight@0: static const VCI_Handler handler[5] = { truelight@0: RailVehicleChangeInfo, truelight@0: NULL, truelight@0: ShipVehicleChangeInfo, truelight@0: NULL, truelight@0: NULL, truelight@0: }; truelight@0: truelight@0: if (len > 5) { truelight@0: uint8 feature = buf[1]; truelight@0: uint8 numprops = buf[2]; truelight@0: uint8 numinfo = buf[3]; truelight@0: byte engine = buf[4]; truelight@0: EngineInfo *ei; truelight@0: truelight@0: if (feature != 0 && feature != 2) { truelight@0: grfmsg(GMS_WARN, "VehicleChangeInfo: Unsupported vehicle type %x, skipping.", feature); truelight@0: return; truelight@0: } truelight@0: truelight@0: ei = &_engine_info[engine + _vehshifts[feature]]; truelight@0: truelight@0: buf += 5; truelight@0: truelight@0: while (numprops-- && buf < bufend) { truelight@0: uint8 prop = grf_load_byte(&buf); truelight@0: truelight@0: switch (prop) { truelight@0: case 0x00: { truelight@0: /* Introduction date */ truelight@0: foreach_engine { truelight@0: uint16 date = grf_load_word(&buf); truelight@0: truelight@0: ei[i].base_intro = date; truelight@0: } truelight@0: break; truelight@0: } truelight@0: case 0x02: { truelight@0: /* Decay speed */ truelight@0: foreach_engine { truelight@0: uint8 decay = grf_load_byte(&buf); truelight@0: truelight@0: ei[i].unk2 &= 0x80; truelight@0: ei[i].unk2 |= decay & 0x7f; truelight@0: } truelight@0: break; truelight@0: } truelight@0: case 0x03: { truelight@0: /* Vehicle life */ truelight@0: foreach_engine { truelight@0: uint8 life = grf_load_byte(&buf); truelight@0: truelight@0: ei[i].lifelength = life; truelight@0: } truelight@0: break; truelight@0: } truelight@0: case 0x04: { truelight@0: /* Model life */ truelight@0: foreach_engine { truelight@0: uint8 life = grf_load_byte(&buf); truelight@0: truelight@0: ei[i].base_life = life; truelight@0: } truelight@0: break; truelight@0: } truelight@0: case 0x06: { truelight@0: /* Climates available */ truelight@0: foreach_engine { truelight@0: uint8 climates = grf_load_byte(&buf); truelight@0: truelight@0: ei[i].railtype_climates &= 0xf0; truelight@0: ei[i].railtype_climates |= climates; truelight@0: } truelight@0: break; truelight@0: } truelight@0: case 0x07: { truelight@0: /* Loading speed */ truelight@0: /* Hyronymus explained me what does truelight@0: * this mean and insists on having a truelight@0: * credit ;-). --pasky */ truelight@0: /* TODO: This needs to be supported by truelight@0: * LoadUnloadVehicle() first. */ truelight@0: foreach_engine { truelight@0: grf_load_byte(&buf); truelight@0: } truelight@0: goto ignoring; truelight@0: } truelight@0: default: truelight@0: { truelight@0: if (handler[feature](engine, numinfo, prop, &buf, bufend - buf)) { truelight@0: ignoring: truelight@0: grfmsg(GMS_WARN, "VehicleChangeInfo: Ignoring property %x.", prop); truelight@0: } truelight@0: break; truelight@0: } truelight@0: } truelight@0: } truelight@0: } truelight@0: #undef shift_buf truelight@0: } truelight@0: truelight@0: truelight@0: /* A sprite superset contains all sprites of a given vehicle (or multiple truelight@0: * vehicles) when carrying given cargo. It consists of several sprite sets. truelight@0: * Superset ids are refered as "cargo id"s by TTDPatch documentation, truelight@0: * contributing to the global confusion. truelight@0: * truelight@0: * A sprite set contains all sprites of a given vehicle carrying given cargo at truelight@0: * a given *stage* - that is usually its load stage. Ie. you can have a truelight@0: * spriteset for an empty wagon, wagon full of coal, half-filled wagon etc. truelight@0: * Each spriteset contains eight sprites (one per direction) or four sprites if truelight@0: * the vehicle is symmetric. */ truelight@0: truelight@0: static int _spriteset_start; truelight@0: static int _spriteset_numsets; truelight@0: static int _spriteset_numents; truelight@0: static int _spriteset_feature; truelight@0: truelight@0: static int _spritesset_count; truelight@0: static struct SpriteSuperSet *_spritesset; truelight@0: truelight@0: static void SpriteNewSet(byte *buf, int len) truelight@0: { truelight@0: /* <01> truelight@0: * truelight@0: * B feature feature to define sprites for truelight@0: * 0, 1, 2, 3: veh-type, 4: train stations truelight@0: * B num-sets number of sprite sets truelight@0: * B num-ent how many entries per sprite set truelight@0: * For vehicles, this is the number of different truelight@0: * vehicle directions in each sprite set truelight@0: * Set num-dirs=8, unless your sprites are symmetric. truelight@0: * In that case, use num-dirs=4. truelight@0: * For stations, must be 12 (hex) for the eighteen truelight@0: * different sprites that make up a station */ truelight@0: /* TODO: No stations support. */ truelight@0: truelight@0: if (len == 4) { truelight@0: uint8 feature = buf[1]; truelight@0: truelight@0: if (feature == 4) { truelight@0: _spriteset_start = 0; truelight@0: grfmsg(GMS_WARN, "SpriteNewSet: Stations unsupported, skipping."); truelight@0: return; truelight@0: } truelight@0: truelight@0: _spriteset_start = _cur_spriteid + 1; truelight@0: _spriteset_numsets = buf[2]; truelight@0: _spriteset_numents = buf[3]; truelight@0: _spriteset_feature = feature; truelight@0: } truelight@0: } truelight@0: truelight@0: static void SpriteNewSuperset(byte *buf, int len) truelight@0: { truelight@0: byte *bufend = buf + len; truelight@0: truelight@0: /* <02> truelight@0: * truelight@0: * B feature see action 1 truelight@0: * B set-id ID of this particular definition truelight@0: * B type/num-entries truelight@0: * if 80 or greater, this is a randomized or variational truelight@0: * list definition, see below truelight@0: * otherwise it specifies a number of entries, the exact truelight@0: * meaning depends on the feature truelight@0: * V feature-specific-data (huge mess, don't even look it up --pasky) */ truelight@0: /* TODO: Only trains supported now. No 0x80-types (ugh). */ truelight@0: /* TODO: Also, empty sprites aren't handled for now. Need to investigate truelight@0: * the "opacity" rules for these, that is which sprite to fall back to truelight@0: * when. --pasky */ truelight@0: truelight@0: if (bufend - buf > 4) { truelight@0: uint8 feature = buf[1]; truelight@0: uint8 setid = buf[2]; truelight@0: uint8 numloaded = buf[3]; truelight@0: uint8 numloading = buf[4]; truelight@0: struct SpriteSuperSet *superset; truelight@0: int i; truelight@0: truelight@0: if (feature == 4) { truelight@0: grfmsg(GMS_WARN, "SpriteNewSuperset: Stations unsupported, skipping."); truelight@0: return; truelight@0: } truelight@0: truelight@0: if (numloaded == 0x81) { truelight@0: // XXX: This is _VERY_ ad hoc just to handle Dm3. And that is truelight@0: // a semi-futile ask because the great Patchman himself says truelight@0: // this is just buggy. It dereferences last (first) byte of truelight@0: // a schedule list pointer of the vehicle and if it's 0xff truelight@0: // it uses superset 01, otherwise it uses superset 00. Now truelight@0: // if _you_ understand _that_... We just assume it is never truelight@0: // 0xff and therefore go for superset 00. --pasky truelight@0: uint8 var = buf[4]; truelight@0: //uint8 shiftnum = buf[5]; truelight@0: //uint8 andmask = buf[6]; truelight@0: uint8 nvar = buf[7]; truelight@0: //uint32 val; truelight@0: uint16 def; truelight@0: truelight@0: grfmsg(GMS_WARN, "SpriteNewSuperset(0x81): Unsupported variable %x. Using default cid.", var); truelight@0: truelight@0: //val = (0xff << shiftnum) & andmask; truelight@0: truelight@0: //Go for the default. truelight@0: if (setid >= _spritesset_count) { truelight@0: _spritesset_count = setid + 1; truelight@0: _spritesset = realloc(_spritesset, _spritesset_count * sizeof(struct SpriteSuperSet)); truelight@0: } truelight@0: buf += 8 + nvar * 4; truelight@0: def = grf_load_word(&buf); truelight@0: _spritesset[setid] = _spritesset[def]; truelight@0: return; truelight@0: truelight@0: } else if (numloaded & 0x80) { truelight@0: grfmsg(GMS_WARN, "SpriteNewSuperset(0x%x): Unsupported special superset.", numloaded); truelight@0: return; truelight@0: } truelight@0: truelight@0: if (!_spriteset_start) { truelight@0: grfmsg(GMS_WARN, "SpriteNewSuperset: No sprite set to work on! Skipping."); truelight@0: return; truelight@0: } truelight@0: truelight@0: if (_spriteset_feature != feature) { truelight@0: grfmsg(GMS_WARN, "SpriteNewSuperset: Superset feature %x doesn't match set feature %x! Skipping.", feature, _spriteset_feature); truelight@0: return; truelight@0: } truelight@0: truelight@0: if (setid >= _spritesset_count) { truelight@0: _spritesset_count = setid + 1; truelight@0: _spritesset = realloc(_spritesset, _spritesset_count * sizeof(struct SpriteSuperSet)); truelight@0: } truelight@0: superset = &_spritesset[setid]; truelight@0: memset(superset, 0, sizeof(struct SpriteSuperSet)); truelight@0: superset->sprites_per_set = _spriteset_numents; truelight@0: truelight@0: buf += 5; truelight@0: truelight@0: for (i = 0; buf < bufend && i < numloaded; i++) { truelight@0: uint16 spriteset_id = grf_load_word(&buf); truelight@0: truelight@0: if (_spritesset[setid].loaded_count > 16) { truelight@0: grfmsg(GMS_WARN, "SpriteNewSuperset: More than 16 sprites in superset %x, skipping.", setid); truelight@0: return; truelight@0: } truelight@0: superset->loaded[superset->loaded_count++] truelight@0: = _spriteset_start + spriteset_id * _spriteset_numents; truelight@0: } truelight@0: truelight@0: for (i = 0; buf < bufend && i < numloading; i++) { truelight@0: uint16 spriteset_id = grf_load_word(&buf); truelight@0: truelight@0: if (_spritesset[setid].loading_count > 16) { truelight@0: grfmsg(GMS_WARN, "SpriteNewSuperset: More than 16 sprites in superset %x, skipping.", setid); truelight@0: return; truelight@0: } truelight@0: superset->loading[superset->loading_count++] = _spriteset_start + spriteset_id * _spriteset_numents; truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: static void VehicleMapSpriteSuperset(byte *buf, int len) truelight@0: { truelight@0: byte *bufend = buf + len; truelight@0: /* <03> ... [ ]... truelight@0: * truelight@0: * B feature see action 0 truelight@0: * B n-id bits 0-6: how many IDs this definition applies to truelight@0: * bit 7: if set, this is a wagon override definition (see below) truelight@0: * B ids the IDs for which this definition applies truelight@0: * B num-cid number of cargo IDs in this definition truelight@0: * can be zero, in that case the def-cid is used always truelight@0: * B cargo-type type of this cargo type (e.g. mail=2, wood=7, see below) truelight@0: * W cid cargo ID for this type of cargo truelight@0: * W def-cid default cargo ID */ truelight@0: /* TODO: Only trains supported now. */ truelight@0: /* TODO: Multiple cargo support could be useful even for trains/cars - truelight@0: * cargo id 0xff is used for showing images in the build train list. */ truelight@0: truelight@0: static byte *last_engines; truelight@0: static int last_engines_count; truelight@0: truelight@0: if (bufend - buf > 6) { truelight@0: uint8 feature = buf[1]; truelight@0: uint8 idcount = buf[2] & 0x7f; truelight@0: int wagover = buf[2] & 0x80; truelight@0: uint8 cidcount = buf[3 + idcount]; truelight@0: int c, i; truelight@0: truelight@0: if (feature == 4) { truelight@0: grfmsg(GMS_WARN, "VehicleMapSpriteSuperset: Stations unsupported, skipping."); truelight@0: return; truelight@0: } truelight@0: truelight@0: // FIXME: Tropicset contains things like: truelight@0: // 03 00 01 19 01 00 00 00 00 - this is missing one 00 at the end, truelight@0: // what should we exactly do with that? --pasky truelight@0: truelight@0: if (!_spriteset_start || !_spritesset) { truelight@0: grfmsg(GMS_WARN, "VehicleMapSpriteSuperset: No sprite set to work on! Skipping."); truelight@0: return; truelight@0: } truelight@0: truelight@0: if (!wagover && last_engines_count != idcount) { truelight@0: last_engines = realloc(last_engines, idcount); truelight@0: last_engines_count = idcount; truelight@0: } truelight@0: truelight@0: for (i = 0; i < idcount; i++) { truelight@0: uint8 engine = buf[3 + i] + _vehshifts[feature]; truelight@0: byte *bp = &buf[4 + idcount]; truelight@0: truelight@0: for (c = 0; c < cidcount; c++) { truelight@0: uint8 ctype = grf_load_byte(&bp); truelight@0: uint16 supersetid = grf_load_word(&bp); truelight@0: truelight@0: if (supersetid >= _spritesset_count) { truelight@0: grfmsg(GMS_WARN, "VehicleMapSpriteSuperset: Spriteset %x out of range %x, skipping.", supersetid, _spritesset_count); truelight@0: return; truelight@0: } truelight@0: truelight@0: if (ctype == 0xff) truelight@0: ctype = CID_PURCHASE; truelight@0: truelight@0: if (wagover) { truelight@0: // TODO: No multiple cargo types per vehicle yet. --pasky truelight@0: SetWagonOverrideSprites(engine, &_spritesset[supersetid], last_engines, last_engines_count); truelight@0: } else { truelight@0: SetCustomEngineSprites(engine, ctype, &_spritesset[supersetid]); truelight@0: last_engines[i] = engine; truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: { truelight@0: byte *bp = buf + 4 + idcount + cidcount * 3; truelight@0: uint16 supersetid = grf_load_word(&bp); truelight@0: truelight@0: for (i = 0; i < idcount; i++) { truelight@0: uint8 engine = buf[3 + i] + _vehshifts[feature]; truelight@0: truelight@0: // Don't tell me you don't love duplicated code! truelight@0: if (supersetid >= _spritesset_count) { truelight@0: grfmsg(GMS_WARN, "VehicleMapSpriteSuperset: Spriteset %x out of range %x, skipping.", supersetid, _spritesset_count); truelight@0: return; truelight@0: } truelight@0: truelight@0: if (wagover) { truelight@0: // TODO: No multiple cargo types per vehicle yet. --pasky truelight@0: SetWagonOverrideSprites(engine, &_spritesset[supersetid], last_engines, last_engines_count); truelight@0: } else { truelight@0: SetCustomEngineSprites(engine, CID_DEFAULT, &_spritesset[supersetid]); truelight@0: last_engines[i] = engine; truelight@0: } truelight@0: } truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: static void VehicleNewName(byte *buf, int len) truelight@0: { truelight@0: /* <04> truelight@0: * truelight@0: * B veh-type see action 0 truelight@0: * B language-id language ID with bit 7 cleared (see below) truelight@0: * B num-veh number of vehicles which are getting a new name truelight@0: * B offset number of the first vehicle that gets a new name truelight@0: * S data new texts, each of them zero-terminated, after truelight@0: * which the next name begins. */ truelight@0: /* TODO: No support for changing non-vehicle text. Perhaps we shouldn't truelight@0: * implement it at all, but it could be useful for some "modpacks" truelight@0: * (completely new scenarios changing all graphics and logically also truelight@0: * factory names etc). We should then also support all languages (by truelight@0: * name), not only the original four ones. --pasky */ truelight@0: truelight@0: if (len > 5) { truelight@0: uint8 feature = buf[1]; truelight@0: uint8 lang = buf[2]; truelight@0: uint8 id = buf[4] + _vehshifts[feature]; truelight@0: uint8 endid = id + buf[3]; truelight@0: truelight@0: if (lang & 0x80) { truelight@0: grfmsg(GMS_WARN, "VehicleNewName: No support for changing in-game texts. Skipping."); truelight@0: return; truelight@0: } truelight@0: truelight@0: if (!(lang & 3)) { truelight@0: /* XXX: If non-English name, silently skip it. */ truelight@0: return; truelight@0: } truelight@0: truelight@0: buf += 5, len -= 5; truelight@0: for (; id < endid && len > 0; id++) { truelight@0: int ofs = strlen(buf) + 1; truelight@0: truelight@0: if (ofs < 128) truelight@0: SetCustomEngineName(id, buf); truelight@0: buf += ofs, len -= ofs; truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: static void GraphicsNew(byte *buf, int len) truelight@0: { truelight@0: /* <05> truelight@0: * truelight@0: * B graphics-type What set of graphics the sprites define. truelight@0: * B num-sprites How many sprites are in this set? truelight@0: * V other data Graphics type specific data. Currently unused. */ truelight@0: /* TODO */ truelight@0: } truelight@0: truelight@0: static void CfgApply(byte *buf, int len) truelight@0: { truelight@0: /* <06> ... truelight@0: * truelight@0: * B param-num Number of parameter to substitute (First = "zero") truelight@0: * Ignored if that parameter was not specified in newgrf.cfg truelight@0: * B param-size How many bytes to replace. If larger than 4, the truelight@0: * bytes of the following parameter are used. In that truelight@0: * case, nothing is applied unless *all* parameters truelight@0: * were specified. truelight@0: * B offset Offset into data from beginning of next sprite truelight@0: * to place where parameter is to be stored. */ truelight@0: /* TODO */ truelight@0: } truelight@0: truelight@0: static void SkipIf(byte *buf, int len) truelight@0: { truelight@0: /* <07/09> truelight@0: * truelight@0: * B param-num truelight@0: * B param-size truelight@0: * B condition-type truelight@0: * V value truelight@0: * B num-sprites */ truelight@0: /* TODO: We only support few tests. */ truelight@0: /* TODO: More params. More condition types. */ truelight@0: truelight@0: if (len > 5) { truelight@0: uint8 param = buf[1]; truelight@0: uint8 paramsize = buf[2]; truelight@0: uint8 condtype = buf[3]; truelight@0: uint8 numsprites = buf[4 + paramsize]; truelight@0: int val, result; truelight@0: truelight@0: if (param == 0x83) { truelight@0: val = _opt.landscape; truelight@0: } else { truelight@0: grfmsg(GMS_WARN, "Unsupported param %x. Ignoring test.", param); truelight@0: return; truelight@0: } truelight@0: truelight@0: switch (condtype) { truelight@0: case 2: result = (buf[4] == val); truelight@0: break; truelight@0: case 3: result = (buf[4] != val); truelight@0: break; truelight@0: default: truelight@0: grfmsg(GMS_WARN, "Unsupported test %d. Ignoring.", condtype); truelight@0: return; truelight@0: } truelight@0: truelight@0: if (!result) truelight@0: return; truelight@0: truelight@0: _skip_sprites = numsprites; truelight@0: if (_skip_sprites == 0) { truelight@0: /* Zero means there are no sprites to skip, so truelight@0: * we use -1 to indicate that all further truelight@0: * sprites should be skipped. */ truelight@0: _skip_sprites = -1; truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: static void GRFInfo(byte *buf, int len) truelight@0: { truelight@0: /* <08> truelight@0: * truelight@0: * B version newgrf version, currently 06 truelight@0: * 4*B grf-id globally unique ID of this .grf file truelight@0: * S name name of this .grf set truelight@0: * S info string describing the set, and e.g. author and copyright */ truelight@0: /* TODO: Check version. (We should have own versioning done somehow.) */ truelight@0: truelight@0: if (len > 8) { truelight@0: uint8 version = buf[1]; truelight@0: // this is de facto big endian - grf_load_dword() unsuitable truelight@0: uint32 grfid = buf[2] << 24 | buf[3] << 16 | buf[4] << 8 | buf[5]; truelight@0: printf("[%s] Loaded GRFv%d set %08lx - %s:\n%s\n", _cur_grffile, version, grfid, buf+6, buf+6+strlen(buf+6)+1); truelight@0: } truelight@0: } truelight@0: truelight@0: static void SpriteReplace(byte *buf, int len) truelight@0: { truelight@0: /* <0A> [ ...] truelight@0: * : truelight@0: * truelight@0: * B num-sets How many sets of sprites to replace. truelight@0: * Each set: truelight@0: * B num-sprites How many sprites are in this set truelight@0: * W first-sprite First sprite number to replace */ truelight@0: /* TODO */ truelight@0: } truelight@0: truelight@0: static void GRFError(byte *buf, int len) truelight@0: { truelight@0: /* <0B> [ 00] [] 00 [] truelight@0: * truelight@0: * B severity 00: notice, contine loading grf file truelight@0: * 01: warning, continue loading grf file truelight@0: * 02: error, but continue loading grf file, and attempt truelight@0: * loading grf again when loading or starting next game truelight@0: * 03: error, abort loading and prevent loading again in truelight@0: * the future (only when restarting the patch) truelight@0: * B language-id see action 4, use 1F for built-in error messages truelight@0: * B message-id message to show, see below truelight@0: * S message for custom messages (message-id FF), text of the message truelight@0: * not present for built-in messages. truelight@0: * V data additional data for built-in (or custom) messages truelight@0: * B parnum see action 6, only used with built-in message 03 */ truelight@0: /* TODO: For now we just show the message, sometimes incomplete and never translated. */ truelight@0: truelight@0: static const char * const msgstr[4] = { truelight@0: "Requires at least pseudo-TTDPatch version %s.", truelight@0: "This file is for %s version of TTD.", truelight@0: "Designed to be used with %s.", truelight@0: "Invalid parameter %s.", truelight@0: }; truelight@0: truelight@0: if (len > 5) { truelight@0: uint8 severity = buf[1]; truelight@0: uint8 msgid = buf[3]; truelight@0: truelight@0: if (msgid == 0xff) { truelight@0: grfmsg(severity, "%s", buf+4); truelight@0: } else { truelight@0: grfmsg(severity, msgstr[msgid], buf+4); truelight@0: } truelight@0: } truelight@0: } truelight@0: truelight@0: static void GRFComment(byte *buf, int len) truelight@0: { truelight@0: /* <0C> [] truelight@0: * truelight@0: * V ignored Anything following the 0C is ignored */ truelight@0: } truelight@0: truelight@0: static void ParamSet(byte *buf, int len) truelight@0: { truelight@0: /* <0D> [] truelight@0: * truelight@0: * B target parameter number where result is stored truelight@0: * B operation operation to perform, see below truelight@0: * B source1 first source operand truelight@0: * B source2 second source operand truelight@0: * D data data to use in the calculation, not necessary truelight@0: * if both source1 and source2 refer to actual parameters truelight@0: * truelight@0: * Operations truelight@0: * 00 Set parameter equal to source1 truelight@0: * 01 Addition, source1 + source2 truelight@0: * 02 Subtraction, source1 - source2 truelight@0: * 03 Unsigned multiplication, source1 * source2 (both unsigned) truelight@0: * 04 Signed multiplication, source1 * source2 (both signed) truelight@0: * 05 Unsigned bit shift, source1 by source2 (source2 taken to be a truelight@0: * signed quantity; left shift if positive and right shift if truelight@0: * negative, source1 is unsigned) truelight@0: * 06 Signed bit shift, source1 by source2 truelight@0: * (source2 like in 05, and source1 as well) truelight@0: */ truelight@0: /* TODO */ truelight@0: } truelight@0: truelight@0: static void GRFInhibit(byte *buf, int len) truelight@0: { truelight@0: /* <0E> truelight@0: * truelight@0: * B num Number of GRFIDs that follow truelight@0: * D grfids GRFIDs of the files to deactivate */ truelight@0: /* TODO */ truelight@0: } truelight@0: truelight@0: /* Here we perform initial decoding of some special sprites (as are they truelight@0: * described at http://www.ttdpatch.net/src/newgrf.txt, but this is only a very truelight@0: * partial implementation yet; also, we ignore the stages stuff). */ truelight@0: /* XXX: We consider GRF files trusted. It would be trivial to exploit OTTD by truelight@0: * a crafted invalid GRF file. We should tell that to the user somehow, or truelight@0: * better make this more robust in the future. */ truelight@0: truelight@0: void DecodeSpecialSprite(const char *filename, int num, int spriteid) truelight@0: { truelight@0: #define NUM_ACTIONS 0xf truelight@0: static const SpecialSpriteHandler handlers[NUM_ACTIONS] = { truelight@0: /* 0x0 */ VehicleChangeInfo, truelight@0: /* 0x1 */ SpriteNewSet, truelight@0: /* 0x2 */ SpriteNewSuperset, truelight@0: /* 0x3 */ VehicleMapSpriteSuperset, truelight@0: /* 0x4 */ VehicleNewName, truelight@0: /* 0x5 */ GraphicsNew, truelight@0: /* 0x6 */ CfgApply, truelight@0: /* 0x7 */ SkipIf, truelight@0: /* 0x8 */ GRFInfo, truelight@0: /* 0x9 */ SkipIf, truelight@0: /* 0xa */ SpriteReplace, truelight@0: /* 0xb */ GRFError, truelight@0: /* 0xc */ GRFComment, truelight@0: /* 0xd */ ParamSet, truelight@0: /* 0xe */ GRFInhibit, truelight@0: }; truelight@0: byte action; truelight@0: byte *buf = malloc(num); truelight@0: int i; truelight@0: truelight@0: _cur_grffile = filename; truelight@0: _cur_spriteid = spriteid; truelight@0: truelight@0: for (i = 0; i != num; i++) truelight@0: buf[i] = FioReadByte(); truelight@0: truelight@0: action = buf[0]; truelight@0: if (action < NUM_ACTIONS) { truelight@0: handlers[action](buf, num); truelight@0: } else { truelight@0: grfmsg(GMS_WARN, "Unknown special sprite action %x, skipping.", action); truelight@0: } truelight@0: truelight@0: free(buf); truelight@0: #undef NUM_ACTIONS truelight@0: }