Debian Codegraph @

terps/scare/scvars.c

raw symbols SCIP
1 /* vi: set ts=2 shiftwidth=2 expandtab:
2 *
3 * Copyright (C) 2003-2008 Simon Baldwin and Mark J. Tilford
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17 * USA
18 */
19
20 /*
21 * Module notes:
22 *
23 * o Gender enumerations are 0/1/2, but 1/2/3 in jAsea. The 0/1/2 values
24 * seem to be right. Is jAsea off by one?
25 *
26 * o jAsea tries to read Globals.CompileDate. It's just CompileDate.
27 *
28 * o State_ and obstate are implemented, but not fully tested due to a lack
29 * of games that use them.
30 */
31
32 #include <assert.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <time.h>
37
38 #include "scare.h"
39 #include "scprotos.h"
40 #include "scgamest.h"
41
42
43 /* Assorted definitions and constants. */
44 static const sc_uint VARS_MAGIC = 0xabcc7a71;
45 static const sc_char NUL = '\0';
46
47 /* Variables trace flag. */
48 static sc_bool var_trace = FALSE;
49
50 /* Table of numbers zero to twenty spelled out. */
51 enum { VAR_NUMBERS_SIZE = 21 };
52 static const sc_char *const VAR_NUMBERS[VAR_NUMBERS_SIZE] = {
53 "zero", "one", "two", "three", "four", "five", "six", "seven", "eight",
54 "nine", "ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen",
55 "sixteen", "seventeen", "eighteen", "nineteen", "twenty"
56 };
57
58 /* Variable entry, held on a list hashed by variable name. */
59 typedef struct sc_var_s
60 {
61 struct sc_var_s *next;
62
63 const sc_char *name;
64 sc_int type;
65 sc_vartype_t value;
66 } sc_var_t;
67 typedef sc_var_t *sc_varref_t;
68
69 /*
70 * Variables set structure. A self-contained set of variables on which
71 * variables functions operate. 211 is prime, making it a reasonable hash
72 * divisor. There's no rehashing here; few games, if any, are likely to
73 * exceed a fill factor of two (~422 variables).
74 */
75 enum { VAR_HASH_TABLE_SIZE = 211 };
76 typedef struct sc_var_set_s
77 {
78 sc_uint magic;
79 sc_prop_setref_t bundle;
80 sc_int referenced_character;
81 sc_int referenced_object;
82 sc_int referenced_number;
83 sc_bool is_number_referenced;
84 sc_char *referenced_text;
85 sc_char *temporary;
86 time_t timestamp;
87 sc_uint time_offset;
88 sc_gameref_t game;
89 sc_varref_t variable[VAR_HASH_TABLE_SIZE];
90 } sc_var_set_t;
91
92
93 /*
94 * var_is_valid()
95 *
96 * Return TRUE if pointer is a valid variables set, FALSE otherwise.
97 */
98 static sc_bool
99 var_is_valid (sc_var_setref_t vars)
100 {
101 return vars && vars->magic == VARS_MAGIC;
102 }
103
104
105 /*
106 * var_hash_name()
107 *
108 * Hash a variable name, modulo'ed to the number of buckets.
109 */
110 static sc_uint
111 var_hash_name (const sc_char *name)
112 {
113 return sc_hash (name) % VAR_HASH_TABLE_SIZE;
114 }
115
116
117 /*
118 * var_create_empty()
119 *
120 * Create and return a new empty set of variables.
121 */
122 static sc_var_setref_t
123 var_create_empty (void)
124 {
125 sc_var_setref_t vars;
126 sc_int index_;
127
128 /* Create a clean set of variables. */
129 vars = sc_malloc (sizeof (*vars));
130 vars->magic = VARS_MAGIC;
131 vars->bundle = NULL;
132 vars->referenced_character = -1;
133 vars->referenced_object = -1;
134 vars->referenced_number = 0;
135 vars->is_number_referenced = FALSE;
136 vars->referenced_text = NULL;
137 vars->temporary = NULL;
138 vars->timestamp = time (NULL);
139 vars->time_offset = 0;
140 vars->game = NULL;
141
142 /* Clear all variable hash lists. */
143 for (index_ = 0; index_ < VAR_HASH_TABLE_SIZE; index_++)
144 vars->variable[index_] = NULL;
145
146 return vars;
147 }
148
149
150 /*
151 * var_destroy()
152 *
153 * Destroy a variable set, and free its heap memory.
154 */
155 void
156 var_destroy (sc_var_setref_t vars)
157 {
158 sc_int index_;
159 assert (var_is_valid (vars));
160
161 /*
162 * Free the content of each string variable, and variable entry. String
163 * variable content needs to use mutable string instead of const string.
164 */
165 for (index_ = 0; index_ < VAR_HASH_TABLE_SIZE; index_++)
166 {
167 sc_varref_t var, next;
168
169 for (var = vars->variable[index_]; var; var = next)
170 {
171 next = var->next;
172 if (var->type == VAR_STRING)
173 sc_free (var->value.mutable_string);
174 sc_free (var);
175 }
176 }
177
178 /* Free any temporary and reference text storage area. */
179 sc_free (vars->temporary);
180 sc_free (vars->referenced_text);
181
182 /* Poison and free the variable set itself. */
183 memset (vars, 0xaa, sizeof (*vars));
184 sc_free (vars);
185 }
186
187
188 /*
189 * var_find()
190 * var_add()
191 *
192 * Find and return a pointer to a named variable structure, or NULL if no such
193 * variable exists, and add a new variable structure to the lists.
194 */
195 static sc_varref_t
196 var_find (sc_var_setref_t vars, const sc_char *name)
197 {
198 sc_uint hash;
199 sc_varref_t var;
200
201 /* Hash name, search list and return if name match found. */
202 hash = var_hash_name (name);
203 for (var = vars->variable[hash]; var; var = var->next)
204 {
205 if (strcmp (name, var->name) == 0)
206 break;
207 }
208
209 /* Return variable, or NULL if no such variable. */
210 return var;
211 }
212
213 static sc_varref_t
214 var_add (sc_var_setref_t vars, const sc_char *name, sc_int type)
215 {
216 sc_varref_t var;
217 sc_uint hash;
218
219 /* Create a new variable entry. */
220 var = sc_malloc (sizeof (*var));
221 var->name = name;
222 var->type = type;
223 var->value.voidp = NULL;
224
225 /* Hash its name, and insert it at start of the relevant list. */
226 hash = var_hash_name (name);
227 var->next = vars->variable[hash];
228 vars->variable[hash] = var;
229
230 return var;
231 }
232
233
234 /*
235 * var_get_scare_version()
236 *
237 * Return the value of %scare_version%. Used to generate the system version
238 * of this variable, and to re-initialize user versions initialized to zero.
239 */
240 static sc_int
241 var_get_scare_version (void)
242 {
243 sc_int major, minor, point, version;
244
245 if (sscanf (SCARE_VERSION, "%ld.%ld.%ld", &major, &minor, &point) != 3)
246 {
247 sc_error ("var_get_scare_version: unable to generate scare_version\n");
248 return 0;
249 }
250
251 version = major * 10000 + minor * 100 + point;
252 return version;
253 }
254
255
256 /*
257 * var_put()
258 *
259 * Store a variable type in a named variable. If not present, the variable
260 * is created. Type is one of 'I' or 'S' for integer or string.
261 */
262 void
263 var_put (sc_var_setref_t vars,
264 const sc_char *name, sc_int type, sc_vartype_t vt_value)
265 {
266 sc_varref_t var;
267 sc_bool is_modification;
268 assert (var_is_valid (vars));
269 assert (name);
270
271 /* Check type is either integer or string. */
272 switch (type)
273 {
274 case VAR_INTEGER:
275 case VAR_STRING:
276 break;
277
278 default:
279 sc_fatal ("var_put: invalid variable type, %ld\n", type);
280 }
281
282 /* See if the user variable already exists. */
283 var = var_find (vars, name);
284 if (var)
285 {
286 /* Verify that nothing is trying to change the variable's type. */
287 if (var->type != type)
288 sc_fatal ("var_put: variable type changed, %s\n", name);
289
290 /*
291 * Special case %scare_version%. If a game changes its value, it may
292 * compromise version checking, so warn here, but continue.
293 */
294 if (strcmp (name, "scare_version") == 0)
295 {
296 if (var->value.integer != vt_value.integer)
297 sc_error ("var_put: warning: %%%s%% value changed\n", name);
298 }
299
300 is_modification = TRUE;
301 }
302 else
303 {
304 /*
305 * Special case %scare_version%. If a game defines this and initializes
306 * it to zero, re-initialize it to SCARE's version number. Games that
307 * define %scare_version%, initially zero, can use this to test if
308 * running under SCARE or Runner.
309 */
310 if (strcmp (name, "scare_version") == 0 && vt_value.integer == 0)
311 {
312 vt_value.integer = var_get_scare_version ();
313
314 if (var_trace)
315 sc_trace ("Variable: %%%s%% [new] caught and mapped\n", name);
316 }
317
318 /*
319 * Create a new and empty variable entry. The mutable string needs to
320 * be set to NULL here so that realloc works correctly on assigning
321 * the value below.
322 */
323 var = var_add (vars, name, type);
324 var->value.mutable_string = NULL;
325
326 is_modification = FALSE;
327 }
328
329 /* Update the existing variable, or populate the new one fully. */
330 switch (var->type)
331 {
332 case VAR_INTEGER:
333 var->value.integer = vt_value.integer;
334 break;
335
336 case VAR_STRING:
337 /* Use mutable string instead of const string. */
338 var->value.mutable_string = sc_realloc (var->value.mutable_string,
339 strlen (vt_value.string) + 1);
340 strcpy (var->value.mutable_string, vt_value.string);
341 break;
342
343 default:
344 sc_fatal ("var_put: invalid variable type, %ld\n", var->type);
345 }
346
347 if (var_trace)
348 {
349 sc_trace ("Variable: %%%s%%%s = ",
350 name, is_modification ? "" : " [new]");
351 switch (var->type)
352 {
353 case VAR_INTEGER:
354 sc_trace ("%ld", var->value.integer);
355 break;
356 case VAR_STRING:
357 sc_trace ("\"%s\"", var->value.string);
358 break;
359
360 default:
361 sc_trace ("[invalid variable type, %ld]", var->type);
362 break;
363 }
364 sc_trace ("\n");
365 }
366 }
367
368
369 /*
370 * var_append_temp()
371 *
372 * Helper for object listers. Extends temporary, and appends the given text
373 * to the string.
374 */
375 static void
376 var_append_temp (sc_var_setref_t vars, const sc_char *string)
377 {
378 sc_bool new_sentence;
379 sc_int noted;
380
381 if (!vars->temporary)
382 {
383 /* Create a new temporary area and copy string. */
384 new_sentence = TRUE;
385 noted = 0;
386 vars->temporary = sc_malloc (strlen (string) + 1);
387 strcpy (vars->temporary, string);
388 }
389 else
390 {
391 /* Append string to existing temporary. */
392 new_sentence = (vars->temporary[0] == NUL);
393 noted = strlen (vars->temporary);
394 vars->temporary = sc_realloc (vars->temporary,
395 strlen (vars->temporary) +
396 strlen (string) + 1);
397 strcat (vars->temporary, string);
398 }
399
400 if (new_sentence)
401 vars->temporary[noted] = sc_toupper (vars->temporary[noted]);
402 }
403
404
405 /*
406 * var_print_object_np
407 * var_print_object
408 *
409 * Convenience functions to append an object's name, with and without any
410 * prefix, to variables temporary.
411 */
412 static void
413 var_print_object_np (sc_gameref_t game, sc_int object)
414 {
415 const sc_var_setref_t vars = gs_get_vars (game);
416 const sc_prop_setref_t bundle = gs_get_bundle (game);
417 sc_vartype_t vt_key[3];
418 const sc_char *prefix, *normalized, *name;
419
420 /* Get the object's prefix. */
421 vt_key[0].string = "Objects";
422 vt_key[1].integer = object;
423 vt_key[2].string = "Prefix";
424 prefix = prop_get_string (bundle, "S<-sis", vt_key);
425
426 /*
427 * Try the same shenanigans as done by the equivalent function in the
428 * library.
429 */
430 normalized = prefix;
431 if (sc_compare_word (prefix, "a", 1))
432 {
433 normalized = prefix + 1;
434 var_append_temp (vars, "the");
435 }
436 else if (sc_compare_word (prefix, "an", 2))
437 {
438 normalized = prefix + 2;
439 var_append_temp (vars, "the");
440 }
441 else if (sc_compare_word (prefix, "the", 3))
442 {
443 normalized = prefix + 3;
444 var_append_temp (vars, "the");
445 }
446 else if (sc_compare_word (prefix, "some", 4))
447 {
448 normalized = prefix + 4;
449 var_append_temp (vars, "the");
450 }
451 else if (sc_strempty (prefix))
452 var_append_temp (vars, "the ");
453
454 /* As with the library, handle the remaining prefix. */
455 if (!sc_strempty (normalized))
456 {
457 var_append_temp (vars, normalized);
458 var_append_temp (vars, " ");
459 }
460 else if (normalized > prefix)
461 var_append_temp (vars, " ");
462
463 /*
464 * Print the object's name, again, as with the library, stripping any
465 * leading article
466 */
467 vt_key[2].string = "Short";
468 name = prop_get_string (bundle, "S<-sis", vt_key);
469 if (sc_compare_word (name, "a", 1))
470 name += 1;
471 else if (sc_compare_word (name, "an", 2))
472 name += 2;
473 else if (sc_compare_word (name, "the", 3))
474 name += 3;
475 else if (sc_compare_word (name, "some", 4))
476 name += 4;
477 var_append_temp (vars, name);
478 }
479
480 static void
481 var_print_object (sc_gameref_t game, sc_int object)
482 {
483 const sc_var_setref_t vars = gs_get_vars (game);
484 const sc_prop_setref_t bundle = gs_get_bundle (game);
485 sc_vartype_t vt_key[3];
486 const sc_char *prefix, *name;
487
488 /*
489 * Get the object's prefix. As with the library, if the prefix is empty,
490 * put in an "a ".
491 */
492 vt_key[0].string = "Objects";
493 vt_key[1].integer = object;
494 vt_key[2].string = "Prefix";
495 prefix = prop_get_string (bundle, "S<-sis", vt_key);
496 if (!sc_strempty (prefix))
497 {
498 var_append_temp (vars, prefix);
499 var_append_temp (vars, " ");
500 }
501 else
502 var_append_temp (vars, "a ");
503
504 /* Print the object's name. */
505 vt_key[2].string = "Short";
506 name = prop_get_string (bundle, "S<-sis", vt_key);
507 var_append_temp (vars, name);
508 }
509
510
511 /*
512 * var_select_plurality()
513 *
514 * Convenience function for listers. Selects one of two responses depending
515 * on whether an object appears singular or plural.
516 */
517 static const sc_char *
518 var_select_plurality (sc_gameref_t game, sc_int object,
519 const sc_char *singular, const sc_char *plural)
520 {
521 return obj_appears_plural (game, object) ? plural : singular;
522 }
523
524
525 /*
526 * var_list_in_object()
527 *
528 * List the objects in a given container object.
529 */
530 static void
531 var_list_in_object (sc_gameref_t game, sc_int container)
532 {
533 const sc_var_setref_t vars = gs_get_vars (game);
534 sc_int object, count, trail;
535
536 /* List out the objects contained in this object. */
537 count = 0;
538 trail = -1;
539 for (object = 0; object < gs_object_count (game); object++)
540 {
541 /* Contained? */
542 if (gs_object_position (game, object) == OBJ_IN_OBJECT
543 && gs_object_parent (game, object) == container)
544 {
545 if (count > 0)
546 {
547 if (count > 1)
548 var_append_temp (vars, ", ");
549
550 /* Print out the current list object. */
551 var_print_object (game, trail);
552 }
553 trail = object;
554 count++;
555 }
556 }
557 if (count >= 1)
558 {
559 /* Print out final listed object. */
560 if (count == 1)
561 {
562 var_print_object (game, trail);
563 var_append_temp (vars,
564 var_select_plurality (game, trail,
565 " is inside ",
566 " are inside "));
567 }
568 else
569 {
570 var_append_temp (vars, " and ");
571 var_print_object (game, trail);
572 var_append_temp (vars, " are inside ");
573 }
574
575 /* Print out the container. */
576 var_print_object_np (game, container);
577 var_append_temp (vars, ".");
578 }
579 }
580
581
582 /*
583 * var_list_on_object()
584 *
585 * List the objects on a given surface object.
586 */
587 static void
588 var_list_on_object (sc_gameref_t game, sc_int supporter)
589 {
590 const sc_var_setref_t vars = gs_get_vars (game);
591 sc_int object, count, trail;
592
593 /* List out the objects standing on this object. */
594 count = 0;
595 trail = -1;
596 for (object = 0; object < gs_object_count (game); object++)
597 {
598 /* Standing on? */
599 if (gs_object_position (game, object) == OBJ_ON_OBJECT
600 && gs_object_parent (game, object) == supporter)
601 {
602 if (count > 0)
603 {
604 if (count > 1)
605 var_append_temp (vars, ", ");
606
607 /* Print out the current list object. */
608 var_print_object (game, trail);
609 }
610 trail = object;
611 count++;
612 }
613 }
614 if (count >= 1)
615 {
616 /* Print out final listed object. */
617 if (count == 1)
618 {
619 var_print_object (game, trail);
620 var_append_temp (vars,
621 var_select_plurality (game, trail,
622 " is on ", " are on "));
623 }
624 else
625 {
626 var_append_temp (vars, " and ");
627 var_print_object (game, trail);
628 var_append_temp (vars, " are on ");
629 }
630
631 /* Print out the surface. */
632 var_print_object_np (game, supporter);
633 var_append_temp (vars, ".");
634 }
635 }
636
637
638 /*
639 * var_list_onin_object()
640 *
641 * List the objects on and in a given associate object.
642 */
643 static void
644 var_list_onin_object (sc_gameref_t game, sc_int associate)
645 {
646 const sc_var_setref_t vars = gs_get_vars (game);
647 sc_int object, count, trail;
648 sc_bool supporting;
649
650 /* List out the objects standing on this object. */
651 count = 0;
652 trail = -1;
653 supporting = FALSE;
654 for (object = 0; object < gs_object_count (game); object++)
655 {
656 /* Standing on? */
657 if (gs_object_position (game, object) == OBJ_ON_OBJECT
658 && gs_object_parent (game, object) == associate)
659 {
660 if (count > 0)
661 {
662 if (count > 1)
663 var_append_temp (vars, ", ");
664
665 /* Print out the current list object. */
666 var_print_object (game, trail);
667 }
668 trail = object;
669 count++;
670 }
671 }
672 if (count >= 1)
673 {
674 /* Print out final listed object. */
675 if (count == 1)
676 {
677 var_print_object (game, trail);
678 var_append_temp (vars,
679 var_select_plurality (game, trail,
680 " is on ", " are on "));
681 }
682 else
683 {
684 var_append_temp (vars, " and ");
685 var_print_object (game, trail);
686 var_append_temp (vars, " are on ");
687 }
688
689 /* Print out the surface. */
690 var_print_object_np (game, associate);
691 supporting = TRUE;
692 }
693
694 /* List out the objects contained in this object. */
695 count = 0;
696 trail = -1;
697 for (object = 0; object < gs_object_count (game); object++)
698 {
699 /* Contained? */
700 if (gs_object_position (game, object) == OBJ_IN_OBJECT
701 && gs_object_parent (game, object) == associate)
702 {
703 if (count > 0)
704 {
705 if (count == 1)
706 {
707 if (supporting)
708 var_append_temp (vars, ", and ");
709 }
710 else
711 var_append_temp (vars, ", ");
712
713 /* Print out the current list object. */
714 var_print_object (game, trail);
715 }
716 trail = object;
717 count++;
718 }
719 }
720 if (count >= 1)
721 {
722 /* Print out final listed object. */
723 if (count == 1)
724 {
725 if (supporting)
726 var_append_temp (vars, ", and ");
727 var_print_object (game, trail);
728 var_append_temp (vars,
729 var_select_plurality (game, trail,
730 " is inside ",
731 " are inside "));
732 }
733 else
734 {
735 var_append_temp (vars, " and ");
736 var_print_object (game, trail);
737 var_append_temp (vars, " are inside");
738 }
739
740 /* Print out the container. */
741 if (!supporting)
742 {
743 var_append_temp (vars, " ");
744 var_print_object_np (game, associate);
745 }
746 var_append_temp (vars, ".");
747 }
748 else
749 {
750 if (supporting)
751 var_append_temp (vars, ".");
752 }
753 }
754
755
756 /*
757 * var_return_integer()
758 * var_return_string()
759 *
760 * Convenience helpers for var_get_system(). Provide convenience and some
761 * mild syntactic sugar for making returning a value as a system variable
762 * a bit easier. Set appropriate values for return type and the relevant
763 * return value field, and always return TRUE. A macro was tempting here...
764 */
765 static sc_bool
766 var_return_integer (sc_int value, sc_int *type, sc_vartype_t *vt_rvalue)
767 {
768 *type = VAR_INTEGER;
769 vt_rvalue->integer = value;
770 return TRUE;
771 }
772
773 static sc_bool
774 var_return_string (const sc_char *value, sc_int *type, sc_vartype_t *vt_rvalue)
775 {
776 *type = VAR_STRING;
777 vt_rvalue->string = value;
778 return TRUE;
779 }
780
781
782 /*
783 * var_get_system()
784 *
785 * Construct a system variable, and return its type and value, or FALSE
786 * if invalid name passed in. Uses var_return_*() to reduce code untidiness.
787 */
788 static sc_bool
789 var_get_system (sc_var_setref_t vars,
790 const sc_char *name, sc_int *type, sc_vartype_t *vt_rvalue)
791 {
792 const sc_prop_setref_t bundle = vars->bundle;
793 const sc_gameref_t game = vars->game;
794
795 /* Check name for known system variables. */
796 if (strcmp (name, "author") == 0)
797 {
798 sc_vartype_t vt_key[2];
799 const sc_char *author;
800
801 /* Get and return the global gameauthor string. */
802 vt_key[0].string = "Globals";
803 vt_key[1].string = "GameAuthor";
804 author = prop_get_string (bundle, "S<-ss", vt_key);
805 if (sc_strempty (author))
806 author = "[Author unknown]";
807
808 return var_return_string (author, type, vt_rvalue);
809 }
810
811 else if (strcmp (name, "character") == 0)
812 {
813 /* See if there is a referenced character. */
814 if (vars->referenced_character != -1)
815 {
816 sc_vartype_t vt_key[3];
817 const sc_char *npc_name;
818
819 /* Return the character name string. */
820 vt_key[0].string = "NPCs";
821 vt_key[1].integer = vars->referenced_character;
822 vt_key[2].string = "Name";
823 npc_name = prop_get_string (bundle, "S<-sis", vt_key);
824 if (sc_strempty (npc_name))
825 npc_name = "[Character unknown]";
826
827 return var_return_string (npc_name, type, vt_rvalue);
828 }
829 else
830 {
831 sc_error ("var_get_system: no referenced character yet\n");
832 return var_return_string ("[Character unknown]", type, vt_rvalue);
833 }
834 }
835
836 else if (strcmp (name, "heshe") == 0 || strcmp (name, "himher") == 0)
837 {
838 /* See if there is a referenced character. */
839 if (vars->referenced_character != -1)
840 {
841 sc_vartype_t vt_key[3];
842 sc_int gender;
843 const sc_char *retval;
844
845 /* Return the appropriate character gender string. */
846 vt_key[0].string = "NPCs";
847 vt_key[1].integer = vars->referenced_character;
848 vt_key[2].string = "Gender";
849 gender = prop_get_integer (bundle, "I<-sis", vt_key);
850 switch (gender)
851 {
852 case NPC_MALE:
853 retval = (strcmp (name, "heshe") == 0) ? "he" : "him";
854 break;
855 case NPC_FEMALE:
856 retval = (strcmp (name, "heshe") == 0) ? "she" : "her";
857 break;
858 case NPC_NEUTER:
859 retval = "it";
860 break;
861
862 default:
863 sc_error ("var_get_system: unknown gender, %ld\n", gender);
864 retval = "[Gender unknown]";
865 break;
866 }
867 return var_return_string (retval, type, vt_rvalue);
868 }
869 else
870 {
871 sc_error ("var_get_system: no referenced character yet\n");
872 return var_return_string ("[Gender unknown]", type, vt_rvalue);
873 }
874 }
875
876 else if (strncmp (name, "in_", 3) == 0)
877 {
878 sc_int saved_ref_object = vars->referenced_object;
879
880 /* Check there's enough information to return a value. */
881 if (!game)
882 {
883 sc_error ("var_get_system: no game for in_\n");
884 return var_return_string ("[In_ unavailable]", type, vt_rvalue);
885 }
886 if (!uip_match ("%object%", name + 3, game))
887 {
888 sc_error ("var_get_system: invalid object for in_\n");
889 return var_return_string ("[In_ unavailable]", type, vt_rvalue);
890 }
891
892 /* Clear any current temporary for appends. */
893 vars->temporary = sc_realloc (vars->temporary, 1);
894 strcpy (vars->temporary, "");
895
896 /* Write what's in the object into temporary. */
897 var_list_in_object (game, vars->referenced_object);
898
899 /* Restore saved referenced object and return. */
900 vars->referenced_object = saved_ref_object;
901 return var_return_string (vars->temporary, type, vt_rvalue);
902 }
903
904 else if (strcmp (name, "maxscore") == 0)
905 {
906 sc_vartype_t vt_key[2];
907 sc_int maxscore;
908
909 /* Return the maximum score. */
910 vt_key[0].string = "Globals";
911 vt_key[1].string = "MaxScore";
912 maxscore = prop_get_integer (bundle, "I<-ss", vt_key);
913
914 return var_return_integer (maxscore, type, vt_rvalue);
915 }
916
917 else if (strcmp (name, "modified") == 0)
918 {
919 sc_vartype_t vt_key;
920 const sc_char *compiledate;
921
922 /* Return the game compilation date. */
923 vt_key.string = "CompileDate";
924 compiledate = prop_get_string (bundle, "S<-s", &vt_key);
925 if (sc_strempty (compiledate))
926 compiledate = "[Modified unknown]";
927
928 return var_return_string (compiledate, type, vt_rvalue);
929 }
930
931 else if (strcmp (name, "number") == 0)
932 {
933 /* Return the referenced number, or 0 if none yet. */
934 if (!vars->is_number_referenced)
935 sc_error ("var_get_system: no referenced number yet\n");
936
937 return var_return_integer (vars->referenced_number, type, vt_rvalue);
938 }
939
940 else if (strcmp (name, "object") == 0)
941 {
942 /* See if we have a referenced object yet. */
943 if (vars->referenced_object != -1)
944 {
945 /* Return object name with its prefix. */
946 sc_vartype_t vt_key[3];
947 const sc_char *prefix, *objname;
948
949 vt_key[0].string = "Objects";
950 vt_key[1].integer = vars->referenced_object;
951 vt_key[2].string = "Prefix";
952 prefix = prop_get_string (bundle, "S<-sis", vt_key);
953
954 vars->temporary = sc_realloc (vars->temporary, strlen (prefix) + 1);
955 strcpy (vars->temporary, prefix);
956
957 vt_key[2].string = "Short";
958 objname = prop_get_string (bundle, "S<-sis", vt_key);
959
960 vars->temporary = sc_realloc (vars->temporary,
961 strlen (vars->temporary)
962 + strlen (objname) + 2);
963 strcat (vars->temporary, " ");
964 strcat (vars->temporary, objname);
965
966 return var_return_string (vars->temporary, type, vt_rvalue);
967 }
968 else
969 {
970 sc_error ("var_get_system: no referenced object yet\n");
971 return var_return_string ("[Object unknown]", type, vt_rvalue);
972 }
973 }
974
975 else if (strcmp (name, "obstate") == 0)
976 {
977 sc_vartype_t vt_key[3];
978 sc_bool is_statussed;
979 sc_char *state;
980
981 /* Check there's enough information to return a value. */
982 if (!game)
983 {
984 sc_error ("var_get_system: no game for obstate\n");
985 return var_return_string ("[Obstate unavailable]", type, vt_rvalue);
986 }
987 if (vars->referenced_object == -1)
988 {
989 sc_error ("var_get_system: no object for obstate\n");
990 return var_return_string ("[Obstate unavailable]", type, vt_rvalue);
991 }
992
993 /*
994 * If not a stateful object, Runner 4.0.45 crashes; we'll do something
995 * different here.
996 */
997 vt_key[0].string = "Objects";
998 vt_key[1].integer = vars->referenced_object;
999 vt_key[2].string = "CurrentState";
1000 is_statussed = prop_get_integer (bundle, "I<-sis", vt_key) != 0;
1001 if (!is_statussed)
1002 return var_return_string ("stateless", type, vt_rvalue);
1003
1004 /* Get state, and copy to temporary. */
1005 state = obj_state_name (game, vars->referenced_object);
1006 if (!state)
1007 {
1008 sc_error ("var_get_system: invalid state for obstate\n");
1009 return var_return_string ("[Obstate unknown]", type, vt_rvalue);
1010 }
1011 vars->temporary = sc_realloc (vars->temporary, strlen (state) + 1);
1012 strcpy (vars->temporary, state);
1013 sc_free (state);
1014
1015 /* Return temporary. */
1016 return var_return_string (vars->temporary, type, vt_rvalue);
1017 }
1018
1019 else if (strcmp (name, "obstatus") == 0)
1020 {
1021 sc_vartype_t vt_key[3];
1022 sc_bool is_openable;
1023 sc_int openness;
1024 const sc_char *retval;
1025
1026 /* Check there's enough information to return a value. */
1027 if (!game)
1028 {
1029 sc_error ("var_get_system: no game for obstatus\n");
1030 return var_return_string ("[Obstatus unavailable]", type, vt_rvalue);
1031 }
1032 if (vars->referenced_object == -1)
1033 {
1034 sc_error ("var_get_system: no object for obstatus\n");
1035 return var_return_string ("[Obstatus unavailable]", type, vt_rvalue);
1036 }
1037
1038 /* If not an openable object, return unopenable to match Adrift. */
1039 vt_key[0].string = "Objects";
1040 vt_key[1].integer = vars->referenced_object;
1041 vt_key[2].string = "Openable";
1042 is_openable = prop_get_integer (bundle, "I<-sis", vt_key) != 0;
1043 if (!is_openable)
1044 return var_return_string ("unopenable", type, vt_rvalue);
1045
1046 /* Return one of open, closed, or locked. */
1047 openness = gs_object_openness (game, vars->referenced_object);
1048 switch (openness)
1049 {
1050 case OBJ_OPEN:
1051 retval = "open";
1052 break;
1053 case OBJ_CLOSED:
1054 retval = "closed";
1055 break;
1056 case OBJ_LOCKED:
1057 retval = "locked";
1058 break;
1059 default:
1060 retval = "[Obstatus unknown]";
1061 break;
1062 }
1063 return var_return_string (retval, type, vt_rvalue);
1064 }
1065
1066 else if (strncmp (name, "on_", 3) == 0)
1067 {
1068 sc_int saved_ref_object = vars->referenced_object;
1069
1070 /* Check there's enough information to return a value. */
1071 if (!game)
1072 {
1073 sc_error ("var_get_system: no game for on_\n");
1074 return var_return_string ("[On_ unavailable]", type, vt_rvalue);
1075 }
1076 if (!uip_match ("%object%", name + 3, game))
1077 {
1078 sc_error ("var_get_system: invalid object for on_\n");
1079 return var_return_string ("[On_ unavailable]", type, vt_rvalue);
1080 }
1081
1082 /* Clear any current temporary for appends. */
1083 vars->temporary = sc_realloc (vars->temporary, 1);
1084 strcpy (vars->temporary, "");
1085
1086 /* Write what's on the object into temporary. */
1087 var_list_on_object (game, vars->referenced_object);
1088
1089 /* Restore saved referenced object and return. */
1090 vars->referenced_object = saved_ref_object;
1091 return var_return_string (vars->temporary, type, vt_rvalue);
1092 }
1093
1094 else if (strncmp (name, "onin_", 5) == 0)
1095 {
1096 sc_int saved_ref_object = vars->referenced_object;
1097
1098 /* Check there's enough information to return a value. */
1099 if (!game)
1100 {
1101 sc_error ("var_get_system: no game for onin_\n");
1102 return var_return_string ("[Onin_ unavailable]", type, vt_rvalue);
1103 }
1104 if (!uip_match ("%object%", name + 5, game))
1105 {
1106 sc_error ("var_get_system: invalid object for onin_\n");
1107 return var_return_string ("[Onin_ unavailable]", type, vt_rvalue);
1108 }
1109
1110 /* Clear any current temporary for appends. */
1111 vars->temporary = sc_realloc (vars->temporary, 1);
1112 strcpy (vars->temporary, "");
1113
1114 /* Write what's on/in the object into temporary. */
1115 var_list_onin_object (game, vars->referenced_object);
1116
1117 /* Restore saved referenced object and return. */
1118 vars->referenced_object = saved_ref_object;
1119 return var_return_string (vars->temporary, type, vt_rvalue);
1120 }
1121
1122 else if (strcmp (name, "player") == 0)
1123 {
1124 sc_vartype_t vt_key[2];
1125 const sc_char *playername;
1126
1127 /*
1128 * Return player's name from properties, or just "Player" if not set
1129 * in the properties.
1130 */
1131 vt_key[0].string = "Globals";
1132 vt_key[1].string = "PlayerName";
1133 playername = prop_get_string (bundle, "S<-ss", vt_key);
1134 if (sc_strempty (playername))
1135 playername = "Player";
1136
1137 return var_return_string (playername, type, vt_rvalue);
1138 }
1139
1140 else if (strcmp (name, "room") == 0)
1141 {
1142 const sc_char *roomname;
1143
1144 /* Check there's enough information to return a value. */
1145 if (!game)
1146 {
1147 sc_error ("var_get_system: no game for room\n");
1148 return var_return_string ("[Room unavailable]", type, vt_rvalue);
1149 }
1150
1151 /* Return the current player room. */
1152 roomname = lib_get_room_name (game, gs_playerroom (game));
1153 return var_return_string (roomname, type, vt_rvalue);
1154 }
1155
1156 else if (strcmp (name, "score") == 0)
1157 {
1158 /* Check there's enough information to return a value. */
1159 if (!game)
1160 {
1161 sc_error ("var_get_system: no game for score\n");
1162 return var_return_integer (0, type, vt_rvalue);
1163 }
1164
1165 /* Return the current game score. */
1166 return var_return_integer (game->score, type, vt_rvalue);
1167 }
1168
1169 else if (strncmp (name, "state_", 6) == 0)
1170 {
1171 sc_int saved_ref_object = vars->referenced_object;
1172 sc_vartype_t vt_key[3];
1173 sc_bool is_statussed;
1174 sc_char *state;
1175
1176 /* Check there's enough information to return a value. */
1177 if (!game)
1178 {
1179 sc_error ("var_get_system: no game for state_\n");
1180 return var_return_string ("[State_ unavailable]", type, vt_rvalue);
1181 }
1182 if (!uip_match ("%object%", name + 6, game))
1183 {
1184 sc_error ("var_get_system: invalid object for state_\n");
1185 return var_return_string ("[State_ unavailable]", type, vt_rvalue);
1186 }
1187
1188 /* Verify this is a stateful object. */
1189 vt_key[0].string = "Objects";
1190 vt_key[1].integer = vars->referenced_object;
1191 vt_key[2].string = "CurrentState";
1192 is_statussed = prop_get_integer (bundle, "I<-sis", vt_key) != 0;
1193 if (!is_statussed)
1194 {
1195 vars->referenced_object = saved_ref_object;
1196 sc_error ("var_get_system: stateless object for state_\n");
1197 return var_return_string ("[State_ unavailable]", type, vt_rvalue);
1198 }
1199
1200 /* Get state, and copy to temporary. */
1201 state = obj_state_name (game, vars->referenced_object);
1202 if (!state)
1203 {
1204 vars->referenced_object = saved_ref_object;
1205 sc_error ("var_get_system: invalid state for state_\n");
1206 return var_return_string ("[State_ unknown]", type, vt_rvalue);
1207 }
1208 vars->temporary = sc_realloc (vars->temporary, strlen (state) + 1);
1209 strcpy (vars->temporary, state);
1210 sc_free (state);
1211
1212 /* Restore saved referenced object and return. */
1213 vars->referenced_object = saved_ref_object;
1214 return var_return_string (vars->temporary, type, vt_rvalue);
1215 }
1216
1217 else if (strncmp (name, "status_", 7) == 0)
1218 {
1219 sc_int saved_ref_object = vars->referenced_object;
1220 sc_vartype_t vt_key[3];
1221 sc_bool is_openable;
1222 sc_int openness;
1223 const sc_char *retval;
1224
1225 /* Check there's enough information to return a value. */
1226 if (!game)
1227 {
1228 sc_error ("var_get_system: no game for status_\n");
1229 return var_return_string ("[Status_ unavailable]", type, vt_rvalue);
1230 }
1231 if (!uip_match ("%object%", name + 7, game))
1232 {
1233 sc_error ("var_get_system: invalid object for status_\n");
1234 return var_return_string ("[Status_ unavailable]", type, vt_rvalue);
1235 }
1236
1237 /* Verify this is an openable object. */
1238 vt_key[0].string = "Objects";
1239 vt_key[1].integer = vars->referenced_object;
1240 vt_key[2].string = "Openable";
1241 is_openable = prop_get_integer (bundle, "I<-sis", vt_key) != 0;
1242 if (!is_openable)
1243 {
1244 vars->referenced_object = saved_ref_object;
1245 sc_error ("var_get_system: stateless object for status_\n");
1246 return var_return_string ("[Status_ unavailable]", type, vt_rvalue);
1247 }
1248
1249 /* Return one of open, closed, or locked. */
1250 openness = gs_object_openness (game, vars->referenced_object);
1251 switch (openness)
1252 {
1253 case OBJ_OPEN:
1254 retval = "open";
1255 break;
1256 case OBJ_CLOSED:
1257 retval = "closed";
1258 break;
1259 case OBJ_LOCKED:
1260 retval = "locked";
1261 break;
1262 default:
1263 retval = "[Status_ unknown]";
1264 break;
1265 }
1266
1267 /* Restore saved referenced object and return. */
1268 vars->referenced_object = saved_ref_object;
1269 return var_return_string (retval, type, vt_rvalue);
1270 }
1271
1272 else if (strcmp (name, "t_number") == 0)
1273 {
1274 /* See if we have a referenced number yet. */
1275 if (vars->is_number_referenced)
1276 {
1277 sc_int number;
1278 const sc_char *retval;
1279
1280 /* Return the referenced number as a string. */
1281 number = vars->referenced_number;
1282 if (number >= 0 && number < VAR_NUMBERS_SIZE)
1283 retval = VAR_NUMBERS[number];
1284 else
1285 {
1286 vars->temporary = sc_realloc (vars->temporary, 32);
1287 sprintf (vars->temporary, "%ld", number);
1288 retval = vars->temporary;
1289 }
1290
1291 return var_return_string (retval, type, vt_rvalue);
1292 }
1293 else
1294 {
1295 sc_error ("var_get_system: no referenced number yet\n");
1296 return var_return_string ("[Number unknown]", type, vt_rvalue);
1297 }
1298 }
1299
1300 else if (strncmp (name, "t_", 2) == 0)
1301 {
1302 sc_varref_t var;
1303
1304 /* Find the variable; must be a user, not a system, one. */
1305 var = var_find (vars, name + 2);
1306 if (!var)
1307 {
1308 sc_error ("var_get_system:"
1309 " no such variable, %s\n", name + 2);
1310 return var_return_string ("[Unknown variable]", type, vt_rvalue);
1311 }
1312 else if (var->type != VAR_INTEGER)
1313 {
1314 sc_error ("var_get_system:"
1315 " not an integer variable, %s\n", name + 2);
1316 return var_return_string (var->value.string, type, vt_rvalue);
1317 }
1318 else
1319 {
1320 sc_int number;
1321 const sc_char *retval;
1322
1323 /* Return the variable value as a string. */
1324 number = var->value.integer;
1325 if (number >= 0 && number < VAR_NUMBERS_SIZE)
1326 retval = VAR_NUMBERS[number];
1327 else
1328 {
1329 vars->temporary = sc_realloc (vars->temporary, 32);
1330 sprintf (vars->temporary, "%ld", number);
1331 retval = vars->temporary;
1332 }
1333
1334 return var_return_string (retval, type, vt_rvalue);
1335 }
1336 }
1337
1338 else if (strcmp (name, "text") == 0)
1339 {
1340 const sc_char *retval;
1341
1342 /* Return any referenced text, otherwise a neutral string. */
1343 if (vars->referenced_text)
1344 retval = vars->referenced_text;
1345 else
1346 {
1347 sc_error ("var_get_system: no text yet to reference\n");
1348 retval = "[Text unknown]";
1349 }
1350
1351 return var_return_string (retval, type, vt_rvalue);
1352 }
1353
1354 else if (strcmp (name, "theobject") == 0)
1355 {
1356 /* See if we have a referenced object yet. */
1357 if (vars->referenced_object != -1)
1358 {
1359 /* Return object name prefixed with "the"... */
1360 sc_vartype_t vt_key[3];
1361 const sc_char *prefix, *normalized, *objname;
1362
1363 vt_key[0].string = "Objects";
1364 vt_key[1].integer = vars->referenced_object;
1365 vt_key[2].string = "Prefix";
1366 prefix = prop_get_string (bundle, "S<-sis", vt_key);
1367
1368 vars->temporary = sc_realloc (vars->temporary, strlen (prefix) + 5);
1369 strcpy (vars->temporary, "");
1370
1371 normalized = prefix;
1372 if (sc_compare_word (prefix, "a", 1))
1373 {
1374 strcat (vars->temporary, "the");
1375 normalized = prefix + 1;
1376 }
1377 else if (sc_compare_word (prefix, "an", 2))
1378 {
1379 strcat (vars->temporary, "the");
1380 normalized = prefix + 2;
1381 }
1382 else if (sc_compare_word (prefix, "the", 3))
1383 {
1384 strcat (vars->temporary, "the");
1385 normalized = prefix + 3;
1386 }
1387 else if (sc_compare_word (prefix, "some", 4))
1388 {
1389 strcat (vars->temporary, "the");
1390 normalized = prefix + 4;
1391 }
1392 else if (sc_strempty (prefix))
1393 strcat (vars->temporary, "the ");
1394
1395 if (!sc_strempty (normalized))
1396 {
1397 strcat (vars->temporary, normalized);
1398 strcat (vars->temporary, " ");
1399 }
1400 else if (normalized > prefix)
1401 strcat (vars->temporary, " ");
1402
1403 vt_key[2].string = "Short";
1404 objname = prop_get_string (bundle, "S<-sis", vt_key);
1405 if (sc_compare_word (objname, "a", 1))
1406 objname += 1;
1407 else if (sc_compare_word (objname, "an", 2))
1408 objname += 2;
1409 else if (sc_compare_word (objname, "the", 3))
1410 objname += 3;
1411 else if (sc_compare_word (objname, "some", 4))
1412 objname += 4;
1413
1414 vars->temporary = sc_realloc (vars->temporary,
1415 strlen (vars->temporary)
1416 + strlen (objname) + 1);
1417 strcat (vars->temporary, objname);
1418
1419 return var_return_string (vars->temporary, type, vt_rvalue);
1420 }
1421 else
1422 {
1423 sc_error ("var_get_system: no referenced object yet\n");
1424 return var_return_string ("[Object unknown]", type, vt_rvalue);
1425 }
1426 }
1427
1428 else if (strcmp (name, "time") == 0)
1429 {
1430 double delta;
1431 sc_int retval;
1432
1433 /* Return the elapsed game time in seconds. */
1434 delta = difftime (time (NULL), vars->timestamp);
1435 retval = (sc_int) delta + vars->time_offset;
1436
1437 return var_return_integer (retval, type, vt_rvalue);
1438 }
1439
1440 else if (strcmp (name, "title") == 0)
1441 {
1442 sc_vartype_t vt_key[2];
1443 const sc_char *gamename;
1444
1445 /* Return the game's title. */
1446 vt_key[0].string = "Globals";
1447 vt_key[1].string = "GameName";
1448 gamename = prop_get_string (bundle, "S<-ss", vt_key);
1449 if (sc_strempty (gamename))
1450 gamename = "[Title unknown]";
1451
1452 return var_return_string (gamename, type, vt_rvalue);
1453 }
1454
1455 else if (strcmp (name, "turns") == 0)
1456 {
1457 /* Check there's enough information to return a value. */
1458 if (!game)
1459 {
1460 sc_error ("var_get_system: no game for turns\n");
1461 return var_return_integer (0, type, vt_rvalue);
1462 }
1463
1464 /* Return the count of game turns. */
1465 return var_return_integer (game->turns, type, vt_rvalue);
1466 }
1467
1468 else if (strcmp (name, "version") == 0)
1469 {
1470 /* Return the Adrift emulation level of SCARE. */
1471 return var_return_integer (SCARE_EMULATION, type, vt_rvalue);
1472 }
1473
1474 else if (strcmp (name, "scare_version") == 0)
1475 {
1476 /* Private system variable, return SCARE's version number. */
1477 return var_return_integer (var_get_scare_version (), type, vt_rvalue);
1478 }
1479
1480 return FALSE;
1481 }
1482
1483
1484 /*
1485 * var_get_user()
1486 *
1487 * Retrieve a user variable, and return its type and value, or FALSE if the
1488 * name passed in is not a defined user variable.
1489 */
1490 static sc_bool
1491 var_get_user (sc_var_setref_t vars,
1492 const sc_char *name, sc_int *type, sc_vartype_t *vt_rvalue)
1493 {
1494 sc_varref_t var;
1495
1496 /* Check user variables for a reference to the named variable. */
1497 var = var_find (vars, name);
1498 if (var)
1499 {
1500 /* Copy out variable details. */
1501 *type = var->type;
1502 switch (var->type)
1503 {
1504 case VAR_INTEGER:
1505 vt_rvalue->integer = var->value.integer;
1506 break;
1507 case VAR_STRING:
1508 vt_rvalue->string = var->value.string;
1509 break;
1510
1511 default:
1512 sc_fatal ("var_get_user: invalid variable type, %ld\n", var->type);
1513 }
1514
1515 /* Return success. */
1516 return TRUE;
1517 }
1518
1519 return FALSE;
1520 }
1521
1522
1523 /*
1524 * var_get()
1525 *
1526 * Retrieve a variable, and return its value and type. Returns FALSE if the
1527 * named variable does not exist.
1528 */
1529 sc_bool
1530 var_get (sc_var_setref_t vars,
1531 const sc_char *name, sc_int *type, sc_vartype_t *vt_rvalue)
1532 {
1533 sc_bool status;
1534 assert (var_is_valid (vars));
1535 assert (name && type && vt_rvalue);
1536
1537 /*
1538 * Check user and system variables for a reference to the name. User
1539 * variables take precedence over system ones; that is, they may override
1540 * them in a game.
1541 */
1542 status = var_get_user (vars, name, type, vt_rvalue);
1543 if (!status)
1544 status = var_get_system (vars, name, type, vt_rvalue);
1545
1546 if (var_trace)
1547 {
1548 if (status)
1549 {
1550 sc_trace ("Variable: %%%s%% retrieved, ", name);
1551 switch (*type)
1552 {
1553 case VAR_INTEGER:
1554 sc_trace ("%ld", vt_rvalue->integer);
1555 break;
1556 case VAR_STRING:
1557 sc_trace ("\"%s\"", vt_rvalue->string);
1558 break;
1559
1560 default:
1561 sc_trace ("Variable: invalid variable type, %ld\n", *type);
1562 break;
1563 }
1564 sc_trace ("\n");
1565 }
1566 else
1567 sc_trace ("Variable: \"%s\", no such variable\n", name);
1568 }
1569
1570 return status;
1571 }
1572
1573
1574 /*
1575 * var_put_integer()
1576 * var_get_integer()
1577 *
1578 * Convenience functions to store and retrieve an integer variable. It is
1579 * an error for the variable not to exist or to have the wrong type.
1580 */
1581 void
1582 var_put_integer (sc_var_setref_t vars, const sc_char *name, sc_int value)
1583 {
1584 sc_vartype_t vt_value;
1585 assert (var_is_valid (vars));
1586
1587 vt_value.integer = value;
1588 var_put (vars, name, VAR_INTEGER, vt_value);
1589 }
1590
1591 sc_int
1592 var_get_integer (sc_var_setref_t vars, const sc_char *name)
1593 {
1594 sc_vartype_t vt_rvalue;
1595 sc_int type;
1596 assert (var_is_valid (vars));
1597
1598 if (!var_get (vars, name, &type, &vt_rvalue))
1599 sc_fatal ("var_get_integer: no such variable, %s\n", name);
1600 else if (type != VAR_INTEGER)
1601 sc_fatal ("var_get_integer: not an integer, %s\n", name);
1602
1603 return vt_rvalue.integer;
1604 }
1605
1606
1607 /*
1608 * var_put_string()
1609 * var_get_string()
1610 *
1611 * Convenience functions to store and retrieve a string variable. It is
1612 * an error for the variable not to exist or to have the wrong type.
1613 */
1614 void
1615 var_put_string (sc_var_setref_t vars,
1616 const sc_char *name, const sc_char *string)
1617 {
1618 sc_vartype_t vt_value;
1619 assert (var_is_valid (vars));
1620
1621 vt_value.string = string;
1622 var_put (vars, name, VAR_STRING, vt_value);
1623 }
1624
1625 const sc_char *
1626 var_get_string (sc_var_setref_t vars, const sc_char *name)
1627 {
1628 sc_vartype_t vt_rvalue;
1629 sc_int type;
1630 assert (var_is_valid (vars));
1631
1632 if (!var_get (vars, name, &type, &vt_rvalue))
1633 sc_fatal ("var_get_string: no such variable, %s\n", name);
1634 else if (type != VAR_STRING)
1635 sc_fatal ("var_get_string: not a string, %s\n", name);
1636
1637 return vt_rvalue.string;
1638 }
1639
1640
1641 /*
1642 * var_create()
1643 *
1644 * Create and return a new set of variables. Variables are created from the
1645 * properties bundle passed in.
1646 */
1647 sc_var_setref_t
1648 var_create (sc_prop_setref_t bundle)
1649 {
1650 sc_var_setref_t vars;
1651 sc_int var_count, index_;
1652 sc_vartype_t vt_key[3];
1653 assert (bundle);
1654
1655 /* Create a clean set of variables to fill from the bundle. */
1656 vars = var_create_empty ();
1657 vars->bundle = bundle;
1658
1659 /* Retrieve the count of variables. */
1660 vt_key[0].string = "Variables";
1661 var_count = prop_get_child_count (bundle, "I<-s", vt_key);
1662
1663 /* Create a variable for each variable property held. */
1664 for (index_ = 0; index_ < var_count; index_++)
1665 {
1666 const sc_char *name;
1667 sc_int var_type;
1668 const sc_char *value;
1669
1670 /* Retrieve variable name, type, and string initial value. */
1671 vt_key[1].integer = index_;
1672 vt_key[2].string = "Name";
1673 name = prop_get_string (bundle, "S<-sis", vt_key);
1674
1675 vt_key[2].string = "Type";
1676 var_type = prop_get_integer (bundle, "I<-sis", vt_key);
1677
1678 vt_key[2].string = "Value";
1679 value = prop_get_string (bundle, "S<-sis", vt_key);
1680
1681 /* Handle numerics and strings differently. */
1682 switch (var_type)
1683 {
1684 case TAFVAR_NUMERIC:
1685 {
1686 sc_int integer_value;
1687 if (sscanf (value, "%ld", &integer_value) != 1)
1688 {
1689 sc_error ("var_create:"
1690 " invalid numeric variable %s, %s\n", name, value);
1691 integer_value = 0;
1692 }
1693 var_put_integer (vars, name, integer_value);
1694 break;
1695 }
1696
1697 case TAFVAR_STRING:
1698 var_put_string (vars, name, value);
1699 break;
1700
1701 default:
1702 sc_fatal ("var_create: invalid variable type, %ld\n", var_type);
1703 }
1704 }
1705
1706 return vars;
1707 }
1708
1709
1710 /*
1711 * var_register_game()
1712 *
1713 * Register the game, used by variables to satisfy requests for selected
1714 * system variables. To ensure integrity, the game being registered must
1715 * reference this variable set.
1716 */
1717 void
1718 var_register_game (sc_var_setref_t vars, sc_gameref_t game)
1719 {
1720 assert (var_is_valid (vars));
1721 assert (gs_is_game_valid (game));
1722
1723 if (vars != gs_get_vars (game))
1724 sc_fatal ("var_register_game: game binding error\n");
1725
1726 vars->game = game;
1727 }
1728
1729
1730 /*
1731 * var_set_ref_character()
1732 * var_set_ref_object()
1733 * var_set_ref_number()
1734 * var_set_ref_text()
1735 *
1736 * Set the "referenced" character, object, number, and text.
1737 */
1738 void
1739 var_set_ref_character (sc_var_setref_t vars, sc_int character)
1740 {
1741 assert (var_is_valid (vars));
1742 vars->referenced_character = character;
1743 }
1744
1745 void
1746 var_set_ref_object (sc_var_setref_t vars, sc_int object)
1747 {
1748 assert (var_is_valid (vars));
1749 vars->referenced_object = object;
1750 }
1751
1752 void
1753 var_set_ref_number (sc_var_setref_t vars, sc_int number)
1754 {
1755 assert (var_is_valid (vars));
1756 vars->referenced_number = number;
1757 vars->is_number_referenced = TRUE;
1758 }
1759
1760 void
1761 var_set_ref_text (sc_var_setref_t vars, const sc_char *text)
1762 {
1763 assert (var_is_valid (vars));
1764
1765 /* Take a copy of the string, and retain it. */
1766 vars->referenced_text = sc_realloc (vars->referenced_text, strlen (text) + 1);
1767 strcpy (vars->referenced_text, text);
1768 }
1769
1770
1771 /*
1772 * var_get_ref_character()
1773 * var_get_ref_object()
1774 * var_get_ref_number()
1775 * var_get_ref_text()
1776 *
1777 * Get the "referenced" character, object, number, and text.
1778 */
1779 sc_int
1780 var_get_ref_character (sc_var_setref_t vars)
1781 {
1782 assert (var_is_valid (vars));
1783 return vars->referenced_character;
1784 }
1785
1786 sc_int
1787 var_get_ref_object (sc_var_setref_t vars)
1788 {
1789 assert (var_is_valid (vars));
1790 return vars->referenced_object;
1791 }
1792
1793 sc_int
1794 var_get_ref_number (sc_var_setref_t vars)
1795 {
1796 assert (var_is_valid (vars));
1797 return vars->referenced_number;
1798 }
1799
1800 const sc_char *
1801 var_get_ref_text (sc_var_setref_t vars)
1802 {
1803 assert (var_is_valid (vars));
1804
1805 /*
1806 * If currently NULL, return "". A game may check restrictions involving
1807 * referenced text before any value has been set; returning "" here for
1808 * this case prevents problems later (strcmp (NULL, ...), for example).
1809 */
1810 return vars->referenced_text ? vars->referenced_text : "";
1811 }
1812
1813
1814 /*
1815 * var_get_elapsed_seconds()
1816 * var_set_elapsed_seconds()
1817 *
1818 * Get a count of seconds elapsed since the variables were created (start
1819 * of game), and set the count to a given value (game restore).
1820 */
1821 sc_uint
1822 var_get_elapsed_seconds (sc_var_setref_t vars)
1823 {
1824 double delta;
1825 assert (var_is_valid (vars));
1826
1827 delta = difftime (time (NULL), vars->timestamp);
1828 return (sc_uint) delta + vars->time_offset;
1829 }
1830
1831 void
1832 var_set_elapsed_seconds (sc_var_setref_t vars, sc_uint seconds)
1833 {
1834 assert (var_is_valid (vars));
1835
1836 /*
1837 * Reset the timestamp to now, and store seconds in offset. This is sort-of
1838 * forced by the fact that ANSI offers difftime but no 'settime' -- here,
1839 * we'd really want to set the timestamp to now less seconds.
1840 */
1841 vars->timestamp = time (NULL);
1842 vars->time_offset = seconds;
1843 }
1844
1845
1846 /*
1847 * var_debug_trace()
1848 *
1849 * Set variable tracing on/off.
1850 */
1851 void
1852 var_debug_trace (sc_bool flag)
1853 {
1854 var_trace = flag;
1855 }
1856
1857
1858 /*
1859 * var_debug_dump()
1860 *
1861 * Print out a complete variables set.
1862 */
1863 void
1864 var_debug_dump (sc_var_setref_t vars)
1865 {
1866 sc_int index_;
1867 sc_varref_t var;
1868 assert (var_is_valid (vars));
1869
1870 /* Dump complete structure. */
1871 sc_trace ("Variable: debug dump follows...\n");
1872 sc_trace ("vars->bundle = %p\n", (void *) vars->bundle);
1873 sc_trace ("vars->referenced_character = %ld\n", vars->referenced_character);
1874 sc_trace ("vars->referenced_object = %ld\n", vars->referenced_object);
1875 sc_trace ("vars->referenced_number = %ld\n", vars->referenced_number);
1876 sc_trace ("vars->is_number_referenced = %s\n",
1877 vars->is_number_referenced ? "true" : "false");
1878
1879 sc_trace ("vars->referenced_text = ");
1880 if (vars->referenced_text)
1881 sc_trace ("\"%s\"\n", vars->referenced_text);
1882 else
1883 sc_trace ("(nil)\n");
1884
1885 sc_trace ("vars->temporary = %p\n", (void *) vars->temporary);
1886 sc_trace ("vars->timestamp = %lu\n", (sc_uint) vars->timestamp);
1887 sc_trace ("vars->game = %p\n", (void *) vars->game);
1888
1889 sc_trace ("vars->variables =\n");
1890 for (index_ = 0; index_ < VAR_HASH_TABLE_SIZE; index_++)
1891 {
1892 for (var = vars->variable[index_]; var; var = var->next)
1893 {
1894 if (var == vars->variable[index_])
1895 sc_trace ("%3ld : ", index_);
1896 else
1897 sc_trace (" : ");
1898 switch (var->type)
1899 {
1900 case VAR_STRING:
1901 sc_trace ("[String ] %s = \"%s\"", var->name, var->value.string);
1902 break;
1903 case VAR_INTEGER:
1904 sc_trace ("[Integer] %s = %ld", var->name, var->value.integer);
1905 break;
1906
1907 default:
1908 sc_trace ("[Invalid] %s = %p", var->name, var->value.voidp);
1909 break;
1910 }
1911 sc_trace ("\n");
1912 }
1913 }
1914 }