aboutsummaryrefslogtreecommitdiffstats
path: root/tools/sim_check.cpp
diff options
context:
space:
mode:
authorEmil A. Overbeck <emil.a.overbeck@gmail.com>2026-07-19 04:19:48 +0200
committerEmil A. Overbeck <emil.a.overbeck@gmail.com>2026-07-19 04:19:48 +0200
commit521b5da83485a956811fc510cba9f139acad8b26 (patch)
tree29b210263300479e9a565af568812c5e707d23f7 /tools/sim_check.cpp
parentff85499d5567c36ce63e2d7903d5f872fd9aef1d (diff)
downloadforts_clone-main.tar.gz
forts_clone-main.zip
New maps and map selectormain
Diffstat (limited to 'tools/sim_check.cpp')
-rw-r--r--tools/sim_check.cpp80
1 files changed, 80 insertions, 0 deletions
diff --git a/tools/sim_check.cpp b/tools/sim_check.cpp
new file mode 100644
index 0000000..449a7c3
--- /dev/null
+++ b/tools/sim_check.cpp
@@ -0,0 +1,80 @@
+// Headless map soak test.
+//
+// The GUI needs a display, but the solver does not: build_graph and the Lua
+// loaders have no SDL dependency. This loads every map, settles it under the
+// same fixed-step tick order the game uses, and reports whether the forts stand.
+// Use it to catch "the fort collapses on load" without opening the game.
+//
+// ./builddir/sim_check [seconds]
+//
+// Exit status is non-zero if any map lost struts, so it can gate a commit.
+
+#include "engine/script.hpp"
+#include "game/build_graph.hpp"
+#include "game/data.hpp"
+
+#include <cmath>
+#include <cstdio>
+#include <cstdlib>
+#include <string>
+#include <vector>
+
+int main(int argc, char** argv) {
+ const float FIXED_DT = 1.0f / 60.0f;
+ const float seconds = (argc > 1) ? std::atof(argv[1]) : 5.0f;
+ const int ticks = (int)(seconds / FIXED_DT);
+
+ ScriptEngine script; // ctor brings the Lua state up
+
+ BuildGraph graph;
+ graph.materials = load_materials(script, "data");
+ if (graph.materials.empty()) { fprintf(stderr, "no materials\n"); return 2; }
+ std::vector<MapDef> maps = load_maps(script, "data");
+ if (maps.empty()) { fprintf(stderr, "no maps\n"); return 2; }
+
+ int failures = 0;
+ for (const auto& m : maps) {
+ build_map(m, graph);
+ // Beams the graph refused (duplicate node pair after snapping, or a
+ // degenerate endpoint) silently weaken a fort, so surface them.
+ if ((int)graph.edges.size() != (int)m.beams.size())
+ printf(" %s: %zu beams declared but %zu welded (%zu rejected)\n",
+ m.name.c_str(), m.beams.size(), graph.edges.size(),
+ m.beams.size() - graph.edges.size());
+ const int edges0 = (int)graph.edges.size();
+ const int nodes0 = (int)graph.nodes.size();
+ std::vector<float> y0;
+ for (const auto& n : graph.nodes) y0.push_back(n.y);
+
+ int snapped = 0, killed = 0, first_break = -1;
+ for (int t = 0; t < ticks; t++) {
+ graph.step(FIXED_DT);
+ int s = graph.check_strain();
+ graph.update_timers(FIXED_DT);
+ graph.update_fire(FIXED_DT);
+ int k = graph.kill_grounded();
+ if (s > 0 && first_break < 0) first_break = t;
+ snapped += s;
+ killed += k;
+ }
+
+ // Worst downward drift of any surviving node (sag).
+ float sag = 0.0f;
+ for (size_t i = 0; i < graph.nodes.size() && i < y0.size(); i++)
+ sag = std::max(sag, y0[i] - graph.nodes[i].y);
+
+ const bool ok = (snapped == 0 && killed == 0);
+ if (!ok) failures++;
+ printf("%-16s %s struts %d -> %d, nodes %d -> %d, snapped %d, killed %d",
+ m.name.c_str(), ok ? "STANDS " : "COLLAPSE",
+ edges0, (int)graph.edges.size(),
+ nodes0, (int)graph.nodes.size(), snapped, killed);
+ if (first_break >= 0)
+ printf(", first break at t=%.2fs", first_break * FIXED_DT);
+ printf(", max sag %.3f\n", sag);
+ }
+
+ printf("\n%d/%zu maps stand after %.1fs\n", (int)maps.size() - failures,
+ maps.size(), seconds);
+ return failures == 0 ? 0 : 1;
+}