Something went wrong. Try again.
[Mirror] Libreopolis is a cross-platform city simulation game fully compatible with SimCity 2000. gitlab.com/conasg/libreopolis
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174// sc2k/sim/coverage.hpp — the police/fire coverage STENCIL: the tail of the original's map-statistics// pass — a per-station spread kernel over a saturating deposit. Each ACTIVE police// (XBLD 0xD2) / fire (0xD3) station spreads a strength field into XPLC / XFIR (32×32 quarter-res, bytes,// saturating 0..255). XPLC feeds crime (`crime -= XPLC/2`, crime.hpp); XFIR feeds the fire-disaster// firefighting roll (disasters.hpp reads XFIR[i/4][j/4]). Strength scales with the funding level and is// halved when the station isn't powered/connected (XBIT 0x40 clear). Plus the Neighborhood-Watch /// Volunteer-Fire ordinance overlays (+2 per developed building tile). Falloff (verbatim, integer):// centre, 4/5, ×3/4, ×2/3, /2 = 100 / 80 / 60 / 40 / 20 %.#pragma once#include "sc2k/city.hpp"#include "sc2k/sim/operating_efficiency.hpp"#include "sc2k/tile_codes.hpp"#include <algorithm>#include <array>#include <cstdint>#include <optional>#include <span>#include <utility>#include <vector>
namespace sc2k {
using CoverageField = std::vector<std::uint8_t>;
// Saturating add of `v` into the 32×32 byte field at (r,c); out-of-range cells dropped.template <class Field>inline void coverage_deposit(Field& f, int dim, int r, int c, int v) { if (r < 0 || c < 0 || r >= dim || c >= dim) return; const int s = int(f[std::size_t(r) * dim + c]) + v; f[std::size_t(r) * dim + c] = std::uint8_t(s < 0 ? 0 : s > 255 ? 255 : s);}
// Spread `strength` from a station at quarter-res cell (r,c) over its 7×7 footprint, with// the fixed concentric falloff (each ring computed from the previous, integer-truncated).// clang-format offtemplate <class Field>inline void coverage_spread(Field& f, int dim, int r, int c, int strength) { coverage_deposit(f, dim, r, c, strength); const int ortho = 4 * strength / 5; // 4 orthogonal d1 (80%) coverage_deposit(f, dim, r - 1, c, ortho); coverage_deposit(f, dim, r + 1, c, ortho); coverage_deposit(f, dim, r, c - 1, ortho); coverage_deposit(f, dim, r, c + 1, ortho); const int diag = 3 * ortho / 4; // 4 diagonal d1 (60%) coverage_deposit(f, dim, r - 1, c - 1, diag); coverage_deposit(f, dim, r + 1, c - 1, diag); coverage_deposit(f, dim, r - 1, c + 1, diag); coverage_deposit(f, dim, r + 1, c + 1, diag); int ring = 2 * diag / 3; // 12 ring-2 cells (40%) for (const auto& d : {std::pair<int, int>{-2,-1},{-2,0},{-2,1},{2,-1},{2,0},{2,1}, {-1,-2},{0,-2},{1,-2},{-1,2},{0,2},{1,2}}) coverage_deposit(f, dim, r + d.first, c + d.second, ring); ring /= 2; // 16 ring-3 cells (20%) for (const auto& d : {std::pair<int, int>{-3,-1},{-3,0},{-3,1},{3,-1},{3,0},{3,1}, {-1,-3},{0,-3},{1,-3},{-1,3},{0,3},{1,3},{-2,-2},{-2,2},{2,-2},{2,2}})// clang-format on coverage_deposit(f, dim, r + d.first, c + d.second, ring);}
// Fill XPLC + XFIR from the city's active stations. Efficiency is the shared fixed-point operating// contract; `prisonBonus` is the prison-ordinance bonus to police reach.inline void police_fire_coverage(City& c, int policeEfficiencyBasisPoints, int fireEfficiencyBasisPoints, int prisonBonus) { std::fill(c.xplc.cells.begin(), c.xplc.cells.end(), 0); std::fill(c.xfir.cells.begin(), c.xfir.cells.end(), 0); const int dim = c.dimension(), quarter = dim / 4; const std::uint32_t ord = c.ordinances(); for (int i = 1; i < dim - 1; ++i) for (int j = 1; j < dim - 1; ++j) { const int code = c.xbld.at(i, j), qi = i / 4, qj = j / 4; if (xbld::is_developed(code) && code < xbld::kPlantFirst) { // developed building: ordinance overlays if (ord & 0x800u) coverage_deposit(c.xplc.cells, quarter, qi, qj, 2); // Neighborhood Watch if (ord & 0x10u) coverage_deposit(c.xfir.cells, quarter, qi, qj, 2); // Volunteer Fire Dept continue; } if (code < 0xC6) continue; // not a service building if (!(c.xzon.at(i, j) & 0x80)) continue; // station must be active (XZON bit7) const bool powered = (c.xbit.at(i, j) & 0x40) != 0; if (code == 0xD2) { // police station int s = sim::scale_by_operating_efficiency( 50 * (prisonBonus + 5), policeEfficiencyBasisPoints); if (!powered) s /= 2; coverage_spread(c.xplc.cells, quarter, qi, qj, s); } else if (code == 0xD3) { // fire station int s = sim::scale_by_operating_efficiency(250, fireEfficiencyBasisPoints); if (!powered) s /= 2; coverage_spread(c.xfir.cells, quarter, qi, qj, s); } }}
namespace sim {
enum class ServiceCoverageKind : std::uint8_t { Police, Fire,};
enum class ServiceCoverageDiagnosticStatus : std::uint8_t { Ready, InvalidGeometry,};
// Borrowed, discardable publication of the exact committed fields consumed by crime and firefighting.// Coverage is quarter-resolution: every diagnostic cell describes one 4x4 block of full map tiles.struct ServiceCoverageDiagnostics { ServiceCoverageDiagnosticStatus status = ServiceCoverageDiagnosticStatus::InvalidGeometry; std::uint32_t fullRows = 0; std::uint32_t fullColumns = 0; std::uint32_t quarterRows = 0; std::uint32_t quarterColumns = 0; std::span<const std::uint8_t> police; std::span<const std::uint8_t> fire;
[[nodiscard]] constexpr bool ok() const noexcept { return status == ServiceCoverageDiagnosticStatus::Ready; }
friend bool operator==(const ServiceCoverageDiagnostics& left, const ServiceCoverageDiagnostics& right) noexcept { return left.status == right.status && left.fullRows == right.fullRows && left.fullColumns == right.fullColumns && left.quarterRows == right.quarterRows && left.quarterColumns == right.quarterColumns && std::equal(left.police.begin(), left.police.end(), right.police.begin(), right.police.end()) && std::equal(left.fire.begin(), left.fire.end(), right.fire.begin(), right.fire.end()); }};
inline ServiceCoverageDiagnostics service_coverage_diagnostics( const City& city) noexcept { ServiceCoverageDiagnostics diagnostics; const int full = city.dimension(); const int quarter = full / 4; if (full <= 0 || full % 4 != 0 || city.xplc.width != quarter || city.xplc.height != quarter || city.xfir.width != quarter || city.xfir.height != quarter || city.xplc.cells.size() != std::size_t(quarter) * quarter || city.xfir.cells.size() != std::size_t(quarter) * quarter) return diagnostics; diagnostics.status = ServiceCoverageDiagnosticStatus::Ready; diagnostics.fullRows = static_cast<std::uint32_t>(full); diagnostics.fullColumns = static_cast<std::uint32_t>(full); diagnostics.quarterRows = static_cast<std::uint32_t>(quarter); diagnostics.quarterColumns = static_cast<std::uint32_t>(quarter); diagnostics.police = city.xplc.cells; diagnostics.fire = city.xfir.cells; return diagnostics;}
inline std::optional<std::uint8_t> service_coverage_at( const ServiceCoverageDiagnostics& diagnostics, ServiceCoverageKind kind, std::uint32_t fullRow, std::uint32_t fullColumn) noexcept { if (!diagnostics.ok() || fullRow >= diagnostics.fullRows || fullColumn >= diagnostics.fullColumns) return std::nullopt; const std::span<const std::uint8_t> field = kind == ServiceCoverageKind::Police ? diagnostics.police : diagnostics.fire; const std::size_t index = std::size_t(fullRow >> 2) * diagnostics.quarterColumns + (fullColumn >> 2); if (index >= field.size()) return std::nullopt; return field[index];}
} // namespace sim
} // namespace sc2k