Debian Codegraph @

terps/bocfel/objects.cpp

raw symbols SCIP
1 // Copyright 2009-2024 Chris Spiegel.
2 //
3 // SPDX-License-Identifier: MIT
4
5 #include "objects.h"
6 #include "branch.h"
7 #include "memory.h"
8 #include "process.h"
9 #include "screen.h"
10 #include "types.h"
11 #include "util.h"
12 #include "zterp.h"
13
14 static uint16_t find_object(uint16_t n)
15 {
16 // Use 32-bit arithmetic to detect 16-bit overflow.
17 uint32_t base = header.objects, object = n, addr;
18 int objsize;
19
20 if (zversion <= 3) {
21 ZASSERT(n <= 255, "illegal object %u referenced", static_cast<unsigned int>(n));
22 addr = base + (31 * 2) + (9 * (object - 1));
23 objsize = 9;
24 } else {
25 addr = base + (63 * 2) + (14 * (object - 1));
26 objsize = 14;
27 }
28
29 ZASSERT(addr + objsize < header.static_start, "object %u out of range", static_cast<unsigned int>(n));
30
31 return addr;
32 }
33
34 #define OFFSET_PARENT (zversion <= 3 ? 4 : 6)
35 #define OFFSET_SIBLING (zversion <= 3 ? 5 : 8)
36 #define OFFSET_CHILD (zversion <= 3 ? 6 : 10)
37 #define OFFSET_PROP (zversion <= 3 ? 7 : 12)
38
39 #define parent_of(object) relation(object, OFFSET_PARENT)
40 #define sibling_of(object) relation(object, OFFSET_SIBLING)
41 #define child_of(object) relation(object, OFFSET_CHILD)
42
43 #define set_parent(obj1, obj2) set_relation(obj1, obj2, OFFSET_PARENT)
44 #define set_sibling(obj1, obj2) set_relation(obj1, obj2, OFFSET_SIBLING)
45 #define set_child(obj1, obj2) set_relation(obj1, obj2, OFFSET_CHILD)
46
47 static uint16_t property_address(uint16_t n)
48 {
49 return word(find_object(n) + OFFSET_PROP);
50 }
51
52 static uint16_t relation(uint16_t object, int offset)
53 {
54 return zversion <= 3 ? byte(find_object(object) + offset) : word(find_object(object) + offset);
55 }
56
57 // the 32 attribute flags parent sibling child properties
58 // ---32 bits in 4 bytes--- ---3 bytes------------------ ---2 bytes--
59 //
60 // the 48 attribute flags parent sibling child properties
61 // ---48 bits in 6 bytes--- ---3 words, i.e. 6 bytes---- ---2 bytes--
62 static void set_relation(uint16_t obj1, uint16_t obj2, int offset)
63 {
64 if (zversion <= 3) {
65 store_byte(find_object(obj1) + offset, obj2);
66 } else {
67 store_word(find_object(obj1) + offset, obj2);
68 }
69 }
70
71 static void remove_object(uint16_t object)
72 {
73 uint16_t parent = parent_of(object);
74
75 if (parent != 0) {
76 uint16_t child = child_of(parent);
77 ZASSERT(child != 0, "malformed object table: parent has no children");
78
79 // Direct child
80 if (child == object) {
81 // parent->child = parent->child->sibling
82 set_child(parent, sibling_of(child));
83 } else {
84 while (sibling_of(child) != object) {
85 // child = child->sibling
86 child = sibling_of(child);
87 ZASSERT(child != 0, "malformed object table: object to remove is not a child of its parent");
88 }
89
90 // Now the sibling of child is the object to remove.
91
92 // child->sibling = child->sibling->sibling
93 set_sibling(child, sibling_of(sibling_of(child)));
94 }
95
96 // object->parent = 0
97 set_parent(object, 0);
98
99 // object->sibling = 0
100 set_sibling(object, 0);
101 }
102 }
103
104 static uint16_t property_length(uint16_t propaddr)
105 {
106 uint16_t length;
107 // The address is to the data; the size byte is right before.
108 uint8_t b = user_byte(propaddr - 1);
109
110 if (zversion <= 3) {
111 length = (b >> 5) + 1;
112 } else {
113 if ((b & 0x80) == 0x80) {
114 length = b & 0x3f;
115 if (length == 0) {
116 length = 64;
117 }
118 } else {
119 length = ((b & 0x40) == 0x40) ? 2 : 1;
120 }
121 }
122
123 return length;
124 }
125
126 static uint8_t property_number(uint16_t propaddr)
127 {
128 uint8_t propnum;
129
130 if (zversion <= 3) {
131 propnum = user_byte(propaddr - 1) & 0x1f;
132 } else {
133 if ((user_byte(propaddr - 1) & 0x80) == 0x80) {
134 propnum = user_byte(propaddr - 2) & 0x3f;
135 } else {
136 propnum = user_byte(propaddr - 1) & 0x3f;
137 }
138 }
139
140 return propnum;
141 }
142
143 static uint16_t advance_prop_addr(uint16_t propaddr)
144 {
145 uint8_t size = user_byte(propaddr++);
146 if (size == 0) {
147 return 0;
148 }
149
150 if (zversion >= 4 && (size & 0x80) == 0x80) {
151 propaddr++;
152 }
153
154 return propaddr;
155 }
156
157 static uint16_t first_property(uint16_t object)
158 {
159 uint16_t propaddr = property_address(object);
160
161 propaddr += (2 * user_byte(propaddr)) + 1;
162
163 return advance_prop_addr(propaddr);
164 }
165
166 static uint16_t next_property(uint16_t propaddr)
167 {
168 propaddr += property_length(propaddr);
169
170 return advance_prop_addr(propaddr);
171 }
172
173 #define FOR_EACH_PROPERTY(object, addr) for (uint16_t addr = first_property(object); addr != 0; addr = next_property(addr))
174
175 static bool find_property(uint16_t object, uint16_t propnum, uint16_t &propaddr, uint16_t &proplen)
176 {
177 FOR_EACH_PROPERTY(object, addr) {
178 if (property_number(addr) == propnum) {
179 propaddr = addr;
180 proplen = property_length(addr);
181 return true;
182 }
183 }
184
185 return false;
186 }
187
188 static void check_attr(uint16_t attr)
189 {
190 ZASSERT(attr <= (zversion <= 3 ? 31 : 47), "invalid attribute: %u", static_cast<unsigned int>(attr));
191 }
192
193 static bool is_zero(bool is_store, bool is_jump)
194 {
195 if (zargs[0] == 0) {
196 if (is_store) {
197 store(0);
198 }
199 if (is_jump) {
200 branch_if(false);
201 }
202
203 return true;
204 }
205
206 return false;
207 }
208
209 #define check_zero(store, jump) do { if (is_zero(store, jump)) return; } while (false)
210
211 static void check_propnum(uint16_t propnum)
212 {
213 ZASSERT(propnum > 0 && propnum < (zversion <= 3 ? 32 : 64), "invalid property: %u", static_cast<unsigned int>(propnum));
214 }
215
216 // Attributes are stored at the very beginning of an object, so the
217 // address find_object() returns refers directly to the attributes. The
218 // leftmost bit is attribute 0. Thus these attribute functions need to
219 // find out first which byte of the attributes to look at; this is done
220 // by dividing by 8. Attributes 0-7 will be in byte 0, 8-15 in byte 1,
221 // and so on. Then the particular bit is found. Attributes 0..7 are
222 // bits 7..0, attributes 8..15 are 7..0, and so on. Taking the
223 // remainder of the attribute divided by 8 gives the bit position,
224 // counting from the left, of the attribute.
225 #define ATTR_BIT(num) (0x80U >> ((num) % 8))
226 void ztest_attr()
227 {
228 check_zero(false, true);
229 check_attr(zargs[1]);
230
231 uint16_t addr = find_object(zargs[0]) + (zargs[1] / 8);
232
233 branch_if((byte(addr) & ATTR_BIT(zargs[1])) != 0);
234 }
235
236 void zset_attr()
237 {
238 check_zero(false, false);
239 check_attr(zargs[1]);
240
241 uint16_t addr = find_object(zargs[0]) + (zargs[1] / 8);
242
243 store_byte(addr, byte(addr) | ATTR_BIT(zargs[1]));
244 }
245
246 void zclear_attr()
247 {
248 check_zero(false, false);
249 check_attr(zargs[1]);
250
251 uint16_t addr = find_object(zargs[0]) + (zargs[1] / 8);
252
253 store_byte(addr, byte(addr) & ~ATTR_BIT(zargs[1]));
254 }
255 #undef ATTR_BIT
256
257 void zremove_obj()
258 {
259 check_zero(false, false);
260
261 remove_object(zargs[0]);
262 }
263
264 void zinsert_obj()
265 {
266 check_zero(false, false);
267
268 remove_object(zargs[0]);
269
270 set_sibling(zargs[0], child_of(zargs[1]));
271 set_child(zargs[1], zargs[0]);
272 set_parent(zargs[0], zargs[1]);
273 }
274
275 void zget_sibling()
276 {
277 check_zero(true, true);
278
279 uint16_t sibling = sibling_of(zargs[0]);
280
281 store(sibling);
282 branch_if(sibling != 0);
283 }
284
285 void zget_child()
286 {
287 check_zero(true, true);
288
289 uint16_t child = child_of(zargs[0]);
290
291 store(child);
292 branch_if(child != 0);
293 }
294
295 void zget_parent()
296 {
297 check_zero(true, false);
298
299 store(parent_of(zargs[0]));
300 }
301
302 void zput_prop()
303 {
304 check_zero(false, false);
305 check_propnum(zargs[1]);
306
307 uint16_t propaddr, proplen;
308 bool found = find_property(zargs[0], zargs[1], propaddr, proplen);
309
310 ZASSERT(found, "broken story: no prop");
311
312 if (proplen == 1) {
313 user_store_byte(propaddr, zargs[2] & 0xff);
314 } else if (proplen == 2) {
315 user_store_word(propaddr, zargs[2]);
316 } else {
317 // As with @get_prop below, this is an invalid length, but
318 // Photograph does this. It’s a bug in the game, but it’s pretty
319 // easy to accidentally do in Inform. Modern Inform versions
320 // detect it, but older ones didn’t, so it’s probably a safe
321 // assumption that there are some other older games out there
322 // which will trigger this as well. As with some other
323 // interpreters, store a word for all invalid values.
324 user_store_word(propaddr, zargs[2]);
325 }
326 }
327
328 void zget_prop()
329 {
330 check_zero(true, false);
331 check_propnum(zargs[1]);
332
333 uint16_t propaddr, proplen;
334
335 if (find_property(zargs[0], zargs[1], propaddr, proplen)) {
336 if (proplen == 1) {
337 store(user_byte(propaddr));
338 } else if (proplen == 2) {
339 store(user_word(propaddr));
340 } else {
341 // If the proplen is > 2, the story file is misbehaving. At
342 // least Christminster does this, and Frotz and Nitfol allow
343 // it, reading a word, so do that here.
344 store(user_word(propaddr));
345 }
346 } else {
347 uint16_t i = header.objects + (2 * (zargs[1] - 1));
348 store(word(i));
349 }
350 }
351
352 void zget_prop_len()
353 {
354 // Z-spec 1.1 says @get_prop_len 0 must yield 0.
355 if (zargs[0] == 0) {
356 store(0);
357 } else {
358 store(property_length(zargs[0]));
359 }
360 }
361
362 void zget_prop_addr()
363 {
364 check_zero(true, false);
365
366 // Theoretically this should check whether the requested property is
367 // valid (i.e. is within the proper range for the current story type).
368 // However, at least one game (Mingsheng) attempts to get the address
369 // of an invalid property (115); the standard could be read to
370 // indicate that zero should be returned for *any* invalid property,
371 // so do that.
372
373 uint16_t propaddr, length;
374
375 if (find_property(zargs[0], zargs[1], propaddr, length)) {
376 store(propaddr);
377 } else {
378 store(0);
379 }
380 }
381
382 void zget_next_prop()
383 {
384 check_zero(true, false);
385
386 uint16_t object = zargs[0], propnum = zargs[1], found_propnum = 0;
387 bool next = false;
388
389 FOR_EACH_PROPERTY(object, propaddr) {
390 uint8_t current_propnum = property_number(propaddr);
391
392 if (propnum == 0 || next) {
393 found_propnum = current_propnum;
394 break;
395 }
396
397 if (current_propnum == propnum) {
398 next = true;
399 }
400 }
401
402 store(found_propnum);
403 }
404
405 void zjin()
406 {
407 // @jin 0 0 is not defined, since @jin requires an object (§15) and
408 // object 0 is not actually an object (§12.3). However, many
409 // interpreters yield a true value for this, and Torbjorn Andersson’s
410 // strictz tester expects it to be true, so go with the flow.
411 if (zargs[0] == 0 && zargs[1] == 0) {
412 branch_if(true);
413 return;
414 }
415
416 check_zero(false, true);
417
418 branch_if(parent_of(zargs[0]) == zargs[1]);
419 }
420
421 void print_object(uint16_t obj, void (*outc)(uint8_t))
422 {
423 if (obj == 0) {
424 return;
425 }
426
427 print_handler(property_address(obj) + 1, outc);
428 }
429
430 void zprint_obj()
431 {
432 check_zero(false, false);
433
434 print_object(zargs[0], nullptr);
435 }