aboutsummaryrefslogtreecommitdiffstats
path: root/tools/sim_check.cpp
blob: 449a7c3271870c256ef5c3d16f57b2099a616cca (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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;
}