Repository navigation
Expand file tree
/
Copy pathtest_obj_commands.cpp
More file actions
170 lines (148 loc) · 8.78 KB
/
Copy pathtest_obj_commands.cpp
File metadata and controls
170 lines (148 loc) · 8.78 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Verifies obj_command() reproduces the byte effects of the handlers in
// decomp/populous.asm. Build: g++ -std=c++17 -I src test_obj_commands.cpp src/obj_commands.cpp
#include "obj_commands.h"
#include <cstdio>
using namespace pop;
static int fails = 0;
#define CHECK(cond) do { if (!(cond)) { std::printf("FAIL %s:%d %s\n", __FILE__, __LINE__, #cond); ++fails; } } while (0)
int main() {
// record layout must match the 32-byte object record exactly
CHECK(offsetof(ObjRecord, type) == 0x00);
CHECK(offsetof(ObjRecord, owner) == 0x01);
CHECK(offsetof(ObjRecord, subtype) == 0x02);
CHECK(offsetof(ObjRecord, strength) == 0x04);
CHECK(offsetof(ObjRecord, cell) == 0x08);
CHECK(offsetof(ObjRecord, facing) == 0x0A);
CHECK(offsetof(ObjRecord, frame) == 0x0B);
CHECK(offsetof(ObjRecord, target) == 0x0C);
CHECK(offsetof(ObjRecord, home) == 0x0E);
CHECK(sizeof(ObjRecord) == 32);
// cursor_cell = (originY+cursorY)*64 + originX+cursorX (routine 0x27F2)
{ CmdState s; s.originX = 10; s.originY = 4; s.cursorX = 3; s.cursorY = 3;
CHECK(cursor_cell(s) == (uint16_t)(((4 + 3) << 6) + (10 + 3))); }
// cmd01/02: 32-bit counter inc / dec-if-nonzero (0x21CE / 0x21DC)
{ ObjRecord o{}; CmdState s; s.counter = 0;
obj_command(2, o, s); CHECK(s.counter == 0); // dec at 0 stays 0
obj_command(1, o, s); CHECK(s.counter == 1);
obj_command(1, o, s); CHECK(s.counter == 2);
obj_command(2, o, s); CHECK(s.counter == 1); }
// cmd05: subtype=8 type=0xE (0x21F6)
{ ObjRecord o{}; CmdState s; obj_command(5, o, s);
CHECK(o.type == 0x0E && o.subtype == 8); }
// cmd06 gated on game-mode [0x128E] (0x2206)
{ ObjRecord o{}; CmdState s; s.mode = 0; obj_command(6, o, s);
CHECK(o.type == 0 && o.subtype == 0); // mode off -> no change
s.mode = 1; obj_command(6, o, s);
CHECK(o.type == 0x0E && o.subtype == 6); }
// cmd07/08: subtype=7 type=0xE, owner 0/1 (0x221A / 0x2232)
{ ObjRecord o{}; CmdState s; obj_command(7, o, s);
CHECK(o.type == 0x0E && o.subtype == 7 && o.owner == 0);
obj_command(8, o, s); CHECK(o.owner == 1); }
// cmd10 (0x2254): gated on mode!=0 AND [0x6918]!=1; probes the 2x2 block
// around the cursor, stopping at the first pick that returns 0 (success).
{ ObjRecord o{}; CmdState s; s.mode = 1; s.flag6918 = 0;
s.cursorX = 5; s.cursorY = 6; int calls = 0;
s.pick = [&](int, int) { ++calls; return 1; }; // all misses
obj_command(10, o, s); CHECK(calls == 4);
calls = 0; s.pick = [&](int, int) { ++calls; return 0; }; // first probe hits
obj_command(10, o, s); CHECK(calls == 1);
calls = 0; s.mode = 0; obj_command(10, o, s); CHECK(calls == 0); // mode gate
s.mode = 1; s.flag6918 = 1; obj_command(10, o, s); CHECK(calls == 0); } // [0x6918]==1 gate
// cmd11: [0xCC06]=3 when mode set and [0x6918]!=1 (0x22C0)
{ ObjRecord o{}; CmdState s; s.mode = 1; s.flag6918 = 1;
obj_command(11, o, s); CHECK(s.var_cc06 == 0); // gated off
s.flag6918 = 0; obj_command(11, o, s); CHECK(s.var_cc06 == 3); }
// cmd14: swap saved<->current window origin (0x2302)
{ ObjRecord o{}; CmdState s; s.mode = 1;
s.originX = 20; s.originY = 21; s.savedX = 5; s.savedY = 6;
obj_command(14, o, s);
CHECK(s.originX == 5 && s.originY == 6 && s.savedX == 20 && s.savedY == 21); }
// cmd15: type=5, [+1]=originX+cursorX, [+2]=originY+cursorY (0x2334 -> 0x233A)
{ ObjRecord o{}; CmdState s; s.originX = 30; s.cursorX = 3; s.originY = 12; s.cursorY = 3;
obj_command(15, o, s);
CHECK(o.type == 5 && o.owner == (uint8_t)33 && o.subtype == (uint8_t)15); }
// cmd16: sound(0x12C0,0,2,*), type=3, pos=origin (0x2358 -> 0x236F -> 0x2375)
{ ObjRecord o{}; CmdState s; s.originX = 7; s.originY = 9;
int p0 = -1, p1 = -1, p2 = -1; bool rang = false;
s.sound = [&](int a, int b, int c, int) { p0 = a; p1 = b; p2 = c; rang = true; };
obj_command(16, o, s);
CHECK(rang && p0 == 0x12C0 && p1 == 0 && p2 == 2);
CHECK(o.type == 3 && o.owner == 7 && o.subtype == 9); }
// cmd18: sound(0x12C0,1,2,*), type=0xE subtype=5 (0x23A4)
{ ObjRecord o{}; CmdState s; int p1 = -1, p2 = -1;
s.sound = [&](int, int b, int c, int) { p1 = b; p2 = c; };
obj_command(18, o, s);
CHECK(p1 == 1 && p2 == 2 && o.type == 0x0E && o.subtype == 5); }
// cmd21: sound(0x12C0,0,1,*), type=0xE subtype=3 (0x240E)
{ ObjRecord o{}; CmdState s; int p1 = -1, p2 = -1;
s.sound = [&](int, int b, int c, int) { p1 = b; p2 = c; };
obj_command(21, o, s);
CHECK(p1 == 0 && p2 == 1 && o.type == 0x0E && o.subtype == 3); }
// cmd30-33: type=0xE subtype=1, owner 1/2/3/0 (0x2562/0x2572/0x258A/0x25A2)
{ ObjRecord o{}; CmdState s;
obj_command(30, o, s); CHECK(o.type == 0x0E && o.subtype == 1 && o.owner == 1);
ObjRecord a{}; obj_command(31, a, s); CHECK(a.type == 0x0E && a.subtype == 1 && a.owner == 2);
ObjRecord b{}; obj_command(32, b, s); CHECK(b.type == 0x0E && b.subtype == 1 && b.owner == 3);
ObjRecord c{}; obj_command(33, c, s); CHECK(c.type == 0x0E && c.subtype == 1 && c.owner == 0); }
// cmd24/25/28/29: 0x6D sound-driver far-calls with the traced offset/arg;
// cmd24/25 then take cmd23's tail (0x24BB): cursor = (3,3)
{ ObjRecord o{}; CmdState s; int off = -1, arg = -99;
s.sound6d = [&](int f, int a) { off = f; arg = a; };
obj_command(24, o, s); CHECK(off == 0x1706 && arg == 1);
CHECK(s.cursorX == 3 && s.cursorY == 3);
s.cursorX = s.cursorY = 0;
obj_command(25, o, s); CHECK(off == 0x164C);
CHECK(s.cursorX == 3 && s.cursorY == 3);
s.cursorX = s.cursorY = 0;
obj_command(28, o, s); CHECK(off == 0x1706 && arg == 2);
obj_command(29, o, s); CHECK(off == 0x1706 && arg == 0);
CHECK(s.cursorX == 0 && s.cursorY == 0); } // 28/29 leave the cursor
// cmd68: gfx call on old flag, toggle [0x1FDC], sound(0x17E4,3,0,*) (0x25B2)
{ ObjRecord o{}; CmdState s; s.flag1fdc = 1; int gfxOff = -1, p1 = -1;
s.gfx = [&](int f) { gfxOff = f; };
s.sound = [&](int, int b, int, int) { p1 = b; };
obj_command(68, o, s);
CHECK(gfxOff == 0x88D && s.flag1fdc == 0 && p1 == 3); // was 1 -> 0x88D, toggles to 0
obj_command(68, o, s);
CHECK(gfxOff == 0x8B8 && s.flag1fdc == 1); } // was 0 -> 0x8B8, toggles to 1
// cmd69: toggle [0x4780], sound(0x17E4,4,0,*) (0x25EC)
{ ObjRecord o{}; CmdState s; int p1 = -1;
s.sound = [&](int, int b, int, int) { p1 = b; };
obj_command(69, o, s); CHECK(s.flag4780 == 1 && p1 == 4);
obj_command(69, o, s); CHECK(s.flag4780 == 0); }
// cmd70: sound(0x17E4, local6a, local66, *), subtype=2, type=0xE (0x2602)
{ ObjRecord o{}; CmdState s; s.local6a = 11; s.local66 = 22;
int p1 = -1, p2 = -1; s.sound = [&](int, int b, int c, int) { p1 = b; p2 = c; };
obj_command(70, o, s);
CHECK(p1 == 11 && p2 == 22 && o.subtype == 2 && o.type == 0x0E); }
// unused code -> no-op (shares 0x26AE)
{ ObjRecord o{}; CmdState s; ObjRecord before = o; obj_command(40, o, s);
CHECK(o.type == before.type && o.owner == before.owner); }
// --- state-assignment table cs:0x81A (obj_stateNN) ---
// states 1-4,7 unconditional; 5,6,8..15 gated on game-mode
{ ObjRecord o{}; CmdState s;
obj_state(1, o, s); CHECK(o.type == 7);
obj_state(2, o, s); CHECK(o.type == 9);
obj_state(3, o, s); CHECK(o.type == 8);
obj_state(4, o, s); CHECK(o.type == 0x0A);
obj_state(7, o, s); CHECK(o.type == 0x0D); } // unconditional
{ ObjRecord o{}; CmdState s; s.mode = 0; obj_state(5, o, s);
CHECK(o.type == 0); // gated off
s.mode = 1; obj_state(5, o, s); CHECK(o.type == 0x0B);
obj_state(6, o, s); CHECK(o.type == 0x0C); }
// compound 0xE states set owner + subtype
{ ObjRecord o{}; CmdState s; s.mode = 1;
obj_state(8, o, s); CHECK(o.type == 0x0E && o.owner == 0 && o.subtype == 9);
obj_state(9, o, s); CHECK(o.owner == 1 && o.subtype == 9);
obj_state(10, o, s); CHECK(o.owner == 0 && o.subtype == 0x0A);
obj_state(11, o, s); CHECK(o.owner == 1 && o.subtype == 0x0A);
obj_state(12, o, s); CHECK(o.type == 0x0E && o.subtype == 0x0B);
obj_state(13, o, s); CHECK(o.subtype == 0x0C); }
// state14/15: owner = (classResult==0xE), subtype=0xD
{ ObjRecord o{}; CmdState s; s.mode = 1; s.classResult = 0x0E;
obj_state(14, o, s); CHECK(o.type == 0x0E && o.subtype == 0x0D && o.owner == 1);
ObjRecord o2{}; s.classResult = 3;
obj_state(15, o2, s); CHECK(o2.owner == 0 && o2.subtype == 0x0D); }
std::printf(fails ? "\n%d CHECK(s) FAILED\n" : "all obj_command transcription checks passed\n", fails);
return fails ? 1 : 0;
}