-
Notifications
You must be signed in to change notification settings - Fork 1
/
op_test.c
431 lines (366 loc) · 9.58 KB
/
op_test.c
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/* Sarien - A Sierra AGI resource interpreter engine
* Copyright (C) 1999-2003 Stuart George and Claudio Matsuoka
*
* $Id: op_test.c,v 1.38 2003/08/26 00:53:16 cmatsuoka Exp $
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; see docs/COPYING for further details.
*/
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "sarien.h"
#include "agi.h"
#include "keyboard.h"
#include "opcodes.h"
static UINT8 test_obj_right (UINT8, UINT8, UINT8, UINT8, UINT8);
static UINT8 test_obj_centre (UINT8, UINT8, UINT8, UINT8, UINT8);
static UINT8 test_obj_in_box (UINT8, UINT8, UINT8, UINT8, UINT8);
static UINT8 test_posn (UINT8, UINT8, UINT8, UINT8, UINT8);
static UINT8 test_said (UINT8, UINT8 *);
static UINT8 test_controller (UINT8);
static UINT8 test_keypressed (void);
static UINT8 test_compare_strings(UINT8, UINT8);
#define ip (game.logics[lognum].cIP)
#define code (game.logics[lognum].data)
#define test_equal(v1,v2) (getvar(v1) == (v2))
#define test_less(v1,v2) (getvar(v1) < (v2))
#define test_greater(v1,v2) (getvar(v1) > (v2))
#define test_isset(flag) (getflag (flag))
#define test_has(obj) (object_get_location (obj) == EGO_OWNED)
#define test_obj_in_room(obj,v) (object_get_location (obj) == getvar (v))
extern int timer_hack; /* For the timer loop in MH1 logic 153 */
static UINT8 test_compare_strings(UINT8 s1, UINT8 s2)
{
char ms1[MAX_STRINGLEN];
char ms2[MAX_STRINGLEN];
int j, k, l;
strcpy(ms1, game.strings[s1]);
strcpy(ms2, game.strings[s2]);
l=strlen(ms1);
for(k=0, j=0; k<l; k++)
{
switch(ms1[k])
{
case 0x20:
case 0x09:
case '-':
case '.':
case ',':
case ':':
case ';':
case '!':
case '\'':
break;
default:
ms1[j++]=toupper(ms1[k]);
break;
}
}
ms1[j]=0x0;
l=strlen(ms2);
for(k=0, j=0; k<l; k++)
{
switch(ms2[k])
{
case 0x20:
case 0x09:
case '-':
case '.':
case ',':
case ':':
case ';':
case '!':
case '\'':
break;
default:
ms2[j++]=toupper(ms2[k]);
break;
}
}
ms2[j]=0x0;
return !strcmp(ms1, ms2);
}
static UINT8 test_keypressed ()
{
int x = game.keypress;
game.keypress = 0;
if (!x) {
int mode = game.input_mode;
game.input_mode = INPUT_NONE;
main_cycle ();
game.input_mode = mode;
}
if (x) _D (_D_WARN "keypress = %02x", x);
return x;
}
static UINT8 test_controller (UINT8 cont)
{
return game.ev_keyp[cont].occured;
}
static UINT8 test_posn (UINT8 n, UINT8 x1, UINT8 y1, UINT8 x2, UINT8 y2)
{
struct vt_entry *v = &game.view_table[n];
UINT8 r;
r = v->x_pos >= x1 && v->y_pos >= y1 &&
v->x_pos <= x2 && v->y_pos <= y2;
/* _D ("(%d,%d) in (%d,%d,%d,%d): %s", v->x_pos, v->y_pos,
x1, y1, x2, y2, r ? "TRUE" : "FALSE"); */
return r;
}
static UINT8 test_obj_in_box (UINT8 n, UINT8 x1, UINT8 y1, UINT8 x2, UINT8 y2)
{
struct vt_entry *v = &game.view_table[n];
return v->x_pos >= x1 &&
v->y_pos >= y1 &&
v->x_pos + v->x_size - 1 <= x2 &&
v->y_pos <= y2;
}
/* if n is in centre of box */
static UINT8 test_obj_centre (UINT8 n, UINT8 x1, UINT8 y1, UINT8 x2, UINT8 y2)
{
struct vt_entry *v = &game.view_table[n];
return v->x_pos + v->x_size / 2 >= x1 &&
v->x_pos + v->x_size / 2 <= x2 &&
v->y_pos >= y1 &&
v->y_pos <= y2;
}
/* if nect N is in right corner */
static UINT8 test_obj_right(UINT8 n, UINT8 x1, UINT8 y1, UINT8 x2, UINT8 y2)
{
struct vt_entry *v = &game.view_table[n];
return v->x_pos + v->x_size - 1 >= x1 &&
v->x_pos + v->x_size - 1 <= x2 &&
v->y_pos >= y1 &&
v->y_pos <= y2;
}
/* When player has entered something, it is parsed elsewhere */
static UINT8 test_said (UINT8 nwords, UINT8 *cc)
{
int c, n = game.num_ego_words;
int z = 0;
if (getflag (F_said_accepted_input) || !getflag (F_entered_cli))
return FALSE;
/* FR:
* I think the reason for the code below is to add some speed....
*
* if (nwords != num_ego_words)
* return FALSE;
*
* In the disco scene in Larry 1 when you type "examine blonde",
* inside the logic is expected ( said("examine", "blonde", "rol") )
* where word("rol") = 9999
*
* According to the interpreter code 9999 means that whatever the
* user typed should be correct, but it looks like code 9999 means that
* if the string is empty at this point, the entry is also correct...
*
* With the removal of this code, the behaviour of the scene was
* corrected
*/
for (c = 0; nwords && n; c++, nwords--, n--) {
z = lohi_getword (cc);
cc += 2;
switch (z) {
case 9999: /* rest of line (empty string counts to...) */
nwords = 1;
break;
case 1: /* any word */
break;
default:
if (game.ego_words[c].id != z)
return FALSE;
break;
}
}
/* The entry string should be entirely parsed, or last word = 9999 */
if (n && z != 9999)
return FALSE;
/* The interpreter string shouldn't be entirely parsed, but next
* word must be 9999.
*/
if (nwords != 0 && lohi_getword(cc) != 9999)
return FALSE;
setflag (F_said_accepted_input, TRUE);
return TRUE;
}
int test_if_code (int lognum)
{
int ec = TRUE;
int retval = TRUE;
UINT8 op = 0;
UINT8 not_test = FALSE;
UINT8 or_test = FALSE;
UINT16 last_ip = ip;
UINT8 p[16] = {0};
while (retval && !game.quit_prog_now) {
#ifdef USE_CONSOLE
if (debug.enabled && (debug.logic0 || lognum))
debug_console (lognum, lTEST_MODE, NULL);
#endif
last_ip = ip;
op = *(code + ip++);
memmove (p, (code + ip), 16);
switch(op) {
case 0xFF: /* END IF, TEST TRUE */
goto end_test;
case 0xFD:
not_test = !not_test;
continue;
case 0xFC: /* OR */
/* if or_test is ON and we hit 0xFC, end of OR, then
* or is STILL false so break.
*/
if (or_test) {
ec = FALSE;
retval = FALSE;
goto end_test;
}
or_test = TRUE;
continue;
case 0x00:
/* return true? */
goto end_test;
case 0x01:
ec = test_equal (p[0], p[1]);
if (p[0] == 11) timer_hack++;
break;
case 0x02:
ec = test_equal (p[0], getvar(p[1]));
if (p[0] == 11 || p[1] == 11) timer_hack++;
break;
case 0x03:
ec = test_less (p[0], p[1]);
if (p[0] == 11) timer_hack++;
break;
case 0x04:
ec = test_less(p[0], getvar(p[1]));
if (p[0] == 11 || p[1] == 11) timer_hack++;
break;
case 0x05:
ec = test_greater (p[0], p[1]);
if (p[0] == 11) timer_hack++;
break;
case 0x06:
ec = test_greater (p[0], getvar (p[1]));
if (p[0] == 11 || p[1] == 11) timer_hack++;
break;
case 0x07:
ec = test_isset (p[0]);
break;
case 0x08:
ec = test_isset (getvar(p[0]));
break;
case 0x09:
ec = test_has (p[0]);
break;
case 0x0A:
ec = test_obj_in_room (p[0], p[1]);
break;
case 0x0B:
ec = test_posn (p[0], p[1], p[2], p[3], p[4]);
break;
case 0x0C:
ec = test_controller (p[0]);
break;
case 0x0D:
ec = test_keypressed ();
break;
case 0x0E:
ec = test_said (p[0], (UINT8*)code + (ip + 1));
ip = last_ip;
ip++; /* skip opcode */
ip += p[0] * 2; /* skip num_words * 2 */
ip++; /* skip num_words opcode */
break;
case 0x0F:
_D(_D_WARN "comparing [%s], [%s]",
game.strings[p[0]], game.strings[p[1]]);
ec = test_compare_strings (p[0], p[1]);
break;
case 0x10:
ec = test_obj_in_box (p[0], p[1], p[2], p[3], p[4]);
break;
case 0x11:
ec = test_obj_centre (p[0], p[1], p[2], p[3], p[4]);
break;
case 0x12:
ec = test_obj_right (p[0], p[1], p[2], p[3], p[4]);
break;
default:
ec = FALSE;
goto end_test;
}
if (op <= 0x12)
ip += logic_names_test[op].num_args;
/* exchange ec value */
if (not_test)
ec = !ec;
/* not is only enabled for 1 test command */
not_test = FALSE;
if (or_test && ec) {
/* a TRUE inside an OR statement passes
* ENTIRE statement scan for end of OR
*/
/* CM: test for opcode < 0xfc changed from 'op' to
* '*(code+ip)', to avoid problem with the 0xfd (NOT)
* opcode byte. Changed a bad ip += ... ip++ construct.
* This should fix the crash with Larry's logic.0 code:
*
* if ((isset(4) ||
* !isset(2) ||
* v30 == 2 ||
* v30 == 1)) {
* goto Label1;
* }
*
* The bytecode is:
* ff fc 07 04 fd 07 02 01 1e 02 01 1e 01 fc ff
*/
/* find end of OR */
while (*(code+ip) != 0xFC) {
if (*(code + ip) == 0x0E) { /* said */
ip++;
/* cover count + ^words */
ip += 1 + ((*(code + ip)) * 2);
continue;
}
if (*(code + ip) < 0xFC)
ip += logic_names_test[*(code +
ip)].num_args;
ip++;
}
ip++;
or_test = FALSE;
retval = TRUE;
} else {
retval = or_test ? retval || ec : retval && ec;
}
}
end_test:
/* if false, scan for end of IP? */
if (retval)
ip += 2;
else {
ip = last_ip;
while (*(code + ip) != 0xff) {
if (*(code + ip) == 0x0e) {
ip++;
ip += (*(code + ip)) * 2 + 1;
} else if (*(code + ip) < 0xfc) {
ip += logic_names_test[*(code + ip)].num_args;
ip++;
} else {
ip++;
}
}
ip++; /* skip over 0xFF */
ip += lohi_getword (code + ip) + 2;
}
#ifdef USE_CONSOLE
if (debug.enabled && (debug.logic0 || lognum))
debug_console (lognum, 0xFF, retval ? "=TRUE" : "=FALSE");
#endif
return retval;
}