aboutsummaryrefslogtreecommitdiff
path: root/engines/glk/adrift/scvars.cpp
blob: f9203fb384b377b989a55545fd5fec4bbb604efb (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
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
/* 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/adrift/scare.h"
#include "glk/adrift/scprotos.h"
#include "glk/adrift/scgamest.h"
#include "glk/glk.h"
#include "glk/events.h"

namespace Glk {
namespace Adrift {

/*
 * Module notes:
 *
 * o Gender enumerations are 0/1/2, but 1/2/3 in jAsea.  The 0/1/2 values
 *   seem to be right.  Is jAsea off by one?
 *
 * o jAsea tries to read Globals.CompileDate.  It's just CompileDate.
 *
 * o State_ and obstate are implemented, but not fully tested due to a lack
 *   of games that use them.
 */

/* Assorted definitions and constants. */
static const sc_uint VARS_MAGIC = 0xabcc7a71;
static const sc_char NUL = '\0';

/* Variables trace flag. */
static sc_bool var_trace = FALSE;

/* Table of numbers zero to twenty spelled out. */
enum { VAR_NUMBERS_SIZE = 21 };
static const sc_char *const VAR_NUMBERS[VAR_NUMBERS_SIZE] = {
	"zero", "one", "two", "three", "four", "five", "six", "seven", "eight",
	"nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
	"sixteen", "seventeen", "eighteen", "nineteen", "twenty"
};

/* Variable entry, held on a list hashed by variable name. */
struct sc_var_s {
	struct sc_var_s *next;

	const sc_char *name;
	sc_int type;
	sc_vartype_t value;
};
typedef sc_var_s sc_var_t;
typedef sc_var_t *sc_varref_t;

/*
 * Variables set structure.  A self-contained set of variables on which
 * variables functions operate.  211 is prime, making it a reasonable hash
 * divisor.  There's no rehashing here; few games, if any, are likely to
 * exceed a fill factor of two (~422 variables).
 */
enum { VAR_HASH_TABLE_SIZE = 211 };
struct sc_var_set_s {
	sc_uint magic;
	sc_prop_setref_t bundle;
	sc_int referenced_character;
	sc_int referenced_object;
	sc_int referenced_number;
	sc_bool is_number_referenced;
	sc_char *referenced_text;
	sc_char *temporary;
	uint32 timestamp;
	sc_uint time_offset;
	sc_gameref_t game;
	sc_varref_t variable[VAR_HASH_TABLE_SIZE];
};
typedef sc_var_set_s sc_var_set_t;


/*
 * var_is_valid()
 *
 * Return TRUE if pointer is a valid variables set, FALSE otherwise.
 */
static sc_bool var_is_valid(sc_var_setref_t vars) {
	return vars && vars->magic == VARS_MAGIC;
}


/*
 * var_hash_name()
 *
 * Hash a variable name, modulo'ed to the number of buckets.
 */
static sc_uint var_hash_name(const sc_char *name) {
	return sc_hash(name) % VAR_HASH_TABLE_SIZE;
}


/*
 * var_create_empty()
 *
 * Create and return a new empty set of variables.
 */
static sc_var_setref_t var_create_empty(void) {
	sc_var_setref_t vars;
	sc_int index_;

	/* Create a clean set of variables. */
	vars = (sc_var_setref_t)sc_malloc(sizeof(*vars));
	vars->magic = VARS_MAGIC;
	vars->bundle = nullptr;
	vars->referenced_character = -1;
	vars->referenced_object = -1;
	vars->referenced_number = 0;
	vars->is_number_referenced = FALSE;
	vars->referenced_text = nullptr;
	vars->temporary = nullptr;
	vars->timestamp = g_vm->_events->getTotalPlayTicks() / 1000;
	vars->time_offset = 0;
	vars->game = nullptr;

	/* Clear all variable hash lists. */
	for (index_ = 0; index_ < VAR_HASH_TABLE_SIZE; index_++)
		vars->variable[index_] = nullptr;

	return vars;
}


/*
 * var_destroy()
 *
 * Destroy a variable set, and free its heap memory.
 */
void var_destroy(sc_var_setref_t vars) {
	sc_int index_;
	assert(var_is_valid(vars));

	/*
	 * Free the content of each string variable, and variable entry.  String
	 * variable content needs to use mutable string instead of const string.
	 */
	for (index_ = 0; index_ < VAR_HASH_TABLE_SIZE; index_++) {
		sc_varref_t var, next;

		for (var = vars->variable[index_]; var; var = next) {
			next = var->next;
			if (var->type == VAR_STRING)
				sc_free(var->value.mutable_string);
			sc_free(var);
		}
	}

	/* Free any temporary and reference text storage area. */
	sc_free(vars->temporary);
	sc_free(vars->referenced_text);

	/* Poison and free the variable set itself. */
	memset(vars, 0xaa, sizeof(*vars));
	sc_free(vars);
}


/*
 * var_find()
 * var_add()
 *
 * Find and return a pointer to a named variable structure, or nullptr if no such
 * variable exists, and add a new variable structure to the lists.
 */
static sc_varref_t var_find(sc_var_setref_t vars, const sc_char *name) {
	sc_uint hash;
	sc_varref_t var;

	/* Hash name, search list and return if name match found. */
	hash = var_hash_name(name);
	for (var = vars->variable[hash]; var; var = var->next) {
		if (strcmp(name, var->name) == 0)
			break;
	}

	/* Return variable, or nullptr if no such variable. */
	return var;
}

static sc_varref_t var_add(sc_var_setref_t vars, const sc_char *name, sc_int type) {
	sc_varref_t var;
	sc_uint hash;

	/* Create a new variable entry. */
	var = (sc_varref_t)sc_malloc(sizeof(*var));
	var->name = name;
	var->type = type;
	var->value.voidp = nullptr;

	/* Hash its name, and insert it at start of the relevant list. */
	hash = var_hash_name(name);
	var->next = vars->variable[hash];
	vars->variable[hash] = var;

	return var;
}


/*
 * var_get_scare_version()
 *
 * Return the value of %scare_version%.  Used to generate the system version
 * of this variable, and to re-initialize user versions initialized to zero.
 */
static sc_int var_get_scare_version(void) {
	sc_int major, minor, point, version;

	if (sscanf(SCARE_VERSION, "%ld.%ld.%ld", &major, &minor, &point) != 3) {
		sc_error("var_get_scare_version: unable to generate scare_version\n");
		return 0;
	}

	version = major * 10000 + minor * 100 + point;
	return version;
}


/*
 * var_put()
 *
 * Store a variable type in a named variable.  If not present, the variable
 * is created.  Type is one of 'I' or 'S' for integer or string.
 */
void var_put(sc_var_setref_t vars, const sc_char *name, sc_int type, sc_vartype_t vt_value) {
	sc_varref_t var;
	sc_bool is_modification;
	assert(var_is_valid(vars));
	assert(name);

	/* Check type is either integer or string. */
	switch (type) {
	case VAR_INTEGER:
	case VAR_STRING:
		break;

	default:
		sc_fatal("var_put: invalid variable type, %ld\n", type);
	}

	/* See if the user variable already exists. */
	var = var_find(vars, name);
	if (var) {
		/* Verify that nothing is trying to change the variable's type. */
		if (var->type != type)
			sc_fatal("var_put: variable type changed, %s\n", name);

		/*
		 * Special case %scare_version%.  If a game changes its value, it may
		 * compromise version checking, so warn here, but continue.
		 */
		if (strcmp(name, "scare_version") == 0) {
			if (var->value.integer != vt_value.integer)
				sc_error("var_put: warning: %%%s%% value changed\n", name);
		}

		is_modification = TRUE;
	} else {
		/*
		 * Special case %scare_version%.  If a game defines this and initializes
		 * it to zero, re-initialize it to SCARE's version number.  Games that
		 * define %scare_version%, initially zero, can use this to test if
		 * running under SCARE or Runner.
		 */
		if (strcmp(name, "scare_version") == 0 && vt_value.integer == 0) {
			vt_value.integer = var_get_scare_version();

			if (var_trace)
				sc_trace("Variable: %%%s%% [new] caught and mapped\n", name);
		}

		/*
		 * Create a new and empty variable entry.  The mutable string needs to
		 * be set to nullptr here so that realloc works correctly on assigning
		 * the value below.
		 */
		var = var_add(vars, name, type);
		var->value.mutable_string = nullptr;

		is_modification = FALSE;
	}

	/* Update the existing variable, or populate the new one fully. */
	switch (var->type) {
	case VAR_INTEGER:
		var->value.integer = vt_value.integer;
		break;

	case VAR_STRING:
		/* Use mutable string instead of const string. */
		var->value.mutable_string = (sc_char *)sc_realloc(var->value.mutable_string,
		                            strlen(vt_value.string) + 1);
		strcpy(var->value.mutable_string, vt_value.string);
		break;

	default:
		sc_fatal("var_put: invalid variable type, %ld\n", var->type);
	}

	if (var_trace) {
		sc_trace("Variable: %%%s%%%s = ",
		         name, is_modification ? "" : " [new]");
		switch (var->type) {
		case VAR_INTEGER:
			sc_trace("%ld", var->value.integer);
			break;
		case VAR_STRING:
			sc_trace("\"%s\"", var->value.string);
			break;

		default:
			sc_trace("[invalid variable type, %ld]", var->type);
			break;
		}
		sc_trace("\n");
	}
}


/*
 * var_append_temp()
 *
 * Helper for object listers.  Extends temporary, and appends the given text
 * to the string.
 */
static void var_append_temp(sc_var_setref_t vars, const sc_char *string) {
	sc_bool new_sentence;
	sc_int noted;

	if (!vars->temporary) {
		/* Create a new temporary area and copy string. */
		new_sentence = TRUE;
		noted = 0;
		vars->temporary = (sc_char *)sc_malloc(strlen(string) + 1);
		strcpy(vars->temporary, string);
	} else {
		/* Append string to existing temporary. */
		new_sentence = (vars->temporary[0] == NUL);
		noted = strlen(vars->temporary);
		vars->temporary = (sc_char *)sc_realloc(vars->temporary,
		                                        strlen(vars->temporary) +
		                                        strlen(string) + 1);
		strcat(vars->temporary, string);
	}

	if (new_sentence)
		vars->temporary[noted] = sc_toupper(vars->temporary[noted]);
}


/*
 * var_print_object_np
 * var_print_object
 *
 * Convenience functions to append an object's name, with and without any
 * prefix, to variables temporary.
 */
static void var_print_object_np(sc_gameref_t game, sc_int object) {
	const sc_var_setref_t vars = gs_get_vars(game);
	const sc_prop_setref_t bundle = gs_get_bundle(game);
	sc_vartype_t vt_key[3];
	const sc_char *prefix, *normalized, *name;

	/* Get the object's prefix. */
	vt_key[0].string = "Objects";
	vt_key[1].integer = object;
	vt_key[2].string = "Prefix";
	prefix = prop_get_string(bundle, "S<-sis", vt_key);

	/*
	 * Try the same shenanigans as done by the equivalent function in the
	 * library.
	 */
	normalized = prefix;
	if (sc_compare_word(prefix, "a", 1)) {
		normalized = prefix + 1;
		var_append_temp(vars, "the");
	} else if (sc_compare_word(prefix, "an", 2)) {
		normalized = prefix + 2;
		var_append_temp(vars, "the");
	} else if (sc_compare_word(prefix, "the", 3)) {
		normalized = prefix + 3;
		var_append_temp(vars, "the");
	} else if (sc_compare_word(prefix, "some", 4)) {
		normalized = prefix + 4;
		var_append_temp(vars, "the");
	} else if (sc_strempty(prefix))
		var_append_temp(vars, "the ");

	/* As with the library, handle the remaining prefix. */
	if (!sc_strempty(normalized)) {
		var_append_temp(vars, normalized);
		var_append_temp(vars, " ");
	} else if (normalized > prefix)
		var_append_temp(vars, " ");

	/*
	 * Print the object's name, again, as with the library, stripping any
	 * leading article
	 */
	vt_key[2].string = "Short";
	name = prop_get_string(bundle, "S<-sis", vt_key);
	if (sc_compare_word(name, "a", 1))
		name += 1;
	else if (sc_compare_word(name, "an", 2))
		name += 2;
	else if (sc_compare_word(name, "the", 3))
		name += 3;
	else if (sc_compare_word(name, "some", 4))
		name += 4;
	var_append_temp(vars, name);
}

static void var_print_object(sc_gameref_t game, sc_int object) {
	const sc_var_setref_t vars = gs_get_vars(game);
	const sc_prop_setref_t bundle = gs_get_bundle(game);
	sc_vartype_t vt_key[3];
	const sc_char *prefix, *name;

	/*
	 * Get the object's prefix.  As with the library, if the prefix is empty,
	 * put in an "a ".
	 */
	vt_key[0].string = "Objects";
	vt_key[1].integer = object;
	vt_key[2].string = "Prefix";
	prefix = prop_get_string(bundle, "S<-sis", vt_key);
	if (!sc_strempty(prefix)) {
		var_append_temp(vars, prefix);
		var_append_temp(vars, " ");
	} else
		var_append_temp(vars, "a ");

	/* Print the object's name. */
	vt_key[2].string = "Short";
	name = prop_get_string(bundle, "S<-sis", vt_key);
	var_append_temp(vars, name);
}


/*
 * var_select_plurality()
 *
 * Convenience function for listers.  Selects one of two responses depending
 * on whether an object appears singular or plural.
 */
static const sc_char *var_select_plurality(sc_gameref_t game, sc_int object,
		const sc_char *singular, const sc_char *plural) {
	return obj_appears_plural(game, object) ? plural : singular;
}


/*
 * var_list_in_object()
 *
 * List the objects in a given container object.
 */
static void var_list_in_object(sc_gameref_t game, sc_int container) {
	const sc_var_setref_t vars = gs_get_vars(game);
	sc_int object, count, trail;

	/* List out the objects contained in this object. */
	count = 0;
	trail = -1;
	for (object = 0; object < gs_object_count(game); object++) {
		/* Contained? */
		if (gs_object_position(game, object) == OBJ_IN_OBJECT
		        && gs_object_parent(game, object) == container) {
			if (count > 0) {
				if (count > 1)
					var_append_temp(vars, ", ");

				/* Print out the current list object. */
				var_print_object(game, trail);
			}
			trail = object;
			count++;
		}
	}
	if (count >= 1) {
		/* Print out final listed object. */
		if (count == 1) {
			var_print_object(game, trail);
			var_append_temp(vars,
			                var_select_plurality(game, trail,
			                                     " is inside ",
			                                     " are inside "));
		} else {
			var_append_temp(vars, " and ");
			var_print_object(game, trail);
			var_append_temp(vars, " are inside ");
		}

		/* Print out the container. */
		var_print_object_np(game, container);
		var_append_temp(vars, ".");
	}
}


/*
 * var_list_on_object()
 *
 * List the objects on a given surface object.
 */
static void var_list_on_object(sc_gameref_t game, sc_int supporter) {
	const sc_var_setref_t vars = gs_get_vars(game);
	sc_int object, count, trail;

	/* List out the objects standing on this object. */
	count = 0;
	trail = -1;
	for (object = 0; object < gs_object_count(game); object++) {
		/* Standing on? */
		if (gs_object_position(game, object) == OBJ_ON_OBJECT
		        && gs_object_parent(game, object) == supporter) {
			if (count > 0) {
				if (count > 1)
					var_append_temp(vars, ", ");

				/* Print out the current list object. */
				var_print_object(game, trail);
			}
			trail = object;
			count++;
		}
	}
	if (count >= 1) {
		/* Print out final listed object. */
		if (count == 1) {
			var_print_object(game, trail);
			var_append_temp(vars,
			                var_select_plurality(game, trail,
			                                     " is on ", " are on "));
		} else {
			var_append_temp(vars, " and ");
			var_print_object(game, trail);
			var_append_temp(vars, " are on ");
		}

		/* Print out the surface. */
		var_print_object_np(game, supporter);
		var_append_temp(vars, ".");
	}
}


/*
 * var_list_onin_object()
 *
 * List the objects on and in a given associate object.
 */
static void var_list_onin_object(sc_gameref_t game, sc_int associate) {
	const sc_var_setref_t vars = gs_get_vars(game);
	sc_int object, count, trail;
	sc_bool supporting;

	/* List out the objects standing on this object. */
	count = 0;
	trail = -1;
	supporting = FALSE;
	for (object = 0; object < gs_object_count(game); object++) {
		/* Standing on? */
		if (gs_object_position(game, object) == OBJ_ON_OBJECT
		        && gs_object_parent(game, object) == associate) {
			if (count > 0) {
				if (count > 1)
					var_append_temp(vars, ", ");

				/* Print out the current list object. */
				var_print_object(game, trail);
			}
			trail = object;
			count++;
		}
	}
	if (count >= 1) {
		/* Print out final listed object. */
		if (count == 1) {
			var_print_object(game, trail);
			var_append_temp(vars,
			                var_select_plurality(game, trail,
			                                     " is on ", " are on "));
		} else {
			var_append_temp(vars, " and ");
			var_print_object(game, trail);
			var_append_temp(vars, " are on ");
		}

		/* Print out the surface. */
		var_print_object_np(game, associate);
		supporting = TRUE;
	}

	/* List out the objects contained in this object. */
	count = 0;
	trail = -1;
	for (object = 0; object < gs_object_count(game); object++) {
		/* Contained? */
		if (gs_object_position(game, object) == OBJ_IN_OBJECT
		        && gs_object_parent(game, object) == associate) {
			if (count > 0) {
				if (count == 1) {
					if (supporting)
						var_append_temp(vars, ", and ");
				} else
					var_append_temp(vars, ", ");

				/* Print out the current list object. */
				var_print_object(game, trail);
			}
			trail = object;
			count++;
		}
	}
	if (count >= 1) {
		/* Print out final listed object. */
		if (count == 1) {
			if (supporting)
				var_append_temp(vars, ", and ");
			var_print_object(game, trail);
			var_append_temp(vars,
			                var_select_plurality(game, trail,
			                                     " is inside ",
			                                     " are inside "));
		} else {
			var_append_temp(vars, " and ");
			var_print_object(game, trail);
			var_append_temp(vars, " are inside");
		}

		/* Print out the container. */
		if (!supporting) {
			var_append_temp(vars, " ");
			var_print_object_np(game, associate);
		}
		var_append_temp(vars, ".");
	} else {
		if (supporting)
			var_append_temp(vars, ".");
	}
}


/*
 * var_return_integer()
 * var_return_string()
 *
 * Convenience helpers for var_get_system().  Provide convenience and some
 * mild syntactic sugar for making returning a value as a system variable
 * a bit easier.  Set appropriate values for return type and the relevant
 * return value field, and always return TRUE.  A macro was tempting here...
 */
static sc_bool var_return_integer(sc_int value, sc_int *type, sc_vartype_t *vt_rvalue) {
	*type = VAR_INTEGER;
	vt_rvalue->integer = value;
	return TRUE;
}

static sc_bool var_return_string(const sc_char *value, sc_int *type, sc_vartype_t *vt_rvalue) {
	*type = VAR_STRING;
	vt_rvalue->string = value;
	return TRUE;
}


/*
 * var_get_system()
 *
 * Construct a system variable, and return its type and value, or FALSE
 * if invalid name passed in.  Uses var_return_*() to reduce code untidiness.
 */
static sc_bool var_get_system(sc_var_setref_t vars, const sc_char *name,
		sc_int *type, sc_vartype_t *vt_rvalue) {
	const sc_prop_setref_t bundle = vars->bundle;
	const sc_gameref_t game = vars->game;

	/* Check name for known system variables. */
	if (strcmp(name, "author") == 0) {
		sc_vartype_t vt_key[2];
		const sc_char *author;

		/* Get and return the global gameauthor string. */
		vt_key[0].string = "Globals";
		vt_key[1].string = "GameAuthor";
		author = prop_get_string(bundle, "S<-ss", vt_key);
		if (sc_strempty(author))
			author = "[Author unknown]";

		return var_return_string(author, type, vt_rvalue);
	}

	else if (strcmp(name, "character") == 0) {
		/* See if there is a referenced character. */
		if (vars->referenced_character != -1) {
			sc_vartype_t vt_key[3];
			const sc_char *npc_name;

			/* Return the character name string. */
			vt_key[0].string = "NPCs";
			vt_key[1].integer = vars->referenced_character;
			vt_key[2].string = "Name";
			npc_name = prop_get_string(bundle, "S<-sis", vt_key);
			if (sc_strempty(npc_name))
				npc_name = "[Character unknown]";

			return var_return_string(npc_name, type, vt_rvalue);
		} else {
			sc_error("var_get_system: no referenced character yet\n");
			return var_return_string("[Character unknown]", type, vt_rvalue);
		}
	}

	else if (strcmp(name, "heshe") == 0 || strcmp(name, "himher") == 0) {
		/* See if there is a referenced character. */
		if (vars->referenced_character != -1) {
			sc_vartype_t vt_key[3];
			sc_int gender;
			const sc_char *retval;

			/* Return the appropriate character gender string. */
			vt_key[0].string = "NPCs";
			vt_key[1].integer = vars->referenced_character;
			vt_key[2].string = "Gender";
			gender = prop_get_integer(bundle, "I<-sis", vt_key);
			switch (gender) {
			case NPC_MALE:
				retval = (strcmp(name, "heshe") == 0) ? "he" : "him";
				break;
			case NPC_FEMALE:
				retval = (strcmp(name, "heshe") == 0) ? "she" : "her";
				break;
			case NPC_NEUTER:
				retval = "it";
				break;

			default:
				sc_error("var_get_system: unknown gender, %ld\n", gender);
				retval = "[Gender unknown]";
				break;
			}
			return var_return_string(retval, type, vt_rvalue);
		} else {
			sc_error("var_get_system: no referenced character yet\n");
			return var_return_string("[Gender unknown]", type, vt_rvalue);
		}
	}

	else if (strncmp(name, "in_", 3) == 0) {
		sc_int saved_ref_object = vars->referenced_object;

		/* Check there's enough information to return a value. */
		if (!game) {
			sc_error("var_get_system: no game for in_\n");
			return var_return_string("[In_ unavailable]", type, vt_rvalue);
		}
		if (!uip_match("%object%", name + 3, game)) {
			sc_error("var_get_system: invalid object for in_\n");
			return var_return_string("[In_ unavailable]", type, vt_rvalue);
		}

		/* Clear any current temporary for appends. */
		vars->temporary = (sc_char *)sc_realloc(vars->temporary, 1);
		strcpy(vars->temporary, "");

		/* Write what's in the object into temporary. */
		var_list_in_object(game, vars->referenced_object);

		/* Restore saved referenced object and return. */
		vars->referenced_object = saved_ref_object;
		return var_return_string(vars->temporary, type, vt_rvalue);
	}

	else if (strcmp(name, "maxscore") == 0) {
		sc_vartype_t vt_key[2];
		sc_int maxscore;

		/* Return the maximum score. */
		vt_key[0].string = "Globals";
		vt_key[1].string = "MaxScore";
		maxscore = prop_get_integer(bundle, "I<-ss", vt_key);

		return var_return_integer(maxscore, type, vt_rvalue);
	}

	else if (strcmp(name, "modified") == 0) {
		sc_vartype_t vt_key;
		const sc_char *compiledate;

		/* Return the game compilation date. */
		vt_key.string = "CompileDate";
		compiledate = prop_get_string(bundle, "S<-s", &vt_key);
		if (sc_strempty(compiledate))
			compiledate = "[Modified unknown]";

		return var_return_string(compiledate, type, vt_rvalue);
	}

	else if (strcmp(name, "number") == 0) {
		/* Return the referenced number, or 0 if none yet. */
		if (!vars->is_number_referenced)
			sc_error("var_get_system: no referenced number yet\n");

		return var_return_integer(vars->referenced_number, type, vt_rvalue);
	}

	else if (strcmp(name, "object") == 0) {
		/* See if we have a referenced object yet. */
		if (vars->referenced_object != -1) {
			/* Return object name with its prefix. */
			sc_vartype_t vt_key[3];
			const sc_char *prefix, *objname;

			vt_key[0].string = "Objects";
			vt_key[1].integer = vars->referenced_object;
			vt_key[2].string = "Prefix";
			prefix = prop_get_string(bundle, "S<-sis", vt_key);

			vars->temporary = (sc_char *)sc_realloc(vars->temporary, strlen(prefix) + 1);
			strcpy(vars->temporary, prefix);

			vt_key[2].string = "Short";
			objname = prop_get_string(bundle, "S<-sis", vt_key);

			vars->temporary = (sc_char *)sc_realloc(vars->temporary,
			                                        strlen(vars->temporary)
			                                        + strlen(objname) + 2);
			strcat(vars->temporary, " ");
			strcat(vars->temporary, objname);

			return var_return_string(vars->temporary, type, vt_rvalue);
		} else {
			sc_error("var_get_system: no referenced object yet\n");
			return var_return_string("[Object unknown]", type, vt_rvalue);
		}
	}

	else if (strcmp(name, "obstate") == 0) {
		sc_vartype_t vt_key[3];
		sc_bool is_statussed;
		sc_char *state;

		/* Check there's enough information to return a value. */
		if (!game) {
			sc_error("var_get_system: no game for obstate\n");
			return var_return_string("[Obstate unavailable]", type, vt_rvalue);
		}
		if (vars->referenced_object == -1) {
			sc_error("var_get_system: no object for obstate\n");
			return var_return_string("[Obstate unavailable]", type, vt_rvalue);
		}

		/*
		 * If not a stateful object, Runner 4.0.45 crashes; we'll do something
		 * different here.
		 */
		vt_key[0].string = "Objects";
		vt_key[1].integer = vars->referenced_object;
		vt_key[2].string = "CurrentState";
		is_statussed = prop_get_integer(bundle, "I<-sis", vt_key) != 0;
		if (!is_statussed)
			return var_return_string("stateless", type, vt_rvalue);

		/* Get state, and copy to temporary. */
		state = obj_state_name(game, vars->referenced_object);
		if (!state) {
			sc_error("var_get_system: invalid state for obstate\n");
			return var_return_string("[Obstate unknown]", type, vt_rvalue);
		}
		vars->temporary = (sc_char *)sc_realloc(vars->temporary, strlen(state) + 1);
		strcpy(vars->temporary, state);
		sc_free(state);

		/* Return temporary. */
		return var_return_string(vars->temporary, type, vt_rvalue);
	}

	else if (strcmp(name, "obstatus") == 0) {
		sc_vartype_t vt_key[3];
		sc_bool is_openable;
		sc_int openness;
		const sc_char *retval;

		/* Check there's enough information to return a value. */
		if (!game) {
			sc_error("var_get_system: no game for obstatus\n");
			return var_return_string("[Obstatus unavailable]", type, vt_rvalue);
		}
		if (vars->referenced_object == -1) {
			sc_error("var_get_system: no object for obstatus\n");
			return var_return_string("[Obstatus unavailable]", type, vt_rvalue);
		}

		/* If not an openable object, return unopenable to match Adrift. */
		vt_key[0].string = "Objects";
		vt_key[1].integer = vars->referenced_object;
		vt_key[2].string = "Openable";
		is_openable = prop_get_integer(bundle, "I<-sis", vt_key) != 0;
		if (!is_openable)
			return var_return_string("unopenable", type, vt_rvalue);

		/* Return one of open, closed, or locked. */
		openness = gs_object_openness(game, vars->referenced_object);
		switch (openness) {
		case OBJ_OPEN:
			retval = "open";
			break;
		case OBJ_CLOSED:
			retval = "closed";
			break;
		case OBJ_LOCKED:
			retval = "locked";
			break;
		default:
			retval = "[Obstatus unknown]";
			break;
		}
		return var_return_string(retval, type, vt_rvalue);
	}

	else if (strncmp(name, "on_", 3) == 0) {
		sc_int saved_ref_object = vars->referenced_object;

		/* Check there's enough information to return a value. */
		if (!game) {
			sc_error("var_get_system: no game for on_\n");
			return var_return_string("[On_ unavailable]", type, vt_rvalue);
		}
		if (!uip_match("%object%", name + 3, game)) {
			sc_error("var_get_system: invalid object for on_\n");
			return var_return_string("[On_ unavailable]", type, vt_rvalue);
		}

		/* Clear any current temporary for appends. */
		vars->temporary = (sc_char *)sc_realloc(vars->temporary, 1);
		strcpy(vars->temporary, "");

		/* Write what's on the object into temporary. */
		var_list_on_object(game, vars->referenced_object);

		/* Restore saved referenced object and return. */
		vars->referenced_object = saved_ref_object;
		return var_return_string(vars->temporary, type, vt_rvalue);
	}

	else if (strncmp(name, "onin_", 5) == 0) {
		sc_int saved_ref_object = vars->referenced_object;

		/* Check there's enough information to return a value. */
		if (!game) {
			sc_error("var_get_system: no game for onin_\n");
			return var_return_string("[Onin_ unavailable]", type, vt_rvalue);
		}
		if (!uip_match("%object%", name + 5, game)) {
			sc_error("var_get_system: invalid object for onin_\n");
			return var_return_string("[Onin_ unavailable]", type, vt_rvalue);
		}

		/* Clear any current temporary for appends. */
		vars->temporary = (sc_char *)sc_realloc(vars->temporary, 1);
		strcpy(vars->temporary, "");

		/* Write what's on/in the object into temporary. */
		var_list_onin_object(game, vars->referenced_object);

		/* Restore saved referenced object and return. */
		vars->referenced_object = saved_ref_object;
		return var_return_string(vars->temporary, type, vt_rvalue);
	}

	else if (strcmp(name, "player") == 0) {
		sc_vartype_t vt_key[2];
		const sc_char *playername;

		/*
		 * Return player's name from properties, or just "Player" if not set
		 * in the properties.
		 */
		vt_key[0].string = "Globals";
		vt_key[1].string = "PlayerName";
		playername = prop_get_string(bundle, "S<-ss", vt_key);
		if (sc_strempty(playername))
			playername = "Player";

		return var_return_string(playername, type, vt_rvalue);
	}

	else if (strcmp(name, "room") == 0) {
		const sc_char *roomname;

		/* Check there's enough information to return a value. */
		if (!game) {
			sc_error("var_get_system: no game for room\n");
			return var_return_string("[Room unavailable]", type, vt_rvalue);
		}

		/* Return the current player room. */
		roomname = lib_get_room_name(game, gs_playerroom(game));
		return var_return_string(roomname, type, vt_rvalue);
	}

	else if (strcmp(name, "score") == 0) {
		/* Check there's enough information to return a value. */
		if (!game) {
			sc_error("var_get_system: no game for score\n");
			return var_return_integer(0, type, vt_rvalue);
		}

		/* Return the current game score. */
		return var_return_integer(game->score, type, vt_rvalue);
	}

	else if (strncmp(name, "state_", 6) == 0) {
		sc_int saved_ref_object = vars->referenced_object;
		sc_vartype_t vt_key[3];
		sc_bool is_statussed;
		sc_char *state;

		/* Check there's enough information to return a value. */
		if (!game) {
			sc_error("var_get_system: no game for state_\n");
			return var_return_string("[State_ unavailable]", type, vt_rvalue);
		}
		if (!uip_match("%object%", name + 6, game)) {
			sc_error("var_get_system: invalid object for state_\n");
			return var_return_string("[State_ unavailable]", type, vt_rvalue);
		}

		/* Verify this is a stateful object. */
		vt_key[0].string = "Objects";
		vt_key[1].integer = vars->referenced_object;
		vt_key[2].string = "CurrentState";
		is_statussed = prop_get_integer(bundle, "I<-sis", vt_key) != 0;
		if (!is_statussed) {
			vars->referenced_object = saved_ref_object;
			sc_error("var_get_system: stateless object for state_\n");
			return var_return_string("[State_ unavailable]", type, vt_rvalue);
		}

		/* Get state, and copy to temporary. */
		state = obj_state_name(game, vars->referenced_object);
		if (!state) {
			vars->referenced_object = saved_ref_object;
			sc_error("var_get_system: invalid state for state_\n");
			return var_return_string("[State_ unknown]", type, vt_rvalue);
		}
		vars->temporary = (sc_char *)sc_realloc(vars->temporary, strlen(state) + 1);
		strcpy(vars->temporary, state);
		sc_free(state);

		/* Restore saved referenced object and return. */
		vars->referenced_object = saved_ref_object;
		return var_return_string(vars->temporary, type, vt_rvalue);
	}

	else if (strncmp(name, "status_", 7) == 0) {
		sc_int saved_ref_object = vars->referenced_object;
		sc_vartype_t vt_key[3];
		sc_bool is_openable;
		sc_int openness;
		const sc_char *retval;

		/* Check there's enough information to return a value. */
		if (!game) {
			sc_error("var_get_system: no game for status_\n");
			return var_return_string("[Status_ unavailable]", type, vt_rvalue);
		}
		if (!uip_match("%object%", name + 7, game)) {
			sc_error("var_get_system: invalid object for status_\n");
			return var_return_string("[Status_ unavailable]", type, vt_rvalue);
		}

		/* Verify this is an openable object. */
		vt_key[0].string = "Objects";
		vt_key[1].integer = vars->referenced_object;
		vt_key[2].string = "Openable";
		is_openable = prop_get_integer(bundle, "I<-sis", vt_key) != 0;
		if (!is_openable) {
			vars->referenced_object = saved_ref_object;
			sc_error("var_get_system: stateless object for status_\n");
			return var_return_string("[Status_ unavailable]", type, vt_rvalue);
		}

		/* Return one of open, closed, or locked. */
		openness = gs_object_openness(game, vars->referenced_object);
		switch (openness) {
		case OBJ_OPEN:
			retval = "open";
			break;
		case OBJ_CLOSED:
			retval = "closed";
			break;
		case OBJ_LOCKED:
			retval = "locked";
			break;
		default:
			retval = "[Status_ unknown]";
			break;
		}

		/* Restore saved referenced object and return. */
		vars->referenced_object = saved_ref_object;
		return var_return_string(retval, type, vt_rvalue);
	}

	else if (strcmp(name, "t_number") == 0) {
		/* See if we have a referenced number yet. */
		if (vars->is_number_referenced) {
			sc_int number;
			const sc_char *retval;

			/* Return the referenced number as a string. */
			number = vars->referenced_number;
			if (number >= 0 && number < VAR_NUMBERS_SIZE)
				retval = VAR_NUMBERS[number];
			else {
				vars->temporary = (sc_char *)sc_realloc(vars->temporary, 32);
				sprintf(vars->temporary, "%ld", number);
				retval = vars->temporary;
			}

			return var_return_string(retval, type, vt_rvalue);
		} else {
			sc_error("var_get_system: no referenced number yet\n");
			return var_return_string("[Number unknown]", type, vt_rvalue);
		}
	}

	else if (strncmp(name, "t_", 2) == 0) {
		sc_varref_t var;

		/* Find the variable; must be a user, not a system, one. */
		var = var_find(vars, name + 2);
		if (!var) {
			sc_error("var_get_system:"
			         " no such variable, %s\n", name + 2);
			return var_return_string("[Unknown variable]", type, vt_rvalue);
		} else if (var->type != VAR_INTEGER) {
			sc_error("var_get_system:"
			         " not an integer variable, %s\n", name + 2);
			return var_return_string(var->value.string, type, vt_rvalue);
		} else {
			sc_int number;
			const sc_char *retval;

			/* Return the variable value as a string. */
			number = var->value.integer;
			if (number >= 0 && number < VAR_NUMBERS_SIZE)
				retval = VAR_NUMBERS[number];
			else {
				vars->temporary = (sc_char *)sc_realloc(vars->temporary, 32);
				sprintf(vars->temporary, "%ld", number);
				retval = vars->temporary;
			}

			return var_return_string(retval, type, vt_rvalue);
		}
	}

	else if (strcmp(name, "text") == 0) {
		const sc_char *retval;

		/* Return any referenced text, otherwise a neutral string. */
		if (vars->referenced_text)
			retval = vars->referenced_text;
		else {
			sc_error("var_get_system: no text yet to reference\n");
			retval = "[Text unknown]";
		}

		return var_return_string(retval, type, vt_rvalue);
	}

	else if (strcmp(name, "theobject") == 0) {
		/* See if we have a referenced object yet. */
		if (vars->referenced_object != -1) {
			/* Return object name prefixed with "the"... */
			sc_vartype_t vt_key[3];
			const sc_char *prefix, *normalized, *objname;

			vt_key[0].string = "Objects";
			vt_key[1].integer = vars->referenced_object;
			vt_key[2].string = "Prefix";
			prefix = prop_get_string(bundle, "S<-sis", vt_key);

			vars->temporary = (sc_char *)sc_realloc(vars->temporary, strlen(prefix) + 5);
			strcpy(vars->temporary, "");

			normalized = prefix;
			if (sc_compare_word(prefix, "a", 1)) {
				strcat(vars->temporary, "the");
				normalized = prefix + 1;
			} else if (sc_compare_word(prefix, "an", 2)) {
				strcat(vars->temporary, "the");
				normalized = prefix + 2;
			} else if (sc_compare_word(prefix, "the", 3)) {
				strcat(vars->temporary, "the");
				normalized = prefix + 3;
			} else if (sc_compare_word(prefix, "some", 4)) {
				strcat(vars->temporary, "the");
				normalized = prefix + 4;
			} else if (sc_strempty(prefix))
				strcat(vars->temporary, "the ");

			if (!sc_strempty(normalized)) {
				strcat(vars->temporary, normalized);
				strcat(vars->temporary, " ");
			} else if (normalized > prefix)
				strcat(vars->temporary, " ");

			vt_key[2].string = "Short";
			objname = prop_get_string(bundle, "S<-sis", vt_key);
			if (sc_compare_word(objname, "a", 1))
				objname += 1;
			else if (sc_compare_word(objname, "an", 2))
				objname += 2;
			else if (sc_compare_word(objname, "the", 3))
				objname += 3;
			else if (sc_compare_word(objname, "some", 4))
				objname += 4;

			vars->temporary = (sc_char *)sc_realloc(vars->temporary,
			                                        strlen(vars->temporary)
			                                        + strlen(objname) + 1);
			strcat(vars->temporary, objname);

			return var_return_string(vars->temporary, type, vt_rvalue);
		} else {
			sc_error("var_get_system: no referenced object yet\n");
			return var_return_string("[Object unknown]", type, vt_rvalue);
		}
	}

	else if (strcmp(name, "time") == 0) {
		double delta;
		sc_int retval;

		/* Return the elapsed game time in seconds. */
		delta = vars->timestamp - (g_vm->_events->getTotalPlayTicks() / 1000);
		retval = (sc_int) delta + vars->time_offset;

		return var_return_integer(retval, type, vt_rvalue);
	}

	else if (strcmp(name, "title") == 0) {
		sc_vartype_t vt_key[2];
		const sc_char *gamename;

		/* Return the game's title. */
		vt_key[0].string = "Globals";
		vt_key[1].string = "GameName";
		gamename = prop_get_string(bundle, "S<-ss", vt_key);
		if (sc_strempty(gamename))
			gamename = "[Title unknown]";

		return var_return_string(gamename, type, vt_rvalue);
	}

	else if (strcmp(name, "turns") == 0) {
		/* Check there's enough information to return a value. */
		if (!game) {
			sc_error("var_get_system: no game for turns\n");
			return var_return_integer(0, type, vt_rvalue);
		}

		/* Return the count of game turns. */
		return var_return_integer(game->turns, type, vt_rvalue);
	}

	else if (strcmp(name, "version") == 0) {
		/* Return the Adrift emulation level of SCARE. */
		return var_return_integer(SCARE_EMULATION, type, vt_rvalue);
	}

	else if (strcmp(name, "scare_version") == 0) {
		/* Private system variable, return SCARE's version number. */
		return var_return_integer(var_get_scare_version(), type, vt_rvalue);
	}

	return FALSE;
}


/*
 * var_get_user()
 *
 * Retrieve a user variable, and return its type and value, or FALSE if the
 * name passed in is not a defined user variable.
 */
static sc_bool var_get_user(sc_var_setref_t vars, const sc_char *name,
		sc_int *type, sc_vartype_t *vt_rvalue) {
	sc_varref_t var;

	/* Check user variables for a reference to the named variable. */
	var = var_find(vars, name);
	if (var) {
		/* Copy out variable details. */
		*type = var->type;
		switch (var->type) {
		case VAR_INTEGER:
			vt_rvalue->integer = var->value.integer;
			break;
		case VAR_STRING:
			vt_rvalue->string = var->value.string;
			break;

		default:
			sc_fatal("var_get_user: invalid variable type, %ld\n", var->type);
		}

		/* Return success. */
		return TRUE;
	}

	return FALSE;
}


/*
 * var_get()
 *
 * Retrieve a variable, and return its value and type.  Returns FALSE if the
 * named variable does not exist.
 */
sc_bool var_get(sc_var_setref_t vars, const sc_char *name, sc_int *type, sc_vartype_t *vt_rvalue) {
	sc_bool status;
	assert(var_is_valid(vars));
	assert(name && type && vt_rvalue);

	/*
	 * Check user and system variables for a reference to the name.  User
	 * variables take precedence over system ones; that is, they may override
	 * them in a game.
	 */
	status = var_get_user(vars, name, type, vt_rvalue);
	if (!status)
		status = var_get_system(vars, name, type, vt_rvalue);

	if (var_trace) {
		if (status) {
			sc_trace("Variable: %%%s%% retrieved, ", name);
			switch (*type) {
			case VAR_INTEGER:
				sc_trace("%ld", vt_rvalue->integer);
				break;
			case VAR_STRING:
				sc_trace("\"%s\"", vt_rvalue->string);
				break;

			default:
				sc_trace("Variable: invalid variable type, %ld\n", *type);
				break;
			}
			sc_trace("\n");
		} else
			sc_trace("Variable: \"%s\", no such variable\n", name);
	}

	return status;
}


/*
 * var_put_integer()
 * var_get_integer()
 *
 * Convenience functions to store and retrieve an integer variable.  It is
 * an error for the variable not to exist or to have the wrong type.
 */
void var_put_integer(sc_var_setref_t vars, const sc_char *name, sc_int value) {
	sc_vartype_t vt_value;
	assert(var_is_valid(vars));

	vt_value.integer = value;
	var_put(vars, name, VAR_INTEGER, vt_value);
}

sc_int var_get_integer(sc_var_setref_t vars, const sc_char *name) {
	sc_vartype_t vt_rvalue;
	sc_int type;
	assert(var_is_valid(vars));

	if (!var_get(vars, name, &type, &vt_rvalue))
		sc_fatal("var_get_integer: no such variable, %s\n", name);
	else if (type != VAR_INTEGER)
		sc_fatal("var_get_integer: not an integer, %s\n", name);

	return vt_rvalue.integer;
}


/*
 * var_put_string()
 * var_get_string()
 *
 * Convenience functions to store and retrieve a string variable.  It is
 * an error for the variable not to exist or to have the wrong type.
 */
void var_put_string(sc_var_setref_t vars, const sc_char *name, const sc_char *string) {
	sc_vartype_t vt_value;
	assert(var_is_valid(vars));

	vt_value.string = string;
	var_put(vars, name, VAR_STRING, vt_value);
}

const sc_char *var_get_string(sc_var_setref_t vars, const sc_char *name) {
	sc_vartype_t vt_rvalue;
	sc_int type;
	assert(var_is_valid(vars));

	if (!var_get(vars, name, &type, &vt_rvalue))
		sc_fatal("var_get_string: no such variable, %s\n", name);
	else if (type != VAR_STRING)
		sc_fatal("var_get_string: not a string, %s\n", name);

	return vt_rvalue.string;
}


/*
 * var_create()
 *
 * Create and return a new set of variables.  Variables are created from the
 * properties bundle passed in.
 */
sc_var_setref_t var_create(sc_prop_setref_t bundle) {
	sc_var_setref_t vars;
	sc_int var_count, index_;
	sc_vartype_t vt_key[3];
	assert(bundle);

	/* Create a clean set of variables to fill from the bundle. */
	vars = var_create_empty();
	vars->bundle = bundle;

	/* Retrieve the count of variables. */
	vt_key[0].string = "Variables";
	var_count = prop_get_child_count(bundle, "I<-s", vt_key);

	/* Create a variable for each variable property held. */
	for (index_ = 0; index_ < var_count; index_++) {
		const sc_char *name;
		sc_int var_type;
		const sc_char *value;

		/* Retrieve variable name, type, and string initial value. */
		vt_key[1].integer = index_;
		vt_key[2].string = "Name";
		name = prop_get_string(bundle, "S<-sis", vt_key);

		vt_key[2].string = "Type";
		var_type = prop_get_integer(bundle, "I<-sis", vt_key);

		vt_key[2].string = "Value";
		value = prop_get_string(bundle, "S<-sis", vt_key);

		/* Handle numerics and strings differently. */
		switch (var_type) {
		case TAFVAR_NUMERIC: {
			sc_int integer_value;
			if (sscanf(value, "%ld", &integer_value) != 1) {
				sc_error("var_create:"
				         " invalid numeric variable %s, %s\n", name, value);
				integer_value = 0;
			}
			var_put_integer(vars, name, integer_value);
			break;
		}

		case TAFVAR_STRING:
			var_put_string(vars, name, value);
			break;

		default:
			sc_fatal("var_create: invalid variable type, %ld\n", var_type);
		}
	}

	return vars;
}


/*
 * var_register_game()
 *
 * Register the game, used by variables to satisfy requests for selected
 * system variables.  To ensure integrity, the game being registered must
 * reference this variable set.
 */
void var_register_game(sc_var_setref_t vars, sc_gameref_t game) {
	assert(var_is_valid(vars));
	assert(gs_is_game_valid(game));

	if (vars != gs_get_vars(game))
		sc_fatal("var_register_game: game binding error\n");

	vars->game = game;
}


/*
 * var_set_ref_character()
 * var_set_ref_object()
 * var_set_ref_number()
 * var_set_ref_text()
 *
 * Set the "referenced" character, object, number, and text.
 */
void var_set_ref_character(sc_var_setref_t vars, sc_int character) {
	assert(var_is_valid(vars));
	vars->referenced_character = character;
}

void var_set_ref_object(sc_var_setref_t vars, sc_int object) {
	assert(var_is_valid(vars));
	vars->referenced_object = object;
}

void var_set_ref_number(sc_var_setref_t vars, sc_int number) {
	assert(var_is_valid(vars));
	vars->referenced_number = number;
	vars->is_number_referenced = TRUE;
}

void var_set_ref_text(sc_var_setref_t vars, const sc_char *text) {
	assert(var_is_valid(vars));

	/* Take a copy of the string, and retain it. */
	vars->referenced_text = (sc_char *)sc_realloc(vars->referenced_text, strlen(text) + 1);
	strcpy(vars->referenced_text, text);
}


/*
 * var_get_ref_character()
 * var_get_ref_object()
 * var_get_ref_number()
 * var_get_ref_text()
 *
 * Get the "referenced" character, object, number, and text.
 */
sc_int var_get_ref_character(sc_var_setref_t vars) {
	assert(var_is_valid(vars));
	return vars->referenced_character;
}

sc_int var_get_ref_object(sc_var_setref_t vars) {
	assert(var_is_valid(vars));
	return vars->referenced_object;
}

sc_int var_get_ref_number(sc_var_setref_t vars) {
	assert(var_is_valid(vars));
	return vars->referenced_number;
}

const sc_char *var_get_ref_text(sc_var_setref_t vars) {
	assert(var_is_valid(vars));

	/*
	 * If currently nullptr, return "".  A game may check restrictions involving
	 * referenced text before any value has been set; returning "" here for
	 * this case prevents problems later (strcmp (nullptr, ...), for example).
	 */
	return vars->referenced_text ? vars->referenced_text : "";
}


/*
 * var_get_elapsed_seconds()
 * var_set_elapsed_seconds()
 *
 * Get a count of seconds elapsed since the variables were created (start
 * of game), and set the count to a given value (game restore).
 */
sc_uint var_get_elapsed_seconds(sc_var_setref_t vars) {
	double delta;
	assert(var_is_valid(vars));

	delta = vars->timestamp - g_vm->_events->getTotalPlayTicks();
	return (sc_uint) delta + vars->time_offset;
}

void var_set_elapsed_seconds(sc_var_setref_t vars, sc_uint seconds) {
	assert(var_is_valid(vars));

	/*
	 * Reset the timestamp to now, and store seconds in offset.  This is sort-of
	 * forced by the fact that ANSI offers difftime but no 'settime' -- here,
	 * we'd really want to set the timestamp to now less seconds.
	 */
	vars->timestamp = g_vm->_events->getTotalPlayTicks() / 1000;
	vars->time_offset = seconds;
}


/*
 * var_debug_trace()
 *
 * Set variable tracing on/off.
 */
void var_debug_trace(sc_bool flag) {
	var_trace = flag;
}


/*
 * var_debug_dump()
 *
 * Print out a complete variables set.
 */
void var_debug_dump(sc_var_setref_t vars) {
	sc_int index_;
	sc_varref_t var;
	assert(var_is_valid(vars));

	/* Dump complete structure. */
	sc_trace("Variable: debug dump follows...\n");
	sc_trace("vars->bundle = %p\n", (void *) vars->bundle);
	sc_trace("vars->referenced_character = %ld\n", vars->referenced_character);
	sc_trace("vars->referenced_object = %ld\n", vars->referenced_object);
	sc_trace("vars->referenced_number = %ld\n", vars->referenced_number);
	sc_trace("vars->is_number_referenced = %s\n",
	         vars->is_number_referenced ? "true" : "false");

	sc_trace("vars->referenced_text = ");
	if (vars->referenced_text)
		sc_trace("\"%s\"\n", vars->referenced_text);
	else
		sc_trace("(nil)\n");

	sc_trace("vars->temporary = %p\n", (void *) vars->temporary);
	sc_trace("vars->timestamp = %lu\n", (sc_uint) vars->timestamp);
	sc_trace("vars->game = %p\n", (void *) vars->game);

	sc_trace("vars->variables =\n");
	for (index_ = 0; index_ < VAR_HASH_TABLE_SIZE; index_++) {
		for (var = vars->variable[index_]; var; var = var->next) {
			if (var == vars->variable[index_])
				sc_trace("%3ld : ", index_);
			else
				sc_trace("    : ");
			switch (var->type) {
			case VAR_STRING:
				sc_trace("[String ] %s = \"%s\"", var->name, var->value.string);
				break;
			case VAR_INTEGER:
				sc_trace("[Integer] %s = %ld", var->name, var->value.integer);
				break;

			default:
				sc_trace("[Invalid] %s = %p", var->name, var->value.voidp);
				break;
			}
			sc_trace("\n");
		}
	}
}

} // End of namespace Adrift
} // End of namespace Glk