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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191// Newspaper (ui/newspaper.hpp) — the recent-headlines model + story-id text. Verifies the disaster// TYPE→story-id map (transcribed from the original's disaster driver), that every fired story id resolves// to a non-empty headline, the queue cap / recency order, and the Extra!!! (auto-open) set.#include "sc2k/render/asset_loader.hpp"#include "sc2k/ui/newspaper.hpp"#include <cstdio>#include <fstream>#include <set>#include <string>
using namespace sc2k::ui;
#include "test_harness.hpp"
static bool have_file(const std::string& p) { std::ifstream f(p, std::ios::binary); return bool(f);}
int main() { // Story text lives in the string catalog — load English so the checks assert the real text. const std::string adir = sc2k::render::assets_dir(); sc2k::i18n::load_locale(adir + "/lang", "en");
// Disaster TYPE → story id (the original's tail switch), incl. the shared ids (fire/firestorm=22, etc.). CHECK(news_story_for_disaster(1) == 22); // Fire CHECK(news_story_for_disaster(0xC) == 22); // Fire Storm → same story as Fire CHECK(news_story_for_disaster(2) == 23); // Flood CHECK(news_story_for_disaster(0xE) == 23); // Major Flood → Flood CHECK(news_story_for_disaster(3) == 35); // Riot CHECK(news_story_for_disaster(0xD) == 35); // Mass Riots → Riot CHECK(news_story_for_disaster(6) == 27); // Earthquake CHECK(news_story_for_disaster(7) == 26); // Tornado CHECK(news_story_for_disaster(8) == 28); // Monster CHECK(news_story_for_disaster(9) == 29); // Meltdown CHECK(news_story_for_disaster(0xA) == 30); // Microwave CHECK(news_story_for_disaster(0xB) == 31); // Volcano CHECK(news_story_for_disaster(0x10) == 34); // Hurricane CHECK(news_story_for_disaster(0x12) == 24); // Air Crash CHECK(news_story_for_disaster(99) == -1); // unknown
// Every disaster story id (22..35) resolves to a non-empty headline + body. for (int id = 22; id <= 35; ++id) { std::string h, b; CHECK(news_story_text(id, 0, "Testville", h, b) && !h.empty() && !b.empty()); } // Milestone story 3 names the size tier and substitutes the city name. { std::string h, b; CHECK(news_story_text(3, 3, "Testville", h, b)); CHECK(h.find("Testville") != std::string::npos && h.find("Capital") != std::string::npos); } // A blank city name falls back to "the city" (no empty substitution). { std::string h, b; news_story_text(22, 0, "", h, b); CHECK(h.find("the city") != std::string::npos); }
// Extra!!! (auto-open) set = disasters + the milestone; ordinary civic stories are not. CHECK(Newspaper::is_extra(27) && Newspaper::is_extra(3)); CHECK(!Newspaper::is_extra(1) && !Newspaper::is_extra(20));
// Queue: most-recent-first, capped at kMax. Newspaper np; for (int i = 0; i < Newspaper::kMax + 4; ++i) np.push(27, 0, 3000 + i, i % 12, "Testville"); CHECK(int(np.stories.size()) == Newspaper::kMax); CHECK(np.stories.front().year == 3000 + Newspaper::kMax + 3); // newest at the front CHECK(np.hasUnread); // A story id with no text doesn't enqueue. const std::size_t before = np.stories.size(); CHECK(!np.push(200, 0, 3100, 1, "Testville") && np.stories.size() == before);
// ── Masthead: the six real names, the paper pick, era price, Sunday dateline. ── { // Deterministic for a given city + date + pick. const Masthead a = make_masthead("Gotham", 2043, 9, 0); const Masthead b = make_masthead("Gotham", 2043, 9, 0); CHECK(a.title == b.title && a.price == b.price && a.weatherBanner == b.weatherBanner); // The title ends in one of the six real paper names, prefixed by the city or "The". auto endsWithName = [](const std::string& t) { for (int i = 0; i < 6; ++i) { const std::string s = news_paper_name(i); if (t.size() >= s.size() && t.compare(t.size() - s.size(), s.size(), s) == 0) return true; } return false; }; CHECK(endsWithName(a.title)); CHECK(a.title.rfind("The ", 0) == 0 || a.title.rfind("Gotham ", 0) == 0); // A newspaper is always a Sunday edition ("Sunday <n>, Oct 2043", the original's quirk). CHECK(a.dateline.rfind("Sunday ", 0) == 0 && a.dateline.find("Oct 2043") != std::string::npos); // The banners come from the real pools. bool wOk = false, oOk = false; for (int i = 0; i < 6; ++i) { if (a.weatherBanner == news_weather_banner(i)) wOk = true; if (a.opinionBanner == news_opinion_banner(i)) oOk = true; } CHECK(wOk && oOk); // Price scales with the half-century (any of the three phrasings for that era). auto priceEra = [](const Masthead& m) { for (int v = 0; v < 3; ++v) for (int e = 0; e < 5; ++e) if (m.price == news_price(v, e)) return e; return -1; }; CHECK(priceEra(make_masthead("X", 1905, 0, 0)) == 0); // 1900s → a cent CHECK(priceEra(make_masthead("X", 2043, 0, 0)) == 2); // 2000s → a quarter CHECK(priceEra(make_masthead("X", 3000, 0, 0)) == 4); // clamp → five dollars // Your paper doesn't shapeshift: the same city reads the same-named paper month after month… std::set<std::string> titles; for (int mo = 0; mo < 36; ++mo) titles.insert(make_masthead("Gotham", 2000 + mo / 12, mo % 12, unsigned(mo)).title); CHECK(titles.size() == 1); // …an explicit menu pick names that paper… const Masthead t = make_masthead("Gotham", 2043, 9, 0, 4); CHECK(t.title.find("Times") != std::string::npos); // …and different towns get different papers (the six names spread across cities). std::set<std::string> names; for (const char* cn : {"Gotham", "Springfield", "Neopolis", "Bedrock", "Quahog", "Sunnydale", "Kings Row", "Fairview"}) names.insert(make_masthead(cn, 2000, 0, 0).title); CHECK(names.size() >= 3); }
// ── Publish: an edition is a frozen snapshot — later stories don't rewrite an open paper. ── { Newspaper p; p.push(28, 0, 2043, 9, "Gotham"); p.publish("Gotham", 2043, 9, "Fair 60 F", {"Taxes 7%"}); CHECK(p.edition.valid); CHECK(p.edition.stories.size() == 1); CHECK(!p.hasUnread); // reading the paper clears the flag const std::string title = p.edition.mh.title; const std::string wx = p.edition.weatherLine; p.push(31, 0, 2044, 2, "Gotham"); // a volcano erupts AFTER press time CHECK(p.edition.stories.size() == 1); // the printed page doesn't change CHECK(p.edition.mh.title == title && p.edition.weatherLine == wx); CHECK(p.hasUnread); // …but the new story flags a fresh unread // The next printing carries it, lead first. p.publish("Gotham", 2044, 2, "Rain 50 F", {}); CHECK(p.edition.stories.size() == 2 && p.edition.stories[0].id == 31); CHECK(p.edition.photoCaption == p.edition.stories[1].headline); // captioned with ANOTHER story's head // The pick is honoured across the effective-choice path too. p.choice = 2; CHECK(p.effective_choice("anything") == 2); p.choice = -1; CHECK(p.effective_choice("Gotham") == int(news_hash("Gotham") % 6)); }
// ── The press-photo table: mapped plates for the mapped stories, the random pool otherwise. ── CHECK(news_photo_id(22, 0) == 408); // fire → the burning-building plate CHECK(news_photo_id(26, 0) == 400); // tornado CHECK(news_photo_id(34, 0) == 403); // hurricane CHECK(news_photo_id(31, 0) == 411); // volcano for (unsigned s = 0; s < 24; ++s) { // unmapped → always inside the 400-411 pool const int n = news_photo_id(1, s); CHECK(n >= 400 && n <= 411); }
// ── Front page renders an edition into a framebuffer (font-gated). ── { if (have_file(adir + "/fonts/NotoSans-Regular.ttf")) { sc2k::ui::Font font(adir + "/fonts/NotoSans-Regular.ttf"); Newspaper page; page.push(28, 0, 2043, 9, "Gotham"); page.push(20, 0, 2043, 9, "Gotham"); page.push(19, 0, 2043, 9, "Gotham"); page.publish("Gotham", 2043, 9, "Fair 60 F", {"Taxes 7%", "Unemp. 3%"}); sc2k::render::Framebuffer fb(560, 420); fb.clear(sc2k::render::Rgba{0, 0, 0, 0xFF}); draw_front_page(fb, font, 8, 8, 544, 404, page.edition, 0); int ink = 0; for (int y = 0; y < fb.height(); ++y) for (int x = 0; x < fb.width(); ++x) if (fb.at(x, y).r || fb.at(x, y).g || fb.at(x, y).b) ++ink; CHECK(ink > 800); // masthead + stories + corner boxes all painted // The empty paper shows the quiet-presses line, not a crash. Newspaper empty; empty.publish("Gotham", 2043, 9, "", {}); draw_front_page(fb, font, 8, 8, 544, 404, empty.edition, 0); std::printf("test_newspaper: masthead + edition + front page render OK\n"); } else { std::printf("test_newspaper: SKIP render (no font assets)\n"); } }
if (failures == 0) std::printf("test_newspaper: all checks passed\n"); else std::printf("test_newspaper: %d FAILURES\n", failures); return failures ? 1 : 0;}