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 " PRINTF_SIZE " (expected " PRINTF_SIZE ")!", 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 const 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
01361 byte year = grf_load_byte(&buf);
01362 bridge->avail_year = (year > 0 ? ORIGINAL_BASE_YEAR + year : 0);
01363 break;
01364 }
01365
01366 case 0x09:
01367 bridge->min_length = grf_load_byte(&buf);
01368 break;
01369
01370 case 0x0A:
01371 bridge->max_length = grf_load_byte(&buf);
01372 break;
01373
01374 case 0x0B:
01375 bridge->price = grf_load_byte(&buf);
01376 break;
01377
01378 case 0x0C:
01379 bridge->speed = grf_load_word(&buf);
01380 break;
01381
01382 case 0x0D: {
01383 byte tableid = grf_load_byte(&buf);
01384 byte numtables = grf_load_byte(&buf);
01385
01386 if (bridge->sprite_table == NULL) {
01387
01388 bridge->sprite_table = CallocT<PalSpriteID*>(7);
01389 }
01390
01391 for (; numtables-- != 0; tableid++) {
01392 if (tableid >= 7) {
01393 grfmsg(1, "BridgeChangeInfo: Table %d >= 7, skipping", tableid);
01394 for (byte sprite = 0; sprite < 32; sprite++) grf_load_dword(&buf);
01395 continue;
01396 }
01397
01398 if (bridge->sprite_table[tableid] == NULL) {
01399 bridge->sprite_table[tableid] = MallocT<PalSpriteID>(32);
01400 }
01401
01402 for (byte sprite = 0; sprite < 32; sprite++) {
01403 SpriteID image = grf_load_word(&buf);
01404 SpriteID pal = grf_load_word(&buf);
01405
01406 bridge->sprite_table[tableid][sprite].sprite = image;
01407 bridge->sprite_table[tableid][sprite].pal = pal;
01408
01409 MapSpriteMappingRecolour(&bridge->sprite_table[tableid][sprite]);
01410 }
01411 }
01412 } break;
01413
01414 case 0x0E:
01415 bridge->flags = grf_load_byte(&buf);
01416 break;
01417
01418 case 0x0F:
01419 bridge->avail_year = Clamp(grf_load_dword(&buf), MIN_YEAR, MAX_YEAR);
01420 break;
01421
01422 case 0x10: {
01423 StringID newone = GetGRFStringID(_cur_grffile->grfid, grf_load_word(&buf));
01424 if (newone != STR_UNDEFINED) bridge->material = newone;
01425 } break;
01426
01427 case 0x11:
01428 case 0x12: {
01429 StringID newone = GetGRFStringID(_cur_grffile->grfid, grf_load_word(&buf));
01430 if (newone != STR_UNDEFINED) bridge->transport_name[prop - 0x11] = newone;
01431 } break;
01432
01433 case 0x13:
01434 bridge->price = grf_load_word(&buf);
01435 break;
01436
01437 default:
01438 ret = CIR_UNKNOWN;
01439 break;
01440 }
01441 }
01442
01443 *bufp = buf;
01444 return ret;
01445 }
01446
01447 static ChangeInfoResult TownHouseChangeInfo(uint hid, int numinfo, int prop, byte **bufp, int len)
01448 {
01449 byte *buf = *bufp;
01450 ChangeInfoResult ret = CIR_SUCCESS;
01451
01452 if (hid + numinfo > HOUSE_MAX) {
01453 grfmsg(1, "TownHouseChangeInfo: Too many houses loaded (%u), max (%u). Ignoring.", hid + numinfo, HOUSE_MAX);
01454 return CIR_INVALID_ID;
01455 }
01456
01457
01458 if (_cur_grffile->housespec == NULL) {
01459 _cur_grffile->housespec = CallocT<HouseSpec*>(HOUSE_MAX);
01460 }
01461
01462 for (int i = 0; i < numinfo; i++) {
01463 HouseSpec *housespec = _cur_grffile->housespec[hid + i];
01464
01465 if (prop != 0x08 && housespec == NULL) {
01466 grfmsg(2, "TownHouseChangeInfo: Attempt to modify undefined house %u. Ignoring.", hid + i);
01467 return CIR_INVALID_ID;
01468 }
01469
01470 switch (prop) {
01471 case 0x08: {
01472 HouseSpec **house = &_cur_grffile->housespec[hid + i];
01473 byte subs_id = grf_load_byte(&buf);
01474
01475 if (subs_id == 0xFF) {
01476
01477
01478 _house_specs[hid + i].enabled = false;
01479 continue;
01480 } else if (subs_id >= NEW_HOUSE_OFFSET) {
01481
01482 grfmsg(2, "TownHouseChangeInfo: Attempt to use new house %u as substitute house for %u. Ignoring.", subs_id, hid + i);
01483 continue;
01484 }
01485
01486
01487 if (*house == NULL) *house = CallocT<HouseSpec>(1);
01488
01489 housespec = *house;
01490
01491 memcpy(housespec, &_house_specs[subs_id], sizeof(_house_specs[subs_id]));
01492
01493 housespec->enabled = true;
01494 housespec->local_id = hid + i;
01495 housespec->substitute_id = subs_id;
01496 housespec->grffile = _cur_grffile;
01497 housespec->random_colour[0] = 0x04;
01498 housespec->random_colour[1] = 0x08;
01499 housespec->random_colour[2] = 0x0C;
01500 housespec->random_colour[3] = 0x06;
01501
01502
01503
01504
01505
01506 if (!GetCargo(housespec->accepts_cargo[2])->IsValid()) {
01507 housespec->cargo_acceptance[2] = 0;
01508 }
01509
01515 if (housespec->min_year < 1930) housespec->min_year = 1930;
01516
01517 _loaded_newgrf_features.has_newhouses = true;
01518 } break;
01519
01520 case 0x09:
01521 housespec->building_flags = (BuildingFlags)grf_load_byte(&buf);
01522 break;
01523
01524 case 0x0A: {
01525 uint16 years = grf_load_word(&buf);
01526 housespec->min_year = GB(years, 0, 8) > 150 ? MAX_YEAR : ORIGINAL_BASE_YEAR + GB(years, 0, 8);
01527 housespec->max_year = GB(years, 8, 8) > 150 ? MAX_YEAR : ORIGINAL_BASE_YEAR + GB(years, 8, 8);
01528 } break;
01529
01530 case 0x0B:
01531 housespec->population = grf_load_byte(&buf);
01532 break;
01533
01534 case 0x0C:
01535 housespec->mail_generation = grf_load_byte(&buf);
01536 break;
01537
01538 case 0x0D:
01539 case 0x0E:
01540 housespec->cargo_acceptance[prop - 0x0D] = grf_load_byte(&buf);
01541 break;
01542
01543 case 0x0F: {
01544 int8 goods = grf_load_byte(&buf);
01545
01546
01547
01548 CargoID cid = (goods >= 0) ? ((_settings_game.game_creation.landscape == LT_TOYLAND) ? CT_CANDY : CT_GOODS) :
01549 ((_settings_game.game_creation.landscape == LT_TOYLAND) ? CT_FIZZY_DRINKS : CT_FOOD);
01550
01551
01552 if (!GetCargo(cid)->IsValid()) goods = 0;
01553
01554 housespec->accepts_cargo[2] = cid;
01555 housespec->cargo_acceptance[2] = abs(goods);
01556 } break;
01557
01558 case 0x10:
01559 housespec->remove_rating_decrease = grf_load_word(&buf);
01560 break;
01561
01562 case 0x11:
01563 housespec->removal_cost = grf_load_byte(&buf);
01564 break;
01565
01566 case 0x12:
01567 housespec->building_name = grf_load_word(&buf);
01568 _string_to_grf_mapping[&housespec->building_name] = _cur_grffile->grfid;
01569 break;
01570
01571 case 0x13:
01572 housespec->building_availability = (HouseZones)grf_load_word(&buf);
01573 break;
01574
01575 case 0x14:
01576 housespec->callback_mask = grf_load_byte(&buf);
01577 break;
01578
01579 case 0x15: {
01580 byte override = grf_load_byte(&buf);
01581
01582
01583 if (override >= NEW_HOUSE_OFFSET) {
01584 grfmsg(2, "TownHouseChangeInfo: Attempt to override new house %u with house id %u. Ignoring.", override, hid + i);
01585 continue;
01586 }
01587
01588 _house_mngr.Add(hid + i, _cur_grffile->grfid, override);
01589 } break;
01590
01591 case 0x16:
01592 housespec->processing_time = grf_load_byte(&buf);
01593 break;
01594
01595 case 0x17:
01596 for (uint j = 0; j < 4; j++) housespec->random_colour[j] = grf_load_byte(&buf);
01597 break;
01598
01599 case 0x18:
01600 housespec->probability = grf_load_byte(&buf);
01601 break;
01602
01603 case 0x19:
01604 housespec->extra_flags = (HouseExtraFlags)grf_load_byte(&buf);
01605 break;
01606
01607 case 0x1A:
01608 housespec->animation_frames = grf_load_byte(&buf);
01609 break;
01610
01611 case 0x1B:
01612 housespec->animation_speed = Clamp(grf_load_byte(&buf), 2, 16);
01613 break;
01614
01615 case 0x1C:
01616 housespec->class_id = AllocateHouseClassID(grf_load_byte(&buf), _cur_grffile->grfid);
01617 break;
01618
01619 case 0x1D:
01620 housespec->callback_mask |= (grf_load_byte(&buf) << 8);
01621 break;
01622
01623 case 0x1E: {
01624 uint32 cargotypes = grf_load_dword(&buf);
01625
01626
01627 if (cargotypes == 0xFFFFFFFF) break;
01628
01629 for (uint j = 0; j < 3; j++) {
01630
01631 uint8 cargo_part = GB(cargotypes, 8 * j, 8);
01632 CargoID cargo = GetCargoTranslation(cargo_part, _cur_grffile);
01633
01634 if (cargo == CT_INVALID) {
01635
01636 housespec->cargo_acceptance[j] = 0;
01637 } else {
01638 housespec->accepts_cargo[j] = cargo;
01639 }
01640 }
01641 } break;
01642
01643 case 0x1F:
01644 housespec->minimum_life = grf_load_byte(&buf);
01645 break;
01646
01647 case 0x20: {
01648 byte count = grf_load_byte(&buf);
01649 for (byte j = 0; j < count; j++) grf_load_byte(&buf);
01650 ret = CIR_UNHANDLED;
01651 } break;
01652
01653 case 0x21:
01654 housespec->min_year = grf_load_word(&buf);
01655 break;
01656
01657 case 0x22:
01658 housespec->max_year = grf_load_word(&buf);
01659 break;
01660
01661 default:
01662 ret = CIR_UNKNOWN;
01663 break;
01664 }
01665 }
01666
01667 *bufp = buf;
01668 return ret;
01669 }
01670
01671 static ChangeInfoResult GlobalVarChangeInfo(uint gvid, int numinfo, int prop, byte **bufp, int len)
01672 {
01673 byte *buf = *bufp;
01674 ChangeInfoResult ret = CIR_SUCCESS;
01675
01676 for (int i = 0; i < numinfo; i++) {
01677 switch (prop) {
01678 case 0x08: {
01679 byte factor = grf_load_byte(&buf);
01680 uint price = gvid + i;
01681
01682 if (price < NUM_PRICES) {
01683 SetPriceBaseMultiplier(price, factor);
01684 } else {
01685 grfmsg(1, "GlobalVarChangeInfo: Price %d out of range, ignoring", price);
01686 }
01687 } break;
01688
01689 case 0x09:
01690
01691
01692 buf += 4;
01693 break;
01694
01695 case 0x0A: {
01696 uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
01697 StringID newone = GetGRFStringID(_cur_grffile->grfid, grf_load_word(&buf));
01698
01699 if ((newone != STR_UNDEFINED) && (curidx < NUM_CURRENCY)) {
01700 _currency_specs[curidx].name = newone;
01701 }
01702 } break;
01703
01704 case 0x0B: {
01705 uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
01706 uint32 rate = grf_load_dword(&buf);
01707
01708 if (curidx < NUM_CURRENCY) {
01709
01710
01711
01712 _currency_specs[curidx].rate = rate / 1000;
01713 } else {
01714 grfmsg(1, "GlobalVarChangeInfo: Currency multipliers %d out of range, ignoring", curidx);
01715 }
01716 } break;
01717
01718 case 0x0C: {
01719 uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
01720 uint16 options = grf_load_word(&buf);
01721
01722 if (curidx < NUM_CURRENCY) {
01723 _currency_specs[curidx].separator = GB(options, 0, 8);
01724
01725
01726 _currency_specs[curidx].symbol_pos = GB(options, 8, 1);
01727 } else {
01728 grfmsg(1, "GlobalVarChangeInfo: Currency option %d out of range, ignoring", curidx);
01729 }
01730 } break;
01731
01732 case 0x0D: {
01733 uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
01734 uint32 tempfix = grf_load_dword(&buf);
01735
01736 if (curidx < NUM_CURRENCY) {
01737 memcpy(_currency_specs[curidx].prefix, &tempfix, 4);
01738 _currency_specs[curidx].prefix[4] = 0;
01739 } else {
01740 grfmsg(1, "GlobalVarChangeInfo: Currency symbol %d out of range, ignoring", curidx);
01741 }
01742 } break;
01743
01744 case 0x0E: {
01745 uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
01746 uint32 tempfix = grf_load_dword(&buf);
01747
01748 if (curidx < NUM_CURRENCY) {
01749 memcpy(&_currency_specs[curidx].suffix, &tempfix, 4);
01750 _currency_specs[curidx].suffix[4] = 0;
01751 } else {
01752 grfmsg(1, "GlobalVarChangeInfo: Currency symbol %d out of range, ignoring", curidx);
01753 }
01754 } break;
01755
01756 case 0x0F: {
01757 uint curidx = GetNewgrfCurrencyIdConverted(gvid + i);
01758 Year year_euro = grf_load_word(&buf);
01759
01760 if (curidx < NUM_CURRENCY) {
01761 _currency_specs[curidx].to_euro = year_euro;
01762 } else {
01763 grfmsg(1, "GlobalVarChangeInfo: Euro intro date %d out of range, ignoring", curidx);
01764 }
01765 } break;
01766
01767 case 0x10:
01768 if (numinfo > 1 || IsSnowLineSet()) {
01769 grfmsg(1, "GlobalVarChangeInfo: The snowline can only be set once (%d)", numinfo);
01770 } else if (len < SNOW_LINE_MONTHS * SNOW_LINE_DAYS) {
01771 grfmsg(1, "GlobalVarChangeInfo: Not enough entries set in the snowline table (%d)", len);
01772 } else {
01773 byte table[SNOW_LINE_MONTHS][SNOW_LINE_DAYS];
01774
01775 for (uint i = 0; i < SNOW_LINE_MONTHS; i++) {
01776 for (uint j = 0; j < SNOW_LINE_DAYS; j++) {
01777 table[i][j] = grf_load_byte(&buf);
01778 }
01779 }
01780 SetSnowLine(table);
01781 }
01782 break;
01783
01784 case 0x11:
01785
01786
01787 buf += 8;
01788 break;
01789
01790 case 0x12:
01791
01792
01793 buf += 4;
01794 break;
01795
01796 default:
01797 ret = CIR_UNKNOWN;
01798 break;
01799 }
01800 }
01801
01802 *bufp = buf;
01803 return ret;
01804 }
01805
01806 static ChangeInfoResult GlobalVarReserveInfo(uint gvid, int numinfo, int prop, byte **bufp, int len)
01807 {
01808 byte *buf = *bufp;
01809 ChangeInfoResult ret = CIR_SUCCESS;
01810
01811 for (int i = 0; i < numinfo; i++) {
01812 switch (prop) {
01813 case 0x08:
01814 grf_load_byte(&buf);
01815 break;
01816
01817 case 0x09: {
01818 if (i == 0) {
01819 if (gvid != 0) {
01820 grfmsg(1, "ReserveChangeInfo: Cargo translation table must start at zero");
01821 return CIR_INVALID_ID;
01822 }
01823
01824 free(_cur_grffile->cargo_list);
01825 _cur_grffile->cargo_max = numinfo;
01826 _cur_grffile->cargo_list = MallocT<CargoLabel>(numinfo);
01827 }
01828
01829 CargoLabel cl = grf_load_dword(&buf);
01830 _cur_grffile->cargo_list[i] = BSWAP32(cl);
01831 break;
01832 }
01833
01834 case 0x0A:
01835 case 0x0C:
01836 case 0x0F:
01837 grf_load_word(&buf);
01838 break;
01839
01840 case 0x0B:
01841 case 0x0D:
01842 case 0x0E:
01843 grf_load_dword(&buf);
01844 break;
01845
01846 case 0x10:
01847 buf += SNOW_LINE_MONTHS * SNOW_LINE_DAYS;
01848 break;
01849
01850 case 0x11: {
01851 uint32 s = grf_load_dword(&buf);
01852 uint32 t = grf_load_dword(&buf);
01853 SetNewGRFOverride(s, t);
01854 break;
01855 }
01856
01857 case 0x12: {
01858 if (i == 0) {
01859 if (gvid != 0) {
01860 grfmsg(1, "ReserveChangeInfo: Rail type translation table must start at zero");
01861 return CIR_INVALID_ID;
01862 }
01863
01864 free(_cur_grffile->railtype_list);
01865 _cur_grffile->railtype_max = numinfo;
01866 _cur_grffile->railtype_list = MallocT<RailTypeLabel>(numinfo);
01867 }
01868
01869 RailTypeLabel rtl = grf_load_dword(&buf);
01870 _cur_grffile->railtype_list[i] = BSWAP32(rtl);
01871 break;
01872 }
01873
01874 default:
01875 ret = CIR_UNKNOWN;
01876 break;
01877 }
01878 }
01879
01880 *bufp = buf;
01881 return ret;
01882 }
01883
01884
01885 static ChangeInfoResult CargoChangeInfo(uint cid, int numinfo, int prop, byte **bufp, int len)
01886 {
01887 byte *buf = *bufp;
01888 ChangeInfoResult ret = CIR_SUCCESS;
01889
01890 if (cid + numinfo > NUM_CARGO) {
01891 grfmsg(2, "CargoChangeInfo: Cargo type %d out of range (max %d)", cid + numinfo, NUM_CARGO - 1);
01892 return CIR_INVALID_ID;
01893 }
01894
01895 for (int i = 0; i < numinfo; i++) {
01896 CargoSpec *cs = &_cargo[cid + i];
01897
01898 switch (prop) {
01899 case 0x08:
01900 cs->bitnum = grf_load_byte(&buf);
01901 if (cs->IsValid()) {
01902 cs->grffile = _cur_grffile;
01903 SetBit(_cargo_mask, cid + i);
01904 } else {
01905 ClrBit(_cargo_mask, cid + i);
01906 }
01907 break;
01908
01909 case 0x09:
01910 cs->name = grf_load_word(&buf);
01911 _string_to_grf_mapping[&cs->name] = _cur_grffile->grfid;
01912 break;
01913
01914 case 0x0A:
01915 cs->name_single = grf_load_word(&buf);
01916 _string_to_grf_mapping[&cs->name_single] = _cur_grffile->grfid;
01917 break;
01918
01919 case 0x0B:
01920
01921
01922 cs->units_volume = grf_load_word(&buf);
01923 _string_to_grf_mapping[&cs->units_volume] = _cur_grffile->grfid;
01924 break;
01925
01926 case 0x0C:
01927 cs->quantifier = grf_load_word(&buf);
01928 _string_to_grf_mapping[&cs->quantifier] = _cur_grffile->grfid;
01929 break;
01930
01931 case 0x0D:
01932 cs->abbrev = grf_load_word(&buf);
01933 _string_to_grf_mapping[&cs->abbrev] = _cur_grffile->grfid;
01934 break;
01935
01936 case 0x0E:
01937 cs->sprite = grf_load_word(&buf);
01938 break;
01939
01940 case 0x0F:
01941 cs->weight = grf_load_byte(&buf);
01942 break;
01943
01944 case 0x10:
01945 cs->transit_days[0] = grf_load_byte(&buf);
01946 break;
01947
01948 case 0x11:
01949 cs->transit_days[1] = grf_load_byte(&buf);
01950 break;
01951
01952 case 0x12:
01953 cs->initial_payment = grf_load_dword(&buf);
01954 break;
01955
01956 case 0x13:
01957 cs->rating_colour = MapDOSColour(grf_load_byte(&buf));
01958 break;
01959
01960 case 0x14:
01961 cs->legend_colour = MapDOSColour(grf_load_byte(&buf));
01962 break;
01963
01964 case 0x15:
01965 cs->is_freight = (grf_load_byte(&buf) != 0);
01966 break;
01967
01968 case 0x16:
01969 cs->classes = grf_load_word(&buf);
01970 break;
01971
01972 case 0x17:
01973 cs->label = grf_load_dword(&buf);
01974 cs->label = BSWAP32(cs->label);
01975 break;
01976
01977 case 0x18: {
01978 uint8 substitute_type = grf_load_byte(&buf);
01979
01980 switch (substitute_type) {
01981 case 0x00: cs->town_effect = TE_PASSENGERS; break;
01982 case 0x02: cs->town_effect = TE_MAIL; break;
01983 case 0x05: cs->town_effect = TE_GOODS; break;
01984 case 0x09: cs->town_effect = TE_WATER; break;
01985 case 0x0B: cs->town_effect = TE_FOOD; break;
01986 default:
01987 grfmsg(1, "CargoChangeInfo: Unknown town growth substitute value %d, setting to none.", substitute_type);
01988 case 0xFF: cs->town_effect = TE_NONE; break;
01989 }
01990 } break;
01991
01992 case 0x19:
01993 cs->multipliertowngrowth = grf_load_word(&buf);
01994 break;
01995
01996 case 0x1A:
01997 cs->callback_mask = grf_load_byte(&buf);
01998 break;
01999
02000 default:
02001 ret = CIR_UNKNOWN;
02002 break;
02003 }
02004 }
02005
02006 *bufp = buf;
02007 return ret;
02008 }
02009
02010
02011 static ChangeInfoResult SoundEffectChangeInfo(uint sid, int numinfo, int prop, byte **bufp, int len)
02012 {
02013 byte *buf = *bufp;
02014 ChangeInfoResult ret = CIR_SUCCESS;
02015
02016 if (_cur_grffile->sound_offset == 0) {
02017 grfmsg(1, "SoundEffectChangeInfo: No effects defined, skipping");
02018 return CIR_INVALID_ID;
02019 }
02020
02021 for (int i = 0; i < numinfo; i++) {
02022 uint sound = sid + i + _cur_grffile->sound_offset - GetNumOriginalSounds();
02023
02024 if (sound >= GetNumSounds()) {
02025 grfmsg(1, "SoundEffectChangeInfo: Sound %d not defined (max %d)", sound, GetNumSounds());
02026 return CIR_INVALID_ID;
02027 }
02028
02029 switch (prop) {
02030 case 0x08:
02031 GetSound(sound)->volume = grf_load_byte(&buf);
02032 break;
02033
02034 case 0x09:
02035 GetSound(sound)->priority = grf_load_byte(&buf);
02036 break;
02037
02038 case 0x0A: {
02039 uint orig_sound = grf_load_byte(&buf);
02040
02041 if (orig_sound >= GetNumSounds()) {
02042 grfmsg(1, "SoundEffectChangeInfo: Original sound %d not defined (max %d)", orig_sound, GetNumSounds());
02043 } else {
02044 FileEntry *newfe = GetSound(sound);
02045 FileEntry *oldfe = GetSound(orig_sound);
02046
02047
02048 *oldfe = *newfe;
02049 }
02050 } break;
02051
02052 default:
02053 ret = CIR_UNKNOWN;
02054 break;
02055 }
02056 }
02057
02058 *bufp = buf;
02059 return ret;
02060 }
02061
02062 static ChangeInfoResult IndustrytilesChangeInfo(uint indtid, int numinfo, int prop, byte **bufp, int len)
02063 {
02064 byte *buf = *bufp;
02065 ChangeInfoResult ret = CIR_SUCCESS;
02066
02067 if (indtid + numinfo > NUM_INDUSTRYTILES) {
02068 grfmsg(1, "IndustryTilesChangeInfo: Too many industry tiles loaded (%u), max (%u). Ignoring.", indtid + numinfo, NUM_INDUSTRYTILES);
02069 return CIR_INVALID_ID;
02070 }
02071
02072
02073 if (_cur_grffile->indtspec == NULL) {
02074 _cur_grffile->indtspec = CallocT<IndustryTileSpec*>(NUM_INDUSTRYTILES);
02075 }
02076
02077 for (int i = 0; i < numinfo; i++) {
02078 IndustryTileSpec *tsp = _cur_grffile->indtspec[indtid + i];
02079
02080 if (prop != 0x08 && tsp == NULL) {
02081 grfmsg(2, "IndustryTilesChangeInfo: Attempt to modify undefined industry tile %u. Ignoring.", indtid + i);
02082 return CIR_INVALID_ID;
02083 }
02084
02085 switch (prop) {
02086 case 0x08: {
02087 IndustryTileSpec **tilespec = &_cur_grffile->indtspec[indtid + i];
02088 byte subs_id = grf_load_byte(&buf);
02089
02090 if (subs_id >= NEW_INDUSTRYTILEOFFSET) {
02091
02092 grfmsg(2, "IndustryTilesChangeInfo: Attempt to use new industry tile %u as substitute industry tile for %u. Ignoring.", subs_id, indtid + i);
02093 continue;
02094 }
02095
02096
02097 if (*tilespec == NULL) {
02098 int tempid;
02099 *tilespec = CallocT<IndustryTileSpec>(1);
02100 tsp = *tilespec;
02101
02102 memcpy(tsp, &_industry_tile_specs[subs_id], sizeof(_industry_tile_specs[subs_id]));
02103 tsp->enabled = true;
02104
02105
02106
02107
02108 tsp->anim_production = INDUSTRYTILE_NOANIM;
02109 tsp->anim_next = INDUSTRYTILE_NOANIM;
02110
02111 tsp->grf_prop.local_id = indtid + i;
02112 tsp->grf_prop.subst_id = subs_id;
02113 tsp->grf_prop.grffile = _cur_grffile;
02114 tempid = _industile_mngr.AddEntityID(indtid + i, _cur_grffile->grfid, subs_id);
02115 }
02116 } break;
02117
02118 case 0x09: {
02119 byte ovrid = grf_load_byte(&buf);
02120
02121
02122 if (ovrid >= NEW_INDUSTRYTILEOFFSET) {
02123 grfmsg(2, "IndustryTilesChangeInfo: Attempt to override new industry tile %u with industry tile id %u. Ignoring.", ovrid, indtid + i);
02124 continue;
02125 }
02126
02127 _industile_mngr.Add(indtid + i, _cur_grffile->grfid, ovrid);
02128 } break;
02129
02130 case 0x0A:
02131 case 0x0B:
02132 case 0x0C: {
02133 uint16 acctp = grf_load_word(&buf);
02134 tsp->accepts_cargo[prop - 0x0A] = GetCargoTranslation(GB(acctp, 0, 8), _cur_grffile);
02135 tsp->acceptance[prop - 0x0A] = GB(acctp, 8, 8);
02136 } break;
02137
02138 case 0x0D:
02139 tsp->slopes_refused = (Slope)grf_load_byte(&buf);
02140 break;
02141
02142 case 0x0E:
02143 tsp->callback_flags = grf_load_byte(&buf);
02144 break;
02145
02146 case 0x0F:
02147 tsp->animation_info = grf_load_word(&buf);
02148 break;
02149
02150 case 0x10:
02151 tsp->animation_speed = grf_load_byte(&buf);
02152 break;
02153
02154 case 0x11:
02155 tsp->animation_triggers = grf_load_byte(&buf);
02156 break;
02157
02158 case 0x12:
02159 tsp->animation_special_flags = grf_load_byte(&buf);
02160 break;
02161
02162 default:
02163 ret = CIR_UNKNOWN;
02164 break;
02165 }
02166 }
02167
02168 *bufp = buf;
02169 return ret;
02170 }
02171
02172 static ChangeInfoResult IndustriesChangeInfo(uint indid, int numinfo, int prop, byte **bufp, int len)
02173 {
02174 byte *buf = *bufp;
02175 ChangeInfoResult ret = CIR_SUCCESS;
02176
02177 if (indid + numinfo > NUM_INDUSTRYTYPES) {
02178 grfmsg(1, "IndustriesChangeInfo: Too many industries loaded (%u), max (%u). Ignoring.", indid + numinfo, NUM_INDUSTRYTYPES);
02179 return CIR_INVALID_ID;
02180 }
02181
02182 grfmsg(1, "IndustriesChangeInfo: newid %u", indid);
02183
02184
02185 if (_cur_grffile->industryspec == NULL) {
02186 _cur_grffile->industryspec = CallocT<IndustrySpec*>(NUM_INDUSTRYTYPES);
02187 }
02188
02189 for (int i = 0; i < numinfo; i++) {
02190 IndustrySpec *indsp = _cur_grffile->industryspec[indid + i];
02191
02192 if (prop != 0x08 && indsp == NULL) {
02193 grfmsg(2, "IndustriesChangeInfo: Attempt to modify undefined industry %u. Ignoring.", indid + i);
02194 return CIR_INVALID_ID;
02195 }
02196
02197 switch (prop) {
02198 case 0x08: {
02199 IndustrySpec **indspec = &_cur_grffile->industryspec[indid + i];
02200 byte subs_id = grf_load_byte(&buf);
02201
02202 if (subs_id == 0xFF) {
02203
02204
02205 _industry_specs[indid + i].enabled = false;
02206 continue;
02207 } else if (subs_id >= NEW_INDUSTRYOFFSET) {
02208
02209 grfmsg(2, "_industry_specs: Attempt to use new industry %u as substitute industry for %u. Ignoring.", subs_id, indid + i);
02210 continue;
02211 }
02212
02213
02214
02215
02216 if (*indspec == NULL) {
02217 *indspec = CallocT<IndustrySpec>(1);
02218 indsp = *indspec;
02219
02220 memcpy(indsp, &_origin_industry_specs[subs_id], sizeof(_industry_specs[subs_id]));
02221 indsp->enabled = true;
02222 indsp->grf_prop.local_id = indid + i;
02223 indsp->grf_prop.subst_id = subs_id;
02224 indsp->grf_prop.grffile = _cur_grffile;
02225
02226
02227 indsp->check_proc = CHECK_NOTHING;
02228 }
02229 } break;
02230
02231 case 0x09: {
02232 byte ovrid = grf_load_byte(&buf);
02233
02234
02235 if (ovrid >= NEW_INDUSTRYOFFSET) {
02236 grfmsg(2, "IndustriesChangeInfo: Attempt to override new industry %u with industry id %u. Ignoring.", ovrid, indid + i);
02237 continue;
02238 }
02239 indsp->grf_prop.override = ovrid;
02240 _industry_mngr.Add(indid + i, _cur_grffile->grfid, ovrid);
02241 } break;
02242
02243 case 0x0A: {
02244 indsp->num_table = grf_load_byte(&buf);
02245 uint32 defsize = grf_load_dword(&buf);
02246 IndustryTileTable **tile_table = CallocT<IndustryTileTable*>(indsp->num_table);
02247 IndustryTileTable *itt = CallocT<IndustryTileTable>(defsize);
02248 int size;
02249 IndustryTileTable *copy_from;
02250
02251 for (byte j = 0; j < indsp->num_table; j++) {
02252 for (int k = 0;; k++) {
02253 itt[k].ti.x = grf_load_byte(&buf);
02254
02255 if (itt[k].ti.x == 0xFE && k == 0) {
02256
02257 IndustryType type = grf_load_byte(&buf);
02258 byte laynbr = grf_load_byte(&buf);
02259
02260 copy_from = (IndustryTileTable*)_origin_industry_specs[type].table[laynbr];
02261 for (size = 1;; size++) {
02262 if (copy_from[size - 1].ti.x == -0x80 && copy_from[size - 1].ti.y == 0) break;
02263 }
02264 break;
02265 }
02266
02267 itt[k].ti.y = grf_load_byte(&buf);
02268
02269 if (itt[k].ti.x == 0 && itt[k].ti.y == 0x80) {
02270
02271
02272 itt[k].ti.x = -0x80;
02273 itt[k].ti.y = 0;
02274 itt[k].gfx = 0;
02275
02276 size = k + 1;
02277 copy_from = itt;
02278 break;
02279 }
02280
02281 itt[k].gfx = grf_load_byte(&buf);
02282
02283 if (itt[k].gfx == 0xFE) {
02284
02285 int local_tile_id = grf_load_word(&buf);
02286
02287
02288 int tempid = _industile_mngr.GetID(local_tile_id, _cur_grffile->grfid);
02289
02290 if (tempid == INVALID_INDUSTRYTILE) {
02291 grfmsg(2, "IndustriesChangeInfo: Attempt to use industry tile %u with industry id %u, not yet defined. Ignoring.", local_tile_id, indid);
02292 } else {
02293
02294 itt[k].gfx = tempid;
02295 size = k + 1;
02296 copy_from = itt;
02297 }
02298 } else if (itt[k].gfx == 0xFF) {
02299 itt[k].ti.x = (int8)GB(itt[k].ti.x, 0, 8);
02300 itt[k].ti.y = (int8)GB(itt[k].ti.y, 0, 8);
02301 }
02302 }
02303 tile_table[j] = CallocT<IndustryTileTable>(size);
02304 memcpy(tile_table[j], copy_from, sizeof(*copy_from) * size);
02305 }
02306
02307 indsp->table = tile_table;
02308 SetBit(indsp->cleanup_flag, 1);
02309 free(itt);
02310 } break;
02311
02312 case 0x0B:
02313 indsp->life_type = (IndustryLifeType)grf_load_byte(&buf);
02314 break;
02315
02316 case 0x0C:
02317 indsp->closure_text = grf_load_word(&buf);
02318 _string_to_grf_mapping[&indsp->closure_text] = _cur_grffile->grfid;
02319 break;
02320
02321 case 0x0D:
02322 indsp->production_up_text = grf_load_word(&buf);
02323 _string_to_grf_mapping[&indsp->production_up_text] = _cur_grffile->grfid;
02324 break;
02325
02326 case 0x0E:
02327 indsp->production_down_text = grf_load_word(&buf);
02328 _string_to_grf_mapping[&indsp->production_down_text] = _cur_grffile->grfid;
02329 break;
02330
02331 case 0x0F:
02332 indsp->cost_multiplier = grf_load_byte(&buf);
02333 break;
02334
02335 case 0x10:
02336 for (byte j = 0; j < 2; j++) {
02337 indsp->produced_cargo[j] = GetCargoTranslation(grf_load_byte(&buf), _cur_grffile);
02338 }
02339 break;
02340
02341 case 0x11:
02342 for (byte j = 0; j < 3; j++) {
02343 indsp->accepts_cargo[j] = GetCargoTranslation(grf_load_byte(&buf), _cur_grffile);
02344 }
02345 grf_load_byte(&buf);
02346 break;
02347
02348 case 0x12:
02349 case 0x13:
02350 indsp->production_rate[prop - 0x12] = grf_load_byte(&buf);
02351 break;
02352
02353 case 0x14:
02354 indsp->minimal_cargo = grf_load_byte(&buf);
02355 break;
02356
02357 case 0x15: {
02358 indsp->number_of_sounds = grf_load_byte(&buf);
02359 uint8 *sounds = MallocT<uint8>(indsp->number_of_sounds);
02360
02361 for (uint8 j = 0; j < indsp->number_of_sounds; j++) sounds[j] = grf_load_byte(&buf);
02362 indsp->random_sounds = sounds;
02363 SetBit(indsp->cleanup_flag, 0);
02364 } break;
02365
02366 case 0x16:
02367 for (byte j = 0; j < 3; j++) indsp->conflicting[j] = grf_load_byte(&buf);
02368 break;
02369
02370 case 0x17:
02371 indsp->appear_creation[_settings_game.game_creation.landscape] = grf_load_byte(&buf);
02372 break;
02373
02374 case 0x18:
02375 indsp->appear_ingame[_settings_game.game_creation.landscape] = grf_load_byte(&buf);
02376 break;
02377
02378 case 0x19:
02379 indsp->map_colour = MapDOSColour(grf_load_byte(&buf));
02380 break;
02381
02382 case 0x1A:
02383 indsp->behaviour = (IndustryBehaviour)grf_load_dword(&buf);
02384 break;
02385
02386 case 0x1B:
02387 indsp->new_industry_text = grf_load_word(&buf);
02388 _string_to_grf_mapping[&indsp->new_industry_text] = _cur_grffile->grfid;
02389 break;
02390
02391 case 0x1C:
02392 case 0x1D:
02393 case 0x1E: {
02394 uint32 multiples = grf_load_dword(&buf);
02395 indsp->input_cargo_multiplier[prop - 0x1C][0] = GB(multiples, 0, 16);
02396 indsp->input_cargo_multiplier[prop - 0x1C][1] = GB(multiples, 16, 16);
02397 } break;
02398
02399 case 0x1F:
02400 indsp->name = grf_load_word(&buf);
02401 _string_to_grf_mapping[&indsp->name] = _cur_grffile->grfid;
02402 break;
02403
02404 case 0x20:
02405 indsp->prospecting_chance = grf_load_dword(&buf);
02406 break;
02407
02408 case 0x21:
02409 case 0x22: {
02410 byte aflag = grf_load_byte(&buf);
02411 SB(indsp->callback_flags, (prop - 0x21) * 8, 8, aflag);
02412 } break;
02413
02414 case 0x23:
02415 indsp->removal_cost_multiplier = grf_load_dword(&buf);
02416 break;
02417
02418 case 0x24:
02419 indsp->station_name = grf_load_word(&buf);
02420 _string_to_grf_mapping[&indsp->station_name] = _cur_grffile->grfid;
02421 break;
02422
02423 default:
02424 ret = CIR_UNKNOWN;
02425 break;
02426 }
02427 }
02428
02429 *bufp = buf;
02430 return ret;
02431 }
02432
02433 static bool HandleChangeInfoResult(const char *caller, ChangeInfoResult cir, uint8 feature, uint8 property)
02434 {
02435 switch (cir) {
02436 default: NOT_REACHED();
02437
02438 case CIR_SUCCESS:
02439 return false;
02440
02441 case CIR_UNHANDLED:
02442 grfmsg(1, "%s: Ignoring property 0x%02X of feature 0x%02X (not implemented)", caller, property, feature);
02443 return false;
02444
02445 case CIR_UNKNOWN:
02446 grfmsg(0, "%s: Unknown property 0x%02X of feature 0x%02X, disabling", caller, property, feature);
02447
02448
02449 case CIR_INVALID_ID:
02450
02451 _skip_sprites = -1;
02452 _cur_grfconfig->status = GCS_DISABLED;
02453 _cur_grfconfig->error = CallocT<GRFError>(1);
02454 _cur_grfconfig->error->severity = STR_NEWGRF_ERROR_MSG_FATAL;
02455 _cur_grfconfig->error->message = (cir == CIR_INVALID_ID) ? STR_NEWGRF_ERROR_INVALID_ID : STR_NEWGRF_ERROR_UNKNOWN_PROPERTY;
02456 return true;
02457 }
02458 }
02459
02460
02461 static void FeatureChangeInfo(byte *buf, size_t len)
02462 {
02463 byte *bufend = buf + len;
02464
02465
02466
02467
02468
02469
02470
02471
02472
02473
02474
02475
02476
02477
02478 static const VCI_Handler handler[] = {
02479 RailVehicleChangeInfo,
02480 RoadVehicleChangeInfo,
02481 ShipVehicleChangeInfo,
02482 AircraftVehicleChangeInfo,
02483 StationChangeInfo,
02484 CanalChangeInfo,
02485 BridgeChangeInfo,
02486 TownHouseChangeInfo,
02487 GlobalVarChangeInfo,
02488 IndustrytilesChangeInfo,
02489 IndustriesChangeInfo,
02490 NULL,
02491 SoundEffectChangeInfo,
02492 };
02493
02494 if (!check_length(len, 6, "FeatureChangeInfo")) return;
02495 buf++;
02496 uint8 feature = grf_load_byte(&buf);
02497 uint8 numprops = grf_load_byte(&buf);
02498 uint numinfo = grf_load_byte(&buf);
02499 uint engine = grf_load_extended(&buf);
02500
02501 grfmsg(6, "FeatureChangeInfo: feature %d, %d properties, to apply to %d+%d",
02502 feature, numprops, engine, numinfo);
02503
02504 if (feature >= lengthof(handler) || handler[feature] == NULL) {
02505 grfmsg(1, "FeatureChangeInfo: Unsupported feature %d, skipping", feature);
02506 return;
02507 }
02508
02509 while (numprops-- && buf < bufend) {
02510 uint8 prop = grf_load_byte(&buf);
02511
02512 ChangeInfoResult cir = handler[feature](engine, numinfo, prop, &buf, bufend - buf);
02513 if (HandleChangeInfoResult("FeatureChangeInfo", cir, feature, prop)) return;
02514 }
02515 }
02516
02517
02518 static void SafeChangeInfo(byte *buf, size_t len)
02519 {
02520 if (!check_length(len, 6, "SafeChangeInfo")) return;
02521 buf++;
02522 uint8 feature = grf_load_byte(&buf);
02523 uint8 numprops = grf_load_byte(&buf);
02524 uint numinfo = grf_load_byte(&buf);
02525 grf_load_extended(&buf);
02526
02527 if (feature == GSF_BRIDGE && numprops == 1) {
02528 uint8 prop = grf_load_byte(&buf);
02529
02530
02531 if (prop == 0x0D) return;
02532 } else if (feature == GSF_GLOBALVAR && numprops == 1) {
02533 uint8 prop = grf_load_byte(&buf);
02534
02535 if (prop == 0x11) {
02536 bool is_safe = true;
02537 for (uint i = 0; i < numinfo; i++) {
02538 uint32 s = grf_load_dword(&buf);
02539 grf_load_dword(&buf);
02540 const GRFConfig *grfconfig = GetGRFConfig(s);
02541 if (grfconfig != NULL && !HasBit(grfconfig->flags, GCF_STATIC)) {
02542 is_safe = false;
02543 break;
02544 }
02545 }
02546 if (is_safe) return;
02547 }
02548 }
02549
02550 SetBit(_cur_grfconfig->flags, GCF_UNSAFE);
02551
02552
02553 _skip_sprites = -1;
02554 }
02555
02556
02557 static void ReserveChangeInfo(byte *buf, size_t len)
02558 {
02559 byte *bufend = buf + len;
02560
02561 if (!check_length(len, 6, "ReserveChangeInfo")) return;
02562 buf++;
02563 uint8 feature = grf_load_byte(&buf);
02564
02565 if (feature != GSF_CARGOS && feature != GSF_GLOBALVAR) return;
02566
02567 uint8 numprops = grf_load_byte(&buf);
02568 uint8 numinfo = grf_load_byte(&buf);
02569 uint8 index = grf_load_extended(&buf);
02570
02571 while (numprops-- && buf < bufend) {
02572 uint8 prop = grf_load_byte(&buf);
02573 ChangeInfoResult cir = CIR_SUCCESS;
02574
02575 switch (feature) {
02576 default: NOT_REACHED();
02577 case GSF_CARGOS:
02578 cir = CargoChangeInfo(index, numinfo, prop, &buf, bufend - buf);
02579 break;
02580
02581 case GSF_GLOBALVAR:
02582 cir = GlobalVarReserveInfo(index, numinfo, prop, &buf, bufend - buf);
02583 break;
02584 }
02585
02586 if (HandleChangeInfoResult("ReserveChangeInfo", cir, feature, prop)) return;
02587 }
02588 }
02589
02595 static const SpriteGroup *NewCallBackResultSpriteGroup(uint16 value)
02596 {
02597 SpriteGroup *group = AllocateSpriteGroup();
02598
02599 group->type = SGT_CALLBACK;
02600
02601
02602
02603 if ((value >> 8) == 0xFF) {
02604 value &= ~0xFF00;
02605 } else {
02606 value &= ~0x8000;
02607 }
02608
02609 group->g.callback.result = value;
02610
02611 return group;
02612 }
02613
02620 static const SpriteGroup *NewResultSpriteGroup(SpriteID sprite, byte num_sprites)
02621 {
02622 SpriteGroup *group = AllocateSpriteGroup();
02623 group->type = SGT_RESULT;
02624 group->g.result.sprite = sprite;
02625 group->g.result.num_sprites = num_sprites;
02626 return group;
02627 }
02628
02629
02630 static void NewSpriteSet(byte *buf, size_t len)
02631 {
02632
02633
02634
02635
02636
02637
02638
02639
02640
02641
02642
02643
02644 if (!check_length(len, 4, "NewSpriteSet")) return;
02645 buf++;
02646 uint8 feature = grf_load_byte(&buf);
02647 uint8 num_sets = grf_load_byte(&buf);
02648 uint16 num_ents = grf_load_extended(&buf);
02649
02650 _cur_grffile->spriteset_start = _cur_spriteid;
02651 _cur_grffile->spriteset_feature = feature;
02652 _cur_grffile->spriteset_numsets = num_sets;
02653 _cur_grffile->spriteset_numents = num_ents;
02654
02655 grfmsg(7, "New sprite set at %d of type %d, consisting of %d sets with %d views each (total %d)",
02656 _cur_spriteid, feature, num_sets, num_ents, num_sets * num_ents
02657 );
02658
02659 for (int i = 0; i < num_sets * num_ents; i++) {
02660 _nfo_line++;
02661 LoadNextSprite(_cur_spriteid++, _file_index, _nfo_line);
02662 }
02663 }
02664
02665
02666 static void SkipAct1(byte *buf, size_t len)
02667 {
02668 if (!check_length(len, 4, "SkipAct1")) return;
02669 buf++;
02670 grf_load_byte(&buf);
02671 uint8 num_sets = grf_load_byte(&buf);
02672 uint16 num_ents = grf_load_extended(&buf);
02673
02674 _skip_sprites = num_sets * num_ents;
02675
02676 grfmsg(3, "SkipAct1: Skipping %d sprites", _skip_sprites);
02677 }
02678
02679
02680
02681 static const SpriteGroup *GetGroupFromGroupID(byte setid, byte type, uint16 groupid)
02682 {
02683 if (HasBit(groupid, 15)) return NewCallBackResultSpriteGroup(groupid);
02684
02685 if (groupid >= _cur_grffile->spritegroups_count || _cur_grffile->spritegroups[groupid] == NULL) {
02686 grfmsg(1, "GetGroupFromGroupID(0x%02X:0x%02X): Groupid 0x%04X does not exist, leaving empty", setid, type, groupid);
02687 return NULL;
02688 }
02689
02690 return _cur_grffile->spritegroups[groupid];
02691 }
02692
02693
02694 static const SpriteGroup *CreateGroupFromGroupID(byte feature, byte setid, byte type, uint16 spriteid, uint16 num_sprites)
02695 {
02696 if (HasBit(spriteid, 15)) return NewCallBackResultSpriteGroup(spriteid);
02697
02698 if (spriteid >= _cur_grffile->spriteset_numsets) {
02699 grfmsg(1, "CreateGroupFromGroupID(0x%02X:0x%02X): Sprite set %u invalid, max %u", setid, type, spriteid, _cur_grffile->spriteset_numsets);
02700 return NULL;
02701 }
02702
02703
02704
02705
02706 if (_cur_grffile->spriteset_start + spriteid * num_sprites + num_sprites > _cur_spriteid) {
02707 grfmsg(1, "CreateGroupFromGroupID(0x%02X:0x%02X): Real Sprite IDs 0x%04X - 0x%04X do not (all) exist (max 0x%04X), leaving empty",
02708 setid, type,
02709 _cur_grffile->spriteset_start + spriteid * num_sprites,
02710 _cur_grffile->spriteset_start + spriteid * num_sprites + num_sprites - 1, _cur_spriteid - 1);
02711 return NULL;
02712 }
02713
02714 if (feature != _cur_grffile->spriteset_feature) {
02715 grfmsg(1, "CreateGroupFromGroupID(0x%02X:0x%02X): Sprite set feature 0x%02X does not match action feature 0x%02X, skipping",
02716 setid, type,
02717 _cur_grffile->spriteset_feature, feature);
02718 return NULL;
02719 }
02720
02721 return NewResultSpriteGroup(_cur_grffile->spriteset_start + spriteid * num_sprites, num_sprites);
02722 }
02723
02724
02725 static void NewSpriteGroup(byte *buf, size_t len)
02726 {
02727
02728
02729
02730
02731
02732
02733
02734
02735
02736
02737 SpriteGroup *group = NULL;
02738 byte *bufend = buf + len;
02739
02740 if (!check_length(len, 5, "NewSpriteGroup")) return;
02741 buf++;
02742
02743 uint8 feature = grf_load_byte(&buf);
02744 uint8 setid = grf_load_byte(&buf);
02745 uint8 type = grf_load_byte(&buf);
02746
02747 if (setid >= _cur_grffile->spritegroups_count) {
02748
02749 _cur_grffile->spritegroups = ReallocT(_cur_grffile->spritegroups, setid + 1);
02750
02751 for (; _cur_grffile->spritegroups_count < (setid + 1); _cur_grffile->spritegroups_count++)
02752 _cur_grffile->spritegroups[_cur_grffile->spritegroups_count] = NULL;
02753 }
02754
02755 switch (type) {
02756
02757 case 0x81:
02758 case 0x82:
02759 case 0x85:
02760 case 0x86:
02761 case 0x89:
02762 case 0x8A:
02763 {
02764 byte varadjust;
02765 byte varsize;
02766
02767
02768 if (!check_length(bufend - buf, 1, "NewSpriteGroup (Deterministic) (1)")) return;
02769
02770 group = AllocateSpriteGroup();
02771 group->type = SGT_DETERMINISTIC;
02772 group->g.determ.var_scope = HasBit(type, 1) ? VSG_SCOPE_PARENT : VSG_SCOPE_SELF;
02773
02774 switch (GB(type, 2, 2)) {
02775 default: NOT_REACHED();
02776 case 0: group->g.determ.size = DSG_SIZE_BYTE; varsize = 1; break;
02777 case 1: group->g.determ.size = DSG_SIZE_WORD; varsize = 2; break;
02778 case 2: group->g.determ.size = DSG_SIZE_DWORD; varsize = 4; break;
02779 }
02780
02781 if (!check_length(bufend - buf, 5 + varsize, "NewSpriteGroup (Deterministic) (2)")) return;
02782
02783
02784
02785 do {
02786 DeterministicSpriteGroupAdjust *adjust;
02787
02788 if (group->g.determ.num_adjusts > 0) {
02789 if (!check_length(bufend - buf, 2 + varsize + 3, "NewSpriteGroup (Deterministic) (3)")) return;
02790 }
02791
02792 group->g.determ.num_adjusts++;
02793 group->g.determ.adjusts = ReallocT(group->g.determ.adjusts, group->g.determ.num_adjusts);
02794
02795 adjust = &group->g.determ.adjusts[group->g.determ.num_adjusts - 1];
02796
02797
02798 adjust->operation = group->g.determ.num_adjusts == 1 ? DSGA_OP_ADD : (DeterministicSpriteGroupAdjustOperation)grf_load_byte(&buf);
02799 adjust->variable = grf_load_byte(&buf);
02800 if (adjust->variable == 0x7E) {
02801
02802 adjust->subroutine = GetGroupFromGroupID(setid, type, grf_load_byte(&buf));
02803 } else {
02804 adjust->parameter = IsInsideMM(adjust->variable, 0x60, 0x80) ? grf_load_byte(&buf) : 0;
02805 }
02806
02807 varadjust = grf_load_byte(&buf);
02808 adjust->shift_num = GB(varadjust, 0, 5);
02809 adjust->type = (DeterministicSpriteGroupAdjustType)GB(varadjust, 6, 2);
02810 adjust->and_mask = grf_load_var(varsize, &buf);
02811
02812 if (adjust->type != DSGA_TYPE_NONE) {
02813 adjust->add_val = grf_load_var(varsize, &buf);
02814 adjust->divmod_val = grf_load_var(varsize, &buf);
02815 } else {
02816 adjust->add_val = 0;
02817 adjust->divmod_val = 0;
02818 }
02819
02820
02821 } while (HasBit(varadjust, 5));
02822
02823 group->g.determ.num_ranges = grf_load_byte(&buf);
02824 if (group->g.determ.num_ranges > 0) group->g.determ.ranges = CallocT<DeterministicSpriteGroupRange>(group->g.determ.num_ranges);
02825
02826 if (!check_length(bufend - buf, 2 + (2 + 2 * varsize) * group->g.determ.num_ranges, "NewSpriteGroup (Deterministic)")) return;
02827
02828 for (uint i = 0; i < group->g.determ.num_ranges; i++) {
02829 group->g.determ.ranges[i].group = GetGroupFromGroupID(setid, type, grf_load_word(&buf));
02830 group->g.determ.ranges[i].low = grf_load_var(varsize, &buf);
02831 group->g.determ.ranges[i].high = grf_load_var(varsize, &buf);
02832 }
02833
02834 group->g.determ.default_group = GetGroupFromGroupID(setid, type, grf_load_word(&buf));
02835 break;
02836 }
02837
02838
02839 case 0x80:
02840 case 0x83:
02841 case 0x84:
02842 {
02843 if (!check_length(bufend - buf, HasBit(type, 2) ? 8 : 7, "NewSpriteGroup (Randomized) (1)")) return;
02844
02845 group = AllocateSpriteGroup();
02846 group->type = SGT_RANDOMIZED;
02847 group->g.random.var_scope = HasBit(type, 1) ? VSG_SCOPE_PARENT : VSG_SCOPE_SELF;
02848
02849 if (HasBit(type, 2)) {
02850 if (feature <= GSF_AIRCRAFT) group->g.random.var_scope = VSG_SCOPE_RELATIVE;
02851 group->g.random.count = grf_load_byte(&buf);
02852 }
02853
02854 uint8 triggers = grf_load_byte(&buf);
02855 group->g.random.triggers = GB(triggers, 0, 7);
02856 group->g.random.cmp_mode = HasBit(triggers, 7) ? RSG_CMP_ALL : RSG_CMP_ANY;
02857 group->g.random.lowest_randbit = grf_load_byte(&buf);
02858 group->g.random.num_groups = grf_load_byte(&buf);
02859 group->g.random.groups = CallocT<const SpriteGroup*>(group->g.random.num_groups);
02860
02861 if (!check_length(bufend - buf, 2 * group->g.random.num_groups, "NewSpriteGroup (Randomized) (2)")) return;
02862
02863 for (uint i = 0; i < group->g.random.num_groups; i++) {
02864 group->g.random.groups[i] = GetGroupFromGroupID(setid, type, grf_load_word(&buf));
02865 }
02866
02867 break;
02868 }
02869
02870
02871 default:
02872 {
02873
02874
02875 switch (feature) {
02876 case GSF_TRAIN:
02877 case GSF_ROAD:
02878 case GSF_SHIP:
02879 case GSF_AIRCRAFT:
02880 case GSF_STATION:
02881 case GSF_CANAL:
02882 case GSF_CARGOS:
02883 {
02884 byte sprites = _cur_grffile->spriteset_numents;
02885 byte num_loaded = type;
02886 byte num_loading = grf_load_byte(&buf);
02887
02888 if (_cur_grffile->spriteset_start == 0) {
02889 grfmsg(0, "NewSpriteGroup: No sprite set to work on! Skipping");
02890 return;
02891 }
02892
02893 if (!check_length(bufend - buf, 2 * num_loaded + 2 * num_loading, "NewSpriteGroup (Real) (1)")) return;
02894
02895 group = AllocateSpriteGroup();
02896 group->type = SGT_REAL;
02897
02898 group->g.real.num_loaded = num_loaded;
02899 group->g.real.num_loading = num_loading;
02900 if (num_loaded > 0) group->g.real.loaded = CallocT<const SpriteGroup*>(num_loaded);
02901 if (num_loading > 0) group->g.real.loading = CallocT<const SpriteGroup*>(num_loading);
02902
02903 grfmsg(6, "NewSpriteGroup: New SpriteGroup 0x%02X, %u views, %u loaded, %u loading",
02904 setid, sprites, num_loaded, num_loading);
02905
02906 for (uint i = 0; i < num_loaded; i++) {
02907 uint16 spriteid = grf_load_word(&buf);
02908 group->g.real.loaded[i] = CreateGroupFromGroupID(feature, setid, type, spriteid, sprites);
02909 grfmsg(8, "NewSpriteGroup: + rg->loaded[%i] = subset %u", i, spriteid);
02910 }
02911
02912 for (uint i = 0; i < num_loading; i++) {
02913 uint16 spriteid = grf_load_word(&buf);
02914 group->g.real.loading[i] = CreateGroupFromGroupID(feature, setid, type, spriteid, sprites);
02915 grfmsg(8, "NewSpriteGroup: + rg->loading[%i] = subset %u", i, spriteid);
02916 }
02917
02918 break;
02919 }
02920
02921 case GSF_TOWNHOUSE:
02922 case GSF_INDUSTRYTILES: {
02923 byte sprites = _cur_grffile->spriteset_numents;
02924 byte num_sprites = max((uint8)1, type);
02925 uint i;
02926
02927 group = AllocateSpriteGroup();
02928 group->type = SGT_TILELAYOUT;
02929 group->g.layout.num_sprites = sprites;
02930 group->g.layout.dts = CallocT<DrawTileSprites>(1);
02931
02932
02933 group->g.layout.dts->ground.sprite = grf_load_word(&buf);
02934 group->g.layout.dts->ground.pal = grf_load_word(&buf);
02935
02936
02937 MapSpriteMappingRecolour(&group->g.layout.dts->ground);
02938
02939 if (HasBit(group->g.layout.dts->ground.pal, 15)) {
02940
02941
02942 SpriteID sprite = _cur_grffile->spriteset_start + GB(group->g.layout.dts->ground.sprite, 0, 14) * sprites;
02943 SB(group->g.layout.dts->ground.sprite, 0, SPRITE_WIDTH, sprite);
02944 ClrBit(group->g.layout.dts->ground.pal, 15);
02945 }
02946
02947 group->g.layout.dts->seq = CallocT<DrawTileSeqStruct>(num_sprites + 1);
02948
02949 for (i = 0; i < num_sprites; i++) {
02950 DrawTileSeqStruct *seq = (DrawTileSeqStruct*)&group->g.layout.dts->seq[i];
02951
02952 seq->image.sprite = grf_load_word(&buf);
02953 seq->image.pal = grf_load_word(&buf);
02954 seq->delta_x = grf_load_byte(&buf);
02955 seq->delta_y = grf_load_byte(&buf);
02956
02957 MapSpriteMappingRecolour(&seq->image);
02958
02959 if (HasBit(seq->image.pal, 15)) {
02960
02961
02962 SpriteID sprite = _cur_grffile->spriteset_start + GB(seq->image.sprite, 0, 14) * sprites;
02963 SB(seq->image.sprite, 0, SPRITE_WIDTH, sprite);
02964 ClrBit(seq->image.pal, 15);
02965 }
02966
02967 if (type > 0) {
02968 seq->delta_z = grf_load_byte(&buf);
02969 if ((byte)seq->delta_z == 0x80) continue;
02970 }
02971
02972 seq->size_x = grf_load_byte(&buf);
02973 seq->size_y = grf_load_byte(&buf);
02974 seq->size_z = grf_load_byte(&buf);
02975 }
02976
02977
02978 ((DrawTileSeqStruct*)group->g.layout.dts->seq)[i].delta_x = (byte)0x80;
02979
02980 break;
02981 }
02982
02983 case GSF_INDUSTRIES: {
02984 if (type > 1) {
02985 grfmsg(1, "NewSpriteGroup: Unsupported industry production version %d, skipping", type);
02986 break;
02987 }
02988
02989 group = AllocateSpriteGroup();
02990 group->type = SGT_INDUSTRY_PRODUCTION;
02991 group->g.indprod.version = type;
02992 if (type == 0) {
02993 for (uint i = 0; i < 3; i++) {
02994 group->g.indprod.substract_input[i] = grf_load_word(&buf);
02995 }
02996 for (uint i = 0; i < 2; i++) {
02997 group->g.indprod.add_output[i] = grf_load_word(&buf);
02998 }
02999 group->g.indprod.again = grf_load_byte(&buf);
03000 } else {
03001 for (uint i = 0; i < 3; i++) {
03002 group->g.indprod.substract_input[i] = grf_load_byte(&buf);
03003 }
03004 for (uint i = 0; i < 2; i++) {
03005 group->g.indprod.add_output[i] = grf_load_byte(&buf);
03006 }
03007 group->g.indprod.again = grf_load_byte(&buf);
03008 }
03009 break;
03010 }
03011
03012
03013 default: grfmsg(1, "NewSpriteGroup: Unsupported feature %d, skipping", feature);
03014 }
03015 }
03016 }
03017
03018 _cur_grffile->spritegroups[setid] = group;
03019 }
03020
03021 static CargoID TranslateCargo(uint8 feature, uint8 ctype)
03022 {
03023
03024 if (feature == GSF_STATION && ctype == 0xFE) return CT_DEFAULT_NA;
03025 if (ctype == 0xFF) return CT_PURCHASE;
03026
03027 if (_cur_grffile->cargo_max == 0) {
03028
03029 if (ctype >= 32) {
03030 grfmsg(1, "TranslateCargo: Cargo bitnum %d out of range (max 31), skipping.", ctype);
03031 return CT_INVALID;
03032 }
03033
03034 for (CargoID c = 0; c < NUM_CARGO; c++) {
03035 const CargoSpec *cs = GetCargo(c);
03036 if (!cs->IsValid()) continue;
03037
03038 if (cs->bitnum == ctype) {
03039 grfmsg(6, "TranslateCargo: Cargo bitnum %d mapped to cargo type %d.", ctype, c);
03040 return c;
03041 }
03042 }
03043
03044 grfmsg(5, "TranslateCargo: Cargo bitnum %d not available in this climate, skipping.", ctype);
03045 return CT_INVALID;
03046 }
03047
03048
03049 if (ctype >= _cur_grffile->cargo_max) {
03050 grfmsg(1, "TranslateCargo: Cargo type %d out of range (max %d), skipping.", ctype, _cur_grffile->cargo_max - 1);
03051 return CT_INVALID;
03052 }
03053
03054
03055 CargoLabel cl = _cur_grffile->cargo_list[ctype];
03056 if (cl == 0) {
03057 grfmsg(5, "TranslateCargo: Cargo type %d not available in this climate, skipping.", ctype);
03058 return CT_INVALID;
03059 }
03060
03061 ctype = GetCargoIDByLabel(cl);
03062 if (ctype == CT_INVALID) {
03063 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));
03064 return CT_INVALID;
03065 }
03066
03067 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);
03068 return ctype;
03069 }
03070
03071
03072 static bool IsValidGroupID(uint16 groupid, const char *function)
03073 {
03074 if (groupid >= _cur_grffile->spritegroups_count || _cur_grffile->spritegroups[groupid] == NULL) {
03075 grfmsg(1, "%s: Spriteset 0x%04X out of range (maximum 0x%02X) or empty, skipping.", function, groupid, _cur_grffile->spritegroups_count - 1);
03076 return false;
03077 }
03078
03079 return true;
03080 }
03081
03082 static void VehicleMapSpriteGroup(byte *buf, byte feature, uint8 idcount)
03083 {
03084 static EngineID *last_engines;
03085 static uint last_engines_count;
03086 bool wagover = false;
03087
03088
03089 if (HasBit(idcount, 7)) {
03090 wagover = true;
03091
03092 idcount = GB(idcount, 0, 7);
03093
03094 if (last_engines_count == 0) {
03095 grfmsg(0, "VehicleMapSpriteGroup: WagonOverride: No engine to do override with");
03096 return;
03097 }
03098
03099 grfmsg(6, "VehicleMapSpriteGroup: WagonOverride: %u engines, %u wagons",
03100 last_engines_count, idcount);
03101 } else {
03102 if (last_engines_count != idcount) {
03103 last_engines = ReallocT(last_engines, idcount);
03104 last_engines_count = idcount;
03105 }
03106 }
03107
03108 EngineID *engines = AllocaM(EngineID, idcount);
03109 for (uint i = 0; i < idcount; i++) {
03110 engines[i] = GetNewEngine(_cur_grffile, (VehicleType)feature, grf_load_extended(&buf))->index;
03111 if (!wagover) last_engines[i] = engines[i];
03112 }
03113
03114 uint8 cidcount = grf_load_byte(&buf);
03115 for (uint c = 0; c < cidcount; c++) {
03116 uint8 ctype = grf_load_byte(&buf);
03117 uint16 groupid = grf_load_word(&buf);
03118 if (!IsValidGroupID(groupid, "VehicleMapSpriteGroup")) continue;
03119
03120 grfmsg(8, "VehicleMapSpriteGroup: * [%d] Cargo type 0x%X, group id 0x%02X", c, ctype, groupid);
03121
03122 ctype = TranslateCargo(feature, ctype);
03123 if (ctype == CT_INVALID) continue;
03124
03125 for (uint i = 0; i < idcount; i++) {
03126 EngineID engine = engines[i];
03127
03128 grfmsg(7, "VehicleMapSpriteGroup: [%d] Engine %d...", i, engine);
03129
03130 if (wagover) {
03131 SetWagonOverrideSprites(engine, ctype, _cur_grffile->spritegroups[groupid], last_engines, last_engines_count);
03132 } else {
03133 SetCustomEngineSprites(engine, ctype, _cur_grffile->spritegroups[groupid]);
03134 }
03135 }
03136 }
03137
03138 uint16 groupid = grf_load_word(&buf);
03139 if (!IsValidGroupID(groupid, "VehicleMapSpriteGroup")) return;
03140
03141 grfmsg(8, "-- Default group id 0x%04X", groupid);
03142
03143 for (uint i = 0; i < idcount; i++) {
03144 EngineID engine = engines[i];
03145
03146 if (wagover) {
03147 SetWagonOverrideSprites(engine, CT_DEFAULT, _cur_grffile->spritegroups[groupid], last_engines, last_engines_count);
03148 } else {
03149 SetCustomEngineSprites(engine, CT_DEFAULT, _cur_grffile->spritegroups[groupid]);
03150 SetEngineGRF(engine, _cur_grffile);
03151 }
03152 }
03153 }
03154
03155
03156 static void CanalMapSpriteGroup(byte *buf, uint8 idcount)
03157 {
03158 CanalFeature *cfs = AllocaM(CanalFeature, idcount);
03159 for (uint i = 0; i < idcount; i++) {
03160 cfs[i] = (CanalFeature)grf_load_byte(&buf);
03161 }
03162
03163 uint8 cidcount = grf_load_byte(&buf);
03164 buf += cidcount * 3;
03165
03166 uint16 groupid = grf_load_word(&buf);
03167 if (!IsValidGroupID(groupid, "CanalMapSpriteGroup")) return;
03168
03169 for (uint i = 0; i < idcount; i++) {
03170 CanalFeature cf = cfs[i];
03171
03172 if (cf >= CF_END) {
03173 grfmsg(1, "CanalMapSpriteGroup: Canal subset %d out of range, skipping", cf);
03174 continue;
03175 }
03176
03177 _water_feature[cf].grffile = _cur_grffile;
03178 _water_feature[cf].group = _cur_grffile->spritegroups[groupid];
03179 }
03180 }
03181
03182
03183 static void StationMapSpriteGroup(byte *buf, uint8 idcount)
03184 {
03185 uint8 *stations = AllocaM(uint8, idcount);
03186 for (uint i = 0; i < idcount; i++) {
03187 stations[i] = grf_load_byte(&buf);
03188 }
03189
03190 uint8 cidcount = grf_load_byte(&buf);
03191 for (uint c = 0; c < cidcount; c++) {
03192 uint8 ctype = grf_load_byte(&buf);
03193 uint16 groupid = grf_load_word(&buf);
03194 if (!IsValidGroupID(groupid, "StationMapSpriteGroup")) continue;
03195
03196 ctype = TranslateCargo(GSF_STATION, ctype);
03197 if (ctype == CT_INVALID) continue;
03198
03199 for (uint i = 0; i < idcount; i++) {
03200 StationSpec *statspec = _cur_grffile->stations[stations[i]];
03201
03202 if (statspec == NULL) {
03203 grfmsg(1, "StationMapSpriteGroup: Station with ID 0x%02X does not exist, skipping", stations[i]);
03204 continue;
03205 }
03206
03207 statspec->spritegroup[ctype] = _cur_grffile->spritegroups[groupid];
03208 }
03209 }
03210
03211 uint16 groupid = grf_load_word(&buf);
03212 if (!IsValidGroupID(groupid, "StationMapSpriteGroup")) return;
03213
03214 for (uint i = 0; i < idcount; i++) {
03215 StationSpec *statspec = _cur_grffile->stations[stations[i]];
03216
03217 if (statspec == NULL) {
03218 grfmsg(1, "StationMapSpriteGroup: Station with ID 0x%02X does not exist, skipping", stations[i]);
03219 continue;
03220 }
03221
03222 statspec->spritegroup[CT_DEFAULT] = _cur_grffile->spritegroups[groupid];
03223 statspec->grffile = _cur_grffile;
03224 statspec->localidx = stations[i];
03225 SetCustomStationSpec(statspec);
03226 }
03227 }
03228
03229
03230 static void TownHouseMapSpriteGroup(byte *buf, uint8 idcount)
03231 {
03232 uint8 *houses = AllocaM(uint8, idcount);
03233 for (uint i = 0; i < idcount; i++) {
03234 houses[i] = grf_load_byte(&buf);
03235 }
03236
03237
03238 uint8 cidcount = grf_load_byte(&buf);
03239 buf += cidcount * 3;
03240
03241 uint16 groupid = grf_load_word(&buf);
03242 if (!IsValidGroupID(groupid, "TownHouseMapSpriteGroup")) return;
03243
03244 for (uint i = 0; i < idcount; i++) {
03245 HouseSpec *hs = _cur_grffile->housespec[houses[i]];
03246
03247 if (hs == NULL) {
03248 grfmsg(1, "TownHouseMapSpriteGroup: House %d undefined, skipping.", houses[i]);
03249 continue;
03250 }
03251
03252 hs->spritegroup = _cur_grffile->spritegroups[groupid];
03253 }
03254 }
03255
03256 static void IndustryMapSpriteGroup(byte *buf, uint8 idcount)
03257 {
03258 uint8 *industries = AllocaM(uint8, idcount);
03259 for (uint i = 0; i < idcount; i++) {
03260 industries[i] = grf_load_byte(&buf);
03261 }
03262
03263
03264 uint8 cidcount = grf_load_byte(&buf);
03265 buf += cidcount * 3;
03266
03267 uint16 groupid = grf_load_word(&buf);
03268 if (!IsValidGroupID(groupid, "IndustryMapSpriteGroup")) return;
03269
03270 for (uint i = 0; i < idcount; i++) {
03271 IndustrySpec *indsp = _cur_grffile->industryspec[industries[i]];
03272
03273 if (indsp == NULL) {
03274 grfmsg(1, "IndustryMapSpriteGroup: Industry %d undefined, skipping", industries[i]);
03275 continue;
03276 }
03277
03278 indsp->grf_prop.spritegroup = _cur_grffile->spritegroups[groupid];
03279 }
03280 }
03281
03282 static void IndustrytileMapSpriteGroup(byte *buf, uint8 idcount)
03283 {
03284 uint8 *indtiles = AllocaM(uint8, idcount);
03285 for (uint i = 0; i < idcount; i++) {
03286 indtiles[i] = grf_load_byte(&buf);
03287 }
03288
03289
03290 uint8 cidcount = grf_load_byte(&buf);
03291 buf += cidcount * 3;
03292
03293 uint16 groupid = grf_load_word(&buf);
03294 if (!IsValidGroupID(groupid, "IndustrytileMapSpriteGroup")) return;
03295
03296 for (uint i = 0; i < idcount; i++) {
03297 IndustryTileSpec *indtsp = _cur_grffile->indtspec[indtiles[i]];
03298
03299 if (indtsp == NULL) {
03300 grfmsg(1, "IndustrytileMapSpriteGroup: Industry tile %d undefined, skipping", indtiles[i]);
03301 continue;
03302 }
03303
03304 indtsp->grf_prop.spritegroup = _cur_grffile->spritegroups[groupid];
03305 }
03306 }
03307
03308 static void CargoMapSpriteGroup(byte *buf, uint8 idcount)
03309 {
03310 CargoID *cargos = AllocaM(CargoID, idcount);
03311 for (uint i = 0; i < idcount; i++) {
03312 cargos[i] = grf_load_byte(&buf);
03313 }
03314
03315
03316 uint8 cidcount = grf_load_byte(&buf);
03317 buf += cidcount * 3;
03318
03319 uint16 groupid = grf_load_word(&buf);
03320 if (!IsValidGroupID(groupid, "CargoMapSpriteGroup")) return;
03321
03322 for (uint i = 0; i < idcount; i++) {
03323 CargoID cid = cargos[i];
03324
03325 if (cid >= NUM_CARGO) {
03326 grfmsg(1, "CargoMapSpriteGroup: Cargo ID %d out of range, skipping", cid);
03327 continue;
03328 }
03329
03330 CargoSpec *cs = &_cargo[cid];
03331 cs->grffile = _cur_grffile;
03332 cs->group = _cur_grffile->spritegroups[groupid];
03333 }
03334 }
03335
03336
03337
03338 static void FeatureMapSpriteGroup(byte *buf, size_t len)
03339 {
03340
03341
03342
03343
03344
03345
03346
03347
03348
03349
03350
03351
03352
03353
03354 if (_cur_grffile->spritegroups == 0) {
03355 grfmsg(1, "FeatureMapSpriteGroup: No sprite groups to work on! Skipping");
03356 return;
03357 }
03358
03359 if (!check_length(len, 6, "FeatureMapSpriteGroup")) return;
03360
03361 buf++;
03362 uint8 feature = grf_load_byte(&buf);
03363 uint8 idcount = grf_load_byte(&buf);
03364
03365
03366 if (idcount == 0) {
03367
03368 grf_load_byte(&buf);
03369 uint16 groupid = grf_load_word(&buf);
03370
03371 grfmsg(6, "FeatureMapSpriteGroup: Adding generic feature callback for feature %d", feature);
03372
03373 AddGenericCallback(feature, _cur_grffile, _cur_grffile->spritegroups[groupid]);
03374 return;
03375 }
03376
03377 grfmsg(6, "FeatureMapSpriteGroup: Feature %d, %d ids", feature, idcount);
03378
03379 switch (feature) {
03380 case GSF_TRAIN:
03381 case GSF_ROAD:
03382 case GSF_SHIP:
03383 case GSF_AIRCRAFT:
03384 VehicleMapSpriteGroup(buf, feature, idcount);
03385 return;
03386
03387 case GSF_CANAL:
03388 CanalMapSpriteGroup(buf, idcount);
03389 return;
03390
03391 case GSF_STATION:
03392 StationMapSpriteGroup(buf, idcount);
03393 return;
03394
03395 case GSF_TOWNHOUSE:
03396 TownHouseMapSpriteGroup(buf, idcount);
03397 return;
03398
03399 case GSF_INDUSTRIES:
03400 IndustryMapSpriteGroup(buf, idcount);
03401 return;
03402
03403 case GSF_INDUSTRYTILES:
03404 IndustrytileMapSpriteGroup(buf, idcount);
03405 return;
03406
03407 case GSF_CARGOS:
03408 CargoMapSpriteGroup(buf, idcount);
03409 return;
03410
03411 default:
03412 grfmsg(1, "FeatureMapSpriteGroup: Unsupported feature %d, skipping", feature);
03413 return;
03414 }
03415 }
03416
03417
03418 static void FeatureNewName(byte *buf, size_t len)
03419 {
03420
03421
03422
03423
03424
03425
03426
03427
03428
03429
03430
03431
03432
03433
03434
03435
03436 bool new_scheme = _cur_grffile->grf_version >= 7;
03437
03438 if (!check_length(len, 6, "FeatureNewName")) return;
03439 buf++;
03440 uint8 feature = grf_load_byte(&buf);
03441 uint8 lang = grf_load_byte(&buf);
03442 uint8 num = grf_load_byte(&buf);
03443 bool generic = HasBit(lang, 7);
03444 uint16 id;
03445 if (generic) {
03446 id = grf_load_word(&buf);
03447 } else if (feature <= GSF_AIRCRAFT) {
03448 id = grf_load_extended(&buf);
03449 } else {
03450 id = grf_load_byte(&buf);
03451 }
03452
03453 ClrBit(lang, 7);
03454
03455 uint16 endid = id + num;
03456
03457 grfmsg(6, "FeatureNewName: About to rename engines %d..%d (feature %d) in language 0x%02X",
03458 id, endid, feature, lang);
03459
03460 len -= generic ? 6 : 5;
03461
03462 for (; id < endid && len > 0; id++) {
03463 const char *name = grf_load_string(&buf, len);
03464 size_t name_length = strlen(name) + 1;
03465
03466 len -= (int)name_length;
03467
03468 grfmsg(8, "FeatureNewName: 0x%04X <- %s", id, name);
03469
03470 switch (feature) {
03471 case GSF_TRAIN:
03472 case GSF_ROAD:
03473 case GSF_SHIP:
03474 case GSF_AIRCRAFT:
03475 if (!generic) {
03476 Engine *e = GetNewEngine(_cur_grffile, (VehicleType)feature, id, HasBit(_cur_grfconfig->flags, GCF_STATIC));
03477 if (e == NULL) break;
03478 StringID string = AddGRFString(_cur_grffile->grfid, e->index, lang, new_scheme, name, e->info.string_id);
03479 e->info.string_id = string;
03480 } else {
03481 AddGRFString(_cur_grffile->grfid, id, lang, new_scheme, name, id);
03482 }
03483 break;
03484
03485 case GSF_INDUSTRIES: {
03486 AddGRFString(_cur_grffile->grfid, id, lang, new_scheme, name, STR_UNDEFINED);
03487 break;
03488 }
03489
03490 case GSF_TOWNHOUSE:
03491 default:
03492 switch (GB(id, 8, 8)) {
03493 case 0xC4:
03494 if (_cur_grffile->stations == NULL || _cur_grffile->stations[GB(id, 0, 8)] == NULL) {
03495 grfmsg(1, "FeatureNewName: Attempt to name undefined station 0x%X, ignoring", GB(id, 0, 8));
03496 } else {
03497 StationClassID sclass = _cur_grffile->stations[GB(id, 0, 8)]->sclass;
03498 SetStationClassName(sclass, AddGRFString(_cur_grffile->grfid, id, lang, new_scheme, name, STR_UNDEFINED));
03499 }
03500 break;
03501
03502 case 0xC5:
03503 if (_cur_grffile->stations == NULL || _cur_grffile->stations[GB(id, 0, 8)] == NULL) {
03504 grfmsg(1, "FeatureNewName: Attempt to name undefined station 0x%X, ignoring", GB(id, 0, 8));
03505 } else {
03506 _cur_grffile->stations[GB(id, 0, 8)]->name = AddGRFString(_cur_grffile->grfid, id, lang, new_scheme, name, STR_UNDEFINED);
03507 }
03508 break;
03509
03510 case 0xC9:
03511 if (_cur_grffile->housespec == NULL || _cur_grffile->housespec[GB(id, 0, 8)] == NULL) {
03512 grfmsg(1, "FeatureNewName: Attempt to name undefined house 0x%X, ignoring.", GB(id, 0, 8));
03513 } else {
03514 _cur_grffile->housespec[GB(id, 0, 8)]->building_name = AddGRFString(_cur_grffile->grfid, id, lang, new_scheme, name, STR_UNDEFINED);
03515 }
03516 break;
03517
03518 case 0xD0:
03519 case 0xD1:
03520 case 0xD2:
03521 case 0xD3:
03522 case 0xDC:
03523 AddGRFString(_cur_grffile->grfid, id, lang, new_scheme, name, STR_UNDEFINED);
03524 break;
03525
03526 default:
03527 grfmsg(7, "FeatureNewName: Unsupported ID (0x%04X)", id);
03528 break;
03529 }
03530 break;
03531
03532 #if 0
03533 case GSF_CANAL :
03534 case GSF_BRIDGE :
03535 AddGRFString(_cur_spriteid, id, lang, name);
03536 switch (GB(id, 8, 8)) {
03537 case 0xC9:
03538 default:
03539 grfmsg(7, "FeatureNewName: Unsupported ID (0x%04X)", id);
03540 }
03541 break;
03542
03543 default :
03544 grfmsg(7, "FeatureNewName: Unsupported feature (0x%02X)", feature);
03545 break;
03546 #endif
03547 }
03548 }
03549 }
03550
03559 static uint16 SanitizeSpriteOffset(uint16& num, uint16 offset, int max_sprites, const char *name)
03560 {
03561
03562 if (offset >= max_sprites) {
03563 grfmsg(1, "GraphicsNew: %s sprite offset must be less than %i, skipping", name, max_sprites);
03564 uint orig_num = num;
03565 num = 0;
03566 return orig_num;
03567 }
03568
03569 if (offset + num > max_sprites) {
03570 grfmsg(4, "GraphicsNew: %s sprite overflow, truncating...", name);
03571 uint orig_num = num;
03572 num = max(max_sprites - offset, 0);
03573 return orig_num - num;
03574 }
03575
03576 return 0;
03577 }
03578
03579
03580 static void GraphicsNew(byte *buf, size_t len)
03581 {
03582
03583
03584
03585
03586
03587
03588
03589 enum Action5BlockType {
03590 A5BLOCK_FIXED,
03591 A5BLOCK_ALLOW_OFFSET,
03592 A5BLOCK_INVALID,
03593 };
03594 struct Action5Type {
03595 Action5BlockType block_type;
03596 SpriteID sprite_base;
03597 uint16 min_sprites;
03598 uint16 max_sprites;
03599 const char *name;
03600 };
03601
03602 static const Action5Type action5_types[] = {
03603
03604 { A5BLOCK_INVALID, 0, 0, 0, "Type 0x00" },
03605 { A5BLOCK_INVALID, 0, 0, 0, "Type 0x01" },
03606 { A5BLOCK_INVALID, 0, 0, 0, "Type 0x02" },
03607 { A5BLOCK_INVALID, 0, 0, 0, "Type 0x03" },
03608 { A5BLOCK_FIXED, SPR_SIGNALS_BASE, 48, PRESIGNAL_SEMAPHORE_AND_PBS_SPRITE_COUNT, "Signal graphics" },
03609 { A5BLOCK_FIXED, SPR_ELRAIL_BASE, 48, ELRAIL_SPRITE_COUNT, "Catenary graphics" },
03610 { A5BLOCK_FIXED, SPR_SLOPES_BASE, 74, NORMAL_AND_HALFTILE_FOUNDATION_SPRITE_COUNT, "Foundation graphics" },
03611 { A5BLOCK_INVALID, 0, 75, 0, "TTDP GUI graphics" },
03612 { A5BLOCK_FIXED, SPR_CANALS_BASE, 65, CANALS_SPRITE_COUNT, "Canal graphics" },
03613 { A5BLOCK_FIXED, SPR_ONEWAY_BASE, 6, ONEWAY_SPRITE_COUNT, "One way road graphics" },
03614 { A5BLOCK_FIXED, SPR_2CCMAP_BASE, 256, TWOCCMAP_SPRITE_COUNT, "2CC colour maps" },
03615 { A5BLOCK_FIXED, SPR_TRAMWAY_BASE, 113, TRAMWAY_SPRITE_COUNT, "Tramway graphics" },
03616 { A5BLOCK_INVALID, 0, 133, 0, "Snowy temperate tree" },
03617 { A5BLOCK_FIXED, SPR_SHORE_BASE, 16, SPR_SHORE_SPRITE_COUNT, "Shore graphics" },
03618 { A5BLOCK_INVALID, 0, 0, 0, "New Signals graphics" },
03619 { A5BLOCK_FIXED, SPR_TRACKS_FOR_SLOPES_BASE, 12, TRACKS_FOR_SLOPES_SPRITE_COUNT, "Sloped rail track" },
03620 { A5BLOCK_FIXED, SPR_AIRPORTX_BASE, 15, AIRPORTX_SPRITE_COUNT, "Airport graphics" },
03621 { A5BLOCK_FIXED, SPR_ROADSTOP_BASE, 8, ROADSTOP_SPRITE_COUNT, "Road stop graphics" },
03622 { A5BLOCK_FIXED, SPR_AQUEDUCT_BASE, 8, AQUEDUCT_SPRITE_COUNT, "Aqueduct graphics" },
03623 { A5BLOCK_FIXED, SPR_AUTORAIL_BASE, 55, AUTORAIL_SPRITE_COUNT, "Autorail graphics" },
03624 { A5BLOCK_ALLOW_OFFSET, SPR_FLAGS_BASE, 1, FLAGS_SPRITE_COUNT, "Flag graphics" },
03625 { A5BLOCK_ALLOW_OFFSET, SPR_OPENTTD_BASE, 1, OPENTTD_SPRITE_COUNT, "OpenTTD GUI graphics" },
03626 };
03627
03628 if (!check_length(len, 2, "GraphicsNew")) return;
03629 buf++;
03630 uint8 type = grf_load_byte(&buf);
03631 uint16 num = grf_load_extended(&buf);
03632 uint16 offset = HasBit(type, 7) ? grf_load_extended(&buf) : 0;
03633 ClrBit(type, 7);
03634
03635 if ((type == 0x0D) && (num == 10) && _cur_grffile->is_ottdfile) {
03636
03637
03638 grfmsg(2, "GraphicsNew: Loading 10 missing shore sprites from openttd(d/w).grf.");
03639 LoadNextSprite(SPR_SHORE_BASE + 0, _file_index, _nfo_line++);
03640 LoadNextSprite(SPR_SHORE_BASE + 5, _file_index, _nfo_line++);
03641 LoadNextSprite(SPR_SHORE_BASE + 7, _file_index, _nfo_line++);
03642 LoadNextSprite(SPR_SHORE_BASE + 10, _file_index, _nfo_line++);
03643 LoadNextSprite(SPR_SHORE_BASE + 11, _file_index, _nfo_line++);
03644 LoadNextSprite(SPR_SHORE_BASE + 13, _file_index, _nfo_line++);
03645 LoadNextSprite(SPR_SHORE_BASE + 14, _file_index, _nfo_line++);
03646 LoadNextSprite(SPR_SHORE_BASE + 15, _file_index, _nfo_line++);
03647 LoadNextSprite(SPR_SHORE_BASE + 16, _file_index, _nfo_line++);
03648 LoadNextSprite(SPR_SHORE_BASE + 17, _file_index, _nfo_line++);
03649 if (_loaded_newgrf_features.shore == SHORE_REPLACE_NONE) _loaded_newgrf_features.shore = SHORE_REPLACE_ONLY_NEW;
03650 return;
03651 }
03652
03653
03654 if ((type >= lengthof(action5_types)) || (action5_types[type].block_type == A5BLOCK_INVALID)) {
03655 grfmsg(2, "GraphicsNew: Custom graphics (type 0x%02X) sprite block of length %u (unimplemented, ignoring)", type, num);
03656 _skip_sprites = num;
03657 return;
03658 }
03659
03660 const Action5Type *action5_type = &action5_types[type];
03661
03662
03663 if ((action5_type->block_type != A5BLOCK_ALLOW_OFFSET) && (offset != 0)) {
03664 grfmsg(1, "GraphicsNew: %s (type 0x%02X) do not allow an <offset> field. Ignoring offset.", action5_type->name, type);
03665 offset = 0;
03666 }
03667
03668
03669
03670 if ((action5_type->block_type == A5BLOCK_FIXED) && (num < action5_type->min_sprites)) {
03671 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);
03672 _skip_sprites = num;
03673 return;
03674 }
03675
03676
03677 uint16 skip_num = SanitizeSpriteOffset(num, offset, action5_type->max_sprites, action5_type->name);
03678 SpriteID replace = action5_type->sprite_base + offset;
03679
03680
03681 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);
03682
03683 for (; num > 0; num--) {
03684 _nfo_line++;
03685 LoadNextSprite(replace == 0 ? _cur_spriteid++ : replace++, _file_index, _nfo_line);
03686 }
03687
03688 if (type == 0x0D) _loaded_newgrf_features.shore = SHORE_REPLACE_ACTION_5;
03689
03690 _skip_sprites = skip_num;
03691 }
03692
03693
03694 static void SkipAct5(byte *buf, size_t len)
03695 {
03696 if (!check_length(len, 2, "SkipAct5")) return;
03697 buf++;
03698
03699
03700 grf_load_byte(&buf);
03701
03702
03703 _skip_sprites = grf_load_extended(&buf);
03704
03705 grfmsg(3, "SkipAct5: Skipping %d sprites", _skip_sprites);
03706 }
03707
03718 bool GetGlobalVariable(byte param, uint32 *value)
03719 {
03720 switch (param) {
03721 case 0x00:
03722 *value = max(_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0);
03723 return true;
03724
03725 case 0x01:
03726 *value = Clamp(_cur_year, ORIGINAL_BASE_YEAR, ORIGINAL_MAX_YEAR) - ORIGINAL_BASE_YEAR;
03727 return true;
03728
03729 case 0x02: {
03730 YearMonthDay ymd;
03731 ConvertDateToYMD(_date, &ymd);
03732 Date start_of_year = ConvertYMDToDate(ymd.year, 0, 1);
03733 *value = ymd.month | (ymd.day - 1) << 8 | (IsLeapYear(ymd.year) ? 1 << 15 : 0) | (_date - start_of_year) << 16;
03734 return true;
03735 }
03736
03737 case 0x03:
03738 *value = _settings_game.game_creation.landscape;
03739 return true;
03740
03741 case 0x06:
03742 *value = _settings_game.vehicle.road_side << 4;
03743 return true;
03744
03745 case 0x09:
03746 *value = _date_fract;
03747 return true;
03748
03749 case 0x0A:
03750 *value = _tick_counter;
03751 return true;
03752
03753 case 0x0B: {
03754 uint major = 2;
03755 uint minor = 6;
03756 uint revision = 1;
03757 uint build = 1382;
03758 *value = (major << 24) | (minor << 20) | (revision << 16) | build;
03759 return true;
03760 }
03761
03762 case 0x0D:
03763 *value = _cur_grfconfig->windows_paletted;
03764 return true;
03765
03766 case 0x0E:
03767 *value = _traininfo_vehicle_pitch;
03768 return true;
03769
03770 case 0x0F:
03771 *value = 0;
03772 SB(*value, 0, 8, GetRailTypeInfo(RAILTYPE_RAIL)->cost_multiplier);
03773 if (_settings_game.vehicle.disable_elrails) {
03774
03775 SB(*value, 8, 8, GetRailTypeInfo(RAILTYPE_MONO)->cost_multiplier);
03776 } else {
03777 SB(*value, 8, 8, GetRailTypeInfo(RAILTYPE_ELECTRIC)->cost_multiplier);
03778
03779 }
03780 SB(*value, 16, 8, GetRailTypeInfo(RAILTYPE_MAGLEV)->cost_multiplier);
03781 return true;
03782
03783 case 0x11:
03784 *value = 0;
03785 return true;
03786
03787 case 0x12:
03788 *value = _game_mode;
03789 return true;
03790
03791
03792
03793
03794
03795
03796
03797 case 0x1A:
03798 *value = UINT_MAX;
03799 return true;
03800
03801 case 0x1B:
03802 *value = GB(_display_opt, 0, 6);
03803 return true;
03804
03805 case 0x1D:
03806 *value = 1;
03807 return true;
03808
03809 case 0x1E:
03810 *value = _misc_grf_features;
03811 return true;
03812
03813
03814
03815 case 0x20:
03816 *value = _settings_game.game_creation.landscape == LT_ARCTIC ? GetSnowLine() : 0xFF;
03817 return true;
03818
03819 case 0x21:
03820 *value = _openttd_newgrf_version;
03821 return true;
03822
03823 case 0x22:
03824 *value = _settings_game.difficulty.diff_level;
03825 return true;
03826
03827 case 0x23:
03828 *value = _date;
03829 return true;
03830
03831 case 0x24:
03832 *value = _cur_year;
03833 return true;
03834
03835 default: return false;
03836 }
03837 }
03838
03839 static uint32 GetParamVal(byte param, uint32 *cond_val)
03840 {
03841
03842 uint32 value;
03843 if (GetGlobalVariable(param - 0x80, &value)) return value;
03844
03845
03846 switch (param) {
03847 case 0x84: {
03848 uint32 res = 0;
03849
03850 if (_cur_stage > GLS_INIT) SetBit(res, 0);
03851 if (_cur_stage == GLS_RESERVE) SetBit(res, 8);
03852 if (_cur_stage == GLS_ACTIVATION) SetBit(res, 9);
03853 return res;
03854 }
03855
03856 case 0x85:
03857 if (cond_val == NULL) {
03858
03859 return 0;
03860 } else {
03861 uint32 param_val = _ttdpatch_flags[*cond_val / 0x20];
03862 *cond_val %= 0x20;
03863 return param_val;
03864 }
03865
03866 case 0x88:
03867 return 0;
03868
03869
03870
03871 default:
03872
03873 if (param < 0x80) return _cur_grffile->param[param];
03874
03875
03876 grfmsg(1, "Unsupported in-game variable 0x%02X", param);
03877 return UINT_MAX;
03878 }
03879 }
03880
03881
03882 static void CfgApply(byte *buf, size_t len)
03883 {
03884
03885
03886
03887
03888
03889
03890
03891
03892
03893
03894
03895
03896 size_t pos = FioGetPos();
03897 uint16 num = FioReadWord();
03898 uint8 type = FioReadByte();
03899 byte *preload_sprite = NULL;
03900
03901
03902 if (type == 0xFF) {
03903 preload_sprite = MallocT<byte>(num);
03904 FioReadBlock(preload_sprite, num);
03905 }
03906
03907
03908 FioSeekTo(pos, SEEK_SET);
03909
03910 if (type != 0xFF) {
03911 grfmsg(2, "CfgApply: Ignoring (next sprite is real, unsupported)");
03912 free(preload_sprite);
03913 return;
03914 }
03915
03916 GRFLocation location(_cur_grfconfig->grfid, _nfo_line + 1);
03917 GRFLineToSpriteOverride::iterator it = _grf_line_to_action6_sprite_override.find(location);
03918 if (it != _grf_line_to_action6_sprite_override.end()) {
03919 free(preload_sprite);
03920 preload_sprite = _grf_line_to_action6_sprite_override[location];
03921 } else {
03922 _grf_line_to_action6_sprite_override[location] = preload_sprite;
03923 }
03924
03925
03926 buf++;
03927
03928 for (;;) {
03929 uint i;
03930 uint param_num;
03931 uint param_size;
03932 uint offset;
03933 bool add_value;
03934
03935
03936 param_num = grf_load_byte(&buf);
03937 if (param_num == 0xFF) break;
03938
03939
03940
03941 param_size = grf_load_byte(&buf);
03942
03943
03944
03945 add_value = HasBit(param_size, 7);
03946 param_size = GB(param_size, 0, 7);
03947
03948
03949 offset = grf_load_extended(&buf);
03950
03951
03952
03953 if (param_num < 0x80 && (param_num + (param_size - 1) / 4) >= _cur_grffile->param_end) {
03954 grfmsg(2, "CfgApply: Ignoring (param %d not set)", (param_num + (param_size - 1) / 4));
03955 break;
03956 }
03957
03958 grfmsg(8, "CfgApply: Applying %u bytes from parameter 0x%02X at offset 0x%04X", param_size, param_num, offset);
03959
03960 bool carry = false;
03961 for (i = 0; i < param_size && offset + i < num; i++) {
03962 uint32 value = GetParamVal(param_num + i / 4, NULL);
03963
03964
03965 if (i == 0) carry = false;
03966
03967 if (add_value) {
03968 uint new_value = preload_sprite[offset + i] + GB(value, (i % 4) * 8, 8) + (carry ? 1 : 0);
03969 preload_sprite[offset + i] = GB(new_value, 0, 8);
03970
03971 carry = new_value >= 256;
03972 } else {
03973 preload_sprite[offset + i] = GB(value, (i % 4) * 8, 8);
03974 }
03975 }
03976 }
03977 }
03978
03988 static void DisableStaticNewGRFInfluencingNonStaticNewGRFs(GRFConfig *c)
03989 {
03990 if (c->error != NULL) {
03991 free(c->error->custom_message);
03992 free(c->error->data);
03993 free(c->error);
03994 }
03995 c->status = GCS_DISABLED;
03996 c->error = CallocT<GRFError>(1);
03997 c->error->data = strdup(_cur_grfconfig->name);
03998 c->error->severity = STR_NEWGRF_ERROR_MSG_FATAL;
03999 c->error->message = STR_NEWGRF_ERROR_STATIC_GRF_CAUSES_DESYNC;
04000
04001 ClearTemporaryNewGRFData(GetFileByGRFID(c->grfid));
04002 }
04003
04004
04005
04006 static void SkipIf(byte *buf, size_t len)
04007 {
04008
04009
04010
04011
04012
04013
04014
04015
04016 uint32 cond_val = 0;
04017 uint32 mask = 0;
04018 bool result;
04019
04020 if (!check_length(len, 6, "SkipIf")) return;
04021 buf++;
04022 uint8 param = grf_load_byte(&buf);
04023 uint8 paramsize = grf_load_byte(&buf);
04024 uint8 condtype = grf_load_byte(&buf);
04025
04026 if (condtype < 2) {
04027
04028 paramsize = 1;
04029 }
04030
04031 switch (paramsize) {
04032 case 8: cond_val = grf_load_dword(&buf); mask = grf_load_dword(&buf); break;
04033 case 4: cond_val = grf_load_dword(&buf); mask = 0xFFFFFFFF; break;
04034 case 2: cond_val = grf_load_word(&buf); mask = 0x0000FFFF; break;
04035 case 1: cond_val = grf_load_byte(&buf); mask = 0x000000FF; break;
04036 default: break;
04037 }
04038
04039 if (param < 0x80 && _cur_grffile->param_end <= param) {
04040 grfmsg(7, "SkipIf: Param %d undefined, skipping test", param);
04041 return;
04042 }
04043
04044 uint32 param_val = GetParamVal(param, &cond_val);
04045
04046 grfmsg(7, "SkipIf: Test condtype %d, param 0x%08X, condval 0x%08X", condtype, param_val, cond_val);
04047
04048
04049
04050
04051
04052
04053
04054
04055 if (param == 0x88 && condtype != 0x0B && condtype != 0x0C) {
04056
04057
04058 GRFConfig *c = GetGRFConfig(cond_val, mask);
04059
04060 if (c != NULL && HasBit(c->flags, GCF_STATIC) && !HasBit(_cur_grfconfig->flags, GCF_STATIC) && c->status != GCS_DISABLED && _networking) {
04061 DisableStaticNewGRFInfluencingNonStaticNewGRFs(c);
04062 c = NULL;
04063 }
04064
04065 if (condtype != 10 && c == NULL) {
04066 grfmsg(7, "SkipIf: GRFID 0x%08X unknown, skipping test", BSWAP32(cond_val));
04067 return;
04068 }
04069
04070 switch (condtype) {
04071
04072 case 0x06:
04073 result = c->status == GCS_ACTIVATED;
04074 break;
04075
04076 case 0x07:
04077 result = c->status != GCS_ACTIVATED;
04078 break;
04079
04080 case 0x08:
04081 result = c->status == GCS_INITIALISED;
04082 break;
04083
04084 case 0x09:
04085 result = c->status == GCS_ACTIVATED || c->status == GCS_INITIALISED;
04086 break;
04087
04088 case 0x0A:
04089
04090 result = c == NULL || c->flags == GCS_DISABLED || c->status == GCS_NOT_FOUND;
04091 break;
04092
04093 default: grfmsg(1, "SkipIf: Unsupported GRF condition type %02X. Ignoring", condtype); return;
04094 }
04095 } else {
04096
04097 switch (condtype) {
04098 case 0x00: result = !!(param_val & (1 << cond_val));
04099 break;
04100 case 0x01: result = !(param_val & (1 << cond_val));
04101 break;
04102 case 0x02: result = (param_val & mask) == cond_val;
04103 break;
04104 case 0x03: result = (param_val & mask) != cond_val;
04105 break;
04106 case 0x04: result = (param_val & mask) < cond_val;
04107 break;
04108 case 0x05: result = (param_val & mask) > cond_val;
04109 break;
04110 case 0x0B: result = GetCargoIDByLabel(BSWAP32(cond_val)) == CT_INVALID;
04111 break;
04112 case 0x0C: result = GetCargoIDByLabel(BSWAP32(cond_val)) != CT_INVALID;
04113 break;
04114 case 0x0D: result = GetRailTypeByLabel(BSWAP32(cond_val)) == INVALID_RAILTYPE;
04115 break;
04116 case 0x0E: result = GetRailTypeByLabel(BSWAP32(cond_val)) != INVALID_RAILTYPE;
04117 break;
04118
04119 default: grfmsg(1, "SkipIf: Unsupported condition type %02X. Ignoring", condtype); return;
04120 }
04121 }
04122
04123 if (!result) {
04124 grfmsg(2, "SkipIf: Not skipping sprites, test was false");
04125 return;
04126 }
04127
04128 uint8 numsprites = grf_load_byte(&buf);
04129
04130
04131
04132
04133
04134 GRFLabel *choice = NULL;
04135 for (GRFLabel *label = _cur_grffile->label; label != NULL; label = label->next) {
04136 if (label->label != numsprites) continue;
04137
04138
04139 if (choice == NULL) choice = label;
04140
04141 if (label->nfo_line > _nfo_line) {
04142 choice = label;
04143 break;
04144 }
04145 }
04146
04147 if (choice != NULL) {
04148 grfmsg(2, "SkipIf: Jumping to label 0x%0X at line %d, test was true", choice->label, choice->nfo_line);
04149 FioSeekTo(choice->pos, SEEK_SET);
04150 _nfo_line = choice->nfo_line;
04151 return;
04152 }
04153
04154 grfmsg(2, "SkipIf: Skipping %d sprites, test was true", numsprites);
04155 _skip_sprites = numsprites;
04156 if (_skip_sprites == 0) {
04157
04158
04159
04160 _skip_sprites = -1;
04161
04162
04163 if (_cur_grfconfig->status != GCS_ACTIVATED) {
04164 _cur_grfconfig->status = GCS_DISABLED;
04165 ClearTemporaryNewGRFData(_cur_grffile);
04166 }
04167 }
04168 }
04169
04170
04171
04172 static void ScanInfo(byte *buf, size_t len)
04173 {
04174 if (!check_length(len, 8, "Info")) return;
04175 buf++;
04176 grf_load_byte(&buf);
04177 uint32 grfid = grf_load_dword(&buf);
04178
04179 _cur_grfconfig->grfid = grfid;
04180
04181
04182 if (GB(grfid, 24, 8) == 0xFF) SetBit(_cur_grfconfig->flags, GCF_SYSTEM);
04183
04184 len -= 6;
04185 const char *name = grf_load_string(&buf, len);
04186 _cur_grfconfig->name = TranslateTTDPatchCodes(grfid, name);
04187
04188 len -= strlen(name) + 1;
04189 if (len > 0) {
04190 const char *info = grf_load_string(&buf, len);
04191 _cur_grfconfig->info = TranslateTTDPatchCodes(grfid, info);
04192 }
04193
04194
04195 _skip_sprites = -1;
04196 }
04197
04198
04199 static void GRFInfo(byte *buf, size_t len)
04200 {
04201
04202
04203
04204
04205
04206
04207
04208 if (!check_length(len, 8, "GRFInfo")) return;
04209 buf++;
04210 uint8 version = grf_load_byte(&buf);
04211 uint32 grfid = grf_load_dword(&buf);
04212 const char *name = grf_load_string(&buf, len - 6);
04213
04214 if (_cur_stage < GLS_RESERVE && _cur_grfconfig->status != GCS_UNKNOWN) {
04215 _cur_grfconfig->status = GCS_DISABLED;
04216 _cur_grfconfig->error = CallocT<GRFError>(1);
04217 _cur_grfconfig->error->severity = STR_NEWGRF_ERROR_MSG_FATAL;
04218 _cur_grfconfig->error->message = STR_NEWGRF_ERROR_MULTIPLE_ACTION_8;
04219
04220 _skip_sprites = -1;
04221 return;
04222 }
04223
04224 _cur_grffile->grfid = grfid;
04225 _cur_grffile->grf_version = version;
04226 _cur_grfconfig->status = _cur_stage < GLS_RESERVE ? GCS_INITIALISED : GCS_ACTIVATED;
04227
04228
04229 DEBUG(grf, 1, "GRFInfo: Loaded GRFv%d set %08X - %s (palette: %s)", version, BSWAP32(grfid), name, _cur_grfconfig->windows_paletted ? "Windows" : "DOS");
04230 }
04231
04232
04233 static void SpriteReplace(byte *buf, size_t len)
04234 {
04235
04236
04237
04238
04239
04240
04241
04242
04243 buf++;
04244 uint8 num_sets = grf_load_byte(&buf);
04245
04246 for (uint i = 0; i < num_sets; i++) {
04247 uint8 num_sprites = grf_load_byte(&buf);
04248 uint16 first_sprite = grf_load_word(&buf);
04249
04250 grfmsg(2, "SpriteReplace: [Set %d] Changing %d sprites, beginning with %d",
04251 i, num_sprites, first_sprite
04252 );
04253
04254 for (uint j = 0; j < num_sprites; j++) {
04255 int load_index = first_sprite + j;
04256 _nfo_line++;
04257 LoadNextSprite(load_index, _file_index, _nfo_line);
04258
04259
04260
04261 if (IsInsideMM(load_index, SPR_ORIGINALSHORE_START, SPR_ORIGINALSHORE_END + 1)) {
04262 if (_loaded_newgrf_features.shore != SHORE_REPLACE_ACTION_5) _loaded_newgrf_features.shore = SHORE_REPLACE_ACTION_A;
04263 }
04264 }
04265 }
04266 }
04267
04268
04269 static void SkipActA(byte *buf, size_t len)
04270 {
04271 buf++;
04272 uint8 num_sets = grf_load_byte(&buf);
04273
04274 for (uint i = 0; i < num_sets; i++) {
04275
04276 _skip_sprites += grf_load_byte(&buf);
04277
04278 grf_load_word(&buf);
04279 }
04280
04281 grfmsg(3, "SkipActA: Skipping %d sprites", _skip_sprites);
04282 }
04283
04284
04285 static void GRFLoadError(byte *buf, size_t len)
04286 {
04287
04288
04289
04290
04291
04292
04293
04294
04295
04296
04297
04298
04299
04300
04301
04302 static const StringID msgstr[] = {
04303 STR_NEWGRF_ERROR_VERSION_NUMBER,
04304 STR_NEWGRF_ERROR_DOS_OR_WINDOWS,
04305 STR_NEWGRF_ERROR_UNSET_SWITCH,
04306 STR_NEWGRF_ERROR_INVALID_PARAMETER,
04307 STR_NEWGRF_ERROR_LOAD_BEFORE,
04308 STR_NEWGRF_ERROR_LOAD_AFTER,
04309 STR_NEWGRF_ERROR_OTTD_VERSION_NUMBER,
04310 };
04311
04312 static const StringID sevstr[] = {
04313 STR_NEWGRF_ERROR_MSG_INFO,
04314 STR_NEWGRF_ERROR_MSG_WARNING,
04315 STR_NEWGRF_ERROR_MSG_ERROR,
04316 STR_NEWGRF_ERROR_MSG_FATAL
04317 };
04318
04319 if (!check_length(len, 6, "GRFLoadError")) return;
04320
04321
04322 if (_cur_grfconfig->error != NULL) return;
04323
04324 buf++;
04325 byte severity = grf_load_byte(&buf);
04326 byte lang = grf_load_byte(&buf);
04327 byte message_id = grf_load_byte(&buf);
04328 len -= 4;
04329
04330
04331 if (!CheckGrfLangID(lang, _cur_grffile->grf_version)) return;
04332
04333
04334
04335 if (!HasBit(severity, 7) && _cur_stage == GLS_INIT) {
04336 grfmsg(7, "GRFLoadError: Skipping non-fatal GRFLoadError in stage %d", _cur_stage);
04337 return;
04338 }
04339 ClrBit(severity, 7);
04340
04341 if (severity >= lengthof(sevstr)) {
04342 grfmsg(7, "GRFLoadError: Invalid severity id %d. Setting to 2 (non-fatal error).", severity);
04343 severity = 2;
04344 } else if (severity == 3) {
04345
04346
04347 _cur_grfconfig->status = GCS_DISABLED;
04348 ClearTemporaryNewGRFData(_cur_grffile);
04349 _skip_sprites = -1;
04350 }
04351
04352 if (message_id >= lengthof(msgstr) && message_id != 0xFF) {
04353 grfmsg(7, "GRFLoadError: Invalid message id.");
04354 return;
04355 }
04356
04357 if (len <= 1) {
04358 grfmsg(7, "GRFLoadError: No message data supplied.");
04359 return;
04360 }
04361
04362 GRFError *error = CallocT<GRFError>(1);
04363
04364 error->severity = sevstr[severity];
04365
04366 if (message_id == 0xFF) {
04367
04368 const char *message = grf_load_string(&buf, len);
04369 len -= (strlen(message) + 1);
04370
04371 error->custom_message = TranslateTTDPatchCodes(_cur_grffile->grfid, message);
04372 } else {
04373 error->message = msgstr[message_id];
04374 }
04375
04376 if (len > 0) {
04377 const char *data = grf_load_string(&buf, len);
04378 len -= (strlen(data) + 1);
04379
04380 error->data = TranslateTTDPatchCodes(_cur_grffile->grfid, data);
04381 }
04382
04383
04384 uint i = 0;
04385 for (; i < 2 && len > 0; i++) {
04386 uint param_number = grf_load_byte(&buf);
04387 error->param_value[i] = (param_number < _cur_grffile->param_end ? _cur_grffile->param[param_number] : 0);
04388 len--;
04389 }
04390 error->num_params = i;
04391
04392 _cur_grfconfig->error = error;
04393 }
04394
04395
04396 static void GRFComment(byte *buf, size_t len)
04397 {
04398
04399
04400
04401
04402 if (len == 1) return;
04403
04404 size_t text_len = len - 1;
04405 const char *text = (const char*)(buf + 1);
04406 grfmsg(2, "GRFComment: %.*s", (int)text_len, text);
04407 }
04408
04409
04410 static void SafeParamSet(byte *buf, size_t len)
04411 {
04412 if (!check_length(len, 5, "SafeParamSet")) return;
04413 buf++;
04414 uint8 target = grf_load_byte(&buf);
04415
04416
04417 if (target < 0x80) return;
04418
04419
04420
04421
04422
04423
04424 SetBit(_cur_grfconfig->flags, GCF_UNSAFE);
04425
04426
04427 _skip_sprites = -1;
04428 }
04429
04430
04431 static uint32 GetPatchVariable(uint8 param)
04432 {
04433 switch (param) {
04434
04435 case 0x0B: return max(_settings_game.game_creation.starting_year, ORIGINAL_BASE_YEAR) - ORIGINAL_BASE_YEAR;
04436
04437
04438 case 0x0E: return _settings_game.vehicle.freight_trains;
04439
04440
04441 case 0x0F: return 0;
04442
04443
04444
04445
04446 case 0x10:
04447 switch (_settings_game.vehicle.plane_speed) {
04448 default:
04449 case 4: return 1;
04450 case 3: return 2;
04451 case 2: return 2;
04452 case 1: return 4;
04453 }
04454
04455
04456
04457 case 0x11: return SPR_2CCMAP_BASE;
04458
04459
04460
04461
04462
04463
04464
04465
04466
04467
04468
04469
04470 case 0x13: {
04471 byte map_bits = 0;
04472 byte log_X = MapLogX() - 6;
04473 byte log_Y = MapLogY() - 6;
04474 byte max_edge = max(log_X, log_Y);
04475
04476 if (log_X == log_Y) {
04477 SetBit(map_bits ,0);
04478 } else {
04479 if (max_edge == log_Y) SetBit(map_bits, 1);
04480 }
04481
04482 return (map_bits << 24) | (min(log_X, log_Y) << 20) | (max_edge << 16) |
04483 (log_X << 12) | (log_Y << 8) | (log_X + log_Y);
04484 }
04485
04486 default:
04487 grfmsg(2, "ParamSet: Unknown Patch variable 0x%02X.", param);
04488 return 0;
04489 }
04490 }
04491
04492
04493 static uint32 PerformGRM(uint32 *grm, uint16 num_ids, uint16 count, uint8 op, uint8 target, const char *type)
04494 {
04495 uint start = 0;
04496 uint size = 0;
04497
04498 if (op == 6) {
04499
04500 return grm[_cur_grffile->param[target]];
04501 }
04502
04503
04504 if (op == 2 || op == 3) start = _cur_grffile->param[target];
04505
04506 for (uint i = start; i < num_ids; i++) {
04507 if (grm[i] == 0) {
04508 size++;
04509 } else {
04510 if (op == 2 || op == 3) break;
04511 start = i + 1;
04512 size = 0;
04513 }
04514
04515 if (size == count) break;
04516 }
04517
04518 if (size == count) {
04519
04520 if (op == 0 || op == 3) {
04521 grfmsg(2, "ParamSet: GRM: Reserving %d %s at %d", count, type, start);
04522 for (uint i = 0; i < count; i++) grm[start + i] = _cur_grffile->grfid;
04523 }
04524 return start;
04525 }
04526
04527
04528 if (op != 4 && op != 5) {
04529
04530 grfmsg(0, "ParamSet: GRM: Unable to allocate %d %s, deactivating", count, type);
04531 _cur_grfconfig->status = GCS_DISABLED;
04532 ClearTemporaryNewGRFData(_cur_grffile);
04533 _skip_sprites = -1;
04534 return UINT_MAX;
04535 }
04536
04537 grfmsg(1, "ParamSet: GRM: Unable to allocate %d %s", count, type);
04538 return UINT_MAX;
04539 }
04540
04541
04542
04543 static void ParamSet(byte *buf, size_t len)
04544 {
04545
04546
04547
04548
04549
04550
04551
04552
04553
04554
04555
04556
04557
04558
04559
04560
04561
04562
04563
04564
04565
04566
04567 if (!check_length(len, 5, "ParamSet")) return;
04568 buf++;
04569 uint8 target = grf_load_byte(&buf);
04570 uint8 oper = grf_load_byte(&buf);
04571 uint32 src1 = grf_load_byte(&buf);
04572 uint32 src2 = grf_load_byte(&buf);
04573
04574 uint32 data = 0;
04575 if (len >= 8) data = grf_load_dword(&buf);
04576
04577
04578
04579
04580
04581
04582
04583 if (HasBit(oper, 7)) {
04584 if (target < 0x80 && target < _cur_grffile->param_end) {
04585 grfmsg(7, "ParamSet: Param %u already defined, skipping", target);
04586 return;
04587 }
04588
04589 oper = GB(oper, 0, 7);
04590 }
04591
04592 if (src2 == 0xFE) {
04593 if (GB(data, 0, 8) == 0xFF) {
04594 if (data == 0x0000FFFF) {
04595
04596 src1 = GetPatchVariable(src1);
04597 } else {
04598
04599 uint8 op = src1;
04600 uint8 feature = GB(data, 8, 8);
04601 uint16 count = GB(data, 16, 16);
04602
04603 if (_cur_stage == GLS_RESERVE) {
04604 if (feature == 0x08) {
04605
04606 if (op == 0) {
04607
04608 if (_cur_spriteid + count >= 16384) {
04609 grfmsg(0, "ParamSet: GRM: Unable to allocate %d sprites; try changing NewGRF order", count);
04610 _cur_grfconfig->status = GCS_DISABLED;
04611 ClearTemporaryNewGRFData(_cur_grffile);
04612 _skip_sprites = -1;
04613 return;
04614 }
04615
04616
04617 grfmsg(4, "ParamSet: GRM: Allocated %d sprites at %d", count, _cur_spriteid);
04618 _grm_sprites[GRFLocation(_cur_grffile->grfid, _nfo_line)] = _cur_spriteid;
04619 _cur_spriteid += count;
04620 }
04621 }
04622
04623 src1 = 0;
04624 } else if (_cur_stage == GLS_ACTIVATION) {
04625 switch (feature) {
04626 case 0x00:
04627 case 0x01:
04628 case 0x02:
04629 case 0x03:
04630 if (!_settings_game.vehicle.dynamic_engines) {
04631 src1 = PerformGRM(&_grm_engines[_engine_offsets[feature]], _engine_counts[feature], count, op, target, "vehicles");
04632 if (_skip_sprites == -1) return;
04633 } else {
04634
04635 switch (op) {
04636 case 2:
04637 case 3:
04638 src1 = _cur_grffile->param[target];
04639 break;
04640
04641 default:
04642 src1 = 0;
04643 break;
04644 }
04645 }
04646 break;
04647
04648 case 0x08:
04649 switch (op) {
04650 case 0:
04651
04652 src1 = _grm_sprites[GRFLocation(_cur_grffile->grfid, _nfo_line)];
04653 grfmsg(4, "ParamSet: GRM: Using pre-allocated sprites at %d", src1);
04654 break;
04655
04656 case 1:
04657 src1 = _cur_spriteid;
04658 break;
04659
04660 default:
04661 grfmsg(1, "ParamSet: GRM: Unsupported operation %d for general sprites", op);
04662 return;
04663 }
04664 break;
04665
04666 case 0x0B:
04667
04668 src1 = PerformGRM(_grm_cargos, NUM_CARGO * 2, count, op, target, "cargos");
04669 if (_skip_sprites == -1) return;
04670 break;
04671
04672 default: grfmsg(1, "ParamSet: GRM: Unsupported feature 0x%X", feature); return;
04673 }
04674 } else {
04675
04676 src1 = 0;
04677 }
04678 }
04679 } else {
04680
04681 const GRFFile *file = GetFileByGRFID(data);
04682 GRFConfig *c = GetGRFConfig(data);
04683 if (c != NULL && HasBit(c->flags, GCF_STATIC) && !HasBit(_cur_grfconfig->flags, GCF_STATIC) && _networking) {
04684
04685 DisableStaticNewGRFInfluencingNonStaticNewGRFs(c);
04686 src1 = 0;
04687 } else if (file == NULL || src1 >= file->param_end || (c != NULL && c->status == GCS_DISABLED)) {
04688 src1 = 0;
04689 } else {
04690 src1 = file->param[src1];
04691 }
04692 }
04693 } else {
04694
04695
04696
04697
04698
04699 src1 = (src1 == 0xFF) ? data : GetParamVal(src1, NULL);
04700 src2 = (src2 == 0xFF) ? data : GetParamVal(src2, NULL);
04701 }
04702
04703
04704
04705
04706
04707
04708
04709 uint32 res;
04710 switch (oper) {
04711 case 0x00:
04712 res = src1;
04713 break;
04714
04715 case 0x01:
04716 res = src1 + src2;
04717 break;
04718
04719 case 0x02:
04720 res = src1 - src2;
04721 break;
04722
04723 case 0x03:
04724 res = src1 * src2;
04725 break;
04726
04727 case 0x04:
04728 res = (int32)src1 * (int32)src2;
04729 break;
04730
04731 case 0x05:
04732 if ((int32)src2 < 0) {
04733 res = src1 >> -(int32)src2;
04734 } else {
04735 res = src1 << src2;
04736 }
04737 break;
04738
04739 case 0x06:
04740 if ((int32)src2 < 0) {
04741 res = (int32)src1 >> -(int32)src2;
04742 } else {
04743 res = (int32)src1 << src2;
04744 }
04745 break;
04746
04747 case 0x07:
04748 res = src1 & src2;
04749 break;
04750
04751 case 0x08:
04752 res = src1 | src2;
04753 break;
04754
04755 case 0x09:
04756 if (src2 == 0) {
04757 res = src1;
04758 } else {
04759 res = src1 / src2;
04760 }
04761 break;
04762
04763 case 0x0A:
04764 if (src2 == 0) {
04765 res = src1;
04766 } else {
04767 res = (int32)src1 / (int32)src2;
04768 }
04769 break;
04770
04771 case 0x0B:
04772 if (src2 == 0) {
04773 res = src1;
04774 } else {
04775 res = src1 % src2;
04776 }
04777 break;
04778
04779 case 0x0C:
04780 if (src2 == 0) {
04781 res = src1;
04782 } else {
04783 res = (int32)src1 % (int32)src2;
04784 }
04785 break;
04786
04787 default: grfmsg(0, "ParamSet: Unknown operation %d, skipping", oper); return;
04788 }
04789
04790 switch (target) {
04791 case 0x8E:
04792 _traininfo_vehicle_pitch = res;
04793 break;
04794
04795 case 0x8F: {
04796 extern RailtypeInfo _railtypes[RAILTYPE_END];
04797 _railtypes[RAILTYPE_RAIL].cost_multiplier = GB(res, 0, 8);
04798 if (_settings_game.vehicle.disable_elrails) {
04799 _railtypes[RAILTYPE_ELECTRIC].cost_multiplier = GB(res, 0, 8);
04800 _railtypes[RAILTYPE_MONO].cost_multiplier = GB(res, 8, 8);
04801 } else {
04802 _railtypes[RAILTYPE_ELECTRIC].cost_multiplier = GB(res, 8, 8);
04803 _railtypes[RAILTYPE_MONO].cost_multiplier = GB(res, 16, 8);
04804 }
04805 _railtypes[RAILTYPE_MAGLEV].cost_multiplier = GB(res, 16, 8);
04806 break;
04807 }
04808
04809
04810 case 0x93:
04811 case 0x94:
04812 case 0x95:
04813 case 0x96:
04814 case 0x97:
04815 case 0x99:
04816 grfmsg(7, "ParamSet: Skipping unimplemented target 0x%02X", target);
04817 break;
04818
04819 case 0x9E:
04820 _misc_grf_features = res;
04821
04822 _traininfo_vehicle_width = HasGrfMiscBit(GMB_TRAIN_WIDTH_32_PIXELS) ? 32 : 29;
04823 break;
04824
04825 case 0x9F:
04826 grfmsg(7, "ParamSet: Skipping unimplemented target 0x%02X", target);
04827 break;
04828
04829 default:
04830 if (target < 0x80) {
04831 _cur_grffile->param[target] = res;
04832 if (target + 1U > _cur_grffile->param_end) _cur_grffile->param_end = target + 1;
04833 } else {
04834 grfmsg(7, "ParamSet: Skipping unknown target 0x%02X", target);
04835 }
04836 break;
04837 }
04838 }
04839
04840
04841 static void SafeGRFInhibit(byte *buf, size_t len)
04842 {
04843
04844
04845
04846
04847
04848 if (!check_length(len, 2, "GRFInhibit")) return;
04849 buf++;
04850 uint8 num = grf_load_byte(&buf);
04851 if (!check_length(len, 2 + 4 * num, "GRFInhibit")) return;
04852
04853 for (uint i = 0; i < num; i++) {
04854 uint32 grfid = grf_load_dword(&buf);
04855
04856
04857 if (grfid != _cur_grfconfig->grfid) {
04858 SetBit(_cur_grfconfig->flags, GCF_UNSAFE);
04859
04860
04861 _skip_sprites = -1;
04862
04863 return;
04864 }
04865 }
04866 }
04867
04868
04869 static void GRFInhibit(byte *buf, size_t len)
04870 {
04871
04872
04873
04874
04875
04876 if (!check_length(len, 2, "GRFInhibit")) return;
04877 buf++;
04878 uint8 num = grf_load_byte(&buf);
04879 if (!check_length(len, 2 + 4 * num, "GRFInhibit")) return;
04880
04881 for (uint i = 0; i < num; i++) {
04882 uint32 grfid = grf_load_dword(&buf);
04883 GRFConfig *file = GetGRFConfig(grfid);
04884
04885
04886 if (file != NULL && file != _cur_grfconfig) {
04887 grfmsg(2, "GRFInhibit: Deactivating file '%s'", file->filename);
04888 file->status = GCS_DISABLED;
04889 }
04890 }
04891 }
04892
04893
04894 static void FeatureTownName(byte *buf, size_t len)
04895 {
04896
04897
04898
04899
04900
04901
04902
04903 if (!check_length(len, 1, "FeatureTownName: definition ID")) return;
04904 buf++; len--;
04905
04906 uint32 grfid = _cur_grffile->grfid;
04907
04908 GRFTownName *townname = AddGRFTownName(grfid);
04909
04910 byte id = grf_load_byte(&buf);
04911 len--;
04912 grfmsg(6, "FeatureTownName: definition 0x%02X", id & 0x7F);
04913
04914 if (HasBit(id, 7)) {
04915
04916 ClrBit(id, 7);
04917 bool new_scheme = _cur_grffile->grf_version >= 7;
04918
04919 if (!check_length(len, 1, "FeatureTownName: lang_id")) return;
04920 byte lang = grf_load_byte(&buf);
04921 len--;
04922
04923 byte nb_gen = townname->nb_gen;
04924 do {
04925 ClrBit(lang, 7);
04926
04927 if (!check_length(len, 1, "FeatureTownName: style name")) return;
04928 const char *name = grf_load_string(&buf, len);
04929 len -= strlen(name) + 1;
04930
04931 char *lang_name = TranslateTTDPatchCodes(grfid, name);
04932 grfmsg(6, "FeatureTownName: lang 0x%X -> '%s'", lang, lang_name);
04933 free(lang_name);
04934
04935 townname->name[nb_gen] = AddGRFString(grfid, id, lang, new_scheme, name, STR_UNDEFINED);
04936
04937 if (!check_length(len, 1, "FeatureTownName: lang_id")) return;
04938 lang = grf_load_byte(&buf);
04939 len--;
04940 } while (lang != 0);
04941 townname->id[nb_gen] = id;
04942 townname->nb_gen++;
04943 }
04944
04945 if (!check_length(len, 1, "FeatureTownName: number of parts")) return;
04946 byte nb = grf_load_byte(&buf);
04947 len--;
04948 grfmsg(6, "FeatureTownName: %u parts", nb);
04949
04950 townname->nbparts[id] = nb;
04951 townname->partlist[id] = CallocT<NamePartList>(nb);
04952
04953 for (int i = 0; i < nb; i++) {
04954 if (!check_length(len, 3, "FeatureTownName: parts header")) return;
04955 byte nbtext = grf_load_byte(&buf);
04956 townname->partlist[id][i].bitstart = grf_load_byte(&buf);
04957 townname->partlist[id][i].bitcount = grf_load_byte(&buf);
04958 townname->partlist[id][i].maxprob = 0;
04959 townname->partlist[id][i].partcount = nbtext;
04960 townname->partlist[id][i].parts = CallocT<NamePart>(nbtext);
04961 len -= 3;
04962 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);
04963
04964 for (int j = 0; j < nbtext; j++) {
04965 if (!check_length(len, 2, "FeatureTownName: part")) return;
04966 byte prob = grf_load_byte(&buf);
04967 len--;
04968
04969 if (HasBit(prob, 7)) {
04970 byte ref_id = grf_load_byte(&buf);
04971 len--;
04972
04973 if (townname->nbparts[ref_id] == 0) {
04974 grfmsg(0, "FeatureTownName: definition 0x%02X doesn't exist, deactivating", ref_id);
04975 DelGRFTownName(grfid);
04976 _cur_grfconfig->status = GCS_DISABLED;
04977 ClearTemporaryNewGRFData(_cur_grffile);
04978 _skip_sprites = -1;
04979 return;
04980 }
04981
04982 grfmsg(6, "FeatureTownName: part %d, text %d, uses intermediate definition 0x%02X (with probability %d)", i, j, ref_id, prob & 0x7F);
04983 townname->partlist[id][i].parts[j].data.id = ref_id;
04984 } else {
04985 const char *text = grf_load_string(&buf, len);
04986 len -= strlen(text) + 1;
04987 townname->partlist[id][i].parts[j].data.text = TranslateTTDPatchCodes(grfid, text);
04988 grfmsg(6, "FeatureTownName: part %d, text %d, '%s' (with probability %d)", i, j, townname->partlist[id][i].parts[j].data.text, prob);
04989 }
04990 townname->partlist[id][i].parts[j].prob = prob;
04991 townname->partlist[id][i].maxprob += GB(prob, 0, 7);
04992 }
04993 grfmsg(6, "FeatureTownName: part %d, total probability %d", i, townname->partlist[id][i].maxprob);
04994 }
04995 }
04996
04997
04998 static void DefineGotoLabel(byte *buf, size_t len)
04999 {
05000
05001
05002
05003
05004
05005 if (!check_length(len, 1, "DefineGotoLabel")) return;
05006 buf++; len--;
05007
05008 GRFLabel *label = MallocT<GRFLabel>(1);
05009 label->label = grf_load_byte(&buf);
05010 label->nfo_line = _nfo_line;
05011 label->pos = FioGetPos();
05012 label->next = NULL;
05013
05014
05015 if (_cur_grffile->label == NULL) {
05016 _cur_grffile->label = label;
05017 } else {
05018
05019 GRFLabel *l;
05020 for (l = _cur_grffile->label; l->next != NULL; l = l->next) {}
05021 l->next = label;
05022 }
05023
05024 grfmsg(2, "DefineGotoLabel: GOTO target with label 0x%02X", label->label);
05025 }
05026
05027
05028 static void GRFSound(byte *buf, size_t len)
05029 {
05030
05031
05032
05033
05034 if (!check_length(len, 1, "GRFSound")) return;
05035 buf++;
05036 uint16 num = grf_load_word(&buf);
05037
05038 _grf_data_blocks = num;
05039 _grf_data_type = GDT_SOUND;
05040
05041 if (_cur_grffile->sound_offset == 0) _cur_grffile->sound_offset = GetNumSounds();
05042 }
05043
05044
05045 static void SkipAct11(byte *buf, size_t len)
05046 {
05047
05048
05049
05050
05051 if (!check_length(len, 1, "SkipAct11")) return;
05052 buf++;
05053 _skip_sprites = grf_load_word(&buf);
05054
05055 grfmsg(3, "SkipAct11: Skipping %d sprites", _skip_sprites);
05056 }
05057
05058 static void ImportGRFSound(byte *buf, int len)
05059 {
05060 const GRFFile *file;
05061 FileEntry *se = AllocateFileEntry();
05062 uint32 grfid = grf_load_dword(&buf);
05063 uint16 sound = grf_load_word(&buf);
05064
05065 file = GetFileByGRFID(grfid);
05066 if (file == NULL || file->sound_offset == 0) {
05067 grfmsg(1, "ImportGRFSound: Source file not available");
05068 return;
05069 }
05070
05071 if (file->sound_offset + sound >= GetNumSounds()) {
05072 grfmsg(1, "ImportGRFSound: Sound effect %d is invalid", sound);
05073 return;
05074 }
05075
05076 grfmsg(2, "ImportGRFSound: Copying sound %d (%d) from file %X", sound, file->sound_offset + sound, grfid);
05077
05078 *se = *GetSound(file->sound_offset + sound);
05079
05080
05081 se->volume = 128;
05082 se->priority = 0;
05083 }
05084
05085
05086 static void GRFImportBlock(byte *buf, int len)
05087 {
05088 if (_grf_data_blocks == 0) {
05089 grfmsg(2, "GRFImportBlock: Unexpected import block, skipping");
05090 return;
05091 }
05092
05093 buf++;
05094
05095 _grf_data_blocks--;
05096
05097
05098
05099 if (grf_load_byte(&buf) != _grf_data_type) {
05100 grfmsg(1, "GRFImportBlock: Import type mismatch");
05101 }
05102
05103 switch (_grf_data_type) {
05104 case GDT_SOUND: ImportGRFSound(buf, len - 1); break;
05105 default: NOT_REACHED(); break;
05106 }
05107 }
05108
05109 static void LoadGRFSound(byte *buf, int len)
05110 {
05111 byte *buf_start = buf;
05112
05113
05114
05115 FileEntry *se = AllocateFileEntry();
05116
05117 if (grf_load_dword(&buf) != BSWAP32('RIFF')) {
05118 grfmsg(1, "LoadGRFSound: Missing RIFF header");
05119 return;
05120 }
05121
05122
05123 grf_load_dword(&buf);
05124
05125 if (grf_load_dword(&buf) != BSWAP32('WAVE')) {
05126 grfmsg(1, "LoadGRFSound: Invalid RIFF type");
05127 return;
05128 }
05129
05130 for (;;) {
05131 uint32 tag = grf_load_dword(&buf);
05132 uint32 size = grf_load_dword(&buf);
05133
05134 switch (tag) {
05135 case ' tmf':
05136
05137 if (grf_load_word(&buf) != 1) {
05138 grfmsg(1, "LoadGRFSound: Invalid audio format");
05139 return;
05140 }
05141 se->channels = grf_load_word(&buf);
05142 se->rate = grf_load_dword(&buf);
05143 grf_load_dword(&buf);
05144 grf_load_word(&buf);
05145 se->bits_per_sample = grf_load_word(&buf);
05146
05147
05148 for (; size > 16; size--) grf_load_byte(&buf);
05149 break;
05150
05151 case 'atad':
05152 se->file_size = size;
05153 se->file_offset = FioGetPos() - (len - (buf - buf_start)) + 1;
05154 se->file_slot = _file_index;
05155
05156
05157 se->volume = 0x80;
05158 se->priority = 0;
05159
05160 grfmsg(2, "LoadGRFSound: channels %u, sample rate %u, bits per sample %u, length %u", se->channels, se->rate, se->bits_per_sample, size);
05161 return;
05162
05163 default:
05164 se->file_size = 0;
05165 return;
05166 }
05167 }
05168 }
05169
05170
05171 static void LoadFontGlyph(byte *buf, size_t len)
05172 {
05173
05174
05175
05176
05177
05178
05179
05180 buf++; len--;
05181 if (!check_length(len, 1, "LoadFontGlyph")) return;
05182
05183 uint8 num_def = grf_load_byte(&buf);
05184
05185 if (!check_length(len, 1 + num_def * 4, "LoadFontGlyph")) return;
05186
05187 for (uint i = 0; i < num_def; i++) {
05188 FontSize size = (FontSize)grf_load_byte(&buf);
05189 uint8 num_char = grf_load_byte(&buf);
05190 uint16 base_char = grf_load_word(&buf);
05191
05192 grfmsg(7, "LoadFontGlyph: Loading %u glyph(s) at 0x%04X for size %u", num_char, base_char, size);
05193
05194 for (uint c = 0; c < num_char; c++) {
05195 SetUnicodeGlyph(size, base_char + c, _cur_spriteid);
05196 _nfo_line++;
05197 LoadNextSprite(_cur_spriteid++, _file_index, _nfo_line);
05198 }
05199 }
05200 }
05201
05202
05203 static void SkipAct12(byte *buf, size_t len)
05204 {
05205
05206
05207
05208
05209
05210
05211
05212 buf++; len--;
05213 if (!check_length(len, 1, "SkipAct12")) return;
05214 uint8 num_def = grf_load_byte(&buf);
05215
05216 if (!check_length(len, 1 + num_def * 4, "SkipAct12")) return;
05217
05218 for (uint i = 0; i < num_def; i++) {
05219
05220 grf_load_byte(&buf);
05221
05222
05223 _skip_sprites += grf_load_byte(&buf);
05224
05225
05226 grf_load_word(&buf);
05227 }
05228
05229 grfmsg(3, "SkipAct12: Skipping %d sprites", _skip_sprites);
05230 }
05231
05232
05233 static void TranslateGRFStrings(byte *buf, size_t len)
05234 {
05235
05236
05237
05238
05239
05240
05241
05242 buf++; len--;
05243 if (!check_length(len, 7, "TranslateGRFString")) return;
05244
05245 uint32 grfid = grf_load_dword(&buf);
05246 const GRFConfig *c = GetGRFConfig(grfid);
05247 if (c == NULL || (c->status != GCS_INITIALISED && c->status != GCS_ACTIVATED)) {
05248 grfmsg(7, "TranslateGRFStrings: GRFID 0x%08x unknown, skipping action 13", BSWAP32(grfid));
05249 return;
05250 }
05251
05252 if (c->status == GCS_INITIALISED) {
05253
05254
05255 GRFError *error = CallocT<GRFError>(1);
05256
05257 char tmp[256];
05258 GetString(tmp, STR_NEWGRF_ERROR_AFTER_TRANSLATED_FILE, lastof(tmp));
05259 error->data = strdup(tmp);
05260
05261 error->message = STR_NEWGRF_ERROR_LOAD_AFTER;
05262 error->severity = STR_NEWGRF_ERROR_MSG_FATAL;
05263
05264 if (_cur_grfconfig->error != NULL) free(_cur_grfconfig->error);
05265 _cur_grfconfig->error = error;
05266
05267 _cur_grfconfig->status = GCS_DISABLED;
05268 ClearTemporaryNewGRFData(_cur_grffile);
05269 _skip_sprites = -1;
05270 return;
05271 }
05272
05273 byte num_strings = grf_load_byte(&buf);
05274 uint16 first_id = grf_load_word(&buf);
05275
05276 if (!((first_id >= 0xD000 && first_id + num_strings <= 0xD3FF) || (first_id >= 0xDC00 && first_id + num_strings <= 0xDCFF))) {
05277 grfmsg(7, "TranslateGRFStrings: Attempting to set out-of-range string IDs in action 13 (first: 0x%4X, number: 0x%2X)", first_id, num_strings);
05278 return;
05279 }
05280
05281 len -= 7;
05282
05283 for (uint i = 0; i < num_strings && len > 0; i++) {
05284 const char *string = grf_load_string(&buf, len);
05285 size_t string_length = strlen(string) + 1;
05286
05287 len -= (int)string_length;
05288
05289 if (string_length == 1) {
05290 grfmsg(7, "TranslateGRFString: Ignoring empty string.");
05291 continue;
05292 }
05293
05294
05295
05296
05297
05298
05299 AddGRFString(grfid, first_id + i, 0x7F, true, string, STR_UNDEFINED);
05300 }
05301 }
05302
05303
05304 static void GRFDataBlock(byte *buf, int len)
05305 {
05306 if (_grf_data_blocks == 0) {
05307 grfmsg(2, "GRFDataBlock: unexpected data block, skipping");
05308 return;
05309 }
05310
05311 buf++;
05312 uint8 name_len = grf_load_byte(&buf);
05313 const char *name = (const char *)buf;
05314 buf += name_len + 1;
05315
05316 grfmsg(2, "GRFDataBlock: block name '%s'...", name);
05317
05318 _grf_data_blocks--;
05319
05320 switch (_grf_data_type) {
05321 case GDT_SOUND: LoadGRFSound(buf, len - name_len - 2); break;
05322 default: NOT_REACHED(); break;
05323 }
05324 }
05325
05326
05327
05328 static void GRFUnsafe(byte *buf, size_t len)
05329 {
05330 SetBit(_cur_grfconfig->flags, GCF_UNSAFE);
05331
05332
05333 _skip_sprites = -1;
05334 }
05335
05336
05337 static void InitializeGRFSpecial()
05338 {
05339 _ttdpatch_flags[0] = ((_settings_game.station.always_small_airport ? 1 : 0) << 0x0C)
05340 | (1 << 0x0D)
05341 | (1 << 0x0E)
05342 | ((_settings_game.construction.longbridges ? 1 : 0) << 0x0F)
05343 | (0 << 0x10)
05344 | (1 << 0x12)
05345 | (1 << 0x13)
05346 | ((_settings_game.vehicle.never_expire_vehicles ? 1 : 0) << 0x16)
05347 | (1 << 0x1B)
05348 | (1 << 0x1D)
05349 | (1 << 0x1E);
05350
05351 _ttdpatch_flags[1] = ((_settings_game.economy.station_noise_level ? 1 : 0) << 0x07)
05352 | ((_settings_game.vehicle.mammoth_trains ? 1 : 0) << 0x08)
05353 | (1 << 0x09)
05354 | (0 << 0x0B)
05355 | ((_settings_game.order.gradual_loading ? 1 : 0) << 0x0C)
05356 | (1 << 0x12)
05357 | (1 << 0x13)
05358 | (1 << 0x14)
05359 | (1 << 0x16)
05360 | (1 << 0x17)
05361 | (1 << 0x18)
05362 | (1 << 0x19)
05363 | (1 << 0x1A)
05364 | ((_settings_game.construction.signal_side ? 1 : 0) << 0x1B)
05365 | ((_settings_game.vehicle.disable_elrails ? 0 : 1) << 0x1C);
05366
05367 _ttdpatch_flags[2] = (1 << 0x01)
05368 | (1 << 0x03)
05369 | (0 << 0x0B)
05370 | (0 << 0x0C)
05371 | ((_settings_game.construction.build_on_slopes ? 1 : 0) << 0x0D)
05372 | (1 << 0x0E)
05373 | (1 << 0x0F)
05374 | (0 << 0x10)
05375 | (0 << 0x11)
05376 | (1 << 0x12)
05377 | (1 << 0x13)
05378 | (1 << 0x14)
05379 | ((_settings_game.construction.build_on_slopes ? 1 : 0) << 0x15)
05380 | (1 << 0x16)
05381 | (1 << 0x17)
05382 | ((_settings_game.vehicle.freight_trains > 1 ? 1 : 0) << 0x18)
05383 | (1 << 0x19)
05384 | (1 << 0x1A)
05385 | (1 << 0x1B)
05386 | (1 << 0x1C)
05387 | ((_settings_game.vehicle.wagon_speed_limits ? 1 : 0) << 0x1D)
05388 | (1 << 0x1E)
05389 | (0 << 0x1F);
05390
05391 _ttdpatch_flags[3] = (0 << 0x00)
05392 | (1 << 0x01)
05393 | ((_settings_game.economy.allow_town_roads || _generating_world ? 0 : 1) << 0x02)
05394 | (1 << 0x03)
05395 | (0 << 0x04)
05396 | (1 << 0x05)
05397 | (1 << 0x06)
05398 | (1 << 0x07)
05399 | ((_settings_game.order.improved_load ? 1 : 0) << 0x08)
05400 | (0 << 0x09)
05401 | (0 << 0x0A)
05402 | (1 << 0x0B)
05403 | (1 << 0x0C)
05404 | (1 << 0x0D)
05405 | ((_settings_game.station.nonuniform_stations ? 1 : 0) << 0x0E)
05406 | (1 << 0x0F)
05407 | (1 << 0x10)
05408 | (1 << 0x11)
05409 | (1 << 0x12)
05410 | (0 << 0x13)
05411 | (1 << 0x14)
05412 | (0 << 0x15)
05413 | (1 << 0x16)
05414 | (1 << 0x17)
05415 | ((_settings_game.vehicle.dynamic_engines ? 1 : 0) << 0x18)
05416 | (1 << 0x1E)
05417 | (1 << 0x1F);
05418 }
05419
05420 static void ResetCustomStations()
05421 {
05422 for (GRFFile *file = _first_grffile; file != NULL; file = file->next) {
05423 if (file->stations == NULL) continue;
05424 for (uint i = 0; i < MAX_STATIONS; i++) {
05425 if (file->stations[i] == NULL) continue;
05426 StationSpec *statspec = file->stations[i];
05427
05428
05429 if (!statspec->copied_renderdata) {
05430 for (uint t = 0; t < statspec->tiles; t++) {
05431 free((void*)statspec->renderdata[t].seq);
05432 }
05433 free(statspec->renderdata);
05434 }
05435
05436
05437 if (!statspec->copied_layouts) {
05438 for (uint l = 0; l < statspec->lengths; l++) {
05439 for (uint p = 0; p < statspec->platforms[l]; p++) {
05440 free(statspec->layouts[l][p]);
05441 }
05442 free(statspec->layouts[l]);
05443 }
05444 free(statspec->layouts);
05445 free(statspec->platforms);
05446 }
05447
05448
05449 free(statspec);
05450 }
05451
05452
05453 free(file->stations);
05454 file->stations = NULL;
05455 }
05456 }
05457
05458 static void ResetCustomHouses()
05459 {
05460 GRFFile *file;
05461 uint i;
05462
05463 for (file = _first_grffile; file != NULL; file = file->next) {
05464 if (file->housespec == NULL) continue;
05465 for (i = 0; i < HOUSE_MAX; i++) {
05466 free(file->housespec[i]);
05467 }
05468
05469 free(file->housespec);
05470 file->housespec = NULL;
05471 }
05472 }
05473
05474 static void ResetCustomIndustries()
05475 {
05476 GRFFile *file;
05477
05478 for (file = _first_grffile; file != NULL; file = file->next) {
05479 uint i;
05480
05481
05482 if (file->industryspec != NULL) {
05483
05484 for (i = 0; i < NUM_INDUSTRYTYPES; i++) {
05485 IndustrySpec *ind = file->industryspec[i];
05486
05487 if (ind != NULL) {
05488
05489 if (HasBit(ind->cleanup_flag, CLEAN_RANDOMSOUNDS)) {
05490 free((void*)ind->random_sounds);
05491 }
05492
05493
05494 if (HasBit(ind->cleanup_flag, CLEAN_TILELSAYOUT) && ind->table != NULL) {
05495 for (int j = 0; j < ind->num_table; j++) {
05496
05497 if (ind->table[j] != NULL) {
05498 free((IndustryTileTable*)ind->table[j]);
05499 }
05500 }
05501
05502 free((IndustryTileTable**)ind->table);
05503 ind->table = NULL;
05504 }
05505
05506 free(ind);
05507 ind = NULL;
05508 }
05509 }
05510
05511 free(file->industryspec);
05512 file->industryspec = NULL;
05513 }
05514
05515 if (file->indtspec != NULL) {
05516 for (i = 0; i < NUM_INDUSTRYTILES; i++) {
05517 if (file->indtspec[i] != NULL) {
05518 free(file->indtspec[i]);
05519 file->indtspec[i] = NULL;
05520 }
05521 }
05522
05523 free(file->indtspec);
05524 file->indtspec = NULL;
05525 }
05526 }
05527 }
05528
05529 static void ResetNewGRF()
05530 {
05531 GRFFile *next;
05532
05533 for (GRFFile *f = _first_grffile; f != NULL; f = next) {
05534 next = f->next;
05535
05536 free(f->filename);
05537 free(f->cargo_list);
05538 free(f->railtype_list);
05539 free(f);
05540 }
05541
05542 _first_grffile = NULL;
05543 _cur_grffile = NULL;
05544 }
05545
05546 static void ResetNewGRFErrors()
05547 {
05548 for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
05549 if (!HasBit(c->flags, GCF_COPY) && c->error != NULL) {
05550 free(c->error->custom_message);
05551 free(c->error->data);
05552 free(c->error);
05553 c->error = NULL;
05554 }
05555 }
05556 }
05557
05562 static void ResetNewGRFData()
05563 {
05564 CleanUpStrings();
05565 CleanUpGRFTownNames();
05566
05567
05568 SetupEngines();
05569
05570
05571 ResetBridges();
05572
05573
05574 ResetRailTypes();
05575
05576
05577 _gted = CallocT<GRFTempEngineData>(GetEnginePoolSize());
05578
05579
05580 memset(&_grm_engines, 0, sizeof(_grm_engines));
05581 memset(&_grm_cargos, 0, sizeof(_grm_cargos));
05582
05583
05584 ResetGenericCallbacks();
05585
05586
05587 ResetPriceBaseMultipliers();
05588
05589
05590 ResetCurrencies();
05591
05592
05593 ResetCustomHouses();
05594 ResetHouses();
05595
05596
05597 ResetCustomIndustries();
05598 ResetIndustries();
05599
05600
05601 ResetStationClasses();
05602 ResetCustomStations();
05603
05604
05605 memset(_water_feature, 0, sizeof(_water_feature));
05606
05607
05608 ClearSnowLine();
05609
05610
05611 ResetNewGRF();
05612
05613
05614 ResetNewGRFErrors();
05615
05616
05617 SetupCargoForClimate(_settings_game.game_creation.landscape);
05618
05619
05620 _misc_grf_features = 0;
05621 _traininfo_vehicle_pitch = 0;
05622 _traininfo_vehicle_width = 29;
05623
05624 _loaded_newgrf_features.has_2CC = false;
05625 _loaded_newgrf_features.has_newhouses = false;
05626 _loaded_newgrf_features.has_newindustries = false;
05627 _loaded_newgrf_features.shore = SHORE_REPLACE_NONE;
05628
05629
05630 _grf_id_overrides.clear();
05631
05632 InitializeSoundPool();
05633 InitializeSpriteGroupPool();
05634 }
05635
05636 static void BuildCargoTranslationMap()
05637 {
05638 memset(_cur_grffile->cargo_map, 0xFF, sizeof(_cur_grffile->cargo_map));
05639
05640 for (CargoID c = 0; c < NUM_CARGO; c++) {
05641 const CargoSpec *cs = GetCargo(c);
05642 if (!cs->IsValid()) continue;
05643
05644 if (_cur_grffile->cargo_max == 0) {
05645
05646 _cur_grffile->cargo_map[c] = cs->bitnum;
05647 } else {
05648
05649 for (uint i = 0; i < _cur_grffile->cargo_max; i++) {
05650 if (cs->label == _cur_grffile->cargo_list[i]) {
05651 _cur_grffile->cargo_map[c] = i;
05652 break;
05653 }
05654 }
05655 }
05656 }
05657 }
05658
05659 static void InitNewGRFFile(const GRFConfig *config, int sprite_offset)
05660 {
05661 GRFFile *newfile = GetFileByFilename(config->filename);
05662 if (newfile != NULL) {
05663
05664 newfile->sprite_offset = sprite_offset;
05665 _cur_grffile = newfile;
05666 return;
05667 }
05668
05669 newfile = CallocT<GRFFile>(1);
05670
05671 if (newfile == NULL) error ("Out of memory");
05672
05673 newfile->filename = strdup(config->filename);
05674 newfile->sprite_offset = sprite_offset;
05675
05676
05677 assert(lengthof(newfile->param) == lengthof(config->param) && lengthof(config->param) == 0x80);
05678 newfile->param_end = config->num_params;
05679 memcpy(newfile->param, config->param, sizeof(newfile->param));
05680
05681 if (_first_grffile == NULL) {
05682 _cur_grffile = newfile;
05683 _first_grffile = newfile;
05684 } else {
05685 _cur_grffile->next = newfile;
05686 _cur_grffile = newfile;
05687 }
05688 }
05689
05690
05693 static const CargoLabel _default_refitmasks_rail[] = {
05694 'PASS', 'COAL', 'MAIL', 'LVST', 'GOOD', 'GRAI', 'WHEA', 'MAIZ', 'WOOD',
05695 'IORE', 'STEL', 'VALU', 'GOLD', 'DIAM', 'PAPR', 'FOOD', 'FRUT', 'CORE',
05696 'WATR', 'SUGR', 'TOYS', 'BATT', 'SWET', 'TOFF', 'COLA', 'CTCD', 'BUBL',
05697 'PLST', 'FZDR',
05698 0 };
05699
05700 static const CargoLabel _default_refitmasks_road[] = {
05701 0 };
05702
05703 static const CargoLabel _default_refitmasks_ships[] = {
05704 'COAL', 'MAIL', 'LVST', 'GOOD', 'GRAI', 'WHEA', 'MAIZ', 'WOOD', 'IORE',
05705 'STEL', 'VALU', 'GOLD', 'DIAM', 'PAPR', 'FOOD', 'FRUT', 'CORE', 'WATR',
05706 'RUBR', 'SUGR', 'TOYS', 'BATT', 'SWET', 'TOFF', 'COLA', 'CTCD', 'BUBL',
05707 'PLST', 'FZDR',
05708 0 };
05709
05710 static const CargoLabel _default_refitmasks_aircraft[] = {
05711 'PASS', 'MAIL', 'GOOD', 'VALU', 'GOLD', 'DIAM', 'FOOD', 'FRUT', 'SUGR',
05712 'TOYS', 'BATT', 'SWET', 'TOFF', 'COLA', 'CTCD', 'BUBL', 'PLST', 'FZDR',
05713 0 };
05714
05715 static const CargoLabel *_default_refitmasks[] = {
05716 _default_refitmasks_rail,
05717 _default_refitmasks_road,
05718 _default_refitmasks_ships,
05719 _default_refitmasks_aircraft,
05720 };
05721
05722
05726 static void CalculateRefitMasks()
05727 {
05728 Engine *e;
05729
05730 FOR_ALL_ENGINES(e) {
05731 EngineID engine = e->index;
05732 EngineInfo *ei = &e->info;
05733 uint32 mask = 0;
05734 uint32 not_mask = 0;
05735 uint32 xor_mask = 0;
05736
05737 if (ei->refit_mask != 0) {
05738 const GRFFile *file = e->grffile;
05739 if (file != NULL && file->cargo_max != 0) {
05740
05741 uint num_cargo = min(32, file->cargo_max);
05742 for (uint i = 0; i < num_cargo; i++) {
05743 if (!HasBit(ei->refit_mask, i)) continue;
05744
05745 CargoID c = GetCargoIDByLabel(file->cargo_list[i]);
05746 if (c == CT_INVALID) continue;
05747
05748 SetBit(xor_mask, c);
05749 }
05750 } else {
05751
05752 for (CargoID c = 0; c < NUM_CARGO; c++) {
05753 const CargoSpec *cs = GetCargo(c);
05754 if (!cs->IsValid()) continue;
05755
05756 if (HasBit(ei->refit_mask, cs->bitnum)) SetBit(xor_mask, c);
05757 }
05758 }
05759 }
05760
05761 if (_gted[engine].cargo_allowed != 0) {
05762
05763 for (CargoID i = 0; i < NUM_CARGO; i++) {
05764 const CargoSpec *cs = GetCargo(i);
05765 if (_gted[engine].cargo_allowed & cs->classes) SetBit(mask, i);
05766 if (_gted[engine].cargo_disallowed & cs->classes) SetBit(not_mask, i);
05767 }
05768 } else if (xor_mask == 0) {
05769
05770 if (e->type != VEH_TRAIN || (e->u.rail.capacity != 0 && e->u.rail.railveh_type != RAILVEH_WAGON)) {
05771 const CargoLabel *cl = _default_refitmasks[e->type];
05772 for (uint i = 0;; i++) {
05773 if (cl[i] == 0) break;
05774
05775 CargoID cargo = GetCargoIDByLabel(cl[i]);
05776 if (cargo == CT_INVALID) continue;
05777
05778 SetBit(xor_mask, cargo);
05779 }
05780 }
05781 }
05782
05783 ei->refit_mask = ((mask & ~not_mask) ^ xor_mask) & _cargo_mask;
05784
05785
05786
05787 switch (e->type) {
05788 default: NOT_REACHED();
05789 case VEH_AIRCRAFT:
05790 if (FindFirstRefittableCargo(engine) == CT_INVALID) ei->climates = 0x80;
05791 break;
05792
05793 case VEH_TRAIN: {
05794 RailVehicleInfo *rvi = &e->u.rail;
05795 if (rvi->cargo_type == CT_INVALID) rvi->cargo_type = FindFirstRefittableCargo(engine);
05796 if (rvi->cargo_type == CT_INVALID) ei->climates = 0x80;
05797 break;
05798 }
05799 case VEH_ROAD: {
05800 RoadVehicleInfo *rvi = &e->u.road;
05801 if (rvi->cargo_type == CT_INVALID) rvi->cargo_type = FindFirstRefittableCargo(engine);
05802 if (rvi->cargo_type == CT_INVALID) ei->climates = 0x80;
05803 break;
05804 }
05805 case VEH_SHIP: {
05806 ShipVehicleInfo *svi = &e->u.ship;
05807 if (svi->cargo_type == CT_INVALID) svi->cargo_type = FindFirstRefittableCargo(engine);
05808 if (svi->cargo_type == CT_INVALID) ei->climates = 0x80;
05809 break;
05810 }
05811 }
05812 }
05813 }
05814
05819 static void FinaliseHouseArray()
05820 {
05821
05822
05823
05824
05825
05826
05827
05828
05829
05830 Year min_year = MAX_YEAR;
05831
05832 for (GRFFile *file = _first_grffile; file != NULL; file = file->next) {
05833 if (file->housespec == NULL) continue;
05834
05835 for (int i = 0; i < HOUSE_MAX; i++) {
05836 HouseSpec *hs = file->housespec[i];
05837 if (hs != NULL) {
05838 _house_mngr.SetEntitySpec(hs);
05839 if (hs->min_year < min_year) min_year = hs->min_year;
05840 }
05841 }
05842 }
05843
05844 if (min_year != 0) {
05845 for (int i = 0; i < HOUSE_MAX; i++) {
05846 HouseSpec *hs = GetHouseSpecs(i);
05847
05848 if (hs->enabled && hs->min_year == min_year) hs->min_year = 0;
05849 }
05850 }
05851 }
05852
05856 static void FinaliseIndustriesArray()
05857 {
05858 for (GRFFile *file = _first_grffile; file != NULL; file = file->next) {
05859 if (file->industryspec != NULL) {
05860 for (int i = 0; i < NUM_INDUSTRYTYPES; i++) {
05861 IndustrySpec *indsp = file->industryspec[i];
05862
05863 if (indsp != NULL && indsp->enabled) {
05864 StringID strid;
05865
05866
05867
05868 strid = GetGRFStringID(indsp->grf_prop.grffile->grfid, indsp->name);
05869 if (strid != STR_UNDEFINED) indsp->name = strid;
05870
05871 strid = GetGRFStringID(indsp->grf_prop.grffile->grfid, indsp->closure_text);
05872 if (strid != STR_UNDEFINED) indsp->closure_text = strid;
05873
05874 strid = GetGRFStringID(indsp->grf_prop.grffile->grfid, indsp->production_up_text);
05875 if (strid != STR_UNDEFINED) indsp->production_up_text = strid;
05876
05877 strid = GetGRFStringID(indsp->grf_prop.grffile->grfid, indsp->production_down_text);
05878 if (strid != STR_UNDEFINED) indsp->production_down_text = strid;
05879
05880 strid = GetGRFStringID(indsp->grf_prop.grffile->grfid, indsp->new_industry_text);
05881 if (strid != STR_UNDEFINED) indsp->new_industry_text = strid;
05882
05883 if (indsp->station_name != STR_NULL) {
05884
05885
05886 strid = GetGRFStringID(indsp->grf_prop.grffile->grfid, indsp->station_name);
05887 if (strid != STR_UNDEFINED) indsp->station_name = strid;
05888 }
05889
05890 _industry_mngr.SetEntitySpec(indsp);
05891 _loaded_newgrf_features.has_newindustries = true;
05892 }
05893 }
05894 }
05895
05896 if (file->indtspec != NULL) {
05897 for (int i = 0; i < NUM_INDUSTRYTILES; i++) {
05898 IndustryTileSpec *indtsp = file->indtspec[i];
05899 if (indtsp != NULL) {
05900 _industile_mngr.SetEntitySpec(indtsp);
05901 }
05902 }
05903 }
05904 }
05905
05906 for (uint j = 0; j < NUM_INDUSTRYTYPES; j++) {
05907 IndustrySpec *indsp = &_industry_specs[j];
05908 if (indsp->enabled && indsp->grf_prop.grffile != NULL) {
05909 for (uint i = 0; i < 3; i++) {
05910 indsp->conflicting[i] = MapNewGRFIndustryType(indsp->conflicting[i], indsp->grf_prop.grffile->grfid);
05911 }
05912 }
05913 }
05914 }
05915
05916
05917
05918
05919
05920
05921
05922 static void DecodeSpecialSprite(byte *buf, uint num, GrfLoadingStage stage)
05923 {
05924
05925
05926
05927
05928
05929
05930
05931
05932
05933
05934
05935
05936 static const SpecialSpriteHandler handlers[][GLS_END] = {
05937 { NULL, SafeChangeInfo, NULL, NULL, ReserveChangeInfo, FeatureChangeInfo, },
05938 { SkipAct1, SkipAct1, SkipAct1, SkipAct1, SkipAct1, NewSpriteSet, },
05939 { NULL, NULL, NULL, NULL, NULL, NewSpriteGroup, },
05940 { NULL, GRFUnsafe, NULL, NULL, NULL, FeatureMapSpriteGroup, },
05941 { NULL, NULL, NULL, NULL, NULL, FeatureNewName, },
05942 { SkipAct5, SkipAct5, SkipAct5, SkipAct5, SkipAct5, GraphicsNew, },
05943 { NULL, NULL, NULL, CfgApply, CfgApply, CfgApply, },
05944 { NULL, NULL, NULL, NULL, SkipIf, SkipIf, },
05945 { ScanInfo, NULL, NULL, GRFInfo, GRFInfo, GRFInfo, },
05946 { NULL, NULL, NULL, SkipIf, SkipIf, SkipIf, },
05947 { SkipActA, SkipActA, SkipActA, SkipActA, SkipActA, SpriteReplace, },
05948 { NULL, NULL, NULL, GRFLoadError, GRFLoadError, GRFLoadError, },
05949 { NULL, NULL, NULL, GRFComment, NULL, GRFComment, },
05950 { NULL, SafeParamSet, NULL, ParamSet, ParamSet, ParamSet, },
05951 { NULL, SafeGRFInhibit, NULL, GRFInhibit, GRFInhibit, GRFInhibit, },
05952 { NULL, GRFUnsafe, NULL, FeatureTownName, NULL, NULL, },
05953 { NULL, NULL, DefineGotoLabel, NULL, NULL, NULL, },
05954 { SkipAct11,GRFUnsafe, SkipAct11, SkipAct11, SkipAct11, GRFSound, },
05955 { SkipAct12, SkipAct12, SkipAct12, SkipAct12, SkipAct12, LoadFontGlyph, },
05956 { NULL, NULL, NULL, NULL, NULL, TranslateGRFStrings, },
05957 };
05958
05959 GRFLocation location(_cur_grfconfig->grfid, _nfo_line);
05960
05961 GRFLineToSpriteOverride::iterator it = _grf_line_to_action6_sprite_override.find(location);
05962 if (it == _grf_line_to_action6_sprite_override.end()) {
05963
05964
05965 FioReadBlock(buf, num);
05966 } else {
05967
05968 buf = _grf_line_to_action6_sprite_override[location];
05969 grfmsg(7, "DecodeSpecialSprite: Using preloaded pseudo sprite data");
05970
05971
05972 FioSeekTo(num, SEEK_CUR);
05973 }
05974
05975 byte action = buf[0];
05976
05977 if (action == 0xFF) {
05978 grfmsg(7, "DecodeSpecialSprite: Handling data block in stage %d", stage);
05979 GRFDataBlock(buf, num);
05980 } else if (action == 0xFE) {
05981 grfmsg(7, "DecodeSpecialSprite: Handling import block in stage %d", stage);
05982 GRFImportBlock(buf, num);
05983 } else if (action >= lengthof(handlers)) {
05984 grfmsg(7, "DecodeSpecialSprite: Skipping unknown action 0x%02X", action);
05985 } else if (handlers[action][stage] == NULL) {
05986 grfmsg(7, "DecodeSpecialSprite: Skipping action 0x%02X in stage %d", action, stage);
05987 } else {
05988 grfmsg(7, "DecodeSpecialSprite: Handling action 0x%02X in stage %d", action, stage);
05989 handlers[action][stage](buf, num);
05990 }
05991 }
05992
05993
05994 void LoadNewGRFFile(GRFConfig *config, uint file_index, GrfLoadingStage stage)
05995 {
05996 const char *filename = config->filename;
05997 uint16 num;
05998
05999
06000
06001
06002
06003
06004
06005
06006
06007
06008 if (stage != GLS_FILESCAN && stage != GLS_SAFETYSCAN && stage != GLS_LABELSCAN) {
06009 _cur_grffile = GetFileByFilename(filename);
06010 if (_cur_grffile == NULL) usererror("File '%s' lost in cache.\n", filename);
06011 if (stage == GLS_RESERVE && config->status != GCS_INITIALISED) return;
06012 if (stage == GLS_ACTIVATION && !HasBit(config->flags, GCF_RESERVED)) return;
06013 _cur_grffile->is_ottdfile = config->IsOpenTTDBaseGRF();
06014 }
06015
06016 if (file_index > LAST_GRF_SLOT) {
06017 DEBUG(grf, 0, "'%s' is not loaded as the maximum number of GRFs has been reached", filename);
06018 config->status = GCS_DISABLED;
06019 config->error = CallocT<GRFError>(1);
06020 config->error->severity = STR_NEWGRF_ERROR_MSG_FATAL;
06021 config->error->message = STR_NEWGRF_ERROR_TOO_MANY_NEWGRFS_LOADED;
06022 return;
06023 }
06024
06025 FioOpenFile(file_index, filename);
06026 _file_index = file_index;
06027 _palette_remap_grf[_file_index] = (config->windows_paletted != (_use_palette == PAL_WINDOWS));
06028
06029 _cur_grfconfig = config;
06030
06031 DEBUG(grf, 2, "LoadNewGRFFile: Reading NewGRF-file '%s'", filename);
06032
06033
06034
06035
06036 if (FioReadWord() == 4 && FioReadByte() == 0xFF) {
06037 FioReadDword();
06038 } else {
06039 DEBUG(grf, 7, "LoadNewGRFFile: Custom .grf has invalid format");
06040 return;
06041 }
06042
06043 _skip_sprites = 0;
06044 _nfo_line = 0;
06045
06046 ReusableBuffer<byte> buf;
06047
06048 while ((num = FioReadWord()) != 0) {
06049 byte type = FioReadByte();
06050 _nfo_line++;
06051
06052 if (type == 0xFF) {
06053 if (_skip_sprites == 0) {
06054 DecodeSpecialSprite(buf.Allocate(num), num, stage);
06055
06056
06057 if (_skip_sprites == -1) break;
06058
06059 continue;
06060 } else {
06061 FioSkipBytes(num);
06062 }
06063 } else {
06064 if (_skip_sprites == 0) {
06065 grfmsg(0, "LoadNewGRFFile: Unexpected sprite, disabling");
06066 config->status = GCS_DISABLED;
06067 config->error = CallocT<GRFError>(1);
06068 config->error->severity = STR_NEWGRF_ERROR_MSG_FATAL;
06069 config->error->message = STR_NEWGRF_ERROR_UNEXPECTED_SPRITE;
06070 break;
06071 }
06072
06073 FioSkipBytes(7);
06074 SkipSpriteData(type, num - 8);
06075 }
06076
06077 if (_skip_sprites > 0) _skip_sprites--;
06078 }
06079 }
06080
06088 static void ActivateOldShore()
06089 {
06090
06091
06092 if (_loaded_newgrf_features.shore == SHORE_REPLACE_NONE) _loaded_newgrf_features.shore = SHORE_REPLACE_ACTION_A;
06093
06094 if (_loaded_newgrf_features.shore != SHORE_REPLACE_ACTION_5) {
06095 DupSprite(SPR_ORIGINALSHORE_START + 1, SPR_SHORE_BASE + 1);
06096 DupSprite(SPR_ORIGINALSHORE_START + 2, SPR_SHORE_BASE + 2);
06097 DupSprite(SPR_ORIGINALSHORE_START + 6, SPR_SHORE_BASE + 3);
06098 DupSprite(SPR_ORIGINALSHORE_START , SPR_SHORE_BASE + 4);
06099 DupSprite(SPR_ORIGINALSHORE_START + 4, SPR_SHORE_BASE + 6);
06100 DupSprite(SPR_ORIGINALSHORE_START + 3, SPR_SHORE_BASE + 8);
06101 DupSprite(SPR_ORIGINALSHORE_START + 7, SPR_SHORE_BASE + 9);
06102 DupSprite(SPR_ORIGINALSHORE_START + 5, SPR_SHORE_BASE + 12);
06103 }
06104
06105 if (_loaded_newgrf_features.shore == SHORE_REPLACE_ACTION_A) {
06106 DupSprite(SPR_FLAT_GRASS_TILE + 16, SPR_SHORE_BASE + 0);
06107 DupSprite(SPR_FLAT_GRASS_TILE + 17, SPR_SHORE_BASE + 5);
06108 DupSprite(SPR_FLAT_GRASS_TILE + 7, SPR_SHORE_BASE + 7);
06109 DupSprite(SPR_FLAT_GRASS_TILE + 15, SPR_SHORE_BASE + 10);
06110 DupSprite(SPR_FLAT_GRASS_TILE + 11, SPR_SHORE_BASE + 11);
06111 DupSprite(SPR_FLAT_GRASS_TILE + 13, SPR_SHORE_BASE + 13);
06112 DupSprite(SPR_FLAT_GRASS_TILE + 14, SPR_SHORE_BASE + 14);
06113 DupSprite(SPR_FLAT_GRASS_TILE + 18, SPR_SHORE_BASE + 15);
06114
06115
06116
06117 DupSprite(SPR_FLAT_GRASS_TILE + 5, SPR_SHORE_BASE + 16);
06118 DupSprite(SPR_FLAT_GRASS_TILE + 10, SPR_SHORE_BASE + 17);
06119 }
06120 }
06121
06122 void InitDepotWindowBlockSizes();
06123
06124 extern void InitGRFTownGeneratorNames();
06125
06126 static void AfterLoadGRFs()
06127 {
06128 for (StringIDToGRFIDMapping::iterator it = _string_to_grf_mapping.begin(); it != _string_to_grf_mapping.end(); it++) {
06129 *((*it).first) = MapGRFStringID((*it).second, *((*it).first));
06130 }
06131 _string_to_grf_mapping.clear();
06132
06133
06134 for (GRFLineToSpriteOverride::iterator it = _grf_line_to_action6_sprite_override.begin(); it != _grf_line_to_action6_sprite_override.end(); it++) {
06135 free((*it).second);
06136 }
06137 _grf_line_to_action6_sprite_override.clear();
06138
06139
06140 CalculateRefitMasks();
06141
06142
06143 InitDepotWindowBlockSizes();
06144
06145
06146 FinaliseHouseArray();
06147
06148
06149 FinaliseIndustriesArray();
06150
06151
06152 BuildIndustriesLegend();
06153
06154
06155 InitGRFTownGeneratorNames();
06156
06157
06158 CommitVehicleListOrderChanges();
06159
06160
06161 ActivateOldShore();
06162
06163 Engine *e;
06164 FOR_ALL_ENGINES_OF_TYPE(e, VEH_ROAD) {
06165 if (_gted[e->index].rv_max_speed != 0) {
06166
06167 e->u.road.max_speed = _gted[e->index].rv_max_speed * 4;
06168 }
06169 }
06170
06171 SetYearEngineAgingStops();
06172
06173
06174 free(_gted);
06175 _grm_sprites.clear();
06176 }
06177
06178 void LoadNewGRF(uint load_index, uint file_index)
06179 {
06180
06181
06182
06183
06184 Date date = _date;
06185 Year year = _cur_year;
06186 DateFract date_fract = _date_fract;
06187 uint16 tick_counter = _tick_counter;
06188 byte display_opt = _display_opt;
06189
06190 if (_networking) {
06191 _cur_year = _settings_game.game_creation.starting_year;
06192 _date = ConvertYMDToDate(_cur_year, 0, 1);
06193 _date_fract = 0;
06194 _tick_counter = 0;
06195 _display_opt = 0;
06196 }
06197
06198 InitializeGRFSpecial();
06199
06200 ResetNewGRFData();
06201
06202
06203
06204
06205
06206
06207
06208
06209 for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
06210 if (c->status != GCS_NOT_FOUND) c->status = GCS_UNKNOWN;
06211 }
06212
06213 _cur_spriteid = load_index;
06214
06215
06216
06217
06218 for (GrfLoadingStage stage = GLS_LABELSCAN; stage <= GLS_ACTIVATION; stage++) {
06219
06220
06221 for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
06222 if (c->status == GCS_ACTIVATED) c->status = GCS_INITIALISED;
06223 }
06224
06225 uint slot = file_index;
06226
06227 _cur_stage = stage;
06228 for (GRFConfig *c = _grfconfig; c != NULL; c = c->next) {
06229 if (c->status == GCS_DISABLED || c->status == GCS_NOT_FOUND) continue;
06230 if (stage > GLS_INIT && HasBit(c->flags, GCF_INIT_ONLY)) continue;
06231
06232
06233 if (!FioCheckFileExists(c->filename)) usererror("NewGRF file is missing '%s'", c->filename);
06234
06235 if (stage == GLS_LABELSCAN) InitNewGRFFile(c, _cur_spriteid);
06236 LoadNewGRFFile(c, slot++, stage);
06237 if (stage == GLS_RESERVE) {
06238 SetBit(c->flags, GCF_RESERVED);
06239 } else if (stage == GLS_ACTIVATION) {
06240 ClrBit(c->flags, GCF_RESERVED);
06241 assert(GetFileByGRFID(c->grfid) == _cur_grffile);
06242 ClearTemporaryNewGRFData(_cur_grffile);
06243 BuildCargoTranslationMap();
06244 DEBUG(sprite, 2, "LoadNewGRF: Currently %i sprites are loaded", _cur_spriteid);
06245 } else if (stage == GLS_INIT && HasBit(c->flags, GCF_INIT_ONLY)) {
06246
06247 ClearTemporaryNewGRFData(_cur_grffile);
06248 }
06249 }
06250 }
06251
06252
06253 AfterLoadGRFs();
06254
06255
06256 _cur_year = year;
06257 _date = date;
06258 _date_fract = date_fract;
06259 _tick_counter = tick_counter;
06260 _display_opt = display_opt;
06261 }
06262
06263 bool HasGrfMiscBit(GrfMiscBit bit)
06264 {
06265 return HasBit(_misc_grf_features, bit);
06266 }