aboutsummaryrefslogtreecommitdiff
path: root/engines/glk/agt/token.cpp
blob: 1305056c8e3262c348e29be5caa30d681a07f5f8 (plain)
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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * 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; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "glk/agt/agility.h"
#include "glk/agt/interp.h"
#include "glk/agt/exec.h"

namespace Glk {
namespace AGT {

/*

   This contains the code for actually executing each of the
   individual metacommand tokens as well as the routines for
   doing range checking to prevent a bad metacommand from crashing
   the interpreter.

*/

static void it_newdesc(integer item, descr_ptr *newdesc) {
	descr_ptr *desc;

	if (tnoun(item)) desc = noun_ptr + (item - first_noun);
	else if (tcreat(item)) desc = creat_ptr + (item - first_creat);
	else if (item >= first_room && item <= maxroom)
		desc = room_ptr + (item - first_room);
	else {
		writeln("INTERNAL ERROR: it_newdesc called with invalid object");
		return;
	}
	desc->start = newdesc->start;
	desc->size = newdesc->size;
}

static void changepict(int old_pict, int new_pict)
/* Replace old_pict by new_pict */
{
	pictable[old_pict - 1] = new_pict - 1;
}


static const int antidir[12] = {1, 0, 3, 2, 7, 6, 5, 4, 9, 8, 11, 10};

static void change_passage(int start, int dir, int newend)
/* dir is 1..12 */
/* START is a room number: i.e. starting at 0. */
/* NEWEND is an object number, so starts at first_room */
{
	int oldend, i;

	dir--;  /* Convert direction to 0..11 scale */
	oldend = room[start].path[dir];
	room[start].path[dir] = newend;
	if (newend == 0) {
		if (room[oldend - first_room].path[antidir[dir]] == start + first_room)
			room[oldend - first_room].path[antidir[dir]] = 0;
		else for (i = 0; i < 12; i++)
				if (room[oldend - first_room].path[i] == start + first_room) {
					room[oldend - first_room].path[i] = 0;
					break;
				}
	} else
		room[newend - first_room].path[antidir[dir]] = start + first_room;
}


static long ask_for_number(int n1, int n2) {
	char s[50];
	int n;

	if (n1 != n2)
		sprintf(s, "Enter a number from %d to %d: ", n1, n2);
	else
		sprintf(s, "Enter a number: ");
	for (;;) {
		writestr(s);
		n = read_number();
		if (n1 == n2 || (n >= n1 && n <= n2)) return n;
		writeln("");
	}
}

/* Check if n1*n2 will fit in 32 bits and print an error message if not */
/* This errs on the side of caution */
static rbool mult_rangecheck(long n1, long n2) {
	int cnt;

	if (n1 == 0 || n2 == 0) return 1;
	if (n1 < 0) n1 = -n1;
	if (n2 < 0) n2 = -n2;

	for (cnt = 0; n1 != 0; n1 >>= 1, cnt++);
	for (; n2 != 0; n2 >>= 1, cnt++);
	cnt--;

	if (cnt <= 31) return 1; /* We're okay */

	if (!PURE_ERROR)
		writeln("GAME ERROR: Multiplication out of range.");
	return 0;
}

static rbool is_numeric(parse_rec *objrec) {
	char *s;

	if (objrec->num != 0 || objrec->info == D_NUM) return 1;
	if (objrec->adj != 0) return 0;
	if (objrec->noun <= 0) return 0;
	(void)strtol(dict[objrec->noun], &s, 10);
	return (*s == 0); /* *s==0 means no error-- it parsed as a number. */
}

static void setcase(char *s, rbool up) {
	for (; *s != 0; s++)
		if (up) *s = toupper(*s);
		else *s = tolower(*s);
}


void move_in_dir(int obj, int dir) {
	int r;

	r = it_room(obj);
	if (!troom(r)) {
		writeln("GAME ERROR: Object not in a room.");
		return;
	}
	r = room[r - first_room].path[dir - 1];
	if (!troom(r)) return; /* Can't go that way; Fail silently */
	if (obj == 1)
		goto_room(r);
	else
		it_move(obj, r);
}


/* ------------------------------------------------------------------- */
/*  Stack routines:   Manipulating the expression stack                */
/* ------------------------------------------------------------------- */

static long *stack = NULL;
static int sp = 0; /* Stack pointer */
static int stacksize = 0; /* Actual space allocated to the stack */

void init_stack(void) {
	rfree(stack);
	sp = 0;
	stacksize = 0;
}

/* This resets the stack to an empty state. */
void clear_stack(void) {
	sp = 0;
}

static void push_stack(long val) {
	sp++;
	if (sp > stacksize) {
		stacksize += 10;
		stack = (long *)rrealloc(stack, stacksize * sizeof(long));
	}
	stack[sp - 1] = val;
}

static long pop_stack(void) {
	long n;
	if (sp == 0) {
		writeln("GAME ERROR: Stack underflow.");
		return 0;
	}
	n = stack[--sp];
	if (sp + 100 < stacksize) {
		stacksize -= 50;
		stack = (long *)rrealloc(stack, stacksize * sizeof(long));
	}
	return n;
}

long pop_expr_stack(void) {
	return pop_stack();
}


/* opnum: 0=+, 1=-, 2=*, 3=/ 4=% */
static void op_stack(int opnum) {
	long n1, n2;
	n1 = pop_stack();
	n2 = pop_stack();
	switch (opnum) {
	case 0:
		n1 = n1 + n2;
		break;
	case 1:
		n1 = n1 - n2;
		break;
	case 2:
		if (mult_rangecheck(n1, n2)) n1 = n1 * n2;
		break;
	case 3:
		if (n2 != 0) n1 = n1 / n2;
		else writeln("GAME ERROR: Division by zero.");
		break;
	case 4:
		if (n2 != 0) n1 = n1 % n2;
		else writeln("GAME ERROR: Division by zero.");
		break;
	default:
		writeln("INTERNAL ERROR: Invalid stack operation.");
	}
	push_stack(n1);
}

/* This is called from the disassembler */
void print_tos(void) {
	if (sp > 0)
		dbgprintf("TOS(%d)", stack[sp - 1]);
	else
		debugout("TOS(xxx)");
}



/* ------------------------------------------------------------------- */
/* METACOMMAND ROUTINES  */
/*  Functions for scanning and decoding of metacommands */
/* ------------------------------------------------------------------- */

/* #define cret(b) return ((b) ? -1 : 0)*/
#define cret(b) return (b)
#define cretn(i,f) cret(tnoun(i) && noun[i-first_noun].f)
#define cretc(i,f) cret(tcreat(i) && creature[i-first_creat].f)
#define icretc(f) cret(do_disambig==1 || \
                       (tcreat(iobj) && creature[iobj-first_creat].f))

static int obj_cond(int op_, int obj, int arg) {
	switch (op_) {
	case 0:
		cret(in_scope(obj));  /* Present--
                      Do we want to use visible here?? */
	case 1:
		cret(is_within(obj, 1000, 1)); /* IsWearing */
	case 2:
		cret(is_within(obj, 1, 1));
	/* if (PURE_WEAR)  return (it_loc(obj)==1); else */
	case 3:
		cret(it_loc(obj) == 0); /* Nowhere */
	case 4:
		cret(it_loc(obj) != 0);
	case 5:
		cret(!player_has(obj) && in_scope(obj));
	case 6:
		cret(it_loc(obj) == arg);
	case 7:
		cret(it_on(obj));
	case 8:
		cret(!it_on(obj));
	case 9:
		cret(it_open(obj));
	case 10:
		cret(!it_open(obj));
	case 11:
		cretn(obj, locked);
	case 12:
		cret(!tnoun(obj) || !noun[obj - first_noun].locked);
	case 13:
		cretn(obj, edible);
	case 14:
		cretn(obj, drinkable);
	case 15:
		cretn(obj, poisonous);
	case 16:
		cretn(obj, movable);
	default:
		writeln("INTERNAL ERROR: Bad obj_cond value.");
		return 2;
	}
}


static int exec_cond(int op_, int arg1, int arg2) {
	int i;

	switch (op_) {
	/* First the conditions */
	case 0:
		cret(loc + first_room == arg1); /* AtLoc(Room) */
	case 1:
		cret(loc + first_room > arg1);
	case 2:
		cret(loc + first_room < arg1);
	case 3:
		return musiccmd(-1, -1); /* SongPlaying */
	case 4:
		return musiccmd(-2, -1); /* SoundIsOn */
	case 5:
		cret(vb <= 13 && vb > 0 &&
		     room[loc].path[vb - 1] >= first_room); /*DirOK*/
	case 6:
		cret(vb == arg1); /* DirectionIs */
	case 7:
		cret(loc + first_room >= arg1 &&
		     loc + first_room <= arg2); /* BetweenRooms  ?? */
	case 8:
		cret(room[arg1 - first_room].seen);
	case 9:  /* Entered Object? i.e. is iobj valid */
		cret(do_disambig == 1 || iobj > 0);
	case 10:
		cret(curr_time > arg1); /* TimeGT */
	case 11:
		cret(curr_time < arg1); /* TimeLT */
	case 12:
		cret(first_visit_flag);
	case 13:
		cret(newlife_flag);
	case 14:
		cret(player_contents != 0); /* CarrySome */
	case 15:
		cret(player_contents == 0); /* CarryNo */
	case 16:
		cret(player_worn != 0); /* WearSome */
	case 18:
		cret(player_worn == 0); /* WearNo */
	case 17:          /* CarryTreas */
		contloop(i, 1)
		if (tnoun(i) && noun[i - first_noun].points >= arg1) return 1;
		contloop(i, 1000)
		if (tnoun(i) && noun[i - first_noun].points >= arg1) return 1;
		return 0;
	case 19:
		cret(totwt == arg1);
	case 20:
		cret(totwt > arg1);
	case 21:
		cret(totwt < arg1);
	case 22:
	case 23:
	case 24:
	case 25:
	case 26:
	case 27:
	case 28:
		return obj_cond(op_ - 22, arg1, arg2);
	case 29:
		cret(it_loc(arg1) == it_loc(arg2));
	case 30:
	case 31:
		return obj_cond(op_ - 23, arg1, arg2);
	case 32:
		cret(it_group(arg1));
	case 33:
	case 34:
	case 35:
	case 36:
	case 37:
	case 38:
	case 39:
	case 40:
		return obj_cond(op_ - 24, arg1, arg2);
	case 41:
	case 42:
	case 43:
	case 44:
	case 45:
	case 46:
	case 47:
	case 48:
	case 49:
	case 50:
	case 51:
	case 52:
	case 53:
	case 54:
	case 55:
	case 56:
	case 57:
		return obj_cond(op_ - 41, dobj, arg1);
	case 58:
		cretn(dobj, points == arg1);
	case 59:
		cretn(dobj, points > arg1);
	case 60:
		cretn(dobj, points < arg1);
	case 61:
		cretn(dobj, weight == arg1);
	case 62:
		cretn(dobj, weight > arg1);
	case 63:
		cretn(dobj, weight < arg1);
	case 64:
		cret(islit());
	case 65:
		cret(room[loc].light != 0);
	case 66:
		cret(flag[arg1]);
	case 67:
		cret(!flag[arg1]);
	case 68:
		cret(room[loc].flag_noun_bits & (1 << (arg1 - 1)));
	case 70:
		cret(!(room[loc].flag_noun_bits & (1 << (arg1 - 1))));
	case 69:
		cret(room[loc].PIX_bits & (1 << (arg1 - 1))); /* Room Pix here? */
	case 71:
		cret(tscore == arg1);
	case 72:
		cret(tscore > arg1);
	case 73:
		cret(tscore < arg1);
	case 74:
		cret(agt_number == arg1);
	case 75:
		cret(agt_number > arg1);
	case 76:
		cret(agt_number < arg1);
	case 77:
		cret(agt_answer);
	case 78:
		cret(!agt_answer);
	case 79:
		cret(turncnt == arg1);
	case 80:
		cret(turncnt > arg1);
	case 81:
		cret(turncnt < arg1);
	case 82:
		cret(cnt_val(agt_counter[arg1]) == arg2);
	case 83:
		cret(cnt_val(agt_counter[arg1]) > arg2);
	case 84:
		cret(cnt_val(agt_counter[arg1]) < arg2);

	case 85:
		cret(agt_var[arg1] == arg2);
	case 86:
		cret(agt_var[arg1] > arg2);
	case 87:
		cret(agt_var[arg1] < arg2);
	case 88:
		cret(agt_var[arg1] < agt_var[arg2]);
	case 89:
		cret(agt_var[arg1] < agt_rand(1, arg2));
	case 90:
		cret((actor != 0) && (it_loc(actor) == loc + first_room));
	case 91:
		cret(actor == arg1);
	case 92:
		cret(dobj == arg1);
	case 93:
		cret(do_disambig == 1 || iobj == arg1);
	case 94:
		cret(it_contents(arg1) != 0);
	case 95:
		cret(agt_rand(1, 100) <= arg1);
	case 96:
		cret(yesno("Yes or no? "));
	case 97:
		cret(!yesno("Yes or no? "));
	case 98:
		cret(vb > 0 && vb <= 13);
	case 99:
		cret(tcreat(dobj));
	case 100:
		cretc(dobj, gender == 2); /* man */
	case 101:
		cretc(dobj, gender == 1); /* woman */
	case 102:
		cretc(dobj, gender == 0); /* thing */
	case 103:
		cretc(iobj, gender == 2);
	case 104:
		cretc(iobj, gender == 1); /* woman */
	case 105:
		cretc(iobj, gender == 0); /* thing */
	case 106:
		cret(do_disambig == 1 || tcreat(iobj));
	case 107:
		return (do_disambig == 1 || obj_cond(0, iobj, 0));
	/* OR and NOT are handled higher up. */
	/* The following are all v1.8x metacommands */
	case 110:
		cret(beforecmd);
	case 111:
		cret(!beforecmd);
	case 112:
		cret(curr_time / 100 == arg1); /* HoursEqual */
	case 113:
		cret(curr_time / 100 > arg1);
	case 114:
		cret(curr_time / 100 < arg1);
	case 115:
		cret(curr_time % 100 == arg1); /* MinutesEqual */
	case 116:
		cret(curr_time % 100 > arg1);
	case 117:
		cret(curr_time % 100 < arg1);
	case 118:
		cret(curr_time < 1200);  /* IsAM */

	case 119:
		cret(do_disambig);     /* OnDisambig */
	case 120:
		cretc(arg1, hostile);   /* IsHostile */
	case 121:         /* HostilePresent */
		creatloop(i)
		if (creature[i].location == loc + first_room &&
		        creature[i].hostile) return 1;
		return 0;
	/* Otherwise, we're in trouble. */
	case 122:
		cret(actor_in_scope); /* NameWasPresent */
	case 123: /* OncePerTurn */
		if (beforecmd)
			cret(start_of_turn);
		else
			cret(end_of_turn);
	case 124:  /* IsClass */
		cret(arg2 == 0 || matchclass(arg1, arg2));
	case 125:
		cret(getattr(arg1, arg2)); /* IsSet */
	case 126:
		cret(is_numeric(dobj_rec));
	case 127:
		cret(is_numeric(iobj_rec));
	case 128:
		cret(arg1 == arg2);
	case 129:
		cret(arg1 > arg2);
	case 130:
		cret(arg1 < arg2);
	case 131:
		cret(arg1 >= arg2);
	case 132:
		cret(arg1 <= arg2);
	case 133:
		cret(strcmp(userstr[arg1 - 1], userstr[arg2 - 1]) == 0);
	case 134:
		cret(strcmp(userstr[arg1 - 1], userstr[arg2 - 1]) < 0);
	case 135:
		cret(strcmp(userstr[arg1 - 1], userstr[arg2 - 1]) > 0);
	case 136:
		cret(strcasecmp(userstr[arg1 - 1], userstr[arg2 - 1]) == 0);
	case 137:
		cret(strcasecmp(userstr[arg1 - 1], userstr[arg2 - 1]) < 0);
	case 138:
		cret(strcasecmp(userstr[arg1 - 1], userstr[arg2 - 1]) > 0);
	case 139:
		cret(match_answer(rstrdup(userstr[arg1 - 1]), arg2 - 1));
	/* Note that match_answer rfrees it's first argument */
	case 140:
		cret(it_seen(arg1));
	case 141:
		cret(op_objflag(2, arg1, arg2));
	case 142:
		cret(!op_objflag(2, arg1, arg2));
	case 143:
		i = it_room(arg1);
		cret(troom(i) && troom(room[i - first_room].path[arg2 - 1]));
	default:
		writeln("INTERNAL ERROR: Condition token not supported.");
		rprintf("Condition #%d", op_);
		writeln("");
		return 0;
	}
}

#undef cret
#undef cretn
#undef cretc


static void obj_act(int op_, int obj) {
	switch (op_) {
	case 0:
	case 1:  /* open and close */
		if (tnoun(obj))
			noun[obj - first_noun].open = (op_ == 0);
		break;
	case 2:
	case 3: /* lock and unlock */
		if (tnoun(obj))
			noun[obj - first_noun].locked = (op_ == 2);
	}
}


static void exec_action(int op_, int arg1, int arg2) {
	int i, j;
	char *tmpstr;

	switch (op_) {
	case 1000:
		goto_room(arg1 - first_room);
		break;
	case 1001:
		goto_room(agt_rand(arg1, arg2) - first_room);
		break;
	case 1002:
		agt_var[arg1] = loc + first_room;
		break;
	case 1003:
		agt_var[arg1] = dobj;
		break;
	case 1004:
		agt_var[arg1] = iobj;
		break;
	case 1005:
		goto_room(agt_var[arg1] - first_room);
		break;
	case 1006:
		it_move(arg1, agt_var[arg2]);
		break;
	case 1007:
		get_obj(agt_var[arg1]);
		break;
	case 1008:
		msgout(agt_var[arg1], 1);
		break;
	case 1009:
		get_obj(arg1);
		break;
	case 1010:
		get_obj(arg1);
		it_move(arg1, 1000);
		break;
	case 1011:
		drop_obj(arg1);
		break;
	case 1012:
		if (it_loc(arg1) == 1000) {
			if (PURE_WEAR) drop_obj(arg1);
			else it_move(arg1, 1);
		}
		break;
	case 1013:
		fontcmd(0, arg1 - 1);
		break;  /* Load font */
	case 1014:
		pictcmd(1, pictable[arg1 - 1]);
		break;  /* Show picture */
	case 1015:
		changepict(arg1, arg2);
		break; /* ChangePicture */
	case 1016:
		if (PICT_SUPPORT &&
		        yesno("Would you like to see the picture?"))
			pictcmd(1, pictable[arg1 - 1]);
		break;
	case 1017:
		pictcmd(2, arg1);
		break;  /* Show room pix */
	case 1018:
		if (PICT_SUPPORT &&
		        yesno("Would you like to see the picture?"))
			pictcmd(2, arg1 - 1);
		break;
	case 1019:
		musiccmd(1, arg1 - 1);
		break;
	case 1020:
		musiccmd(1, agt_rand(arg1, arg2) - 1);
		break;
	case 1021:
		musiccmd(2, arg1 - 1);
		break;
	case 1022:
		musiccmd(3, -1);
		break;  /* Stop Repeat */
	case 1023:
		musiccmd(4, -1);
		break;  /* Stop song */
	case 1024:
		musiccmd(5, -1);
		break;  /* Suspend song */
	case 1025:
		musiccmd(6, -1);
		break;   /* Resume song */
	case 1026:
		if (tnoun(dobj))
			noun[dobj - first_noun].movable = !noun[dobj - first_noun].movable;
		break;
	case 1027:
		it_newdesc(arg1, &msg_ptr[arg2 - 1]);
		break;
	case 1028:
		if (tnoun(arg1)) noun[arg1 - first_noun].points = arg2;
		else if (tcreat(arg1)) creature[arg1 - first_creat].points = arg2;
		else if (troom(arg1)) room[arg1 - first_room].points = arg2;
		break;
	case 1029:
		it_destroy(iobj);
		break;
	case 1030:
		tmpstr = agt_readline(3);
		i = strlen(tmpstr) - 1;
		if (i > 0 && tmpstr[i] == '\n') tmpstr[i] = 0;
		strncpy(userstr[arg1 - 1], tmpstr, 80);
		rfree(tmpstr);
		break;
	case 1031:
		agt_var[arg1] = read_number();
		break;
	case 1032:
		agt_var[arg1] = curr_time;
		break;
	case 1033:
		curr_time = normalize_time(agt_var[arg1]);
		break;
	case 1034:
		curr_time = normalize_time(arg1);
		break;
	case 1035:
		add_time(arg1);
		break;
	case 1036:
		delta_time = arg1;
		break;
	/* 1037 and 1038 are subroutine commands */
	case 1039:
		get_obj(dobj);
		break;
	case 1040:
		it_move(dobj, 1000);
		break;
	case 1041:
		drop_obj(dobj);
		break;
	case 1042:
		if (it_loc(dobj) == 1000) {
			if (PURE_WEAR) it_move(dobj, 1);
			else drop_obj(dobj);
		}
		break;
	case 1043: /* drop all */
		safecontloop(i, j, 1) drop_obj(i);
		break;
	case 1044: /* remove all */
		safecontloop(i, j, 1000) drop_obj(i);
		break;
	case 1045:
		deadflag = 1;
		break;
	case 1046:
		it_move(arg1, loc + first_room);
		break;
	case 1047:
		it_move(arg1, arg2);
		break;
	case 1048:
		it_reposition(arg1, arg2, 1);
		break; /* RePosition */
	case 1049:
		it_move(dobj, loc + first_room);
		break;
	case 1050:
		it_move(dobj, arg1);
		break;
	case 1051:
		safecontloop(i, j, 1) it_move(i, arg1);
		safecontloop(i, j, 1000) it_move(i, arg1);
		break;
	case 1052:
		nounloop(i)
		if (player_has(i + first_noun) && noun[i].points > arg2)
			it_move(i + first_noun, arg1);
		break;
	case 1053:
		safecontloop(i, j, arg1)
		if (tnoun(i)) it_move(i, arg2);
		break;
	case 1054:
		it_destroy(arg1);
		break;
	case 1055:
		it_destroy(dobj);
		break;
	case 1056:
		i = it_loc(arg1);
		it_move(arg1, it_loc(arg2));
		it_move(arg2, i);
		break;
	case 1057:
		it_move(arg1, it_loc(arg2));
		break;
	case 1058:
		it_move(dobj, it_loc(arg2));
		break;
	case 1059:
	case 1060: /* Add to/remove from group */
		if (tcreat(arg1))
			creature[arg1 - first_creat].groupmemb = (op_ == 1059);
		break;
	case 1061:   /* Move group */
		safecontloop(i, j, loc + first_room)
		if (it_group(i)) it_move(i, arg1);
		break;
	/* 1062 is RedirectTo */
	case 1063:
		msgout(agt_rand(arg1, arg2), 1);
		break;
	case 1064:
		print_contents(arg1, 1);
		break;
	case 1065:
	case 1066:
	case 1067:
	case 1068:
		obj_act(op_ - 1065, arg1);
		break;
	case 1069:
	case 1070:
	case 1071:
	case 1072:
		obj_act(op_ - 1069, dobj);
		break;
	case 1073:
		print_score();
		break;
	case 1074:
		tscore += arg1;
		break;
	case 1075:
		tscore -= arg1;
		break;
	case 1076:
		v_inventory();
		break;
	case 1077:
		wait_return();
		break;
	case 1078:
		writeln("Time passes...");
		break;
	case 1079:
		agt_delay(arg1);
		break;
	case 1080:
		agt_clrscr();
		break;
	case 1081:
		it_describe(arg1);
		break;
	case 1082:
		look_room();
		break;   /* LOOK */
	case 1083:
		msgout(arg1, 1);
		break;
	case 1084:
		writeln("");
		break;
	case 1085:
		if (PURE_TONE && sound_on)
			agt_tone(arg1, arg2);
		break; /* Tone */
	case 1086:
		agt_number = ask_for_number(arg1, arg2);
		break;
	case 1087:
		agt_answer = ask_question(arg1);
		break;
	case 1088:
		change_passage(loc, arg1, arg2);
		break;
	case 1089:
		flag[arg1] = 1;
		break;
	case 1090:
		flag[arg1] = 0;
		break;
	case 1091:
		flag[arg1] = !flag[arg1];
		break;
	case 1092:
		room[loc].flag_noun_bits |= (1 << (arg1 - 1));
		break; /* Roomflag on */
	case 1093:
		room[loc].flag_noun_bits &= ~(1 << (arg1 - 1));
		break; /* Off */
	case 1094:
		room[loc].flag_noun_bits ^= (1 << (arg1 - 1));
		break; /* Toggle */
	case 1095: /* if (agt_counter[arg1]==-1)*/
		agt_counter[arg1] = 1;
		break;
	case 1096:
		agt_counter[arg1] = -1;
		break;
	case 1097:
		agt_var[arg1] = arg2;
		break;
	case 1098:
		agt_var[arg1] += arg2;
		break;
	case 1099:
		agt_var[arg1] -= arg2;
		break;
	case 1100:
		agt_var[arg1] += agt_var[arg2];
		break;
	case 1101:
		agt_var[arg1] -= agt_var[arg2];
		break;
	case 1102:
		agt_var[arg1] = agt_rand(0, arg2);
		break;
	case 1103:
		agt_var[arg1] = dobj_rec->num;
		break;
	case 1104:
		agt_var[arg1] = iobj_rec->num;
		break;

	/* The following are v1.8x specific */
	case 1105:
		quote(arg1);
		break;
	case 1106:
		add_time(arg1);
		break;
	case 1107:
		add_time(-arg1);
		break;
	case 1108:
		curr_time = (curr_time % 100) + 100 * arg1;
		break;
	case 1109:
		curr_time = (curr_time / 100) * 100 + arg1;
		break;
	case 1110:
		add_time(agt_var[arg1]);
		break;
	case 1111:
		add_time(-agt_var[arg1]);
		break;
	case 1112:
		curr_time = (curr_time % 100) + 100 * agt_var[arg1];
		break;
	case 1113:
		curr_time = (curr_time / 100) * 100 + agt_var[arg1];
		break;

	/* Now for the AGX additions */
	case 1114:
		add_time(-arg1);
		break; /* ME-style SubtractFromTime */
	case 1115:
		disambig_score = arg1;
		break; /* SetDisambigPriority */
	case 1116:
		agt_var[arg1] = delta_time;
		break;
	case 1117: /* ChangeStatus */
		statusmode = arg1;
		break;
	case 1118:
		if (!mult_rangecheck(agt_var[arg1], arg2)) break;
		agt_var[arg1] *= arg2;
		break;
	case 1119:
		if (arg2 == 0) {
			if (!PURE_ERROR)
				writeln("GAME ERROR: Division by zero.");
		} else agt_var[arg1] /= arg2;
		break;
	case 1120:
		if (arg2 == 0) {
			if (!PURE_ERROR)
				writeln("GAME ERROR: Attempt to divide by zero.");
		} else agt_var[arg1] %= arg2;
		break;
	case 1121:
		agt_waitkey();
		break;
	case 1122:
		last_he = arg1;
		break;   /* SetHE */
	case 1123:
		last_she = arg1;
		break;
	case 1124:
		last_it = arg1;
		break;
	case 1125:
		last_they = arg1;
		break;
	case 1126:
		msgout(arg1, 0);
		break;
	case 1127:
		if (!PURE_ERROR)
			sysmsg(arg1, "GAME ERROR: Standard message not defined.");
		break;
	case 1128:
		msgout(arg1, 1);
		break; /* FailMessage */
	case 1129:  /* StdMessage */
		sysmsg(arg1, "GAME ERROR: Standard message not defined.");
		break;
	case 1130:
		msgout(arg2, 1);
		break; /* ErrMessage */
	case 1131: /* StdErrMessage */
		sysmsg(arg1, "GAME ERROR: Standard message not defined.");
		break;
	case 1132: /* AND */
		break;  /* These don't do anything under normal circumstances */
	case 1133:  /* SetClass */
		if (troom(arg1)) room[arg1 - first_room].oclass = arg2;
		else if (tnoun(arg1)) noun[arg1 - first_noun].oclass = arg2;
		else if (tcreat(arg1)) noun[arg1 - first_creat].oclass = arg2;
		break;
	case 1134:
		agt_var[arg1] = it_class(arg2);
		break; /* SetVariableToClass */

	/* Stack commands */
	case 1135:
		push_stack(arg1);
		break;
	case 1136:
		agt_var[arg1] = pop_stack();
		break;
	case 1137:
	case 1138:
	case 1139:
	case 1140:
	case 1141:
		op_stack(op_ - 1137); /* +,-,*, /,% * */
		break;
	case 1142: { /* DupStack */
		long n;
		n = pop_stack();
		push_stack(n);
		push_stack(n);
		break;
	}
	case 1143:
		pop_stack();
		break; /* Discard TOS */
	case 1144:
		agt_var[arg1] = agt_number;
		break; /* SetVariableToInput */
	case 1145:
		setattr(arg1, arg2, 1);
		break; /* Set */
	case 1146:
		setattr(arg1, arg2, 0);
		break; /* Clear */
	case 1147:
		push_stack(getprop(arg1, arg2));
		break; /* PushProp */
	case 1148:
		setprop(arg1, arg2, pop_stack());
		break; /* PopProp */
	/* 1149, 1150 handled by run_metacommand */
	/* 1151 is EndDisambig */
	/* 1152 is XRedirect */
	case 1153:
		rstrncpy(userstr[arg1 - 1], userstr[arg2 - 1], 81);
		break;
	case 1154:
		setcase(userstr[arg1 - 1], 1);
		break;
	case 1155:
		setcase(userstr[arg1 - 1], 0);
		break;
	case 1156:
		op_objflag(1, arg1, arg2);
		break;
	case 1157:
		op_objflag(0, arg1, arg2);
		break;
	case 1158:
		op_objflag(3, arg1, arg2);
		break;
	case 1159:
		push_stack(op_objprop(2, arg1, arg2, 0));
		break;
	case 1160:
		op_objprop(1, arg1, arg2, pop_stack());
		break;
	case 1161:
		move_in_dir(arg1, arg2);
		break;
	default:
		writeln("INTERNAL ERROR: Action token not supported.");
		rprintf("Action #%d", op_);
		writeln("");
	}
}

int exec_instr(op_rec *oprec)
/* This routine is responsible for executing all conditions and action
   tokens. Remember action tokens are numbered from 1000. */
/* Return codes:
   0 to continue running commands
   1 for a failed conditional token
   100 Next metacommand
   101 Stop running metacommands
   102 End turn
   103 Redirection */
{
	rbool r;

	if (oprec->op < 1000) {
		r = exec_cond(oprec->op, oprec->arg1, oprec->arg2);
		return (oprec->negate ? r : !r); /* Is it prefixed by NOT? */
	}
	switch (oprec->op) {
	case 1151: /* EndDisambig <num> */
		if (do_disambig) {
			disambig_score = oprec->arg1;
			return 102;  /* "End turn" if disambiguating */
		}
		return 0;  /* ... otherwise do nothing */
	case 1062:
	case 1152:
		return 103; /* Redirect */
	case WIN_ACT:
		winflag = 1;
		return 0;  /* win game */
	case (WIN_ACT+1):
		endflag = 1;
		return 102; /* end game */
	case (WIN_ACT+2):
		return 100; /* end this command */
	case (WIN_ACT+3):
		return 101; /* end all commands */
	case (WIN_ACT+4):
		return 102; /* end turn */
	default:
		exec_action(oprec->op, oprec->arg1, oprec->arg2);
		if (oprec->failmsg) return 102;
		else return 0;
	}
}

} // End of namespace AGT
} // End of namespace Glk