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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252// Persisted service-disruption identity from state through both release map presentations.#include "sc2k/render/asset_loader.hpp"#include "sc2k/render/iso_projection.hpp"#include "sc2k/render/minimap.hpp"#include "sc2k/render/overlay_draw.hpp"#include "sc2k/render/rotate.hpp"#include "sc2k/sim/simulation.hpp"#include "sc2k/ui/service_disruption_legend.hpp"
#include <cstdio>
#include "test_harness.hpp"#include "test_paths.hpp"
using namespace sc2k;
namespace {
void set_episode(sim::ServiceDisruptionState& state, sim::ServiceDisruptionKind kind, sim::ServiceDisruptionPhase phase) { sim::ServiceDisruptionEpisode& episode = state.episodes[static_cast<std::size_t>(kind)]; episode.phase = phase; episode.cause = sim::ServiceDisruptionCause::Underfunding; episode.phaseStartedPeriod = state.lastProcessedPeriod; episode.activeIntervals = 0;}
sim::ServiceDisruptionState episode_fixture() { sim::ServiceDisruptionState state; state.lastProcessedPeriod = 42; set_episode(state, sim::ServiceDisruptionKind::Bus, sim::ServiceDisruptionPhase::Warning); set_episode(state, sim::ServiceDisruptionKind::Rail, sim::ServiceDisruptionPhase::Active); set_episode(state, sim::ServiceDisruptionKind::Subway, sim::ServiceDisruptionPhase::Active); set_episode(state, sim::ServiceDisruptionKind::Police, sim::ServiceDisruptionPhase::Active); set_episode(state, sim::ServiceDisruptionKind::Fire, sim::ServiceDisruptionPhase::Active); return state;}
void publication_is_validated_and_exact() { const sim::ServiceDisruptionState state = episode_fixture(); const sim::ServiceDisruptionDiagnostics diagnostics = sim::service_disruption_diagnostics(state); CHECK(diagnostics.ok()); CHECK(diagnostics.lastProcessedPeriod == 42); CHECK(diagnostics.episodes == state.episodes); CHECK(sim::service_disruption_diagnostic_episode(diagnostics, sim::ServiceDisruptionKind::Bus) == std::optional{state.episodes[0]}); CHECK(!sim::service_disruption_diagnostic_episode(diagnostics, static_cast<sim::ServiceDisruptionKind>(99)));
sim::ServiceDisruptionState invalid = state; invalid.randomSalt = 0; const sim::ServiceDisruptionDiagnostics rejected = sim::service_disruption_diagnostics(invalid); CHECK(!rejected.ok()); CHECK(!sim::service_disruption_diagnostic_episode(rejected, sim::ServiceDisruptionKind::Bus));
City city{MapSize::Tiny}; sim::MunicipalDebt debt; sim::OperatingBudgetState budget; sim::ServiceDisruptionState boundState = state; Simulation simulation(city, debt, budget, boundState); CHECK(simulation.service_disruption_diagnostics() == diagnostics);}
void spatial_projection_is_truthful() { City city{MapSize::Tiny}; city.xbld.at(5, 5) = xbld::kRoadOverRailTB; city.xbld.at(6, 6) = xbld::kRoadFirst; city.xbld.at(7, 7) = xbld::kRailFirst; city.xund.at(8, 8) = xund::kSubwayFirst; city.xplc.at(2, 2) = 80; // full tiles [8..11] x [8..11] city.xfir.at(2, 2) = 120; city.xbld.at(12, 12) = xbld::kPoliceStation; city.xbld.at(16, 16) = xbld::kFireStation;
const sim::ServiceDisruptionDiagnostics disruptions = sim::service_disruption_diagnostics(episode_fixture()); const sim::ServiceCoverageDiagnostics coverage = sim::service_coverage_diagnostics(city); CHECK(disruptions.ok() && coverage.ok()); const std::uint8_t bus = 1u << static_cast<unsigned>( sim::ServiceDisruptionKind::Bus); const std::uint8_t rail = 1u << static_cast<unsigned>( sim::ServiceDisruptionKind::Rail); const std::uint8_t subway = 1u << static_cast<unsigned>( sim::ServiceDisruptionKind::Subway); const std::uint8_t police = 1u << static_cast<unsigned>( sim::ServiceDisruptionKind::Police); const std::uint8_t fire = 1u << static_cast<unsigned>( sim::ServiceDisruptionKind::Fire);
const auto crossing = render::service_disruption_view_sample( city, disruptions, &coverage, 5, 5); CHECK(crossing && crossing->warningServices == bus); CHECK(crossing && crossing->activeServices == rail); CHECK(crossing && crossing->paletteIndex == 29); const auto road = render::service_disruption_view_sample( city, disruptions, &coverage, 6, 6); CHECK(road && road->warningServices == bus && road->activeServices == 0 && road->paletteIndex == 178); const auto railBed = render::service_disruption_view_sample( city, disruptions, &coverage, 7, 7); CHECK(railBed && railBed->activeServices == rail); const auto subwayBed = render::service_disruption_view_sample( city, disruptions, &coverage, 8, 8); CHECK(subwayBed && (subwayBed->activeServices & subway) != 0); const auto covered = render::service_disruption_view_sample( city, disruptions, &coverage, 9, 9); CHECK(covered && (covered->activeServices & police) != 0); CHECK(covered && (covered->activeServices & fire) != 0); const auto policeStation = render::service_disruption_view_sample( city, disruptions, nullptr, 12, 12); CHECK(policeStation && policeStation->activeServices == police); const auto fireStation = render::service_disruption_view_sample( city, disruptions, nullptr, 16, 16); CHECK(fireStation && fireStation->activeServices == fire); const auto bare = render::service_disruption_view_sample( city, disruptions, &coverage, 20, 20); CHECK(bare && !bare->affected() && bare->paletteIndex == 0); CHECK(!render::service_disruption_view_sample( city, disruptions, &coverage, -1, 0));
sim::ServiceDisruptionDiagnostics invalid; CHECK(!render::service_disruption_view_sample( city, invalid, &coverage, 5, 5));
for (const MapSize size : {MapSize::Tiny, MapSize::Small, MapSize::Medium, MapSize::Large}) { City sized{size}; const int last = sized.dimension() - 1; sized.xbld.at(last, last) = xbld::kRoadFirst; const auto sample = render::service_disruption_view_sample( sized, disruptions, nullptr, last, last); CHECK(sample && sample->warningServices == bus); }}
void map_presentations_are_numerically_identical() { City city{MapSize::Tiny}; city.xbld.at(9, 10) = xbld::kRailFirst; const sim::ServiceDisruptionDiagnostics disruptions = sim::service_disruption_diagnostics(episode_fixture()); const sim::ServiceCoverageDiagnostics coverage = sim::service_coverage_diagnostics(city); const std::uint8_t index = render::view_index(city, render::MapView::Disruptions, 9, 10, nullptr, &coverage, nullptr, nullptr, &disruptions); CHECK(index == 29); CHECK(render::view_index(city, render::MapView::Disruptions, 9, 10) == render::minimap_base_index(city.xbld.at(9, 10), city.xbit.at(9, 10), city.altm.at(9, 10))); const render::Framebuffer minimap = render::render_minimap(city, render::MapView::Disruptions, nullptr, nullptr, &coverage, nullptr, nullptr, &disruptions); CHECK(minimap.at(9, 10) == render::minimap_color(index));
constexpr int rotation = 1; constexpr int zoom = 0; constexpr int camX = 300; constexpr int camY = 100; int viewRow = 0; int viewColumn = 0; render::rotate_index_cw_n(city.dimension(), 9, 10, rotation, viewRow, viewColumn); const render::ScreenPos anchor = render::tile_to_screen( viewRow, viewColumn, 0, zoom, camX, camY); const render::Rgba background{7, 11, 13, 255}; render::Framebuffer software(900, 900); software.clear(background); render::draw_mapview_overlay(software, city, camX, camY, rotation, zoom, render::MapView::Disruptions, 255, 1.f, nullptr, &coverage, nullptr, nullptr, &disruptions); render::Framebuffer gpuChrome(1800, 1800); gpuChrome.clear(background); render::draw_mapview_overlay(gpuChrome, city, camX, camY, rotation, zoom, render::MapView::Disruptions, 255, 2.f, nullptr, &coverage, nullptr, nullptr, &disruptions); const render::Rgba expected = render::minimap_color(index); CHECK(software.at(anchor.x + 4, anchor.y + 2) == expected); CHECK(gpuChrome.at(2 * (anchor.x + 4), 2 * (anchor.y + 2)) == expected);}
void legend_covers_both_phases_and_overlap() { CHECK(i18n::load_locale(render::assets_dir() + "/lang", "en")); City city{MapSize::Tiny}; city.xbld.at(5, 5) = xbld::kRoadOverRailTB; const sim::ServiceDisruptionDiagnostics disruptions = sim::service_disruption_diagnostics(episode_fixture()); const auto hovered = render::service_disruption_view_sample( city, disruptions, nullptr, 5, 5); const ui::ServiceDisruptionLegendContent content = ui::service_disruption_legend_content(&disruptions, hovered); CHECK(content.sample.find(':') != std::string::npos); CHECK(content.sample.find(" · ") != std::string::npos);
const std::string fontPath = render::assets_dir() + "/fonts/NotoSans-Regular.ttf"; if (!exists(fontPath)) return; ui::Font font(fontPath, 15); render::Framebuffer frame(640, 240); const render::Rgba untouched{3, 5, 7, 255}; frame.clear(untouched); const ui::ServiceDisruptionLegendGeometry geometry = ui::draw_service_disruption_legend(frame, font, &disruptions, hovered, 20, 220); CHECK(geometry.width >= 280); CHECK(geometry.height > 3 * font.height()); CHECK(frame.at(0, 0) == untouched); const int top = 220 - geometry.height; const render::ServiceDisruptionViewLegend legend = render::service_disruption_view_legend(); const std::uint8_t expectedIndices[2] = { legend.warningPaletteIndex, legend.activePaletteIndex}; for (int segment = 0; segment < 2; ++segment) { const int sampleX = 20 + geometry.barX + (2 * segment + 1) * geometry.barWidth / 4; const int sampleY = top + geometry.barY + geometry.barHeight / 2; CHECK(frame.at(sampleX, sampleY) == render::minimap_color(expectedIndices[segment])); }}
} // namespace
int main() { publication_is_validated_and_exact(); spatial_projection_is_truthful(); map_presentations_are_numerically_identical(); legend_covers_both_phases_and_overlap();
if (failures == 0) { std::puts("service disruption diagnostics: all checks passed"); return 0; } std::printf("service disruption diagnostics: %d failure(s)\n", failures); return 1;}