From 74bd064d107a6619549e74e8d976c3220e7d2a64 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Tue, 14 Jan 2025 21:22:52 +0000 Subject: [PATCH] Codechange: Pass first and last ID range instead of first and count. (#13319) This allows range checks and loops to be slightly simplified. --- src/newgrf.cpp | 462 ++++++++++++++++++++++++------------------------- 1 file changed, 231 insertions(+), 231 deletions(-) diff --git a/src/newgrf.cpp b/src/newgrf.cpp index 7d69676b19..9ace4ba853 100644 --- a/src/newgrf.cpp +++ b/src/newgrf.cpp @@ -1005,7 +1005,7 @@ enum ChangeInfoResult { CIR_INVALID_ID, ///< Attempt to modify an invalid ID }; -typedef ChangeInfoResult (*VCI_Handler)(uint engine, int numinfo, int prop, ByteReader &buf); +using ChangeInfoHandler = ChangeInfoResult(uint first, uint last, int prop, ByteReader &buf); /** * Define properties common to all vehicles @@ -1051,18 +1051,18 @@ static ChangeInfoResult CommonVehicleChangeInfo(EngineInfo *ei, int prop, ByteRe /** * Define properties for rail vehicles - * @param engine :ocal ID of the first vehicle. - * @param numinfo Number of subsequent IDs to change the property for. + * @param first Local ID of the first vehicle. + * @param last Local ID of the last vehicle. * @param prop The property to change. * @param buf The property value. * @return ChangeInfoResult. */ -static ChangeInfoResult RailVehicleChangeInfo(uint engine, int numinfo, int prop, ByteReader &buf) +static ChangeInfoResult RailVehicleChangeInfo(uint first, uint last, int prop, ByteReader &buf) { ChangeInfoResult ret = CIR_SUCCESS; - for (int i = 0; i < numinfo; i++) { - Engine *e = GetNewEngine(_cur.grffile, VEH_TRAIN, engine + i); + for (uint id = first; id < last; ++id) { + Engine *e = GetNewEngine(_cur.grffile, VEH_TRAIN, id); if (e == nullptr) return CIR_INVALID_ID; // No engine could be allocated, so neither can any next vehicles EngineInfo *ei = &e->info; @@ -1359,18 +1359,18 @@ static ChangeInfoResult RailVehicleChangeInfo(uint engine, int numinfo, int prop /** * Define properties for road vehicles - * @param engine Local ID of the first vehicle. - * @param numinfo Number of subsequent IDs to change the property for. + * @param first Local ID of the first vehicle. + * @param last Local ID of the last vehicle. * @param prop The property to change. * @param buf The property value. * @return ChangeInfoResult. */ -static ChangeInfoResult RoadVehicleChangeInfo(uint engine, int numinfo, int prop, ByteReader &buf) +static ChangeInfoResult RoadVehicleChangeInfo(uint first, uint last, int prop, ByteReader &buf) { ChangeInfoResult ret = CIR_SUCCESS; - for (int i = 0; i < numinfo; i++) { - Engine *e = GetNewEngine(_cur.grffile, VEH_ROAD, engine + i); + for (uint id = first; id < last; ++id) { + Engine *e = GetNewEngine(_cur.grffile, VEH_ROAD, id); if (e == nullptr) return CIR_INVALID_ID; // No engine could be allocated, so neither can any next vehicles EngineInfo *ei = &e->info; @@ -1564,18 +1564,18 @@ static ChangeInfoResult RoadVehicleChangeInfo(uint engine, int numinfo, int prop /** * Define properties for ships - * @param engine Local ID of the first vehicle. - * @param numinfo Number of subsequent IDs to change the property for. + * @param first Local ID of the first vehicle. + * @param last Local ID of the last vehicle. * @param prop The property to change. * @param buf The property value. * @return ChangeInfoResult. */ -static ChangeInfoResult ShipVehicleChangeInfo(uint engine, int numinfo, int prop, ByteReader &buf) +static ChangeInfoResult ShipVehicleChangeInfo(uint first, uint last, int prop, ByteReader &buf) { ChangeInfoResult ret = CIR_SUCCESS; - for (int i = 0; i < numinfo; i++) { - Engine *e = GetNewEngine(_cur.grffile, VEH_SHIP, engine + i); + for (uint id = first; id < last; ++id) { + Engine *e = GetNewEngine(_cur.grffile, VEH_SHIP, id); if (e == nullptr) return CIR_INVALID_ID; // No engine could be allocated, so neither can any next vehicles EngineInfo *ei = &e->info; @@ -1755,18 +1755,18 @@ static ChangeInfoResult ShipVehicleChangeInfo(uint engine, int numinfo, int prop /** * Define properties for aircraft - * @param engine Local ID of the aircraft. - * @param numinfo Number of subsequent IDs to change the property for. + * @param first Local ID of the first vehicle. + * @param last Local ID of the last vehicle. * @param prop The property to change. * @param buf The property value. * @return ChangeInfoResult. */ -static ChangeInfoResult AircraftVehicleChangeInfo(uint engine, int numinfo, int prop, ByteReader &buf) +static ChangeInfoResult AircraftVehicleChangeInfo(uint first, uint last, int prop, ByteReader &buf) { ChangeInfoResult ret = CIR_SUCCESS; - for (int i = 0; i < numinfo; i++) { - Engine *e = GetNewEngine(_cur.grffile, VEH_AIRCRAFT, engine + i); + for (uint id = first; id < last; ++id) { + Engine *e = GetNewEngine(_cur.grffile, VEH_AIRCRAFT, id); if (e == nullptr) return CIR_INVALID_ID; // No engine could be allocated, so neither can any next vehicles EngineInfo *ei = &e->info; @@ -1924,30 +1924,30 @@ static ChangeInfoResult AircraftVehicleChangeInfo(uint engine, int numinfo, int /** * Define properties for stations - * @param stid StationID of the first station tile. - * @param numinfo Number of subsequent station tiles to change the property for. + * @param first Local ID of the first station. + * @param last Local ID of the last station. * @param prop The property to change. * @param buf The property value. * @return ChangeInfoResult. */ -static ChangeInfoResult StationChangeInfo(uint stid, int numinfo, int prop, ByteReader &buf) +static ChangeInfoResult StationChangeInfo(uint first, uint last, int prop, ByteReader &buf) { ChangeInfoResult ret = CIR_SUCCESS; - if (stid + numinfo > NUM_STATIONS_PER_GRF) { - GrfMsg(1, "StationChangeInfo: Station {} is invalid, max {}, ignoring", stid + numinfo, NUM_STATIONS_PER_GRF); + if (last > NUM_STATIONS_PER_GRF) { + GrfMsg(1, "StationChangeInfo: Station {} is invalid, max {}, ignoring", last, NUM_STATIONS_PER_GRF); return CIR_INVALID_ID; } /* Allocate station specs if necessary */ - if (_cur.grffile->stations.size() < stid + numinfo) _cur.grffile->stations.resize(stid + numinfo); + if (_cur.grffile->stations.size() < last) _cur.grffile->stations.resize(last); - for (int i = 0; i < numinfo; i++) { - auto &statspec = _cur.grffile->stations[stid + i]; + for (uint id = first; id < last; ++id) { + auto &statspec = _cur.grffile->stations[id]; /* Check that the station we are modifying is defined. */ if (statspec == nullptr && prop != 0x08) { - GrfMsg(2, "StationChangeInfo: Attempt to modify undefined station {}, ignoring", stid + i); + GrfMsg(2, "StationChangeInfo: Attempt to modify undefined station {}, ignoring", id); return CIR_INVALID_ID; } @@ -2008,7 +2008,7 @@ static ChangeInfoResult StationChangeInfo(uint stid, int numinfo, int prop, Byte /* Number of layouts must be even, alternating X and Y */ if (statspec->renderdata.size() & 1) { - GrfMsg(1, "StationChangeInfo: Station {} defines an odd number of sprite layouts, dropping the last item", stid + i); + GrfMsg(1, "StationChangeInfo: Station {} defines an odd number of sprite layouts, dropping the last item", id); statspec->renderdata.pop_back(); } break; @@ -2019,7 +2019,7 @@ static ChangeInfoResult StationChangeInfo(uint stid, int numinfo, int prop, Byte const StationSpec *srcstatspec = srcid >= _cur.grffile->stations.size() ? nullptr : _cur.grffile->stations[srcid].get(); if (srcstatspec == nullptr) { - GrfMsg(1, "StationChangeInfo: Station {} is not defined, cannot copy sprite layout to {}.", srcid, stid + i); + GrfMsg(1, "StationChangeInfo: Station {} is not defined, cannot copy sprite layout to {}.", srcid, id); continue; } @@ -2073,7 +2073,7 @@ static ChangeInfoResult StationChangeInfo(uint stid, int numinfo, int prop, Byte const StationSpec *srcstatspec = srcid >= _cur.grffile->stations.size() ? nullptr : _cur.grffile->stations[srcid].get(); if (srcstatspec == nullptr) { - GrfMsg(1, "StationChangeInfo: Station {} is not defined, cannot copy tile layout to {}.", srcid, stid + i); + GrfMsg(1, "StationChangeInfo: Station {} is not defined, cannot copy tile layout to {}.", srcid, id); continue; } @@ -2165,7 +2165,7 @@ static ChangeInfoResult StationChangeInfo(uint stid, int numinfo, int prop, Byte /* Number of layouts must be even, alternating X and Y */ if (statspec->renderdata.size() & 1) { - GrfMsg(1, "StationChangeInfo: Station {} defines an odd number of sprite layouts, dropping the last item", stid + i); + GrfMsg(1, "StationChangeInfo: Station {} defines an odd number of sprite layouts, dropping the last item", id); statspec->renderdata.pop_back(); } break; @@ -2204,23 +2204,23 @@ static ChangeInfoResult StationChangeInfo(uint stid, int numinfo, int prop, Byte /** * Define properties for water features - * @param id Type of the first water feature. - * @param numinfo Number of subsequent water feature ids to change the property for. + * @param first Local ID of the first water feature. + * @param last Local ID of the last water feature. * @param prop The property to change. * @param buf The property value. * @return ChangeInfoResult. */ -static ChangeInfoResult CanalChangeInfo(uint id, int numinfo, int prop, ByteReader &buf) +static ChangeInfoResult CanalChangeInfo(uint first, uint last, int prop, ByteReader &buf) { ChangeInfoResult ret = CIR_SUCCESS; - if (id + numinfo > CF_END) { - GrfMsg(1, "CanalChangeInfo: Canal feature 0x{:02X} is invalid, max {}, ignoring", id + numinfo, CF_END); + if (last > CF_END) { + GrfMsg(1, "CanalChangeInfo: Canal feature 0x{:02X} is invalid, max {}, ignoring", last, CF_END); return CIR_INVALID_ID; } - for (int i = 0; i < numinfo; i++) { - CanalProperties *cp = &_cur.grffile->canal_local_properties[id + i]; + for (uint id = first; id < last; ++id) { + CanalProperties *cp = &_cur.grffile->canal_local_properties[id]; switch (prop) { case 0x08: @@ -2242,23 +2242,23 @@ static ChangeInfoResult CanalChangeInfo(uint id, int numinfo, int prop, ByteRead /** * Define properties for bridges - * @param brid BridgeID of the bridge. - * @param numinfo Number of subsequent bridgeIDs to change the property for. + * @param first Local ID of the first bridge. + * @param last Local ID of the last bridge. * @param prop The property to change. * @param buf The property value. * @return ChangeInfoResult. */ -static ChangeInfoResult BridgeChangeInfo(uint brid, int numinfo, int prop, ByteReader &buf) +static ChangeInfoResult BridgeChangeInfo(uint first, uint last, int prop, ByteReader &buf) { ChangeInfoResult ret = CIR_SUCCESS; - if (brid + numinfo > MAX_BRIDGES) { - GrfMsg(1, "BridgeChangeInfo: Bridge {} is invalid, max {}, ignoring", brid + numinfo, MAX_BRIDGES); + if (last > MAX_BRIDGES) { + GrfMsg(1, "BridgeChangeInfo: Bridge {} is invalid, max {}, ignoring", last, MAX_BRIDGES); return CIR_INVALID_ID; } - for (int i = 0; i < numinfo; i++) { - BridgeSpec *bridge = &_bridge[brid + i]; + for (uint id = first; id < last; ++id) { + BridgeSpec *bridge = &_bridge[id]; switch (prop) { case 0x08: { // Year of availability @@ -2419,26 +2419,26 @@ static ChangeInfoResult IgnoreTownHouseProperty(int prop, ByteReader &buf) /** * Define properties for houses - * @param hid HouseID of the house. - * @param numinfo Number of subsequent houseIDs to change the property for. + * @param first Local ID of the first house. + * @param last Local ID of the last house. * @param prop The property to change. * @param buf The property value. * @return ChangeInfoResult. */ -static ChangeInfoResult TownHouseChangeInfo(uint hid, int numinfo, int prop, ByteReader &buf) +static ChangeInfoResult TownHouseChangeInfo(uint first, uint last, int prop, ByteReader &buf) { ChangeInfoResult ret = CIR_SUCCESS; - if (hid + numinfo > NUM_HOUSES_PER_GRF) { - GrfMsg(1, "TownHouseChangeInfo: Too many houses loaded ({}), max ({}). Ignoring.", hid + numinfo, NUM_HOUSES_PER_GRF); + if (last > NUM_HOUSES_PER_GRF) { + GrfMsg(1, "TownHouseChangeInfo: Too many houses loaded ({}), max ({}). Ignoring.", last, NUM_HOUSES_PER_GRF); return CIR_INVALID_ID; } /* Allocate house specs if they haven't been allocated already. */ - if (_cur.grffile->housespec.size() < hid + numinfo) _cur.grffile->housespec.resize(hid + numinfo); + if (_cur.grffile->housespec.size() < last) _cur.grffile->housespec.resize(last); - for (int i = 0; i < numinfo; i++) { - auto &housespec = _cur.grffile->housespec[hid + i]; + for (uint id = first; id < last; ++id) { + auto &housespec = _cur.grffile->housespec[id]; if (prop != 0x08 && housespec == nullptr) { /* If the house property 08 is not yet set, ignore this property */ @@ -2453,11 +2453,11 @@ static ChangeInfoResult TownHouseChangeInfo(uint hid, int numinfo, int prop, Byt if (subs_id == 0xFF) { /* Instead of defining a new house, a substitute house id * of 0xFF disables the old house with the current id. */ - if (hid + i < NEW_HOUSE_OFFSET) HouseSpec::Get(hid + i)->enabled = false; + if (id < NEW_HOUSE_OFFSET) HouseSpec::Get(id)->enabled = false; continue; } else if (subs_id >= NEW_HOUSE_OFFSET) { /* The substitute id must be one of the original houses. */ - GrfMsg(2, "TownHouseChangeInfo: Attempt to use new house {} as substitute house for {}. Ignoring.", subs_id, hid + i); + GrfMsg(2, "TownHouseChangeInfo: Attempt to use new house {} as substitute house for {}. Ignoring.", subs_id, id); continue; } @@ -2467,7 +2467,7 @@ static ChangeInfoResult TownHouseChangeInfo(uint hid, int numinfo, int prop, Byt housespec = std::make_unique(*HouseSpec::Get(subs_id)); housespec->enabled = true; - housespec->grf_prop.local_id = hid + i; + housespec->grf_prop.local_id = id; housespec->grf_prop.subst_id = subs_id; housespec->grf_prop.grfid = _cur.grffile->grfid; housespec->grf_prop.grffile = _cur.grffile; @@ -2559,11 +2559,11 @@ static ChangeInfoResult TownHouseChangeInfo(uint hid, int numinfo, int prop, Byt /* The house being overridden must be an original house. */ if (override >= NEW_HOUSE_OFFSET) { - GrfMsg(2, "TownHouseChangeInfo: Attempt to override new house {} with house id {}. Ignoring.", override, hid + i); + GrfMsg(2, "TownHouseChangeInfo: Attempt to override new house {} with house id {}. Ignoring.", override, id); continue; } - _house_mngr.Add(hid + i, _cur.grffile->grfid, override); + _house_mngr.Add(id, _cur.grffile->grfid, override); break; } @@ -2697,25 +2697,25 @@ static ChangeInfoResult TownHouseChangeInfo(uint hid, int numinfo, int prop, Byt /** * Load a cargo- or railtype-translation table. - * @param gvid ID of the global variable. This is basically only checked for zerones. - * @param numinfo Number of subsequent IDs to change the property for. + * @param first ID of the first translation table entry. + * @param last ID of the last translation table entry. * @param buf The property value. * @param gettable Function to get storage for the translation table. * @param name Name of the table for debug output. * @return ChangeInfoResult. */ template -static ChangeInfoResult LoadTranslationTable(uint gvid, int numinfo, ByteReader &buf, TGetTableFunc gettable, std::string_view name) +static ChangeInfoResult LoadTranslationTable(uint first, uint last, ByteReader &buf, TGetTableFunc gettable, std::string_view name) { - if (gvid != 0) { + if (first != 0) { GrfMsg(1, "LoadTranslationTable: {} translation table must start at zero", name); return CIR_INVALID_ID; } std::vector &translation_table = gettable(*_cur.grffile); translation_table.clear(); - translation_table.reserve(numinfo); - for (int i = 0; i < numinfo; i++) { + translation_table.reserve(last); + for (uint id = first; id < last; ++id) { translation_table.push_back(T(BSWAP32(buf.ReadDWord()))); } @@ -2745,27 +2745,27 @@ static std::string ReadDWordAsString(ByteReader &reader) /** * Define properties for global variables - * @param gvid ID of the global variable. - * @param numinfo Number of subsequent IDs to change the property for. + * @param first ID of the first global var. + * @param last ID of the last global var. * @param prop The property to change. * @param buf The property value. * @return ChangeInfoResult. */ -static ChangeInfoResult GlobalVarChangeInfo(uint gvid, int numinfo, int prop, ByteReader &buf) +static ChangeInfoResult GlobalVarChangeInfo(uint first, uint last, int prop, ByteReader &buf) { /* Properties which are handled as a whole */ switch (prop) { case 0x09: // Cargo Translation Table; loading during both reservation and activation stage (in case it is selected depending on defined cargos) - return LoadTranslationTable(gvid, numinfo, buf, [](GRFFile &grf) -> std::vector & { return grf.cargo_list; }, "Cargo"); + return LoadTranslationTable(first, last, buf, [](GRFFile &grf) -> std::vector & { return grf.cargo_list; }, "Cargo"); case 0x12: // Rail type translation table; loading during both reservation and activation stage (in case it is selected depending on defined railtypes) - return LoadTranslationTable(gvid, numinfo, buf, [](GRFFile &grf) -> std::vector & { return grf.railtype_list; }, "Rail type"); + return LoadTranslationTable(first, last, buf, [](GRFFile &grf) -> std::vector & { return grf.railtype_list; }, "Rail type"); case 0x16: // Road type translation table; loading during both reservation and activation stage (in case it is selected depending on defined roadtypes) - return LoadTranslationTable(gvid, numinfo, buf, [](GRFFile &grf) -> std::vector & { return grf.roadtype_list; }, "Road type"); + return LoadTranslationTable(first, last, buf, [](GRFFile &grf) -> std::vector & { return grf.roadtype_list; }, "Road type"); case 0x17: // Tram type translation table; loading during both reservation and activation stage (in case it is selected depending on defined tramtypes) - return LoadTranslationTable(gvid, numinfo, buf, [](GRFFile &grf) -> std::vector & { return grf.tramtype_list; }, "Tram type"); + return LoadTranslationTable(first, last, buf, [](GRFFile &grf) -> std::vector & { return grf.tramtype_list; }, "Tram type"); default: break; @@ -2773,22 +2773,21 @@ static ChangeInfoResult GlobalVarChangeInfo(uint gvid, int numinfo, int prop, By /* Properties which are handled per item */ ChangeInfoResult ret = CIR_SUCCESS; - for (int i = 0; i < numinfo; i++) { + for (uint id = first; id < last; ++id) { switch (prop) { case 0x08: { // Cost base factor int factor = buf.ReadByte(); - uint price = gvid + i; - if (price < PR_END) { - _cur.grffile->price_base_multipliers[price] = std::min(factor - 8, MAX_PRICE_MODIFIER); + if (id < PR_END) { + _cur.grffile->price_base_multipliers[id] = std::min(factor - 8, MAX_PRICE_MODIFIER); } else { - GrfMsg(1, "GlobalVarChangeInfo: Price {} out of range, ignoring", price); + GrfMsg(1, "GlobalVarChangeInfo: Price {} out of range, ignoring", id); } break; } case 0x0A: { // Currency display names - uint curidx = GetNewgrfCurrencyIdConverted(gvid + i); + uint curidx = GetNewgrfCurrencyIdConverted(id); if (curidx < CURRENCY_END) { AddStringForMapping(GRFStringID{buf.ReadWord()}, [curidx](StringID str) { _currency_specs[curidx].name = str; @@ -2801,7 +2800,7 @@ static ChangeInfoResult GlobalVarChangeInfo(uint gvid, int numinfo, int prop, By } case 0x0B: { // Currency multipliers - uint curidx = GetNewgrfCurrencyIdConverted(gvid + i); + uint curidx = GetNewgrfCurrencyIdConverted(id); uint32_t rate = buf.ReadDWord(); if (curidx < CURRENCY_END) { @@ -2816,7 +2815,7 @@ static ChangeInfoResult GlobalVarChangeInfo(uint gvid, int numinfo, int prop, By } case 0x0C: { // Currency options - uint curidx = GetNewgrfCurrencyIdConverted(gvid + i); + uint curidx = GetNewgrfCurrencyIdConverted(id); uint16_t options = buf.ReadWord(); if (curidx < CURRENCY_END) { @@ -2832,7 +2831,7 @@ static ChangeInfoResult GlobalVarChangeInfo(uint gvid, int numinfo, int prop, By } case 0x0D: { // Currency prefix symbol - uint curidx = GetNewgrfCurrencyIdConverted(gvid + i); + uint curidx = GetNewgrfCurrencyIdConverted(id); std::string prefix = ReadDWordAsString(buf); if (curidx < CURRENCY_END) { @@ -2844,7 +2843,7 @@ static ChangeInfoResult GlobalVarChangeInfo(uint gvid, int numinfo, int prop, By } case 0x0E: { // Currency suffix symbol - uint curidx = GetNewgrfCurrencyIdConverted(gvid + i); + uint curidx = GetNewgrfCurrencyIdConverted(id); std::string suffix = ReadDWordAsString(buf); if (curidx < CURRENCY_END) { @@ -2856,7 +2855,7 @@ static ChangeInfoResult GlobalVarChangeInfo(uint gvid, int numinfo, int prop, By } case 0x0F: { // Euro introduction dates - uint curidx = GetNewgrfCurrencyIdConverted(gvid + i); + uint curidx = GetNewgrfCurrencyIdConverted(id); TimerGameCalendar::Year year_euro{buf.ReadWord()}; if (curidx < CURRENCY_END) { @@ -2868,8 +2867,8 @@ static ChangeInfoResult GlobalVarChangeInfo(uint gvid, int numinfo, int prop, By } case 0x10: // Snow line height table - if (numinfo > 1 || IsSnowLineSet()) { - GrfMsg(1, "GlobalVarChangeInfo: The snowline can only be set once ({})", numinfo); + if (last > 1 || IsSnowLineSet()) { + GrfMsg(1, "GlobalVarChangeInfo: The snowline can only be set once ({})", last); } else if (buf.Remaining() < SNOW_LINE_MONTHS * SNOW_LINE_DAYS) { GrfMsg(1, "GlobalVarChangeInfo: Not enough entries set in the snowline table ({})", buf.Remaining()); } else { @@ -2907,7 +2906,7 @@ static ChangeInfoResult GlobalVarChangeInfo(uint gvid, int numinfo, int prop, By case 0x13: // Gender translation table case 0x14: // Case translation table case 0x15: { // Plural form translation - uint curidx = gvid + i; // The current index, i.e. language. + uint curidx = id; // The current index, i.e. language. const LanguageMetadata *lang = curidx < MAX_LANG ? GetLanguage(curidx) : nullptr; if (lang == nullptr) { GrfMsg(1, "GlobalVarChangeInfo: Language {} is not known, ignoring", curidx); @@ -2975,21 +2974,21 @@ static ChangeInfoResult GlobalVarChangeInfo(uint gvid, int numinfo, int prop, By return ret; } -static ChangeInfoResult GlobalVarReserveInfo(uint gvid, int numinfo, int prop, ByteReader &buf) +static ChangeInfoResult GlobalVarReserveInfo(uint first, uint last, int prop, ByteReader &buf) { /* Properties which are handled as a whole */ switch (prop) { case 0x09: // Cargo Translation Table; loading during both reservation and activation stage (in case it is selected depending on defined cargos) - return LoadTranslationTable(gvid, numinfo, buf, [](GRFFile &grf) -> std::vector & { return grf.cargo_list; }, "Cargo"); + return LoadTranslationTable(first, last, buf, [](GRFFile &grf) -> std::vector & { return grf.cargo_list; }, "Cargo"); case 0x12: // Rail type translation table; loading during both reservation and activation stage (in case it is selected depending on defined railtypes) - return LoadTranslationTable(gvid, numinfo, buf, [](GRFFile &grf) -> std::vector & { return grf.railtype_list; }, "Rail type"); + return LoadTranslationTable(first, last, buf, [](GRFFile &grf) -> std::vector & { return grf.railtype_list; }, "Rail type"); case 0x16: // Road type translation table; loading during both reservation and activation stage (in case it is selected depending on defined roadtypes) - return LoadTranslationTable(gvid, numinfo, buf, [](GRFFile &grf) -> std::vector & { return grf.roadtype_list; }, "Road type"); + return LoadTranslationTable(first, last, buf, [](GRFFile &grf) -> std::vector & { return grf.roadtype_list; }, "Road type"); case 0x17: // Tram type translation table; loading during both reservation and activation stage (in case it is selected depending on defined tramtypes) - return LoadTranslationTable(gvid, numinfo, buf, [](GRFFile &grf) -> std::vector & { return grf.tramtype_list; }, "Tram type"); + return LoadTranslationTable(first, last, buf, [](GRFFile &grf) -> std::vector & { return grf.tramtype_list; }, "Tram type"); default: break; @@ -2997,7 +2996,8 @@ static ChangeInfoResult GlobalVarReserveInfo(uint gvid, int numinfo, int prop, B /* Properties which are handled per item */ ChangeInfoResult ret = CIR_SUCCESS; - for (int i = 0; i < numinfo; i++) { + + for (uint id = first; id < last; ++id) { switch (prop) { case 0x08: // Cost base factor case 0x15: // Plural form translation @@ -3046,32 +3046,32 @@ static ChangeInfoResult GlobalVarReserveInfo(uint gvid, int numinfo, int prop, B /** * Define properties for cargoes - * @param cid Local ID of the cargo. - * @param numinfo Number of subsequent IDs to change the property for. + * @param first ID of the first cargo. + * @param last ID of the last cargo. * @param prop The property to change. * @param buf The property value. * @return ChangeInfoResult. */ -static ChangeInfoResult CargoChangeInfo(uint cid, int numinfo, int prop, ByteReader &buf) +static ChangeInfoResult CargoChangeInfo(uint first, uint last, int prop, ByteReader &buf) { ChangeInfoResult ret = CIR_SUCCESS; - if (cid + numinfo > NUM_CARGO) { - GrfMsg(2, "CargoChangeInfo: Cargo type {} out of range (max {})", cid + numinfo, NUM_CARGO - 1); + if (last > NUM_CARGO) { + GrfMsg(2, "CargoChangeInfo: Cargo type {} out of range (max {})", last, NUM_CARGO - 1); return CIR_INVALID_ID; } - for (int i = 0; i < numinfo; i++) { - CargoSpec *cs = CargoSpec::Get(cid + i); + for (uint id = first; id < last; ++id) { + CargoSpec *cs = CargoSpec::Get(id); switch (prop) { case 0x08: // Bit number of cargo cs->bitnum = buf.ReadByte(); if (cs->IsValid()) { cs->grffile = _cur.grffile; - SetBit(_cargo_mask, cid + i); + SetBit(_cargo_mask, id); } else { - ClrBit(_cargo_mask, cid + i); + ClrBit(_cargo_mask, id); } BuildCargoLabelMap(); break; @@ -3204,13 +3204,13 @@ static ChangeInfoResult CargoChangeInfo(uint cid, int numinfo, int prop, ByteRea /** * Define properties for sound effects - * @param sid Local ID of the sound. - * @param numinfo Number of subsequent IDs to change the property for. + * @param first Local ID of the first sound. + * @param last Local ID of the last sound. * @param prop The property to change. * @param buf The property value. * @return ChangeInfoResult. */ -static ChangeInfoResult SoundEffectChangeInfo(uint sid, int numinfo, int prop, ByteReader &buf) +static ChangeInfoResult SoundEffectChangeInfo(uint first, uint last, int prop, ByteReader &buf) { ChangeInfoResult ret = CIR_SUCCESS; @@ -3219,13 +3219,13 @@ static ChangeInfoResult SoundEffectChangeInfo(uint sid, int numinfo, int prop, B return CIR_INVALID_ID; } - if (sid + numinfo - ORIGINAL_SAMPLE_COUNT > _cur.grffile->num_sounds) { - GrfMsg(1, "SoundEffectChangeInfo: Attempting to change undefined sound effect ({}), max ({}). Ignoring.", sid + numinfo, ORIGINAL_SAMPLE_COUNT + _cur.grffile->num_sounds); + if (last - ORIGINAL_SAMPLE_COUNT > _cur.grffile->num_sounds) { + GrfMsg(1, "SoundEffectChangeInfo: Attempting to change undefined sound effect ({}), max ({}). Ignoring.", last, ORIGINAL_SAMPLE_COUNT + _cur.grffile->num_sounds); return CIR_INVALID_ID; } - for (int i = 0; i < numinfo; i++) { - SoundEntry *sound = GetSound(sid + i + _cur.grffile->sound_offset - ORIGINAL_SAMPLE_COUNT); + for (uint id = first; id < last; ++id) { + SoundEntry *sound = GetSound(first + _cur.grffile->sound_offset - ORIGINAL_SAMPLE_COUNT); switch (prop) { case 0x08: // Relative volume @@ -3299,26 +3299,26 @@ static ChangeInfoResult IgnoreIndustryTileProperty(int prop, ByteReader &buf) /** * Define properties for industry tiles - * @param indtid Local ID of the industry tile. - * @param numinfo Number of subsequent industry tile IDs to change the property for. + * @param first Local ID of the first industry tile. + * @param last Local ID of the last industry tile. * @param prop The property to change. * @param buf The property value. * @return ChangeInfoResult. */ -static ChangeInfoResult IndustrytilesChangeInfo(uint indtid, int numinfo, int prop, ByteReader &buf) +static ChangeInfoResult IndustrytilesChangeInfo(uint first, uint last, int prop, ByteReader &buf) { ChangeInfoResult ret = CIR_SUCCESS; - if (indtid + numinfo > NUM_INDUSTRYTILES_PER_GRF) { - GrfMsg(1, "IndustryTilesChangeInfo: Too many industry tiles loaded ({}), max ({}). Ignoring.", indtid + numinfo, NUM_INDUSTRYTILES_PER_GRF); + if (last > NUM_INDUSTRYTILES_PER_GRF) { + GrfMsg(1, "IndustryTilesChangeInfo: Too many industry tiles loaded ({}), max ({}). Ignoring.", last, NUM_INDUSTRYTILES_PER_GRF); return CIR_INVALID_ID; } /* Allocate industry tile specs if they haven't been allocated already. */ - if (_cur.grffile->indtspec.size() < indtid + numinfo) _cur.grffile->indtspec.resize(indtid + numinfo); + if (_cur.grffile->indtspec.size() < last) _cur.grffile->indtspec.resize(last); - for (int i = 0; i < numinfo; i++) { - auto &tsp = _cur.grffile->indtspec[indtid + i]; + for (uint id = first; id < last; ++id) { + auto &tsp = _cur.grffile->indtspec[id]; if (prop != 0x08 && tsp == nullptr) { ChangeInfoResult cir = IgnoreIndustryTileProperty(prop, buf); @@ -3331,7 +3331,7 @@ static ChangeInfoResult IndustrytilesChangeInfo(uint indtid, int numinfo, int pr uint8_t subs_id = buf.ReadByte(); if (subs_id >= NEW_INDUSTRYTILEOFFSET) { /* The substitute id must be one of the original industry tile. */ - GrfMsg(2, "IndustryTilesChangeInfo: Attempt to use new industry tile {} as substitute industry tile for {}. Ignoring.", subs_id, indtid + i); + GrfMsg(2, "IndustryTilesChangeInfo: Attempt to use new industry tile {} as substitute industry tile for {}. Ignoring.", subs_id, id); continue; } @@ -3347,11 +3347,11 @@ static ChangeInfoResult IndustrytilesChangeInfo(uint indtid, int numinfo, int pr tsp->anim_production = INDUSTRYTILE_NOANIM; tsp->anim_next = INDUSTRYTILE_NOANIM; - tsp->grf_prop.local_id = indtid + i; + tsp->grf_prop.local_id = id; tsp->grf_prop.subst_id = subs_id; tsp->grf_prop.grfid = _cur.grffile->grfid; tsp->grf_prop.grffile = _cur.grffile; - _industile_mngr.AddEntityID(indtid + i, _cur.grffile->grfid, subs_id); // pre-reserve the tile slot + _industile_mngr.AddEntityID(id, _cur.grffile->grfid, subs_id); // pre-reserve the tile slot } break; } @@ -3361,11 +3361,11 @@ static ChangeInfoResult IndustrytilesChangeInfo(uint indtid, int numinfo, int pr /* The industry being overridden must be an original industry. */ if (ovrid >= NEW_INDUSTRYTILEOFFSET) { - GrfMsg(2, "IndustryTilesChangeInfo: Attempt to override new industry tile {} with industry tile id {}. Ignoring.", ovrid, indtid + i); + GrfMsg(2, "IndustryTilesChangeInfo: Attempt to override new industry tile {} with industry tile id {}. Ignoring.", ovrid, id); continue; } - _industile_mngr.Add(indtid + i, _cur.grffile->grfid, ovrid); + _industile_mngr.Add(id, _cur.grffile->grfid, ovrid); break; } @@ -3557,26 +3557,26 @@ static bool ValidateIndustryLayout(const IndustryTileLayout &layout) /** * Define properties for industries - * @param indid Local ID of the industry. - * @param numinfo Number of subsequent industry IDs to change the property for. + * @param first Local ID of the first industry. + * @param last Local ID of the last industry. * @param prop The property to change. * @param buf The property value. * @return ChangeInfoResult. */ -static ChangeInfoResult IndustriesChangeInfo(uint indid, int numinfo, int prop, ByteReader &buf) +static ChangeInfoResult IndustriesChangeInfo(uint first, uint last, int prop, ByteReader &buf) { ChangeInfoResult ret = CIR_SUCCESS; - if (indid + numinfo > NUM_INDUSTRYTYPES_PER_GRF) { - GrfMsg(1, "IndustriesChangeInfo: Too many industries loaded ({}), max ({}). Ignoring.", indid + numinfo, NUM_INDUSTRYTYPES_PER_GRF); + if (last > NUM_INDUSTRYTYPES_PER_GRF) { + GrfMsg(1, "IndustriesChangeInfo: Too many industries loaded ({}), max ({}). Ignoring.", last, NUM_INDUSTRYTYPES_PER_GRF); return CIR_INVALID_ID; } /* Allocate industry specs if they haven't been allocated already. */ - if (_cur.grffile->industryspec.size() < indid + numinfo) _cur.grffile->industryspec.resize(indid + numinfo); + if (_cur.grffile->industryspec.size() < last) _cur.grffile->industryspec.resize(last); - for (int i = 0; i < numinfo; i++) { - auto &indsp = _cur.grffile->industryspec[indid + i]; + for (uint id = first; id < last; ++id) { + auto &indsp = _cur.grffile->industryspec[id]; if (prop != 0x08 && indsp == nullptr) { ChangeInfoResult cir = IgnoreIndustryProperty(prop, buf); @@ -3590,11 +3590,11 @@ static ChangeInfoResult IndustriesChangeInfo(uint indid, int numinfo, int prop, if (subs_id == 0xFF) { /* Instead of defining a new industry, a substitute industry id * of 0xFF disables the old industry with the current id. */ - _industry_specs[indid + i].enabled = false; + _industry_specs[id].enabled = false; continue; } else if (subs_id >= NEW_INDUSTRYOFFSET) { /* The substitute id must be one of the original industry. */ - GrfMsg(2, "_industry_specs: Attempt to use new industry {} as substitute industry for {}. Ignoring.", subs_id, indid + i); + GrfMsg(2, "_industry_specs: Attempt to use new industry {} as substitute industry for {}. Ignoring.", subs_id, id); continue; } @@ -3605,7 +3605,7 @@ static ChangeInfoResult IndustriesChangeInfo(uint indid, int numinfo, int prop, indsp = std::make_unique(_origin_industry_specs[subs_id]); indsp->enabled = true; - indsp->grf_prop.local_id = indid + i; + indsp->grf_prop.local_id = id; indsp->grf_prop.subst_id = subs_id; indsp->grf_prop.grfid = _cur.grffile->grfid; indsp->grf_prop.grffile = _cur.grffile; @@ -3621,11 +3621,11 @@ static ChangeInfoResult IndustriesChangeInfo(uint indid, int numinfo, int prop, /* The industry being overridden must be an original industry. */ if (ovrid >= NEW_INDUSTRYOFFSET) { - GrfMsg(2, "IndustriesChangeInfo: Attempt to override new industry {} with industry id {}. Ignoring.", ovrid, indid + i); + GrfMsg(2, "IndustriesChangeInfo: Attempt to override new industry {} with industry id {}. Ignoring.", ovrid, id); continue; } indsp->grf_prop.override = ovrid; - _industry_mngr.Add(indid + i, _cur.grffile->grfid, ovrid); + _industry_mngr.Add(id, _cur.grffile->grfid, ovrid); break; } @@ -3642,7 +3642,7 @@ static ChangeInfoResult IndustriesChangeInfo(uint indid, int numinfo, int prop, for (uint k = 0;; k++) { if (bytes_read >= definition_size) { - GrfMsg(3, "IndustriesChangeInfo: Incorrect size for industry tile layout definition for industry {}.", indid); + GrfMsg(3, "IndustriesChangeInfo: Incorrect size for industry tile layout definition for industry {}.", id); /* Avoid warning twice */ definition_size = UINT32_MAX; } @@ -3659,12 +3659,12 @@ static ChangeInfoResult IndustriesChangeInfo(uint indid, int numinfo, int prop, bytes_read += 2; if (type >= lengthof(_origin_industry_specs)) { - GrfMsg(1, "IndustriesChangeInfo: Invalid original industry number for layout import, industry {}", indid); + GrfMsg(1, "IndustriesChangeInfo: Invalid original industry number for layout import, industry {}", id); DisableGrf(STR_NEWGRF_ERROR_INVALID_ID); return CIR_DISABLED; } if (laynbr >= _origin_industry_specs[type].layouts.size()) { - GrfMsg(1, "IndustriesChangeInfo: Invalid original industry layout index for layout import, industry {}", indid); + GrfMsg(1, "IndustriesChangeInfo: Invalid original industry layout index for layout import, industry {}", id); DisableGrf(STR_NEWGRF_ERROR_INVALID_ID); return CIR_DISABLED; } @@ -3693,7 +3693,7 @@ static ChangeInfoResult IndustriesChangeInfo(uint indid, int numinfo, int prop, int tempid = _industile_mngr.GetID(local_tile_id, _cur.grffile->grfid); if (tempid == INVALID_INDUSTRYTILE) { - GrfMsg(2, "IndustriesChangeInfo: Attempt to use industry tile {} with industry id {}, not yet defined. Ignoring.", local_tile_id, indid); + GrfMsg(2, "IndustriesChangeInfo: Attempt to use industry tile {} with industry id {}, not yet defined. Ignoring.", local_tile_id, id); } else { /* Declared as been valid, can be used */ it.gfx = tempid; @@ -3715,7 +3715,7 @@ static ChangeInfoResult IndustriesChangeInfo(uint indid, int numinfo, int prop, if (!ValidateIndustryLayout(layout)) { /* The industry layout was not valid, so skip this one. */ - GrfMsg(1, "IndustriesChangeInfo: Invalid industry layout for industry id {}. Ignoring", indid); + GrfMsg(1, "IndustriesChangeInfo: Invalid industry layout for industry id {}. Ignoring", id); new_num_layouts--; j--; } else { @@ -3931,29 +3931,29 @@ static ChangeInfoResult IndustriesChangeInfo(uint indid, int numinfo, int prop, /** * Define properties for airports - * @param airport Local ID of the airport. - * @param numinfo Number of subsequent airport IDs to change the property for. + * @param first Local ID of the first airport. + * @param last Local ID of the last airport. * @param prop The property to change. * @param buf The property value. * @return ChangeInfoResult. */ -static ChangeInfoResult AirportChangeInfo(uint airport, int numinfo, int prop, ByteReader &buf) +static ChangeInfoResult AirportChangeInfo(uint first, uint last, int prop, ByteReader &buf) { ChangeInfoResult ret = CIR_SUCCESS; - if (airport + numinfo > NUM_AIRPORTS_PER_GRF) { - GrfMsg(1, "AirportChangeInfo: Too many airports, trying id ({}), max ({}). Ignoring.", airport + numinfo, NUM_AIRPORTS_PER_GRF); + if (last > NUM_AIRPORTS_PER_GRF) { + GrfMsg(1, "AirportChangeInfo: Too many airports, trying id ({}), max ({}). Ignoring.", last, NUM_AIRPORTS_PER_GRF); return CIR_INVALID_ID; } /* Allocate industry specs if they haven't been allocated already. */ - if (_cur.grffile->airportspec.size() < airport + numinfo) _cur.grffile->airportspec.resize(airport + numinfo); + if (_cur.grffile->airportspec.size() < last) _cur.grffile->airportspec.resize(last); - for (int i = 0; i < numinfo; i++) { - auto &as = _cur.grffile->airportspec[airport + i]; + for (uint id = first; id < last; ++id) { + auto &as = _cur.grffile->airportspec[id]; if (as == nullptr && prop != 0x08 && prop != 0x09) { - GrfMsg(2, "AirportChangeInfo: Attempt to modify undefined airport {}, ignoring", airport + i); + GrfMsg(2, "AirportChangeInfo: Attempt to modify undefined airport {}, ignoring", id); return CIR_INVALID_ID; } @@ -3963,11 +3963,11 @@ static ChangeInfoResult AirportChangeInfo(uint airport, int numinfo, int prop, B if (subs_id == 0xFF) { /* Instead of defining a new airport, an airport id * of 0xFF disables the old airport with the current id. */ - AirportSpec::GetWithoutOverride(airport + i)->enabled = false; + AirportSpec::GetWithoutOverride(id)->enabled = false; continue; } else if (subs_id >= NEW_AIRPORT_OFFSET) { /* The substitute id must be one of the original airports. */ - GrfMsg(2, "AirportChangeInfo: Attempt to use new airport {} as substitute airport for {}. Ignoring.", subs_id, airport + i); + GrfMsg(2, "AirportChangeInfo: Attempt to use new airport {} as substitute airport for {}. Ignoring.", subs_id, id); continue; } @@ -3978,12 +3978,12 @@ static ChangeInfoResult AirportChangeInfo(uint airport, int numinfo, int prop, B as = std::make_unique(*AirportSpec::GetWithoutOverride(subs_id)); as->enabled = true; - as->grf_prop.local_id = airport + i; + as->grf_prop.local_id = id; as->grf_prop.subst_id = subs_id; as->grf_prop.grfid = _cur.grffile->grfid; as->grf_prop.grffile = _cur.grffile; /* override the default airport */ - _airport_mngr.Add(airport + i, _cur.grffile->grfid, subs_id); + _airport_mngr.Add(id, _cur.grffile->grfid, subs_id); } break; } @@ -4023,7 +4023,7 @@ static ChangeInfoResult AirportChangeInfo(uint airport, int numinfo, int prop, B uint16_t tempid = _airporttile_mngr.GetID(local_tile_id, _cur.grffile->grfid); if (tempid == INVALID_AIRPORTTILE) { - GrfMsg(2, "AirportChangeInfo: Attempt to use airport tile {} with airport id {}, not yet defined. Ignoring.", local_tile_id, airport + i); + GrfMsg(2, "AirportChangeInfo: Attempt to use airport tile {} with airport id {}, not yet defined. Ignoring.", local_tile_id, id); } else { /* Declared as been valid, can be used */ tile.gfx = tempid; @@ -4131,26 +4131,26 @@ static ChangeInfoResult IgnoreObjectProperty(uint prop, ByteReader &buf) /** * Define properties for objects - * @param id Local ID of the object. - * @param numinfo Number of subsequent objectIDs to change the property for. + * @param first Local ID of the first object. + * @param last Local ID of the last object. * @param prop The property to change. * @param buf The property value. * @return ChangeInfoResult. */ -static ChangeInfoResult ObjectChangeInfo(uint id, int numinfo, int prop, ByteReader &buf) +static ChangeInfoResult ObjectChangeInfo(uint first, uint last, int prop, ByteReader &buf) { ChangeInfoResult ret = CIR_SUCCESS; - if (id + numinfo > NUM_OBJECTS_PER_GRF) { - GrfMsg(1, "ObjectChangeInfo: Too many objects loaded ({}), max ({}). Ignoring.", id + numinfo, NUM_OBJECTS_PER_GRF); + if (last > NUM_OBJECTS_PER_GRF) { + GrfMsg(1, "ObjectChangeInfo: Too many objects loaded ({}), max ({}). Ignoring.", last, NUM_OBJECTS_PER_GRF); return CIR_INVALID_ID; } /* Allocate object specs if they haven't been allocated already. */ - if (_cur.grffile->objectspec.size() < id + numinfo) _cur.grffile->objectspec.resize(id + numinfo); + if (_cur.grffile->objectspec.size() < last) _cur.grffile->objectspec.resize(last); - for (int i = 0; i < numinfo; i++) { - auto &spec = _cur.grffile->objectspec[id + i]; + for (uint id = first; id < last; ++id) { + auto &spec = _cur.grffile->objectspec[id]; if (prop != 0x08 && spec == nullptr) { /* If the object property 08 is not yet set, ignore this property */ @@ -4190,7 +4190,7 @@ static ChangeInfoResult ObjectChangeInfo(uint id, int numinfo, int prop, ByteRea case 0x0C: // Size spec->size = buf.ReadByte(); if (GB(spec->size, 0, 4) == 0 || GB(spec->size, 4, 4) == 0) { - GrfMsg(0, "ObjectChangeInfo: Invalid object size requested (0x{:X}) for object id {}. Ignoring.", spec->size, id + i); + GrfMsg(0, "ObjectChangeInfo: Invalid object size requested (0x{:X}) for object id {}. Ignoring.", spec->size, id); spec->size = OBJECT_SIZE_1X1; } break; @@ -4241,7 +4241,7 @@ static ChangeInfoResult ObjectChangeInfo(uint id, int numinfo, int prop, ByteRea case 0x17: // Views spec->views = buf.ReadByte(); if (spec->views != 1 && spec->views != 2 && spec->views != 4) { - GrfMsg(2, "ObjectChangeInfo: Invalid number of views ({}) for object id {}. Ignoring.", spec->views, id + i); + GrfMsg(2, "ObjectChangeInfo: Invalid number of views ({}) for object id {}. Ignoring.", spec->views, id); spec->views = 1; } break; @@ -4261,25 +4261,25 @@ static ChangeInfoResult ObjectChangeInfo(uint id, int numinfo, int prop, ByteRea /** * Define properties for railtypes - * @param id ID of the railtype. - * @param numinfo Number of subsequent IDs to change the property for. + * @param first Local ID of the first railtype. + * @param last Local ID of the last railtype. * @param prop The property to change. * @param buf The property value. * @return ChangeInfoResult. */ -static ChangeInfoResult RailTypeChangeInfo(uint id, int numinfo, int prop, ByteReader &buf) +static ChangeInfoResult RailTypeChangeInfo(uint first, uint last, int prop, ByteReader &buf) { ChangeInfoResult ret = CIR_SUCCESS; extern RailTypeInfo _railtypes[RAILTYPE_END]; - if (id + numinfo > RAILTYPE_END) { - GrfMsg(1, "RailTypeChangeInfo: Rail type {} is invalid, max {}, ignoring", id + numinfo, RAILTYPE_END); + if (last > RAILTYPE_END) { + GrfMsg(1, "RailTypeChangeInfo: Rail type {} is invalid, max {}, ignoring", last, RAILTYPE_END); return CIR_INVALID_ID; } - for (int i = 0; i < numinfo; i++) { - RailType rt = _cur.grffile->railtype_map[id + i]; + for (uint id = first; id < last; ++id) { + RailType rt = _cur.grffile->railtype_map[id]; if (rt == INVALID_RAILTYPE) return CIR_INVALID_ID; RailTypeInfo *rti = &_railtypes[rt]; @@ -4397,18 +4397,18 @@ static ChangeInfoResult RailTypeChangeInfo(uint id, int numinfo, int prop, ByteR return ret; } -static ChangeInfoResult RailTypeReserveInfo(uint id, int numinfo, int prop, ByteReader &buf) +static ChangeInfoResult RailTypeReserveInfo(uint first, uint last, int prop, ByteReader &buf) { ChangeInfoResult ret = CIR_SUCCESS; extern RailTypeInfo _railtypes[RAILTYPE_END]; - if (id + numinfo > RAILTYPE_END) { - GrfMsg(1, "RailTypeReserveInfo: Rail type {} is invalid, max {}, ignoring", id + numinfo, RAILTYPE_END); + if (last > RAILTYPE_END) { + GrfMsg(1, "RailTypeReserveInfo: Rail type {} is invalid, max {}, ignoring", last, RAILTYPE_END); return CIR_INVALID_ID; } - for (int i = 0; i < numinfo; i++) { + for (uint id = first; id < last; ++id) { switch (prop) { case 0x08: // Label of rail type { @@ -4421,7 +4421,7 @@ static ChangeInfoResult RailTypeReserveInfo(uint id, int numinfo, int prop, Byte rt = AllocateRailType(rtl); } - _cur.grffile->railtype_map[id + i] = rt; + _cur.grffile->railtype_map[id] = rt; break; } @@ -4438,14 +4438,14 @@ static ChangeInfoResult RailTypeReserveInfo(uint id, int numinfo, int prop, Byte break; case 0x1D: // Alternate rail type label list - if (_cur.grffile->railtype_map[id + i] != INVALID_RAILTYPE) { + if (_cur.grffile->railtype_map[id] != INVALID_RAILTYPE) { int n = buf.ReadByte(); for (int j = 0; j != n; j++) { - _railtypes[_cur.grffile->railtype_map[id + i]].alternate_labels.push_back(BSWAP32(buf.ReadDWord())); + _railtypes[_cur.grffile->railtype_map[id]].alternate_labels.push_back(BSWAP32(buf.ReadDWord())); } break; } - GrfMsg(1, "RailTypeReserveInfo: Ignoring property 1D for rail type {} because no label was set", id + i); + GrfMsg(1, "RailTypeReserveInfo: Ignoring property 1D for rail type {} because no label was set", id); [[fallthrough]]; case 0x0E: // Compatible railtype list @@ -4479,26 +4479,26 @@ static ChangeInfoResult RailTypeReserveInfo(uint id, int numinfo, int prop, Byte /** * Define properties for roadtypes - * @param id ID of the roadtype. - * @param numinfo Number of subsequent IDs to change the property for. + * @param first Local ID of the first roadtype. + * @param last Local ID of the last roadtype. * @param prop The property to change. * @param buf The property value. * @return ChangeInfoResult. */ -static ChangeInfoResult RoadTypeChangeInfo(uint id, int numinfo, int prop, ByteReader &buf, RoadTramType rtt) +static ChangeInfoResult RoadTypeChangeInfo(uint first, uint last, int prop, ByteReader &buf, RoadTramType rtt) { ChangeInfoResult ret = CIR_SUCCESS; extern RoadTypeInfo _roadtypes[ROADTYPE_END]; std::array &type_map = (rtt == RTT_TRAM) ? _cur.grffile->tramtype_map : _cur.grffile->roadtype_map; - if (id + numinfo > ROADTYPE_END) { - GrfMsg(1, "RoadTypeChangeInfo: Road type {} is invalid, max {}, ignoring", id + numinfo, ROADTYPE_END); + if (last > ROADTYPE_END) { + GrfMsg(1, "RoadTypeChangeInfo: Road type {} is invalid, max {}, ignoring", last, ROADTYPE_END); return CIR_INVALID_ID; } - for (int i = 0; i < numinfo; i++) { - RoadType rt = type_map[id + i]; + for (uint id = first; id < last; ++id) { + RoadType rt = type_map[id]; if (rt == INVALID_ROADTYPE) return CIR_INVALID_ID; RoadTypeInfo *rti = &_roadtypes[rt]; @@ -4602,30 +4602,30 @@ static ChangeInfoResult RoadTypeChangeInfo(uint id, int numinfo, int prop, ByteR return ret; } -static ChangeInfoResult RoadTypeChangeInfo(uint id, int numinfo, int prop, ByteReader &buf) +static ChangeInfoResult RoadTypeChangeInfo(uint first, uint last, int prop, ByteReader &buf) { - return RoadTypeChangeInfo(id, numinfo, prop, buf, RTT_ROAD); + return RoadTypeChangeInfo(first, last, prop, buf, RTT_ROAD); } -static ChangeInfoResult TramTypeChangeInfo(uint id, int numinfo, int prop, ByteReader &buf) +static ChangeInfoResult TramTypeChangeInfo(uint first, uint last, int prop, ByteReader &buf) { - return RoadTypeChangeInfo(id, numinfo, prop, buf, RTT_TRAM); + return RoadTypeChangeInfo(first, last, prop, buf, RTT_TRAM); } -static ChangeInfoResult RoadTypeReserveInfo(uint id, int numinfo, int prop, ByteReader &buf, RoadTramType rtt) +static ChangeInfoResult RoadTypeReserveInfo(uint first, uint last, int prop, ByteReader &buf, RoadTramType rtt) { ChangeInfoResult ret = CIR_SUCCESS; extern RoadTypeInfo _roadtypes[ROADTYPE_END]; std::array &type_map = (rtt == RTT_TRAM) ? _cur.grffile->tramtype_map : _cur.grffile->roadtype_map; - if (id + numinfo > ROADTYPE_END) { - GrfMsg(1, "RoadTypeReserveInfo: Road type {} is invalid, max {}, ignoring", id + numinfo, ROADTYPE_END); + if (last > ROADTYPE_END) { + GrfMsg(1, "RoadTypeReserveInfo: Road type {} is invalid, max {}, ignoring", last, ROADTYPE_END); return CIR_INVALID_ID; } - for (int i = 0; i < numinfo; i++) { + for (uint id = first; id < last; ++id) { switch (prop) { case 0x08: { // Label of road type RoadTypeLabel rtl = buf.ReadDWord(); @@ -4636,11 +4636,11 @@ static ChangeInfoResult RoadTypeReserveInfo(uint id, int numinfo, int prop, Byte /* Set up new road type */ rt = AllocateRoadType(rtl, rtt); } else if (GetRoadTramType(rt) != rtt) { - GrfMsg(1, "RoadTypeReserveInfo: Road type {} is invalid type (road/tram), ignoring", id + numinfo); + GrfMsg(1, "RoadTypeReserveInfo: Road type {} is invalid type (road/tram), ignoring", id); return CIR_INVALID_ID; } - type_map[id + i] = rt; + type_map[id] = rt; break; } case 0x09: // Toolbar caption of roadtype @@ -4656,14 +4656,14 @@ static ChangeInfoResult RoadTypeReserveInfo(uint id, int numinfo, int prop, Byte break; case 0x1D: // Alternate road type label list - if (type_map[id + i] != INVALID_ROADTYPE) { + if (type_map[id] != INVALID_ROADTYPE) { int n = buf.ReadByte(); for (int j = 0; j != n; j++) { - _roadtypes[type_map[id + i]].alternate_labels.push_back(BSWAP32(buf.ReadDWord())); + _roadtypes[type_map[id]].alternate_labels.push_back(BSWAP32(buf.ReadDWord())); } break; } - GrfMsg(1, "RoadTypeReserveInfo: Ignoring property 1D for road type {} because no label was set", id + i); + GrfMsg(1, "RoadTypeReserveInfo: Ignoring property 1D for road type {} because no label was set", id); /* FALL THROUGH */ case 0x0F: // Powered roadtype list @@ -4691,33 +4691,33 @@ static ChangeInfoResult RoadTypeReserveInfo(uint id, int numinfo, int prop, Byte return ret; } -static ChangeInfoResult RoadTypeReserveInfo(uint id, int numinfo, int prop, ByteReader &buf) +static ChangeInfoResult RoadTypeReserveInfo(uint first, uint last, int prop, ByteReader &buf) { - return RoadTypeReserveInfo(id, numinfo, prop, buf, RTT_ROAD); + return RoadTypeReserveInfo(first, last, prop, buf, RTT_ROAD); } -static ChangeInfoResult TramTypeReserveInfo(uint id, int numinfo, int prop, ByteReader &buf) +static ChangeInfoResult TramTypeReserveInfo(uint first, uint last, int prop, ByteReader &buf) { - return RoadTypeReserveInfo(id, numinfo, prop, buf, RTT_TRAM); + return RoadTypeReserveInfo(first, last, prop, buf, RTT_TRAM); } -static ChangeInfoResult AirportTilesChangeInfo(uint airtid, int numinfo, int prop, ByteReader &buf) +static ChangeInfoResult AirportTilesChangeInfo(uint first, uint last, int prop, ByteReader &buf) { ChangeInfoResult ret = CIR_SUCCESS; - if (airtid + numinfo > NUM_AIRPORTTILES_PER_GRF) { - GrfMsg(1, "AirportTileChangeInfo: Too many airport tiles loaded ({}), max ({}). Ignoring.", airtid + numinfo, NUM_AIRPORTTILES_PER_GRF); + if (last > NUM_AIRPORTTILES_PER_GRF) { + GrfMsg(1, "AirportTileChangeInfo: Too many airport tiles loaded ({}), max ({}). Ignoring.", last, NUM_AIRPORTTILES_PER_GRF); return CIR_INVALID_ID; } /* Allocate airport tile specs if they haven't been allocated already. */ - if (_cur.grffile->airtspec.size() < airtid + numinfo) _cur.grffile->airtspec.resize(airtid + numinfo); + if (_cur.grffile->airtspec.size() < last) _cur.grffile->airtspec.resize(last); - for (int i = 0; i < numinfo; i++) { - auto &tsp = _cur.grffile->airtspec[airtid + i]; + for (uint id = first; id < last; ++id) { + auto &tsp = _cur.grffile->airtspec[id]; if (prop != 0x08 && tsp == nullptr) { - GrfMsg(2, "AirportTileChangeInfo: Attempt to modify undefined airport tile {}. Ignoring.", airtid + i); + GrfMsg(2, "AirportTileChangeInfo: Attempt to modify undefined airport tile {}. Ignoring.", id); return CIR_INVALID_ID; } @@ -4726,7 +4726,7 @@ static ChangeInfoResult AirportTilesChangeInfo(uint airtid, int numinfo, int pro uint8_t subs_id = buf.ReadByte(); if (subs_id >= NEW_AIRPORTTILE_OFFSET) { /* The substitute id must be one of the original airport tiles. */ - GrfMsg(2, "AirportTileChangeInfo: Attempt to use new airport tile {} as substitute airport tile for {}. Ignoring.", subs_id, airtid + i); + GrfMsg(2, "AirportTileChangeInfo: Attempt to use new airport tile {} as substitute airport tile for {}. Ignoring.", subs_id, id); continue; } @@ -4738,11 +4738,11 @@ static ChangeInfoResult AirportTilesChangeInfo(uint airtid, int numinfo, int pro tsp->animation.status = ANIM_STATUS_NO_ANIMATION; - tsp->grf_prop.local_id = airtid + i; + tsp->grf_prop.local_id = id; tsp->grf_prop.subst_id = subs_id; tsp->grf_prop.grfid = _cur.grffile->grfid; tsp->grf_prop.grffile = _cur.grffile; - _airporttile_mngr.AddEntityID(airtid + i, _cur.grffile->grfid, subs_id); // pre-reserve the tile slot + _airporttile_mngr.AddEntityID(id, _cur.grffile->grfid, subs_id); // pre-reserve the tile slot } break; } @@ -4752,11 +4752,11 @@ static ChangeInfoResult AirportTilesChangeInfo(uint airtid, int numinfo, int pro /* The airport tile being overridden must be an original airport tile. */ if (override >= NEW_AIRPORTTILE_OFFSET) { - GrfMsg(2, "AirportTileChangeInfo: Attempt to override new airport tile {} with airport tile id {}. Ignoring.", override, airtid + i); + GrfMsg(2, "AirportTileChangeInfo: Attempt to override new airport tile {} with airport tile id {}. Ignoring.", override, id); continue; } - _airporttile_mngr.Add(airtid + i, _cur.grffile->grfid, override); + _airporttile_mngr.Add(id, _cur.grffile->grfid, override); break; } @@ -4826,22 +4826,22 @@ static ChangeInfoResult IgnoreRoadStopProperty(uint prop, ByteReader &buf) return ret; } -static ChangeInfoResult RoadStopChangeInfo(uint id, int numinfo, int prop, ByteReader &buf) +static ChangeInfoResult RoadStopChangeInfo(uint first, uint last, int prop, ByteReader &buf) { ChangeInfoResult ret = CIR_SUCCESS; - if (id + numinfo > NUM_ROADSTOPS_PER_GRF) { - GrfMsg(1, "RoadStopChangeInfo: RoadStop {} is invalid, max {}, ignoring", id + numinfo, NUM_ROADSTOPS_PER_GRF); + if (last > NUM_ROADSTOPS_PER_GRF) { + GrfMsg(1, "RoadStopChangeInfo: RoadStop {} is invalid, max {}, ignoring", last, NUM_ROADSTOPS_PER_GRF); return CIR_INVALID_ID; } - if (_cur.grffile->roadstops.size() < id + numinfo) _cur.grffile->roadstops.resize(id + numinfo); + if (_cur.grffile->roadstops.size() < last) _cur.grffile->roadstops.resize(last); - for (int i = 0; i < numinfo; i++) { - auto &rs = _cur.grffile->roadstops[id + i]; + for (uint id = first; id < last; ++id) { + auto &rs = _cur.grffile->roadstops[id]; if (rs == nullptr && prop != 0x08) { - GrfMsg(1, "RoadStopChangeInfo: Attempt to modify undefined road stop {}, ignoring", id + i); + GrfMsg(1, "RoadStopChangeInfo: Attempt to modify undefined road stop {}, ignoring", id); ChangeInfoResult cir = IgnoreRoadStopProperty(prop, buf); if (cir > ret) ret = cir; continue; @@ -4956,7 +4956,7 @@ static void FeatureChangeInfo(ByteReader &buf) * B property what property to change, depends on the feature * V new-info new bytes of info (variable size; depends on properties) */ - static const VCI_Handler handler[] = { + static ChangeInfoHandler * const handler[] = { /* GSF_TRAINS */ RailVehicleChangeInfo, /* GSF_ROADVEHICLES */ RoadVehicleChangeInfo, /* GSF_SHIPS */ ShipVehicleChangeInfo, @@ -4979,7 +4979,7 @@ static void FeatureChangeInfo(ByteReader &buf) /* GSF_TRAMTYPES */ TramTypeChangeInfo, /* GSF_ROADSTOPS */ RoadStopChangeInfo, }; - static_assert(GSF_END == lengthof(handler)); + static_assert(GSF_END == std::size(handler)); uint8_t feature = buf.ReadByte(); uint8_t numprops = buf.ReadByte(); @@ -5005,7 +5005,7 @@ static void FeatureChangeInfo(ByteReader &buf) while (numprops-- && buf.HasData()) { uint8_t prop = buf.ReadByte(); - ChangeInfoResult cir = handler[feature](engine, numinfo, prop, buf); + ChangeInfoResult cir = handler[feature](engine, engine + numinfo, prop, buf); if (HandleChangeInfoResult("FeatureChangeInfo", cir, feature, prop)) return; } } @@ -5065,23 +5065,23 @@ static void ReserveChangeInfo(ByteReader &buf) switch (feature) { default: NOT_REACHED(); case GSF_CARGOES: - cir = CargoChangeInfo(index, numinfo, prop, buf); + cir = CargoChangeInfo(index, index + numinfo, prop, buf); break; case GSF_GLOBALVAR: - cir = GlobalVarReserveInfo(index, numinfo, prop, buf); + cir = GlobalVarReserveInfo(index, index + numinfo, prop, buf); break; case GSF_RAILTYPES: - cir = RailTypeReserveInfo(index, numinfo, prop, buf); + cir = RailTypeReserveInfo(index, index + numinfo, prop, buf); break; case GSF_ROADTYPES: - cir = RoadTypeReserveInfo(index, numinfo, prop, buf); + cir = RoadTypeReserveInfo(index, index + numinfo, prop, buf); break; case GSF_TRAMTYPES: - cir = TramTypeReserveInfo(index, numinfo, prop, buf); + cir = TramTypeReserveInfo(index, index + numinfo, prop, buf); break; }