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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136// Numeric verification of the build tools (ui/tools.hpp): zoning writes the right nibble and charges// funds only on success; legality gates (occupied / water / non-flat / funds) reject; bulldoze clears a// whole multi-tile structure. Synthetic city — no assets needed.#include "sc2k/ui/tools.hpp"#include <cstdio>
using namespace sc2k;
#include "test_harness.hpp"
int main() { City c; c.set_funds(1000);
// Zone R on a flat, dry, empty tile: nibble 1, $5 charged. auto r = ui::apply_tool(c, ui::Tool::ZoneR, 10, 10); CHECK(r.ok); CHECK((c.xzon.at(10, 10) & 0x0F) == 1); CHECK(c.funds() == 995); // Zone C / I write 3 / 5. ui::apply_tool(c, ui::Tool::ZoneC, 10, 11); ui::apply_tool(c, ui::Tool::ZoneI, 10, 12); CHECK((c.xzon.at(10, 11) & 0x0F) == 3); CHECK((c.xzon.at(10, 12) & 0x0F) == 5);
// Re-zoning the same nibble is a no-op (no charge). const int before = c.funds(); CHECK(!ui::apply_tool(c, ui::Tool::ZoneR, 10, 10).ok); CHECK(c.funds() == before);
// Legality: occupied tile, water tile, sloped tile all reject (no charge). c.xbld.at(20, 20) = 0x80; // a building CHECK(!ui::apply_tool(c, ui::Tool::ZoneR, 20, 20).ok); c.xbit.at(21, 21) = 0x04; // water CHECK(!ui::apply_tool(c, ui::Tool::ZoneR, 21, 21).ok); c.xter.at(22, 22) = 0x01; // a slope CHECK(!ui::apply_tool(c, ui::Tool::ZoneR, 22, 22).ok); CHECK(c.funds() == before);
// Bulldoze a 1x1 building on flat land: razed to rubble (XBLD 1..4, as the original) + $1. c.xbld.at(30, 30) = 0x80; const int f0 = c.funds(); auto b = ui::apply_tool(c, ui::Tool::Bulldoze, 30, 30); CHECK(b.ok); CHECK(c.xbld.at(30, 30) >= 1 && c.xbld.at(30, 30) <= 4); // building → rubble (not bare) CHECK(c.funds() == f0 - 1);
// Bulldoze a 2x2 structure (code 0x90, BSIZE 2) from any of its cells razes all four to rubble. for (int dr = 0; dr < 2; ++dr) for (int dc = 0; dc < 2; ++dc) c.xbld.at(40 + dr, 40 + dc) = 0x90; CHECK(ui::apply_tool(c, ui::Tool::Bulldoze, 41, 41).ok); // click the SE cell int razed = 0; for (int dr = 0; dr < 2; ++dr) for (int dc = 0; dc < 2; ++dc) { const int v = c.xbld.at(40 + dr, 40 + dc); razed += (v >= 1 && v <= 4); // every cell is rubble now } CHECK(razed == 4);
// Re-bulldoze rubble: the original bulldozer clears rubble (XBLD 1-4) to bare ground, // so a razed building can be cleared a second time (previously stuck as "nothing to bulldoze"). c.set_funds(1000); CHECK(c.xbld.at(30, 30) >= 1 && c.xbld.at(30, 30) <= 4); // still rubble from above auto rb = ui::apply_tool(c, ui::Tool::Bulldoze, 30, 30); CHECK(rb.ok); CHECK(c.xbld.at(30, 30) == 0); // rubble → bare ground
// Bulldozing KEEPS the zone designation (the bulldozer never writes XZON). Zone a tile, grow a // building on it, bulldoze: the zone nibble survives so the lot can redevelop. c.xzon.at(32, 32) = std::uint8_t((c.xzon.at(32, 32) & 0xF0) | 1); // R zone c.xbld.at(32, 32) = 0x80; // a house CHECK(ui::apply_tool(c, ui::Tool::Bulldoze, 32, 32).ok); CHECK((c.xzon.at(32, 32) & 0x0F) == 1); // zone PRESERVED (was wiped before) CHECK(c.xbld.at(32, 32) >= 1 && c.xbld.at(32, 32) <= 4); // building → rubble
// Radioactivity (XBLD 5) can't be cleared (faithful to `if (v3 == 5) return 0`). c.xbld.at(33, 33) = 5; CHECK(!ui::apply_tool(c, ui::Tool::Bulldoze, 33, 33).ok); CHECK(c.xbld.at(33, 33) == 5);
// Trees are secondary — no "occupied" block. A NETWORK overwrites the tree; a ZONE keeps it (the original's zoning // only writes the XZON nibble; the tree is cleared later when the lot develops, not on zoning). c.set_funds(1000); c.xbld.at(70, 70) = 7; // a tree CHECK(ui::apply_tool(c, ui::Tool::Road, 70, 70).ok); CHECK(ui::detail::is_road(c.xbld.at(70, 70))); // road placed, tree gone c.xbld.at(71, 71) = 9; // a tree on flat dry land CHECK(ui::apply_tool(c, ui::Tool::ZoneR, 71, 71).ok); CHECK((c.xzon.at(71, 71) & 0x0F) == 1 && c.xbld.at(71, 71) == 9); // zoned; tree KEPT (cleared on development)
// Bulldoze nothing → reject. Insufficient funds → reject. CHECK(!ui::apply_tool(c, ui::Tool::Bulldoze, 50, 50).ok); // empty, unzoned c.set_funds(0); c.xbld.at(51, 51) = 0x80; CHECK(!ui::apply_tool(c, ui::Tool::Bulldoze, 51, 51).ok); // 0 < cost 1 CHECK(c.xbld.at(51, 51) == 0x80); // untouched
// Zoning rules: zonable over empty / rubble / trees / POWER LINES; blocked over roads, rail, // buildings, slopes, water, military. Existing zones can be re-painted; de-zone clears the nibble + rubble. { City z; z.set_funds(100000); CHECK(ui::zone_applicable(z, 10, 10, ui::Tool::ZoneR)); // empty flat dry → ok z.xbld.at(10, 11) = 3; CHECK(ui::zone_applicable(z, 10, 11, ui::Tool::ZoneR)); // rubble → ok z.xbld.at(10, 12) = 0x0E; CHECK(ui::zone_applicable(z, 10, 12, ui::Tool::ZoneR)); // power line → ok z.xbld.at(10, 13) = 0x1D; CHECK(!ui::zone_applicable(z, 10, 13, ui::Tool::ZoneR));// road → blocked z.xbld.at(10, 14) = 0x80; CHECK(!ui::zone_applicable(z, 10, 14, ui::Tool::ZoneR));// building → blocked z.xter.at(10, 15) = 1; CHECK(!ui::zone_applicable(z, 10, 15, ui::Tool::ZoneR)); // slope → blocked z.xbit.at(10, 16) = 4; CHECK(!ui::zone_applicable(z, 10, 16, ui::Tool::ZoneR)); // water → blocked // can zone over UNDERGROUND (pipe/subway in XUND) with no issue z.xund.at(10, 17) = 16; CHECK(ui::zone_applicable(z, 10, 17, ui::Tool::ZoneR)); // apply R, then re-paint C over it (existing zone overwritten), then it's a no-op if already C CHECK(ui::apply_tool(z, ui::Tool::ZoneR, 20, 20).ok && (z.xzon.at(20, 20) & 0xF) == 1); CHECK(ui::apply_tool(z, ui::Tool::ZoneC, 20, 20).ok && (z.xzon.at(20, 20) & 0xF) == 3); // over existing zone CHECK(!ui::apply_tool(z, ui::Tool::ZoneC, 20, 20).ok); // same nibble → no-op, no charge // de-zone clears the nibble AND any rubble z.xbld.at(20, 20) = 2; // rubble left on the zoned lot CHECK(ui::apply_tool(z, ui::Tool::Dezone, 20, 20).ok); CHECK((z.xzon.at(20, 20) & 0xF) == 0 && z.xbld.at(20, 20) == 0); // unzoned + rubble cleared }
if (failures == 0) { std::printf("tools: all checks passed\n"); return 0; } std::printf("tools: %d failure(s)\n", failures); return 1;}