From ae1729fa50d039fb95ca704bab1c5750b4e4e414 Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Tue, 26 Dec 2023 20:02:57 +0000 Subject: [PATCH] Network: Include town noise levels in network game saves Don't recalculate on load for network clients --- src/sl/town_sl.cpp | 36 ++++++++++++++++++++++++++++-------- src/station_cmd.cpp | 4 ++++ 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/sl/town_sl.cpp b/src/sl/town_sl.cpp index a06b4a1e40..f427496900 100644 --- a/src/sl/town_sl.cpp +++ b/src/sl/town_sl.cpp @@ -22,6 +22,7 @@ static bool _town_zone_radii_no_update = false; +extern bool _town_noise_no_update; extern bool IsGetTownZonesCallbackHandlerPresent(); HouseID SLGetCleanHouseType(TileIndex t, bool old_map_position) @@ -372,6 +373,7 @@ static void Ptrs_TOWN() void SlResetTNNC() { + _town_noise_no_update = false; _town_zone_radii_no_update = false; } @@ -379,19 +381,31 @@ void Save_TNNC() { assert(_sl_xv_feature_versions[XSLFI_TNNC_CHUNK] != 0); - if (!IsNetworkServerSave() || !IsGetTownZonesCallbackHandlerPresent()) { + if (!IsNetworkServerSave()) { SlSetLength(0); return; } - SlSetLength(4 + (Town::GetNumItems() * (1 + lengthof(TownCache::squared_town_zone_radius)) * 4)); + size_t length = 8 + (Town::GetNumItems() * 6); + uint32 flags = 0; + if (IsGetTownZonesCallbackHandlerPresent()) { + flags |= 1; + length += lengthof(TownCache::squared_town_zone_radius) * 4; + } + + SlSetLength(length); + + SlWriteUint32(flags); SlWriteUint32((uint32)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]); + SlWriteUint16(t->noise_reached); + if (flags & 2) { + for (uint i = 0; i < lengthof(TownCache::squared_town_zone_radius); i++) { + SlWriteUint32(t->cache.squared_town_zone_radius[i]); + } } } } @@ -405,13 +419,19 @@ void Load_TNNC() return; } - _town_zone_radii_no_update = true; - + const uint32 flags = SlReadUint32(); const uint32 count = SlReadUint32(); + + _town_noise_no_update = true; + _town_zone_radii_no_update = (flags & 1); + 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(); + t->noise_reached = SlReadUint16(); + if (flags & 1) { + for (uint i = 0; i < lengthof(TownCache::squared_town_zone_radius); i++) { + t->cache.squared_town_zone_radius[i] = SlReadUint32(); + } } } } diff --git a/src/station_cmd.cpp b/src/station_cmd.cpp index 3a0e04b1de..7ae79e8c1f 100644 --- a/src/station_cmd.cpp +++ b/src/station_cmd.cpp @@ -70,6 +70,8 @@ #include "safeguards.h" +bool _town_noise_no_update = false; + /** * Check whether the given tile is a hangar. * @param t the tile to of whether it is a hangar. @@ -2639,6 +2641,8 @@ Town *AirportGetNearestTown(const AirportSpec *as, TileIndex tile, const TileIte /** Recalculate the noise generated by the airports of each town */ void UpdateAirportsNoise() { + if (_town_noise_no_update) return; + for (Town *t : Town::Iterate()) t->noise_reached = 0; for (const Station *st : Station::Iterate()) {