Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/bridge/bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,15 @@ bool fromWorld(const World& w, Colony& out, std::string* err) {
return setErr(err, "settler state out of range"), false;
for (size_t i = 0; i < c.buildings.size(); ++i)
if (!c.grid.inBounds(c.buildings.x[i], c.buildings.y[i])) return setErr(err, "building out of bounds"), false;
if (!c.grid.inBounds(static_cast<int32_t>(c.globals.stockX), static_cast<int32_t>(c.globals.stockY)))
if (!fits(c.globals.stockX) || !fits(c.globals.stockY) ||
!c.grid.inBounds(static_cast<int32_t>(c.globals.stockX), static_cast<int32_t>(c.globals.stockY)))
return setErr(err, "stockpile out of bounds"), false;
// Settlers::add and Buildings::add throw unless each new id exceeds the last one.
auto ahead = [](int64_t next, const std::vector<int64_t>& ids) {
return next >= 0 && (ids.empty() || next > ids.back());
};
if (!ahead(c.globals.nextSettlerId, c.settlers.id) || !ahead(c.globals.nextBuildingId, c.buildings.id))
return setErr(err, "id counter behind ids"), false;
out = std::move(c);
return true;
}
Expand Down
38 changes: 38 additions & 0 deletions tests/test_saves.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <filesystem>
#include <fstream>
#include <string>
#include <utility>

#include "bridge/bridge.h"
#include "bridge/session.h"
Expand Down Expand Up @@ -75,6 +76,43 @@ int main(int argc, char** argv) {
bad.tables[0].ints[field][0] = -1;
CHECK(!bridge::fromWorld(bad, back, &why));
}
// Id counters must stay ahead of the ids, or the first birth or hut throws.
CHECK(c.settlers.size() > 0 && c.buildings.size() > 0);
const std::pair<const char*, int64_t> lagging[] = {
{"colony:nextSettler", c.settlers.id.back()}, // equal to the last settler id
{"colony:nextSettler", c.settlers.id.back() - 1}, // behind it
{"colony:nextBuilding", c.buildings.id.back()},
{"colony:nextBuilding", -1},
};
for (const auto& [key, value] : lagging) {
bad = bridge::toWorld(c);
bad.globals[key] = value;
why.clear();
CHECK(!bridge::fromWorld(bad, back, &why));
CHECK_MSG(why == "id counter behind ids", why);
}
{ // with no settlers left, a negative counter would still hand out a negative id
Colony empty = c;
while (empty.settlers.size() > 0) empty.settlers.remove(empty.settlers.size() - 1);
CHECK_MSG(bridge::fromWorld(bridge::toWorld(empty), back, &why), why);
bad = bridge::toWorld(empty);
bad.globals["colony:nextSettler"] = -5;
why.clear();
CHECK(!bridge::fromWorld(bad, back, &why));
CHECK_MSG(why == "id counter behind ids", why);
}
// The stockpile is checked at its full 64 bits: off the map, and off the map by
// exactly 2^32 (which a 32-bit cast would wrap back onto the map).
for (const char* key : {"colony:stockX", "colony:stockY"}) {
const int64_t onMap = std::string(key) == "colony:stockX" ? c.globals.stockX : c.globals.stockY;
for (int64_t value : {int64_t{9999}, onMap + (int64_t{1} << 32), onMap - (int64_t{1} << 32)}) {
bad = bridge::toWorld(c);
bad.globals[key] = value;
why.clear();
CHECK(!bridge::fromWorld(bad, back, &why));
CHECK_MSG(why == "stockpile out of bounds", why);
}
}
}

SECTION("1,000-tick soak: save every 100 ticks, reload, exact, keep playing from the reload");
Expand Down
Loading