dominik@452: #include "stdafx.h" dominik@452: dominik@452: #include dominik@452: dominik@452: #include "ttd.h" dominik@452: #include "gfx.h" dominik@452: #include "fileio.h" dominik@452: #include "engine.h" dominik@452: #include "station.h" dominik@452: #include "sprite.h" dominik@452: #include "newgrf.h" dominik@452: dominik@452: /* TTDPatch extended GRF format codec dominik@452: * (c) Petr Baudis 2004 (GPL'd) dominik@452: * Changes by Florian octo Forster are (c) by the OpenTTD development team. dominik@452: * dominik@452: * Contains portions of documentation by TTDPatch team. dominik@452: * Thanks especially to Josef Drexler for the documentation as well as a lot dominik@452: * of help at #tycoon. Also thanks to Michael Blunck for is GRF files which dominik@452: * served as subject to the initial testing of this codec. */ dominik@452: dominik@455: dominik@452: extern int _skip_sprites; dominik@452: extern int _replace_sprites_count[16]; dominik@452: extern int _replace_sprites_offset[16]; dominik@452: extern int _traininfo_vehicle_pitch; dominik@452: dominik@455: struct GRFFile *_cur_grffile, *_first_grffile; dominik@478: int _grffile_count; dominik@452: static int _cur_spriteid; dominik@452: static int _cur_stage; dominik@452: extern int _custom_sprites_base; dominik@452: dominik@452: static int32 _paramlist[0x7f]; dominik@452: static int _param_max; dominik@452: dominik@452: /* 32 * 8 = 256 flags. Apparently TTDPatch uses this many.. */ dominik@452: static uint32 _ttdpatch_flags[8]; dominik@452: dominik@452: dominik@452: enum grfspec_feature { dominik@452: GSF_TRAIN, dominik@452: GSF_ROAD, dominik@452: GSF_SHIP, dominik@452: GSF_AIRCRAFT, dominik@452: GSF_STATION, dominik@452: GSF_BRIDGE, dominik@452: GSF_TOWNHOUSE, dominik@452: }; dominik@452: dominik@452: dominik@452: typedef void (*SpecialSpriteHandler)(byte *buf, int len); dominik@452: dominik@452: static const int _vehcounts[4] = { dominik@452: /* GSF_TRAIN */ NUM_TRAIN_ENGINES, dominik@452: /* GSF_ROAD */ NUM_ROAD_ENGINES, dominik@452: /* GSF_SHIP */ NUM_SHIP_ENGINES, dominik@452: /* GSF_AIRCRAFT */ NUM_AIRCRAFT_ENGINES dominik@452: }; dominik@452: dominik@452: static const int _vehshifts[4] = { dominik@452: /* GSF_TRAIN */ 0, dominik@452: /* GSF_ROAD */ ROAD_ENGINES_INDEX, dominik@452: /* GSF_SHIP */ SHIP_ENGINES_INDEX, dominik@452: /* GSF_AIRCRAFT */ AIRCRAFT_ENGINES_INDEX, dominik@452: }; dominik@452: dominik@452: dominik@452: /* Debugging messages policy: dominik@452: * dominik@452: * These should be the severities used for direct DEBUG() calls dominik@452: * (there is room for exceptions, but you have to have a good cause): dominik@452: * dominik@452: * 0..2 - dedicated to grfmsg() dominik@452: * 3 dominik@452: * 4 dominik@452: * 5 dominik@452: * 6 - action handler entry reporting - one per action dominik@452: * 7 - basic action progress reporting - in loops, only single one allowed dominik@452: * 8 - more detailed progress reporting - less important stuff, in deep loops etc dominik@452: * 9 - extremely detailed progress reporting - detailed reports inside of deep loops and so dominik@452: */ dominik@452: dominik@452: dominik@452: enum grfmsg_severity { dominik@452: GMS_NOTICE, dominik@452: GMS_WARN, dominik@452: GMS_ERROR, dominik@452: GMS_FATAL, dominik@452: }; dominik@452: dominik@452: static void CDECL grfmsg(enum grfmsg_severity severity, const char *str, ...) dominik@452: { dominik@452: static const char * const severitystr[4] = { "Notice", "Warning", "Error", "Fatal" }; dominik@452: int export_severity = 0; dominik@452: char buf[1024]; dominik@452: va_list va; dominik@452: dominik@452: va_start(va, str); dominik@452: vsprintf(buf, str, va); dominik@452: va_end(va); dominik@452: dominik@452: export_severity = 2 - (severity == GMS_FATAL ? 2 : severity); dominik@452: DEBUG(grf, export_severity) ("[%s][%s] %s", _cur_grffile->filename, severitystr[severity], buf); dominik@452: } dominik@452: dominik@452: dominik@452: #define check_length(real, wanted, where) \ dominik@452: do { \ dominik@452: if (real < wanted) { \ dominik@452: grfmsg(GMS_ERROR, "%s: Invalid special sprite length %d (expected %d)!", where, real, wanted); \ dominik@452: return; \ dominik@452: } \ dominik@452: } while (0) dominik@452: dominik@452: tron@500: static inline byte grf_load_byte(byte **buf) dominik@452: { dominik@452: return *(*buf)++; dominik@452: } dominik@452: dominik@452: static uint16 grf_load_word(byte **buf) dominik@452: { dominik@452: uint16 val; dominik@452: byte *p = *buf; dominik@452: val = p[0]; dominik@452: val |= p[1] << 8; dominik@452: *buf = p + 2; dominik@452: return val; dominik@452: } dominik@452: dominik@452: static uint16 grf_load_dword(byte **buf) dominik@452: { dominik@452: uint32 val; dominik@452: byte *p = *buf; dominik@452: val = p[0]; dominik@452: val |= p[1] << 8; dominik@452: val |= p[2] << 16; dominik@452: val |= p[3] << 24; dominik@452: *buf = p + 4; dominik@452: return val; dominik@452: } dominik@452: dominik@452: dominik@452: static struct GRFFile *GetFileByGRFID(uint32 grfid) dominik@452: { dominik@452: struct GRFFile *file; dominik@452: dominik@452: file = _first_grffile; dominik@452: while ((file != NULL) && (file->grfid != grfid)) dominik@452: file = file->next; dominik@452: dominik@452: return file; dominik@452: } dominik@452: dominik@452: static struct GRFFile *GetFileByFilename(const char *filename) dominik@452: { dominik@452: struct GRFFile *file; dominik@452: dominik@452: file = _first_grffile; dominik@452: while ((file != NULL) && strcmp(file->filename, filename)) dominik@452: file = file->next; dominik@452: dominik@452: return file; dominik@452: } dominik@452: dominik@452: dominik@452: typedef bool (*VCI_Handler)(uint engine, int numinfo, int prop, byte **buf, int len); dominik@452: dominik@452: #define FOR_EACH_OBJECT for (i = 0; i < numinfo; i++) dominik@452: dominik@452: static void dewagonize(int condition, int engine) dominik@452: { dominik@452: EngineInfo *ei = &_engine_info[engine]; dominik@452: RailVehicleInfo *rvi = &_rail_vehicle_info[engine]; dominik@452: dominik@452: if (condition != 0) { dominik@452: ei->unk2 &= ~0x80; dominik@452: rvi->flags &= ~2; dominik@452: } else { dominik@452: ei->unk2 |= 0x80; dominik@452: rvi->flags |= 2; dominik@452: } dominik@452: } dominik@452: dominik@452: static bool RailVehicleChangeInfo(uint engine, int numinfo, int prop, byte **bufp, int len) dominik@452: { dominik@452: EngineInfo *ei = &_engine_info[engine]; dominik@452: RailVehicleInfo *rvi = &_rail_vehicle_info[engine]; dominik@452: byte *buf = *bufp; dominik@452: int i; dominik@452: bool ret = false; dominik@452: dominik@452: switch (prop) { dominik@452: case 0x05: { /* Track type */ dominik@452: FOR_EACH_OBJECT { dominik@452: uint8 tracktype = grf_load_byte(&buf); dominik@452: dominik@452: ei[i].railtype_climates &= 0xf; dominik@452: ei[i].railtype_climates |= tracktype << 4; dominik@452: } dominik@452: } break; dominik@452: case 0x08: { /* AI passenger service */ dominik@452: /* TODO */ dominik@452: FOR_EACH_OBJECT { dominik@452: grf_load_byte(&buf); dominik@452: } dominik@452: ret = true; dominik@452: } break; dominik@452: case 0x09: { /* Speed */ dominik@452: FOR_EACH_OBJECT { dominik@452: uint16 speed = grf_load_word(&buf); dominik@452: dominik@452: rvi[i].max_speed = speed; dominik@452: dewagonize(speed, engine + i); dominik@452: } dominik@452: } break; dominik@452: case 0x0B: { /* Power */ dominik@452: FOR_EACH_OBJECT { dominik@452: uint16 power = grf_load_word(&buf); dominik@452: dominik@452: rvi[i].power = power; dominik@452: dewagonize(power, engine + i); dominik@452: } dominik@452: } break; dominik@452: case 0x0D: { /* Running cost factor */ dominik@452: FOR_EACH_OBJECT { dominik@452: uint8 runcostfact = grf_load_byte(&buf); dominik@452: dominik@452: rvi[i].running_cost_base = runcostfact; dominik@452: dewagonize(runcostfact, engine + i); dominik@452: } dominik@452: } break; dominik@452: case 0x0E: { /* Running cost base */ dominik@452: FOR_EACH_OBJECT { dominik@452: uint32 base = grf_load_dword(&buf); dominik@452: dominik@452: switch (base) { dominik@452: case 0x4C30: dominik@452: rvi[i].engclass = 0; dominik@452: break; dominik@452: case 0x4C36: dominik@452: rvi[i].engclass = 1; dominik@452: break; dominik@452: case 0x4C3C: dominik@452: rvi[i].engclass = 2; dominik@452: break; dominik@452: } dominik@452: dewagonize(base, engine + i); dominik@452: } dominik@452: } break; dominik@452: case 0x12: { /* Sprite ID */ dominik@452: FOR_EACH_OBJECT { dominik@452: uint8 spriteid = grf_load_byte(&buf); dominik@452: dominik@452: if (spriteid == 0xFD && rvi[i].image_index != 0xFD) dominik@452: _engine_original_sprites[engine + i] = rvi[i].image_index; dominik@452: rvi[i].image_index = spriteid; dominik@452: } dominik@452: } break; dominik@452: case 0x13: { /* Dual-headed */ dominik@452: FOR_EACH_OBJECT { dominik@452: uint8 dual = grf_load_byte(&buf); dominik@452: dominik@452: if (dual != 0) { dominik@452: rvi[i].flags |= 1; dominik@452: } else { dominik@452: rvi[i].flags &= ~1; dominik@452: } dominik@452: } dominik@452: } break; dominik@452: case 0x14: { /* Cargo capacity */ dominik@452: FOR_EACH_OBJECT { dominik@452: uint8 capacity = grf_load_byte(&buf); dominik@452: dominik@452: rvi[i].capacity = capacity; dominik@452: } dominik@452: } break; dominik@452: case 0x15: { /* Cargo type */ dominik@452: FOR_EACH_OBJECT { dominik@452: uint8 ctype = grf_load_byte(&buf); dominik@452: dominik@452: rvi[i].cargo_type = ctype; dominik@452: } dominik@452: } break; dominik@452: case 0x16: { /* Weight */ dominik@452: FOR_EACH_OBJECT { dominik@452: uint8 weight = grf_load_byte(&buf); dominik@452: dominik@452: rvi[i].weight = weight; dominik@452: } dominik@452: } break; dominik@452: case 0x17: { /* Cost factor */ dominik@452: FOR_EACH_OBJECT { dominik@452: uint8 cfactor = grf_load_byte(&buf); dominik@452: dominik@452: rvi[i].base_cost = cfactor; dominik@452: } dominik@452: } break; dominik@452: case 0x18: { /* AI rank */ dominik@452: /* TODO: _railveh_score should be merged to _rail_vehicle_info. */ dominik@452: FOR_EACH_OBJECT { dominik@452: grf_load_byte(&buf); dominik@452: } dominik@452: ret = true; dominik@452: } break; dominik@452: case 0x19: { /* Engine traction type */ dominik@452: /* TODO: What do the individual numbers mean? dominik@452: * XXX: And in what base are they, in fact? --pasky */ dominik@452: FOR_EACH_OBJECT { dominik@452: uint8 traction = grf_load_byte(&buf); dominik@452: int engclass; dominik@452: dominik@452: if (traction <= 0x07) dominik@452: engclass = 0; dominik@452: else if (traction <= 0x27) dominik@452: engclass = 1; dominik@452: else if (traction <= 0x31) dominik@452: engclass = 2; dominik@452: else dominik@452: break; dominik@452: dominik@452: rvi[i].engclass = engclass; dominik@452: } dominik@452: } break; dominik@452: case 0x1D: { /* Refit cargo */ dominik@452: FOR_EACH_OBJECT { dominik@452: uint32 refit_mask = grf_load_dword(&buf); dominik@452: dominik@452: _engine_refit_masks[engine + i] = refit_mask; dominik@452: } dominik@452: } break; dominik@452: dominik@452: /* TODO */ dominik@452: /* Fall-through for unimplemented two bytes long properties. */ dominik@452: case 0x1B: /* Powered wagons power bonus */ dominik@452: FOR_EACH_OBJECT { dominik@452: grf_load_byte(&buf); dominik@452: } dominik@452: /* Fall-through for unimplemented one byte long properties. */ dominik@452: case 0x1A: /* Sort order */ dominik@452: case 0x1C: /* Refit cost */ dominik@452: case 0x1E: /* Callback */ dominik@452: case 0x1F: /* Tractive effort */ dominik@452: case 0x21: /* Shorter tenders */ dominik@452: case 0x22: /* Visual */ dominik@452: case 0x23: {/* Powered wagons weight bonus */ dominik@452: /* TODO */ dominik@452: FOR_EACH_OBJECT { dominik@452: grf_load_byte(&buf); dominik@452: } dominik@452: ret = true; dominik@452: } break; dominik@452: default: dominik@452: ret = true; dominik@452: } dominik@452: *bufp = buf; dominik@452: return ret; dominik@452: } dominik@452: dominik@452: static bool RoadVehicleChangeInfo(uint engine, int numinfo, int prop, byte **bufp, int len) dominik@452: { dominik@452: RoadVehicleInfo *rvi = &_road_vehicle_info[engine]; dominik@452: byte *buf = *bufp; dominik@452: int i; dominik@452: bool ret = false; dominik@452: dominik@452: switch (prop) { dominik@452: case 0x08: { /* Speed */ dominik@452: FOR_EACH_OBJECT { dominik@452: uint8 speed = grf_load_byte(&buf); dominik@452: dominik@452: rvi[i].max_speed = speed; // ?? units dominik@452: } dominik@452: } break; dominik@452: case 0x09: { /* Running cost factor */ dominik@452: FOR_EACH_OBJECT { dominik@452: uint8 runcost = grf_load_byte(&buf); dominik@452: dominik@452: rvi[i].running_cost = runcost; dominik@452: } dominik@452: } break; dominik@452: case 0x0A: /* Running cost base */ dominik@452: /* TODO: I have no idea. --pasky */ dominik@452: FOR_EACH_OBJECT { dominik@452: grf_load_byte(&buf); dominik@452: } dominik@452: ret = true; dominik@452: break; dominik@452: case 0x0E: { /* Sprite ID */ dominik@452: FOR_EACH_OBJECT { dominik@452: uint8 spriteid = grf_load_byte(&buf); dominik@452: dominik@452: if (spriteid == 0xFF) dominik@452: spriteid = 0xFD; // cars have different custom id in the GRF file dominik@452: dominik@452: // This is currently not used but there's no reason dominik@452: // in not having it here for the future. dominik@452: if (spriteid == 0xFD && rvi[i].image_index != 0xFD) dominik@452: _engine_original_sprites[ROAD_ENGINES_INDEX + engine + i] = rvi[i].image_index; dominik@452: dominik@452: rvi[i].image_index = spriteid; dominik@452: } dominik@452: } break; dominik@452: case 0x0F: { /* Cargo capacity */ dominik@452: FOR_EACH_OBJECT { dominik@452: uint16 capacity = grf_load_word(&buf); dominik@452: dominik@452: rvi[i].capacity = capacity; dominik@452: } dominik@452: } break; dominik@452: case 0x10: { /* Cargo type */ dominik@452: FOR_EACH_OBJECT { dominik@452: uint8 cargo = grf_load_byte(&buf); dominik@452: dominik@452: rvi[i].cargo_type = cargo; dominik@452: } dominik@452: } break; dominik@452: case 0x11: { /* Cost factor */ dominik@452: FOR_EACH_OBJECT { dominik@452: uint8 cost_factor = grf_load_byte(&buf); dominik@452: dominik@452: rvi[i].base_cost = cost_factor; // ?? is it base_cost? dominik@452: } dominik@452: } break; dominik@452: case 0x12: { /* SFX */ dominik@452: FOR_EACH_OBJECT { dominik@452: uint8 sfx = grf_load_byte(&buf); dominik@452: dominik@452: rvi[i].sfx = sfx; dominik@452: } dominik@452: } break; dominik@452: case 0x13: /* Power in 10hp */ dominik@452: case 0x14: /* Weight in 1/4 tons */ dominik@452: case 0x15: /* Speed in mph*0.8 */ dominik@452: /* TODO: Support for road vehicles realistic power dominik@452: * computations (called rvpower in TTDPatch) is just dominik@452: * missing in OTTD yet. --pasky */ dominik@452: FOR_EACH_OBJECT { dominik@452: grf_load_byte(&buf); dominik@452: } dominik@452: ret = true; dominik@452: break; dominik@452: case 0x16: { /* Cargos available for refitting */ dominik@452: FOR_EACH_OBJECT { dominik@452: uint32 refit_mask = grf_load_dword(&buf); dominik@452: dominik@452: _engine_refit_masks[ROAD_ENGINES_INDEX + engine + i] = refit_mask; dominik@452: } dominik@452: } break; dominik@452: case 0x17: /* Callback */ dominik@452: case 0x18: /* Tractive effort */ dominik@452: /* TODO */ dominik@452: FOR_EACH_OBJECT { dominik@452: grf_load_byte(&buf); dominik@452: } dominik@452: ret = true; dominik@452: break; dominik@452: default: dominik@452: ret = true; dominik@452: } dominik@452: dominik@452: *bufp = buf; dominik@452: return ret; dominik@452: } dominik@452: dominik@452: static bool ShipVehicleChangeInfo(uint engine, int numinfo, int prop, byte **bufp, int len) dominik@452: { dominik@452: ShipVehicleInfo *svi = &_ship_vehicle_info[engine]; dominik@452: byte *buf = *bufp; dominik@452: int i; dominik@452: bool ret = false; dominik@452: dominik@452: //printf("e %x prop %x?\n", engine, prop); dominik@452: switch (prop) { dominik@452: case 0x08: { /* Sprite ID */ dominik@452: FOR_EACH_OBJECT { dominik@452: uint8 spriteid = grf_load_byte(&buf); dominik@452: dominik@452: if (spriteid == 0xFF) dominik@452: spriteid = 0xFD; // ships have different custom id in the GRF file dominik@452: dominik@452: // This is currently not used but there's no reason dominik@452: // in not having it here for the future. dominik@452: if (spriteid == 0xFD && svi[i].image_index != 0xFD) dominik@452: _engine_original_sprites[SHIP_ENGINES_INDEX + engine + i] = svi[i].image_index; dominik@452: dominik@452: svi[i].image_index = spriteid; dominik@452: } dominik@452: } break; dominik@452: case 0x09: { /* Refittable */ dominik@452: FOR_EACH_OBJECT { dominik@452: uint8 refittable = grf_load_byte(&buf); dominik@452: dominik@452: svi[i].refittable = refittable; dominik@452: } dominik@452: } break; dominik@452: case 0x0A: { /* Cost factor */ dominik@452: FOR_EACH_OBJECT { dominik@452: uint8 cost_factor = grf_load_byte(&buf); dominik@452: dominik@452: svi[i].base_cost = cost_factor; // ?? is it base_cost? dominik@452: } dominik@452: } break; dominik@452: case 0x0B: { /* Speed */ dominik@452: FOR_EACH_OBJECT { dominik@452: uint8 speed = grf_load_byte(&buf); dominik@452: dominik@452: svi[i].max_speed = speed; // ?? units dominik@452: } dominik@452: } break; dominik@452: case 0x0C: { /* Cargo type */ dominik@452: FOR_EACH_OBJECT { dominik@452: uint8 cargo = grf_load_byte(&buf); dominik@452: dominik@452: // XXX: Need to consult this with patchman yet. dominik@452: #if 0 dominik@452: // Documentation claims this is already the dominik@452: // per-landscape cargo type id, but newships.grf dominik@452: // assume otherwise. dominik@452: cargo = local_cargo_id_ctype[cargo]; dominik@452: #endif dominik@452: svi[i].cargo_type = cargo; dominik@452: } dominik@452: } break; dominik@452: case 0x0D: { /* Cargo capacity */ dominik@452: FOR_EACH_OBJECT { dominik@452: uint16 capacity = grf_load_word(&buf); dominik@452: dominik@452: svi[i].capacity = capacity; dominik@452: } dominik@452: } break; dominik@452: case 0x0F: { /* Running cost factor */ dominik@452: FOR_EACH_OBJECT { dominik@452: uint8 runcost = grf_load_byte(&buf); dominik@452: dominik@452: svi[i].running_cost = runcost; dominik@452: } dominik@452: } break; dominik@452: case 0x10: { /* SFX */ dominik@452: FOR_EACH_OBJECT { dominik@452: uint8 sfx = grf_load_byte(&buf); dominik@452: dominik@452: svi[i].sfx = sfx; dominik@452: } dominik@452: } break; dominik@452: case 0x11: { /* Cargos available for refitting */ dominik@452: FOR_EACH_OBJECT { dominik@452: uint32 refit_mask = grf_load_dword(&buf); dominik@452: dominik@452: _engine_refit_masks[SHIP_ENGINES_INDEX + engine + i] = refit_mask; dominik@452: } dominik@452: } break; dominik@452: case 0x12: { /* Callback TODO */ dominik@452: ret = true; dominik@452: } break; dominik@452: default: dominik@452: ret = true; dominik@452: } dominik@452: dominik@452: *bufp = buf; dominik@452: return ret; dominik@452: } dominik@452: dominik@452: static bool AircraftVehicleChangeInfo(uint engine, int numinfo, int prop, byte **bufp, int len) dominik@452: { dominik@452: AircraftVehicleInfo *avi = &_aircraft_vehicle_info[engine]; dominik@452: byte *buf = *bufp; dominik@452: int i; dominik@452: bool ret = false; dominik@452: dominik@452: //printf("e %x prop %x?\n", engine, prop); dominik@452: switch (prop) { dominik@452: case 0x08: { /* Sprite ID */ dominik@452: FOR_EACH_OBJECT { dominik@452: uint8 spriteid = grf_load_byte(&buf); dominik@452: dominik@452: if (spriteid == 0xFF) dominik@452: spriteid = 0xFD; // ships have different custom id in the GRF file dominik@452: dominik@452: // This is currently not used but there's no reason dominik@452: // in not having it here for the future. dominik@452: if (spriteid == 0xFD && avi[i].image_index != 0xFD) dominik@452: _engine_original_sprites[AIRCRAFT_ENGINES_INDEX + engine + i] = avi[i].image_index; dominik@452: dominik@452: avi[i].image_index = spriteid; dominik@452: } dominik@452: } break; dominik@452: case 0x09: { /* Helicopter */ dominik@452: FOR_EACH_OBJECT { dominik@452: uint8 heli = grf_load_byte(&buf); dominik@452: dominik@452: avi[i].subtype = (heli == 0) ? 0 : 1; dominik@452: } dominik@452: } break; dominik@452: case 0x0A: { /* Large */ dominik@452: FOR_EACH_OBJECT { dominik@452: uint8 large = grf_load_byte(&buf); dominik@452: dominik@452: avi[i].subtype = (large == 1) ? 3 : 1; dominik@452: } dominik@452: } break; dominik@452: case 0x0B: { /* Cost factor */ dominik@452: FOR_EACH_OBJECT { dominik@452: uint8 cost_factor = grf_load_byte(&buf); dominik@452: dominik@452: avi[i].base_cost = cost_factor; // ?? is it base_cost? dominik@452: } dominik@452: } break; dominik@452: case 0x0C: { /* Speed */ dominik@452: FOR_EACH_OBJECT { dominik@452: uint8 speed = grf_load_byte(&buf); dominik@452: dominik@452: avi[i].max_speed = speed; // ?? units dominik@452: } dominik@452: } break; dominik@452: case 0x0D: { /* Acceleration */ dominik@452: FOR_EACH_OBJECT { dominik@452: uint8 accel = grf_load_byte(&buf); dominik@452: dominik@452: avi[i].acceleration = accel; dominik@452: } dominik@452: } break; dominik@452: case 0x0E: { /* Running cost factor */ dominik@452: FOR_EACH_OBJECT { dominik@452: uint8 runcost = grf_load_byte(&buf); dominik@452: dominik@452: avi[i].running_cost = runcost; dominik@452: } dominik@452: } break; dominik@452: case 0x0F: { /* Passenger capacity */ dominik@452: FOR_EACH_OBJECT { dominik@452: uint16 capacity = grf_load_word(&buf); dominik@452: dominik@452: avi[i].passanger_capacity = capacity; dominik@452: } dominik@452: } break; dominik@452: case 0x11: { /* Mail capacity */ dominik@452: FOR_EACH_OBJECT { dominik@452: uint8 capacity = grf_load_byte(&buf); dominik@452: dominik@452: avi[i].mail_capacity = capacity; dominik@452: } dominik@452: } break; dominik@452: case 0x12: { /* SFX */ dominik@452: FOR_EACH_OBJECT { dominik@452: uint8 sfx = grf_load_byte(&buf); dominik@452: dominik@452: avi[i].sfx = sfx; dominik@452: } dominik@452: } break; dominik@452: case 0x13: { /* Cargos available for refitting */ dominik@452: FOR_EACH_OBJECT { dominik@452: uint32 refit_mask = grf_load_dword(&buf); dominik@452: dominik@452: _engine_refit_masks[AIRCRAFT_ENGINES_INDEX + engine + i] = refit_mask; dominik@452: } dominik@452: } break; dominik@452: case 0x14: { /* Callback TODO */ dominik@452: ret = true; dominik@452: } break; dominik@452: default: dominik@452: ret = true; dominik@452: } dominik@452: dominik@452: *bufp = buf; dominik@452: return ret; dominik@452: } dominik@452: dominik@452: static bool StationChangeInfo(uint stid, int numinfo, int prop, byte **bufp, int len) dominik@452: { dominik@452: byte *buf = *bufp; dominik@452: int i; dominik@452: int ret = 0; dominik@452: dominik@452: /* This is one single huge TODO. It doesn't handle anything more than dominik@452: * just waypoints for now. */ dominik@452: dominik@452: //printf("sci %d %d [0x%02x]\n", stid, numinfo, prop); dominik@452: switch (prop) { dominik@452: case 0x08: dominik@452: { /* Class ID */ dominik@452: FOR_EACH_OBJECT { dominik@452: struct StationSpec *stat = &_cur_grffile->stations[stid + i]; dominik@452: uint32 classid; dominik@452: dominik@452: /* classid, for a change, is always little-endian */ dominik@452: classid = *(buf++) << 24; dominik@452: classid |= *(buf++) << 16; dominik@452: classid |= *(buf++) << 8; dominik@452: classid |= *(buf++); dominik@452: dominik@452: switch (classid) { dominik@452: case 'DFLT': dominik@452: stat->sclass = STAT_CLASS_DFLT; dominik@452: break; dominik@452: case 'WAYP': dominik@452: stat->sclass = STAT_CLASS_WAYP; dominik@452: break; dominik@452: default: dominik@452: /* TODO: No support for custom dominik@452: * classes for now, so stuff dominik@452: * everything to the single dominik@452: * default one. --pasky */ dominik@452: stat->sclass = STAT_CLASS_DFLT; dominik@452: //stat->sclass = STAT_CLASS_CUSTOM; dominik@452: break; dominik@452: } dominik@452: } dominik@452: break; dominik@452: } dominik@452: case 0x09: dominik@452: { /* Define sprite layout */ dominik@452: FOR_EACH_OBJECT { dominik@452: struct StationSpec *stat = &_cur_grffile->stations[stid + i]; dominik@452: int t; dominik@452: dominik@452: stat->tiles = grf_load_byte(&buf); dominik@452: for (t = 0; t < stat->tiles; t++) { dominik@452: DrawTileSprites *dts = &stat->renderdata[t]; dominik@452: int seq_count = 0; dominik@452: dominik@452: if (t >= 8) { dominik@452: grfmsg(GMS_WARN, "StationChangeInfo: Sprite %d>=8, skipping.", t); dominik@452: grf_load_dword(&buf); // at least something dominik@452: continue; dominik@452: } dominik@452: dominik@452: dts->ground_sprite = grf_load_dword(&buf); dominik@452: if (!dts->ground_sprite) { tron@536: static const DrawTileSeqStruct empty = {0x80, 0, 0, 0, 0, 0, 0}; dominik@452: dts->seq = ∅ dominik@452: continue; dominik@452: } dominik@452: dominik@452: dts->seq = NULL; dominik@452: while (buf < *bufp + len) { dominik@452: DrawTileSeqStruct *dtss; dominik@452: dominik@452: // no relative bounding box support dominik@452: dts->seq = realloc((void*)dts->seq, ++seq_count * sizeof(DrawTileSeqStruct)); dominik@452: dtss = (DrawTileSeqStruct*) &dts->seq[seq_count - 1]; dominik@452: dominik@452: dtss->delta_x = grf_load_byte(&buf); dominik@452: if ((byte) dtss->delta_x == 0x80) break; dominik@452: dtss->delta_y = grf_load_byte(&buf); dominik@452: dtss->delta_z = grf_load_byte(&buf); dominik@452: dtss->width = grf_load_byte(&buf); dominik@452: dtss->height = grf_load_byte(&buf); dominik@452: dtss->unk = grf_load_byte(&buf); dominik@452: dtss->image = grf_load_dword(&buf) - 0x42d; dominik@452: } dominik@452: } dominik@452: } dominik@452: break; dominik@452: } dominik@452: case 0x0a: dominik@452: { /* Copy sprite layout */ dominik@452: FOR_EACH_OBJECT { dominik@452: struct StationSpec *stat = &_cur_grffile->stations[stid + i]; dominik@452: byte srcid = grf_load_byte(&buf); dominik@452: struct StationSpec *srcstat = &_cur_grffile->stations[srcid]; dominik@452: int t; dominik@452: dominik@452: stat->tiles = srcstat->tiles; dominik@452: for (t = 0; t < stat->tiles; t++) { dominik@452: DrawTileSprites *dts = &stat->renderdata[t]; dominik@452: DrawTileSprites *sdts = &srcstat->renderdata[t]; dominik@452: DrawTileSeqStruct const *sdtss = sdts->seq; dominik@452: int seq_count = 0; dominik@452: dominik@452: dts->ground_sprite = sdts->ground_sprite; dominik@452: if (!dts->ground_sprite) { tron@536: static const DrawTileSeqStruct empty = {0x80, 0, 0, 0, 0, 0, 0}; dominik@452: dts->seq = ∅ dominik@452: continue; dominik@452: } dominik@452: dominik@452: dts->seq = NULL; dominik@452: while (1) { dominik@452: DrawTileSeqStruct *dtss; dominik@452: dominik@452: // no relative bounding box support dominik@452: dts->seq = realloc((void*)dts->seq, ++seq_count * sizeof(DrawTileSeqStruct)); dominik@452: dtss = (DrawTileSeqStruct*) &dts->seq[seq_count - 1]; dominik@452: *dtss = *sdtss; dominik@452: if ((byte) dtss->delta_x == 0x80) break; dominik@452: sdtss++; dominik@452: } dominik@452: } dominik@452: } dominik@452: break; dominik@452: } dominik@452: case 0x0b: dominik@452: { /* Callback */ dominik@452: /* TODO */ dominik@452: FOR_EACH_OBJECT { dominik@452: grf_load_byte(&buf); dominik@452: } dominik@452: ret = 1; dominik@452: break; dominik@452: } dominik@452: case 0x0C: dominik@452: { /* Platforms number */ dominik@452: FOR_EACH_OBJECT { dominik@452: struct StationSpec *stat = &_cur_grffile->stations[stid + i]; dominik@452: dominik@452: stat->allowed_platforms = ~grf_load_byte(&buf); dominik@452: } dominik@452: break; dominik@452: } dominik@452: case 0x0D: dominik@452: { /* Platforms length */ dominik@452: FOR_EACH_OBJECT { dominik@452: struct StationSpec *stat = &_cur_grffile->stations[stid + i]; dominik@452: dominik@452: stat->allowed_lengths = ~grf_load_byte(&buf); dominik@452: } dominik@452: break; dominik@452: } dominik@452: case 0x0e: dominik@452: { /* Define custom layout */ dominik@452: FOR_EACH_OBJECT { dominik@452: struct StationSpec *stat = &_cur_grffile->stations[stid + i]; dominik@452: dominik@452: while (buf < *bufp + len) { dominik@452: byte length = grf_load_byte(&buf); dominik@452: byte number = grf_load_byte(&buf); dominik@452: StationLayout layout; dominik@452: int l, p; dominik@452: dominik@452: if (length == 0 || number == 0) break; dominik@452: dominik@452: //debug("l %d > %d ?", length, stat->lengths); dominik@452: if (length > stat->lengths) { dominik@452: stat->platforms = realloc(stat->platforms, length); dominik@452: memset(stat->platforms + stat->lengths, 0, length - stat->lengths); dominik@452: dominik@452: stat->layouts = realloc(stat->layouts, length * sizeof(*stat->layouts)); dominik@452: memset(stat->layouts + stat->lengths, 0, dominik@452: (length - stat->lengths) * sizeof(*stat->layouts)); dominik@452: dominik@452: stat->lengths = length; dominik@452: } dominik@452: l = length - 1; // index is zero-based dominik@452: dominik@452: //debug("p %d > %d ?", number, stat->platforms[l]); dominik@452: if (number > stat->platforms[l]) { dominik@452: stat->layouts[l] = realloc(stat->layouts[l], dominik@452: number * sizeof(**stat->layouts)); dominik@452: // We expect NULL being 0 here, but C99 guarantees that. dominik@452: memset(stat->layouts[l] + stat->platforms[l], 0, dominik@452: (number - stat->platforms[l]) * sizeof(**stat->layouts)); dominik@452: dominik@452: stat->platforms[l] = number; dominik@452: } dominik@452: truelight@542: p = 0; dominik@452: layout = malloc(length * number); dominik@452: for (l = 0; l < length; l++) dominik@452: for (p = 0; p < number; p++) dominik@452: layout[l * number + p] = grf_load_byte(&buf); dominik@452: dominik@452: l--; dominik@452: p--; truelight@542: assert(p >= 0); dominik@452: free(stat->layouts[l][p]); dominik@452: stat->layouts[l][p] = layout; dominik@452: } dominik@452: } dominik@452: break; dominik@452: } dominik@452: case 0x0f: dominik@452: { /* Copy custom layout */ dominik@452: /* TODO */ dominik@452: FOR_EACH_OBJECT { dominik@452: grf_load_byte(&buf); dominik@452: } dominik@452: ret = 1; dominik@452: break; dominik@452: } dominik@452: case 0x10: dominik@452: { /* Little/lots cargo threshold */ dominik@452: /* TODO */ dominik@452: FOR_EACH_OBJECT { dominik@452: grf_load_word(&buf); dominik@452: } dominik@452: ret = 1; dominik@452: break; dominik@452: } dominik@452: case 0x11: dominik@452: { /* Pylon placement */ dominik@452: /* TODO; makes sense only for electrified tracks */ dominik@452: FOR_EACH_OBJECT { dominik@452: grf_load_word(&buf); dominik@452: } dominik@452: ret = 1; dominik@452: break; dominik@452: } dominik@452: case 0x12: dominik@452: { /* Cargo types for random triggers */ dominik@452: /* TODO */ dominik@452: FOR_EACH_OBJECT { dominik@452: grf_load_dword(&buf); dominik@452: } dominik@452: ret = 1; dominik@452: break; dominik@452: } dominik@452: default: dominik@452: ret = 1; dominik@452: break; dominik@452: } dominik@452: dominik@452: *bufp = buf; dominik@452: return ret; dominik@452: } dominik@452: dominik@452: #undef shift_buf dominik@452: dominik@452: dominik@452: /* Action 0x00 */ dominik@452: static void VehicleChangeInfo(byte *buf, int len) dominik@452: { dominik@452: byte *bufend = buf + len; dominik@452: int i; dominik@452: dominik@452: /* <00> ()... dominik@452: * dominik@452: * B feature 0, 1, 2 or 3 for trains, road vehicles, ships or planes dominik@452: * 4 for defining new train station sets dominik@452: * B num-props how many properties to change per vehicle/station dominik@452: * B num-info how many vehicles/stations to change dominik@452: * B id ID of first vehicle/station to change, if num-info is dominik@452: * greater than one, this one and the following dominik@452: * vehicles/stations will be changed dominik@452: * B property what property to change, depends on the feature dominik@452: * V new-info new bytes of info (variable size; depends on properties) */ dominik@452: /* TODO: Bridges, town houses. */ dominik@452: dominik@452: static const VCI_Handler handler[7] = { dominik@452: /* GSF_TRAIN */ RailVehicleChangeInfo, dominik@452: /* GSF_ROAD */ RoadVehicleChangeInfo, dominik@452: /* GSF_SHIP */ ShipVehicleChangeInfo, dominik@452: /* GSF_AIRCRAFT */ AircraftVehicleChangeInfo, dominik@452: /* GSF_STATION */ StationChangeInfo, dominik@452: /* GSF_BRIDGE */ NULL, dominik@452: /* GSF_TOWNHOUSE */NULL, dominik@452: }; dominik@452: dominik@452: uint8 feature; dominik@452: uint8 numprops; dominik@452: uint8 numinfo; dominik@452: byte engine; truelight@542: EngineInfo *ei = NULL; dominik@452: dominik@452: if (len == 1) { dominik@452: DEBUG(grf, 8) ("Silently ignoring one-byte special sprite 0x00."); dominik@452: return; dominik@452: } dominik@452: dominik@452: check_length(len, 6, "VehicleChangeInfo"); dominik@452: feature = buf[1]; dominik@452: numprops = buf[2]; dominik@452: numinfo = buf[3]; dominik@452: engine = buf[4]; dominik@452: dominik@452: DEBUG(grf, 6) ("VehicleChangeInfo: Feature %d, %d properties, to apply to %d+%d", dominik@452: feature, numprops, engine, numinfo); dominik@452: dominik@452: if (feature > GSF_STATION) { dominik@452: grfmsg(GMS_WARN, "VehicleChangeInfo: Unsupported feature %d, skipping.", feature); dominik@452: return; dominik@452: } dominik@452: dominik@452: if (feature != GSF_STATION) dominik@452: ei = &_engine_info[engine + _vehshifts[feature]]; truelight@542: /* XXX - Should there not be a check to see if 'ei' is NULL truelight@542: when it is used in the switch below?? -- TrueLight */ dominik@452: dominik@452: buf += 5; dominik@452: dominik@452: while (numprops-- && buf < bufend) { dominik@452: uint8 prop = grf_load_byte(&buf); dominik@452: dominik@452: if (feature == GSF_STATION) dominik@452: // stations don't share those common properties dominik@452: goto run_handler; dominik@452: dominik@452: switch (prop) { dominik@452: case 0x00: { /* Introduction date */ dominik@452: FOR_EACH_OBJECT { dominik@452: uint16 date = grf_load_word(&buf); dominik@452: dominik@452: ei[i].base_intro = date; dominik@452: } dominik@452: } break; dominik@452: case 0x02: { /* Decay speed */ dominik@452: FOR_EACH_OBJECT { dominik@452: uint8 decay = grf_load_byte(&buf); dominik@452: dominik@452: ei[i].unk2 &= 0x80; dominik@452: ei[i].unk2 |= decay & 0x7f; dominik@452: } dominik@452: } break; dominik@452: case 0x03: { /* Vehicle life */ dominik@452: FOR_EACH_OBJECT { dominik@452: uint8 life = grf_load_byte(&buf); dominik@452: dominik@452: ei[i].lifelength = life; dominik@452: } dominik@452: } break; dominik@452: case 0x04: { /* Model life */ dominik@452: FOR_EACH_OBJECT { dominik@452: uint8 life = grf_load_byte(&buf); dominik@452: dominik@452: ei[i].base_life = life; dominik@452: } dominik@452: } break; dominik@452: case 0x06: { /* Climates available */ dominik@452: FOR_EACH_OBJECT { dominik@452: uint8 climates = grf_load_byte(&buf); dominik@452: dominik@452: ei[i].railtype_climates &= 0xf0; dominik@452: ei[i].railtype_climates |= climates; dominik@452: } dominik@452: } break; dominik@452: case 0x07: { /* Loading speed */ dominik@452: /* TODO */ dominik@452: /* Hyronymus explained me what does dominik@452: * this mean and insists on having a dominik@452: * credit ;-). --pasky */ dominik@452: /* TODO: This needs to be supported by dominik@452: * LoadUnloadVehicle() first. */ dominik@452: FOR_EACH_OBJECT { dominik@452: grf_load_byte(&buf); dominik@452: } dominik@452: goto ignoring; dominik@452: } dominik@452: default: { dominik@452: run_handler: dominik@452: if (handler[feature](engine, numinfo, prop, &buf, bufend - buf)) { dominik@452: ignoring: dominik@452: grfmsg(GMS_NOTICE, "VehicleChangeInfo: Ignoring property %x (not implemented).", prop); dominik@452: } dominik@452: break; dominik@452: } dominik@452: } dominik@452: } dominik@452: #undef shift_buf dominik@452: } dominik@452: dominik@452: #undef FOR_EACH_OBJECT dominik@452: dominik@452: dominik@452: /* Action 0x01 */ dominik@452: static void NewSpriteSet(byte *buf, int len) dominik@452: { dominik@452: /* <01> dominik@452: * dominik@452: * B feature feature to define sprites for dominik@452: * 0, 1, 2, 3: veh-type, 4: train stations dominik@452: * B num-sets number of sprite sets dominik@452: * B num-ent how many entries per sprite set dominik@452: * For vehicles, this is the number of different dominik@452: * vehicle directions in each sprite set dominik@452: * Set num-dirs=8, unless your sprites are symmetric. dominik@452: * In that case, use num-dirs=4. dominik@452: * For stations, must be 12 (hex) for the eighteen dominik@452: * different sprites that make up a station */ dominik@452: /* TODO: No stations support. */ dominik@452: uint8 feature; dominik@452: dominik@452: check_length(len, 4, "NewSpriteSet"); dominik@452: feature = buf[1]; dominik@452: dominik@452: _cur_grffile->spriteset_start = _cur_spriteid + 1; dominik@452: _cur_grffile->spriteset_feature = feature; dominik@452: _cur_grffile->spriteset_numsets = buf[2]; dominik@452: _cur_grffile->spriteset_numents = buf[3]; dominik@452: } dominik@452: dominik@452: /* Action 0x02 */ dominik@452: static void NewSpriteGroup(byte *buf, int len) dominik@452: { dominik@452: byte *bufend = buf + len; dominik@452: dominik@452: /* <02> dominik@452: * dominik@452: * B feature see action 1 dominik@452: * B set-id ID of this particular definition dominik@452: * B type/num-entries dominik@452: * if 80 or greater, this is a randomized or variational dominik@452: * list definition, see below dominik@452: * otherwise it specifies a number of entries, the exact dominik@452: * meaning depends on the feature dominik@452: * V feature-specific-data (huge mess, don't even look it up --pasky) */ dominik@452: /* TODO: No 0x80-types (ugh). */ dominik@452: /* TODO: Also, empty sprites aren't handled for now. Need to investigate dominik@452: * the "opacity" rules for these, that is which sprite to fall back to dominik@452: * when. --pasky */ dominik@452: uint8 feature; dominik@452: uint8 setid; dominik@452: /* XXX: For stations, these two are "little cargo" and "lotsa cargo" sets. */ dominik@452: uint8 numloaded; dominik@452: uint8 numloading; dominik@452: struct SpriteGroup *group; dominik@452: struct RealSpriteGroup *rg; dominik@452: byte *loaded_ptr; dominik@452: byte *loading_ptr; dominik@452: int i; dominik@452: dominik@452: check_length(len, 5, "NewSpriteGroup"); dominik@452: feature = buf[1]; dominik@452: setid = buf[2]; dominik@452: numloaded = buf[3]; dominik@452: numloading = buf[4]; dominik@452: dominik@452: if (numloaded == 0x81 || numloaded == 0x82) { dominik@452: struct DeterministicSpriteGroup *dg; dominik@452: uint16 groupid; dominik@452: int i; dominik@452: dominik@452: // Ok, this is gonna get a little wild, so hold your breath... dominik@452: dominik@452: /* This stuff is getting actually evaluated in dominik@452: * EvalDeterministicSpriteGroup(). */ dominik@452: dominik@452: buf += 4; len -= 4; dominik@452: check_length(len, 6, "NewSpriteGroup 0x81/0x82"); dominik@452: dominik@452: if (setid >= _cur_grffile->spritegroups_count) { dominik@452: _cur_grffile->spritegroups_count = setid + 1; dominik@452: _cur_grffile->spritegroups = realloc(_cur_grffile->spritegroups, _cur_grffile->spritegroups_count * sizeof(struct SpriteGroup)); dominik@452: } dominik@452: dominik@452: group = &_cur_grffile->spritegroups[setid]; dominik@452: memset(group, 0, sizeof(struct SpriteGroup)); dominik@452: group->type = SGT_DETERMINISTIC; dominik@452: dg = &group->g.determ; dominik@452: dominik@452: /* XXX: We don't free() anything, assuming that if there was dominik@452: * some action here before, it got associated by action 3. dominik@452: * We should perhaps keep some refcount? --pasky */ dominik@452: dominik@452: dg->var_scope = numloaded == 0x82 ? VSG_SCOPE_PARENT : VSG_SCOPE_SELF; dominik@452: dg->variable = grf_load_byte(&buf); dominik@452: dominik@452: dg->shift_num = grf_load_byte(&buf); dominik@452: dg->and_mask = grf_load_byte(&buf); dominik@452: dg->operation = dg->shift_num >> 6; /* w00t */ dominik@452: dg->shift_num &= 0x3F; dominik@452: if (dg->operation != DSG_OP_NONE) { dominik@452: dg->add_val = grf_load_byte(&buf); dominik@452: dg->divmod_val = grf_load_byte(&buf); dominik@452: } dominik@452: dominik@452: /* (groupid & 0x8000) means this is callback result; we happily dominik@452: * ignore that for now. */ dominik@452: dominik@452: dg->num_ranges = grf_load_byte(&buf); dominik@452: dg->ranges = calloc(dg->num_ranges, sizeof(*dg->ranges)); dominik@452: for (i = 0; i < dg->num_ranges; i++) { dominik@452: groupid = grf_load_word(&buf); dominik@452: if (groupid & 0x8000 || groupid >= _cur_grffile->spritegroups_count) { dominik@452: /* This doesn't exist for us. */ dominik@452: grf_load_word(&buf); // skip range dominik@452: i--; dg->num_ranges--; dominik@452: continue; dominik@452: } dominik@452: /* XXX: If multiple surreal sets attach a surreal dominik@452: * set this way, we are in trouble. */ dominik@452: dg->ranges[i].group = _cur_grffile->spritegroups[groupid]; dominik@452: dg->ranges[i].low = grf_load_byte(&buf); dominik@452: dg->ranges[i].high = grf_load_byte(&buf); dominik@452: } dominik@452: dominik@452: groupid = grf_load_word(&buf); dominik@452: if (groupid & 0x8000 || groupid >= _cur_grffile->spritegroups_count) { dominik@452: /* This spritegroup stinks. */ dominik@452: free(dg->ranges), dg->ranges = NULL; dominik@452: grfmsg(GMS_WARN, "NewSpriteGroup(%02x:0x%x): Default groupid %04x is cargo callback or unknown, ignoring spritegroup.", setid, numloaded, groupid); dominik@452: return; dominik@452: } dominik@452: dominik@452: dg->default_group = malloc(sizeof(*dg->default_group)); dominik@452: memcpy(dg->default_group, &_cur_grffile->spritegroups[groupid], sizeof(*dg->default_group)); dominik@452: dominik@452: return; dominik@452: dominik@452: } else if (numloaded == 0x80 || numloaded == 0x83) { dominik@452: struct RandomizedSpriteGroup *rg; dominik@452: int i; dominik@452: dominik@452: /* This stuff is getting actually evaluated in dominik@452: * EvalRandomizedSpriteGroup(). */ dominik@452: dominik@452: buf += 4; dominik@452: len -= 4; dominik@452: check_length(len, 6, "NewSpriteGroup 0x80/0x83"); dominik@452: dominik@452: if (setid >= _cur_grffile->spritegroups_count) { dominik@452: _cur_grffile->spritegroups_count = setid + 1; dominik@452: _cur_grffile->spritegroups = realloc(_cur_grffile->spritegroups, _cur_grffile->spritegroups_count * sizeof(struct SpriteGroup)); dominik@452: } dominik@452: dominik@452: group = &_cur_grffile->spritegroups[setid]; dominik@452: memset(group, 0, sizeof(*group)); dominik@452: group->type = SGT_RANDOMIZED; dominik@452: rg = &group->g.random; dominik@452: dominik@452: /* XXX: We don't free() anything, assuming that if there was dominik@452: * some action here before, it got associated by action 3. dominik@452: * We should perhaps keep some refcount? --pasky */ dominik@452: dominik@452: rg->var_scope = numloaded == 0x83 ? VSG_SCOPE_PARENT : VSG_SCOPE_SELF; dominik@452: dominik@452: rg->triggers = grf_load_byte(&buf); dominik@452: rg->cmp_mode = rg->triggers & 0x80; dominik@452: rg->triggers &= 0x7F; dominik@452: dominik@452: rg->lowest_randbit = grf_load_byte(&buf); dominik@452: rg->num_groups = grf_load_byte(&buf); dominik@452: dominik@452: rg->groups = calloc(rg->num_groups, sizeof(*rg->groups)); dominik@452: for (i = 0; i < rg->num_groups; i++) { dominik@452: uint16 groupid = grf_load_word(&buf); dominik@452: dominik@452: if (groupid & 0x8000 || groupid >= _cur_grffile->spritegroups_count) { dominik@452: /* This doesn't exist for us. */ dominik@452: i--; dominik@452: rg->num_groups--; dominik@452: continue; dominik@452: } dominik@452: /* XXX: If multiple surreal sets attach a surreal dominik@452: * set this way, we are in trouble. */ dominik@452: rg->groups[i] = _cur_grffile->spritegroups[groupid]; dominik@452: } dominik@452: dominik@452: return; dominik@452: } dominik@452: dominik@452: if (!_cur_grffile->spriteset_start) { dominik@452: grfmsg(GMS_ERROR, "NewSpriteGroup: No sprite set to work on! Skipping."); dominik@452: return; dominik@452: } dominik@452: dominik@452: if (_cur_grffile->spriteset_feature != feature) { dominik@452: 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); dominik@452: // return; // XXX: we can't because of MB's newstats.grf --pasky dominik@452: } dominik@452: dominik@452: check_length(bufend - buf, 5, "NewSpriteGroup"); dominik@452: buf += 5; dominik@452: check_length(bufend - buf, 2 * numloaded, "NewSpriteGroup"); dominik@452: loaded_ptr = buf; dominik@452: loading_ptr = buf + 2 * numloaded; dominik@452: dominik@452: if (numloaded > 16) { dominik@452: grfmsg(GMS_WARN, "NewSpriteGroup: More than 16 sprites in group %x, skipping the rest.", setid); dominik@452: numloaded = 16; dominik@452: } dominik@452: if (numloading > 16) { dominik@452: grfmsg(GMS_WARN, "NewSpriteGroup: More than 16 sprites in group %x, skipping the rest.", setid); dominik@452: numloading = 16; dominik@452: } dominik@452: dominik@452: if (setid >= _cur_grffile->spritegroups_count) { dominik@452: _cur_grffile->spritegroups_count = setid + 1; dominik@452: _cur_grffile->spritegroups = realloc(_cur_grffile->spritegroups, _cur_grffile->spritegroups_count * sizeof(struct SpriteGroup)); dominik@452: } dominik@452: group = &_cur_grffile->spritegroups[setid]; dominik@452: memset(group, 0, sizeof(struct SpriteGroup)); dominik@452: group->type = SGT_REAL; dominik@452: rg = &group->g.real; dominik@452: dominik@452: rg->sprites_per_set = _cur_grffile->spriteset_numents; dominik@452: rg->loaded_count = numloaded; dominik@452: rg->loading_count = numloading; dominik@452: dominik@452: DEBUG(grf, 6) ("NewSpriteGroup: New SpriteGroup 0x%02hhx, %u views, %u loaded, %u loading, sprites %u - %u", dominik@452: setid, rg->sprites_per_set, rg->loaded_count, rg->loading_count, dominik@452: _cur_grffile->spriteset_start - _cur_grffile->sprite_offset, dominik@452: _cur_grffile->spriteset_start + (_cur_grffile->spriteset_numents * (numloaded + numloading)) - _cur_grffile->sprite_offset); dominik@452: dominik@452: for (i = 0; i < numloaded; i++) { dominik@452: uint16 spriteset_id = grf_load_word(&loaded_ptr); dominik@452: rg->loaded[i] = _cur_grffile->spriteset_start + spriteset_id * _cur_grffile->spriteset_numents; dominik@452: DEBUG(grf, 8) ("NewSpriteGroup: + rg->loaded[%i] = %u (subset %u)", i, rg->loaded[i], spriteset_id); dominik@452: } dominik@452: dominik@452: for (i = 0; i < numloading; i++) { dominik@452: uint16 spriteset_id = grf_load_word(&loading_ptr); dominik@452: rg->loading[i] = _cur_grffile->spriteset_start + spriteset_id * _cur_grffile->spriteset_numents; dominik@452: DEBUG(grf, 8) ("NewSpriteGroup: + rg->loading[%i] = %u (subset %u)", i, rg->loading[i], spriteset_id); dominik@452: } dominik@452: } dominik@452: dominik@452: /* Action 0x03 */ dominik@452: static void NewVehicle_SpriteGroupMapping(byte *buf, int len) dominik@452: { dominik@452: /* <03> ... [ ]... dominik@452: * id-list := [] [id-list] dominik@452: * cargo-list := [cargo-list] dominik@452: * dominik@452: * B feature see action 0 dominik@452: * B n-id bits 0-6: how many IDs this definition applies to dominik@452: * bit 7: if set, this is a wagon override definition (see below) dominik@452: * B ids the IDs for which this definition applies dominik@452: * B num-cid number of cargo IDs (sprite group IDs) in this definition dominik@452: * can be zero, in that case the def-cid is used always dominik@452: * B cargo-type type of this cargo type (e.g. mail=2, wood=7, see below) dominik@452: * W cid cargo ID (sprite group ID) for this type of cargo dominik@452: * W def-cid default cargo ID (sprite group ID) */ dominik@452: /* TODO: Bridges, town houses. */ dominik@452: /* TODO: Multiple cargo support could be useful even for trains/cars - dominik@452: * cargo id 0xff is used for showing images in the build train list. */ dominik@452: dominik@452: static byte *last_engines; dominik@452: static int last_engines_count; dominik@452: uint8 feature; dominik@452: uint8 idcount; dominik@452: bool wagover; dominik@452: uint8 cidcount; dominik@452: int c, i; dominik@452: dominik@452: check_length(len, 7, "VehicleMapSpriteGroup"); dominik@452: feature = buf[1]; dominik@452: idcount = buf[2] & 0x7F; dominik@452: wagover = (buf[2] & 0x80) == 0x80; dominik@452: check_length(len, 3 + idcount, "VehicleMapSpriteGroup"); dominik@452: cidcount = buf[3 + idcount]; dominik@452: check_length(len, 4 + idcount + cidcount * 3, "VehicleMapSpriteGroup"); dominik@452: dominik@452: DEBUG(grf, 6) ("VehicleMapSpriteGroup: Feature %d, %d ids, %d cids, wagon override %d.", dominik@452: feature, idcount, cidcount, wagover); dominik@452: dominik@452: if (feature > GSF_STATION) { dominik@452: grfmsg(GMS_WARN, "VehicleMapSpriteGroup: Unsupported feature %d, skipping.", feature); dominik@452: return; dominik@452: } dominik@452: dominik@452: dominik@452: if (feature == GSF_STATION) { dominik@452: // We do things differently for stations. dominik@452: dominik@452: for (i = 0; i < idcount; i++) { dominik@452: uint8 stid = buf[3 + i]; dominik@452: struct StationSpec *stat = &_cur_grffile->stations[stid]; dominik@452: byte *bp = &buf[4 + idcount]; dominik@452: dominik@452: for (c = 0; c < cidcount; c++) { dominik@452: uint8 ctype = grf_load_byte(&bp); dominik@452: uint16 groupid = grf_load_word(&bp); dominik@452: dominik@452: if (groupid >= _cur_grffile->spritegroups_count) { dominik@452: grfmsg(GMS_WARN, "VehicleMapSpriteGroup: Spriteset %x out of range %x, skipping.", dominik@452: groupid, _cur_grffile->spritegroups_count); dominik@452: return; dominik@452: } dominik@452: dominik@452: if (ctype != 0xFF) { dominik@452: /* TODO: No support for any other cargo. */ dominik@452: continue; dominik@452: } dominik@452: dominik@452: stat->spritegroup[1] = _cur_grffile->spritegroups[groupid]; dominik@452: } dominik@452: } dominik@452: dominik@452: { dominik@452: byte *bp = buf + 4 + idcount + cidcount * 3; dominik@452: uint16 groupid = grf_load_word(&bp); dominik@452: dominik@452: if (groupid >= _cur_grffile->spritegroups_count) { dominik@452: grfmsg(GMS_WARN, "VehicleMapSpriteGroup: Spriteset %x out of range %x, skipping.", dominik@452: groupid, _cur_grffile->spritegroups_count); dominik@452: return; dominik@452: } dominik@452: dominik@452: for (i = 0; i < idcount; i++) { dominik@452: uint8 stid = buf[3 + i]; dominik@452: struct StationSpec *stat = &_cur_grffile->stations[stid]; dominik@452: dominik@452: stat->spritegroup[0] = _cur_grffile->spritegroups[groupid]; dominik@452: stat->grfid = _cur_grffile->grfid; dominik@452: SetCustomStation(stid, stat); dominik@452: stat->sclass = STAT_CLASS_NONE; dominik@452: } dominik@452: } dominik@452: return; dominik@452: } dominik@452: dominik@452: dominik@452: /* If ``n-id'' (or ``idcount'') is zero, this is a ``feature dominik@452: * callback''. I have no idea how this works, so we will ignore it for dominik@452: * now. --octo */ dominik@452: if (idcount == 0) { dominik@452: grfmsg(GMS_NOTICE, "NewMapping: Feature callbacks not implemented yet."); dominik@452: return; dominik@452: } dominik@452: dominik@452: // FIXME: Tropicset contains things like: dominik@452: // 03 00 01 19 01 00 00 00 00 - this is missing one 00 at the end, dominik@452: // what should we exactly do with that? --pasky dominik@452: dominik@452: if (!_cur_grffile->spriteset_start || !_cur_grffile->spritegroups) { dominik@452: grfmsg(GMS_WARN, "VehicleMapSpriteGroup: No sprite set to work on! Skipping."); dominik@452: return; dominik@452: } dominik@452: dominik@452: if (!wagover && last_engines_count != idcount) { dominik@452: last_engines = realloc(last_engines, idcount); dominik@452: last_engines_count = idcount; dominik@452: } dominik@452: dominik@452: if (wagover) { dominik@452: if (last_engines_count == 0) { dominik@452: grfmsg(GMS_ERROR, "VehicleMapSpriteGroup: WagonOverride: No engine to do override with."); dominik@452: return; dominik@452: } dominik@452: DEBUG(grf, 6) ("VehicleMapSpriteGroup: WagonOverride: %u engines, %u wagons.", dominik@452: last_engines_count, idcount); dominik@452: } dominik@452: dominik@452: dominik@452: for (i = 0; i < idcount; i++) { dominik@452: uint8 engine_id = buf[3 + i]; dominik@452: uint8 engine = engine_id + _vehshifts[feature]; dominik@452: byte *bp = &buf[4 + idcount]; dominik@452: dominik@452: if (engine_id > _vehcounts[feature]) { dominik@452: grfmsg(GMS_ERROR, "Id %u for feature %x is out of bounds.", dominik@452: engine_id, feature); dominik@452: return; dominik@452: } dominik@452: dominik@452: DEBUG(grf, 7) ("VehicleMapSpriteGroup: [%d] Engine %d...", i, engine); dominik@452: dominik@452: for (c = 0; c < cidcount; c++) { dominik@452: uint8 ctype = grf_load_byte(&bp); dominik@452: uint16 groupid = grf_load_word(&bp); dominik@452: dominik@452: DEBUG(grf, 8) ("VehicleMapSpriteGroup: * [%d] Cargo type %x, group id %x", c, ctype, groupid); dominik@452: dominik@452: if (groupid >= _cur_grffile->spritegroups_count) { dominik@452: grfmsg(GMS_WARN, "VehicleMapSpriteGroup: Spriteset %x out of range %x, skipping.", groupid, _cur_grffile->spritegroups_count); dominik@452: return; dominik@452: } dominik@452: dominik@452: if (ctype == 0xFF) dominik@452: ctype = CID_PURCHASE; dominik@452: dominik@452: if (wagover) { dominik@452: // TODO: No multiple cargo types per vehicle yet. --pasky dominik@452: SetWagonOverrideSprites(engine, &_cur_grffile->spritegroups[groupid], last_engines, last_engines_count); dominik@452: } else { dominik@452: SetCustomEngineSprites(engine, ctype, &_cur_grffile->spritegroups[groupid]); dominik@452: last_engines[i] = engine; dominik@452: } dominik@452: } dominik@452: } dominik@452: dominik@452: { dominik@452: byte *bp = buf + 4 + idcount + cidcount * 3; dominik@452: uint16 groupid = grf_load_word(&bp); dominik@452: dominik@452: DEBUG(grf, 8) ("-- Default group id %x", groupid); dominik@452: dominik@452: for (i = 0; i < idcount; i++) { dominik@452: uint8 engine = buf[3 + i] + _vehshifts[feature]; dominik@452: dominik@452: // Don't tell me you don't love duplicated code! dominik@452: if (groupid >= _cur_grffile->spritegroups_count) { dominik@452: grfmsg(GMS_WARN, "VehicleMapSpriteGroup: Spriteset %x out of range %x, skipping.", groupid, _cur_grffile->spritegroups_count); dominik@452: return; dominik@452: } dominik@452: dominik@452: if (wagover) { dominik@452: // TODO: No multiple cargo types per vehicle yet. --pasky dominik@452: SetWagonOverrideSprites(engine, &_cur_grffile->spritegroups[groupid], last_engines, last_engines_count); dominik@452: } else { dominik@452: SetCustomEngineSprites(engine, CID_DEFAULT, &_cur_grffile->spritegroups[groupid]); dominik@452: last_engines[i] = engine; dominik@452: } dominik@452: } dominik@452: } dominik@452: } dominik@452: dominik@452: /* Action 0x04 */ dominik@452: static void VehicleNewName(byte *buf, int len) dominik@452: { dominik@452: /* <04> dominik@452: * dominik@452: * B veh-type see action 0 dominik@452: * B language-id language ID with bit 7 cleared (see below) dominik@452: * B num-veh number of vehicles which are getting a new name dominik@452: * B offset number of the first vehicle that gets a new name dominik@452: * S data new texts, each of them zero-terminated, after dominik@452: * which the next name begins. */ dominik@452: /* TODO: No support for changing non-vehicle text. Perhaps we shouldn't dominik@452: * implement it at all, but it could be useful for some "modpacks" dominik@452: * (completely new scenarios changing all graphics and logically also dominik@452: * factory names etc). We should then also support all languages (by dominik@452: * name), not only the original four ones. --pasky */ dominik@452: /* TODO: Support for custom station class/type names. */ dominik@452: dominik@452: uint8 feature; dominik@452: uint8 lang; dominik@452: uint8 id; dominik@452: uint8 endid; dominik@452: dominik@452: check_length(len, 6, "VehicleNewName"); dominik@452: feature = buf[1]; dominik@452: lang = buf[2]; dominik@452: id = buf[4] + _vehshifts[feature]; dominik@452: endid = id + buf[3]; dominik@452: dominik@452: DEBUG(grf, 6) ("VehicleNewName: About to rename engines %d..%d (feature %d) in language 0x%x.", dominik@452: id, endid, feature, lang); dominik@452: dominik@452: if (lang & 0x80) { dominik@452: grfmsg(GMS_WARN, "VehicleNewName: No support for changing in-game texts. Skipping."); dominik@452: return; dominik@452: } dominik@452: dominik@452: if (!(lang & 3)) { dominik@452: /* XXX: If non-English name, silently skip it. */ dominik@452: DEBUG(grf, 7) ("VehicleNewName: Skipping non-English name."); dominik@452: return; dominik@452: } dominik@452: dominik@452: buf += 5, len -= 5; dominik@452: for (; id < endid && len > 0; id++) { dominik@452: int ofs = strlen(buf) + 1; dominik@452: dominik@452: if (ofs < 128) { dominik@452: DEBUG(grf, 8) ("VehicleNewName: %d <- %s", id, buf); dominik@452: SetCustomEngineName(id, buf); dominik@452: } else { dominik@452: DEBUG(grf, 7) ("VehicleNewName: Too long a name (%d)", ofs); dominik@452: } dominik@452: buf += ofs, len -= ofs; dominik@452: } dominik@452: } dominik@452: dominik@452: /* Action 0x05 */ dominik@452: static void GraphicsNew(byte *buf, int len) dominik@452: { dominik@452: /* <05> dominik@452: * dominik@452: * B graphics-type What set of graphics the sprites define. dominik@452: * B num-sprites How many sprites are in this set? dominik@452: * V other data Graphics type specific data. Currently unused. */ dominik@452: /* TODO */ dominik@452: dominik@452: uint8 type; dominik@452: uint8 num; dominik@452: dominik@452: check_length(len, 2, "GraphicsNew"); dominik@452: type = buf[0]; dominik@452: num = buf[1]; dominik@452: dominik@452: grfmsg(GMS_NOTICE, "GraphicsNew: Custom graphics (type %x) sprite block of length %d (unimplemented, ignoring).\n", dominik@452: type, num); dominik@452: } dominik@452: dominik@452: /* Action 0x06 */ dominik@452: static void CfgApply(byte *buf, int len) dominik@452: { dominik@452: /* <06> ... dominik@452: * dominik@452: * B param-num Number of parameter to substitute (First = "zero") dominik@452: * Ignored if that parameter was not specified in newgrf.cfg dominik@452: * B param-size How many bytes to replace. If larger than 4, the dominik@452: * bytes of the following parameter are used. In that dominik@452: * case, nothing is applied unless *all* parameters dominik@452: * were specified. dominik@452: * B offset Offset into data from beginning of next sprite dominik@452: * to place where parameter is to be stored. */ dominik@452: /* TODO */ dominik@452: grfmsg(GMS_NOTICE, "CfgApply: Ignoring (not implemented).\n"); dominik@452: } dominik@452: dominik@452: /* Action 0x07 */ dominik@452: /* Action 0x09 */ dominik@452: static void SkipIf(byte *buf, int len) dominik@452: { dominik@452: /* <07/09> dominik@452: * dominik@452: * B param-num dominik@452: * B param-size dominik@452: * B condition-type dominik@452: * V value dominik@452: * B num-sprites */ dominik@452: /* TODO: More params. More condition types. */ dominik@452: uint8 param; dominik@452: uint8 paramsize; dominik@452: uint8 condtype; dominik@452: uint8 numsprites; dominik@452: int param_val = 0, cond_val = 0; dominik@452: bool result; dominik@452: dominik@452: check_length(len, 6, "SkipIf"); dominik@452: param = buf[1]; dominik@452: paramsize = buf[2]; dominik@452: condtype = buf[3]; dominik@452: dominik@452: if (condtype < 2) { dominik@452: /* Always 1 for bit tests, the given value should dominik@452: * be ignored. */ dominik@452: paramsize = 1; dominik@452: } dominik@452: dominik@452: buf += 4; dominik@452: if (paramsize == 4) dominik@452: cond_val = grf_load_dword(&buf); dominik@452: else if (paramsize == 2) dominik@452: cond_val = grf_load_word(&buf); dominik@452: else if (paramsize == 1) dominik@452: cond_val = grf_load_byte(&buf); dominik@452: dominik@452: switch (param) { dominik@452: case 0x83: /* current climate, 0=temp, 1=arctic, 2=trop, 3=toyland */ dominik@452: param_val = _opt.landscape; dominik@452: break; dominik@452: case 0x84: /* .grf loading stage, 0=initialization, 1=activation */ dominik@452: param_val = _cur_stage; dominik@452: break; dominik@452: case 0x85: /* TTDPatch flags, only for bit tests */ dominik@452: param_val = _ttdpatch_flags[cond_val / 0x20]; dominik@452: cond_val %= 0x20; dominik@452: break; dominik@452: case 0x86: /* road traffic side, bit 4 clear=left, set=right */ dominik@452: param_val = _opt.road_side << 4; dominik@452: break; dominik@452: case 0x88: { /* see if specified GRFID is active */ dominik@452: struct GRFFile *file; dominik@452: dominik@452: file = GetFileByGRFID(cond_val); dominik@452: param_val = (file != NULL); dominik@452: } break; dominik@452: case 0x8B: /* TTDPatch version */ dominik@452: param_val = 0xFFFF; dominik@452: break; dominik@452: case 0x8D: /* TTD Version, 00=DOS, 01=Windows */ dominik@452: param_val = 1; dominik@452: break; dominik@452: case 0x8E: dominik@452: param_val = _traininfo_vehicle_pitch; dominik@452: break; dominik@452: /* TODO */ dominik@452: case 0x8F: /* Track type cost multipliers */ dominik@452: default: dominik@452: if (param >= 0x80) { dominik@452: /* In-game variable. */ dominik@452: grfmsg(GMS_WARN, "Unsupported in-game variable %x. Ignoring test.", param); dominik@452: } else { dominik@452: /* Parameter. */ dominik@452: param_val = _paramlist[param]; dominik@452: } dominik@452: return; dominik@452: } dominik@452: dominik@452: switch (condtype) { dominik@452: case 0: result = (param_val & (1 << cond_val)); dominik@452: break; dominik@452: case 1: result = !(param_val & (1 << cond_val)); dominik@452: break; dominik@452: /* TODO: For the following, make it to work with paramsize>1. */ dominik@452: case 2: result = (param_val == cond_val); dominik@452: break; dominik@452: case 3: result = (param_val != cond_val); dominik@452: break; dominik@452: case 4: result = (param_val < cond_val); dominik@452: break; dominik@452: case 5: result = (param_val > cond_val); dominik@452: break; dominik@452: case 6: result = param_val; /* GRFID is active (only for param-num=88) */ dominik@452: break; dominik@452: case 7: result = !param_val; /* GRFID is not active (only for param-num=88) */ dominik@452: break; dominik@452: default: dominik@452: grfmsg(GMS_WARN, "Unsupported test %d. Ignoring.", condtype); dominik@452: return; dominik@452: } dominik@452: dominik@452: if (result == 0) { dominik@452: grfmsg(GMS_NOTICE, "Not skipping sprites, test was false."); dominik@452: return; dominik@452: } dominik@452: dominik@452: numsprites = grf_load_byte(&buf); dominik@452: grfmsg(GMS_NOTICE, "Skipping %d->+%d sprites, test was true.", _cur_spriteid - _custom_sprites_base, numsprites); dominik@452: _skip_sprites = numsprites; dominik@452: if (_skip_sprites == 0) { dominik@452: /* Zero means there are no sprites to skip, so dominik@452: * we use -1 to indicate that all further dominik@452: * sprites should be skipped. */ dominik@452: _skip_sprites = -1; dominik@452: } dominik@452: } dominik@452: dominik@452: static void GRFInfo(byte *buf, int len) dominik@452: { dominik@452: /* <08> dominik@452: * dominik@452: * B version newgrf version, currently 06 dominik@452: * 4*B grf-id globally unique ID of this .grf file dominik@452: * S name name of this .grf set dominik@452: * S info string describing the set, and e.g. author and copyright */ dominik@452: /* TODO: Check version. (We should have own versioning done somehow.) */ dominik@452: uint8 version; dominik@452: uint32 grfid; dominik@452: char *name; dominik@452: char *info; dominik@452: dominik@452: check_length(len, 9, "GRFInfo"); dominik@452: version = buf[1]; dominik@452: /* this is de facto big endian - grf_load_dword() unsuitable */ dominik@452: grfid = buf[2] << 24 | buf[3] << 16 | buf[4] << 8 | buf[5]; dominik@452: name = buf + 6; dominik@452: info = name + strlen(name) + 1; dominik@452: dominik@452: _cur_grffile->grfid = grfid; dominik@452: _cur_grffile->flags |= 0x0001; /* set active flag */ dominik@452: dominik@452: DEBUG(grf, 1) ("[%s] Loaded GRFv%d set %08lx - %s:\n%s\n", dominik@452: _cur_grffile->filename, version, grfid, name, info); dominik@452: } dominik@452: dominik@452: static void SpriteReplace(byte *buf, int len) dominik@452: { dominik@452: /* <0A> [ ...] dominik@452: * : dominik@452: * dominik@452: * B num-sets How many sets of sprites to replace. dominik@452: * Each set: dominik@452: * B num-sprites How many sprites are in this set dominik@452: * W first-sprite First sprite number to replace */ dominik@452: uint8 num_sets; dominik@452: int i; dominik@452: dominik@452: buf++; /* skip action byte */ dominik@452: num_sets = grf_load_byte(&buf); dominik@452: dominik@452: if (num_sets > 16) { dominik@452: grfmsg(GMS_ERROR, "SpriteReplace: Too many sets (%d), taking only the first 16!", num_sets); dominik@452: } dominik@452: dominik@452: for (i = 0; i < 16; i++) { dominik@452: if (i < num_sets) { dominik@452: uint8 num_sprites = grf_load_byte(&buf); dominik@452: uint16 first_sprite = grf_load_word(&buf); dominik@452: dominik@452: _replace_sprites_count[i] = num_sprites; dominik@452: _replace_sprites_offset[i] = first_sprite; dominik@452: grfmsg(GMS_NOTICE, "SpriteReplace: [Set %d] Changing %d sprites, beginning with %d", dominik@452: i, num_sprites, first_sprite); dominik@452: } else { dominik@452: _replace_sprites_count[i] = 0; dominik@452: _replace_sprites_offset[i] = 0; dominik@452: } dominik@452: } dominik@452: } dominik@452: dominik@452: static void GRFError(byte *buf, int len) dominik@452: { dominik@452: /* <0B> [ 00] [] 00 [] dominik@452: * dominik@452: * B severity 00: notice, contine loading grf file dominik@452: * 01: warning, continue loading grf file dominik@452: * 02: error, but continue loading grf file, and attempt dominik@452: * loading grf again when loading or starting next game dominik@452: * 03: error, abort loading and prevent loading again in dominik@452: * the future (only when restarting the patch) dominik@452: * B language-id see action 4, use 1F for built-in error messages dominik@452: * B message-id message to show, see below dominik@452: * S message for custom messages (message-id FF), text of the message dominik@452: * not present for built-in messages. dominik@452: * V data additional data for built-in (or custom) messages dominik@452: * B parnum see action 6, only used with built-in message 03 */ dominik@452: /* TODO: For now we just show the message, sometimes incomplete and never translated. */ dominik@452: dominik@452: static const char * const msgstr[4] = { dominik@452: "Requires at least pseudo-TTDPatch version %s.", dominik@452: "This file is for %s version of TTD.", dominik@452: "Designed to be used with %s.", dominik@452: "Invalid parameter %s.", dominik@452: }; dominik@452: uint8 severity; dominik@452: uint8 msgid; dominik@452: dominik@452: check_length(len, 6, "GRFError"); dominik@452: severity = buf[1]; dominik@452: msgid = buf[3]; dominik@452: dominik@452: // Undocumented TTDPatch feature. dominik@452: if ((severity & 0x80) == 0 && _cur_stage == 0) dominik@452: return; dominik@452: severity &= 0x7F; dominik@452: dominik@452: if (msgid == 0xFF) { dominik@452: grfmsg(severity, "%s", buf+4); dominik@452: } else { dominik@452: grfmsg(severity, msgstr[msgid], buf+4); dominik@452: } dominik@452: } dominik@452: dominik@452: static void GRFComment(byte *buf, int len) dominik@452: { dominik@452: /* <0C> [] dominik@452: * dominik@452: * V ignored Anything following the 0C is ignored */ dominik@452: } dominik@452: dominik@452: /* Action 0x0D */ dominik@452: static void ParamSet(byte *buf, int len) dominik@452: { dominik@452: /* <0D> [] dominik@452: * dominik@452: * B target parameter number where result is stored dominik@452: * B operation operation to perform, see below dominik@452: * B source1 first source operand dominik@452: * B source2 second source operand dominik@452: * D data data to use in the calculation, not necessary dominik@452: * if both source1 and source2 refer to actual parameters dominik@452: * dominik@452: * Operations dominik@452: * 00 Set parameter equal to source1 dominik@452: * 01 Addition, source1 + source2 dominik@452: * 02 Subtraction, source1 - source2 dominik@452: * 03 Unsigned multiplication, source1 * source2 (both unsigned) dominik@452: * 04 Signed multiplication, source1 * source2 (both signed) dominik@452: * 05 Unsigned bit shift, source1 by source2 (source2 taken to be a dominik@452: * signed quantity; left shift if positive and right shift if dominik@452: * negative, source1 is unsigned) dominik@452: * 06 Signed bit shift, source1 by source2 dominik@452: * (source2 like in 05, and source1 as well) dominik@452: */ dominik@452: dominik@452: byte target; dominik@452: byte oper; dominik@452: uint16 src1; dominik@452: uint16 src2; dominik@452: uint16 data = 0; dominik@452: int32 *dest; dominik@452: dominik@452: check_length(len, 5, "ParamSet"); dominik@452: buf++; dominik@452: target = grf_load_byte(&buf); dominik@452: oper = grf_load_byte(&buf); dominik@452: src1 = grf_load_byte(&buf); dominik@452: src2 = grf_load_byte(&buf); dominik@452: dominik@452: if (len >= 8) dominik@452: data = grf_load_dword(&buf); dominik@452: dominik@452: /* You can add 80 to the operation to make it apply only if the target dominik@452: * is not defined yet. In this respect, a parameter is taken to be dominik@452: * defined if any of the following applies: dominik@452: * - it has been set to any value in the newgrf(w).cfg parameter list dominik@452: * - it OR A PARAMETER WITH HIGHER NUMBER has been set to any value by dominik@452: * an earlier action D */ dominik@452: if (oper & 0x80) { dominik@452: if (_param_max < target) dominik@452: oper &= 0x7F; dominik@452: else dominik@452: return; dominik@452: } dominik@452: dominik@452: /* The source1 and source2 operands refer to the grf parameter number dominik@452: * like in action 6 and 7. In addition, they can refer to the special dominik@452: * variables available in action 7, or they can be FF to use the value dominik@452: * of . If referring to parameters that are undefined, a value dominik@452: * of 0 is used instead. */ dominik@452: if (src1 == 0xFF) { dominik@452: src1 = data; dominik@452: } else { dominik@452: src1 = _param_max >= src1 ? _paramlist[src1] : 0; dominik@452: } dominik@452: dominik@452: if (src2 == 0xFF) { dominik@452: src2 = data; dominik@452: } else { dominik@452: src2 = _param_max >= src2 ? _paramlist[src2] : 0; dominik@452: } dominik@452: dominik@452: /* TODO: You can access the parameters of another GRF file by using dominik@452: * source2=FE, source1=the other GRF's parameter number and data=GRF dominik@452: * ID. This is only valid with operation 00 (set). If the GRF ID dominik@452: * cannot be found, a value of 0 is used for the parameter value dominik@452: * instead. */ dominik@452: dominik@452: /* TODO: The target operand can also refer to the special variables dominik@452: * from action 7, but at the moment the only variable that is valid to dominik@452: * write is 8E. */ dominik@452: dominik@452: if (target == 0x8E) { dominik@452: dest = &_traininfo_vehicle_pitch; dominik@452: } else { dominik@452: if (_param_max < target) dominik@452: _param_max = target; dominik@452: dest = &_paramlist[target]; dominik@452: } dominik@452: dominik@452: /* FIXME: No checking for overflows. */ dominik@452: switch (oper) { dominik@452: case 0x00: dominik@452: *dest = src1; dominik@452: break; dominik@452: case 0x01: dominik@452: *dest = src1 + src2; dominik@452: break; dominik@452: case 0x02: dominik@452: *dest = src1 - src2; dominik@452: break; dominik@452: case 0x03: dominik@452: *dest = ((uint32) src1) * ((uint32) src2); dominik@452: break; dominik@452: case 0x04: dominik@452: *dest = ((int32) src1) * ((int32) src2); dominik@452: break; dominik@452: case 0x05: dominik@452: if (src2 & 0x8000) /* src2 is "negative" */ dominik@452: *dest = src1 >> -((int16) src2); dominik@452: else dominik@452: *dest = src1 << src2; dominik@452: break; dominik@452: case 0x06: dominik@452: if (src2 & 0x8000) /* src2 is "negative" */ dominik@452: *dest = ((int16) src1) >> -((int16) src2); dominik@452: else dominik@452: *dest = ((int16) src1) << src2; dominik@452: break; dominik@452: default: dominik@452: grfmsg(GMS_ERROR, "ParamSet: Unknown operation %d, skipping.", oper); dominik@452: } dominik@452: } dominik@452: dominik@452: static void GRFInhibit(byte *buf, int len) dominik@452: { dominik@452: /* <0E> dominik@452: * dominik@452: * B num Number of GRFIDs that follow dominik@452: * D grfids GRFIDs of the files to deactivate */ dominik@452: dominik@452: byte num; dominik@452: int i; dominik@452: dominik@452: check_length(len, 1, "GRFInhibit"); dominik@452: buf++, len--; dominik@452: num = grf_load_byte(&buf); len--; dominik@452: check_length(len, 4 * num, "GRFInhibit"); dominik@452: dominik@452: for (i = 0; i < num; i++) { dominik@452: uint32 grfid = grf_load_dword(&buf); dominik@452: struct GRFFile *file = GetFileByGRFID(grfid); dominik@452: dominik@452: /* Unset activation flag */ dominik@452: if (file != NULL) { dominik@452: grfmsg(GMS_NOTICE, "GRFInhibit: Deactivating file ``%s''", file->filename); dominik@452: file->flags &= 0xFFFE; dominik@452: } dominik@452: } dominik@452: } dominik@452: dominik@452: dominik@452: static void InitializeGRFSpecial(void) dominik@452: { dominik@452: /* FIXME: We should rather reflect reality in _ttdpatch_flags[]. */ dominik@452: dominik@452: _ttdpatch_flags[1] = (1 << 0x08) /* mammothtrains */ dominik@452: | (1 << 0x0B) /* subsidiaries */ dominik@452: | (1 << 0x14) /* bridgespeedlimits */ dominik@452: | (1 << 0x16) /* eternalgame */ dominik@452: | (1 << 0x17) /* newtrains */ dominik@452: | (1 << 0x18) /* newrvs */ dominik@452: | (1 << 0x19) /* newships */ dominik@452: | (1 << 0x1A) /* newplanes */ dominik@452: | (1 << 0x1B); /* signalsontrafficside */ dominik@452: /* Uncomment following if you want to fool the GRF file. dominik@452: * Some GRF files will refuse to load without this dominik@452: * but you can still squeeze something from them even dominik@452: * without the support - i.e. USSet. --pasky */ dominik@452: //| (1 << 0x1C); /* electrifiedrailway */ dominik@452: dominik@452: _ttdpatch_flags[2] = (1 << 0x0D) /* buildonslopes */ dominik@452: | (1 << 0x16) /* canals */ dominik@452: | (1 << 0x17); /* newstartyear */ dominik@452: } dominik@452: dominik@452: void InitNewGRFFile(const char *filename, int sprite_offset) dominik@452: { dominik@452: struct GRFFile *newfile; dominik@452: pasky@489: newfile = GetFileByFilename(filename); pasky@490: if (newfile != NULL) { pasky@489: /* We already loaded it once. */ pasky@489: newfile->sprite_offset = sprite_offset; pasky@489: _cur_grffile = newfile; pasky@489: return; pasky@489: } pasky@489: dominik@452: newfile = calloc(1, sizeof(struct GRFFile)); dominik@452: dominik@452: if (newfile == NULL) dominik@452: error ("Out of memory"); dominik@452: dominik@452: newfile->filename = strdup(filename); dominik@452: newfile->sprite_offset = sprite_offset; dominik@452: dominik@452: if (_first_grffile == NULL) { dominik@452: _cur_grffile = newfile; dominik@452: _first_grffile = newfile; dominik@452: } else { dominik@452: _cur_grffile->next = newfile; dominik@452: _cur_grffile = newfile; dominik@452: } dominik@452: } dominik@452: dominik@452: dominik@452: /* Here we perform initial decoding of some special sprites (as are they dominik@452: * described at http://www.ttdpatch.net/src/newgrf.txt, but this is only a very dominik@452: * partial implementation yet). */ dominik@452: /* XXX: We consider GRF files trusted. It would be trivial to exploit OTTD by dominik@452: * a crafted invalid GRF file. We should tell that to the user somehow, or dominik@452: * better make this more robust in the future. */ dominik@452: dominik@452: void DecodeSpecialSprite(const char *filename, int num, int spriteid, int stage) dominik@452: { dominik@452: #define NUM_ACTIONS 0xF dominik@452: static const SpecialSpriteHandler handlers[NUM_ACTIONS] = { dominik@452: /* 0x0 */ VehicleChangeInfo, dominik@452: /* 0x1 */ NewSpriteSet, dominik@452: /* 0x2 */ NewSpriteGroup, dominik@452: /* 0x3 */ NewVehicle_SpriteGroupMapping, dominik@452: /* 0x4 */ VehicleNewName, dominik@452: /* 0x5 */ GraphicsNew, dominik@452: /* 0x6 */ CfgApply, dominik@452: /* 0x7 */ SkipIf, dominik@452: /* 0x8 */ GRFInfo, dominik@452: /* 0x9 */ SkipIf, dominik@452: /* 0xa */ SpriteReplace, dominik@452: /* 0xb */ GRFError, dominik@452: /* 0xc */ GRFComment, dominik@452: /* 0xd */ ParamSet, dominik@452: /* 0xe */ GRFInhibit, dominik@452: }; dominik@452: static int initialized; dominik@452: byte action; dominik@452: byte *buf = malloc(num); dominik@452: int i; dominik@452: dominik@452: if (buf == NULL) error("DecodeSpecialSprite: Could not allocate memory"); dominik@452: dominik@452: if (initialized == 0) { dominik@452: InitializeGRFSpecial(); dominik@452: initialized = 1; dominik@452: } dominik@452: dominik@452: _cur_stage = stage; dominik@452: _cur_spriteid = spriteid; dominik@452: dominik@452: for (i = 0; i != num; i++) dominik@452: buf[i] = FioReadByte(); dominik@452: dominik@452: action = buf[0]; dominik@452: dominik@452: /* XXX: There is a difference between staged loading in TTDPatch and dominik@452: * here. In TTDPatch, for some reason actions 1 and 2 are carried out dominik@452: * during stage 0, whilst action 3 is carried out during stage 1 (to dominik@452: * "resolve" cargo IDs... wtf). This is a little problem, because cargo dominik@452: * IDs are valid only within a given set (action 1) block, and may be dominik@452: * overwritten after action 3 associates them. But overwriting happens dominik@452: * in an earlier stage than associating, so... We just process actions dominik@452: * 1 and 2 in stage 1 now, let's hope that won't get us into problems. dominik@452: * --pasky */ dominik@452: dominik@452: if (stage == 0) { dominik@452: /* During initialization, actions 0, 1, 2, 3, 4, 5 and 7 are ignored. */ dominik@452: dominik@452: if ((action == 0x00) || (action == 0x01) || (action == 0x02) || (action == 0x03) dominik@452: || (action == 0x04) || (action == 0x05) || (action == 0x07)) { dominik@452: DEBUG (grf, 7) ("DecodeSpecialSprite: Action: %x, Stage 0, Skipped", action); dominik@452: /* Do nothing. */ dominik@452: dominik@452: } else if (action < NUM_ACTIONS) { dominik@452: DEBUG (grf, 7) ("DecodeSpecialSprite: Action: %x, Stage 0", action); dominik@452: handlers[action](buf, num); dominik@452: dominik@452: } else { dominik@452: grfmsg(GMS_WARN, "Unknown special sprite action %x, skipping.", action); dominik@452: } dominik@452: dominik@452: } else if (stage == 1) { dominik@452: /* A .grf file is activated only if it was active when the game was dominik@452: * started. If a game is loaded, only its active .grfs will be dominik@452: * reactivated, unless "loadallgraphics on" is used. A .grf file is dominik@452: * considered active if its action 8 has been processed, i.e. its dominik@452: * action 8 hasn't been skipped using an action 7. dominik@452: * dominik@452: * During activation, only actions 0, 1, 2, 3, 4, 5, 7, 8, 9, 0A and 0B are dominik@452: * carried out. All others are ignored, because they only need to be dominik@452: * processed once at initialization. */ dominik@452: dominik@452: if ((_cur_grffile == NULL) || strcmp(_cur_grffile->filename, filename)) dominik@452: _cur_grffile = GetFileByFilename(filename); dominik@452: dominik@452: if (_cur_grffile == NULL) dominik@452: error("File ``%s'' lost in cache.\n", filename); dominik@452: dominik@452: if (!(_cur_grffile->flags & 0x0001)) { dominik@452: DEBUG (grf, 7) ("DecodeSpecialSprite: Action: %x, Stage 1, Not activated", action); dominik@452: /* Do nothing. */ dominik@452: dominik@452: } else if ((action == 0x00) || (action == 0x01) || (action == 0x02) || (action == 0x03) dominik@452: || (action == 0x04) || (action == 0x05) || (action == 0x07) || (action == 0x08) dominik@452: || (action == 0x09) || (action == 0x0A) || (action == 0x0B)) { dominik@452: DEBUG (grf, 7) ("DecodeSpecialSprite: Action: %x, Stage 1", action); dominik@452: handlers[action](buf, num); dominik@452: dominik@452: } else if (action < NUM_ACTIONS) { dominik@452: DEBUG (grf, 7) ("DecodeSpecialSprite: Action: %x, Stage 1, Skipped", action); dominik@452: /* Do nothing. */ dominik@452: dominik@452: } else { dominik@452: grfmsg(GMS_WARN, "Unknown special sprite action %x, skipping.", action); dominik@452: } dominik@452: dominik@452: } else { dominik@452: error("Invalid stage %d", stage); dominik@452: } dominik@452: dominik@452: free(buf); dominik@452: #undef NUM_ACTIONS dominik@452: }