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