Send town zone radii to network clients in new TNNC chunk

This commit is contained in:
Jonathan G Rennison
2023-03-11 17:51:02 +00:00
parent ae99f667b0
commit f44d455adc
5 changed files with 59 additions and 3 deletions

View File

@@ -183,6 +183,7 @@ const SlxiSubChunkInfo _sl_xv_sub_chunk_infos[] = {
{ XSLFI_LINKGRAPH_SPARSE_EDGES, XSCF_NULL, 1, 1, "linkgraph_sparse_edges", nullptr, nullptr, nullptr }, { XSLFI_LINKGRAPH_SPARSE_EDGES, XSCF_NULL, 1, 1, "linkgraph_sparse_edges", nullptr, nullptr, nullptr },
{ XSLFI_AUX_TILE_LOOP, XSCF_NULL, 1, 1, "aux_tile_loop", nullptr, nullptr, nullptr }, { XSLFI_AUX_TILE_LOOP, XSCF_NULL, 1, 1, "aux_tile_loop", nullptr, nullptr, nullptr },
{ XSLFI_NEWGRF_ENTITY_EXTRA, XSCF_NULL, 1, 1, "newgrf_entity_extra", nullptr, nullptr, nullptr }, { XSLFI_NEWGRF_ENTITY_EXTRA, XSCF_NULL, 1, 1, "newgrf_entity_extra", nullptr, nullptr, nullptr },
{ XSLFI_TNNC_CHUNK, XSCF_IGNORABLE_ALL, 1, 1, "tnnc_chunk", nullptr, nullptr, "TNNC" },
{ XSLFI_SCRIPT_INT64, XSCF_NULL, 1, 1, "script_int64", nullptr, nullptr, nullptr }, { XSLFI_SCRIPT_INT64, XSCF_NULL, 1, 1, "script_int64", nullptr, nullptr, nullptr },
{ XSLFI_U64_TICK_COUNTER, XSCF_NULL, 1, 1, "u64_tick_counter", nullptr, nullptr, nullptr }, { XSLFI_U64_TICK_COUNTER, XSCF_NULL, 1, 1, "u64_tick_counter", nullptr, nullptr, nullptr },
{ XSLFI_LINKGRAPH_TRAVEL_TIME, XSCF_NULL, 1, 1, "linkgraph_travel_time", nullptr, nullptr, nullptr }, { XSLFI_LINKGRAPH_TRAVEL_TIME, XSCF_NULL, 1, 1, "linkgraph_travel_time", nullptr, nullptr, nullptr },

View File

@@ -134,6 +134,7 @@ enum SlXvFeatureIndex {
XSLFI_LINKGRAPH_SPARSE_EDGES, ///< Link graph edge matrix is stored in sparse format, and saved in order XSLFI_LINKGRAPH_SPARSE_EDGES, ///< Link graph edge matrix is stored in sparse format, and saved in order
XSLFI_AUX_TILE_LOOP, ///< Auxiliary tile loop XSLFI_AUX_TILE_LOOP, ///< Auxiliary tile loop
XSLFI_NEWGRF_ENTITY_EXTRA, ///< NewGRF entity mappings are 16 bit XSLFI_NEWGRF_ENTITY_EXTRA, ///< NewGRF entity mappings are 16 bit
XSLFI_TNNC_CHUNK, ///< TNNC chunk
XSLFI_SCRIPT_INT64, ///< See: SLV_SCRIPT_INT64 XSLFI_SCRIPT_INT64, ///< See: SLV_SCRIPT_INT64
XSLFI_U64_TICK_COUNTER, ///< See: SLV_U64_TICK_COUNTER XSLFI_U64_TICK_COUNTER, ///< See: SLV_U64_TICK_COUNTER

View File

@@ -3437,8 +3437,10 @@ static SaveOrLoadResult DoLoad(LoadFilter *reader, bool load_check)
SlXvResetState(); SlXvResetState();
SlResetVENC(); SlResetVENC();
SlResetTNNC();
auto guard = scope_guard([&]() { auto guard = scope_guard([&]() {
SlResetVENC(); SlResetVENC();
SlResetTNNC();
}); });
uint32 hdr[2]; uint32 hdr[2];

View File

@@ -750,6 +750,8 @@ bool SaveloadCrashWithMissingNewGRFs();
void SlResetVENC(); void SlResetVENC();
void SlProcessVENC(); void SlProcessVENC();
void SlResetTNNC();
extern std::string _savegame_format; extern std::string _savegame_format;
extern bool _do_autosave; extern bool _do_autosave;

View File

@@ -13,12 +13,15 @@
#include "../landscape.h" #include "../landscape.h"
#include "../subsidy_func.h" #include "../subsidy_func.h"
#include "../strings_func.h" #include "../strings_func.h"
#include "../network/network.h"
#include "saveload.h" #include "saveload.h"
#include "newgrf_sl.h" #include "newgrf_sl.h"
#include "../safeguards.h" #include "../safeguards.h"
static bool _town_zone_radii_no_update = false;
HouseID SLGetCleanHouseType(TileIndex t, bool old_map_position) HouseID SLGetCleanHouseType(TileIndex t, bool old_map_position)
{ {
if (old_map_position && SlXvIsFeatureMissing(XSLFI_MORE_HOUSES)) { if (old_map_position && SlXvIsFeatureMissing(XSLFI_MORE_HOUSES)) {
@@ -54,9 +57,11 @@ void RebuildTownCaches(bool cargo_update_required, bool old_map_position)
if (GetHouseNorthPart(house_id) == 0) town->cache.num_houses++; if (GetHouseNorthPart(house_id) == 0) town->cache.num_houses++;
} }
/* Update the population and num_house dependent values */ if (!_town_zone_radii_no_update) {
for (Town *town : Town::Iterate()) { /* Update the population and num_house dependent values */
UpdateTownRadius(town); for (Town *town : Town::Iterate()) {
UpdateTownRadius(town);
}
} }
} }
@@ -363,10 +368,55 @@ static void Ptrs_TOWN()
} }
} }
void SlResetTNNC()
{
_town_zone_radii_no_update = false;
}
void Save_TNNC()
{
if (!IsNetworkServerSave()) {
SlSetLength(0);
return;
}
SlSetLength(4 + (Town::GetNumItems() * (1 + lengthof(TownCache::squared_town_zone_radius)) * 4));
SlWriteUint32(Town::GetNumItems());
for (const Town *t : Town::Iterate()) {
SlWriteUint32(t->index);
for (uint i = 0; i < lengthof(TownCache::squared_town_zone_radius); i++) {
SlWriteUint32(t->cache.squared_town_zone_radius[i]);
}
}
}
void Load_TNNC()
{
if (SlGetFieldLength() == 0) return;
if (!_networking || _network_server) {
SlSkipBytes(SlGetFieldLength());
return;
}
_town_zone_radii_no_update = true;
const uint32 count = SlReadUint32();
for (uint32 idx = 0; idx < count; idx++) {
Town *t = Town::Get(SlReadUint32());
for (uint i = 0; i < lengthof(TownCache::squared_town_zone_radius); i++) {
t->cache.squared_town_zone_radius[i] = SlReadUint32();
}
}
}
/** Chunk handler for towns. */ /** Chunk handler for towns. */
static const ChunkHandler town_chunk_handlers[] = { static const ChunkHandler town_chunk_handlers[] = {
{ 'HIDS', Save_HIDS, Load_HIDS, nullptr, nullptr, CH_ARRAY }, { 'HIDS', Save_HIDS, Load_HIDS, nullptr, nullptr, CH_ARRAY },
{ 'CITY', Save_TOWN, Load_TOWN, Ptrs_TOWN, nullptr, CH_ARRAY }, { 'CITY', Save_TOWN, Load_TOWN, Ptrs_TOWN, nullptr, CH_ARRAY },
{ 'TNNC', Save_TNNC, Load_TNNC, nullptr, nullptr, CH_RIFF },
}; };
extern const ChunkHandlerTable _town_chunk_handlers(town_chunk_handlers); extern const ChunkHandlerTable _town_chunk_handlers(town_chunk_handlers);