Repository navigation
Expand file tree
/
Copy pathtest_savegame.cpp
More file actions
121 lines (112 loc) · 5.32 KB
/
Copy pathtest_savegame.cpp
File metadata and controls
121 lines (112 loc) · 5.32 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Round-trip test for the original .GAM save-game format (savegame.{h,cpp};
// writer 0xB676, loader 0xBA96). Generates a level, runs the transcribed sim,
// saves, reloads into a fresh world and verifies the restored state, plus the
// loader's header/size/checksum rejection paths.
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include "src/world.h"
#include "src/savegame.h"
static int fails = 0;
#define CHECK(cond) do { if (!(cond)) { std::fprintf(stderr, "FAIL %s:%d: %s\n", __FILE__, __LINE__, #cond); ++fails; } } while (0)
static void run_ticks(World& w, int n) {
for (int i = 0; i < n; ++i) {
w.sim.flag478E = (uint16_t)(w.sim.flag478E == 0 ? 1 : 0);
pop::sim_execute_orders(w.sim);
pop::sim_tick(w.sim);
}
}
int main() {
const std::string path = "test_savegame.GAM";
World w;
std::memcpy(w.traitRec, "\x07\x09\x02\x3f\x00\x03\x03\x03", 8); // LEVEL0 record 3
w.traitRec[8] = 0x2D; w.traitRec[9] = 0;
w.aiCounter = 7; w.aiPeriod = 9; w.aiPowerMask = 2; w.powerMask = 0x3F;
w.levelFlags = 0; w.startPop[0] = w.startPop[1] = 3;
w.landscape = 0x2D + 1;
w.deityRank = 0x123; // [0x420E] slot
w.generate(w.landscape);
run_ticks(w, 60);
CHECK(pop::save_game(w, 15, path));
{ FILE* f = std::fopen(path.c_str(), "rb");
CHECK(f != nullptr);
std::fseek(f, 0, SEEK_END);
CHECK((size_t)std::ftell(f) == pop::SAVE_GAME_SIZE); // 0x7C00 exactly
std::fclose(f); }
World v;
int lvl = -1;
CHECK(pop::load_game(v, lvl, path));
CHECK(lvl == 15);
CHECK(std::memcmp(v.sim.heightMap, w.sim.heightMap, sizeof w.sim.heightMap) == 0);
CHECK(std::memcmp(v.sim.grid1FDE, w.sim.grid1FDE, sizeof w.sim.grid1FDE) == 0);
CHECK(std::memcmp(v.sim.tile, w.sim.tile, sizeof w.sim.tile) == 0);
CHECK(std::memcmp(v.sim.overlay, w.sim.overlay, sizeof w.sim.overlay) == 0);
CHECK(std::memcmp(v.sim.occ, w.sim.occ, sizeof w.sim.occ) == 0);
for (int t = 0; t < 2; ++t) {
CHECK(v.sim.tribe[t].leader == w.sim.tribe[t].leader);
CHECK(v.sim.tribe[t].building == w.sim.tribe[t].building);
CHECK(v.sim.tribe[t].strengthSum == w.sim.tribe[t].strengthSum);
CHECK(v.sim.tribe[t].manaSum == w.sim.tribe[t].manaSum);
// loader fixup 0xBDB2: pending command bytes cleared
CHECK(v.sim.desc[t].cmd == 0 && v.sim.desc[t].x == 0 && v.sim.desc[t].y == 0);
CHECK(v.sim.desc[t].counter == w.sim.desc[t].counter);
CHECK(v.sim.desc[t].goalFlags == w.sim.desc[t].goalFlags);
CHECK(v.sim.desc[t].period == w.sim.desc[t].period);
}
CHECK(v.sim.objCount == w.sim.objCount); // fixup 0xBD8C rescan
for (int i = 0; i < (int)w.sim.objCount; ++i) { // records match (target
const pop::ObjRecord& a = w.sim.obj[i]; // is re-resolved, 0xBDCF)
const pop::ObjRecord& b = v.sim.obj[i];
CHECK(a.type == b.type && a.owner == b.owner && a.strength == b.strength &&
a.cell == b.cell && a.rank == b.rank && a.frame == b.frame);
}
CHECK(v.sim.rng == w.sim.rng);
CHECK(v.sim.tick == w.sim.tick);
CHECK(v.sim.curTribe == w.sim.curTribe);
CHECK(v.sim.level == w.sim.level);
CHECK(v.sim.levelFlags == w.sim.levelFlags);
CHECK(v.sim.killCount[0] == w.sim.killCount[0] && v.sim.killCount[1] == w.sim.killCount[1]);
CHECK(v.landscape == w.landscape);
CHECK(v.deityRank == 0x123);
CHECK(std::memcmp(v.traitRec, w.traitRec, 10) == 0);
CHECK(v.aiCounter == 7 && v.aiPeriod == 9 && v.aiPowerMask == 2);
// the loaded world must keep simulating without tripping anything
run_ticks(v, 30);
CHECK(v.sim.objCount > 0);
// rejection: flipped payload byte -> checksum mismatch (0xBCEB verify)
{ std::vector<uint8_t> img(pop::SAVE_GAME_SIZE);
FILE* f = std::fopen(path.c_str(), "rb");
CHECK(std::fread(img.data(), 1, img.size(), f) == img.size());
std::fclose(f);
img[4 + 100] ^= 0xFF;
f = std::fopen(path.c_str(), "wb");
std::fwrite(img.data(), 1, img.size(), f); std::fclose(f);
World r; int l2;
CHECK(!pop::load_game(r, l2, path));
img[4 + 100] ^= 0xFF; // restore, corrupt header
img[0] ^= 1;
f = std::fopen(path.c_str(), "wb");
std::fwrite(img.data(), 1, img.size(), f); std::fclose(f);
CHECK(!pop::load_game(r, l2, path));
f = std::fopen(path.c_str(), "wb"); // truncated file
img[0] ^= 1;
std::fwrite(img.data(), 1, img.size() - 8, f); std::fclose(f);
CHECK(!pop::load_game(r, l2, path)); }
// pause/paint gate (0x884/0x88B on [0xCBC6]/[0xCBD0]): World::tick must
// not advance the sim while paused or in paint mode; it resumes after
{ uint16_t t0 = w.sim.tick;
w.sim.simLock = 1;
w.tick(0.15f); w.tick(0.15f);
CHECK(w.sim.tick == t0); // frozen while paused
w.sim.simLock = 0; w.sim.freeMode = 1;
w.tick(0.15f);
CHECK(w.sim.tick == t0); // frozen while painting
w.sim.freeMode = 0;
w.tick(0.15f);
CHECK(w.sim.tick == t0 + 1); }
std::remove(path.c_str());
if (fails) { std::fprintf(stderr, "%d check(s) failed\n", fails); return 1; }
std::puts("savegame: all checks passed");
return 0;
}