00001
00002
00005 #include "stdafx.h"
00006
00007 #include <stdarg.h>
00008
00009 #include "openttd.h"
00010 #include "debug.h"
00011 #include "fileio_func.h"
00012 #include "engine_func.h"
00013 #include "engine_base.h"
00014 #include "spritecache.h"
00015 #include "variables.h"
00016 #include "bridge.h"
00017 #include "town.h"
00018 #include "newgrf_engine.h"
00019 #include "newgrf_text.h"
00020 #include "fontcache.h"
00021 #include "currency.h"
00022 #include "landscape.h"
00023 #include "newgrf_house.h"
00024 #include "newgrf_sound.h"
00025 #include "newgrf_station.h"
00026 #include "industry.h"
00027 #include "newgrf_canal.h"
00028 #include "newgrf_commons.h"
00029 #include "newgrf_townname.h"
00030 #include "newgrf_industries.h"
00031 #include "rev.h"
00032 #include "fios.h"
00033 #include "rail.h"
00034 #include "strings_func.h"
00035 #include "gfx_func.h"
00036 #include "date_func.h"
00037 #include "vehicle_func.h"
00038 #include "sound_func.h"
00039 #include "string_func.h"
00040 #include "network/network.h"
00041 #include "map_func.h"
00042 #include <map>
00043 #include "core/alloc_type.hpp"
00044
00045 #include "table/strings.h"
00046 #include "table/build_industry.h"
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058 static int _skip_sprites;
00059 static uint _file_index;
00060
00061 static GRFFile *_cur_grffile;
00062 GRFFile *_first_grffile;
00063 static SpriteID _cur_spriteid;
00064 static GrfLoadingStage _cur_stage;
00065 static uint32 _nfo_line;
00066
00067 static GRFConfig *_cur_grfconfig;
00068
00069
00070 static byte _misc_grf_features = 0;
00071
00072
00073 static uint32 _ttdpatch_flags[8];
00074
00075
00076 GRFLoadedFeatures _loaded_newgrf_features;
00077
00078 enum GrfDataType {
00079 GDT_SOUND,
00080 };
00081
00082 static byte _grf_data_blocks;
00083 static GrfDataType _grf_data_type;
00084
00085
00086 typedef void (*SpecialSpriteHandler)(byte *buf, size_t len);
00087
00088 enum {
00089 MAX_STATIONS = 256,
00090 };
00091
00092
00093 struct GRFTempEngineData {
00094 uint16 cargo_allowed;
00095 uint16 cargo_disallowed;
00096 uint8 rv_max_speed;
00097 };
00098
00099 static GRFTempEngineData *_gted;
00100
00101
00102
00103
00104 static uint32 _grm_engines[256];
00105
00106
00107 static uint32 _grm_cargos[NUM_CARGO * 2];
00108
00109 struct GRFLocation {
00110 uint32 grfid;
00111 uint32 nfoline;
00112
00113 GRFLocation(uint32 grfid, uint32 nfoline) : grfid(grfid), nfoline(nfoline) { }
00114
00115 bool operator<(const GRFLocation &other) const
00116 {
00117 return this->grfid < other.grfid || (this->grfid == other.grfid && this->nfoline < other.nfoline);
00118 }
00119
00120 bool operator==(const GRFLocation &other) const
00121 {
00122 return this->grfid == other.grfid && this->nfoline == other.nfoline;
00123 }
00124 };
00125
00126 static std::map<GRFLocation, SpriteID> _grm_sprites;
00127 typedef std::map<GRFLocation, byte*> GRFLineToSpriteOverride;
00128 GRFLineToSpriteOverride _grf_line_to_action6_sprite_override;
00129
00138 void CDECL grfmsg(int severity, const char *str, ...)
00139 {
00140 char buf[1024];
00141 va_list va;
00142
00143 va_start(va, str);
00144 vsnprintf(buf, sizeof(buf), str, va);
00145 va_end(va);
00146
00147 DEBUG(grf, severity, "[%s:%d] %s", _cur_grfconfig->filename, _nfo_line, buf);
00148 }
00149
00150 static inline bool check_length(size_t real, size_t wanted, const char *str)
00151 {
00152 if (real >= wanted) return true;
00153 grfmsg(0, "%s: Invalid pseudo sprite length %d (expected %d)!", str, real, wanted);
00154 return false;
00155 }
00156
00157 static inline byte grf_load_byte(byte **buf)
00158 {
00159 return *(*buf)++;
00160 }
00161
00162 static uint16 grf_load_word(byte **buf)
00163 {
00164 uint16 val = grf_load_byte(buf);
00165 return val | (grf_load_byte(buf) << 8);
00166 }
00167
00168 static uint16 grf_load_extended(byte** buf)
00169 {
00170 uint16 val;
00171 val = grf_load_byte(buf);
00172 if (val == 0xFF) val = grf_load_word(buf);
00173 return val;
00174 }
00175
00176 static uint32 grf_load_dword(byte **buf)
00177 {
00178 uint32 val = grf_load_word(buf);
00179 return val | (grf_load_word(buf) << 16);
00180 }
00181
00182 static uint32 grf_load_var(byte size, byte **buf)
00183 {
00184 switch (size) {
00185 case 1: return grf_load_byte(buf);
00186 case 2: return grf_load_word(buf);
00187 case 4: return grf_load_dword(buf);
00188 default:
00189 NOT_REACHED();
00190 return 0;
00191 }
00192 }
00193
00194 static const char *grf_load_string(byte **buf, size_t max_len)
00195 {
00196 const char *string = *(const char **)buf;
00197 size_t string_length = ttd_strnlen(string, max_len);
00198
00199 if (string_length == max_len) {
00200
00201 (*buf)[string_length - 1] = '\0';
00202 grfmsg(7, "String was not terminated with a zero byte.");
00203 } else {
00204
00205 string_length++;
00206 }
00207 *buf += string_length;
00208
00209 return string;
00210 }
00211
00212 static GRFFile *GetFileByGRFID(uint32 grfid)
00213 {
00214 GRFFile *file;
00215
00216 for (file = _first_grffile; file != NULL; file = file->next) {
00217 if (file->grfid == grfid) break;
00218 }
00219 return file;
00220 }
00221
00222 static GRFFile *GetFileByFilename(const char *filename)
00223 {
00224 GRFFile *file;
00225
00226 for (file = _first_grffile; file != NULL; file = file->next) {
00227 if (strcmp(file->filename, filename) == 0) break;
00228 }
00229 return file;
00230 }
00231
00233 static void ClearTemporaryNewGRFData(GRFFile *gf)
00234 {
00235
00236 for (GRFLabel *l = gf->label; l != NULL;) {
00237 GRFLabel *l2 = l->next;
00238 free(l);
00239 l = l2;
00240 }
00241 gf->label = NULL;
00242
00243
00244 free(gf->spritegroups);
00245 gf->spritegroups = NULL;
00246 gf->spritegroups_count = 0;
00247 }
00248
00249
00250 typedef std::map<StringID *, uint32> StringIDToGRFIDMapping;
00251 StringIDToGRFIDMapping _string_to_grf_mapping;
00252
00259 StringID MapGRFStringID(uint32 grfid, StringID str)
00260 {
00261
00262 static StringID units_volume[] = {
00263 STR_NOTHING, STR_PASSENGERS, STR_TONS, STR_BAGS,
00264 STR_LITERS, STR_ITEMS, STR_CRATES, STR_TONS,
00265 STR_TONS, STR_TONS, STR_TONS, STR_BAGS,
00266 STR_TONS, STR_TONS, STR_TONS, STR_BAGS,
00267 STR_TONS, STR_TONS, STR_BAGS, STR_LITERS,
00268 STR_TONS, STR_LITERS, STR_TONS, STR_NOTHING,
00269 STR_BAGS, STR_LITERS, STR_TONS, STR_NOTHING,
00270 STR_TONS, STR_NOTHING, STR_LITERS, STR_NOTHING
00271 };
00272
00273
00274
00275
00276
00277 switch (GB(str, 8, 8)) {
00278 case 0xD0: case 0xD1: case 0xD2: case 0xD3:
00279 case 0xDC:
00280 return GetGRFStringID(grfid, str);
00281
00282 case 0xD4: case 0xD5: case 0xD6: case 0xD7:
00283
00284
00285 return GetGRFStringID(grfid, str - 0x400);
00286
00287 default: break;
00288 }
00289
00290 #define TEXID_TO_STRINGID(begin, end, stringid) if (str >= begin && str <= end) return str + (stringid - begin)
00291
00292 TEXID_TO_STRINGID(0x000E, 0x002D, STR_000E);
00293 TEXID_TO_STRINGID(0x002E, 0x004D, STR_002E);
00294 if (str >= 0x004E && str <= 0x006D) str = units_volume[str - 0x004E];
00295 TEXID_TO_STRINGID(0x006E, 0x008D, STR_QUANTITY_NOTHING);
00296 TEXID_TO_STRINGID(0x008E, 0x00AD, STR_ABBREV_NOTHING);
00297
00298
00299
00300
00301 TEXID_TO_STRINGID(0x200F, 0x201F, STR_200F_TALL_OFFICE_BLOCK);
00302 TEXID_TO_STRINGID(0x2036, 0x2041, STR_2036_COTTAGES);
00303 TEXID_TO_STRINGID(0x2059, 0x205C, STR_2059_IGLOO);
00304
00305
00306 TEXID_TO_STRINGID(0x482A, 0x483B, STR_482A_PRODUCTION_LAST_MONTH);
00307 #undef TEXTID_TO_STRINGID
00308
00309 if (str == STR_NULL) return STR_EMPTY;
00310
00311 return str;
00312 }
00313
00314 static inline uint8 MapDOSColour(uint8 colour)
00315 {
00316 extern const byte _palmap_d2w[];
00317 return (_use_palette == PAL_DOS ? colour : _palmap_d2w[colour]);
00318 }
00319
00320 static std::map<uint32, uint32> _grf_id_overrides;
00321
00322 static void SetNewGRFOverride(uint32 source_grfid, uint32 target_grfid)
00323 {
00324 _grf_id_overrides[source_grfid] = target_grfid;
00325 grfmsg(5, "SetNewGRFOverride: Added override of 0x%X to 0x%X", BSWAP32(source_grfid), BSWAP32(target_grfid));
00326 }
00327
00336 static Engine *GetNewEngine(const GRFFile *file, VehicleType type, uint16 internal_id, bool static_access = false)
00337 {
00338
00339
00340 uint32 scope_grfid = INVALID_GRFID;
00341 if (_settings_game.vehicle.dynamic_engines) {
00342
00343 scope_grfid = file->grfid;
00344 uint32 override = _grf_id_overrides[file->grfid];
00345 if (override != 0) {
00346 scope_grfid = override;
00347 const GRFFile *grf_match = GetFileByGRFID(override);
00348 if (grf_match == NULL) {
00349 grfmsg(5, "Tried mapping from GRFID %x to %x but target is not loaded", BSWAP32(file->grfid), BSWAP32(override));
00350 } else {
00351 grfmsg(5, "Mapping from GRFID %x to %x", BSWAP32(file->grfid), BSWAP32(override));
00352 }
00353 }
00354
00355
00356 EngineID engine = _engine_mngr.GetID(type, internal_id, scope_grfid);
00357 if (engine != INVALID_ENGINE) return GetEngine(engine);
00358 }
00359
00360
00361 EngineID engine = _engine_mngr.GetID(type, internal_id, INVALID_GRFID);
00362 if (engine != INVALID_ENGINE) {
00363 Engine *e = GetEngine(engine);
00364
00365 if (e->grffile == NULL) {
00366 e->grffile = file;
00367 grfmsg(5, "Replaced engine at index %d for GRFID %x, type %d, index %d", e->index, BSWAP32(file->grfid), type, internal_id);
00368 }
00369
00370
00371 if (!static_access) {
00372 EngineIDMapping *eid = _engine_mngr.Get(engine);
00373 eid->grfid = scope_grfid;
00374 }
00375
00376 return e;
00377 }
00378
00379 if (static_access) return NULL;
00380
00381 uint engine_pool_size = GetEnginePoolSize();
00382
00383
00384 Engine *e = new Engine(type, internal_id);
00385 e->grffile = file;
00386
00387
00388 assert(_engine_mngr.Length() == e->index);
00389 EngineIDMapping *eid = _engine_mngr.Append();
00390 eid->type = type;
00391 eid->grfid = scope_grfid;
00392 eid->internal_id = internal_id;
00393 eid->substitute_id = min(internal_id, _engine_counts[type]);
00394
00395 if (engine_pool_size != GetEnginePoolSize()) {
00396
00397 _gted = ReallocT(_gted, GetEnginePoolSize());
00398
00399
00400 size_t len = (GetEnginePoolSize() - engine_pool_size) * sizeof(*_gted);
00401 memset(_gted + engine_pool_size, 0, len);
00402 }
00403
00404 grfmsg(5, "Created new engine at index %d for GRFID %x, type %d, index %d", e->index, BSWAP32(file->grfid), type, internal_id);
00405
00406 return e;
00407 }
00408
00409 EngineID GetNewEngineID(const GRFFile *file, VehicleType type, uint16 internal_id)
00410 {
00411 uint32 scope_grfid = INVALID_GRFID;
00412 if (_settings_game.vehicle.dynamic_engines) {
00413 scope_grfid = file->grfid;
00414 uint32 override = _grf_id_overrides[file->grfid];
00415 if (override != 0) scope_grfid = override;
00416 }
00417
00418 return _engine_mngr.GetID(type, internal_id, scope_grfid);
00419 }
00420
00424 static void MapSpriteMappingRecolour(PalSpriteID *grf_sprite)
00425 {
00426 if (HasBit(grf_sprite->pal, 14)) {
00427 ClrBit(grf_sprite->pal, 14);
00428 SetBit(grf_sprite->sprite, SPRITE_MODIFIER_OPAQUE);
00429 }
00430
00431 if (HasBit(grf_sprite->sprite, 14)) {
00432 ClrBit(grf_sprite->sprite, 14);
00433 SetBit(grf_sprite->sprite, PALETTE_MODIFIER_TRANSPARENT);
00434 }
00435
00436 if (HasBit(grf_sprite->sprite, 15)) {
00437 ClrBit(grf_sprite->sprite, 15);
00438 SetBit(grf_sprite->sprite, PALETTE_MODIFIER_COLOUR);
00439 }
00440 }
00441
00442 enum ChangeInfoResult {
00443 CIR_SUCCESS,
00444 CIR_UNHANDLED,
00445 CIR_UNKNOWN,
00446 CIR_INVALID_ID,
00447 };
00448
00449 typedef ChangeInfoResult (*VCI_Handler)(uint engine, int numinfo, int prop, byte **buf, int len);
00450
00451 static ChangeInfoResult CommonVehicleChangeInfo(EngineInfo *ei, int prop, byte **buf)
00452 {
00453 switch (prop) {
00454 case 0x00:
00455 ei->base_intro = grf_load_word(buf) + DAYS_TILL_ORIGINAL_BASE_YEAR;
00456 break;
00457
00458 case 0x02:
00459 ei->decay_speed = grf_load_byte(buf);
00460 break;
00461
00462 case 0x03:
00463 ei->lifelength = grf_load_byte(buf);
00464 break;
00465
00466 case 0x04:
00467 ei->base_life = grf_load_byte(buf);
00468 break;
00469
00470 case 0x06:
00471 ei->climates = grf_load_byte(buf);
00472
00473
00474 if (ei->climates == 0) ei->climates = 0x80;
00475 break;
00476
00477 case 0x07:
00478
00479 ei->load_amount = grf_load_byte(buf);
00480 break;
00481
00482 default:
00483 return CIR_UNKNOWN;
00484 }
00485
00486 return CIR_SUCCESS;
00487 }
00488
00489 static ChangeInfoResult RailVehicleChangeInfo(uint engine, int numinfo, int prop, byte **bufp, int len)
00490 {
00491 byte *buf = *bufp;
00492 ChangeInfoResult ret = CIR_SUCCESS;
00493
00494 for (int i = 0; i < numinfo; i++) {
00495 Engine *e = GetNewEngine(_cur_grffile, VEH_TRAIN, engine + i);
00496 EngineInfo *ei = &e->info;
00497 RailVehicleInfo *rvi = &e->u.rail;
00498
00499 switch (prop) {
00500 case 0x05: {
00501 uint8 tracktype = grf_load_byte(&buf);
00502
00503 if (tracktype < _cur_grffile->railtype_max) {
00504 RailType railtype = GetRailTypeByLabel(_cur_grffile->railtype_list[tracktype]);
00505 if (railtype == INVALID_RAILTYPE) {
00506
00507 ei[i].climates = 0x80;
00508 } else {
00509 rvi[i].railtype = railtype;
00510 }
00511 break;
00512 }
00513
00514 switch (tracktype) {
00515 case 0: rvi->railtype = rvi->engclass >= 2 ? RAILTYPE_ELECTRIC : RAILTYPE_RAIL; break;
00516 case 1: rvi->railtype = RAILTYPE_MONO; break;
00517 case 2: rvi->railtype = RAILTYPE_MAGLEV; break;
00518 default:
00519 grfmsg(1, "RailVehicleChangeInfo: Invalid track type %d specified, ignoring", tracktype);
00520 break;
00521 }
00522 } break;
00523
00524 case 0x08:
00525
00526
00527 rvi->ai_passenger_only = grf_load_byte(&buf);
00528 break;
00529
00530 case 0x09: {
00531 uint16 speed = grf_load_word(&buf);
00532 if (speed == 0xFFFF) speed = 0;
00533
00534 rvi->max_speed = speed;
00535 } break;
00536
00537 case 0x0B:
00538 rvi->power = grf_load_word(&buf);
00539
00540
00541 if (rvi->power != 0) {
00542 if (rvi->railveh_type == RAILVEH_WAGON) {
00543 rvi->railveh_type = RAILVEH_SINGLEHEAD;
00544 }
00545 } else {
00546 rvi->railveh_type = RAILVEH_WAGON;
00547 }
00548 break;
00549
00550 case 0x0D:
00551 rvi->running_cost = grf_load_byte(&buf);
00552 break;
00553
00554 case 0x0E: {
00555 uint32 base = grf_load_dword(&buf);
00556
00557
00558
00559
00560 if (base == 0) {
00561 rvi->running_cost_class = 0xFF;
00562 } else if (base < 0x4B34 || base > 0x4C54 || (base - 0x4B34) % 6 != 0) {
00563 grfmsg(1, "RailVehicleChangeInfo: Unsupported running cost base 0x%04X, ignoring", base);
00564 } else {
00565
00566 rvi->running_cost_class = (base - 0x4B34) / 6;
00567 }
00568 } break;
00569
00570 case 0x12: {
00571 uint8 spriteid = grf_load_byte(&buf);
00572
00573
00574
00575 if (spriteid < 0xFD) spriteid >>= 1;
00576
00577 rvi->image_index = spriteid;
00578 } break;
00579
00580 case 0x13: {
00581 uint8 dual = grf_load_byte(&buf);
00582
00583 if (dual != 0) {
00584 rvi->railveh_type = RAILVEH_MULTIHEAD;
00585 } else {
00586 rvi->railveh_type = rvi->power == 0 ?
00587 RAILVEH_WAGON : RAILVEH_SINGLEHEAD;
00588 }
00589 } break;
00590
00591 case 0x14:
00592 rvi->capacity = grf_load_byte(&buf);
00593 break;
00594
00595 case 0x15: {
00596 uint8 ctype = grf_load_byte(&buf);
00597
00598 if (ctype < NUM_CARGO && HasBit(_cargo_mask, ctype)) {
00599 rvi->cargo_type = ctype;
00600 } else if (ctype == 0xFF) {
00601
00602 rvi->cargo_type = CT_INVALID;
00603 } else {
00604 rvi->cargo_type = CT_INVALID;
00605 grfmsg(2, "RailVehicleChangeInfo: Invalid cargo type %d, using first refittable", ctype);
00606 }
00607 } break;
00608
00609 case 0x16:
00610 SB(rvi->weight, 0, 8, grf_load_byte(&buf));
00611 break;
00612
00613 case 0x17:
00614 rvi->cost_factor = grf_load_byte(&buf);
00615 break;
00616
00617 case 0x18:
00618 rvi->ai_rank = grf_load_byte(&buf);
00619 break;
00620
00621 case 0x19: {
00622
00623
00624
00625
00626
00627
00628
00629 uint8 traction = grf_load_byte(&buf);
00630 EngineClass engclass;
00631
00632 if (traction <= 0x07) {
00633 engclass = EC_STEAM;
00634 } else if (traction <= 0x27) {
00635 engclass = EC_DIESEL;
00636 } else if (traction <= 0x31) {
00637 engclass = EC_ELECTRIC;
00638 } else if (traction <= 0x37) {
00639 engclass = EC_MONORAIL;
00640 } else if (traction <= 0x41) {
00641 engclass = EC_MAGLEV;
00642 } else {
00643 break;
00644 }
00645
00646 if (_cur_grffile->railtype_max == 0) {
00647
00648
00649 if (rvi->railtype == RAILTYPE_RAIL && engclass >= EC_ELECTRIC) rvi->railtype = RAILTYPE_ELECTRIC;
00650 if (rvi->railtype == RAILTYPE_ELECTRIC && engclass < EC_ELECTRIC) rvi->railtype = RAILTYPE_RAIL;
00651 }
00652
00653 rvi->engclass = engclass;
00654 } break;
00655
00656 case 0x1A:
00657 AlterVehicleListOrder(e->index, grf_load_extended(&buf));
00658 break;
00659
00660 case 0x1B:
00661 rvi->pow_wag_power = grf_load_word(&buf);
00662 break;
00663
00664 case 0x1C:
00665 ei->refit_cost = grf_load_byte(&buf);
00666 break;
00667
00668 case 0x1D:
00669 ei->refit_mask = grf_load_dword(&buf);
00670 break;
00671
00672 case 0x1E:
00673 ei->callbackmask = grf_load_byte(&buf);
00674 break;
00675
00676 case 0x1F:
00677 rvi->tractive_effort = grf_load_byte(&buf);
00678 break;
00679
00680 case 0x20:
00682 grf_load_byte(&buf);
00683 ret = CIR_UNHANDLED;
00684 break;
00685
00686 case 0x21:
00687 rvi->shorten_factor = grf_load_byte(&buf);
00688 break;
00689
00690 case 0x22:
00692 rvi->visual_effect = grf_load_byte(&buf);
00693 break;
00694
00695 case 0x23:
00696 rvi->pow_wag_weight = grf_load_byte(&buf);
00697 break;
00698
00699 case 0x24: {
00700 byte weight = grf_load_byte(&buf);
00701
00702 if (weight > 4) {
00703 grfmsg(2, "RailVehicleChangeInfo: Nonsensical weight of %d tons, ignoring", weight << 8);
00704 } else {
00705 SB(rvi->weight, 8, 8, weight);
00706 }
00707 } break;
00708
00709 case 0x25:
00710 rvi->user_def_data = grf_load_byte(&buf);
00711 break;
00712
00713 case 0x26:
00714 ei->retire_early = grf_load_byte(&buf);
00715 break;
00716
00717 case 0x27:
00718 ei->misc_flags = grf_load_byte(&buf);
00719 _loaded_newgrf_features.has_2CC |= HasBit(ei->misc_flags, EF_USES_2CC);
00720 break;
00721
00722 case 0x28:
00723 _gted[e->index].cargo_allowed = grf_load_word(&buf);
00724 break;
00725
00726 case 0x29:
00727 _gted[e->index].cargo_disallowed = grf_load_word(&buf);
00728 break;
00729
00730 case 0x2A:
00731 ei->base_intro = grf_load_dword(&buf);
00732 break;
00733
00734 default:
00735 ret = CommonVehicleChangeInfo(ei, prop, &buf);
00736 break;
00737 }
00738 }
00739
00740 *bufp = buf;
00741 return ret;
00742 }
00743
00744 static ChangeInfoResult RoadVehicleChangeInfo(uint engine, int numinfo, int prop, byte **bufp, int len)
00745 {
00746 byte *buf = *bufp;
00747 ChangeInfoResult ret = CIR_SUCCESS;
00748
00749 for (int i = 0; i < numinfo; i++) {
00750 Engine *e = GetNewEngine(_cur_grffile, VEH_ROAD, engine + i);
00751 EngineInfo *ei = &e->info;
00752 RoadVehicleInfo *rvi = &e->u.road;
00753
00754 switch (prop) {
00755 case 0x08:
00756 rvi->max_speed = grf_load_byte(&buf);
00757 break;
00758
00759 case 0x09:
00760 rvi->running_cost = grf_load_byte(&buf);
00761 break;
00762
00763 case 0x0A: {
00764 uint32 base = grf_load_dword(&buf);
00765
00766
00767
00768
00769 if (base == 0) {
00770 rvi->running_cost_class = 0xFF;
00771 } else if (base < 0x4B34 || base > 0x4C54 || (base - 0x4B34) % 6 != 0) {
00772 grfmsg(1, "RailVehicleChangeInfo: Unsupported running cost base 0x%04X, ignoring", base);
00773 } else {
00774
00775 rvi->running_cost_class = (base - 0x4B34) / 6;
00776 }
00777
00778 break;
00779 }
00780
00781 case 0x0E: {
00782 uint8 spriteid = grf_load_byte(&buf);
00783
00784
00785 if (spriteid == 0xFF) spriteid = 0xFD;
00786
00787 if (spriteid < 0xFD) spriteid >>= 1;
00788
00789 rvi->image_index = spriteid;
00790 } break;
00791
00792 case 0x0F:
00793 rvi->capacity = grf_load_byte(&buf);
00794 break;
00795
00796 case 0x10: {
00797 uint8 cargo = grf_load_byte(&buf);
00798
00799 if (cargo < NUM_CARGO && HasBit(_cargo_mask, cargo)) {
00800 rvi->cargo_type = cargo;
00801 } else if (cargo == 0xFF) {
00802 rvi->cargo_type = CT_INVALID;
00803 } else {
00804 rvi->cargo_type = CT_INVALID;
00805 grfmsg(2, "RoadVehicleChangeInfo: Invalid cargo type %d, using first refittable", cargo);
00806 }
00807 } break;
00808
00809 case 0x11:
00810 rvi->cost_factor = grf_load_byte(&buf);
00811 break;
00812
00813 case 0x12:
00814 rvi->sfx = (SoundFx)grf_load_byte(&buf);
00815 break;
00816
00817 case 0x13:
00818 rvi->power = grf_load_byte(&buf);
00819 break;
00820
00821 case 0x14:
00822 rvi->weight = grf_load_byte(&buf);
00823 break;
00824
00825 case 0x15:
00826 _gted[e->index].rv_max_speed = grf_load_byte(&buf);
00827 break;
00828
00829 case 0x16:
00830 ei->refit_mask = grf_load_dword(&buf);
00831 break;
00832
00833 case 0x17:
00834 ei->callbackmask = grf_load_byte(&buf);
00835 break;
00836
00837 case 0x18:
00838 rvi->tractive_effort = grf_load_byte(&buf);
00839 break;
00840
00841 case 0x19:
00842 rvi->air_drag = grf_load_byte(&buf);
00843 break;
00844
00845 case 0x1A:
00846 ei->refit_cost = grf_load_byte(&buf);
00847 break;
00848
00849 case 0x1B:
00850 ei->retire_early = grf_load_byte(&buf);
00851 break;
00852
00853 case 0x1C:
00854 ei->misc_flags = grf_load_byte(&buf);
00855 _loaded_newgrf_features.has_2CC |= HasBit(ei->misc_flags, EF_USES_2CC);
00856 break;
00857
00858 case 0x1D:
00859 _gted[e->index].cargo_allowed = grf_load_word(&buf);
00860 break;
00861
00862 case 0x1E:
00863 _gted[e->index].cargo_disallowed = grf_load_word(&buf);
00864 break;
00865
00866 case 0x1F:
00867 ei->base_intro = grf_load_dword(&buf);
00868 break;
00869
00870 case 0x20:
00871 AlterVehicleListOrder(e->index, grf_load_extended(&buf));
00872 break;
00873
00874 default:
00875 ret = CommonVehicleChangeInfo(ei, prop, &buf);
00876 break;
00877 }
00878 }
00879
00880 *bufp = buf;
00881 return ret;
00882 }
00883
00884 static ChangeInfoResult ShipVehicleChangeInfo(uint engine, int numinfo, int prop, byte **bufp, int len)
00885 {
00886 byte *buf = *bufp;
00887 ChangeInfoResult ret = CIR_SUCCESS;
00888
00889 for (int i = 0; i < numinfo; i++) {
00890 Engine *e = GetNewEngine(_cur_grffile, VEH_SHIP, engine + i);
00891 EngineInfo *ei = &e->info;
00892 ShipVehicleInfo *svi = &e->u.ship;
00893
00894 switch (prop) {
00895 case 0x08: {
00896 uint8 spriteid = grf_load_byte(&buf);
00897
00898
00899 if (spriteid == 0xFF) spriteid = 0xFD;
00900
00901 if (spriteid < 0xFD) spriteid >>= 1;
00902
00903 svi->image_index = spriteid;
00904 } break;
00905
00906 case 0x09:
00907 svi->refittable = (grf_load_byte(&buf) != 0);
00908 break;
00909
00910 case 0x0A:
00911 svi->cost_factor = grf_load_byte(&buf);
00912 break;
00913
00914 case 0x0B:
00915 svi->max_speed = grf_load_byte(&buf);
00916 break;
00917
00918 case 0x0C: {
00919 uint8 cargo = grf_load_byte(&buf);
00920
00921 if (cargo < NUM_CARGO && HasBit(_cargo_mask, cargo)) {
00922 svi->cargo_type = cargo;
00923 } else if (cargo == 0xFF) {
00924 svi->cargo_type = CT_INVALID;
00925 } else {
00926 svi->cargo_type = CT_INVALID;
00927 grfmsg(2, "ShipVehicleChangeInfo: Invalid cargo type %d, using first refittable", cargo);
00928 }
00929 } break;
00930
00931 case 0x0D:
00932 svi->capacity = grf_load_word(&buf);
00933 break;
00934
00935 case 0x0F:
00936 svi->running_cost = grf_load_byte(&buf);
00937 break;
00938
00939 case 0x10:
00940 svi->sfx = (SoundFx)grf_load_byte(&buf);
00941 break;
00942
00943 case 0x11:
00944 ei->refit_mask = grf_load_dword(&buf);
00945 break;
00946
00947 case 0x12:
00948 ei->callbackmask = grf_load_byte(&buf);
00949 break;
00950
00951 case 0x13:
00952 ei->refit_cost = grf_load_byte(&buf);
00953 break;
00954
00955 case 0x14:
00956 case 0x15:
00958 grf_load_byte(&buf);
00959 ret = CIR_UNHANDLED;
00960 break;
00961
00962 case 0x16:
00963 ei->retire_early = grf_load_byte(&buf);
00964 break;
00965
00966 case 0x17:
00967 ei->misc_flags = grf_load_byte(&buf);
00968 _loaded_newgrf_features.has_2CC |= HasBit(ei->misc_flags, EF_USES_2CC);
00969 break;
00970
00971 case 0x18:
00972 _gted[e->index].cargo_allowed = grf_load_word(&buf);
00973 break;
00974
00975 case 0x19:
00976 _gted[e->index].cargo_disallowed = grf_load_word(&buf);
00977 break;
00978
00979 case 0x1A:
00980 ei->base_intro = grf_load_dword(&buf);
00981 break;
00982
00983 case 0x1B:
00984 AlterVehicleListOrder(e->index, grf_load_extended(&buf));
00985 break;
00986
00987 default:
00988 ret = CommonVehicleChangeInfo(ei, prop, &buf);
00989 break;
00990 }
00991 }
00992
00993 *bufp = buf;
00994 return ret;
00995 }
00996
00997 static ChangeInfoResult AircraftVehicleChangeInfo(uint engine, int numinfo, int prop, byte **bufp, int len)
00998 {
00999 byte *buf = *bufp;
01000 ChangeInfoResult ret = CIR_SUCCESS;
01001
01002 for (int i = 0; i < numinfo; i++) {
01003 Engine *e = GetNewEngine(_cur_grffile, VEH_AIRCRAFT, engine + i);
01004 EngineInfo *ei = &e->info;
01005 AircraftVehicleInfo *avi = &e->u.air;
01006
01007 switch (prop) {
01008 case 0x08: {
01009 uint8 spriteid = grf_load_byte(&buf);
01010
01011
01012 if (spriteid == 0xFF) spriteid = 0xFD;
01013
01014 if (spriteid < 0xFD) spriteid >>= 1;
01015
01016 avi->image_index = spriteid;
01017 } break;
01018
01019 case 0x09:
01020 if (grf_load_byte(&buf) == 0) {
01021 avi->subtype = AIR_HELI;
01022 } else {
01023 SB(avi->subtype, 0, 1, 1);
01024 }
01025 break;
01026
01027 case 0x0A:
01028 SB(avi->subtype, 1, 1, (grf_load_byte(&buf) != 0 ? 1 : 0));
01029 break;
01030
01031 case 0x0B:
01032 avi->cost_factor = grf_load_byte(&buf);
01033 break;
01034
01035 case 0x0C:
01036 avi->max_speed = (grf_load_byte(&buf) * 129) / 10;
01037 break;
01038
01039 case 0x0D:
01040 avi->acceleration = (grf_load_byte(&buf) * 129) / 10;
01041 break;
01042
01043 case 0x0E:
01044 avi->running_cost = grf_load_byte(&buf);
01045 break;
01046
01047 case 0x0F:
01048 avi->passenger_capacity = grf_load_word(&buf);
01049 break;
01050
01051 case 0x11:
01052 avi->mail_capacity = grf_load_byte(&buf);
01053 break;
01054
01055 case 0x12:
01056 avi->sfx = (SoundFx)grf_load_byte(&buf);
01057 break;
01058
01059 case 0x13:
01060 ei->refit_mask = grf_load_dword(&buf);
01061 break;
01062
01063 case 0x14:
01064 ei->callbackmask = grf_load_byte(&buf);
01065 break;
01066
01067 case 0x15:
01068 ei->refit_cost = grf_load_byte(&buf);
01069 break;
01070
01071 case 0x16:
01072 ei->retire_early = grf_load_byte(&buf);
01073 break;
01074
01075 case 0x17:
01076 ei->misc_flags = grf_load_byte(&buf);
01077 _loaded_newgrf_features.has_2CC |= HasBit(ei->misc_flags, EF_USES_2CC);
01078 break;
01079
01080 case 0x18:
01081 _gted[e->index].cargo_allowed = grf_load_word(&buf);
01082 break;
01083
01084 case 0x19:
01085 _gted[e->index].cargo_disallowed = grf_load_word(&buf);
01086 break;
01087
01088 case 0x1A:
01089 ei->base_intro = grf_load_dword(&buf);
01090 break;
01091
01092 case 0x1B:
01093 AlterVehicleListOrder(e->index, grf_load_extended(&buf));
01094 break;
01095
01096 default:
01097 ret = CommonVehicleChangeInfo(ei, prop, &buf);
01098 break;
01099 }
01100 }
01101
01102 *bufp = buf;
01103 return ret;
01104 }
01105
01106 static ChangeInfoResult StationChangeInfo(uint stid, int numinfo, int prop, byte **bufp, int len)
01107 {
01108 byte *buf = *bufp;
01109 ChangeInfoResult ret = CIR_SUCCESS;
01110
01111 if (stid + numinfo > MAX_STATIONS) {
01112 grfmsg(1, "StationChangeInfo: Station %u is invalid, max %u, ignoring", stid + numinfo, MAX_STATIONS);
01113 return CIR_INVALID_ID;
01114 }
01115
01116
01117 if (_cur_grffile->stations == NULL) _cur_grffile->stations = CallocT<StationSpec*>(MAX_STATIONS);
01118
01119 for (int i = 0; i < numinfo; i++) {
01120 StationSpec *statspec = _cur_grffile->stations[stid + i];
01121
01122
01123 if (statspec == NULL && prop != 0x08) {
01124 grfmsg(2, "StationChangeInfo: Attempt to modify undefined station %u, ignoring", stid + i);
01125 return CIR_INVALID_ID;
01126 }
01127
01128 switch (prop) {
01129 case 0x08: {
01130 StationSpec **spec = &_cur_grffile->stations[stid + i];
01131
01132
01133 if (*spec == NULL) *spec = CallocT<StationSpec>(1);
01134
01135
01136 uint32 classid = grf_load_dword(&buf);
01137 (*spec)->sclass = AllocateStationClass(BSWAP32(classid));
01138 } break;
01139
01140 case 0x09:
01141 statspec->tiles = grf_load_extended(&buf);
01142 statspec->renderdata = CallocT<DrawTileSprites>(statspec->tiles);
01143 statspec->copied_renderdata = false;
01144
01145 for (uint t = 0; t < statspec->tiles; t++) {
01146 DrawTileSprites *dts = &statspec->renderdata[t];
01147 uint seq_count = 0;
01148
01149 dts->seq = NULL;
01150 dts->ground.sprite = grf_load_word(&buf);
01151 dts->ground.pal = grf_load_word(&buf);
01152 if (dts->ground.sprite == 0) continue;
01153 if (HasBit(dts->ground.pal, 15)) {
01154 ClrBit(dts->ground.pal, 15);
01155 SetBit(dts->ground.sprite, SPRITE_MODIFIER_USE_OFFSET);
01156 }
01157
01158 MapSpriteMappingRecolour(&dts->ground);
01159
01160 while (buf < *bufp + len) {
01161 DrawTileSeqStruct *dtss;
01162
01163
01164 dts->seq = ReallocT((DrawTileSeqStruct*)dts->seq, ++seq_count);
01165 dtss = (DrawTileSeqStruct*) &dts->seq[seq_count - 1];
01166
01167 dtss->delta_x = grf_load_byte(&buf);
01168 if ((byte) dtss->delta_x == 0x80) break;
01169 dtss->delta_y = grf_load_byte(&buf);
01170 dtss->delta_z = grf_load_byte(&buf);
01171 dtss->size_x = grf_load_byte(&buf);
01172 dtss->size_y = grf_load_byte(&buf);
01173 dtss->size_z = grf_load_byte(&buf);
01174 dtss->image.sprite = grf_load_word(&buf);
01175 dtss->image.pal = grf_load_word(&buf);
01176
01177
01178 if (HasBit(dtss->image.pal, 15)) {
01179 ClrBit(dtss->image.pal, 15);
01180 SetBit(dtss->image.sprite, SPRITE_MODIFIER_USE_OFFSET);
01181 }
01182
01183 MapSpriteMappingRecolour(&dtss->image);
01184 }
01185 }
01186 break;
01187
01188 case 0x0A: {
01189 byte srcid = grf_load_byte(&buf);
01190 const StationSpec *srcstatspec = _cur_grffile->stations[srcid];
01191
01192 statspec->tiles = srcstatspec->tiles;
01193 statspec->renderdata = srcstatspec->renderdata;
01194 statspec->copied_renderdata = true;
01195 } break;
01196
01197 case 0x0B:
01198 statspec->callbackmask = grf_load_byte(&buf);
01199 break;
01200
01201 case 0x0C:
01202 statspec->disallowed_platforms = grf_load_byte(&buf);
01203 break;
01204
01205 case 0x0D:
01206 statspec->disallowed_lengths = grf_load_byte(&buf);
01207 break;
01208
01209 case 0x0E:
01210 statspec->copied_layouts = false;
01211
01212 while (buf < *bufp + len) {
01213 byte length = grf_load_byte(&buf);
01214 byte number = grf_load_byte(&buf);
01215 StationLayout layout;
01216 uint l, p;
01217
01218 if (length == 0 || number == 0) break;
01219
01220 if (length > statspec->lengths) {
01221 statspec->platforms = ReallocT(statspec->platforms, length);
01222 memset(statspec->platforms + statspec->lengths, 0, length - statspec->lengths);
01223
01224 statspec->layouts = ReallocT(statspec->layouts, length);
01225 memset(statspec->layouts + statspec->lengths, 0,
01226 (length - statspec->lengths) * sizeof(*statspec->layouts));
01227
01228 statspec->lengths = length;
01229 }
01230 l = length - 1;
01231
01232 if (number > statspec->platforms[l]) {
01233 statspec->layouts[l] = ReallocT(statspec->layouts[l], number);
01234
01235 memset(statspec->layouts[l] + statspec->platforms[l], 0,
01236 (number - statspec->platforms[l]) * sizeof(**statspec->layouts));
01237
01238 statspec->platforms[l] = number;
01239 }
01240
01241 p = 0;
01242 layout = MallocT<byte>(length * number);
01243 for (l = 0; l < length; l++) {
01244 for (p = 0; p < number; p++) {
01245 layout[l * number + p] = grf_load_byte(&buf);
01246 }
01247 }
01248
01249 l--;
01250 p--;
01251 free(statspec->layouts[l][p]);
01252 statspec->layouts[l][p] = layout;
01253 }
01254 break;
01255
01256 case 0x0F: {
01257 byte srcid = grf_load_byte(&buf);
01258 const StationSpec *srcstatspec = _cur_grffile->stations[srcid];
01259
01260 statspec->lengths = srcstatspec->lengths;
01261 statspec->platforms = srcstatspec->platforms;
01262 statspec->layouts = srcstatspec->layouts;
01263 statspec->copied_layouts = true;
01264 } break;
01265
01266 case 0x10:
01267 statspec->cargo_threshold = grf_load_word(&buf);
01268 break;
01269
01270 case 0x11:
01271 statspec->pylons = grf_load_byte(&buf);
01272 break;
01273
01274 case 0x12:
01275 statspec->cargo_triggers = grf_load_dword(&buf);
01276 break;
01277
01278 case 0x13:
01279 statspec->flags = grf_load_byte(&buf);
01280 break;
01281
01282 case 0x14:
01283 statspec->wires = grf_load_byte(&buf);
01284 break;
01285
01286 case 0x15:
01287 statspec->blocked = grf_load_byte(&buf);
01288 break;
01289
01290 case 0x16:
01291 statspec->anim_frames = grf_load_byte(&buf);
01292 statspec->anim_status = grf_load_byte(&buf);
01293 break;
01294
01295 case 0x17:
01296 statspec->anim_speed = grf_load_byte(&buf);
01297 break;
01298
01299 case 0x18:
01300 statspec->anim_triggers = grf_load_word(&buf);
01301 break;
01302
01303 default:
01304 ret = CIR_UNKNOWN;
01305 break;
01306 }
01307 }
01308
01309 *bufp = buf;
01310 return ret;
01311 }
01312
01313 static ChangeInfoResult CanalChangeInfo(uint id, int numinfo, int prop, byte **bufp, int len)
01314 {
01315 byte *buf = *bufp;
01316 ChangeInfoResult ret = CIR_SUCCESS;
01317
01318 if (id + numinfo > CF_END) {
01319 grfmsg(1, "CanalChangeInfo: Canal feature %u is invalid, max %u, ignoreing", id + numinfo, CF_END);
01320 return CIR_INVALID_ID;
01321 }
01322
01323 for (int i = 0; i < numinfo; i++) {
01324 WaterFeature *wf = &_water_feature[id + i];
01325
01326 switch (prop) {
01327 case 0x08:
01328 wf->callbackmask = grf_load_byte(&buf);
01329 break;
01330
01331 case 0x09:
01332 wf->flags = grf_load_byte(&buf);
01333 break;
01334
01335 default:
01336 ret = CIR_UNKNOWN;
01337 break;
01338 }
01339 }
01340
01341 *bufp = buf;
01342 return ret;
01343 }
01344
01345 static ChangeInfoResult BridgeChangeInfo(uint brid, int numinfo, int prop, byte **bufp, int len)
01346 {
01347 byte *buf = *bufp;
01348 ChangeInfoResult ret = CIR_SUCCESS;
01349
01350 if (brid + numinfo > MAX_BRIDGES) {
01351 grfmsg(1, "BridgeChangeInfo: Bridge %u is invalid, max %u, ignoring", brid + numinfo, MAX_BRIDGES);
01352 return CIR_INVALID_ID;
01353 }
01354
01355 for (int i = 0; i < numinfo; i++) {
01356 BridgeSpec *bridge = &_bridge[brid + i];
01357
01358 switch (prop) {
01359 case 0x08:
01360 bridge->avail_year = ORIGINAL_BASE_YEAR + grf_load_byte(&buf);
01361 break;
01362
01363 case 0x09:
01364 bridge->min_length = grf_load_byte(&buf);
01365 break;
01366
01367 case 0x0A:
01368 bridge->max_length = grf_load_byte(&buf);
01369 break;
01370
01371 case 0x0B:
01372 bridge->price = grf_load_byte(&buf);
01373 break;
01374
01375 case 0x0C:
01376 bridge->speed = grf_load_word(&buf);
01377 break;
01378
01379 case 0x0D: {
01380 byte tableid = grf_load_byte(&buf);
01381 byte numtables = grf_load_byte(&buf);
01382
01383 if (bridge->sprite_table == NULL) {
01384
01385 bridge->sprite_table = CallocT<PalSpriteID*>(7);
01386 }
01387
01388 for (; numtables-- != 0; tableid++) {
01389 if (tableid >= 7) {
01390 grfmsg(1, "BridgeChangeInfo: Table %d >= 7, skipping", tableid);
01391 for (byte sprite = 0; sprite < 32; sprite++) grf_load_dword(&buf);
01392 continue;
01393 }
01394
01395 if (bridge->sprite_table[tableid] == NULL) {
01396 bridge->sprite_table[tableid] = MallocT<PalSpriteID>(32);
01397 }
01398
01399 for (byte sprite = 0; sprite < 32; sprite++) {
01400 SpriteID image = grf_load_word(&buf);
01401 SpriteID pal = grf_load_word(&buf);
01402
01403 bridge->sprite_table[tableid][sprite].sprite = image;
01404 bridge->sprite_table[tableid][sprite].pal = pal;
01405
01406 MapSpriteMappingRecolour(&bridge->sprite_table[tableid][sprite]);
01407 }
01408 }
01409 } break;
01410
01411 case 0x0E:
01412 bridge->flags = grf_load_byte(&buf);
01413 break;
01414
01415 case 0x0F:
01416 bridge->avail_year = Clamp(grf_load_dword(&buf), MIN_YEAR, MAX_YEAR);
01417 break;
01418
01419 case 0x10: {
01420 StringID newone = GetGRFStringID(_cur_grffile->grfid, grf_load_word(&buf));
01421 if (newone != STR_UNDEFINED) bridge->material = newone;
01422 } break;
01423
01424 case 0x11:
01425 case 0x12: {
01426 StringID newone = GetGRFStringID(_cur_grffile->grfid, grf_load_word(&buf));
01427 if (newone != STR_UNDEFINED) bridge->transport_name[prop - 0x11] = newone;
01428 } break;
01429
01430 case 0x13:
01431 bridge->price = grf_load_word(&buf);
01432 break;
01433
01434 default:
01435 ret = CIR_UNKNOWN;
01436 break;
01437 }
01438 }
01439
01440 *bufp = buf;
01441 return ret;
01442 }
01443
01444 static ChangeInfoResult TownHouseChangeInfo(uint hid, int numinfo, int prop, byte **bufp, int len)
01445 {
01446 byte *buf = *bufp;
01447 ChangeInfoResult ret = CIR_SUCCESS;
01448
01449 if (hid + numinfo > HOUSE_MAX) {
01450 grfmsg(1, "TownHouseChangeInfo: Too many houses loaded (%u), max (%u). Ignoring.", hid + numinfo, HOUSE_MAX);
01451 return CIR_INVALID_ID;
01452 }
01453
01454
01455 if (_cur_grffile->housespec == NULL) {
01456 _cur_grffile->housespec = CallocT<HouseSpec*>(HOUSE_MAX);
01457 }
01458
01459 for (int i = 0; i < numinfo; i++) {
01460 HouseSpec *housespec = _cur_grffile->housespec[hid + i];
01461
01462 if (prop != 0x08 && housespec == NULL) {
01463 grfmsg(2, "TownHouseChangeInfo: Attempt to modify undefined house %u. Ignoring.", hid + i);
01464 return CIR_INVALID_ID;
01465 }
01466
01467 switch (prop) {
01468 case 0x08: {
01469 HouseSpec **house = &_cur_grffile->housespec[hid + i];
01470 byte subs_id = grf_load_byte(&buf);
01471
01472 if (subs_id == 0xFF) {
01473
01474
01475 _house_specs[hid + i].enabled = false;
01476 continue;
01477 } else if (subs_id >= NEW_HOUSE_OFFSET) {
01478
01479 grfmsg(2, "TownHouseChangeInfo: Attempt to use new house %u as substitute house for %u. Ignoring.", subs_id, hid + i);
01480 continue;
01481 }
01482
01483
01484 if (*house == NULL) *house = CallocT<HouseSpec>(1);
01485
01486 housespec = *house;
01487
01488 memcpy(housespec, &_house_specs[subs_id], sizeof(_house_specs[subs_id]));
01489
01490 housespec->enabled = true;
01491 housespec->local_id = hid + i;
01492 housespec->substitute_id = subs_id;
01493 housespec->grffile = _cur_grffile;
01494 housespec->random_colour[0] = 0x04;
01495 housespec->random_colour[1] = 0x08;
01496 housespec->random_colour[2] = 0x0C;
01497 housespec->random_colour[3] = 0x06;
01498
01499
01500
01501
01502
01503 if (!GetCargo(housespec->accepts_cargo[2])->IsValid()) {
01504 housespec->cargo_acceptance[2] = 0;
01505 }
01506
01512 if (housespec->min_year < 1930) housespec->min_year = 1930;
01513
01514 _loaded_newgrf_features.has_newhouses = true;
01515 } break;
01516
01517 case 0x09:
01518 housespec->building_flags = (BuildingFlags)grf_load_byte(&buf);
01519 break;
01520
01521 case 0x0A: {
01522 uint16 years = grf_load_word(&buf);
01523 housespec->min_year = GB(years, 0, 8) > 150 ? MAX_YEAR : ORIGINAL_BASE_YEAR + GB(years, 0, 8);
01524 housespec->max_year = GB(years, 8, 8) > 150 ? MAX_YEAR : ORIGINAL_BASE_YEAR + GB(years, 8, 8);
01525 } break;
01526
01527 case 0x0B:
01528 housespec->population = grf_load_byte(&buf);
01529 break;
01530
01531 case 0x0C:
01532 housespec->mail_generation = grf_load_byte(&buf);
01533 break;
01534
01535 case 0x0D:
01536 case 0x0E:
01537 housespec->cargo_acceptance[prop - 0x0D] = grf_load_byte(&buf);
01538 break;
01539
01540 case 0x0F: {
01541 int8 goods = grf_load_byte(&buf);
01542
01543
01544
01545 CargoID cid = (goods >= 0) ? ((_settings_game.game_creation.landscape == LT_TOYLAND) ? CT_CANDY : CT_GOODS) :
01546 ((_settings_game.game_creation.landscape == LT_TOYLAND) ? CT_FIZZY_DRINKS : CT_FOOD);
01547
01548
01549 if (!GetCargo(cid)->IsValid()) goods = 0;
01550
01551 housespec->accepts_cargo[2] = cid;
01552 housespec->cargo_acceptance[2] = abs(goods);
01553 } break;
01554
01555 case 0x10:
01556 housespec->remove_rating_decrease = grf_load_word(&buf);
01557 break;
01558
01559 case 0x11:
01560 housespec->removal_cost = grf_load_byte(&buf);
01561 break;
01562
01563 case 0x12:
01564 housespec->building_name = grf_load_word(&buf);
01565 _string_to_grf_mapping[&housespec->building_name] = _cur_grffile->grfid;
01566 break;
01567
01568 case 0x13:
01569 housespec->building_availability = (HouseZones)grf_load_word(&buf);
01570 break;
01571
01572 case 0x14:
01573 housespec->callback_mask = grf_load_byte(&buf);
01574 break;
01575
01576 case 0x15: {
01577 byte override = grf_load_byte(&buf);
01578
01579
01580 if (override >= NEW_HOUSE_OFFSET) {
01581 grfmsg(2, "TownHouseChangeInfo: Attempt to override new house %u with house id %u. Ignoring.", override, hid + i);
01582 continue;
01583 }
01584
01585 _house_mngr.Add(hid + i, _cur_grffile->grfid, override);
01586 } break;
01587
01588 case 0x16:
01589 housespec->processing_time = grf_load_byte(&buf);
01590 break;
01591
01592 case 0x17:
01593 for (uint j = 0; j < 4; j++) housespec->random_colour[j] = grf_load_byte(&buf);
01594 break;
01595
01596 case 0x18:
01597 housespec->probability = grf_load_byte(&buf);
01598 break;
01599
01600 case 0x19:
01601 housespec->extra_flags = (HouseExtraFlags)grf_load_byte(&buf);
01602 break;
01603
01604 case 0x1A:
01605 housespec->animation_frames = grf_load_byte(&buf);
01606 break;
01607
01608 case 0x1B:
01609 housespec->animation_speed = Clamp(grf_load_byte(&buf), 2, 16);
01610 break;
01611
01612 case 0x1C:
01613 housespec->class_id = AllocateHouseClassID(grf_load_byte(&buf), _cur_grffile->grfid);
01614 break;
01615
01616 case 0x1D:
01617 housespec->callback_mask |= (grf_load_byte(&buf) << 8);
01618 break;
01619
01620 case 0x1E: {
01621 uint32 cargotypes = grf_load_dword(&buf);
01622
01623
01624 if (cargotypes == 0xFFFFFFFF) break;
01625
01626 for (uint j = 0; j < 3; j++) {
01627
01628 uint8 cargo_part = GB(cargotypes, 8 * j, 8);
01629 CargoID cargo = GetCargoTranslation(cargo_part, _cur_grffile);
01630
01631 if (cargo == CT_INVALID) {
01632
01633 housespec->cargo_acceptance[j] = 0;
01634 } else {
01635 housespec->accepts_cargo[j] = cargo;
01636 }
01637 }
01638 } break;
01639
01640 case 0x1F:
01641 housespec->minimum_life = grf_load_byte(&buf);
01642 break;
01643
01644 case 0x20: {
01645 byte count = grf_load_byte(&buf);
01646 for (byte j = 0; j < count; j++) grf_load_byte(&buf);
01647 ret = CIR_UNHANDLED;
01648 } break;
01649
01650 case 0x21:
01651 housespec->min_year = grf_load_word(&buf);
01652 break;
01653
01654 case 0x22:
01655 housespec->max_year = grf_load_word(&buf);
01656 break;
01657
01658 default:
01659 ret = CIR_UNKNOWN;
01660 break;
01661 }
01662 }
01663
01664 *bufp = buf;
01665 return ret;
01666 }
01667
01668 static ChangeInfoResult GlobalVarChangeInfo(uint gvid, int numinfo, int prop, byte **bufp, int len)
01669 {
01670 byte *buf = *bufp;
01671 ChangeInfoResult ret = CIR_SUCCESS;
01672
01673 for (int i = 0; i < numinfo; i++) {
01674 switch (prop) {
01675 case 0x08: {
01676 byte factor = grf_load_byte(&buf);
01677 uint price = gvid + i;
01678
01679 if (price < NUM_PRICES) {
01680 SetPriceBaseMultiplier(price, factor);
01681 } else {
01682 grfmsg(1, "GlobalVarChangeInfo: Price %d out of range, ignoring", price);
01683 }
01684 } break;
01685
01686 case 0x09:
01687
01688
01689 buf += 4;
01690 break;
01691
01692 case 0x0A: {
01693 uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
01694 StringID newone = GetGRFStringID(_cur_grffile->grfid, grf_load_word(&buf));
01695
01696 if ((newone != STR_UNDEFINED) && (curidx < NUM_CURRENCY)) {
01697 _currency_specs[curidx].name = newone;
01698 }
01699 } break;
01700
01701 case 0x0B: {
01702 uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
01703 uint32 rate = grf_load_dword(&buf);
01704
01705 if (curidx < NUM_CURRENCY) {
01706
01707
01708
01709 _currency_specs[curidx].rate = rate / 1000;
01710 } else {
01711 grfmsg(1, "GlobalVarChangeInfo: Currency multipliers %d out of range, ignoring", curidx);
01712 }
01713 } break;
01714
01715 case 0x0C: {
01716 uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
01717 uint16 options = grf_load_word(&buf);
01718
01719 if (curidx < NUM_CURRENCY) {
01720 _currency_specs[curidx].separator = GB(options, 0, 8);
01721
01722
01723 _currency_specs[curidx].symbol_pos = GB(options, 8, 1);
01724 } else {
01725 grfmsg(1, "GlobalVarChangeInfo: Currency option %d out of range, ignoring", curidx);
01726 }
01727 } break;
01728
01729 case 0x0D: {
01730 uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
01731 uint32 tempfix = grf_load_dword(&buf);
01732
01733 if (curidx < NUM_CURRENCY) {
01734 memcpy(_currency_specs[curidx].prefix, &tempfix, 4);
01735 _currency_specs[curidx].prefix[4] = 0;
01736 } else {
01737 grfmsg(1, "GlobalVarChangeInfo: Currency symbol %d out of range, ignoring", curidx);
01738 }
01739 } break;
01740
01741 case 0x0E: {
01742 uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
01743 uint32 tempfix = grf_load_dword(&buf);
01744
01745 if (curidx < NUM_CURRENCY) {
01746 memcpy(&_currency_specs[curidx].suffix, &tempfix, 4);
01747 _currency_specs[curidx].suffix[4] = 0;
01748 } else {
01749 grfmsg(1, "GlobalVarChangeInfo: Currency symbol %d out of range, ignoring", curidx);
01750 }
01751 } break;
01752
01753 case 0x0F: {
01754 uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
01755 Year year_euro = grf_load_word(&buf);
01756
01757 if (curidx < NUM_CURRENCY) {
01758 _currency_specs[curidx].to_euro = year_euro;
01759 } else {
01760 grfmsg(1, "GlobalVarChangeInfo: Euro intro date %d out of range, ignoring", curidx);
01761 }
01762 } break;
01763
01764 case 0x10:
01765 if (numinfo > 1 || IsSnowLineSet()) {
01766 grfmsg(1, "GlobalVarChangeInfo: The snowline can only be set once (%d)", numinfo);
01767 } else if (len < SNOW_LINE_MONTHS * SNOW_LINE_DAYS) {
01768 grfmsg(1, "GlobalVarChangeInfo: Not enough entries set in the snowline table (%d)", len);
01769 } else {
01770 byte table[SNOW_LINE_MONTHS][SNOW_LINE_DAYS];
01771
01772 for (uint i = 0; i < SNOW_LINE_MONTHS; i++) {
01773 for (uint j = 0; j < SNOW_LINE_DAYS; j++) {
01774 table[i][j] = grf_load_byte(&buf);
01775 }
01776 }
01777 SetSnowLine(table);
01778 }
01779 break;
01780
01781 case 0x11:
01782
01783
01784 buf += 8;
01785 break;
01786
01787 case 0x12:
01788
01789
01790 buf += 4;
01791 break;
01792
01793 default:
01794 ret = CIR_UNKNOWN;
01795 break;
01796 }
01797 }
01798
01799 *bufp = buf;
01800 return ret;
01801 }
01802
01803 static ChangeInfoResult GlobalVarReserveInfo(uint gvid, int numinfo, int prop, byte **bufp, int len)
01804 {
01805 byte *buf = *bufp;
01806 ChangeInfoResult ret = CIR_SUCCESS;
01807
01808 for (int i = 0; i < numinfo; i++) {
01809 switch (prop) {
01810 case 0x08:
01811 grf_load_byte(&buf);
01812 break;
01813
01814 case 0x09: {
01815 if (i == 0) {
01816 if (gvid != 0) {
01817 grfmsg(1, "ReserveChangeInfo: Cargo translation table must start at zero");
01818 return CIR_INVALID_ID;
01819 }
01820
01821 free(_cur_grffile->cargo_list);
01822 _cur_grffile->cargo_max = numinfo;
01823 _cur_grffile->cargo_list = MallocT<CargoLabel>(numinfo);
01824 }
01825
01826 CargoLabel cl = grf_load_dword(&buf);
01827 _cur_grffile->cargo_list[i] = BSWAP32(cl);
01828 break;
01829 }
01830
01831 case 0x0A:
01832 case 0x0C:
01833 case 0x0F:
01834 grf_load_word(&buf);
01835 break;
01836
01837 case 0x0B:
01838 case 0x0D:
01839 case 0x0E:
01840 grf_load_dword(&buf);
01841 break;
01842
01843 case 0x10:
01844 buf += SNOW_LINE_MONTHS * SNOW_LINE_DAYS;
01845 break;
01846
01847 case 0x11: {
01848 uint32 s = grf_load_dword(&buf);
01849 uint32 t = grf_load_dword(&buf);
01850 SetNewGRFOverride(s, t);
01851 break;
01852 }
01853
01854 case 0x12: {
01855 if (i == 0) {
01856 if (gvid != 0) {
01857 grfmsg(1, "ReserveChangeInfo: Rail type translation table must start at zero");
01858 return CIR_INVALID_ID;
01859 }
01860
01861 free(_cur_grffile->railtype_list);
01862 _cur_grffile->railtype_max = numinfo;
01863 _cur_grffile->railtype_list = MallocT<RailTypeLabel>(numinfo);
01864 }
01865
01866 RailTypeLabel rtl = grf_load_dword(&buf);
01867 _cur_grffile->railtype_list[i] = BSWAP32(rtl);
01868 break;
01869 }
01870
01871 default:
01872 ret = CIR_UNKNOWN;
01873 break;
01874 }
01875 }
01876
01877 *bufp = buf;
01878 return ret;
01879 }
01880
01881
01882 static ChangeInfoResult CargoChangeInfo(uint cid, int numinfo, int prop, byte **bufp, int len)
01883 {
01884 byte *buf = *bufp;
01885 ChangeInfoResult ret = CIR_SUCCESS;
01886
01887 if (cid + numinfo > NUM_CARGO) {
01888 grfmsg(2, "CargoChangeInfo: Cargo type %d out of range (max %d)", cid + numinfo, NUM_CARGO - 1);
01889 return CIR_INVALID_ID;
01890 }
01891
01892 for (int i = 0; i < numinfo; i++) {
01893 CargoSpec *cs = &_cargo[cid + i];
01894
01895 switch (prop) {
01896 case 0x08:
01897 cs->bitnum = grf_load_byte(&buf);
01898 if (cs->IsValid()) {
01899 cs->grffile = _cur_grffile;
01900 SetBit(_cargo_mask, cid + i);
01901 } else {
01902 ClrBit(_cargo_mask, cid + i);
01903 }
01904 break;
01905
01906 case 0x09:
01907 cs->name = grf_load_word(&buf);
01908 _string_to_grf_mapping[&cs->name] = _cur_grffile->grfid;
01909 break;
01910
01911 case 0x0A:
01912 cs->name_single = grf_load_word(&buf);
01913 _string_to_grf_mapping[&cs->name_single] = _cur_grffile->grfid;
01914 break;
01915
01916 case 0x0B:
01917
01918
01919 cs->units_volume = grf_load_word(&buf);
01920 _string_to_grf_mapping[&cs->units_volume] = _cur_grffile->grfid;
01921 break;
01922
01923 case 0x0C:
01924 cs->quantifier = grf_load_word(&buf);
01925 _string_to_grf_mapping[&cs->quantifier] = _cur_grffile->grfid;
01926 break;
01927
01928 case 0x0D:
01929 cs->abbrev = grf_load_word(&buf);
01930 _string_to_grf_mapping[&cs->abbrev] = _cur_grffile->grfid;
01931 break;
01932
01933 case 0x0E:
01934 cs->sprite = grf_load_word(&buf);
01935 break;
01936
01937 case 0x0F:
01938 cs->weight = grf_load_byte(&buf);
01939 break;
01940
01941 case 0x10:
01942 cs->transit_days[0] = grf_load_byte(&buf);
01943 break;
01944
01945 case 0x11:
01946 cs->transit_days[1] = grf_load_byte(&buf);
01947 break;
01948
01949 case 0x12:
01950 cs->initial_payment = grf_load_dword(&buf);
01951 break;
01952
01953 case 0x13:
01954 cs->rating_colour = MapDOSColour(grf_load_byte(&buf));
01955 break;
01956
01957 case 0x14:
01958 cs->legend_colour = MapDOSColour(grf_load_byte(&buf));
01959 break;
01960
01961 case 0x15:
01962 cs->is_freight = (grf_load_byte(&buf) != 0);
01963 break;
01964
01965 case 0x16:
01966 cs->classes = grf_load_word(&buf);
01967 break;
01968
01969 case 0x17:
01970 cs->label = grf_load_dword(&buf);
01971 cs->label = BSWAP32(cs->label);
01972 break;
01973
01974 case 0x18: {
01975 uint8 substitute_type = grf_load_byte(&buf);
01976
01977 switch (substitute_type) {
01978 case 0x00: cs->town_effect = TE_PASSENGERS; break;
01979 case 0x02: cs->town_effect = TE_MAIL; break;
01980 case 0x05: cs->town_effect = TE_GOODS; break;
01981 case 0x09: cs->town_effect = TE_WATER; break;
01982 case 0x0B: cs->town_effect = TE_FOOD; break;
01983 default:
01984 grfmsg(1, "CargoChangeInfo: Unknown town growth substitute value %d, setting to none.", substitute_type);
01985 case 0xFF: cs->town_effect = TE_NONE; break;
01986 }
01987 } break;
01988
01989 case 0x19:
01990 cs->multipliertowngrowth = grf_load_word(&buf);
01991 break;
01992
01993 case 0x1A:
01994 cs->callback_mask = grf_load_byte(&buf);
01995 break;
01996
01997 default:
01998 ret = CIR_UNKNOWN;
01999 break;
02000 }
02001 }
02002
02003 *bufp = buf;
02004 return ret;
02005 }
02006
02007
02008 static ChangeInfoResult SoundEffectChangeInfo(uint sid, int numinfo, int prop, byte **bufp, int len)
02009 {
02010 byte *buf = *bufp;
02011 ChangeInfoResult ret = CIR_SUCCESS;
02012
02013 if (_cur_grffile->sound_offset == 0) {
02014 grfmsg(1, "SoundEffectChangeInfo: No effects defined, skipping");
02015 return CIR_INVALID_ID;
02016 }
02017
02018 for (int i = 0; i < numinfo; i++) {
02019 uint sound = sid + i + _cur_grffile->sound_offset - GetNumOriginalSounds();
02020
02021 if (sound >= GetNumSounds()) {
02022 grfmsg(1, "SoundEffectChangeInfo: Sound %d not defined (max %d)", sound, GetNumSounds());
02023 return CIR_INVALID_ID;
02024 }
02025
02026 switch (prop) {
02027 case 0x08:
02028 GetSound(sound)->volume = grf_load_byte(&buf);
02029 break;
02030
02031 case 0x09:
02032 GetSound(sound)->priority = grf_load_byte(&buf);
02033 break;
02034
02035 case 0x0A: {
02036 uint orig_sound = grf_load_byte(&buf);
02037
02038 if (orig_sound >= GetNumSounds()) {
02039 grfmsg(1, "SoundEffectChangeInfo: Original sound %d not defined (max %d)", orig_sound, GetNumSounds());
02040 } else {
02041 FileEntry *newfe = GetSound(sound);
02042 FileEntry *oldfe = GetSound(orig_sound);
02043
02044
02045 *oldfe = *newfe;
02046 }
02047 } break;
02048
02049 default:
02050 ret = CIR_UNKNOWN;
02051 break;
02052 }
02053 }
02054
02055 *bufp = buf;
02056 return ret;
02057 }
02058
02059 static ChangeInfoResult IndustrytilesChangeInfo(uint indtid, int numinfo, int prop, byte **bufp, int len)
02060 {
02061 byte *buf = *bufp;
02062 ChangeInfoResult ret = CIR_SUCCESS;
02063
02064 if (indtid + numinfo > NUM_INDUSTRYTILES) {
02065 grfmsg(1, "IndustryTilesChangeInfo: Too many industry tiles loaded (%u), max (%u). Ignoring.", indtid + numinfo, NUM_INDUSTRYTILES);
02066 return CIR_INVALID_ID;
02067 }
02068
02069
02070 if (_cur_grffile->indtspec == NULL) {
02071 _cur_grffile->indtspec = CallocT<IndustryTileSpec*>(NUM_INDUSTRYTILES);
02072 }
02073
02074 for (int i = 0; i < numinfo; i++) {
02075 IndustryTileSpec *tsp = _cur_grffile->indtspec[indtid + i];
02076
02077 if (prop != 0x08 && tsp == NULL) {
02078 grfmsg(2, "IndustryTilesChangeInfo: Attempt to modify undefined industry tile %u. Ignoring.", indtid + i);
02079 return CIR_INVALID_ID;
02080 }
02081
02082 switch (prop) {
02083 case 0x08: {
02084 IndustryTileSpec **tilespec = &_cur_grffile->indtspec[indtid + i];
02085 byte subs_id = grf_load_byte(&buf);
02086
02087 if (subs_id >= NEW_INDUSTRYTILEOFFSET) {
02088
02089 grfmsg(2, "IndustryTilesChangeInfo: Attempt to use new industry tile %u as substitute industry tile for %u. Ignoring.", subs_id, indtid + i);
02090 continue;
02091 }
02092
02093
02094 if (*tilespec == NULL) {
02095 int tempid;
02096 *tilespec = CallocT<IndustryTileSpec>(1);
02097 tsp = *tilespec;
02098
02099 memcpy(tsp, &_industry_tile_specs[subs_id], sizeof(_industry_tile_specs[subs_id]));
02100 tsp->enabled = true;
02101
02102
02103
02104
02105 tsp->anim_production = INDUSTRYTILE_NOANIM;
02106 tsp->anim_next = INDUSTRYTILE_NOANIM;
02107
02108 tsp->grf_prop.local_id = indtid + i;
02109 tsp->grf_prop.subst_id = subs_id;
02110 tsp->grf_prop.grffile = _cur_grffile;
02111 tempid = _industile_mngr.AddEntityID(indtid + i, _cur_grffile->grfid, subs_id);
02112 }
02113 } break;
02114
02115 case 0x09: {
02116 byte ovrid = grf_load_byte(&buf);
02117
02118
02119 if (ovrid >= NEW_INDUSTRYTILEOFFSET) {
02120 grfmsg(2, "IndustryTilesChangeInfo: Attempt to override new industry tile %u with industry tile id %u. Ignoring.", ovrid, indtid + i);
02121 continue;
02122 }
02123
02124 _industile_mngr.Add(indtid + i, _cur_grffile->grfid, ovrid);
02125 } break;
02126
02127 case 0x0A:
02128 case 0x0B:
02129 case 0x0C: {
02130 uint16 acctp = grf_load_word(&buf);
02131 tsp->accepts_cargo[prop - 0x0A] = GetCargoTranslation(GB(acctp, 0, 8), _cur_grffile);
02132 tsp->acceptance[prop - 0x0A] = GB(acctp, 8, 8);
02133 } break;
02134
02135 case 0x0D:
02136 tsp->slopes_refused = (Slope)grf_load_byte(&buf);
02137 break;
02138
02139 case 0x0E:
02140 tsp->callback_flags = grf_load_byte(&buf);
02141 break;
02142
02143 case 0x0F:
02144 tsp->animation_info = grf_load_word(&buf);
02145 break;
02146
02147 case 0x10:
02148 tsp->animation_speed = grf_load_byte(&buf);
02149 break;
02150
02151 case 0x11:
02152 tsp->animation_triggers = grf_load_byte(&buf);
02153 break;
02154
02155 case 0x12:
02156 tsp->animation_special_flags = grf_load_byte(&buf);
02157 break;
02158
02159 default:
02160 ret = CIR_UNKNOWN;
02161 break;
02162 }
02163 }
02164
02165 *bufp = buf;
02166 return ret;
02167 }
02168
02169 static ChangeInfoResult IndustriesChangeInfo(uint indid, int numinfo, int prop, byte **bufp, int len)
02170 {
02171 byte *buf = *bufp;
02172 ChangeInfoResult ret = CIR_SUCCESS;
02173
02174 if (indid + numinfo > NUM_INDUSTRYTYPES) {
02175 grfmsg(1, "IndustriesChangeInfo: Too many industries loaded (%u), max (%u). Ignoring.", indid + numinfo, NUM_INDUSTRYTYPES);
02176 return CIR_INVALID_ID;
02177 }
02178
02179 grfmsg(1, "IndustriesChangeInfo: newid %u", indid);
02180
02181
02182 if (_cur_grffile->industryspec == NULL) {
02183 _cur_grffile->industryspec = CallocT<IndustrySpec*>(NUM_INDUSTRYTYPES);
02184 }
02185
02186 for (int i = 0; i < numinfo; i++) {
02187 IndustrySpec *indsp = _cur_grffile->industryspec[indid + i];
02188
02189 if (prop != 0x08 && indsp == NULL) {
02190 grfmsg(2, "IndustriesChangeInfo: Attempt to modify undefined industry %u. Ignoring.", indid + i);
02191 return CIR_INVALID_ID;
02192 }
02193
02194 switch (prop) {
02195 case 0x08: {
02196 IndustrySpec **indspec = &_cur_grffile->industryspec[indid + i];
02197 byte subs_id = grf_load_byte(&buf);
02198
02199 if (subs_id == 0xFF) {
02200
02201
02202 _industry_specs[indid + i].enabled = false;
02203 continue;
02204 } else if (subs_id >= NEW_INDUSTRYOFFSET) {
02205
02206 grfmsg(2, "_industry_specs: Attempt to use new industry %u as substitute industry for %u. Ignoring.", subs_id, indid + i);
02207 continue;
02208 }
02209
02210
02211
02212
02213 if (*indspec == NULL) {
02214 *indspec = CallocT<IndustrySpec>(1);
02215 indsp = *indspec;
02216
02217 memcpy(indsp, &_origin_industry_specs[subs_id], sizeof(_industry_specs[subs_id]));
02218 indsp->enabled = true;
02219 indsp->grf_prop.local_id = indid + i;
02220 indsp->grf_prop.subst_id = subs_id;
02221 indsp->grf_prop.grffile = _cur_grffile;
02222
02223
02224 indsp->check_proc = CHECK_NOTHING;
02225 }
02226 } break;
02227
02228 case 0x09: {
02229 byte ovrid = grf_load_byte(&buf);
02230
02231
02232 if (ovrid >= NEW_INDUSTRYOFFSET) {
02233 grfmsg(2, "IndustriesChangeInfo: Attempt to override new industry %u with industry id %u. Ignoring.", ovrid, indid + i);
02234 continue;
02235 }
02236 indsp->grf_prop.override = ovrid;
02237 _industry_mngr.Add(indid + i, _cur_grffile->grfid, ovrid);
02238 } break;
02239
02240 case 0x0A: {
02241 indsp->num_table = grf_load_byte(&buf);
02242 uint32 defsize = grf_load_dword(&buf);
02243 IndustryTileTable **tile_table = CallocT<IndustryTileTable*>(indsp->num_table);
02244 IndustryTileTable *itt = CallocT<IndustryTileTable>(defsize);
02245 int size;
02246 IndustryTileTable *copy_from;
02247
02248 for (byte j = 0; j < indsp->num_table; j++) {
02249 for (int k = 0;; k++) {
02250 itt[k].ti.x = grf_load_byte(&buf);
02251
02252 if (itt[k].ti.x == 0xFE && k == 0) {
02253
02254 IndustryType type = grf_load_byte(&buf);
02255 byte laynbr = grf_load_byte(&buf);
02256
02257 copy_from = (IndustryTileTable*)_origin_industry_specs[type].table[laynbr];
02258 for (size = 1;; size++) {
02259 if (copy_from[size - 1].ti.x == -0x80 && copy_from[size - 1].ti.y == 0) break;
02260 }
02261 break;
02262 }
02263
02264 itt[k].ti.y = grf_load_byte(&buf);
02265
02266 if (itt[k].ti.x == 0 && itt[k].ti.y == 0x80) {
02267
02268
02269 itt[k].ti.x = -0x80;
02270 itt[k].ti.y = 0;
02271 itt[k].gfx = 0;
02272
02273 size = k + 1;
02274 copy_from = itt;
02275 break;
02276 }
02277
02278 itt[k].gfx = grf_load_byte(&buf);
02279
02280 if (itt[k].gfx == 0xFE) {
02281
02282 int local_tile_id = grf_load_word(&buf);
02283
02284
02285 int tempid = _industile_mngr.GetID(local_tile_id, _cur_grffile->grfid);
02286
02287 if (tempid == INVALID_INDUSTRYTILE) {
02288 grfmsg(2, "IndustriesChangeInfo: Attempt to use industry tile %u with industry id %u, not yet defined. Ignoring.", local_tile_id, indid);
02289 } else {
02290
02291 itt[k].gfx = tempid;
02292 size = k + 1;
02293 copy_from = itt;
02294 }
02295 } else if (itt[k].gfx == 0xFF) {
02296 itt[k].ti.x = (int8)GB(itt[k].ti.x, 0, 8);
02297 itt[k].ti.y = (int8)GB(itt[k].ti.y, 0, 8);
02298 }
02299 }
02300 tile_table[j] = CallocT<IndustryTileTable>(size);
02301 memcpy(tile_table[j], copy_from, sizeof(*copy_from) * size);
02302 }
02303
02304 indsp->table = tile_table;
02305 SetBit(indsp->cleanup_flag, 1);
02306 free(itt);
02307 } break;
02308
02309 case 0x0B:
02310 indsp->life_type = (IndustryLifeType)grf_load_byte(&buf);
02311 break;
02312
02313 case 0x0C:
02314 indsp->closure_text = grf_load_word(&buf);
02315 _string_to_grf_mapping[&indsp->closure_text] = _cur_grffile->grfid;
02316 break;
02317
02318 case 0x0D:
02319 indsp->production_up_text = grf_load_word(&buf);
02320 _string_to_grf_mapping[&indsp->production_up_text] = _cur_grffile->grfid;
02321 break;
02322
02323 case 0x0E:
02324 indsp->production_down_text = grf_load_word(&buf);
02325 _string_to_grf_mapping[&indsp->production_down_text] = _cur_grffile->grfid;
02326 break;
02327
02328 case 0x0F:
02329 indsp->cost_multiplier = grf_load_byte(&buf);
02330 break;
02331
02332 case 0x10:
02333 for (byte j = 0; j < 2; j++) {
02334 indsp->produced_cargo[j] = GetCargoTranslation(grf_load_byte(&buf), _cur_grffile);
02335 }
02336 break;
02337
02338 case 0x11:
02339 for (byte j = 0; j < 3; j++) {
02340 indsp->accepts_cargo[j] = GetCargoTranslation(grf_load_byte(&buf), _cur_grffile);
02341 }
02342 grf_load_byte(&buf);
02343 break;
02344
02345 case 0x12:
02346 case 0x13:
02347 indsp->production_rate[prop - 0x12] = grf_load_byte(&buf);
02348 break;
02349
02350 case 0x14:
02351 indsp->minimal_cargo = grf_load_byte(&buf);
02352 break;
02353
02354 case 0x15: {
02355 indsp->number_of_sounds = grf_load_byte(&buf);
02356 uint8 *sounds = MallocT<uint8>(indsp->number_of_sounds);
02357
02358 for (uint8 j = 0; j < indsp->number_of_sounds; j++) sounds[j] = grf_load_byte(&buf);
02359 indsp->random_sounds = sounds;
02360 SetBit(indsp->cleanup_flag, 0);
02361 } break;
02362
02363 case 0x16:
02364 for (byte j = 0; j < 3; j++) indsp->conflicting[j] = grf_load_byte(&buf);
02365 break;
02366
02367 case 0x17:
02368 indsp->appear_creation[_settings_game.game_creation.landscape] = grf_load_byte(&buf);
02369 break;
02370
02371 case 0x18:
02372 indsp->appear_ingame[_settings_game.game_creation.landscape] = grf_load_byte(&buf);
02373 break;
02374
02375 case 0x19:
02376 indsp->map_colour = MapDOSColour(grf_load_byte(&buf));
02377 break;
02378
02379 case 0x1A:
02380 indsp->behaviour = (IndustryBehaviour)grf_load_dword(&buf);
02381 break;
02382
02383 case 0x1B:
02384 indsp->new_industry_text = grf_load_word(&buf);
02385 _string_to_grf_mapping[&indsp->new_industry_text] = _cur_grffile->grfid;
02386 break;
02387
02388 case 0x1C:
02389 case 0x1D:
02390 case 0x1E: {
02391 uint32 multiples = grf_load_dword(&buf);
02392 indsp->input_cargo_multiplier[prop - 0x1C][0] = GB(multiples, 0, 16);
02393 indsp->input_cargo_multiplier[prop - 0x1C][1] = GB(multiples, 16, 16);
02394 } break;
02395
02396 case 0x1F:
02397 indsp->name = grf_load_word(&buf);
02398 _string_to_grf_mapping[&indsp->name] = _cur_grffile->grfid;
02399 break;
02400
02401 case 0x20:
02402 indsp->prospecting_chance = grf_load_dword(&buf);
02403 break;
02404
02405 case 0x21:
02406 case 0x22: {
02407 byte aflag = grf_load_byte(&buf);
02408 SB(indsp->callback_flags, (prop - 0x21) * 8, 8, aflag);
02409 } break;
02410
02411 case 0x23:
02412 indsp->removal_cost_multiplier = grf_load_dword(&buf);
02413 break;
02414
02415 case 0x24:
02416 indsp->station_name = grf_load_word(&buf);
02417 _string_to_grf_mapping[&indsp->station_name] = _cur_grffile->grfid;
02418 break;
02419
02420 default:
02421 ret = CIR_UNKNOWN;
02422 break;
02423 }
02424 }
02425
02426 *bufp = buf;
02427 return ret;
02428 }
02429
02430 static bool HandleChangeInfoResult(const char *caller, ChangeInfoResult cir, uint8 feature, uint8 property)
02431 {
02432 switch (cir) {
02433 default: NOT_REACHED();
02434
02435 case CIR_SUCCESS:
02436 return false;
02437
02438 case CIR_UNHANDLED:
02439 grfmsg(1, "%s: Ignoring property 0x%02X of feature 0x%02X (not implemented)", caller, property, feature);
02440 return false;
02441
02442 case CIR_UNKNOWN:
02443 grfmsg(0, "%s: Unknown property 0x%02X of feature 0x%02X, disabling", caller, property, feature);
02444
02445
02446 case CIR_INVALID_ID:
02447
02448 _skip_sprites = -1;
02449 _cur_grfconfig->status = GCS_DISABLED;
02450 _cur_grfconfig->error = CallocT<GRFError>(1);
02451 _cur_grfconfig->error->severity = STR_NEWGRF_ERROR_MSG_FATAL;
02452 _cur_grfconfig->error->message = (cir == CIR_INVALID_ID) ? STR_NEWGRF_ERROR_INVALID_ID : STR_NEWGRF_ERROR_UNKNOWN_PROPERTY;
02453 return true;
02454 }
02455 }
02456
02457
02458 static void FeatureChangeInfo(byte *buf, size_t len)
02459 {
02460 byte *bufend = buf + len;
02461
02462
02463
02464
02465
02466
02467
02468
02469
02470
02471
02472
02473
02474
02475 static const VCI_Handler handler[] = {
02476 RailVehicleChangeInfo,
02477 RoadVehicleChangeInfo,
02478 ShipVehicleChangeInfo,
02479 AircraftVehicleChangeInfo,
02480 StationChangeInfo,
02481 CanalChangeInfo,
02482 BridgeChangeInfo,
02483 TownHouseChangeInfo,
02484 GlobalVarChangeInfo,
02485 IndustrytilesChangeInfo,
02486 IndustriesChangeInfo,
02487 NULL,
02488 SoundEffectChangeInfo,
02489 };
02490
02491 if (!check_length(len, 6, "FeatureChangeInfo")) return;
02492 buf++;
02493 uint8 feature = grf_load_byte(&buf);
02494 uint8 numprops = grf_load_byte(&buf);
02495 uint numinfo = grf_load_byte(&buf);
02496 uint engine = grf_load_extended(&buf);
02497
02498 grfmsg(6, "FeatureChangeInfo: feature %d, %d properties, to apply to %d+%d",
02499 feature, numprops, engine, numinfo);
02500
02501 if (feature >= lengthof(handler) || handler[feature] == NULL) {
02502 grfmsg(1, "FeatureChangeInfo: Unsupported feature %d, skipping", feature);
02503 return;
02504 }
02505
02506 while (numprops-- && buf < bufend) {
02507 uint8 prop = grf_load_byte(&buf);
02508
02509 ChangeInfoResult cir = handler[feature](engine, numinfo, prop, &buf, bufend - buf);
02510 if (HandleChangeInfoResult("FeatureChangeInfo", cir, feature, prop)) return;
02511 }
02512 }
02513
02514
02515 static void SafeChangeInfo(byte *buf, size_t len)
02516 {
02517 if (!check_length(len, 6, "SafeChangeInfo")) return;
02518 buf++;
02519 uint8 feature = grf_load_byte(&buf);
02520 uint8 numprops = grf_load_byte(&buf);
02521 uint numinfo = grf_load_byte(&buf);
02522 grf_load_extended(&buf);
02523
02524 if (feature == GSF_BRIDGE && numprops == 1) {
02525 uint8 prop = grf_load_byte(&buf);
02526
02527
02528 if (prop == 0x0D) return;
02529 } else if (feature == GSF_GLOBALVAR && numprops == 1) {
02530 uint8 prop = grf_load_byte(&buf);
02531
02532 if (prop == 0x11) {
02533 bool is_safe = true;
02534 for (uint i = 0; i < numinfo; i++) {
02535 uint32 s = grf_load_dword(&buf);
02536 grf_load_dword(&buf);
02537 const GRFConfig *grfconfig = GetGRFConfig(s);
02538 if (grfconfig != NULL && !HasBit(grfconfig->flags, GCF_STATIC)) {
02539 is_safe = false;
02540 break;
02541 }
02542 }
02543 if (is_safe) return;
02544 }
02545 }
02546
02547 SetBit(_cur_grfconfig->flags, GCF_UNSAFE);
02548
02549
02550 _skip_sprites = -1;
02551 }
02552
02553
02554 static void ReserveChangeInfo(byte *buf, size_t len)
02555 {
02556 byte *bufend = buf + len;
02557
02558 if (!check_length(len, 6, "ReserveChangeInfo")) return;
02559 buf++;
02560 uint8 feature = grf_load_byte(&buf);
02561
02562 if (feature != GSF_CARGOS && feature != GSF_GLOBALVAR) return;
02563
02564 uint8 numprops = grf_load_byte(&buf);
02565 uint8 numinfo = grf_load_byte(&buf);
02566 uint8 index = grf_load_extended(&buf);
02567
02568 while (numprops-- && buf < bufend) {
02569 uint8 prop = grf_load_byte(&buf);
02570 ChangeInfoResult cir = CIR_SUCCESS;
02571
02572 switch (feature) {
02573 default: NOT_REACHED();
02574 case GSF_CARGOS:
02575 cir = CargoChangeInfo(index, numinfo, prop, &buf, bufend - buf);
02576 break;
02577
02578 case GSF_GLOBALVAR:
02579 cir = GlobalVarReserveInfo(index, numinfo, prop, &buf, bufend - buf);
02580 break;
02581 }
02582
02583 if (HandleChangeInfoResult("ReserveChangeInfo", cir, feature, prop)) return;
02584 }
02585 }
02586
02592 static const SpriteGroup *NewCallBackResultSpriteGroup(uint16 value)
02593 {
02594 SpriteGroup *group = AllocateSpriteGroup();
02595
02596 group->type = SGT_CALLBACK;
02597
02598
02599
02600 if ((value >> 8) == 0xFF) {
02601 value &= ~0xFF00;
02602 } else {
02603 value &= ~0x8000;
02604 }
02605
02606 group->g.callback.result = value;
02607
02608 return group;
02609 }
02610
02617 static const SpriteGroup *NewResultSpriteGroup(SpriteID sprite, byte num_sprites)
02618 {
02619 SpriteGroup *group = AllocateSpriteGroup();
02620 group->type = SGT_RESULT;
02621 group->g.result.sprite = sprite;
02622 group->g.result.num_sprites = num_sprites;
02623 return group;
02624 }
02625
02626
02627 static void NewSpriteSet(byte *buf, size_t len)
02628 {
02629
02630
02631
02632
02633
02634
02635
02636
02637
02638
02639
02640
02641 if (!check_length(len, 4, "NewSpriteSet")) return;
02642 buf++;
02643 uint8 feature = grf_load_byte(&buf);
02644 uint8 num_sets = grf_load_byte(&buf);
02645 uint16 num_ents = grf_load_extended(&buf);
02646
02647 _cur_grffile->spriteset_start = _cur_spriteid;
02648 _cur_grffile->spriteset_feature = feature;
02649 _cur_grffile->spriteset_numsets = num_sets;
02650 _cur_grffile->spriteset_numents = num_ents;
02651
02652 grfmsg(7, "New sprite set at %d of type %d, consisting of %d sets with %d views each (total %d)",
02653 _cur_spriteid, feature, num_sets, num_ents, num_sets * num_ents
02654 );
02655
02656 for (int i = 0; i < num_sets * num_ents; i++) {
02657 _nfo_line++;
02658 LoadNextSprite(_cur_spriteid++, _file_index, _nfo_line);
02659 }
02660 }
02661
02662
02663 static void SkipAct1(byte *buf, size_t len)
02664 {
02665 if (!check_length(len, 4, "SkipAct1")) return;
02666 buf++;
02667 grf_load_byte(&buf);
02668 uint8 num_sets = grf_load_byte(&buf);
02669 uint16 num_ents = grf_load_extended(&buf);
02670
02671 _skip_sprites = num_sets * num_ents;
02672
02673 grfmsg(3, "SkipAct1: Skipping %d sprites", _skip_sprites);
02674 }
02675
02676
02677
02678 static const SpriteGroup *GetGroupFromGroupID(byte setid, byte type, uint16 groupid)
02679 {
02680 if (HasBit(groupid, 15)) return NewCallBackResultSpriteGroup(groupid);
02681
02682 if (groupid >= _cur_grffile->spritegroups_count || _cur_grffile->spritegroups[groupid] == NULL) {
02683 grfmsg(1, "GetGroupFromGroupID(0x%02X:0x%02X): Groupid 0x%04X does not exist, leaving empty", setid, type, groupid);
02684 return NULL;
02685 }
02686
02687 return _cur_grffile->spritegroups[groupid];
02688 }
02689
02690
02691 static const SpriteGroup *CreateGroupFromGroupID(byte feature, byte setid, byte type, uint16 spriteid, uint16 num_sprites)
02692 {
02693 if (HasBit(spriteid, 15)) return NewCallBackResultSpriteGroup(spriteid);
02694
02695 if (spriteid >= _cur_grffile->spriteset_numsets) {
02696 grfmsg(1, "CreateGroupFromGroupID(0x%02X:0x%02X): Sprite set %u invalid, max %u", setid, type, spriteid, _cur_grffile->spriteset_numsets);
02697 return NULL;
02698 }
02699
02700
02701
02702
02703 if (_cur_grffile->spriteset_start + spriteid * num_sprites + num_sprites > _cur_spriteid) {
02704 grfmsg(1, "CreateGroupFromGroupID(0x%02X:0x%02X): Real Sprite IDs 0x%04X - 0x%04X do not (all) exist (max 0x%04X), leaving empty",
02705 setid, type,
02706 _cur_grffile->spriteset_start + spriteid * num_sprites,
02707 _cur_grffile->spriteset_start + spriteid * num_sprites + num_sprites - 1, _cur_spriteid - 1);
02708 return NULL;
02709 }
02710
02711 if (feature != _cur_grffile->spriteset_feature) {
02712 grfmsg(1, "CreateGroupFromGroupID(0x%02X:0x%02X): Sprite set feature 0x%02X does not match action feature 0x%02X, skipping",
02713 setid, type,
02714 _cur_grffile->spriteset_feature, feature);
02715 return NULL;
02716 }
02717
02718 return NewResultSpriteGroup(_cur_grffile->spriteset_start + spriteid * num_sprites, num_sprites);
02719 }
02720
02721
02722 static void NewSpriteGroup(byte *buf, size_t len)
02723 {
02724
02725
02726
02727
02728
02729
02730
02731
02732
02733
02734 SpriteGroup *group = NULL;
02735 byte *bufend = buf + len;
02736
02737 if (!check_length(len, 5, "NewSpriteGroup")) return;
02738 buf++;
02739
02740 uint8 feature = grf_load_byte(&buf);
02741 uint8 setid = grf_load_byte(&buf);
02742 uint8 type = grf_load_byte(&buf);
02743
02744 if (setid >= _cur_grffile->spritegroups_count) {
02745
02746 _cur_grffile->spritegroups = ReallocT(_cur_grffile->spritegroups, setid + 1);
02747
02748 for (; _cur_grffile->spritegroups_count < (setid + 1); _cur_grffile->spritegroups_count++)
02749 _cur_grffile->spritegroups[_cur_grffile->spritegroups_count] = NULL;
02750 }
02751
02752 switch (type) {
02753
02754 case 0x81:
02755 case 0x82:
02756 case 0x85:
02757 case 0x86:
02758 case 0x89:
02759 case 0x8A:
02760 {
02761 byte varadjust;
02762 byte varsize;
02763
02764
02765 if (!check_length(bufend - buf, 1, "NewSpriteGroup (Deterministic) (1)")) return;
02766
02767 group = AllocateSpriteGroup();
02768 group->type = SGT_DETERMINISTIC;
02769 group->g.determ.var_scope = HasBit(type, 1) ? VSG_SCOPE_PARENT : VSG_SCOPE_SELF;
02770
02771 switch (GB(type, 2, 2)) {
02772 default: NOT_REACHED();
02773 case 0: group->g.determ.size = DSG_SIZE_BYTE; varsize = 1; break;
02774 case 1: group->g.determ.size = DSG_SIZE_WORD; varsize = 2; break;
02775 case 2: group->g.determ.size = DSG_SIZE_DWORD; varsize = 4; break;
02776 }
02777
02778 if (!check_length(bufend - buf, 5 + varsize, "NewSpriteGroup (Deterministic) (2)")) return;
02779
02780
02781
02782 do {
02783 DeterministicSpriteGroupAdjust *adjust;
02784
02785 if (group->g.determ.num_adjusts > 0) {
02786 if (!check_length(bufend - buf, 2 + varsize + 3, "NewSpriteGroup (Deterministic) (3)")) return;
02787 }
02788
02789 group->g.determ.num_adjusts++;
02790 group->g.determ.adjusts = ReallocT(group->g.determ.adjusts, group->g.determ.num_adjusts);
02791
02792 adjust = &group->g.determ.adjusts[group->g.determ.num_adjusts - 1];
02793
02794
02795 adjust->operation = group->g.determ.num_adjusts == 1 ? DSGA_OP_ADD : (DeterministicSpriteGroupAdjustOperation)grf_load_byte(&buf);
02796 adjust->variable = grf_load_byte(&buf);
02797 if (adjust->variable == 0x7E) {
02798
02799 adjust->subroutine = GetGroupFromGroupID(setid, type, grf_load_byte(&buf));
02800 } else {
02801 adjust->parameter = IsInsideMM(adjust->variable, 0x60, 0x80) ? grf_load_byte(&buf) : 0;
02802 }
02803
02804 varadjust = grf_load_byte(&buf);
02805 adjust->shift_num = GB(varadjust, 0, 5);
02806 adjust->type = (DeterministicSpriteGroupAdjustType)GB(varadjust, 6, 2);
02807 adjust->and_mask = grf_load_var(varsize, &buf);
02808
02809 if (adjust->type != DSGA_TYPE_NONE) {
02810 adjust->add_val = grf_load_var(varsize, &buf);
02811 adjust->divmod_val = grf_load_var(varsize, &buf);
02812 } else {
02813 adjust->add_val = 0;
02814 adjust->divmod_val = 0;
02815 }
02816
02817
02818 } while (HasBit(varadjust, 5));
02819
02820 group->g.determ.num_ranges = grf_load_byte(&buf);
02821 if (group->g.determ.num_ranges > 0) group->g.determ.ranges = CallocT<DeterministicSpriteGroupRange>(group->g.determ.num_ranges);
02822
02823 if (!check_length(bufend - buf, 2 + (2 + 2 * varsize) * group->g.determ.num_ranges, "NewSpriteGroup (Deterministic)")) return;
02824
02825 for (uint i = 0; i < group->g.determ.num_ranges; i++) {
02826 group->g.determ.ranges[i].group = GetGroupFromGroupID(setid, type, grf_load_word(&buf));
02827 group->g.determ.ranges[i].low = grf_load_var(varsize, &buf);
02828 group->g.determ.ranges[i].high = grf_load_var(varsize, &buf);
02829 }
02830
02831 group->g.determ.default_group = GetGroupFromGroupID(setid, type, grf_load_word(&buf));
02832 break;
02833 }
02834
02835
02836 case 0x80:
02837 case 0x83:
02838 case 0x84:
02839 {
02840 if (!check_length(bufend - buf, HasBit(type, 2) ? 8 : 7, "NewSpriteGroup (Randomized) (1)")) return;
02841
02842 group = AllocateSpriteGroup();
02843 group->type = SGT_RANDOMIZED;
02844 group->g.random.var_scope = HasBit(type, 1) ? VSG_SCOPE_PARENT : VSG_SCOPE_SELF;
02845
02846 if (HasBit(type, 2)) {
02847 if (feature <= GSF_AIRCRAFT) group->g.random.var_scope = VSG_SCOPE_RELATIVE;
02848 group->g.random.count = grf_load_byte(&buf);
02849 }
02850
02851 uint8 triggers = grf_load_byte(&buf);
02852 group->g.random.triggers = GB(triggers, 0, 7);
02853 group->g.random.cmp_mode = HasBit(triggers, 7) ? RSG_CMP_ALL : RSG_CMP_ANY;
02854 group->g.random.lowest_randbit = grf_load_byte(&buf);
02855 group->g.random.num_groups = grf_load_byte(&buf);
02856 group->g.random.groups = CallocT<const SpriteGroup*>(group->g.random.num_groups);
02857
02858 if (!check_length(bufend - buf, 2 * group->g.random.num_groups, "NewSpriteGroup (Randomized) (2)")) return;
02859
02860 for (uint i = 0; i < group->g.random.num_groups; i++) {
02861 group->g.random.groups[i] = GetGroupFromGroupID(setid, type, grf_load_word(&buf));
02862 }
02863
02864 break;
02865 }
02866
02867
02868 default:
02869 {
02870
02871
02872 switch (feature) {
02873 case GSF_TRAIN:
02874 case GSF_ROAD:
02875 case GSF_SHIP:
02876 case GSF_AIRCRAFT:
02877 case GSF_STATION:
02878 case GSF_CANAL:
02879 case GSF_CARGOS:
02880 {
02881 byte sprites = _cur_grffile->spriteset_numents;
02882 byte num_loaded = type;
02883 byte num_loading = grf_load_byte(&buf);
02884
02885 if (_cur_grffile->spriteset_start == 0) {
02886 grfmsg(0, "NewSpriteGroup: No sprite set to work on! Skipping");
02887 return;
02888 }
02889
02890 if (!check_length(bufend - buf, 2 * num_loaded + 2 * num_loading, "NewSpriteGroup (Real) (1)")) return;
02891
02892 group = AllocateSpriteGroup();
02893 group->type = SGT_REAL;
02894
02895 group->g.real.num_loaded = num_loaded;
02896 group->g.real.num_loading = num_loading;
02897 if (num_loaded > 0) group->g.real.loaded = CallocT<const SpriteGroup*>(num_loaded);
02898 if (num_loading > 0) group->g.real.loading = CallocT<const SpriteGroup*>(num_loading);
02899
02900 grfmsg(6, "NewSpriteGroup: New SpriteGroup 0x%02X, %u views, %u loaded, %u loading",
02901 setid, sprites, num_loaded, num_loading);
02902
02903 for (uint i = 0; i < num_loaded; i++) {
02904 uint16 spriteid = grf_load_word(&buf);
02905 group->g.real.loaded[i] = CreateGroupFromGroupID(feature, setid, type, spriteid, sprites);
02906 grfmsg(8, "NewSpriteGroup: + rg->loaded[%i] = subset %u", i, spriteid);
02907 }
02908
02909 for (uint i = 0; i < num_loading; i++) {
02910 uint16 spriteid = grf_load_word(&buf);
02911 group->g.real.loading[i] = CreateGroupFromGroupID(feature, setid, type, spriteid, sprites);
02912 grfmsg(8, "NewSpriteGroup: + rg->loading[%i] = subset %u", i, spriteid);
02913 }
02914
02915 break;
02916 }
02917
02918 case GSF_TOWNHOUSE:
02919 case GSF_INDUSTRYTILES: {
02920 byte sprites = _cur_grffile->spriteset_numents;
02921 byte num_sprites = max((uint8)1, type);
02922 uint i;
02923
02924 group = AllocateSpriteGroup();
02925 group->type = SGT_TILELAYOUT;
02926 group->g.layout.num_sprites = sprites;
02927 group->g.layout.dts = CallocT<DrawTileSprites>(1);
02928
02929
02930 group->g.layout.dts->ground.sprite = grf_load_word(&buf);
02931 group->g.layout.dts->ground.pal = grf_load_word(&buf);
02932
02933
02934 MapSpriteMappingRecolour(&group->g.layout.dts->ground);
02935
02936 if (HasBit(group->g.layout.dts->ground.pal, 15)) {
02937
02938
02939 SpriteID sprite = _cur_grffile->spriteset_start + GB(group->g.layout.dts->ground.sprite, 0, 14) * sprites;
02940 SB(group->g.layout.dts->ground.sprite, 0, SPRITE_WIDTH, sprite);
02941 ClrBit(group->g.layout.dts->ground.pal, 15);
02942 }
02943
02944 group->g.layout.dts->seq = CallocT<DrawTileSeqStruct>(num_sprites + 1);
02945
02946 for (i = 0; i < num_sprites; i++) {
02947 DrawTileSeqStruct *seq = (DrawTileSeqStruct*)&group->g.layout.dts->seq[i];
02948
02949 seq->image.sprite = grf_load_word(&buf);
02950 seq->image.pal = grf_load_word(&buf);
02951 seq->delta_x = grf_load_byte(&buf);
02952 seq->delta_y = grf_load_byte(&buf);
02953
02954 MapSpriteMappingRecolour(&seq->image);
02955
02956 if (HasBit(seq->image.pal, 15)) {
02957
02958
02959 SpriteID sprite = _cur_grffile->spriteset_start + GB(seq->image.sprite, 0, 14) * sprites;
02960 SB(seq->image.sprite, 0, SPRITE_WIDTH, sprite);
02961 ClrBit(seq->image.pal, 15);
02962 }
02963
02964 if (type > 0) {
02965 seq->delta_z = grf_load_byte(&buf);
02966 if ((byte)seq->delta_z == 0x80) continue;
02967 }
02968
02969 seq->size_x = grf_load_byte(&buf);
02970 seq->size_y = grf_load_byte(&buf);
02971 seq->size_z = grf_load_byte(&buf);
02972 }
02973
02974
02975 ((DrawTileSeqStruct*)group->g.layout.dts->seq)[i].delta_x = (byte)0x80;
02976
02977 break;
02978 }
02979
02980 case GSF_INDUSTRIES: {
02981 if (type > 1) {
02982 grfmsg(1, "NewSpriteGroup: Unsupported industry production version %d, skipping", type);
02983 break;
02984 }
02985
02986 group = AllocateSpriteGroup();
02987 group->type = SGT_INDUSTRY_PRODUCTION;
02988 group->g.indprod.version = type;
02989 if (type == 0) {
02990 for (uint i = 0; i < 3; i++) {
02991 group->g.indprod.substract_input[i] = grf_load_word(&buf);
02992 }
02993 for (uint i = 0; i < 2; i++) {
02994 group->g.indprod.add_output[i] = grf_load_word(&buf);
02995 }
02996 group->g.indprod.again = grf_load_byte(&buf);
02997 } else {
02998 for (uint i = 0; i < 3; i++) {
02999 group->g.indprod.substract_input[i] = grf_load_byte(&buf);
03000 }
03001 for (uint i = 0; i < 2; i++) {
03002 group->g.indprod.add_output[i] = grf_load_byte(&buf);
03003 }
03004 group->g.indprod.again = grf_load_byte(&buf);
03005 }
03006 break;
03007 }
03008
03009
03010 default: grfmsg(1, "NewSpriteGroup: Unsupported feature %d, skipping", feature);
03011 }
03012 }
03013 }
03014
03015 _cur_grffile->spritegroups[setid] = group;
03016 }
03017
03018 static CargoID TranslateCargo(uint8 feature, uint8 ctype)
03019 {
03020
03021 if (feature == GSF_STATION && ctype == 0xFE) return CT_DEFAULT_NA;
03022 if (ctype == 0xFF) return CT_PURCHASE;
03023
03024 if (_cur_grffile->cargo_max == 0) {
03025
03026 if (ctype >= 32) {
03027 grfmsg(1, "TranslateCargo: Cargo bitnum %d out of range (max 31), skipping.", ctype);
03028 return CT_INVALID;
03029 }
03030
03031 for (CargoID c = 0; c < NUM_CARGO; c++) {
03032 const CargoSpec *cs = GetCargo(c);
03033 if (!cs->IsValid()) continue;
03034
03035 if (cs->bitnum == ctype) {
03036 grfmsg(6, "TranslateCargo: Cargo bitnum %d mapped to cargo type %d.", ctype, c);
03037 return c;
03038 }
03039 }
03040
03041 grfmsg(5, "TranslateCargo: Cargo bitnum %d not available in this climate, skipping.", ctype);
03042 return CT_INVALID;
03043 }
03044
03045
03046 if (ctype >= _cur_grffile->cargo_max) {
03047 grfmsg(1, "TranslateCargo: Cargo type %d out of range (max %d), skipping.", ctype, _cur_grffile->cargo_max - 1);
03048 return CT_INVALID;
03049 }
03050
03051
03052 CargoLabel cl = _cur_grffile->cargo_list[ctype];
03053 if (cl == 0) {
03054 grfmsg(5, "TranslateCargo: Cargo type %d not available in this climate, skipping.", ctype);
03055 return CT_INVALID;
03056 }
03057
03058 ctype = GetCargoIDByLabel(cl);
03059 if (ctype == CT_INVALID) {
03060 grfmsg(5, "TranslateCargo: Cargo '%c%c%c%c' unsupported, skipping.", GB(cl, 24, 8), GB(cl, 16, 8), GB(cl, 8, 8), GB(cl, 0, 8));
03061 return CT_INVALID;
03062 }
03063
03064 grfmsg(6, "TranslateCargo: Cargo '%c%c%c%c' mapped to cargo type %d.", GB(cl, 24, 8), GB(cl, 16, 8), GB(cl, 8, 8), GB(cl, 0, 8), ctype);
03065 return ctype;
03066 }
03067
03068
03069 static bool IsValidGroupID(uint16 groupid, const char *function)
03070 {
03071 if (groupid >= _cur_grffile->spritegroups_count || _cur_grffile->spritegroups[groupid] == NULL) {
03072 grfmsg(1, "%s: Spriteset 0x%04X out of range (maximum 0x%02X) or empty, skipping.", function, groupid, _cur_grffile->spritegroups_count - 1);
03073 return false;
03074 }
03075
03076 return true;
03077 }
03078
03079 static void VehicleMapSpriteGroup(byte *buf, byte feature, uint8 idcount)
03080 {
03081 static EngineID *last_engines;
03082 static uint last_engines_count;
03083 bool wagover = false;
03084
03085
03086 if (HasBit(idcount, 7)) {
03087 wagover = true;
03088
03089 idcount = GB(idcount, 0, 7);
03090
03091 if (last_engines_count == 0) {
03092 grfmsg(0, "VehicleMapSpriteGroup: WagonOverride: No engine to do override with");
03093 return;
03094 }
03095
03096 grfmsg(6, "VehicleMapSpriteGroup: WagonOverride: %u engines, %u wagons",
03097 last_engines_count, idcount);
03098 } else {
03099 if (last_engines_count != idcount) {
03100 last_engines = ReallocT(last_engines, idcount);
03101 last_engines_count = idcount;
03102 }
03103 }
03104
03105 EngineID *engines = AllocaM(EngineID, idcount);
03106 for (uint i = 0; i < idcount; i++) {
03107 engines[i] = GetNewEngine(_cur_grffile, (VehicleType)feature, grf_load_extended(&buf))->index;
03108 if (!wagover) last_engines[i] = engines[i];
03109 }
03110
03111 uint8 cidcount = grf_load_byte(&buf);
03112 for (uint c = 0; c < cidcount; c++) {
03113 uint8 ctype = grf_load_byte(&buf);
03114 uint16 groupid = grf_load_word(&buf);
03115 if (!IsValidGroupID(groupid, "VehicleMapSpriteGroup")) continue;
03116
03117 grfmsg(8, "VehicleMapSpriteGroup: * [%d] Cargo type 0x%X, group id 0x%02X", c, ctype, groupid);
03118
03119 ctype = TranslateCargo(feature, ctype);
03120 if (ctype == CT_INVALID) continue;
03121
03122 for (uint i = 0; i < idcount; i++) {
03123 EngineID engine = engines[i];
03124
03125 grfmsg(7, "VehicleMapSpriteGroup: [%d] Engine %d...", i, engine);
03126
03127 if (wagover) {
03128 SetWagonOverrideSprites(engine, ctype, _cur_grffile->spritegroups[groupid], last_engines, last_engines_count);
03129 } else {
03130 SetCustomEngineSprites(engine, ctype, _cur_grffile->spritegroups[groupid]);
03131 }
03132 }
03133 }
03134
03135 uint16 groupid = grf_load_word(&buf);
03136 if (!IsValidGroupID(groupid, "VehicleMapSpriteGroup")) return;
03137
03138 grfmsg(8, "-- Default group id 0x%04X", groupid);
03139
03140 for (uint i = 0; i < idcount; i++) {
03141 EngineID engine = engines[i];
03142
03143 if (wagover) {
03144 SetWagonOverrideSprites(engine, CT_DEFAULT, _cur_grffile->spritegroups[groupid], last_engines, last_engines_count);
03145 } else {
03146 SetCustomEngineSprites(engine, CT_DEFAULT, _cur_grffile->spritegroups[groupid]);
03147 SetEngineGRF(engine, _cur_grffile);
03148 }
03149 }
03150 }
03151
03152
03153 static void CanalMapSpriteGroup(byte *buf, uint8 idcount)
03154 {
03155 CanalFeature *cfs = AllocaM(CanalFeature, idcount);
03156 for (uint i = 0; i < idcount; i++) {
03157 cfs[i] = (CanalFeature)grf_load_byte(&buf);
03158 }
03159
03160 uint8 cidcount = grf_load_byte(&buf);
03161 buf += cidcount * 3;
03162
03163 uint16 groupid = grf_load_word(&buf);
03164 if (!IsValidGroupID(groupid, "CanalMapSpriteGroup")) return;
03165
03166 for (uint i = 0; i < idcount; i++) {
03167 CanalFeature cf = cfs[i];
03168
03169 if (cf >= CF_END) {
03170 grfmsg(1, "CanalMapSpriteGroup: Canal subset %d out of range, skipping", cf);
03171 continue;
03172 }
03173
03174 _water_feature[cf].grffile = _cur_grffile;
03175 _water_feature[cf].group = _cur_grffile->spritegroups[groupid];
03176 }
03177 }
03178
03179
03180 static void StationMapSpriteGroup(byte *buf, uint8 idcount)
03181 {
03182 uint8 *stations = AllocaM(uint8, idcount);
03183 for (uint i = 0; i < idcount; i++) {
03184 stations[i] = grf_load_byte(&buf);
03185 }
03186
03187 uint8 cidcount = grf_load_byte(&buf);
03188 for (uint c = 0; c < cidcount; c++) {
03189 uint8 ctype = grf_load_byte(&buf);
03190 uint16 groupid = grf_load_word(&buf);
03191 if (!IsValidGroupID(groupid, "StationMapSpriteGroup")) continue;
03192
03193 ctype = TranslateCargo(GSF_STATION, ctype);
03194 if (ctype == CT_INVALID) continue;
03195
03196 for (uint i = 0; i < idcount; i++) {
03197 StationSpec *statspec = _cur_grffile->stations[stations[i]];
03198
03199 if (statspec == NULL) {
03200 grfmsg(1, "StationMapSpriteGroup: Station with ID 0x%02X does not exist, skipping", stations[i]);
03201 continue;
03202 }
03203
03204 statspec->spritegroup[ctype] = _cur_grffile->spritegroups[groupid];
03205 }
03206 }
03207
03208 uint16 groupid = grf_load_word(&buf);
03209 if (!IsValidGroupID(groupid, "StationMapSpriteGroup")) return;
03210
03211 for (uint i = 0; i < idcount; i++) {
03212 StationSpec *statspec = _cur_grffile->stations[stations[i]];
03213
03214 if (statspec == NULL) {
03215 grfmsg(1, "StationMapSpriteGroup: Station with ID 0x%02X does not exist, skipping", stations[i]);
03216 continue;
03217 }
03218
03219 statspec->spritegroup[CT_DEFAULT] = _cur_grffile->spritegroups[groupid];
03220 statspec->grffile = _cur_grffile;
03221 statspec->localidx = stations[i];
03222 SetCustomStationSpec(statspec);
03223 }
03224 }
03225
03226
03227 static void TownHouseMapSpriteGroup(byte *buf, uint8 idcount)
03228 {
03229 uint8 *houses = AllocaM(uint8, idcount);
03230 for (uint i = 0; i < idcount; i++) {
03231 houses[i] = grf_load_byte(&buf);
03232 }
03233
03234
03235 uint8 cidcount = grf_load_byte(&buf);
03236 buf += cidcount * 3;
03237
03238 uint16 groupid = grf_load_word(&buf);
03239 if (!IsValidGroupID(groupid, "TownHouseMapSpriteGroup")) return;
03240
03241 for (uint i = 0; i < idcount; i++) {
03242 HouseSpec *hs = _cur_grffile->housespec[houses[i]];
03243
03244 if (hs == NULL) {
03245 grfmsg(1, "TownHouseMapSpriteGroup: House %d undefined, skipping.", houses[i]);
03246 continue;
03247 }
03248
03249 hs->spritegroup = _cur_grffile->spritegroups[groupid];
03250 }
03251 }
03252
03253 static void IndustryMapSpriteGroup(byte *buf, uint8 idcount)
03254 {
03255 uint8 *industries = AllocaM(uint8, idcount);
03256 for (uint i = 0; i < idcount; i++) {
03257 industries[i] = grf_load_byte(&buf);
03258 }
03259
03260
03261 uint8 cidcount = grf_load_byte(&buf);
03262 buf += cidcount * 3;
03263
03264 uint16 groupid = grf_load_word(&buf);
03265 if (!IsValidGroupID(groupid, "IndustryMapSpriteGroup")) return;
03266
03267 for (uint i = 0; i < idcount; i++) {
03268 IndustrySpec *indsp = _cur_grffile->industryspec[industries[i]];
03269
03270 if (indsp == NULL) {
03271 grfmsg(1, "IndustryMapSpriteGroup: Industry %d undefined, skipping", industries[i]);
03272 continue;
03273 }
03274
03275 indsp->grf_prop.spritegroup = _cur_grffile->spritegroups[groupid];
03276 }
03277 }
03278
03279 static void IndustrytileMapSpriteGroup(byte *buf, uint8 idcount)
03280 {
03281 uint8 *indtiles = AllocaM(uint8, idcount);
03282 for (uint i = 0; i < idcount; i++) {
03283 indtiles[i] = grf_load_byte(&buf);
03284 }
03285
03286
03287 uint8 cidcount = grf_load_byte(&buf);
03288 buf += cidcount * 3;
03289
03290 uint16 groupid = grf_load_word(&buf);
03291 if (!IsValidGroupID(groupid, "IndustrytileMapSpriteGroup")) return;
03292
03293 for (uint i = 0; i < idcount; i++) {
03294 IndustryTileSpec *indtsp = _cur_grffile->indtspec[indtiles[i]];
03295
03296 if (indtsp == NULL) {
03297 grfmsg(1, "IndustrytileMapSpriteGroup: Industry tile %d undefined, skipping", indtiles[i]);
03298 continue;
03299 }
03300
03301 indtsp->grf_prop.spritegroup = _cur_grffile->spritegroups[groupid];
03302 }
03303 }
03304
03305 static void CargoMapSpriteGroup(byte *buf, uint8 idcount)
03306 {
03307 CargoID *cargos = AllocaM(CargoID, idcount);
03308 for (uint i = 0; i < idcount; i++) {
03309 cargos[i] = grf_load_byte(&buf);
03310 }
03311
03312
03313 uint8 cidcount = grf_load_byte(&buf);
03314 buf += cidcount * 3;
03315
03316 uint16 groupid = grf_load_word(&buf);
03317 if (!IsValidGroupID(groupid, "CargoMapSpriteGroup")) return;
03318
03319 for (uint i = 0; i < idcount; i++) {
03320 CargoID cid = cargos[i];
03321
03322 if (cid >= NUM_CARGO) {
03323 grfmsg(1, "CargoMapSpriteGroup: Cargo ID %d out of range, skipping", cid);
03324 continue;
03325 }
03326
03327 CargoSpec *cs = &_cargo[cid];
03328 cs->grffile = _cur_grffile;
03329 cs->group = _cur_grffile->spritegroups[groupid];
03330 }
03331 }
03332
03333
03334
03335 static void FeatureMapSpriteGroup(byte *buf, size_t len)
03336 {
03337
03338
03339
03340
03341
03342
03343
03344
03345
03346
03347
03348
03349
03350
03351 if (_cur_grffile->spritegroups == 0) {
03352 grfmsg(1, "FeatureMapSpriteGroup: No sprite groups to work on! Skipping");
03353 return;
03354 }
03355
03356 if (!check_length(len, 6, "FeatureMapSpriteGroup")) return;
03357
03358 buf++;
03359 uint8 feature = grf_load_byte(&buf);
03360 uint8 idcount = grf_load_byte(&buf);
03361
03362
03363 if (idcount == 0) {
03364
03365 grf_load_byte(&buf);
03366 uint16 groupid = grf_load_word(&buf);
03367
03368 grfmsg(6, "FeatureMapSpriteGroup: Adding generic feature callback for feature %d", feature);
03369
03370 AddGenericCallback(feature, _cur_grffile, _cur_grffile->spritegroups[groupid]);
03371 return;
03372 }
03373
03374 grfmsg(6, "FeatureMapSpriteGroup: Feature %d, %d ids", feature, idcount);
03375
03376 switch (feature) {
03377 case GSF_TRAIN:
03378 case GSF_ROAD:
03379 case GSF_SHIP:
03380 case GSF_AIRCRAFT:
03381 VehicleMapSpriteGroup(buf, feature, idcount);
03382 return;
03383
03384 case GSF_CANAL:
03385 CanalMapSpriteGroup(buf, idcount);
03386 return;
03387
03388 case GSF_STATION:
03389 StationMapSpriteGroup(buf, idcount);
03390 return;
03391
03392 case GSF_TOWNHOUSE:
03393 TownHouseMapSpriteGroup(buf, idcount);
03394 return;
03395
03396 case GSF_INDUSTRIES:
03397 IndustryMapSpriteGroup(buf, idcount);
03398 return;
03399
03400 case GSF_INDUSTRYTILES:
03401 IndustrytileMapSpriteGroup(buf, idcount);
03402 return;
03403
03404 case GSF_CARGOS:
03405 CargoMapSpriteGroup(buf, idcount);
03406 return;
03407
03408 default:
03409 grfmsg(1, "FeatureMapSpriteGroup: Unsupported feature %d, skipping", feature);
03410 return;
03411 }
03412 }
03413
03414
03415 static void FeatureNewName(byte *buf, size_t len)
03416 {
03417
03418
03419
03420
03421
03422
03423
03424
03425
03426
03427
03428
03429
03430
03431
03432
03433 bool new_scheme = _cur_grffile->grf_version >= 7;
03434
03435 if (!check_length(len, 6, "FeatureNewName")) return;
03436 buf++;
03437 uint8 feature = grf_load_byte(&buf);
03438 uint8 lang = grf_load_byte(&buf);
03439 uint8 num = grf_load_byte(&buf);
03440 bool generic = HasBit(lang, 7);
03441 uint16 id;
03442 if (generic) {
03443 id = grf_load_word(&buf);
03444 } else if (feature <= GSF_AIRCRAFT) {
03445 id = grf_load_extended(&buf);
03446 } else {
03447 id = grf_load_byte(&buf);
03448 }
03449
03450 ClrBit(lang, 7);
03451
03452 uint16 endid = id + num;
03453
03454 grfmsg(6, "FeatureNewName: About to rename engines %d..%d (feature %d) in language 0x%02X",
03455 id, endid, feature, lang);
03456
03457 len -= generic ? 6 : 5;
03458
03459 for (; id < endid && len > 0; id++) {
03460 const char *name = grf_load_string(&buf, len);
03461 size_t name_length = strlen(name) + 1;
03462
03463 len -= (int)name_length;
03464
03465 grfmsg(8, "FeatureNewName: 0x%04X <- %s", id, name);
03466
03467 switch (feature) {
03468 case GSF_TRAIN:
03469 case GSF_ROAD:
03470 case GSF_SHIP:
03471 case GSF_AIRCRAFT:
03472 if (!generic) {
03473 Engine *e = GetNewEngine(_cur_grffile, (VehicleType)feature, id, HasBit(_cur_grfconfig->flags, GCF_STATIC));
03474 if (e == NULL) break;
03475 StringID string = AddGRFString(_cur_grffile->grfid, e->index, lang, new_scheme, name, e->info.string_id);
03476 e->info.string_id = string;
03477 } else {
03478 AddGRFString(_cur_grffile->grfid, id, lang, new_scheme, name, id);
03479 }
03480 break;
03481
03482 case GSF_INDUSTRIES: {
03483 AddGRFString(_cur_grffile->grfid, id, lang, new_scheme, name, STR_UNDEFINED);
03484 break;
03485 }
03486
03487 case GSF_TOWNHOUSE:
03488 default:
03489 switch (GB(id, 8, 8)) {
03490 case 0xC4:
03491 if (_cur_grffile->stations == NULL || _cur_grffile->stations[GB(id, 0, 8)] == NULL) {
03492 grfmsg(1, "FeatureNewName: Attempt to name undefined station 0x%X, ignoring", GB(id, 0, 8));
03493 } else {
03494 StationClassID sclass = _cur_grffile->stations[GB(id, 0, 8)]->sclass;
03495 SetStationClassName(sclass, AddGRFString(_cur_grffile->grfid, id, lang, new_scheme, name, STR_UNDEFINED));
03496 }
03497 break;
03498
03499 case 0xC5:
03500 if (_cur_grffile->stations == NULL || _cur_grffile->stations[GB(id, 0, 8)] == NULL) {
03501 grfmsg(1, "FeatureNewName: Attempt to name undefined station 0x%X, ignoring", GB(id, 0, 8));
03502 } else {
03503 _cur_grffile->stations[GB(id, 0, 8)]->name = AddGRFString(_cur_grffile->grfid, id, lang, new_scheme, name, STR_UNDEFINED);
03504 }
03505 break;
03506
03507 case 0xC9:
03508 if (_cur_grffile->housespec == NULL || _cur_grffile->housespec[GB(id, 0, 8)] == NULL) {
03509 grfmsg(1, "FeatureNewName: Attempt to name undefined house 0x%X, ignoring.", GB(id, 0, 8));
03510 } else {
03511 _cur_grffile->housespec[GB(id, 0, 8)]->building_name = AddGRFString(_cur_grffile->grfid, id, lang, new_scheme, name, STR_UNDEFINED);
03512 }
03513 break;
03514
03515 case 0xD0:
03516 case 0xD1:
03517 case 0xD2:
03518 case 0xD3:
03519 case 0xDC:
03520 AddGRFString(_cur_grffile->grfid, id, lang, new_scheme, name, STR_UNDEFINED);
03521 break;
03522
03523 default:
03524 grfmsg(7, "FeatureNewName: Unsupported ID (0x%04X)", id);
03525 break;
03526 }
03527 break;
03528
03529 #if 0
03530 case GSF_CANAL :
03531 case GSF_BRIDGE :
03532 AddGRFString(_cur_spriteid, id, lang, name);
03533 switch (GB(id, 8, 8)) {
03534 case 0xC9:
03535 default:
03536 grfmsg(7, "FeatureNewName: Unsupported ID (0x%04X)", id);
03537 }
03538 break;
03539
03540 default :
03541 grfmsg(7, "FeatureNewName: Unsupported feature (0x%02X)", feature);
03542 break;
03543 #endif
03544 }
03545 }
03546 }
03547
03556 static uint16 SanitizeSpriteOffset(uint16& num, uint16 offset, int max_sprites, const char *name)
03557 {
03558
03559 if (offset >= max_sprites) {
03560 grfmsg(1, "GraphicsNew: %s sprite offset must be less than %i, skipping", name, max_sprites);
03561 uint orig_num = num;
03562 num = 0;
03563 return orig_num;
03564 }
03565
03566 if (offset + num > max_sprites) {
03567 grfmsg(4, "GraphicsNew: %s sprite overflow, truncating...", name);
03568 uint orig_num = num;
03569 num = max(max_sprites - offset, 0);
03570 return orig_num - num;
03571 }
03572
03573 return 0;
03574 }
03575
03576
03577 static void GraphicsNew(byte *buf, size_t len)
03578 {
03579
03580
03581
03582
03583
03584
03585
03586 enum Action5BlockType {
03587 A5BLOCK_FIXED,
03588 A5BLOCK_ALLOW_OFFSET,
03589 A5BLOCK_INVALID,
03590 };
03591 struct Action5Type {
03592 Action5BlockType block_type;
03593 SpriteID sprite_base;
03594 uint16 min_sprites;
03595 uint16 max_sprites;
03596 const char *name;
03597 };
03598
03599 static const Action5Type action5_types[] = {
03600
03601 { A5BLOCK_INVALID, 0, 0, 0, "Type 0x00" },
03602 { A5BLOCK_INVALID, 0, 0, 0, "Type 0x01" },
03603 { A5BLOCK_INVALID, 0, 0, 0, "Type 0x02" },
03604 { A5BLOCK_INVALID, 0, 0, 0, "Type 0x03" },
03605 { A5BLOCK_FIXED, SPR_SIGNALS_BASE, 48, PRESIGNAL_SEMAPHORE_AND_PBS_SPRITE_COUNT, "Signal graphics" },
03606 { A5BLOCK_FIXED, SPR_ELRAIL_BASE, 48, ELRAIL_SPRITE_COUNT, "Catenary graphics" },
03607 { A5BLOCK_FIXED, SPR_SLOPES_BASE, 74, NORMAL_AND_HALFTILE_FOUNDATION_SPRITE_COUNT, "Foundation graphics" },
03608 { A5BLOCK_INVALID, 0, 75, 0, "TTDP GUI graphics" },
03609 { A5BLOCK_FIXED, SPR_CANALS_BASE, 65, CANALS_SPRITE_COUNT, "Canal graphics" },
03610 { A5BLOCK_FIXED, SPR_ONEWAY_BASE, 6, ONEWAY_SPRITE_COUNT, "One way road graphics" },
03611 { A5BLOCK_FIXED, SPR_2CCMAP_BASE, 256, TWOCCMAP_SPRITE_COUNT, "2CC colour maps" },
03612 { A5BLOCK_FIXED, SPR_TRAMWAY_BASE, 113, TRAMWAY_SPRITE_COUNT, "Tramway graphics" },
03613 { A5BLOCK_INVALID, 0, 133, 0, "Snowy temperate tree" },
03614 { A5BLOCK_FIXED, SPR_SHORE_BASE, 16, SPR_SHORE_SPRITE_COUNT, "Shore graphics" },
03615 { A5BLOCK_INVALID, 0, 0, 0, "New Signals graphics" },
03616 { A5BLOCK_FIXED, SPR_TRACKS_FOR_SLOPES_BASE, 12, TRACKS_FOR_SLOPES_SPRITE_COUNT, "Sloped rail track" },
03617 { A5BLOCK_FIXED, SPR_AIRPORTX_BASE, 15, AIRPORTX_SPRITE_COUNT, "Airport graphics" },
03618 { A5BLOCK_FIXED, SPR_ROADSTOP_BASE, 8, ROADSTOP_SPRITE_COUNT, "Road stop graphics" },
03619 { A5BLOCK_FIXED, SPR_AQUEDUCT_BASE, 8, AQUEDUCT_SPRITE_COUNT, "Aqueduct graphics" },
03620 { A5BLOCK_FIXED, SPR_AUTORAIL_BASE, 55, AUTORAIL_SPRITE_COUNT, "Autorail graphics" },
03621 { A5BLOCK_ALLOW_OFFSET, SPR_FLAGS_BASE, 1, FLAGS_SPRITE_COUNT, "Flag graphics" },
03622 { A5BLOCK_ALLOW_OFFSET, SPR_OPENTTD_BASE, 1, OPENTTD_SPRITE_COUNT, "OpenTTD GUI graphics" },
03623 };
03624
03625 if (!check_length(len, 2, "GraphicsNew")) return;
03626 buf++;
03627 uint8 type = grf_load_byte(&buf);
03628 uint16 num = grf_load_extended(&buf);
03629 uint16 offset = HasBit(type, 7) ? grf_load_extended(&buf) : 0;
03630 ClrBit(type, 7);
03631
03632 if ((type == 0x0D) && (num == 10) && _cur_grffile->is_ottdfile) {
03633
03634
03635 grfmsg(2, "GraphicsNew: Loading 10 missing shore sprites from openttd(d/w).grf.");
03636 LoadNextSprite(SPR_SHORE_BASE + 0, _file_index, _nfo_line++);
03637 LoadNextSprite(SPR_SHORE_BASE + 5, _file_index, _nfo_line++);
03638 LoadNextSprite(SPR_SHORE_BASE + 7, _file_index, _nfo_line++);
03639 LoadNextSprite(SPR_SHORE_BASE + 10, _file_index, _nfo_line++);
03640 LoadNextSprite(SPR_SHORE_BASE + 11, _file_index, _nfo_line++);
03641 LoadNextSprite(SPR_SHORE_BASE + 13, _file_index, _nfo_line++);
03642 LoadNextSprite(SPR_SHORE_BASE + 14, _file_index, _nfo_line++);
03643 LoadNextSprite(SPR_SHORE_BASE + 15, _file_index, _nfo_line++);
03644 LoadNextSprite(SPR_SHORE_BASE + 16, _file_index, _nfo_line++);
03645 LoadNextSprite(SPR_SHORE_BASE + 17, _file_index, _nfo_line++);
03646 if (_loaded_newgrf_features.shore == SHORE_REPLACE_NONE) _loaded_newgrf_features.shore = SHORE_REPLACE_ONLY_NEW;
03647 return;
03648 }
03649
03650
03651 if ((type >= lengthof(action5_types)) || (action5_types[type].block_type == A5BLOCK_INVALID)) {
03652 grfmsg(2, "GraphicsNew: Custom graphics (type 0x%02X) sprite block of length %u (unimplemented, ignoring)", type, num);
03653 _skip_sprites = num;
03654 return;
03655 }
03656
03657 const Action5Type *action5_type = &action5_types[type];
03658
03659
03660 if ((action5_type->block_type != A5BLOCK_ALLOW_OFFSET) && (offset != 0)) {
03661 grfmsg(1, "GraphicsNew: %s (type 0x%02X) do not allow an <offset> field. Ignoring offset.", action5_type->name, type);
03662 offset = 0;
03663 }
03664
03665
03666
03667 if ((action5_type->block_type == A5BLOCK_FIXED) && (num < action5_type->min_sprites)) {
03668 grfmsg(1, "GraphicsNew: %s (type 0x%02X) count must be at least %d. Only %d were specified. Skipping.", action5_type->name, type, action5_type->min_sprites, num);
03669 _skip_sprites = num;
03670 return;
03671 }
03672
03673
03674 uint16 skip_num = SanitizeSpriteOffset(num, offset, action5_type->max_sprites, action5_type->name);
03675 SpriteID replace = action5_type->sprite_base + offset;
03676
03677
03678 grfmsg(2, "GraphicsNew: Replacing sprites %d to %d of %s (type 0x%02X) at SpriteID 0x%04X", offset, offset + num - 1, action5_type->name, type, replace);
03679
03680 for (; num > 0; num--) {
03681 _nfo_line++;
03682 LoadNextSprite(replace == 0 ? _cur_spriteid++ : replace++, _file_index, _nfo_line);
03683 }
03684
03685 if (type == 0x0D) _loaded_newgrf_features.shore = SHORE_REPLACE_ACTION_5;
03686
03687 _skip_sprites = skip_num;
03688 }
03689
03690
03691 static void SkipAct5(byte *buf, size_t len)
03692 {
03693 if (!check_length(len, 2, "SkipAct5")) return;
03694 buf++;
03695
03696
03697 grf_load_byte(&buf);
03698
03699
03700 _skip_sprites = grf_load_extended(&buf);
03701
03702 grfmsg(3, "SkipAct5: Skipping %d sprites", _skip_sprites);
03703 }
03704
03715 bool GetGlobalVariable(byte param, uint32 *value)
03716 {
03717 switch (param) {
03718 case 0x00:
03719 *value = max(_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0);
03720 return true;
03721
03722 case 0x01:
03723 *value = Clamp(_cur_year, ORIGINAL_BASE_YEAR, ORIGINAL_MAX_YEAR) - ORIGINAL_BASE_YEAR;
03724 return true;
03725
03726 case 0x02: {
03727 YearMonthDay ymd;
03728 ConvertDateToYMD(_date, &ymd);
03729 Date start_of_year = ConvertYMDToDate(ymd.year, 0, 1);
03730 *value = ymd.month | (ymd.day - 1) << 8 | (IsLeapYear(ymd.year) ? 1 << 15 : 0) | (_date - start_of_year) << 16;
03731 return true;
03732 }
03733
03734 case 0x03:
03735 *value = _settings_game.game_creation.landscape;
03736 return true;
03737
03738 case 0x06:
03739 *value = _settings_game.vehicle.road_side << 4;
03740 return true;
03741
03742 case 0x09:
03743 *value = _date_fract;
03744 return true;
03745
03746 case 0x0A:
03747 *value = _tick_counter;
03748 return true;
03749
03750 case 0x0B: {
03751 uint major = 2;
03752 uint minor = 6;
03753 uint revision = 1;
03754 uint build = 1382;
03755 *value = (major << 24) | (minor << 20) | (revision << 16) | build;
03756 return true;
03757 }
03758
03759 case 0x0D:
03760 *value = _cur_grfconfig->windows_paletted;
03761 return true;
03762
03763 case 0x0E:
03764 *value = _traininfo_vehicle_pitch;
03765 return true;
03766
03767 case 0x0F:
03768 *value = 0;
03769 SB(*value, 0, 8, GetRailTypeInfo(RAILTYPE_RAIL)->cost_multiplier);
03770 if (_settings_game.vehicle.disable_elrails) {
03771
03772 SB(*value, 8, 8, GetRailTypeInfo(RAILTYPE_MONO)->cost_multiplier);
03773 } else {
03774 SB(*value, 8, 8, GetRailTypeInfo(RAILTYPE_ELECTRIC)->cost_multiplier);
03775
03776 }
03777 SB(*value, 16, 8, GetRailTypeInfo(RAILTYPE_MAGLEV)->cost_multiplier);
03778 return true;
03779
03780 case 0x11:
03781 *value = 0;
03782 return true;
03783
03784 case 0x12:
03785 *value = _game_mode;
03786 return true;
03787
03788
03789
03790
03791
03792
03793
03794 case 0x1A:
03795 *value = UINT_MAX;
03796 return true;
03797
03798 case 0x1B:
03799 *value = GB(_display_opt, 0, 6);
03800 return true;
03801
03802 case 0x1D:
03803 *value = 1;
03804 return true;
03805
03806 case 0x1E:
03807 *value = _misc_grf_features;
03808 return true;
03809
03810
03811
03812 case 0x20:
03813 *value = _settings_game.game_creation.landscape == LT_ARCTIC ? GetSnowLine() : 0xFF;
03814 return true;
03815
03816 case 0x21:
03817 *value = _openttd_newgrf_version;
03818 return true;
03819
03820 case 0x22:
03821 *value = _settings_game.difficulty.diff_level;
03822 return true;
03823
03824 case 0x23:
03825 *value = _date;
03826 return true;
03827
03828 case 0x24:
03829 *value = _cur_year;
03830 return true;
03831
03832 default: return false;
03833 }
03834 }
03835
03836 static uint32 GetParamVal(byte param, uint32 *cond_val)
03837 {
03838
03839 uint32 value;
03840 if (GetGlobalVariable(param - 0x80, &value)) return value;
03841
03842
03843 switch (param) {
03844 case 0x84: {
03845 uint32 res = 0;
03846
03847 if (_cur_stage > GLS_INIT) SetBit(res, 0);
03848 if (_cur_stage == GLS_RESERVE) SetBit(res, 8);
03849 if (_cur_stage == GLS_ACTIVATION) SetBit(res, 9);
03850 return res;
03851 }
03852
03853 case 0x85:
03854 if (cond_val == NULL) {
03855
03856 return 0;
03857 } else {
03858 uint32 param_val = _ttdpatch_flags[*cond_val / 0x20];
03859 *cond_val %= 0x20;
03860 return param_val;
03861 }
03862
03863 case 0x88:
03864 return 0;
03865
03866
03867
03868 default:
03869
03870 if (param < 0x80) return _cur_grffile->param[param];
03871
03872
03873 grfmsg(1, "Unsupported in-game variable 0x%02X", param);
03874 return UINT_MAX;
03875 }
03876 }
03877
03878
03879 static void CfgApply(byte *buf, size_t len)
03880 {
03881
03882
03883
03884
03885
03886
03887
03888
03889
03890
03891
03892
03893 size_t pos = FioGetPos();
03894 uint16 num = FioReadWord();
03895 uint8 type = FioReadByte();
03896 byte *preload_sprite = NULL;
03897
03898
03899 if (type == 0xFF) {
03900 preload_sprite = MallocT<byte>(num);
03901 FioReadBlock(preload_sprite, num);
03902 }
03903
03904
03905 FioSeekTo(pos, SEEK_SET);
03906
03907 if (type != 0xFF) {
03908 grfmsg(2, "CfgApply: Ignoring (next sprite is real, unsupported)");
03909 free(preload_sprite);
03910 return;
03911 }
03912
03913 GRFLocation location(_cur_grfconfig->grfid, _nfo_line + 1);
03914 GRFLineToSpriteOverride::iterator it = _grf_line_to_action6_sprite_override.find(location);
03915 if (it != _grf_line_to_action6_sprite_override.end()) {
03916 free(preload_sprite);
03917 preload_sprite = _grf_line_to_action6_sprite_override[location];
03918 } else {
03919 _grf_line_to_action6_sprite_override[location] = preload_sprite;
03920 }
03921
03922
03923 buf++;
03924
03925 for (;;) {
03926 uint i;
03927 uint param_num;
03928 uint param_size;
03929 uint offset;
03930 bool add_value;
03931
03932
03933 param_num = grf_load_byte(&buf);
03934 if (param_num == 0xFF) break;
03935
03936
03937
03938 param_size = grf_load_byte(&buf);
03939
03940
03941
03942 add_value = HasBit(param_size, 7);
03943 param_size = GB(param_size, 0, 7);
03944
03945
03946 offset = grf_load_extended(&buf);
03947
03948
03949
03950 if (param_num < 0x80 && (param_num + (param_size - 1) / 4) >= _cur_grffile->param_end) {
03951 grfmsg(2, "CfgApply: Ignoring (param %d not set)", (param_num + (param_size - 1) / 4));
03952 break;
03953 }
03954
03955 grfmsg(8, "CfgApply: Applying %u bytes from parameter 0x%02X at offset 0x%04X", param_size, param_num, offset);
03956
03957 bool carry = false;
03958 for (i = 0; i < param_size && offset + i < num; i++) {
03959 uint32 value = GetParamVal(param_num + i / 4, NULL);
03960
03961
03962 if (i == 0) carry = false;
03963
03964 if (add_value) {
03965 uint new_value = preload_sprite[offset + i] + GB(value, (i % 4) * 8, 8) + (carry ? 1 : 0);
03966 preload_sprite[offset + i] = GB(new_value, 0, 8);
03967
03968 carry = new_value >= 256;
03969 } else {
03970 preload_sprite[offset + i] = GB(value, (i % 4) * 8, 8);
03971 }
03972 }
03973 }
03974 }
03975
03985 static void DisableStaticNewGRFInfluencingNonStaticNewGRFs(GRFConfig *c)
03986 {
03987 if (c->error != NULL) {
03988 free(c->error->custom_message);
03989 free(c->error->data);
03990 free(c->error);
03991 }
03992 c->status = GCS_DISABLED;
03993 c->error = CallocT<GRFError>(1);
03994 c->error->data = strdup(_cur_grfconfig->name);
03995 c->error->severity = STR_NEWGRF_ERROR_MSG_FATAL;
03996 c->error->message = STR_NEWGRF_ERROR_STATIC_GRF_CAUSES_DESYNC;
03997
03998 ClearTemporaryNewGRFData(GetFileByGRFID(c->grfid));
03999 }
04000
04001
04002
04003 static void SkipIf(byte *buf, size_t len)
04004 {
04005
04006
04007
04008
04009
04010
04011
04012
04013 uint32 cond_val = 0;
04014 uint32 mask = 0;
04015 bool result;
04016
04017 if (!check_length(len, 6, "SkipIf")) return;
04018 buf++;
04019 uint8 param = grf_load_byte(&buf);
04020 uint8 paramsize = grf_load_byte(&buf);
04021 uint8 condtype = grf_load_byte(&buf);
04022
04023 if (condtype < 2) {
04024
04025 paramsize = 1;
04026 }
04027
04028 switch (paramsize) {
04029 case 8: cond_val = grf_load_dword(&buf); mask = grf_load_dword(&buf); break;
04030 case 4: cond_val = grf_load_dword(&buf); mask = 0xFFFFFFFF; break;
04031 case 2: cond_val = grf_load_word(&buf); mask = 0x0000FFFF; break;
04032 case 1: cond_val = grf_load_byte(&buf); mask = 0x000000FF; break;
04033 default: break;
04034 }
04035
04036 if (param < 0x80 && _cur_grffile->param_end <= param) {
04037 grfmsg(7, "SkipIf: Param %d undefined, skipping test", param);
04038 return;
04039 }
04040
04041 uint32 param_val = GetParamVal(param, &cond_val);
04042
04043 grfmsg(7, "SkipIf: Test condtype %d, param 0x%08X, condval 0x%08X", condtype, param_val, cond_val);
04044
04045
04046
04047
04048
04049
04050
04051
04052 if (param == 0x88 && condtype != 0x0B && condtype != 0x0C) {
04053
04054
04055 GRFConfig *c = GetGRFConfig(cond_val, mask);
04056
04057 if (c != NULL && HasBit(c->flags, GCF_STATIC) && !HasBit(_cur_grfconfig->flags, GCF_STATIC) && c->status != GCS_DISABLED && _networking) {
04058 DisableStaticNewGRFInfluencingNonStaticNewGRFs(c);
04059 c = NULL;
04060 }
04061
04062 if (condtype != 10 && c == NULL) {
04063 grfmsg(7, "SkipIf: GRFID 0x%08X unknown, skipping test", BSWAP32(cond_val));
04064 return;
04065 }
04066
04067 switch (condtype) {
04068
04069 case 0x06:
04070 result = c->status == GCS_ACTIVATED;
04071 break;
04072
04073 case 0x07:
04074 result = c->status != GCS_ACTIVATED;
04075 break;
04076
04077 case 0x08:
04078 result = c->status == GCS_INITIALISED;
04079 break;
04080
04081 case 0x09:
04082 result = c->status == GCS_ACTIVATED || c->status == GCS_INITIALISED;
04083 break;
04084
04085 case 0x0A:
04086
04087 result = c == NULL || c->flags == GCS_DISABLED || c->status == GCS_NOT_FOUND;
04088 break;
04089
04090 default: grfmsg(1, "SkipIf: Unsupported GRF condition type %02X. Ignoring", condtype); return;
04091 }
04092 } else {
04093
04094 switch (condtype) {
04095 case 0x00: result = !!(param_val & (1 << cond_val));
04096 break;
04097 case 0x01: result = !(param_val & (1 << cond_val));
04098 break;
04099 case 0x02: result = (param_val & mask) == cond_val;
04100 break;
04101 case 0x03: result = (param_val & mask) != cond_val;
04102 break;
04103 case 0x04: result = (param_val & mask) < cond_val;
04104 break;
04105 case 0x05: result = (param_val & mask) > cond_val;
04106 break;
04107 case 0x0B: result = GetCargoIDByLabel(BSWAP32(cond_val)) == CT_INVALID;
04108 break;
04109 case 0x0C: result = GetCargoIDByLabel(BSWAP32(cond_val)) != CT_INVALID;
04110 break;
04111 case 0x0D: result = GetRailTypeByLabel(BSWAP32(cond_val)) == INVALID_RAILTYPE;
04112 break;
04113 case 0x0E: result = GetRailTypeByLabel(BSWAP32(cond_val)) != INVALID_RAILTYPE;
04114 break;
04115
04116 default: grfmsg(1, "SkipIf: Unsupported condition type %02X. Ignoring", condtype); return;
04117 }
04118 }
04119
04120 if (!result) {
04121 grfmsg(2, "SkipIf: Not skipping sprites, test was false");
04122 return;
04123 }
04124
04125 uint8 numsprites = grf_load_byte(&buf);
04126
04127
04128
04129
04130
04131 GRFLabel *choice = NULL;
04132 for (GRFLabel *label = _cur_grffile->label; label != NULL; label = label->next) {
04133 if (label->label != numsprites) continue;
04134
04135
04136 if (choice == NULL) choice = label;
04137
04138 if (label->nfo_line > _nfo_line) {
04139 choice = label;
04140 break;
04141 }
04142 }
04143
04144 if (choice != NULL) {
04145 grfmsg(2, "SkipIf: Jumping to label 0x%0X at line %d, test was true", choice->label, choice->nfo_line);
04146 FioSeekTo(choice->pos, SEEK_SET);
04147 _nfo_line = choice->nfo_line;
04148 return;
04149 }
04150
04151 grfmsg(2, "SkipIf: Skipping %d sprites, test was true", numsprites);
04152 _skip_sprites = numsprites;
04153 if (_skip_sprites == 0) {
04154
04155
04156
04157 _skip_sprites = -1;
04158
04159
04160 if (_cur_grfconfig->status != GCS_ACTIVATED) {
04161 _cur_grfconfig->status = GCS_DISABLED;
04162 ClearTemporaryNewGRFData(_cur_grffile);
04163 }
04164 }
04165 }
04166
04167
04168
04169 static void ScanInfo(byte *buf, size_t len)
04170 {
04171 if (!check_length(len, 8, "Info")) return;
04172 buf++;
04173 grf_load_byte(&buf);
04174 uint32 grfid = grf_load_dword(&buf);
04175
04176 _cur_grfconfig->grfid = grfid;
04177
04178
04179 if (GB(grfid, 24, 8) == 0xFF) SetBit(_cur_grfconfig->flags, GCF_SYSTEM);
04180
04181 len -= 6;
04182 const char *name = grf_load_string(&buf, len);
04183 _cur_grfconfig->name = TranslateTTDPatchCodes(grfid, name);
04184
04185 len -= strlen(name) + 1;
04186 if (len > 0) {
04187 const char *info = grf_load_string(&buf, len);
04188 _cur_grfconfig->info = TranslateTTDPatchCodes(grfid, info);
04189 }
04190
04191
04192 _skip_sprites = -1;
04193 }
04194
04195
04196 static void GRFInfo(byte *buf, size_t len)
04197 {
04198
04199
04200
04201
04202
04203
04204
04205 if (!check_length(len, 8, "GRFInfo")) return;
04206 buf++;
04207 uint8 version = grf_load_byte(&buf);
04208 uint32 grfid = grf_load_dword(&buf);
04209 const char *name = grf_load_string(&buf, len - 6);
04210
04211 if (_cur_stage < GLS_RESERVE && _cur_grfconfig->status != GCS_UNKNOWN) {
04212 _cur_grfconfig->status = GCS_DISABLED;
04213 _cur_grfconfig->error = CallocT<GRFError>(1);
04214 _cur_grfconfig->error->severity = STR_NEWGRF_ERROR_MSG_FATAL;
04215 _cur_grfconfig->error->message = STR_NEWGRF_ERROR_MULTIPLE_ACTION_8;
04216
04217 _skip_sprites = -1;
04218 return;
04219 }
04220
04221 _cur_grffile->grfid = grfid;
04222 _cur_grffile->grf_version = version;
04223 _cur_grfconfig->status = _cur_stage < GLS_RESERVE ? GCS_INITIALISED : GCS_ACTIVATED;
04224
04225
04226 DEBUG(grf, 1, "GRFInfo: Loaded GRFv%d set %08lX - %s (palette: %s)", version, BSWAP32(grfid), name, _cur_grfconfig->windows_paletted ? "Windows" : "DOS");
04227 }
04228
04229
04230 static void SpriteReplace(byte *buf, size_t len)
04231 {
04232
04233
04234
04235
04236
04237
04238
04239
04240 buf++;
04241 uint8 num_sets = grf_load_byte(&buf);
04242
04243 for (uint i = 0; i < num_sets; i++) {
04244 uint8 num_sprites = grf_load_byte(&buf);
04245 uint16 first_sprite = grf_load_word(&buf);
04246
04247 grfmsg(2, "SpriteReplace: [Set %d] Changing %d sprites, beginning with %d",
04248 i, num_sprites, first_sprite
04249 );
04250
04251 for (uint j = 0; j < num_sprites; j++) {
04252 int load_index = first_sprite + j;
04253 _nfo_line++;
04254 LoadNextSprite(load_index, _file_index, _nfo_line);
04255
04256
04257
04258 if (IsInsideMM(load_index, SPR_ORIGINALSHORE_START, SPR_ORIGINALSHORE_END + 1)) {
04259 if (_loaded_newgrf_features.shore != SHORE_REPLACE_ACTION_5) _loaded_newgrf_features.shore = SHORE_REPLACE_ACTION_A;
04260 }
04261 }
04262 }
04263 }
04264
04265
04266 static void SkipActA(byte *buf, size_t len)
04267 {
04268 buf++;
04269 uint8 num_sets = grf_load_byte(&buf);
04270
04271 for (uint i = 0; i < num_sets; i++) {
04272
04273 _skip_sprites += grf_load_byte(&buf);
04274
04275 grf_load_word(&buf);
04276 }
04277
04278 grfmsg(3, "SkipActA: Skipping %d sprites", _skip_sprites);
04279 }
04280
04281
04282 static void GRFLoadError(byte *buf, size_t len)
04283 {
04284
04285
04286
04287
04288
04289
04290
04291
04292
04293
04294
04295
04296
04297
04298
04299 static const StringID msgstr[] = {
04300 STR_NEWGRF_ERROR_VERSION_NUMBER,
04301 STR_NEWGRF_ERROR_DOS_OR_WINDOWS,
04302 STR_NEWGRF_ERROR_UNSET_SWITCH,
04303 STR_NEWGRF_ERROR_INVALID_PARAMETER,
04304 STR_NEWGRF_ERROR_LOAD_BEFORE,
04305 STR_NEWGRF_ERROR_LOAD_AFTER,
04306 STR_NEWGRF_ERROR_OTTD_VERSION_NUMBER,
04307 };
04308
04309 static const StringID sevstr[] = {
04310 STR_NEWGRF_ERROR_MSG_INFO,
04311 STR_NEWGRF_ERROR_MSG_WARNING,
04312 STR_NEWGRF_ERROR_MSG_ERROR,
04313 STR_NEWGRF_ERROR_MSG_FATAL
04314 };
04315
04316 if (!check_length(len, 6, "GRFLoadError")) return;
04317
04318
04319 if (_cur_grfconfig->error != NULL) return;
04320
04321 buf++;
04322 byte severity = grf_load_byte(&buf);
04323 byte lang = grf_load_byte(&buf);
04324 byte message_id = grf_load_byte(&buf);
04325 len -= 4;
04326
04327
04328 if (!CheckGrfLangID(lang, _cur_grffile->grf_version)) return;
04329
04330
04331
04332 if (!HasBit(severity, 7) && _cur_stage == GLS_INIT) {
04333 grfmsg(7, "GRFLoadError: Skipping non-fatal GRFLoadError in stage %d", _cur_stage);
04334 return;
04335 }
04336 ClrBit(severity, 7);
04337
04338 if (severity >= lengthof(sevstr)) {
04339 grfmsg(7, "GRFLoadError: Invalid severity id %d. Setting to 2 (non-fatal error).", severity);
04340 severity = 2;
04341 } else if (severity == 3) {
04342
04343
04344 _cur_grfconfig->status = GCS_DISABLED;
04345 ClearTemporaryNewGRFData(_cur_grffile);
04346 _skip_sprites = -1;
04347 }
04348
04349 if (message_id >= lengthof(msgstr) && message_id != 0xFF) {
04350 grfmsg(7, "GRFLoadError: Invalid message id.");
04351 return;
04352 }
04353
04354 if (len <= 1) {
04355 grfmsg(7, "GRFLoadError: No message data supplied.");
04356 return;
04357 }
04358
04359 GRFError *error = CallocT<GRFError>(1);
04360
04361 error->severity = sevstr[severity];
04362
04363 if (message_id == 0xFF) {
04364
04365 const char *message = grf_load_string(&buf, len);
04366 len -= (strlen(message) + 1);
04367
04368 error->custom_message = TranslateTTDPatchCodes(_cur_grffile->grfid, message);
04369 } else {
04370 error->message = msgstr[message_id];
04371 }
04372
04373 if (len > 0) {
04374 const char *data = grf_load_string(&buf, len);
04375 len -= (strlen(data) + 1);
04376
04377 error->data = TranslateTTDPatchCodes(_cur_grffile->grfid, data);
04378 }
04379
04380
04381 uint i = 0;
04382 for (; i < 2 && len > 0; i++) {
04383 error->param_number[i] = grf_load_byte(&buf);
04384 len--;
04385 }
04386 error->num_params = i;
04387
04388 _cur_grfconfig->error = error;
04389 }
04390
04391
04392 static void GRFComment(byte *buf, size_t len)
04393 {
04394
04395
04396
04397
04398 if (len == 1) return;
04399
04400 size_t text_len = len - 1;
04401 const char *text = (const char*)(buf + 1);
04402 grfmsg(2, "GRFComment: %.*s", text_len, text);
04403 }
04404
04405
04406 static void SafeParamSet(byte *buf, size_t len)
04407 {
04408 if (!check_length(len, 5, "SafeParamSet")) return;
04409 buf++;
04410 uint8 target = grf_load_byte(&buf);
04411
04412
04413 if (target < 0x80) return;
04414
04415
04416
04417
04418
04419
04420 SetBit(_cur_grfconfig->flags, GCF_UNSAFE);
04421
04422
04423 _skip_sprites = -1;
04424 }
04425
04426
04427 static uint32 GetPatchVariable(uint8 param)
04428 {
04429 switch (param) {
04430
04431 case 0x0B: return max(_settings_game.game_creation.starting_year, ORIGINAL_BASE_YEAR) - ORIGINAL_BASE_YEAR;
04432
04433
04434 case 0x0E: return _settings_game.vehicle.freight_trains;
04435
04436
04437 case 0x0F: return 0;
04438
04439
04440
04441
04442 case 0x10:
04443 switch (_settings_game.vehicle.plane_speed) {
04444 default:
04445 case 4: return 1;
04446 case 3: return 2;
04447 case 2: return 2;
04448 case 1: return 4;
04449 }
04450
04451
04452
04453 case 0x11: return SPR_2CCMAP_BASE;
04454
04455
04456
04457
04458
04459
04460
04461
04462
04463
04464
04465
04466 case 0x13: {
04467 byte map_bits = 0;
04468 byte log_X = MapLogX() - 6;
04469 byte log_Y = MapLogY() - 6;
04470 byte max_edge = max(log_X, log_Y);
04471
04472 if (log_X == log_Y) {
04473 SetBit(map_bits ,0);
04474 } else {
04475 if (max_edge == log_Y) SetBit(map_bits, 1);
04476 }
04477
04478 return (map_bits << 24) | (min(log_X, log_Y) << 20) | (max_edge << 16) |
04479 (log_X << 12) | (log_Y << 8) | (log_X + log_Y);
04480 }
04481
04482 default:
04483 grfmsg(2, "ParamSet: Unknown Patch variable 0x%02X.", param);
04484 return 0;
04485 }
04486 }
04487
04488
04489 static uint32 PerformGRM(uint32 *grm, uint16 num_ids, uint16 count, uint8 op, uint8 target, const char *type)
04490 {
04491 uint start = 0;
04492 uint size = 0;
04493
04494 if (op == 6) {
04495
04496 return grm[_cur_grffile->param[target]];
04497 }
04498
04499
04500 if (op == 2 || op == 3) start = _cur_grffile->param[target];
04501
04502 for (uint i = start; i < num_ids; i++) {
04503 if (grm[i] == 0) {
04504 size++;
04505 } else {
04506 if (op == 2 || op == 3) break;
04507 start = i + 1;
04508 size = 0;
04509 }
04510
04511 if (size == count) break;
04512 }
04513
04514 if (size == count) {
04515
04516 if (op == 0 || op == 3) {
04517 grfmsg(2, "ParamSet: GRM: Reserving %d %s at %d", count, type, start);
04518 for (uint i = 0; i < count; i++) grm[start + i] = _cur_grffile->grfid;
04519 }
04520 return start;
04521 }
04522
04523
04524 if (op != 4 && op != 5) {
04525
04526 grfmsg(0, "ParamSet: GRM: Unable to allocate %d %s, deactivating", count, type);
04527 _cur_grfconfig->status = GCS_DISABLED;
04528 ClearTemporaryNewGRFData(_cur_grffile);
04529 _skip_sprites = -1;
04530 return UINT_MAX;
04531 }
04532
04533 grfmsg(1, "ParamSet: GRM: Unable to allocate %d %s", count, type);
04534 return UINT_MAX;
04535 }
04536
04537
04538
04539 static void ParamSet(byte *buf, size_t len)
04540 {
04541
04542
04543
04544
04545
04546
04547
04548
04549
04550
04551
04552
04553
04554
04555
04556
04557
04558
04559
04560
04561
04562
04563 if (!check_length(len, 5, "ParamSet")) return;
04564 buf++;
04565 uint8 target = grf_load_byte(&buf);
04566 uint8 oper = grf_load_byte(&buf);
04567 uint32 src1 = grf_load_byte(&buf);
04568 uint32 src2 = grf_load_byte(&buf);
04569
04570 uint32 data = 0;
04571 if (len >= 8) data = grf_load_dword(&buf);
04572
04573
04574
04575
04576
04577
04578
04579 if (HasBit(oper, 7)) {
04580 if (target < 0x80 && target < _cur_grffile->param_end) {
04581 grfmsg(7, "ParamSet: Param %u already defined, skipping", target);
04582 return;
04583 }
04584
04585 oper = GB(oper, 0, 7);
04586 }
04587
04588 if (src2 == 0xFE) {
04589 if (GB(data, 0, 8) == 0xFF) {
04590 if (data == 0x0000FFFF) {
04591
04592 src1 = GetPatchVariable(src1);
04593 } else {
04594
04595 uint8 op = src1;
04596 uint8 feature = GB(data, 8, 8);
04597 uint16 count = GB(data, 16, 16);
04598
04599 if (_cur_stage == GLS_RESERVE) {
04600 if (feature == 0x08) {
04601
04602 if (op == 0) {
04603
04604 if (_cur_spriteid + count >= 16384) {
04605 grfmsg(0, "ParamSet: GRM: Unable to allocate %d sprites; try changing NewGRF order", count);
04606 _cur_grfconfig->status = GCS_DISABLED;
04607 ClearTemporaryNewGRFData(_cur_grffile);
04608 _skip_sprites = -1;
04609 return;
04610 }
04611
04612
04613 grfmsg(4, "ParamSet: GRM: Allocated %d sprites at %d", count, _cur_spriteid);
04614 _grm_sprites[GRFLocation(_cur_grffile->grfid, _nfo_line)] = _cur_spriteid;
04615 _cur_spriteid += count;
04616 }
04617 }
04618
04619 src1 = 0;
04620 } else if (_cur_stage == GLS_ACTIVATION) {
04621 switch (feature) {
04622 case 0x00:
04623 case 0x01:
04624 case 0x02:
04625 case 0x03:
04626 if (!_settings_game.vehicle.dynamic_engines) {
04627 src1 = PerformGRM(&_grm_engines[_engine_offsets[feature]], _engine_counts[feature], count, op, target, "vehicles");
04628 if (_skip_sprites == -1) return;
04629 } else {
04630
04631 switch (op) {
04632 case 2:
04633 case 3:
04634 src1 = _cur_grffile->param[target];
04635 break;
04636
04637 default:
04638 src1 = 0;
04639 break;
04640 }
04641 }
04642 break;
04643
04644 case 0x08:
04645 switch (op) {
04646 case 0:
04647
04648 src1 = _grm_sprites[GRFLocation(_cur_grffile->grfid, _nfo_line)];
04649 grfmsg(4, "ParamSet: GRM: Using pre-allocated sprites at %d", src1);
04650 break;
04651
04652 case 1:
04653 src1 = _cur_spriteid;
04654 break;
04655
04656 default:
04657 grfmsg(1, "ParamSet: GRM: Unsupported operation %d for general sprites", op);
04658 return;
04659 }
04660 break;
04661
04662 case 0x0B:
04663
04664 src1 = PerformGRM(_grm_cargos, NUM_CARGO * 2, count, op, target, "cargos");
04665 if (_skip_sprites == -1) return;
04666 break;
04667
04668 default: grfmsg(1, "ParamSet: GRM: Unsupported feature 0x%X", feature); return;
04669 }
04670 } else {
04671
04672 src1 = 0;
04673 }
04674 }
04675 } else {
04676
04677 const GRFFile *file = GetFileByGRFID(data);
04678 GRFConfig *c = GetGRFConfig(data);
04679 if (c != NULL && HasBit(c->flags, GCF_STATIC) && !HasBit(_cur_grfconfig->flags, GCF_STATIC) && _networking) {
04680
04681 DisableStaticNewGRFInfluencingNonStaticNewGRFs(c);
04682 src1 = 0;
04683 } else if (file == NULL || src1 >= file->param_end || (c != NULL && c->status == GCS_DISABLED)) {
04684 src1 = 0;
04685 } else {
04686 src1 = file->param[src1];
04687 }
04688 }
04689 } else {
04690
04691
04692
04693
04694
04695 src1 = (src1 == 0xFF) ? data : GetParamVal(src1, NULL);
04696 src2 = (src2 == 0xFF) ? data : GetParamVal(src2, NULL);
04697 }
04698
04699
04700
04701
04702
04703
04704
04705 uint32 res;
04706 switch (oper) {
04707 case 0x00:
04708 res = src1;
04709 break;
04710
04711 case 0x01:
04712 res = src1 + src2;
04713 break;
04714
04715 case 0x02:
04716 res = src1 - src2;
04717 break;
04718
04719 case 0x03:
04720 res = src1 * src2;
04721 break;
04722
04723 case 0x04:
04724 res = (int32)src1 * (int32)src2;
04725 break;
04726
04727 case 0x05:
04728 if ((int32)src2 < 0) {
04729 res = src1 >> -(int32)src2;
04730 } else {
04731 res = src1 << src2;
04732 }
04733 break;
04734
04735 case 0x06:
04736 if ((int32)src2 < 0) {
04737 res = (int32)src1 >> -(int32)src2;
04738 } else {
04739 res = (int32)src1 << src2;
04740 }
04741 break;
04742
04743 case 0x07:
04744 res = src1 & src2;
04745 break;
04746
04747 case 0x08:
04748 res = src1 | src2;
04749 break;
04750
04751 case 0x09:
04752 if (src2 == 0) {
04753 res = src1;
04754 } else {
04755 res = src1 / src2;
04756 }
04757 break;
04758
04759 case 0x0A:
04760 if (src2 == 0) {
04761 res = src1;
04762 } else {
04763 res = (int32)src1 / (int32)src2;
04764 }
04765 break;
04766
04767 case 0x0B:
04768 if (src2 == 0) {
04769 res = src1;
04770 } else {
04771 res = src1 % src2;
04772 }
04773 break;
04774
04775 case 0x0C:
04776 if (src2 == 0) {
04777 res = src1;
04778 } else {
04779 res = (int32)src1 % (int32)src2;
04780 }
04781 break;
04782
04783 default: grfmsg(0, "ParamSet: Unknown operation %d, skipping", oper); return;
04784 }
04785
04786 switch (target) {
04787 case 0x8E:
04788 _traininfo_vehicle_pitch = res;
04789 break;
04790
04791 case 0x8F: {
04792 extern RailtypeInfo _railtypes[RAILTYPE_END];
04793 _railtypes[RAILTYPE_RAIL].cost_multiplier = GB(res, 0, 8);
04794 if (_settings_game.vehicle.disable_elrails) {
04795 _railtypes[RAILTYPE_ELECTRIC].cost_multiplier = GB(res, 0, 8);
04796 _railtypes[RAILTYPE_MONO].cost_multiplier = GB(res, 8, 8);
04797 } else {
04798 _railtypes[RAILTYPE_ELECTRIC].cost_multiplier = GB(res, 8, 8);
04799 _railtypes[RAILTYPE_MONO].cost_multiplier = GB(res, 16, 8);
04800 }
04801 _railtypes[RAILTYPE_MAGLEV].cost_multiplier = GB(res, 16, 8);
04802 break;
04803 }
04804
04805
04806 case 0x93:
04807 case 0x94:
04808 case 0x95:
04809 case 0x96:
04810 case 0x97:
04811 case 0x99:
04812 grfmsg(7, "ParamSet: Skipping unimplemented target 0x%02X", target);
04813 break;
04814
04815 case 0x9E:
04816 _misc_grf_features = res;
04817
04818 _traininfo_vehicle_width = HasGrfMiscBit(GMB_TRAIN_WIDTH_32_PIXELS) ? 32 : 29;
04819 break;
04820
04821 case 0x9F:
04822 grfmsg(7, "ParamSet: Skipping unimplemented target 0x%02X", target);
04823 break;
04824
04825 default:
04826 if (target < 0x80) {
04827 _cur_grffile->param[target] = res;
04828 if (target + 1U > _cur_grffile->param_end) _cur_grffile->param_end = target + 1;
04829 } else {
04830 grfmsg(7, "ParamSet: Skipping unknown target 0x%02X", target);
04831 }
04832 break;
04833 }
04834 }
04835
04836
04837 static void SafeGRFInhibit(byte *buf, size_t len)
04838 {
04839
04840
04841
04842
04843
04844 if (!check_length(len, 2, "GRFInhibit")) return;
04845 buf++;
04846 uint8 num = grf_load_byte(&buf);
04847 if (!check_length(len, 2 + 4 * num, "GRFInhibit")) return;
04848
04849 for (uint i = 0; i < num; i++) {
04850 uint32 grfid = grf_load_dword(&buf);
04851
04852
04853 if (grfid != _cur_grfconfig->grfid) {
04854 SetBit(_cur_grfconfig->flags, GCF_UNSAFE);
04855
04856
04857 _skip_sprites = -1;
04858
04859 return;
04860 }
04861 }
04862 }
04863
04864
04865 static void GRFInhibit(byte *buf, size_t len)
04866 {
04867
04868
04869
04870
04871
04872 if (!check_length(len, 2, "GRFInhibit")) return;
04873 buf++;
04874 uint8 num = grf_load_byte(&buf);
04875 if (!check_length(len, 2 + 4 * num, "GRFInhibit")) return;
04876
04877 for (uint i = 0; i < num; i++) {
04878 uint32 grfid = grf_load_dword(&buf);
04879 GRFConfig *file = GetGRFConfig(grfid);
04880
04881
04882 if (file != NULL && file != _cur_grfconfig) {
04883 grfmsg(2, "GRFInhibit: Deactivating file '%s'", file->filename);
04884 file->status = GCS_DISABLED;
04885 }
04886 }
04887 }
04888
04889
04890 static void FeatureTownName(byte *buf, size_t len)
04891 {
04892
04893
04894
04895
04896
04897
04898
04899 if (!check_length(len, 1, "FeatureTownName: definition ID")) return;
04900 buf++; len--;
04901
04902 uint32 grfid = _cur_grffile->grfid;
04903
04904 GRFTownName *townname = AddGRFTownName(grfid);
04905
04906 byte id = grf_load_byte(&buf);
04907 len--;
04908 grfmsg(6, "FeatureTownName: definition 0x%02X", id & 0x7F);
04909
04910 if (HasBit(id, 7)) {
04911
04912 ClrBit(id, 7);
04913 bool new_scheme = _cur_grffile->grf_version >= 7;
04914
04915 if (!check_length(len, 1, "FeatureTownName: lang_id")) return;
04916 byte lang = grf_load_byte(&buf);
04917 len--;
04918
04919 byte nb_gen = townname->nb_gen;
04920 do {
04921 ClrBit(lang, 7);
04922
04923 if (!check_length(len, 1, "FeatureTownName: style name")) return;
04924 const char *name = grf_load_string(&buf, len);
04925 len -= strlen(name) + 1;
04926
04927 char *lang_name = TranslateTTDPatchCodes(grfid, name);
04928 grfmsg(6, "FeatureTownName: lang 0x%X -> '%s'", lang, lang_name);
04929 free(lang_name);
04930
04931 townname->name[nb_gen] = AddGRFString(grfid, id, lang, new_scheme, name, STR_UNDEFINED);
04932
04933 if (!check_length(len, 1, "FeatureTownName: lang_id")) return;
04934 lang = grf_load_byte(&buf);
04935 len--;
04936 } while (lang != 0);
04937 townname->id[nb_gen] = id;
04938 townname->nb_gen++;
04939 }
04940
04941 if (!check_length(len, 1, "FeatureTownName: number of parts")) return;
04942 byte nb = grf_load_byte(&buf);
04943 len--;
04944 grfmsg(6, "FeatureTownName: %d parts", nb, nb);
04945
04946 townname->nbparts[id] = nb;
04947 townname->partlist[id] = CallocT<NamePartList>(nb);
04948
04949 for (int i = 0; i < nb; i++) {
04950 if (!check_length(len, 3, "FeatureTownName: parts header")) return;
04951 byte nbtext = grf_load_byte(&buf);
04952 townname->partlist[id][i].bitstart = grf_load_byte(&buf);
04953 townname->partlist[id][i].bitcount = grf_load_byte(&buf);
04954 townname->partlist[id][i].maxprob = 0;
04955 townname->partlist[id][i].partcount = nbtext;
04956 townname->partlist[id][i].parts = CallocT<NamePart>(nbtext);
04957 len -= 3;
04958 grfmsg(6, "FeatureTownName: part %d contains %d texts and will use GB(seed, %d, %d)", i, nbtext, townname->partlist[id][i].bitstart, townname->partlist[id][i].bitcount);
04959
04960 for (int j = 0; j < nbtext; j++) {
04961 if (!check_length(len, 2, "FeatureTownName: part")) return;
04962 byte prob = grf_load_byte(&buf);
04963 len--;
04964
04965 if (HasBit(prob, 7)) {
04966 byte ref_id = grf_load_byte(&buf);
04967 len--;
04968
04969 if (townname->nbparts[ref_id] == 0) {
04970 grfmsg(0, "FeatureTownName: definition 0x%02X doesn't exist, deactivating", ref_id);
04971 DelGRFTownName(grfid);
04972 _cur_grfconfig->status = GCS_DISABLED;
04973 ClearTemporaryNewGRFData(_cur_grffile);
04974 _skip_sprites = -1;
04975 return;
04976 }
04977
04978 grfmsg(6, "FeatureTownName: part %d, text %d, uses intermediate definition 0x%02X (with probability %d)", i, j, ref_id, prob & 0x7F);
04979 townname->partlist[id][i].parts[j].data.id = ref_id;
04980 } else {
04981 const char *text = grf_load_string(&buf, len);
04982 len -= strlen(text) + 1;
04983 townname->partlist[id][i].parts[j].data.text = TranslateTTDPatchCodes(grfid, text);
04984 grfmsg(6, "FeatureTownName: part %d, text %d, '%s' (with probability %d)", i, j, townname->partlist[id][i].parts[j].data.text, prob);
04985 }
04986 townname->partlist[id][i].parts[j].prob = prob;
04987 townname->partlist[id][i].maxprob += GB(prob, 0, 7);
04988 }
04989 grfmsg(6, "FeatureTownName: part %d, total probability %d", i, townname->partlist[id][i].maxprob);
04990 }
04991 }
04992
04993
04994 static void DefineGotoLabel(byte *buf, size_t len)
04995 {
04996
04997
04998
04999
05000
05001 if (!check_length(len, 1, "DefineGotoLabel")) return;
05002 buf++; len--;
05003
05004 GRFLabel *label = MallocT<GRFLabel>(1);
05005 label->label = grf_load_byte(&buf);
05006 label->nfo_line = _nfo_line;
05007 label->pos = FioGetPos();
05008 label->next = NULL;
05009
05010
05011 if (_cur_grffile->label == NULL) {
05012 _cur_grffile->label = label;
05013 } else {
05014
05015 GRFLabel *l;
05016 for (l = _cur_grffile->label; l->next != NULL; l = l->next) {}
05017 l->next = label;
05018 }
05019
05020 grfmsg(2, "DefineGotoLabel: GOTO target with label 0x%02X", label->label);
05021 }
05022
05023
05024 static void GRFSound(byte *buf, size_t len)
05025 {
05026
05027
05028
05029
05030 if (!check_length(len, 1, "GRFSound")) return;
05031 buf++;
05032 uint16 num = grf_load_word(&buf);
05033
05034 _grf_data_blocks = num;
05035 _grf_data_type = GDT_SOUND;
05036
05037 if (_cur_grffile->sound_offset == 0) _cur_grffile->sound_offset = GetNumSounds();
05038 }
05039
05040
05041 static void SkipAct11(byte *buf, size_t len)
05042 {
05043
05044
05045
05046
05047 if (!check_length(len, 1, "SkipAct11")) return;
05048 buf++;
05049 _skip_sprites = grf_load_word(&buf);
05050
05051 grfmsg(3, "SkipAct11: Skipping %d sprites", _skip_sprites);
05052 }
05053
05054 static void ImportGRFSound(byte *buf, int len)
05055 {
05056 const GRFFile *file;
05057 FileEntry *se = AllocateFileEntry();
05058 uint32 grfid = grf_load_dword(&buf);
05059 uint16 sound = grf_load_word(&buf);
05060
05061 file = GetFileByGRFID(grfid);
05062 if (file == NULL || file->sound_offset == 0) {
05063 grfmsg(1, "ImportGRFSound: Source file not available");
05064 return;
05065 }
05066
05067 if (file->sound_offset + sound >= GetNumSounds()) {
05068 grfmsg(1, "ImportGRFSound: Sound effect %d is invalid", sound);
05069 return;
05070 }
05071
05072 grfmsg(2, "ImportGRFSound: Copying sound %d (%d) from file %X", sound, file->sound_offset + sound, grfid);
05073
05074 *se = *GetSound(file->sound_offset + sound);
05075
05076
05077 se->volume = 128;
05078 se->priority = 0;
05079 }
05080
05081
05082 static void GRFImportBlock(byte *buf, int len)
05083 {
05084 if (_grf_data_blocks == 0) {
05085 grfmsg(2, "GRFImportBlock: Unexpected import block, skipping");
05086 return;
05087 }
05088
05089 buf++;
05090
05091 _grf_data_blocks--;
05092
05093
05094
05095 if (grf_load_byte(&buf) != _grf_data_type) {
05096 grfmsg(1, "GRFImportBlock: Import type mismatch");
05097 }
05098
05099 switch (_grf_data_type) {
05100 case GDT_SOUND: ImportGRFSound(buf, len - 1); break;
05101 default: NOT_REACHED(); break;
05102 }
05103 }
05104
05105 static void LoadGRFSound(byte *buf, int len)
05106 {
05107 byte *buf_start = buf;
05108
05109
05110
05111 FileEntry *se = AllocateFileEntry();
05112
05113 if (grf_load_dword(&buf) != BSWAP32('RIFF')) {
05114 grfmsg(1, "LoadGRFSound: Missing RIFF header");
05115 return;
05116 }
05117
05118
05119 grf_load_dword(&buf);
05120
05121 if (grf_load_dword(&buf) != BSWAP32('WAVE')) {
05122 grfmsg(1, "LoadGRFSound: Invalid RIFF type");
05123 return;
05124 }
05125
05126 for (;;) {
05127 uint32 tag = grf_load_dword(&buf);
05128 uint32 size = grf_load_dword(&buf);
05129
05130 switch (tag) {
05131 case ' tmf':
05132
05133 if (grf_load_word(&buf) != 1) {
05134 grfmsg(1, "LoadGRFSound: Invalid audio format");
05135 return;
05136 }
05137 se->channels = grf_load_word(&buf);
05138 se->rate = grf_load_dword(&buf);
05139 grf_load_dword(&buf);
05140 grf_load_word(&buf);
05141 se->bits_per_sample = grf_load_word(&buf);
05142
05143
05144 for (; size > 16; size--) grf_load_byte(&buf);
05145 break;
05146
05147 case 'atad':
05148 se->file_size = size;
05149 se->file_offset = FioGetPos() - (len - (buf - buf_start)) + 1;
05150 se->file_slot = _file_index;
05151
05152
05153 se->volume = 0x80;
05154 se->priority = 0;
05155
05156 grfmsg(2, "LoadGRFSound: channels %u, sample rate %u, bits per sample %u, length %u", se->channels, se->rate, se->bits_per_sample, size);
05157 return;
05158
05159 default:
05160 se->file_size = 0;
05161 return;
05162 }
05163 }
05164 }
05165
05166
05167 static void LoadFontGlyph(byte *buf, size_t len)
05168 {
05169
05170
05171
05172
05173
05174
05175
05176 buf++; len--;
05177 if (!check_length(len, 1, "LoadFontGlyph")) return;
05178
05179 uint8 num_def = grf_load_byte(&buf);
05180
05181 if (!check_length(len, 1 + num_def * 4, "LoadFontGlyph")) return;
05182
05183 for (uint i = 0; i < num_def; i++) {
05184 FontSize size = (FontSize)grf_load_byte(&buf);
05185 uint8 num_char = grf_load_byte(&buf);
05186 uint16 base_char = grf_load_word(&buf);
05187
05188 grfmsg(7, "LoadFontGlyph: Loading %u glyph(s) at 0x%04X for size %u", num_char, base_char, size);
05189
05190 for (uint c = 0; c < num_char; c++) {
05191 SetUnicodeGlyph(size, base_char + c, _cur_spriteid);
05192 _nfo_line++;
05193 LoadNextSprite(_cur_spriteid++, _file_index, _nfo_line);
05194 }
05195 }
05196 }
05197
05198
05199 static void SkipAct12(byte *buf, size_t len)
05200 {
05201
05202
05203
05204
05205
05206
05207
05208 buf++; len--;
05209 if (!check_length(len, 1, "SkipAct12")) return;
05210 uint8 num_def = grf_load_byte(&buf);
05211
05212 if (!check_length(len, 1 + num_def * 4, "SkipAct12")) return;
05213
05214 for (uint i = 0; i < num_def; i++) {
05215
05216 grf_load_byte(&buf);
05217
05218
05219 _skip_sprites += grf_load_byte(&buf);
05220
05221
05222 grf_load_word(&buf);
05223 }
05224
05225 grfmsg(3, "SkipAct12: Skipping %d sprites", _skip_sprites);
05226 }
05227
05228
05229 static void TranslateGRFStrings(byte *buf, size_t len)
05230 {
05231
05232
05233
05234
05235
05236
05237
05238 buf++; len--;
05239 if (!check_length(len, 7, "TranslateGRFString")) return;
05240
05241 uint32 grfid = grf_load_dword(&buf);
05242 const GRFConfig *c = GetGRFConfig(grfid);
05243 if (c == NULL || (c->status != GCS_INITIALISED && c->status != GCS_ACTIVATED)) {
05244 grfmsg(7, "TranslateGRFStrings: GRFID 0x%08x unknown, skipping action 13", BSWAP32(grfid));
05245 return;
05246 }
05247
05248 if (c->status == GCS_INITIALISED) {
05249
05250
05251 GRFError *error = CallocT<GRFError>(1);
05252
05253 char tmp[256];
05254 GetString(tmp, STR_NEWGRF_ERROR_AFTER_TRANSLATED_FILE, lastof(tmp));
05255 error->data = strdup(tmp);
05256
05257 error->message = STR_NEWGRF_ERROR_LOAD_AFTER;
05258 error->severity = STR_NEWGRF_ERROR_MSG_FATAL;
05259
05260 if (_cur_grfconfig->error != NULL) free(_cur_grfconfig->error);
05261 _cur_grfconfig->error = error;
05262
05263 _cur_grfconfig->status = GCS_DISABLED;
05264 ClearTemporaryNewGRFData(_cur_grffile);
05265 _skip_sprites = -1;
05266 return;
05267 }
05268
05269 byte num_strings = grf_load_byte(&buf);
05270 uint16 first_id = grf_load_word(&buf);
05271
05272 if (!((first_id >= 0xD000 && first_id + num_strings <= 0xD3FF) || (first_id >= 0xDC00 && first_id + num_strings <= 0xDCFF))) {
05273 grfmsg(7, "TranslateGRFStrings: Attempting to set out-of-range string IDs in action 13 (first: 0x%4X, number: 0x%2X)", first_id, num_strings);
05274 return;
05275 }
05276
05277 len -= 7;
05278
05279 for (uint i = 0; i < num_strings && len > 0; i++) {
05280 const char *string = grf_load_string(&buf, len);
05281 size_t string_length = strlen(string) + 1;
05282
05283 len -= (int)string_length;
05284
05285 if (string_length == 1) {
05286 grfmsg(7, "TranslateGRFString: Ignoring empty string.");
05287 continue;
05288 }
05289
05290
05291
05292
05293
05294
05295 AddGRFString(grfid, first_id + i, 0x7F, true, string, STR_UNDEFINED);
05296 }
05297 }
05298
05299
05300 static void GRFDataBlock(byte *buf, int len)
05301 {
05302 if (_grf_data_blocks == 0) {
05303 grfmsg(2, "GRFDataBlock: unexpected data block, skipping");
05304 return;
05305 }
05306
05307 buf++;
05308 uint8 name_len = grf_load_byte(&buf);
05309 const char *name = (const char *)buf;
05310 buf += name_len + 1;
05311
05312 grfmsg(2, "GRFDataBlock: block name '%s'...", name);
05313
05314 _grf_data_blocks--;
05315
05316 switch (_grf_data_type) {
05317 case GDT_SOUND: LoadGRFSound(buf, len - name_len - 2); break;
05318 default: NOT_REACHED(); break;
05319 }
05320 }
05321
05322
05323
05324 static void GRFUnsafe(byte *buf, size_t len)
05325 {
05326 SetBit(_cur_grfconfig->flags, GCF_UNSAFE);
05327
05328
05329 _skip_sprites = -1;
05330 }
05331
05332
05333 static void InitializeGRFSpecial()
05334 {
05335 _ttdpatch_flags[0] = ((_settings_game.station.always_small_airport ? 1 : 0) << 0x0C)
05336 | (1 << 0x0D)
05337 | (1 << 0x0E)
05338 | ((_settings_game.construction.longbridges ? 1 : 0) << 0x0F)
05339 | (0 << 0x10)
05340 | (1 << 0x12)
05341 | (1 << 0x13)
05342 | ((_settings_game.vehicle.never_expire_vehicles ? 1 : 0) << 0x16)
05343 | (1 << 0x1B)
05344 | (1 << 0x1D)
05345 | (1 << 0x1E);
05346
05347 _ttdpatch_flags[1] = ((_settings_game.economy.station_noise_level ? 1 : 0) << 0x07)
05348 | ((_settings_game.vehicle.mammoth_trains ? 1 : 0) << 0x08)
05349 | (1 << 0x09)
05350 | (0 << 0x0B)
05351 | ((_settings_game.order.gradual_loading ? 1 : 0) << 0x0C)
05352 | (1 << 0x12)
05353 | (1 << 0x13)
05354 | (1 << 0x14)
05355 | (1 << 0x16)
05356 | (1 << 0x17)
05357 | (1 << 0x18)
05358 | (1 << 0x19)
05359 | (1 << 0x1A)
05360 | ((_settings_game.construction.signal_side ? 1 : 0) << 0x1B)
05361 | ((_settings_game.vehicle.disable_elrails ? 0 : 1) << 0x1C);
05362
05363 _ttdpatch_flags[2] = (1 << 0x01)
05364 | (1 << 0x03)
05365 | (0 << 0x0B)
05366 | (0 << 0x0C)
05367 | ((_settings_game.construction.build_on_slopes ? 1 : 0) << 0x0D)
05368 | (1 << 0x0E)
05369 | (1 << 0x0F)
05370 | (0 << 0x10)
05371 | (0 << 0x11)
05372 | (1 << 0x12)
05373 | (1 << 0x13)
05374 | (1 << 0x14)
05375 | ((_settings_game.construction.build_on_slopes ? 1 : 0) << 0x15)
05376 | (1 << 0x16)
05377 | (1 << 0x17)
05378 | ((_settings_game.vehicle.freight_trains > 1 ? 1 : 0) << 0x18)
05379 | (1 << 0x19)
05380 | (1 << 0x1A)
05381 | (1 << 0x1B)
05382 | (1 << 0x1C)
05383 | ((_settings_game.vehicle.wagon_speed_limits ? 1 : 0) << 0x1D)
05384 | (1 << 0x1E)
05385 | (0 << 0x1F);
05386
05387 _ttdpatch_flags[3] = (0 << 0x00)
05388 | (1 << 0x01)
05389 | ((_settings_game.economy.allow_town_roads || _generating_world ? 0 : 1) << 0x02)
05390 | (1 << 0x03)
05391 | (0 << 0x04)
05392 | (1 << 0x05)
05393 | (1 << 0x06)
05394 | (1 << 0x07)
05395 | ((_settings_game.order.improved_load ? 1 : 0) << 0x08)
05396 | (0 << 0x09)
05397 | (0 << 0x0A)
05398 | (1 << 0x0B)
05399 | (1 << 0x0C)
05400 | (1 << 0x0D)
05401 | ((_settings_game.station.nonuniform_stations ? 1 : 0) << 0x0E)
05402 | (1 << 0x0F)
05403 | (1 << 0x10)
05404 | (1 << 0x11)
05405 | (1 << 0x12)
05406 | (0 << 0x13)
05407 | (1 << 0x14)
05408 | (0 << 0x15)
05409 | (1 << 0x16)
05410 | (1 << 0x17)
05411 | ((_settings_game.vehicle.dynamic_engines ? 1 : 0) << 0x18)
05412 | (1 << 0x1E)
05413 | (1 << 0x1F);
05414 }
05415
05416 static void ResetCustomStations()
05417 {
05418 for (GRFFile *file = _first_grffile; file != NULL; file = file->next) {
05419 if (file->stations == NULL) continue;
05420 for (uint i = 0; i < MAX_STATIONS; i++) {
05421 if (file->stations[i] == NULL) continue;
05422 StationSpec *statspec = file->stations[i];
05423
05424
05425 if (!statspec->copied_renderdata) {
05426 for (uint t = 0; t < statspec->tiles; t++) {
05427 free((void*)statspec->renderdata[t].seq);
05428 }
05429 free(statspec->renderdata);
05430 }
05431
05432
05433 if (!statspec->copied_layouts) {
05434 for (uint l = 0; l < statspec->lengths; l++) {
05435 for (uint p = 0; p < statspec->platforms[l]; p++) {
05436 free(statspec->layouts[l][p]);
05437 }
05438 free(statspec->layouts[l]);
05439 }
05440 free(statspec->layouts);
05441 free(statspec->platforms);
05442 }
05443
05444
05445 free(statspec);
05446 }
05447
05448
05449 free(file->stations);
05450 file->stations = NULL;
05451 }
05452 }
05453
05454 static void ResetCustomHouses()
05455 {
05456 GRFFile *file;
05457 uint i;
05458
05459 for (file = _first_grffile; file != NULL; file = file->next) {
05460 if (file->housespec == NULL) continue;
05461 for (i = 0; i < HOUSE_MAX; i++) {
05462 free(file->housespec[i]);
05463 }
05464
05465 free(file->housespec);
05466 file->housespec = NULL;
05467 }
05468 }
05469
05470 static void ResetCustomIndustries()
05471 {
05472 GRFFile *file;
05473
05474 for (file = _first_grffile; file != NULL; file = file->next) {
05475 uint i;
05476
05477
05478 if (file->industryspec != NULL) {
05479
05480 for (i = 0; i < NUM_INDUSTRYTYPES; i++) {
05481 IndustrySpec *ind = file->industryspec[i];
05482
05483 if (ind != NULL) {
05484
05485 if (HasBit(ind->cleanup_flag, CLEAN_RANDOMSOUNDS)) {
05486 free((void*)ind->random_sounds);
05487 }
05488
05489
05490 if (HasBit(ind->cleanup_flag, CLEAN_TILELSAYOUT) && ind->table != NULL) {
05491 for (int j = 0; j < ind->num_table; j++) {
05492
05493 if (ind->table[j] != NULL) {
05494 free((IndustryTileTable*)ind->table[j]);
05495 }
05496 }
05497
05498 free((IndustryTileTable**)ind->table);
05499 ind->table = NULL;
05500 }
05501
05502 free(ind);
05503 ind = NULL;
05504 }
05505 }
05506
05507 free(file->industryspec);
05508 file->industryspec = NULL;
05509 }
05510
05511 if (file->indtspec != NULL) {
05512 for (i = 0; i < NUM_INDUSTRYTILES; i++) {
05513 if (file->indtspec[i] != NULL) {
05514 free(file->indtspec[i]);
05515 file->indtspec[i] = NULL;
05516 }
05517 }
05518
05519 free(file->indtspec);
05520 file->indtspec = NULL;
05521 }
05522 }
05523 }
05524
05525 static void ResetNewGRF()
05526 {
05527 GRFFile *next;
05528
05529 for (GRFFile *f = _first_grffile; f != NULL; f = next) {
05530 next = f->next;
05531
05532 free(f->filename);
05533 free(f->cargo_list);
05534 free(f->railtype_list);
05535 free(f);
05536 }
05537
05538 _first_grffile = NULL;
05539 _cur_grffile = NULL;
05540 }
05541
05542 static void ResetNewGRFErrors()
05543 {
05544 for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
05545 if (!HasBit(c->flags, GCF_COPY) && c->error != NULL) {
05546 free(c->error->custom_message);
05547 free(c->error->data);
05548 free(c->error);
05549 c->error = NULL;
05550 }
05551 }
05552 }
05553
05558 static void ResetNewGRFData()
05559 {
05560 CleanUpStrings();
05561 CleanUpGRFTownNames();
05562
05563
05564 SetupEngines();
05565
05566
05567 ResetBridges();
05568
05569
05570 ResetRailTypes();
05571
05572
05573 _gted = CallocT<GRFTempEngineData>(GetEnginePoolSize());
05574
05575
05576 memset(&_grm_engines, 0, sizeof(_grm_engines));
05577 memset(&_grm_cargos, 0, sizeof(_grm_cargos));
05578
05579
05580 ResetGenericCallbacks();
05581
05582
05583 ResetPriceBaseMultipliers();
05584
05585
05586 ResetCurrencies();
05587
05588
05589 ResetCustomHouses();
05590 ResetHouses();
05591
05592
05593 ResetCustomIndustries();
05594 ResetIndustries();
05595
05596
05597 ResetStationClasses();
05598 ResetCustomStations();
05599
05600
05601 memset(_water_feature, 0, sizeof(_water_feature));
05602
05603
05604 ClearSnowLine();
05605
05606
05607 ResetNewGRF();
05608
05609
05610 ResetNewGRFErrors();
05611
05612
05613 SetupCargoForClimate(_settings_game.game_creation.landscape);
05614
05615
05616 _misc_grf_features = 0;
05617 _traininfo_vehicle_pitch = 0;
05618 _traininfo_vehicle_width = 29;
05619
05620 _loaded_newgrf_features.has_2CC = false;
05621 _loaded_newgrf_features.has_newhouses = false;
05622 _loaded_newgrf_features.has_newindustries = false;
05623 _loaded_newgrf_features.shore = SHORE_REPLACE_NONE;
05624
05625
05626 _grf_id_overrides.clear();
05627
05628 InitializeSoundPool();
05629 InitializeSpriteGroupPool();
05630 }
05631
05632 static void BuildCargoTranslationMap()
05633 {
05634 memset(_cur_grffile->cargo_map, 0xFF, sizeof(_cur_grffile->cargo_map));
05635
05636 for (CargoID c = 0; c < NUM_CARGO; c++) {
05637 const CargoSpec *cs = GetCargo(c);
05638 if (!cs->IsValid()) continue;
05639
05640 if (_cur_grffile->cargo_max == 0) {
05641
05642 _cur_grffile->cargo_map[c] = cs->bitnum;
05643 } else {
05644
05645 for (uint i = 0; i < _cur_grffile->cargo_max; i++) {
05646 if (cs->label == _cur_grffile->cargo_list[i]) {
05647 _cur_grffile->cargo_map[c] = i;
05648 break;
05649 }
05650 }
05651 }
05652 }
05653 }
05654
05655 static void InitNewGRFFile(const GRFConfig *config, int sprite_offset)
05656 {
05657 GRFFile *newfile = GetFileByFilename(config->filename);
05658 if (newfile != NULL) {
05659
05660 newfile->sprite_offset = sprite_offset;
05661 _cur_grffile = newfile;
05662 return;
05663 }
05664
05665 newfile = CallocT<GRFFile>(1);
05666
05667 if (newfile == NULL) error ("Out of memory");
05668
05669 newfile->filename = strdup(config->filename);
05670 newfile->sprite_offset = sprite_offset;
05671
05672
05673 assert(lengthof(newfile->param) == lengthof(config->param) && lengthof(config->param) == 0x80);
05674 newfile->param_end = config->num_params;
05675 memcpy(newfile->param, config->param, sizeof(newfile->param));
05676
05677 if (_first_grffile == NULL) {
05678 _cur_grffile = newfile;
05679 _first_grffile = newfile;
05680 } else {
05681 _cur_grffile->next = newfile;
05682 _cur_grffile = newfile;
05683 }
05684 }
05685
05686
05689 static const CargoLabel _default_refitmasks_rail[] = {
05690 'PASS', 'COAL', 'MAIL', 'LVST', 'GOOD', 'GRAI', 'WHEA', 'MAIZ', 'WOOD',
05691 'IORE', 'STEL', 'VALU', 'GOLD', 'DIAM', 'PAPR', 'FOOD', 'FRUT', 'CORE',
05692 'WATR', 'SUGR', 'TOYS', 'BATT', 'SWET', 'TOFF', 'COLA', 'CTCD', 'BUBL',
05693 'PLST', 'FZDR',
05694 0 };
05695
05696 static const CargoLabel _default_refitmasks_road[] = {
05697 0 };
05698
05699 static const CargoLabel _default_refitmasks_ships[] = {
05700 'COAL', 'MAIL', 'LVST', 'GOOD', 'GRAI', 'WHEA', 'MAIZ', 'WOOD', 'IORE',
05701 'STEL', 'VALU', 'GOLD', 'DIAM', 'PAPR', 'FOOD', 'FRUT', 'CORE', 'WATR',
05702 'RUBR', 'SUGR', 'TOYS', 'BATT', 'SWET', 'TOFF', 'COLA', 'CTCD', 'BUBL',
05703 'PLST', 'FZDR',
05704 0 };
05705
05706 static const CargoLabel _default_refitmasks_aircraft[] = {
05707 'PASS', 'MAIL', 'GOOD', 'VALU', 'GOLD', 'DIAM', 'FOOD', 'FRUT', 'SUGR',
05708 'TOYS', 'BATT', 'SWET', 'TOFF', 'COLA', 'CTCD', 'BUBL', 'PLST', 'FZDR',
05709 0 };
05710
05711 static const CargoLabel *_default_refitmasks[] = {
05712 _default_refitmasks_rail,
05713 _default_refitmasks_road,
05714 _default_refitmasks_ships,
05715 _default_refitmasks_aircraft,
05716 };
05717
05718
05722 static void CalculateRefitMasks()
05723 {
05724 Engine *e;
05725
05726 FOR_ALL_ENGINES(e) {
05727 EngineID engine = e->index;
05728 EngineInfo *ei = &e->info;
05729 uint32 mask = 0;
05730 uint32 not_mask = 0;
05731 uint32 xor_mask = 0;
05732
05733 if (ei->refit_mask != 0) {
05734 const GRFFile *file = e->grffile;
05735 if (file != NULL && file->cargo_max != 0) {
05736
05737 uint num_cargo = min(32, file->cargo_max);
05738 for (uint i = 0; i < num_cargo; i++) {
05739 if (!HasBit(ei->refit_mask, i)) continue;
05740
05741 CargoID c = GetCargoIDByLabel(file->cargo_list[i]);
05742 if (c == CT_INVALID) continue;
05743
05744 SetBit(xor_mask, c);
05745 }
05746 } else {
05747
05748 for (CargoID c = 0; c < NUM_CARGO; c++) {
05749 const CargoSpec *cs = GetCargo(c);
05750 if (!cs->IsValid()) continue;
05751
05752 if (HasBit(ei->refit_mask, cs->bitnum)) SetBit(xor_mask, c);
05753 }
05754 }
05755 }
05756
05757 if (_gted[engine].cargo_allowed != 0) {
05758
05759 for (CargoID i = 0; i < NUM_CARGO; i++) {
05760 const CargoSpec *cs = GetCargo(i);
05761 if (_gted[engine].cargo_allowed & cs->classes) SetBit(mask, i);
05762 if (_gted[engine].cargo_disallowed & cs->classes) SetBit(not_mask, i);
05763 }
05764 } else if (xor_mask == 0) {
05765
05766 if (e->type != VEH_TRAIN || (e->u.rail.capacity != 0 && e->u.rail.railveh_type != RAILVEH_WAGON)) {
05767 const CargoLabel *cl = _default_refitmasks[e->type];
05768 for (uint i = 0;; i++) {
05769 if (cl[i] == 0) break;
05770
05771 CargoID cargo = GetCargoIDByLabel(cl[i]);
05772 if (cargo == CT_INVALID) continue;
05773
05774 SetBit(xor_mask, cargo);
05775 }
05776 }
05777 }
05778
05779 ei->refit_mask = ((mask & ~not_mask) ^ xor_mask) & _cargo_mask;
05780
05781
05782
05783 switch (e->type) {
05784 default: NOT_REACHED();
05785 case VEH_AIRCRAFT:
05786 if (FindFirstRefittableCargo(engine) == CT_INVALID) ei->climates = 0x80;
05787 break;
05788
05789 case VEH_TRAIN: {
05790 RailVehicleInfo *rvi = &e->u.rail;
05791 if (rvi->cargo_type == CT_INVALID) rvi->cargo_type = FindFirstRefittableCargo(engine);
05792 if (rvi->cargo_type == CT_INVALID) ei->climates = 0x80;
05793 break;
05794 }
05795 case VEH_ROAD: {
05796 RoadVehicleInfo *rvi = &e->u.road;
05797 if (rvi->cargo_type == CT_INVALID) rvi->cargo_type = FindFirstRefittableCargo(engine);
05798 if (rvi->cargo_type == CT_INVALID) ei->climates = 0x80;
05799 break;
05800 }
05801 case VEH_SHIP: {
05802 ShipVehicleInfo *svi = &e->u.ship;
05803 if (svi->cargo_type == CT_INVALID) svi->cargo_type = FindFirstRefittableCargo(engine);
05804 if (svi->cargo_type == CT_INVALID) ei->climates = 0x80;
05805 break;
05806 }
05807 }
05808 }
05809 }
05810
05815 static void FinaliseHouseArray()
05816 {
05817
05818
05819
05820
05821
05822
05823
05824
05825
05826 Year min_year = MAX_YEAR;
05827
05828 for (GRFFile *file = _first_grffile; file != NULL; file = file->next) {
05829 if (file->housespec == NULL) continue;
05830
05831 for (int i = 0; i < HOUSE_MAX; i++) {
05832 HouseSpec *hs = file->housespec[i];
05833 if (hs != NULL) {
05834 _house_mngr.SetEntitySpec(hs);
05835 if (hs->min_year < min_year) min_year = hs->min_year;
05836 }
05837 }
05838 }
05839
05840 if (min_year != 0) {
05841 for (int i = 0; i < HOUSE_MAX; i++) {
05842 HouseSpec *hs = GetHouseSpecs(i);
05843
05844 if (hs->enabled && hs->min_year == min_year) hs->min_year = 0;
05845 }
05846 }
05847 }
05848
05852 static void FinaliseIndustriesArray()
05853 {
05854 for (GRFFile *file = _first_grffile; file != NULL; file = file->next) {
05855 if (file->industryspec != NULL) {
05856 for (int i = 0; i < NUM_INDUSTRYTYPES; i++) {
05857 IndustrySpec *indsp = file->industryspec[i];
05858
05859 if (indsp != NULL && indsp->enabled) {
05860 StringID strid;
05861
05862
05863
05864 strid = GetGRFStringID(indsp->grf_prop.grffile->grfid, indsp->name);
05865 if (strid != STR_UNDEFINED) indsp->name = strid;
05866
05867 strid = GetGRFStringID(indsp->grf_prop.grffile->grfid, indsp->closure_text);
05868 if (strid != STR_UNDEFINED) indsp->closure_text = strid;
05869
05870 strid = GetGRFStringID(indsp->grf_prop.grffile->grfid, indsp->production_up_text);
05871 if (strid != STR_UNDEFINED) indsp->production_up_text = strid;
05872
05873 strid = GetGRFStringID(indsp->grf_prop.grffile->grfid, indsp->production_down_text);
05874 if (strid != STR_UNDEFINED) indsp->production_down_text = strid;
05875
05876 strid = GetGRFStringID(indsp->grf_prop.grffile->grfid, indsp->new_industry_text);
05877 if (strid != STR_UNDEFINED) indsp->new_industry_text = strid;
05878
05879 if (indsp->station_name != STR_NULL) {
05880
05881
05882 strid = GetGRFStringID(indsp->grf_prop.grffile->grfid, indsp->station_name);
05883 if (strid != STR_UNDEFINED) indsp->station_name = strid;
05884 }
05885
05886 _industry_mngr.SetEntitySpec(indsp);
05887 _loaded_newgrf_features.has_newindustries = true;
05888 }
05889 }
05890 }
05891
05892 if (file->indtspec != NULL) {
05893 for (int i = 0; i < NUM_INDUSTRYTILES; i++) {
05894 IndustryTileSpec *indtsp = file->indtspec[i];
05895 if (indtsp != NULL) {
05896 _industile_mngr.SetEntitySpec(indtsp);
05897 }
05898 }
05899 }
05900 }
05901
05902 for (uint j = 0; j < NUM_INDUSTRYTYPES; j++) {
05903 IndustrySpec *indsp = &_industry_specs[j];
05904 if (indsp->enabled && indsp->grf_prop.grffile != NULL) {
05905 for (uint i = 0; i < 3; i++) {
05906 indsp->conflicting[i] = MapNewGRFIndustryType(indsp->conflicting[i], indsp->grf_prop.grffile->grfid);
05907 }
05908 }
05909 }
05910 }
05911
05912
05913
05914
05915
05916
05917
05918 static void DecodeSpecialSprite(byte *buf, uint num, GrfLoadingStage stage)
05919 {
05920
05921
05922
05923
05924
05925
05926
05927
05928
05929
05930
05931
05932 static const SpecialSpriteHandler handlers[][GLS_END] = {
05933 { NULL, SafeChangeInfo, NULL, NULL, ReserveChangeInfo, FeatureChangeInfo, },
05934 { SkipAct1, SkipAct1, SkipAct1, SkipAct1, SkipAct1, NewSpriteSet, },
05935 { NULL, NULL, NULL, NULL, NULL, NewSpriteGroup, },
05936 { NULL, GRFUnsafe, NULL, NULL, NULL, FeatureMapSpriteGroup, },
05937 { NULL, NULL, NULL, NULL, NULL, FeatureNewName, },
05938 { SkipAct5, SkipAct5, SkipAct5, SkipAct5, SkipAct5, GraphicsNew, },
05939 { NULL, NULL, NULL, CfgApply, CfgApply, CfgApply, },
05940 { NULL, NULL, NULL, NULL, SkipIf, SkipIf, },
05941 { ScanInfo, NULL, NULL, GRFInfo, GRFInfo, GRFInfo, },
05942 { NULL, NULL, NULL, SkipIf, SkipIf, SkipIf, },
05943 { SkipActA, SkipActA, SkipActA, SkipActA, SkipActA, SpriteReplace, },
05944 { NULL, NULL, NULL, GRFLoadError, GRFLoadError, GRFLoadError, },
05945 { NULL, NULL, NULL, GRFComment, NULL, GRFComment, },
05946 { NULL, SafeParamSet, NULL, ParamSet, ParamSet, ParamSet, },
05947 { NULL, SafeGRFInhibit, NULL, GRFInhibit, GRFInhibit, GRFInhibit, },
05948 { NULL, GRFUnsafe, NULL, FeatureTownName, NULL, NULL, },
05949 { NULL, NULL, DefineGotoLabel, NULL, NULL, NULL, },
05950 { SkipAct11,GRFUnsafe, SkipAct11, SkipAct11, SkipAct11, GRFSound, },
05951 { SkipAct12, SkipAct12, SkipAct12, SkipAct12, SkipAct12, LoadFontGlyph, },
05952 { NULL, NULL, NULL, NULL, NULL, TranslateGRFStrings, },
05953 };
05954
05955 GRFLocation location(_cur_grfconfig->grfid, _nfo_line);
05956
05957 GRFLineToSpriteOverride::iterator it = _grf_line_to_action6_sprite_override.find(location);
05958 if (it == _grf_line_to_action6_sprite_override.end()) {
05959
05960
05961 FioReadBlock(buf, num);
05962 } else {
05963
05964 buf = _grf_line_to_action6_sprite_override[location];
05965 grfmsg(7, "DecodeSpecialSprite: Using preloaded pseudo sprite data");
05966
05967
05968 FioSeekTo(num, SEEK_CUR);
05969 }
05970
05971 byte action = buf[0];
05972
05973 if (action == 0xFF) {
05974 grfmsg(7, "DecodeSpecialSprite: Handling data block in stage %d", stage);
05975 GRFDataBlock(buf, num);
05976 } else if (action == 0xFE) {
05977 grfmsg(7, "DecodeSpecialSprite: Handling import block in stage %d", stage);
05978 GRFImportBlock(buf, num);
05979 } else if (action >= lengthof(handlers)) {
05980 grfmsg(7, "DecodeSpecialSprite: Skipping unknown action 0x%02X", action);
05981 } else if (handlers[action][stage] == NULL) {
05982 grfmsg(7, "DecodeSpecialSprite: Skipping action 0x%02X in stage %d", action, stage);
05983 } else {
05984 grfmsg(7, "DecodeSpecialSprite: Handling action 0x%02X in stage %d", action, stage);
05985 handlers[action][stage](buf, num);
05986 }
05987 }
05988
05989
05990 void LoadNewGRFFile(GRFConfig *config, uint file_index, GrfLoadingStage stage)
05991 {
05992 const char *filename = config->filename;
05993 uint16 num;
05994
05995
05996
05997
05998
05999
06000
06001
06002
06003
06004 if (stage != GLS_FILESCAN && stage != GLS_SAFETYSCAN && stage != GLS_LABELSCAN) {
06005 _cur_grffile = GetFileByFilename(filename);
06006 if (_cur_grffile == NULL) usererror("File '%s' lost in cache.\n", filename);
06007 if (stage == GLS_RESERVE && config->status != GCS_INITIALISED) return;
06008 if (stage == GLS_ACTIVATION && !HasBit(config->flags, GCF_RESERVED)) return;
06009 _cur_grffile->is_ottdfile = config->IsOpenTTDBaseGRF();
06010 }
06011
06012 if (file_index > LAST_GRF_SLOT) {
06013 DEBUG(grf, 0, "'%s' is not loaded as the maximum number of GRFs has been reached", filename);
06014 config->status = GCS_DISABLED;
06015 config->error = CallocT<GRFError>(1);
06016 config->error->severity = STR_NEWGRF_ERROR_MSG_FATAL;
06017 config->error->message = STR_NEWGRF_ERROR_TOO_MANY_NEWGRFS_LOADED;
06018 return;
06019 }
06020
06021 FioOpenFile(file_index, filename);
06022 _file_index = file_index;
06023 _palette_remap_grf[_file_index] = (config->windows_paletted != (_use_palette == PAL_WINDOWS));
06024
06025 _cur_grfconfig = config;
06026
06027 DEBUG(grf, 2, "LoadNewGRFFile: Reading NewGRF-file '%s'", filename);
06028
06029
06030
06031
06032 if (FioReadWord() == 4 && FioReadByte() == 0xFF) {
06033 FioReadDword();
06034 } else {
06035 DEBUG(grf, 7, "LoadNewGRFFile: Custom .grf has invalid format");
06036 return;
06037 }
06038
06039 _skip_sprites = 0;
06040 _nfo_line = 0;
06041
06042 ReusableBuffer<byte> buf;
06043
06044 while ((num = FioReadWord()) != 0) {
06045 byte type = FioReadByte();
06046 _nfo_line++;
06047
06048 if (type == 0xFF) {
06049 if (_skip_sprites == 0) {
06050 DecodeSpecialSprite(buf.Allocate(num), num, stage);
06051
06052
06053 if (_skip_sprites == -1) break;
06054
06055 continue;
06056 } else {
06057 FioSkipBytes(num);
06058 }
06059 } else {
06060 if (_skip_sprites == 0) {
06061 grfmsg(0, "LoadNewGRFFile: Unexpected sprite, disabling");
06062 config->status = GCS_DISABLED;
06063 config->error = CallocT<GRFError>(1);
06064 config->error->severity = STR_NEWGRF_ERROR_MSG_FATAL;
06065 config->error->message = STR_NEWGRF_ERROR_UNEXPECTED_SPRITE;
06066 break;
06067 }
06068
06069 FioSkipBytes(7);
06070 SkipSpriteData(type, num - 8);
06071 }
06072
06073 if (_skip_sprites > 0) _skip_sprites--;
06074 }
06075 }
06076
06084 static void ActivateOldShore()
06085 {
06086
06087
06088 if (_loaded_newgrf_features.shore == SHORE_REPLACE_NONE) _loaded_newgrf_features.shore = SHORE_REPLACE_ACTION_A;
06089
06090 if (_loaded_newgrf_features.shore != SHORE_REPLACE_ACTION_5) {
06091 DupSprite(SPR_ORIGINALSHORE_START + 1, SPR_SHORE_BASE + 1);
06092 DupSprite(SPR_ORIGINALSHORE_START + 2, SPR_SHORE_BASE + 2);
06093 DupSprite(SPR_ORIGINALSHORE_START + 6, SPR_SHORE_BASE + 3);
06094 DupSprite(SPR_ORIGINALSHORE_START , SPR_SHORE_BASE + 4);
06095 DupSprite(SPR_ORIGINALSHORE_START + 4, SPR_SHORE_BASE + 6);
06096 DupSprite(SPR_ORIGINALSHORE_START + 3, SPR_SHORE_BASE + 8);
06097 DupSprite(SPR_ORIGINALSHORE_START + 7, SPR_SHORE_BASE + 9);
06098 DupSprite(SPR_ORIGINALSHORE_START + 5, SPR_SHORE_BASE + 12);
06099 }
06100
06101 if (_loaded_newgrf_features.shore == SHORE_REPLACE_ACTION_A) {
06102 DupSprite(SPR_FLAT_GRASS_TILE + 16, SPR_SHORE_BASE + 0);
06103 DupSprite(SPR_FLAT_GRASS_TILE + 17, SPR_SHORE_BASE + 5);
06104 DupSprite(SPR_FLAT_GRASS_TILE + 7, SPR_SHORE_BASE + 7);
06105 DupSprite(SPR_FLAT_GRASS_TILE + 15, SPR_SHORE_BASE + 10);
06106 DupSprite(SPR_FLAT_GRASS_TILE + 11, SPR_SHORE_BASE + 11);
06107 DupSprite(SPR_FLAT_GRASS_TILE + 13, SPR_SHORE_BASE + 13);
06108 DupSprite(SPR_FLAT_GRASS_TILE + 14, SPR_SHORE_BASE + 14);
06109 DupSprite(SPR_FLAT_GRASS_TILE + 18, SPR_SHORE_BASE + 15);
06110
06111
06112
06113 DupSprite(SPR_FLAT_GRASS_TILE + 5, SPR_SHORE_BASE + 16);
06114 DupSprite(SPR_FLAT_GRASS_TILE + 10, SPR_SHORE_BASE + 17);
06115 }
06116 }
06117
06118 void InitDepotWindowBlockSizes();
06119
06120 extern void InitGRFTownGeneratorNames();
06121
06122 static void AfterLoadGRFs()
06123 {
06124 for (StringIDToGRFIDMapping::iterator it = _string_to_grf_mapping.begin(); it != _string_to_grf_mapping.end(); it++) {
06125 *((*it).first) = MapGRFStringID((*it).second, *((*it).first));
06126 }
06127 _string_to_grf_mapping.clear();
06128
06129
06130 for (GRFLineToSpriteOverride::iterator it = _grf_line_to_action6_sprite_override.begin(); it != _grf_line_to_action6_sprite_override.end(); it++) {
06131 free((*it).second);
06132 }
06133 _grf_line_to_action6_sprite_override.clear();
06134
06135
06136 CalculateRefitMasks();
06137
06138
06139 InitDepotWindowBlockSizes();
06140
06141
06142 FinaliseHouseArray();
06143
06144
06145 FinaliseIndustriesArray();
06146
06147
06148 BuildIndustriesLegend();
06149
06150
06151 InitGRFTownGeneratorNames();
06152
06153
06154 CommitVehicleListOrderChanges();
06155
06156
06157 ActivateOldShore();
06158
06159 Engine *e;
06160 FOR_ALL_ENGINES_OF_TYPE(e, VEH_ROAD) {
06161 if (_gted[e->index].rv_max_speed != 0) {
06162
06163 e->u.road.max_speed = _gted[e->index].rv_max_speed * 4;
06164 }
06165 }
06166
06167 SetYearEngineAgingStops();
06168
06169
06170 free(_gted);
06171 _grm_sprites.clear();
06172 }
06173
06174 void LoadNewGRF(uint load_index, uint file_index)
06175 {
06176
06177
06178
06179
06180 Date date = _date;
06181 Year year = _cur_year;
06182 DateFract date_fract = _date_fract;
06183 uint16 tick_counter = _tick_counter;
06184 byte display_opt = _display_opt;
06185
06186 if (_networking) {
06187 _cur_year = _settings_game.game_creation.starting_year;
06188 _date = ConvertYMDToDate(_cur_year, 0, 1);
06189 _date_fract = 0;
06190 _tick_counter = 0;
06191 _display_opt = 0;
06192 }
06193
06194 InitializeGRFSpecial();
06195
06196 ResetNewGRFData();
06197
06198
06199
06200
06201
06202
06203
06204
06205 for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
06206 if (c->status != GCS_NOT_FOUND) c->status = GCS_UNKNOWN;
06207 }
06208
06209 _cur_spriteid = load_index;
06210
06211
06212
06213
06214 for (GrfLoadingStage stage = GLS_LABELSCAN; stage <= GLS_ACTIVATION; stage++) {
06215
06216
06217 for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
06218 if (c->status == GCS_ACTIVATED) c->status = GCS_INITIALISED;
06219 }
06220
06221 uint slot = file_index;
06222
06223 _cur_stage = stage;
06224 for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
06225 if (c->status == GCS_DISABLED || c->status == GCS_NOT_FOUND) continue;
06226 if (stage > GLS_INIT && HasBit(c->flags, GCF_INIT_ONLY)) continue;
06227
06228
06229 if (!FioCheckFileExists(c->filename)) usererror("NewGRF file is missing '%s'", c->filename);
06230
06231 if (stage == GLS_LABELSCAN) InitNewGRFFile(c, _cur_spriteid);
06232 LoadNewGRFFile(c, slot++, stage);
06233 if (stage == GLS_RESERVE) {
06234 SetBit(c->flags, GCF_RESERVED);
06235 } else if (stage == GLS_ACTIVATION) {
06236 ClrBit(c->flags, GCF_RESERVED);
06237 assert(GetFileByGRFID(c->grfid) == _cur_grffile);
06238 ClearTemporaryNewGRFData(_cur_grffile);
06239 BuildCargoTranslationMap();
06240 DEBUG(sprite, 2, "LoadNewGRF: Currently %i sprites are loaded", _cur_spriteid);
06241 } else if (stage == GLS_INIT && HasBit(c->flags, GCF_INIT_ONLY)) {
06242
06243 ClearTemporaryNewGRFData(_cur_grffile);
06244 }
06245 }
06246 }
06247
06248
06249 AfterLoadGRFs();
06250
06251
06252 _cur_year = year;
06253 _date = date;
06254 _date_fract = date_fract;
06255 _tick_counter = tick_counter;
06256 _display_opt = display_opt;
06257 }
06258
06259 bool HasGrfMiscBit(GrfMiscBit bit)
06260 {
06261 return HasBit(_misc_grf_features, bit);
06262 }