diff --git a/src/cargomonitor.h b/src/cargomonitor.h index 30a87ea320..505ce957e8 100644 --- a/src/cargomonitor.h +++ b/src/cargomonitor.h @@ -25,9 +25,9 @@ struct Station; * - bits 0-15 town or industry number * - bit 16 is set if it is an industry number (else it is a town number). * - bits 19-23 Cargo type. - * - bits 24-COMPANY_BIT_SIZE %Company number. + * - bits 24 - 24 + COMPANY_BIT_SIZE %Company number. */ -typedef uint64_t CargoMonitorID; ///< Type of the cargo monitor number. +typedef uint32_t CargoMonitorID; ///< Type of the cargo monitor number. /** Map type for storing and updating active cargo monitor numbers and their amounts. */ typedef std::map CargoMonitorMap; @@ -49,7 +49,7 @@ enum CargoCompanyBits { }; -static_assert(CCB_COMPANY_LENGTH <= (1 << 30)); // This should never be a limiting factor +static_assert(CCB_COMPANY_LENGTH <= (1 << 8)); static_assert(NUM_CARGO <= (1 << CCB_CARGO_TYPE_LENGTH)); static_assert(MAX_COMPANIES <= (1 << CCB_COMPANY_LENGTH)); diff --git a/src/company_type.h b/src/company_type.h index b283f4b565..42305ecbbd 100644 --- a/src/company_type.h +++ b/src/company_type.h @@ -41,6 +41,8 @@ enum Owner : uint8_t { COMPANY_NEW_COMPANY = 0xF8, ///< The client wants a new company COMPANY_SPECTATOR = 0xF9, ///< The client is spectating }; + + const uint8_t COMPANY_SIZE_BITS = 8; /// Size of the company id in bits static_assert(COMPANY_SIZE_BITS <= 10); /// 32bit m9 can only fit 3 owners of size 10 diff --git a/src/industry_map.h b/src/industry_map.h index ee45ade7c9..0f6021527f 100644 --- a/src/industry_map.h +++ b/src/industry_map.h @@ -10,7 +10,9 @@ #ifndef INDUSTRY_MAP_H #define INDUSTRY_MAP_H +#include "company_type.h" #include "industrytype.h" +#include "tile_map.h" #include "water_map.h" @@ -100,7 +102,7 @@ inline void SetIndustryCompleted(Tile tile) inline uint8_t GetIndustryConstructionStage(Tile tile) { assert(IsTileType(tile, MP_INDUSTRY)); - return IsIndustryCompleted(tile) ? (uint8_t)INDUSTRY_COMPLETED : GB(tile.m1(), 0, 2); + return IsIndustryCompleted(tile) ? (uint8_t)INDUSTRY_COMPLETED : GB(tile.m1(), 0, 2); // MYTODO: Figure this out, seams like it would read bits of the owner } /** @@ -286,6 +288,8 @@ inline void MakeIndustry(Tile t, IndustryID index, IndustryGfx gfx, uint8_t rand SetIndustryTriggers(t, 0); // rest of m6 SetWaterClass(t, wc); t.m7() = 0; + t.m8() = 0; + t.m9() = 0; } #endif /* INDUSTRY_MAP_H */ diff --git a/src/road_map.h b/src/road_map.h index d979f65b78..e5b3a01ceb 100644 --- a/src/road_map.h +++ b/src/road_map.h @@ -240,7 +240,7 @@ inline Owner OldGetRoadOwner(Tile t, RoadTramType rtt) /* Trams don't need OWNER_TOWN, and remapping OWNER_NONE * to OWNER_TOWN makes it use one bit less */ Owner o = (Owner)GB(t.m3(), 4, 4); - return o == OWNER_TOWN ? OWNER_NONE : o; + return o == OLD_OWNER_TOWN ? OLD_OWNER_NONE : o; } /** @@ -284,7 +284,7 @@ inline void OldSetRoadOwner(Tile t, RoadTramType rtt, Owner o) if (rtt == RTT_ROAD) { SB(IsNormalRoadTile(t) ? t.m1() : t.m7(), 0, 5, o); } else { - SB(t.m3(), 4, 4, o == OWNER_NONE ? OWNER_TOWN : o); + SB(t.m3(), 4, 4, o == OLD_OWNER_NONE ? OLD_OWNER_TOWN : o); } } diff --git a/src/saveload/afterload.cpp b/src/saveload/afterload.cpp index bf1d2c1486..78f14c4a74 100644 --- a/src/saveload/afterload.cpp +++ b/src/saveload/afterload.cpp @@ -63,6 +63,8 @@ #include "../timer/timer_game_economy.h" #include "../timer/timer_game_tick.h" +#include "company_type.h" +#include "road_map.h" #include "saveload/saveload.h" #include "saveload_internal.h" @@ -454,8 +456,8 @@ static void FixOwnerOfRailTrack(Tile t) if (IsLevelCrossingTile(t)) { /* else change the crossing to normal road (road vehicles won't care) */ - Owner road = GetRoadOwner(t, RTT_ROAD); - Owner tram = GetRoadOwner(t, RTT_TRAM); + Owner road = OldGetRoadOwner(t, RTT_ROAD); + Owner tram = OldGetRoadOwner(t, RTT_TRAM); RoadBits bits = GetCrossingRoadBits(t); bool hasroad = HasBit(t.m7(), 6); bool hastram = HasBit(t.m7(), 7); @@ -466,7 +468,7 @@ static void FixOwnerOfRailTrack(Tile t) t.m3() = (hasroad ? bits : 0); t.m5() = (hastram ? bits : 0) | ROAD_TILE_NORMAL << 6; SB(t.m6(), 2, 4, 0); - SetRoadOwner(t, RTT_TRAM, tram); + OldSetRoadOwner(t, RTT_TRAM, tram); return; } @@ -1226,7 +1228,7 @@ bool AfterLoadGame() t.m5() = (axis == AXIS_X ? ROAD_Y : ROAD_X) | ROAD_TILE_NORMAL << 6; SB(t.m6(), 2, 4, 0); t.m7() = 1 << 6; - SetRoadOwner(t, RTT_TRAM, OWNER_NONE); + OldSetRoadOwner(t, RTT_TRAM, OWNER_NONE); } } else { if (GB(t.m5(), 3, 2) == 0) { @@ -1883,8 +1885,8 @@ bool AfterLoadGame() /* works for all RoadTileType */ for (RoadTramType rtt : _roadtramtypes) { /* update even non-existing road types to update tile owner too */ - Owner o = GetRoadOwner(t, rtt); - if (o < OLD_MAX_COMPANIES && !Company::IsValidID(o)) SetRoadOwner(t, rtt, OWNER_NONE); + Owner o = OldGetRoadOwner(t, rtt); + if (o < OLD_MAX_COMPANIES && !Company::IsValidID(o)) OldSetRoadOwner(t, rtt, OWNER_NONE); } if (IsLevelCrossing(t)) { if (!Company::IsValidID(OldGetTileOwner(t))) FixOwnerOfRailTrack(t); @@ -2411,7 +2413,6 @@ bool AfterLoadGame() /* We need to properly number/name the depots. * The first step is making sure none of the depots uses the * 'default' names, after that we can assign the names. */ - for (Depot *d : Depot::Iterate()) d->town_cn = UINT16_MAX; for (Depot *d : Depot::Iterate()) MakeDefaultName(d); } @@ -2846,8 +2847,8 @@ bool AfterLoadGame() for (auto t : Map::Iterate()) { if (!IsBayRoadStopTile(t)) continue; Owner o = OldGetTileOwner(t); - SetRoadOwner(t, RTT_ROAD, o); - SetRoadOwner(t, RTT_TRAM, o); + OldSetRoadOwner(t, RTT_ROAD, o); + OldSetRoadOwner(t, RTT_TRAM, o); } } @@ -3268,10 +3269,22 @@ bool AfterLoadGame() } if (IsSavegameVersionBefore(SLV_MORE_COMPANIES)) { for (auto t : Map::Iterate()) { - if (IsValidTile(t) - && !IsTileType(t, MP_HOUSE) + //SB(t.m9(), 0, COMPANY_SIZE_BITS, OWNER_NONE); + if (!IsValidTile(t)) { + continue; + } + if (!IsTileType(t, MP_HOUSE) && !IsTileType(t, MP_INDUSTRY)) { - SetTileOwner(t, OldGetTileOwner(t)); + Owner o =OldGetTileOwner(t) ; + o = ParseOldOwner(o); + SetTileOwner(t, o); + } + if (MayHaveRoad(t)) { + for (RoadTramType rtt : _roadtramtypes) { + Owner o = OldGetRoadOwner(t, rtt); + o = ParseOldOwner(o); + SetRoadOwner(t, rtt, o); + } } } } diff --git a/src/saveload/cargomonitor_sl.cpp b/src/saveload/cargomonitor_sl.cpp index cdd166779b..c932ea18d7 100644 --- a/src/saveload/cargomonitor_sl.cpp +++ b/src/saveload/cargomonitor_sl.cpp @@ -24,7 +24,7 @@ struct TempStorage { /** Description of the #TempStorage structure for the purpose of load and save. */ static const SaveLoad _cargomonitor_pair_desc[] = { - SLE_VAR(TempStorage, number, SLE_UINT64), + SLE_VAR(TempStorage, number, SLE_UINT32), SLE_VAR(TempStorage, amount, SLE_UINT32), }; diff --git a/src/saveload/company_sl.cpp b/src/saveload/company_sl.cpp index e56a674b40..ae58efb6b2 100644 --- a/src/saveload/company_sl.cpp +++ b/src/saveload/company_sl.cpp @@ -523,7 +523,7 @@ struct PLYRChunkHandler : ChunkHandler { SlObject(c, slt); _company_colours[index] = c->colour; if (IsSavegameVersionBefore(SLV_MORE_COMPANIES)) { - c->bankrupt_asked = owner_from_int(c->old_bankrupt_asked); + c->bankrupt_asked = ParseOldCompMask(c->old_bankrupt_asked); } } } diff --git a/src/saveload/engine_sl.cpp b/src/saveload/engine_sl.cpp index d6e78d6638..987fd93fd5 100644 --- a/src/saveload/engine_sl.cpp +++ b/src/saveload/engine_sl.cpp @@ -120,9 +120,9 @@ struct ENGNChunkHandler : ChunkHandler { e->preview_asked = MAX_UVALUE(CompanyMask); } if (IsSavegameVersionBefore(SLV_MORE_COMPANIES)) { - e->preview_asked = owner_from_int(e->old_preview_asked); - e->company_avail = owner_from_int(e->old_company_avail); - e->company_hidden = owner_from_int(e->old_company_hidden); + e->preview_asked = ParseOldCompMask(e->old_preview_asked); + e->company_avail = ParseOldCompMask(e->old_company_avail); + e->company_hidden = ParseOldCompMask(e->old_company_hidden); } } } diff --git a/src/saveload/group_sl.cpp b/src/saveload/group_sl.cpp index 82ef465e08..e4e8674671 100644 --- a/src/saveload/group_sl.cpp +++ b/src/saveload/group_sl.cpp @@ -54,6 +54,9 @@ struct GRPSChunkHandler : ChunkHandler { SlObject(g, slt); if (IsSavegameVersionBefore(SLV_189)) g->parent = INVALID_GROUP; + if (IsSavegameVersionBefore(SLV_MORE_COMPANIES)) { + g->owner = ParseOldOwner(g->owner); + } } } }; diff --git a/src/saveload/industry_sl.cpp b/src/saveload/industry_sl.cpp index 6edea24e05..5cfdf58404 100644 --- a/src/saveload/industry_sl.cpp +++ b/src/saveload/industry_sl.cpp @@ -9,6 +9,7 @@ #include "../stdafx.h" +#include "company_type.h" #include "saveload.h" #include "compat/industry_sl_compat.h" @@ -265,6 +266,12 @@ struct INDYChunkHandler : ChunkHandler { } else if (IsSavegameVersionBefore(SLV_INDUSTRY_CARGO_REORGANISE)) { LoadMoveAcceptsProduced(i, INDUSTRY_NUM_INPUTS, INDUSTRY_NUM_OUTPUTS); } + if (IsSavegameVersionBefore(SLV_MORE_COMPANIES)) { + i->owner = ParseOldOwner(i-> owner); + i->founder = ParseOldOwner(i-> founder); + i->exclusive_supplier = ParseOldOwner(i-> exclusive_supplier); + i->exclusive_consumer = ParseOldOwner(i-> exclusive_consumer); + } Industry::IncIndustryTypeCount(i->type); } } diff --git a/src/saveload/map_sl.cpp b/src/saveload/map_sl.cpp index 8054753908..5869f823b4 100644 --- a/src/saveload/map_sl.cpp +++ b/src/saveload/map_sl.cpp @@ -17,6 +17,7 @@ #include "../fios.h" #include "../safeguards.h" +#include static uint32_t _map_dim_x; static uint32_t _map_dim_y; @@ -128,10 +129,10 @@ struct MAPOChunkHandler : ChunkHandler { std::array buf; uint size = Map::Size(); - for (TileIndex i = 0; i != size;) { - SlCopy(buf.data(), MAP_SL_BUF_SIZE, SLE_UINT8); - for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) Tile(i++).m1() = buf[j]; - } + for (TileIndex i = 0; i != size;) { + SlCopy(buf.data(), MAP_SL_BUF_SIZE, SLE_UINT8); + for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) Tile(i++).m1() = buf[j]; + } } void Save() const override @@ -362,7 +363,7 @@ struct MAP9ChunkHandler : ChunkHandler { for (TileIndex i = 0; i != size;) { SlCopy(buf.data(), MAP_SL_BUF_SIZE, SLE_UINT32); - for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) Tile(i++).m8() = buf[j]; + for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) Tile(i++).m9() = buf[j]; } } @@ -371,9 +372,10 @@ struct MAP9ChunkHandler : ChunkHandler { std::array buf; uint size = Map::Size(); + std::cout<< "loading m9" << std::endl; SlSetLength(static_cast(size) * sizeof(uint32_t)); for (TileIndex i = 0; i != size;) { - for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) buf[j] = Tile(i++).m8(); + for (uint j = 0; j != MAP_SL_BUF_SIZE; j++) buf[j] = Tile(i++).m9(); SlCopy(buf.data(), MAP_SL_BUF_SIZE, SLE_UINT32); } } @@ -390,7 +392,7 @@ static const MAP5ChunkHandler MAP5; static const MAPEChunkHandler MAPE; static const MAP7ChunkHandler MAP7; static const MAP8ChunkHandler MAP8; -static const MAP8ChunkHandler MAP9; +static const MAP9ChunkHandler MAP9; static const ChunkHandlerRef map_chunk_handlers[] = { MAPS, MAPT, diff --git a/src/saveload/saveload.cpp b/src/saveload/saveload.cpp index d3c1a7f308..a62836060c 100644 --- a/src/saveload/saveload.cpp +++ b/src/saveload/saveload.cpp @@ -60,6 +60,7 @@ #include "saveload_filter.h" #include "../safeguards.h" +#include extern const SaveLoadVersion SAVEGAME_VERSION = (SaveLoadVersion)(SL_MAX_VERSION - 1); ///< Current savegame version of OpenTTD. @@ -1096,6 +1097,7 @@ static void SlArray(void *array, size_t length, VarType conv) /* If the SLE_ARR changes size, a savegame bump is required * and the developer should have written conversion lines. * Error out to make this more visible. */ + std::cout << "Corrupted array!!" << std::endl; SlErrorCorrupt("Fixed-length array is of wrong length"); } } @@ -1132,7 +1134,7 @@ CompanyMask bitset_from_bytes(const std::vector& buf) { } -CompanyMask owner_from_int(uint16_t old_owner) { +CompanyMask ParseOldCompMask(uint16_t old_owner) { CompanyMask result; for (int i = 0; i < 16; i++) { result[i] = GB(old_owner, i, 1) & 1; @@ -1140,6 +1142,17 @@ CompanyMask owner_from_int(uint16_t old_owner) { return result; } +Owner ParseOldOwner(Owner old) { + if (old == OLD_OWNER_NONE) { + old = OWNER_NONE; + } else if (old == OLD_OWNER_TOWN){ + old = OWNER_TOWN; + } else if (old == OLD_OWNER_WATER) { + old = OWNER_WATER; + } + return old; +} + /** * Save/Load the length of the bitset followed by the array of SL_VAR bits. @@ -1625,6 +1638,7 @@ size_t SlCalcObjMemberLength(const void *object, const SaveLoad &sld) static bool SlObjectMember(void *object, const SaveLoad &sld) { + std::cout <<"Object: "<< sld.name << std::endl; if (!SlIsObjectValidInSavegame(sld)) return false; VarType conv = GB(sld.conv, 0, 8); diff --git a/src/saveload/saveload.h b/src/saveload/saveload.h index 28c128b0ed..458fc5bca0 100644 --- a/src/saveload/saveload.h +++ b/src/saveload/saveload.h @@ -1314,7 +1314,8 @@ void SlCopy(void *object, size_t length, VarType conv); std::vector SlTableHeader(const SaveLoadTable &slt); std::vector SlCompatTableHeader(const SaveLoadTable &slt, const SaveLoadCompatTable &slct); void SlObject(void *object, const SaveLoadTable &slt); -CompanyMask owner_from_int(uint16_t old_owner); +CompanyMask ParseOldCompMask(uint16_t old_owner); +Owner ParseOldOwner(Owner old); bool SaveloadCrashWithMissingNewGRFs(); diff --git a/src/saveload/signs_sl.cpp b/src/saveload/signs_sl.cpp index e3a69c2f5b..a7aa87d373 100644 --- a/src/saveload/signs_sl.cpp +++ b/src/saveload/signs_sl.cpp @@ -9,6 +9,7 @@ #include "../stdafx.h" +#include "company_type.h" #include "saveload.h" #include "compat/signs_sl_compat.h" @@ -65,6 +66,9 @@ struct SIGNChunkHandler : ChunkHandler { if (IsSavegameVersionBefore(SLV_171) && si->owner == OLD_OWNER_NONE && _file_to_saveload.abstract_ftype == FT_SCENARIO) { si->owner = OWNER_DEITY; } + if (IsSavegameVersionBefore(SLV_MORE_COMPANIES)) { + si->owner = ParseOldOwner(si->owner); + } } } }; diff --git a/src/saveload/station_sl.cpp b/src/saveload/station_sl.cpp index c964ceb46a..fc01c80be9 100644 --- a/src/saveload/station_sl.cpp +++ b/src/saveload/station_sl.cpp @@ -502,6 +502,9 @@ struct STNSChunkHandler : ChunkHandler { _waiting_acceptance = 0; SlObject(st, slt); + if (IsSavegameVersionBefore(SLV_MORE_COMPANIES)) { + st->owner = ParseOldOwner(st->owner); + } } } diff --git a/src/saveload/town_sl.cpp b/src/saveload/town_sl.cpp index ea024634f7..66375d663a 100644 --- a/src/saveload/town_sl.cpp +++ b/src/saveload/town_sl.cpp @@ -224,9 +224,12 @@ SLE_CONDVARNAME(Town, old_have_ratings, "have_ratings", SLE_FILE_U8 | SLE_CONDVARNAME(Town, old_have_ratings, "have_ratings", SLE_UINT16, SLV_104, SLV_MORE_COMPANIES), SLE_CONDARR(Town, ratings, SLE_INT16, 8, SL_MIN_VERSION, SLV_104), - SLE_CONDARR(Town, ratings, SLE_INT16, MAX_COMPANIES, SLV_104, SL_MAX_VERSION), + SLE_CONDARR(Town, ratings, SLE_INT16, 15, SLV_104, SLV_MORE_COMPANIES), + SLE_CONDARR(Town, ratings, SLE_INT16, MAX_COMPANIES, SLV_MORE_COMPANIES, SL_MAX_VERSION), + SLE_CONDARR(Town, unwanted, SLE_INT8, 8, SLV_4, SLV_104), - SLE_CONDARR(Town, unwanted, SLE_INT8, MAX_COMPANIES, SLV_104, SL_MAX_VERSION), + SLE_CONDARR(Town, unwanted, SLE_INT8, 15, SLV_104, SLV_MORE_COMPANIES), + SLE_CONDARR(Town, unwanted, SLE_INT8, MAX_COMPANIES, SLV_MORE_COMPANIES, SL_MAX_VERSION), /* Slots 0 and 2 are passengers and mail respectively for old saves. */ SLE_CONDVARNAME(Town, supplied[0].old_max, "supplied[CT_PASSENGERS].old_max", SLE_FILE_U16 | SLE_VAR_U32, SL_MIN_VERSION, SLV_9), @@ -310,8 +313,8 @@ struct CITYChunkHandler : ChunkHandler { SlErrorCorrupt("Invalid town name generator"); } if (IsSavegameVersionBefore(SLV_MORE_COMPANIES)) { - t->statues = owner_from_int(t->old_statues); - t->have_ratings = owner_from_int(t->old_have_ratings); + t->statues = ParseOldCompMask(t->old_statues); + t->have_ratings = ParseOldCompMask(t->old_have_ratings); } } } diff --git a/src/saveload/vehicle_sl.cpp b/src/saveload/vehicle_sl.cpp index 4e42de3962..02989dfe13 100644 --- a/src/saveload/vehicle_sl.cpp +++ b/src/saveload/vehicle_sl.cpp @@ -405,6 +405,11 @@ void AfterLoadVehicles(bool part_of_load) v->economy_age = v->age.base(); } } + if (IsSavegameVersionBefore(SLV_MORE_COMPANIES)) { + for (Vehicle *v : Vehicle::Iterate()) { + v->owner = ParseOldOwner(v->owner); + } + } } CheckValidVehicles(); diff --git a/src/saveload/waypoint_sl.cpp b/src/saveload/waypoint_sl.cpp index 7745c9b6e5..557081faff 100644 --- a/src/saveload/waypoint_sl.cpp +++ b/src/saveload/waypoint_sl.cpp @@ -16,6 +16,7 @@ #include "../newgrf.h" #include "../timer/timer_game_calendar.h" +#include "saveload/saveload.h" #include "table/strings.h" #include "saveload_internal.h" @@ -200,6 +201,9 @@ struct CHKPChunkHandler : ChunkHandler { wp->index = index; SlObject(wp, _old_waypoint_desc); + if (IsSavegameVersionBefore(SLV_MORE_COMPANIES)) { + wp->owner = ParseOldOwner(wp->owner); + } } }