Debian Codegraph @

terps/scare/screstrs.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 ...
24 */
25
26 #include <assert.h>
27 #include <setjmp.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "scare.h"
32 #include "scprotos.h"
33 #include "scgamest.h"
34
35
36 /* Assorted definitions and constants. */
37 enum { MAX_NESTING_DEPTH = 32 };
38 static const sc_char NUL = '\0';
39
40 /* Trace flag, set before running. */
41 static sc_bool restr_trace = FALSE;
42
43
44 /*
45 * restr_integer_variable()
46 *
47 * Return the index of the n'th integer found.
48 */
49 static sc_int
50 restr_integer_variable (sc_gameref_t game, sc_int n)
51 {
52 const sc_prop_setref_t bundle = gs_get_bundle (game);
53 sc_vartype_t vt_key[3];
54 sc_int var_count, var, count;
55
56 /* Get the count of variables. */
57 vt_key[0].string = "Variables";
58 var_count = prop_get_child_count (bundle, "I<-s", vt_key);
59
60 /* Progress through variables until n integers found. */
61 count = n;
62 for (var = 0; var < var_count && count >= 0; var++)
63 {
64 sc_int type;
65
66 vt_key[1].integer = var;
67 vt_key[2].string = "Type";
68 type = prop_get_integer (bundle, "I<-sis", vt_key);
69 if (type == TAFVAR_NUMERIC)
70 count--;
71 }
72 return var - 1;
73 }
74
75
76 /*
77 * restr_object_in_place()
78 *
79 * Is object in a certain place, state, or condition.
80 */
81 static sc_bool
82 restr_object_in_place (sc_gameref_t game,
83 sc_int object, sc_int var2, sc_int var3)
84 {
85 const sc_var_setref_t vars = gs_get_vars (game);
86 sc_int npc;
87
88 if (restr_trace)
89 {
90 sc_trace ("Restr: checking"
91 " object in place, %ld, %ld, %ld\n", object, var2, var3);
92 }
93
94 /* Var2 controls what we do. */
95 switch (var2)
96 {
97 case 0:
98 case 6: /* In room */
99 if (var3 == 0)
100 return gs_object_position (game, object) == OBJ_HIDDEN;
101 else
102 return gs_object_position (game, object) == var3;
103
104 case 1:
105 case 7: /* Held by */
106 if (var3 == 0) /* Player */
107 return gs_object_position (game, object) == OBJ_HELD_PLAYER;
108 else if (var3 == 1) /* Ref character */
109 npc = var_get_ref_character (vars);
110 else
111 npc = var3 - 2;
112
113 return gs_object_position (game, object) == OBJ_HELD_NPC
114 && gs_object_parent (game, object) == npc;
115
116 case 2:
117 case 8: /* Worn by */
118 if (var3 == 0) /* Player */
119 return gs_object_position (game, object) == OBJ_WORN_PLAYER;
120 else if (var3 == 1) /* Ref character */
121 npc = var_get_ref_character (vars);
122 else
123 npc = var3 - 2;
124
125 return gs_object_position (game, object) == OBJ_WORN_NPC
126 && gs_object_parent (game, object) == npc;
127
128 case 3:
129 case 9: /* Visible to */
130 if (var3 == 0) /* Player */
131 return obj_indirectly_in_room (game,
132 object, gs_playerroom (game));
133 else if (var3 == 1) /* Ref character */
134 npc = var_get_ref_character (vars);
135 else
136 npc = var3 - 2;
137
138 return obj_indirectly_in_room (game, object,
139 gs_npc_location (game, npc) - 1);
140
141 case 4:
142 case 10: /* Inside */
143 if (var3 == 0) /* Nothing? */
144 return gs_object_position (game, object) != OBJ_IN_OBJECT;
145
146 return gs_object_position (game, object) == OBJ_IN_OBJECT
147 && gs_object_parent (game, object) == obj_container_object (game,
148 var3 - 1);
149
150 case 5:
151 case 11: /* On top of */
152 if (var3 == 0) /* Nothing? */
153 return gs_object_position (game, object) != OBJ_ON_OBJECT;
154
155 return gs_object_position (game, object) == OBJ_ON_OBJECT
156 && gs_object_parent (game, object) == obj_surface_object (game,
157 var3 - 1);
158
159 default:
160 sc_fatal ("restr_object_in_place: bad var2, %ld\n", var2);
161 return FALSE;
162 }
163 }
164
165
166 /*
167 * restr_pass_task_object_location()
168 *
169 * Evaluate restrictions relating to object location.
170 */
171 static sc_bool
172 restr_pass_task_object_location (sc_gameref_t game,
173 sc_int var1, sc_int var2, sc_int var3)
174 {
175 const sc_var_setref_t vars = gs_get_vars (game);
176 sc_bool should_be;
177 sc_int object;
178
179 if (restr_trace)
180 {
181 sc_trace ("Restr: running object"
182 " location restriction, %ld, %ld, %ld\n", var1, var2, var3);
183 }
184
185 /* Initialize variables to avoid gcc warnings. */
186 should_be = FALSE;
187 object = -1;
188
189 /* See how things should look. */
190 if (var2 >= 0 && var2 < 6)
191 should_be = TRUE;
192 else if (var2 >= 6 && var2 < 12)
193 should_be = FALSE;
194 else
195 sc_fatal ("restr_pass_task_object_location: bad var2, %ld\n", var2);
196
197 /* Now find the addressed object. */
198 if (var1 == 0)
199 {
200 object = -1; /* No object */
201 should_be = !should_be;
202 }
203 else if (var1 == 1)
204 object = -1; /* Any object */
205 else if (var1 == 2)
206 object = var_get_ref_object (vars);
207 else if (var1 >= 3)
208 object = obj_dynamic_object (game, var1 - 3);
209 else
210 sc_fatal ("restr_pass_task_object_location: bad var1, %ld\n", var1);
211
212 /*
213 * Here it seems that we have to special case static objects that may have
214 * crept in through the referenced object. The object in place function
215 * isn't built to handle these.
216 *
217 * TODO What is the meaning of applying object restrictions to static
218 * objects?
219 */
220 if (var1 == 2 && object != -1 && obj_is_static (game, object))
221 {
222 if (restr_trace)
223 {
224 sc_trace ("Restr:"
225 " restriction object %ld is static, rejecting\n", object);
226 }
227
228 return FALSE;
229 }
230
231 /* Try to put it all together. */
232 if (object == -1)
233 {
234 sc_int target;
235
236 for (target = 0; target < gs_object_count (game); target++)
237 {
238 if (restr_object_in_place (game, target, var2, var3))
239 return should_be;
240 }
241 return !should_be;
242 }
243 return should_be == restr_object_in_place (game, object, var2, var3);
244 }
245
246
247 /*
248 * restr_pass_task_object_state()
249 *
250 * Evaluate restrictions relating to object states. This function is called
251 * from the library by lib_pass_alt_room(), so cannot be static.
252 */
253 sc_bool
254 restr_pass_task_object_state (sc_gameref_t game, sc_int var1, sc_int var2)
255 {
256 const sc_prop_setref_t bundle = gs_get_bundle (game);
257 const sc_var_setref_t vars = gs_get_vars (game);
258 sc_vartype_t vt_key[3];
259 sc_int object, openable, key;
260
261 if (restr_trace)
262 {
263 sc_trace ("Restr:"
264 " running object state restriction, %ld, %ld\n", var1, var2);
265 }
266
267 /* Find the object being addressed. */
268 if (var1 == 0)
269 object = var_get_ref_object (vars);
270 else
271 object = obj_stateful_object (game, var1 - 1);
272
273 /* We're interested only in openable objects. */
274 vt_key[0].string = "Objects";
275 vt_key[1].integer = object;
276 vt_key[2].string = "Openable";
277 openable = prop_get_integer (bundle, "I<-sis", vt_key);
278 if (openable > 0)
279 {
280 /* Is this object lockable? */
281 vt_key[2].string = "Key";
282 key = prop_get_integer (bundle, "I<-sis", vt_key);
283 if (key >= 0)
284 {
285 if (var2 <= 2)
286 return gs_object_openness (game, object) == var2 + 5;
287 else
288 return gs_object_state (game, object) == var2 - 2;
289 }
290 else
291 {
292 if (var2 <= 1)
293 return gs_object_openness (game, object) == var2 + 5;
294 else
295 return gs_object_state (game, object) == var2 - 1;
296 }
297 }
298 else
299 return gs_object_state (game, object) == var2 + 1;
300 }
301
302
303 /*
304 * restr_pass_task_task_state()
305 *
306 * Evaluate restrictions relating to task states.
307 */
308 static sc_bool
309 restr_pass_task_task_state (sc_gameref_t game, sc_int var1, sc_int var2)
310 {
311 sc_bool should_be;
312
313 if (restr_trace)
314 sc_trace ("Restr: running task restriction, %ld, %ld\n", var1, var2);
315
316 /* Initialize variables to avoid gcc warnings. */
317 should_be = FALSE;
318
319 /* See if the task should be done or not done. */
320 if (var2 == 0)
321 should_be = TRUE;
322 else if (var2 == 1)
323 should_be = FALSE;
324 else
325 sc_fatal ("restr_pass_task_task_state: bad var2, %ld\n", var2);
326
327 /* Check all tasks? */
328 if (var1 == 0)
329 {
330 sc_int task;
331
332 for (task = 0; task < gs_task_count (game); task++)
333 {
334 if (gs_task_done (game, task) == should_be)
335 return FALSE;
336 }
337 return TRUE;
338 }
339
340 /* Check just the given task. */
341 return gs_task_done (game, var1 - 1) == should_be;
342 }
343
344
345 /*
346 * restr_pass_task_char()
347 *
348 * Evaluate restrictions relating to player and NPCs.
349 */
350 static sc_bool
351 restr_pass_task_char (sc_gameref_t game, sc_int var1, sc_int var2, sc_int var3)
352 {
353 const sc_prop_setref_t bundle = gs_get_bundle (game);
354 const sc_var_setref_t vars = gs_get_vars (game);
355 sc_int npc1, npc2;
356
357 if (restr_trace)
358 {
359 sc_trace ("Restr:"
360 " running char restriction, %ld, %ld, %ld\n", var1, var2, var3);
361 }
362
363 /* Handle var2 types 1 and 2. */
364 if (var2 == 1) /* Not in same room as */
365 return !restr_pass_task_char (game, var1, 0, var3);
366 else if (var2 == 2) /* Alone */
367 return !restr_pass_task_char (game, var1, 3, var3);
368
369 /* Decode NPC number, -1 if none. */
370 npc1 = npc2 = -1;
371 if (var1 == 1)
372 npc1 = var_get_ref_character (vars);
373 else if (var1 > 1)
374 npc1 = var1 - 2;
375
376 /* Player or NPC? */
377 if (var1 == 0)
378 {
379 sc_vartype_t vt_key[2];
380 sc_int gender;
381
382 /* Player -- decode based on var2. */
383 switch (var2)
384 {
385 case 0: /* In same room as */
386 if (var3 == 1)
387 npc2 = var_get_ref_character (vars);
388 else if (var3 > 1)
389 npc2 = var3 - 2;
390 if (var3 == 0) /* Player */
391 return TRUE;
392 else
393 return npc_in_room (game, npc2, gs_playerroom (game));
394
395 case 3: /* Not alone */
396 return npc_count_in_room (game, gs_playerroom (game)) > 1;
397
398 case 4: /* Standing on */
399 return gs_playerposition (game) == 0
400 && gs_playerparent (game) == obj_standable_object (game,
401 var3 - 1);
402
403 case 5: /* Sitting on */
404 return gs_playerposition (game) == 1
405 && gs_playerparent (game) == obj_standable_object (game,
406 var3 - 1);
407
408 case 6: /* Lying on */
409 return gs_playerposition (game) == 2
410 && gs_playerparent (game) == obj_lieable_object (game,
411 var3 - 1);
412
413 case 7: /* Player gender */
414 vt_key[0].string = "Globals";
415 vt_key[1].string = "PlayerGender";
416 gender = prop_get_integer (bundle, "I<-ss", vt_key);
417 return gender == var3;
418
419 default:
420 sc_fatal ("restr_pass_task_char: invalid type, %ld\n", var2);
421 return FALSE;
422 }
423 }
424 else
425 {
426 sc_vartype_t vt_key[3];
427 sc_int gender;
428
429 /* NPC -- decode based on var2. */
430 switch (var2)
431 {
432 case 0: /* In same room as */
433 if (var3 == 0)
434 return npc_in_room (game, npc1, gs_playerroom (game));
435 if (var3 == 1)
436 npc2 = var_get_ref_character (vars);
437 else if (var3 > 1)
438 npc2 = var3 - 2;
439 return npc_in_room (game, npc1, gs_npc_location (game, npc2) - 1);
440
441 case 3: /* Not alone */
442 return npc_count_in_room (game, gs_npc_location (game, npc1) - 1) > 1;
443
444 case 4: /* Standing on */
445 return gs_npc_position (game, npc1) == 0
446 && gs_playerparent (game) == obj_standable_object (game, var3);
447
448 case 5: /* Sitting on */
449 return gs_npc_position (game, npc1) == 1
450 && gs_playerparent (game) == obj_standable_object (game, var3);
451
452 case 6: /* Lying on */
453 return gs_npc_position (game, npc1) == 2
454 && gs_playerparent (game) == obj_lieable_object (game, var3);
455
456 case 7: /* NPC gender */
457 vt_key[0].string = "NPCs";
458 vt_key[1].integer = npc1;
459 vt_key[2].string = "Gender";
460 gender = prop_get_integer (bundle, "I<-sis", vt_key);
461 return gender == var3;
462
463 default:
464 sc_fatal ("restr_pass_task_char: invalid type, %ld\n", var2);
465 return FALSE;
466 }
467 }
468 }
469
470
471 /*
472 * restr_pass_task_int_var()
473 *
474 * Helper for restr_pass_task_var(), handles integer variable restrictions.
475 */
476 static sc_bool
477 restr_pass_task_int_var (sc_gameref_t game,
478 sc_int var2, sc_int var3, sc_int value)
479 {
480 const sc_prop_setref_t bundle = gs_get_bundle (game);
481 const sc_var_setref_t vars = gs_get_vars (game);
482 sc_vartype_t vt_key[3];
483 sc_int value2;
484
485 if (restr_trace)
486 {
487 sc_trace ("Restr: running"
488 " integer var restriction, %ld, %ld, %ld\n", var2, var3, value);
489 }
490
491 /* Compare against var3 if that's what var2 says. */
492 switch (var2)
493 {
494 case 0:
495 return value < var3;
496 case 1:
497 return value <= var3;
498 case 2:
499 return value == var3;
500 case 3:
501 return value >= var3;
502 case 4:
503 return value > var3;
504 case 5:
505 return value != var3;
506
507 default:
508 /*
509 * Compare against the integer var numbered in var3 - 1, or the
510 * referenced number if var3 is zero. Make sure that we're comparing
511 * integer variables.
512 */
513 if (var3 == 0)
514 value2 = var_get_ref_number (vars);
515 else
516 {
517 const sc_char *name;
518 sc_int ivar, type;
519
520 ivar = restr_integer_variable (game, var3 - 1);
521 vt_key[0].string = "Variables";
522 vt_key[1].integer = ivar;
523 vt_key[2].string = "Name";
524 name = prop_get_string (bundle, "S<-sis", vt_key);
525 vt_key[2].string = "Type";
526 type = prop_get_integer (bundle, "I<-sis", vt_key);
527
528 if (type != TAFVAR_NUMERIC)
529 {
530 sc_fatal ("restr_pass_task_int_var:"
531 " non-integer in comparison, %s\n", name);
532 }
533
534 /* Get the value in variable numbered in var3 - 1. */
535 value2 = var_get_integer (vars, name);
536 }
537
538 switch (var2)
539 {
540 case 10:
541 return value < value2;
542 case 11:
543 return value <= value2;
544 case 12:
545 return value == value2;
546 case 13:
547 return value >= value2;
548 case 14:
549 return value > value2;
550 case 15:
551 return value != value2;
552
553 default:
554 sc_fatal ("restr_pass_task_int_var:"
555 " unknown int comparison, %ld\n", var2);
556 return FALSE;
557 }
558 }
559 }
560
561
562 /*
563 * restr_pass_task_string_var()
564 *
565 * Helper for restr_pass_task_var(), handles string variable restrictions.
566 */
567 static sc_bool
568 restr_pass_task_string_var (sc_int var2,
569 const sc_char *var4, const sc_char *value)
570 {
571 if (restr_trace)
572 {
573 sc_trace ("Restr: running string"
574 " var restriction, %ld, \"%s\", \"%s\"\n", var2, var4, value);
575 }
576
577 /* Make comparison against var4 based on var2 value. */
578 switch (var2)
579 {
580 case 0:
581 return strcmp (value, var4) == 0; /* == */
582 case 1:
583 return strcmp (value, var4) != 0; /* != */
584
585 default:
586 sc_fatal ("restr_pass_task_string_var:"
587 " unknown string comparison, %ld\n", var2);
588 return FALSE;
589 }
590 }
591
592
593 /*
594 * restr_pass_task_var()
595 *
596 * Evaluate restrictions relating to variables.
597 */
598 static sc_bool
599 restr_pass_task_var (sc_gameref_t game,
600 sc_int var1, sc_int var2, sc_int var3,
601 const sc_char *var4)
602 {
603 const sc_prop_setref_t bundle = gs_get_bundle (game);
604 const sc_var_setref_t vars = gs_get_vars (game);
605 sc_vartype_t vt_key[3];
606 sc_int type, value;
607 const sc_char *name, *string;
608
609 if (restr_trace)
610 {
611 sc_trace ("Restr: running var restriction,"
612 " %ld, %ld, %ld, \"%s\"\n", var1, var2, var3, var4);
613 }
614
615 /*
616 * For var1=0, compare against referenced number. For var1=1, compare
617 * against referenced text.
618 */
619 if (var1 == 0)
620 {
621 value = var_get_ref_number (vars);
622 return restr_pass_task_int_var (game, var2, var3, value);
623 }
624 else if (var1 == 1)
625 {
626 string = var_get_ref_text (vars);
627 return restr_pass_task_string_var (var2, var4, string);
628 }
629
630 /* Get the name and type of the variable being addressed. */
631 vt_key[0].string = "Variables";
632 vt_key[1].integer = var1 - 2;
633 vt_key[2].string = "Name";
634 name = prop_get_string (bundle, "S<-sis", vt_key);
635 vt_key[2].string = "Type";
636 type = prop_get_integer (bundle, "I<-sis", vt_key);
637
638 /* Select first based on variable type. */
639 switch (type)
640 {
641 case TAFVAR_NUMERIC:
642 value = var_get_integer (vars, name);
643 return restr_pass_task_int_var (game, var2, var3, value);
644
645 case TAFVAR_STRING:
646 string = var_get_string (vars, name);
647 return restr_pass_task_string_var (var2, var4, string);
648
649 default:
650 sc_fatal ("restr_pass_task_var: invalid variable type, %ld\n", type);
651 return FALSE;
652 }
653 }
654
655
656 /*
657 * restr_pass_task_restriction()
658 *
659 * Demultiplexer for task restrictions.
660 */
661 static sc_bool
662 restr_pass_task_restriction (sc_gameref_t game, sc_int task, sc_int restriction)
663 {
664 const sc_prop_setref_t bundle = gs_get_bundle (game);
665 sc_vartype_t vt_key[5];
666 sc_int type, var1, var2, var3;
667 const sc_char *var4;
668 sc_bool result = FALSE;
669
670 if (restr_trace)
671 {
672 sc_trace ("Restr:"
673 " evaluating task %ld restriction %ld\n", task, restriction);
674 }
675
676 /* Get the task restriction type. */
677 vt_key[0].string = "Tasks";
678 vt_key[1].integer = task;
679 vt_key[2].string = "Restrictions";
680 vt_key[3].integer = restriction;
681 vt_key[4].string = "Type";
682 type = prop_get_integer (bundle, "I<-sisis", vt_key);
683
684 /* Demultiplex depending on type. */
685 switch (type)
686 {
687 case 0: /* Object location. */
688 vt_key[4].string = "Var1";
689 var1 = prop_get_integer (bundle, "I<-sisis", vt_key);
690 vt_key[4].string = "Var2";
691 var2 = prop_get_integer (bundle, "I<-sisis", vt_key);
692 vt_key[4].string = "Var3";
693 var3 = prop_get_integer (bundle, "I<-sisis", vt_key);
694 result = restr_pass_task_object_location (game, var1, var2, var3);
695 break;
696
697 case 1: /* Object state. */
698 vt_key[4].string = "Var1";
699 var1 = prop_get_integer (bundle, "I<-sisis", vt_key);
700 vt_key[4].string = "Var2";
701 var2 = prop_get_integer (bundle, "I<-sisis", vt_key);
702 result = restr_pass_task_object_state (game, var1, var2);
703 break;
704
705 case 2: /* Task state. */
706 vt_key[4].string = "Var1";
707 var1 = prop_get_integer (bundle, "I<-sisis", vt_key);
708 vt_key[4].string = "Var2";
709 var2 = prop_get_integer (bundle, "I<-sisis", vt_key);
710 result = restr_pass_task_task_state (game, var1, var2);
711 break;
712
713 case 3: /* Player and NPCs. */
714 vt_key[4].string = "Var1";
715 var1 = prop_get_integer (bundle, "I<-sisis", vt_key);
716 vt_key[4].string = "Var2";
717 var2 = prop_get_integer (bundle, "I<-sisis", vt_key);
718 vt_key[4].string = "Var3";
719 var3 = prop_get_integer (bundle, "I<-sisis", vt_key);
720 result = restr_pass_task_char (game, var1, var2, var3);
721 break;
722
723 case 4: /* Variable. */
724 vt_key[4].string = "Var1";
725 var1 = prop_get_integer (bundle, "I<-sisis", vt_key);
726 vt_key[4].string = "Var2";
727 var2 = prop_get_integer (bundle, "I<-sisis", vt_key);
728 vt_key[4].string = "Var3";
729 var3 = prop_get_integer (bundle, "I<-sisis", vt_key);
730 vt_key[4].string = "Var4";
731 var4 = prop_get_string (bundle, "S<-sisis", vt_key);
732 result = restr_pass_task_var (game, var1, var2, var3, var4);
733 break;
734
735 default:
736 sc_fatal ("restr_pass_task_restriction:"
737 " unknown restriction type %ld\n", type);
738 }
739
740 if (restr_trace)
741 {
742 sc_trace ("Restr: task %ld restriction"
743 " %ld is %s\n", task, restriction, result ? "PASS" : "FAIL");
744 }
745
746 return result;
747 }
748
749
750 /* Enumeration of restrictions combination string tokens. */
751 enum
752 { TOK_RESTRICTION = '#',
753 TOK_AND = 'A',
754 TOK_OR = 'O',
755 TOK_LPAREN = '(',
756 TOK_RPAREN = ')',
757 TOK_EOS = '\0'
758 };
759
760 /* #O#A(#O#)-style expression, for tokenizing. */
761 static const sc_char *restr_expression = NULL;
762 static sc_int restr_index = 0;
763
764 /*
765 * restr_tokenize_start()
766 * restr_tokenize_end()
767 *
768 * Start and wrap up restrictions combinations string tokenization.
769 */
770 static void
771 restr_tokenize_start (const sc_char *expression)
772 {
773 /* Save expression, and restart index. */
774 restr_expression = expression;
775 restr_index = 0;
776 }
777
778 static void
779 restr_tokenize_end (void)
780 {
781 restr_expression = NULL;
782 restr_index = 0;
783 }
784
785
786 /*
787 * restr_next_token()
788 *
789 * Simple tokenizer for restrictions combination expressions.
790 */
791 static sc_char
792 restr_next_token (void)
793 {
794 assert (restr_expression);
795
796 /* Find the next non-space, and return it. */
797 while (TRUE)
798 {
799 /* Return NUL if at string end. */
800 if (restr_expression[restr_index] == NUL)
801 return restr_expression[restr_index];
802
803 /* Spin on whitespace. */
804 restr_index++;
805 if (sc_isspace (restr_expression[restr_index - 1]))
806 continue;
807
808 /* Return the character just passed. */
809 return restr_expression[restr_index - 1];
810 }
811 }
812
813
814 /* Evaluation values stack. */
815 static sc_bool restr_eval_values[MAX_NESTING_DEPTH];
816 static sc_int restr_eval_stack = 0;
817
818 /*
819 * The restriction number to evaluate. This advances with each call to
820 * evaluate and stack a restriction result.
821 */
822 static sc_int restr_eval_restriction = 0;
823
824 /* The current game used to evaluate restrictions, and the task in question. */
825 static sc_gameref_t restr_eval_game = NULL;
826 static sc_int restr_eval_task = 0;
827
828 /* The id of the lowest-indexed failing restriction. */
829 static sc_int restr_lowest_fail = -1;
830
831 /*
832 * restr_eval_start()
833 *
834 * Reset the evaluation stack to an empty state, and note the things we have
835 * to note for when we need to evaluate a restriction.
836 */
837 static void
838 restr_eval_start (sc_gameref_t game, sc_int task)
839 {
840 /* Clear stack. */
841 restr_eval_stack = 0;
842 restr_eval_restriction = 0;
843
844 /* Note evaluation details. */
845 restr_eval_game = game;
846 restr_eval_task = task;
847
848 /* Clear lowest indexed failing restriction. */
849 restr_lowest_fail = -1;
850 }
851
852
853 /*
854 * restr_eval_push()
855 *
856 * Push a value onto the values stack.
857 */
858 static void
859 restr_eval_push (sc_bool value)
860 {
861 if (restr_eval_stack >= MAX_NESTING_DEPTH)
862 sc_fatal ("restr_eval_push: stack overflow\n");
863
864 restr_eval_values[restr_eval_stack++] = value;
865 }
866
867
868 /*
869 * expr_restr_action()
870 *
871 * Evaluate the effect of an and/or into the values stack.
872 */
873 static void
874 restr_eval_action (sc_char token)
875 {
876 /* Select action based on parsed token. */
877 switch (token)
878 {
879 /* Handle evaluating and pushing a restriction result. */
880 case TOK_RESTRICTION:
881 {
882 sc_bool result;
883
884 /* Evaluate and push the next restriction. */
885 result = restr_pass_task_restriction (restr_eval_game,
886 restr_eval_task,
887 restr_eval_restriction);
888 restr_eval_push (result);
889
890 /*
891 * If the restriction failed, and there isn't yet a first failing one
892 * set, note this one as the first to fail.
893 */
894 if (restr_lowest_fail == -1 && !result)
895 restr_lowest_fail = restr_eval_restriction;
896
897 /* Increment restriction sequence identifier. */
898 restr_eval_restriction++;
899 break;
900 }
901
902 /* Handle cases of or-ing/and-ing restrictions. */
903 case TOK_OR:
904 case TOK_AND:
905 {
906 sc_bool val1, val2, result = FALSE;
907 assert (restr_eval_stack >= 2);
908
909 /* Get the top two stack values. */
910 val1 = restr_eval_values[restr_eval_stack - 2];
911 val2 = restr_eval_values[restr_eval_stack - 1];
912
913 /* Or, or and, into result. */
914 switch (token)
915 {
916 case TOK_OR:
917 result = val1 || val2;
918 break;
919 case TOK_AND:
920 result = val1 && val2;
921 break;
922
923 default:
924 sc_fatal ("restr_eval_action: bad token, '%c'\n", token);
925 }
926
927 /* Put result back at top of stack. */
928 restr_eval_stack--;
929 restr_eval_values[restr_eval_stack - 1] = result;
930 break;
931 }
932
933 default:
934 sc_fatal ("restr_eval_action: bad token, '%c'\n", token);
935 }
936 }
937
938
939 /*
940 * restr_eval_result()
941 *
942 * Return the top of the values stack as the evaluation result.
943 */
944 static sc_int
945 restr_eval_result (sc_int *lowest_fail)
946 {
947 if (restr_eval_stack != 1)
948 sc_fatal ("restr_eval_result: values stack not completed\n");
949
950 *lowest_fail = restr_lowest_fail;
951 return restr_eval_values[0];
952 }
953
954
955 /* Parse error jump buffer. */
956 static jmp_buf restr_parse_error;
957
958 /* Single lookahead token for parser. */
959 static sc_char restr_lookahead = '\0';
960
961 /*
962 * restr_match()
963 *
964 * Match a token with an expectation.
965 */
966 static void
967 restr_match (sc_char c)
968 {
969 if (restr_lookahead == c)
970 restr_lookahead = restr_next_token ();
971 else
972 {
973 sc_error ("restr_match:"
974 " syntax error, expected %d, got %d\n", c, restr_lookahead);
975 longjmp (restr_parse_error, 1);
976 }
977 }
978
979
980 /* Forward declaration for recursion. */
981 static void restr_bexpr (void);
982
983 /*
984 * restr_andexpr()
985 * restr_orexpr()
986 * restr_bexpr()
987 *
988 * Expression parsers. Here we go again...
989 */
990 static void
991 restr_andexpr (void)
992 {
993 restr_bexpr ();
994 while (restr_lookahead == TOK_AND)
995 {
996 restr_match (TOK_AND);
997 restr_bexpr ();
998 restr_eval_action (TOK_AND);
999 }
1000 }
1001
1002 static void
1003 restr_orexpr (void)
1004 {
1005 restr_andexpr ();
1006 while (restr_lookahead == TOK_OR)
1007 {
1008 restr_match (TOK_OR);
1009 restr_andexpr ();
1010 restr_eval_action (TOK_OR);
1011 }
1012 }
1013
1014 static void
1015 restr_bexpr (void)
1016 {
1017 switch (restr_lookahead)
1018 {
1019 case TOK_RESTRICTION:
1020 restr_match (TOK_RESTRICTION);
1021 restr_eval_action (TOK_RESTRICTION);
1022 break;
1023
1024 case TOK_LPAREN:
1025 restr_match (TOK_LPAREN);
1026 restr_orexpr ();
1027 restr_match (TOK_RPAREN);
1028 break;
1029
1030 default:
1031 sc_error ("restr_bexpr: syntax error, unexpected %d\n", restr_lookahead);
1032 longjmp (restr_parse_error, 1);
1033 }
1034 }
1035
1036
1037 /*
1038 * restr_get_fail_message()
1039 *
1040 * Get the FailMessage for the given task restriction; NULL if none.
1041 */
1042 static const sc_char *
1043 restr_get_fail_message (sc_gameref_t game, sc_int task, sc_int restriction)
1044 {
1045 const sc_prop_setref_t bundle = gs_get_bundle (game);
1046 sc_vartype_t vt_key[5];
1047 const sc_char *message;
1048
1049 /* Get the restriction message. */
1050 vt_key[0].string = "Tasks";
1051 vt_key[1].integer = task;
1052 vt_key[2].string = "Restrictions";
1053 vt_key[3].integer = restriction;
1054 vt_key[4].string = "FailMessage";
1055 message = prop_get_string (bundle, "S<-sisis", vt_key);
1056
1057 /* Return it, or NULL if empty. */
1058 return !sc_strempty (message) ? message : NULL;
1059 }
1060
1061
1062 /*
1063 * restr_debug_trace()
1064 *
1065 * Set restrictions tracing on/off.
1066 */
1067 void
1068 restr_debug_trace (sc_bool flag)
1069 {
1070 restr_trace = flag;
1071 }
1072
1073
1074 /*
1075 * restr_eval_task_restrictions()
1076 *
1077 * Main handler for a given set of task restrictions. Returns TRUE in pass
1078 * if the restrictions pass, FALSE if not. On FALSE pass returns, it also
1079 * returns a fail message string from the restriction deemed to have caused
1080 * the failure (that is, the first one with a FailMessage property), or NULL
1081 * if no failing restriction has a FailMessage. The function's main return
1082 * value is TRUE if restrictions parsed successfully, FALSE otherwise.
1083 */
1084 sc_bool
1085 restr_eval_task_restrictions (sc_gameref_t game,
1086 sc_int task, sc_bool *pass,
1087 const sc_char **fail_message)
1088 {
1089 const sc_prop_setref_t bundle = gs_get_bundle (game);
1090 sc_vartype_t vt_key[3];
1091 sc_int restr_count, lowest_fail;
1092 const sc_char *pattern;
1093 sc_bool result;
1094 assert (pass && fail_message);
1095
1096 /* Get the count of restrictions on the task. */
1097 vt_key[0].string = "Tasks";
1098 vt_key[1].integer = task;
1099 vt_key[2].string = "Restrictions";
1100 restr_count = prop_get_child_count (bundle, "I<-sis", vt_key);
1101
1102 /* If none, stop now, acting as if all passed. */
1103 if (restr_count == 0)
1104 {
1105 if (restr_trace)
1106 sc_trace ("Restr: task %ld has no restrictions\n", task);
1107
1108 *pass = TRUE;
1109 *fail_message = NULL;
1110 return TRUE;
1111 }
1112
1113 /* Get the task's restriction combination pattern. */
1114 vt_key[2].string = "RestrMask";
1115 pattern = prop_get_string (bundle, "S<-sis", vt_key);
1116
1117 if (restr_trace)
1118 {
1119 sc_trace ("Restr: task %ld"
1120 " has %ld restrictions, %s\n", task, restr_count, pattern);
1121 }
1122
1123 /* Set up the evaluation stack and tokenizer. */
1124 restr_eval_start (game, task);
1125 restr_tokenize_start (pattern);
1126
1127 /* Try parsing the pattern, and catch errors. */
1128 if (setjmp (restr_parse_error) == 0)
1129 {
1130 /* Parse the pattern, and ensure it ends at string end. */
1131 restr_lookahead = restr_next_token ();
1132 restr_orexpr ();
1133 restr_match (TOK_EOS);
1134 }
1135 else
1136 {
1137 /* Parse error -- clean up tokenizer and return fail. */
1138 restr_tokenize_end ();
1139 return FALSE;
1140 }
1141
1142 /* Clean up tokenizer and get the evaluation result. */
1143 restr_tokenize_end ();
1144 result = restr_eval_result (&lowest_fail);
1145
1146 if (restr_trace)
1147 {
1148 sc_trace ("Restr: task %ld"
1149 " restrictions %s\n", task, result ? "PASS" : "FAIL");
1150 }
1151
1152 /*
1153 * Return the result, and if a restriction fails, then return the
1154 * FailMessage of the lowest indexed failing restriction (or NULL if this
1155 * restriction has no FailMessage).
1156 *
1157 * Then return TRUE since parsing and running the restrictions succeeded
1158 * (even if the restrictions themselves didn't).
1159 */
1160 *pass = result;
1161 if (result)
1162 *fail_message = NULL;
1163 else
1164 *fail_message = restr_get_fail_message (game, task, lowest_fail);
1165 return TRUE;
1166 }