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.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233// make_scenario — author a scenario: CITY.SC2 (+ goals + briefing) → CITY.SCN.//// A .SCN is a normal FORM/SCDH city plus three chunks (the SCEN/TEXT/PICT layout, verified against the// original game's scenario loader): TEXT ×2 (0x80.. picker blurb,// 0x81.. in-game briefing), SCEN (the big-endian goal block: scripted disaster type + X/Y, time limit,// win goals) and PICT (a 65×65 palette-indexed thumbnail). This tool reuses save_sc2 for the city body// and splices the scenario chunks in after MISC (where the original puts them), then re-loads its own output// and decodes the goals back as a round-trip check.//// Editing an existing scenario works too: pass a .SCN as input — its goals/briefing/PICT seed the// output and any flag overrides just that field.//// make_scenario IN.SC2|IN.SCN OUT.SCN [flags]// --disaster N scripted disaster type (0 none; 1 fire, 2 flood, 3 riot, 6 quake, 7 tornado,// 8 monster, 9 meltdown, 0xA microwave, 0xB volcano, 0xC firestorm, 0xD mass// riots, 0xE major flood, 0x10 hurricane — decimal or 0x hex)// --at X,Y its scripted epicentre (city tiles, 0..127)// --months N time limit; the countdown to the "voted out" loss// --size N city population goal --res/--com/--ind N sector population goals// --cash N funds EARNED since start --landvalue N total land value goal// --le N --eq N life expectancy / education goals// --pollution/--crime/--traffic N stay-UNDER limits (0 = not tested)// --bld1 ID:COUNT --bld2 ID:COUNT "build COUNT tiles of XBLD ID" goals// --blurb TEXT the scenario-picker description (defaults to the briefing's first line)// --brief TEXT | --brief-file FILE the in-game briefing story// --pict-from F.SCN copy another scenario's thumbnail (else: input's, else blank)#include "sc2k/io/sc2_file.hpp"#include "sc2k/sim/scenario.hpp"#include <cstdio>#include <cstdlib>#include <cstring>#include <fstream>#include <string>#include <vector>
using namespace sc2k;
namespace {
std::vector<std::uint8_t> slurp_file(const std::string& p) { std::ifstream f(p, std::ios::binary); return {std::istreambuf_iterator<char>(f), std::istreambuf_iterator<char>()};}
void put_w(std::vector<std::uint8_t>& o, int v) { o.push_back(std::uint8_t(v >> 8)); o.push_back(std::uint8_t(v));}void put_d(std::vector<std::uint8_t>& o, long v) { io::detail::put_be32(o, std::uint32_t(v)); }
// The SCEN payload: 4-byte 0x80000000 entry header + the 52-byte big-endian goal struct.std::vector<std::uint8_t> encode_scen(const ScenarioGoals& g) { std::vector<std::uint8_t> s; io::detail::put_be32(s, 0x80000000u); put_w(s, g.disasterId); s.push_back(std::uint8_t(g.disasterX)); s.push_back(std::uint8_t(g.disasterY)); put_w(s, g.timeLimitMonths); put_d(s, g.citySize); put_d(s, g.res); put_d(s, g.com); put_d(s, g.ind); put_d(s, g.cash); put_d(s, g.landValue); put_w(s, g.le); put_w(s, g.eq); put_d(s, g.pollutionLimit); put_d(s, g.crimeLimit); put_d(s, g.trafficLimit); s.push_back(std::uint8_t(g.bld1Id)); s.push_back(std::uint8_t(g.bld2Id)); put_w(s, g.bld1Count); put_w(s, g.bld2Count); return s;}
// A TEXT payload: the entry header (0x80.. blurb / 0x81.. extended) + the raw text.std::vector<std::uint8_t> encode_text(std::uint32_t header, const std::string& text) { std::vector<std::uint8_t> t; io::detail::put_be32(t, header); t.insert(t.end(), text.begin(), text.end()); return t;}
// A blank 65×65 PICT (newer-scenario style: no 0xFF row terminators, all index 0).std::vector<std::uint8_t> blank_pict() { std::vector<std::uint8_t> p; io::detail::put_be32(p, 0x80000000u); p.insert(p.end(), {65, 0, 65, 0}); // width/height, LITTLE-endian (unlike the goals) p.insert(p.end(), std::size_t(65) * 65, 0); return p;}
int fail(const char* msg) { std::fprintf(stderr, "make_scenario: %s\n", msg); return 1;}
} // namespace
int main(int argc, char** argv) { if (argc < 3) { std::fprintf(stderr, "usage: make_scenario IN.SC2 OUT.SCN [--disaster N --at X,Y --months N --size N ... ] (see the header comment)\n"); return 2; } const std::string inPath = argv[1], outPath = argv[2];
City city; if (!io::load_sc2(inPath, city).ok) return fail("cannot load the input city"); ScenarioGoals g = city.scen.empty() ? ScenarioGoals{} : parse_scen(city.scen); // editing a .SCN seeds its goals g.active = true; std::string brief = city.briefing, blurb; std::vector<std::uint8_t> pict = city.pict;
for (int i = 3; i < argc; ++i) { const std::string a = argv[i]; auto next = [&]() -> const char* { if (i + 1 >= argc) { std::fprintf(stderr, "make_scenario: %s needs a value\n", a.c_str()); std::exit(2); } return argv[++i]; }; auto num = [&]() -> long { return std::strtol(next(), nullptr, 0); }; // accepts 0x.. too if (a == "--disaster") g.disasterId = int(num()); else if (a == "--at") { const char* v = next(); int x = 0, y = 0; if (std::sscanf(v, "%d,%d", &x, &y) != 2) return fail("--at wants X,Y"); g.disasterX = x; g.disasterY = y; } else if (a == "--months") g.timeLimitMonths = int(num()); else if (a == "--size") g.citySize = num(); else if (a == "--res") g.res = num(); else if (a == "--com") g.com = num(); else if (a == "--ind") g.ind = num(); else if (a == "--cash") g.cash = num(); else if (a == "--landvalue") g.landValue = num(); else if (a == "--le") g.le = int(num()); else if (a == "--eq") g.eq = int(num()); else if (a == "--pollution") g.pollutionLimit = num(); else if (a == "--crime") g.crimeLimit = num(); else if (a == "--traffic") g.trafficLimit = num(); else if (a == "--bld1" || a == "--bld2") { const char* v = next(); int id = 0, n = 0; if (std::sscanf(v, "%d:%d", &id, &n) != 2) return fail("--bldN wants ID:COUNT"); if (a == "--bld1") { g.bld1Id = id; g.bld1Count = n; } else { g.bld2Id = id; g.bld2Count = n; } } else if (a == "--blurb") blurb = next(); else if (a == "--brief") brief = next(); else if (a == "--brief-file") { const auto b = slurp_file(next()); brief.assign(b.begin(), b.end()); } else if (a == "--pict-from") { City donor; if (!io::load_sc2(next(), donor).ok || donor.pict.empty()) return fail("--pict-from: no PICT there"); pict = donor.pict; } else { std::fprintf(stderr, "make_scenario: unknown flag %s\n", a.c_str()); return 2; } } if (brief.empty()) return fail("a briefing is required (--brief / --brief-file)"); if (blurb.empty()) blurb = brief.substr(0, brief.find_first_of("\r\n")); // picker blurb defaults to line 1 if (g.timeLimitMonths <= 0) return fail("--months is required (>0) — without it the scenario can never be lost"); if (pict.empty()) pict = blank_pict();
// City body via the standard saver, then splice the scenario chunks in right after MISC (the original's spot). if (!io::save_sc2(outPath, city).ok) return fail("cannot write the output"); std::vector<std::uint8_t> file = slurp_file(outPath); std::vector<std::uint8_t> extra; io::detail::put_chunk(extra, "TEXT", encode_text(0x80000000u, blurb)); io::detail::put_chunk(extra, "TEXT", encode_text(0x81000000u, brief)); io::detail::put_chunk(extra, "SCEN", encode_scen(g)); io::detail::put_chunk(extra, "PICT", pict); std::size_t at = file.size(); // fallback: append at the end (loaders are order-blind) for (std::size_t cur = 12; cur + 8 <= file.size();) { // walk FORM(8)+SCDH(4) chunk list for MISC's end const std::uint32_t len = (std::uint32_t(file[cur + 4]) << 24) | (file[cur + 5] << 16) | (file[cur + 6] << 8) | file[cur + 7]; const bool isMisc = std::memcmp(file.data() + cur, "MISC", 4) == 0; cur += 8 + len; if (isMisc) { at = cur; break; } } file.insert(file.begin() + long(at), extra.begin(), extra.end()); const std::uint32_t formLen = std::uint32_t(file.size() - 8); // FORM length covers everything after its own header file[4] = std::uint8_t(formLen >> 24); file[5] = std::uint8_t(formLen >> 16); file[6] = std::uint8_t(formLen >> 8); file[7] = std::uint8_t(formLen); std::ofstream out(outPath, std::ios::binary); out.write(reinterpret_cast<const char*>(file.data()), std::streamsize(file.size())); if (!out.good()) return fail("write failed");
// Round-trip: re-load our own output and decode the goals back. City check; if (!io::load_sc2(outPath, check).ok || check.scen.empty()) return fail("round-trip failed: output has no SCEN"); const ScenarioGoals r = parse_scen(check.scen); if (r.disasterId != g.disasterId || r.disasterX != g.disasterX || r.disasterY != g.disasterY || r.timeLimitMonths != g.timeLimitMonths || r.citySize != g.citySize || r.res != g.res || r.com != g.com || r.ind != g.ind || r.cash != g.cash || r.landValue != g.landValue || r.le != g.le || r.eq != g.eq || r.pollutionLimit != g.pollutionLimit || r.crimeLimit != g.crimeLimit || r.trafficLimit != g.trafficLimit || r.bld1Id != g.bld1Id || r.bld1Count != g.bld1Count || r.bld2Id != g.bld2Id || r.bld2Count != g.bld2Count) return fail("round-trip failed: decoded goals differ"); if (check.briefing != brief) return fail("round-trip failed: briefing differs");
std::printf("%s: disaster=%d at (%d,%d), %d months; goals: size=%ld res=%ld com=%ld ind=%ld cash=%ld\n" " landvalue=%ld le=%d eq=%d limits(poll/crime/traffic)=%ld/%ld/%ld bld1=%d:%d bld2=%d:%d\n" " briefing %zuB, blurb %zuB, PICT %zuB — round-trip OK\n", outPath.c_str(), g.disasterId, g.disasterX, g.disasterY, g.timeLimitMonths, g.citySize, g.res, g.com, g.ind, g.cash, g.landValue, g.le, g.eq, g.pollutionLimit, g.crimeLimit, g.trafficLimit, g.bld1Id, g.bld1Count, g.bld2Id, g.bld2Count, brief.size(), blurb.size(), pict.size()); return 0;}