Debian Codegraph @

terps/scare/sctasks.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 Implements task return FALSE on no output, a slight extension of
24 * current jAsea behavior.
25 */
26
27 #include <assert.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 /*
37 * Tasks can run other tasks, leading to the possibility of an infinite loop
38 * in the task calling sequence. It's a game error, and we'll apply a limit
39 * to the task recursion depth to try and catch it more controllably than
40 * waiting for memory exhaustion.
41 */
42 enum { TASK_MAXIMUM_RECURSION = 128 };
43
44 /* Trace flag, set before running. */
45 static sc_bool task_trace = FALSE;
46
47
48 /*
49 * task_get_hint_common()
50 * task_get_hint_question()
51 * task_get_hint_subtle()
52 * task_get_hint_unsubtle()
53 * task_has_hints()
54 *
55 * Return the assorted hint text strings, and TRUE if the given task offers
56 * hints.
57 */
58 static const sc_char *
59 task_get_hint_common (sc_gameref_t game, sc_int task, const sc_char *hint)
60 {
61 const sc_prop_setref_t bundle = gs_get_bundle (game);
62 sc_vartype_t vt_key[3];
63 const sc_char *retval;
64
65 /* Look up and return the requested hint string. */
66 vt_key[0].string = "Tasks";
67 vt_key[1].integer = task;
68 vt_key[2].string = hint;
69 retval = prop_get_string (bundle, "S<-sis", vt_key);
70 return retval;
71 }
72
73 const sc_char *
74 task_get_hint_question (sc_gameref_t game, sc_int task)
75 {
76 return task_get_hint_common (game, task, "Question");
77 }
78
79 const sc_char *
80 task_get_hint_subtle (sc_gameref_t game, sc_int task)
81 {
82 return task_get_hint_common (game, task, "Hint1");
83 }
84
85 const sc_char *
86 task_get_hint_unsubtle (sc_gameref_t game, sc_int task)
87 {
88 return task_get_hint_common (game, task, "Hint2");
89 }
90
91 sc_bool
92 task_has_hints (sc_gameref_t game, sc_int task)
93 {
94 /* A non-empty question implies hints available. */
95 return !sc_strempty (task_get_hint_question (game, task));
96 }
97
98
99 /*
100 * task_can_run_task_directional()
101 *
102 * Return TRUE if player is in a room where the task can be run and the task
103 * is runnable in the given direction.
104 */
105 sc_bool
106 task_can_run_task_directional (sc_gameref_t game,
107 sc_int task, sc_bool forwards)
108 {
109 const sc_prop_setref_t bundle = gs_get_bundle (game);
110 sc_vartype_t vt_key[5];
111 sc_int type;
112
113 /* If already run, non-repeatable tasks are not re-runnable forwards. */
114 if (forwards && gs_task_done (game, task))
115 {
116 sc_bool repeatable;
117 const sc_char *repeattext;
118
119 vt_key[0].string = "Tasks";
120 vt_key[1].integer = task;
121 vt_key[2].string = "Repeatable";
122 repeatable = prop_get_boolean (bundle, "B<-sis", vt_key);
123 if (!repeatable)
124 return FALSE;
125
126 vt_key[2].string = "RepeatText";
127 repeattext = prop_get_string (bundle, "S<-sis", vt_key);
128 if (!sc_strempty (repeattext))
129 return FALSE;
130 }
131
132 /* If checking for reverse, test the reversibility flag. */
133 if (!forwards)
134 {
135 sc_bool reversible;
136
137 vt_key[0].string = "Tasks";
138 vt_key[1].integer = task;
139 vt_key[2].string = "Reversible";
140 reversible = prop_get_boolean (bundle, "B<-sis", vt_key);
141 if (!reversible)
142 return FALSE;
143 }
144
145 /* Check room list for the task and return it. */
146 vt_key[0].string = "Tasks";
147 vt_key[1].integer = task;
148 vt_key[2].string = "Where";
149 vt_key[3].string = "Type";
150 type = prop_get_integer (bundle, "I<-siss", vt_key);
151 switch (type)
152 {
153 case ROOMLIST_NO_ROOMS:
154 return FALSE;
155 case ROOMLIST_ALL_ROOMS:
156 return TRUE;
157
158 case ROOMLIST_ONE_ROOM:
159 vt_key[3].string = "Room";
160 return prop_get_integer (bundle,
161 "I<-siss", vt_key) == gs_playerroom (game);
162
163 case ROOMLIST_SOME_ROOMS:
164 vt_key[3].string = "Rooms";
165 vt_key[4].integer = gs_playerroom (game);
166 return prop_get_boolean (bundle, "B<-sissi", vt_key);
167
168 default:
169 sc_fatal ("task_can_run_task_directional: invalid type, %ld\n", type);
170 return FALSE;
171 }
172 }
173
174
175 /*
176 * task_can_run_task()
177 *
178 * Returns TRUE if the task can be run in either direction.
179 */
180 sc_bool
181 task_can_run_task (sc_gameref_t game, sc_int task)
182 {
183 /*
184 * Testing reversible tasks first may be a little more efficient if they
185 * aren't common in games. There is, though, probably a little bit of
186 * redundant work going on here.
187 */
188 return task_can_run_task_directional (game, task, FALSE)
189 || task_can_run_task_directional (game, task, TRUE);
190 }
191
192
193 /*
194 * task_move_object()
195 *
196 * Move an object to a place.
197 */
198 static void
199 task_move_object (sc_gameref_t game, sc_int object, sc_int var2, sc_int var3)
200 {
201 const sc_var_setref_t vars = gs_get_vars (game);
202
203 /* Select action depending on var2. */
204 switch (var2)
205 {
206 case 0: /* To room */
207 if (var3 == 0)
208 {
209 if (task_trace)
210 sc_trace ("Task: moving object %ld to hidden\n", object);
211
212 gs_object_make_hidden (game, object);
213 }
214 else
215 {
216 if (task_trace)
217 {
218 sc_trace ("Task: moving object %ld to room %ld\n",
219 object, var3 - 1);
220 }
221
222 if (var3 == 0)
223 gs_object_player_get (game, object);
224 else
225 gs_object_to_room (game, object, var3 - 1);
226 }
227 break;
228
229 case 1: /* To roomgroup part */
230 if (task_trace)
231 {
232 sc_trace ("Task: moving object %ld to random room in group %ld\n",
233 object, var3);
234 }
235
236 gs_object_to_room (game, object,
237 lib_random_roomgroup_member (game, var3));
238 break;
239
240 case 2: /* Into object */
241 if (task_trace)
242 sc_trace ("Task: moving object %ld into %ld\n", object, var3);
243
244 gs_object_move_into (game, object, obj_container_object (game, var3));
245 break;
246
247 case 3: /* Onto object */
248 if (task_trace)
249 sc_trace ("Task: moving object %ld onto %ld\n", object, var3);
250
251 gs_object_move_onto (game, object, obj_surface_object (game, var3));
252 break;
253
254 case 4: /* Held by */
255 if (task_trace)
256 sc_trace ("Task: moving object %ld to held by %ld\n", object, var3);
257
258 if (var3 == 0) /* Player */
259 gs_object_player_get (game, object);
260 else if (var3 == 1) /* Ref character */
261 gs_object_npc_get (game, object, var_get_ref_character (vars));
262 else /* NPC id */
263 gs_object_npc_get (game, object, var3 - 2);
264 break;
265
266 case 5: /* Worn by */
267 if (task_trace)
268 sc_trace ("Task: moving object %ld to worn by %ld\n", object, var3);
269
270 if (var3 == 0) /* Player */
271 gs_object_player_wear (game, object);
272 else if (var3 == 1) /* Ref character */
273 gs_object_npc_wear (game, object, var_get_ref_character (vars));
274 else /* NPC id */
275 gs_object_npc_wear (game, object, var3 - 2);
276 break;
277
278 case 6: /* Same room as */
279 {
280 sc_int room, npc;
281
282 if (task_trace)
283 {
284 sc_trace ("Task: moving object %ld to same room as %ld\n",
285 object, var3);
286 }
287
288 if (var3 == 0) /* Player */
289 room = gs_playerroom (game);
290 else if (var3 == 1) /* Ref character */
291 {
292 npc = var_get_ref_character (vars);
293 room = gs_npc_location (game, npc) - 1;
294 }
295 else /* NPC id */
296 {
297 npc = var3 - 2;
298 room = gs_npc_location (game, npc) - 1;
299 }
300 gs_object_to_room (game, object, room);
301 break;
302 }
303
304 default:
305 sc_fatal ("task_move_object: unknown move type, %ld\n", var2);
306 }
307 }
308
309
310 /*
311 * task_run_move_object_action()
312 *
313 * Demultiplex an object move action and execute it.
314 */
315 static void
316 task_run_move_object_action (sc_gameref_t game,
317 sc_int var1, sc_int var2, sc_int var3)
318 {
319 const sc_var_setref_t vars = gs_get_vars (game);
320 sc_int object;
321
322 /* Select depending on value in var1. */
323 switch (var1)
324 {
325 case 0: /* All held */
326 for (object = 0; object < gs_object_count (game); object++)
327 {
328 if (gs_object_position (game, object) == OBJ_HELD_PLAYER)
329 task_move_object (game, object, var2, var3);
330 }
331 break;
332
333 case 1: /* All worn */
334 for (object = 0; object < gs_object_count (game); object++)
335 {
336 if (gs_object_position (game, object) == OBJ_WORN_PLAYER)
337 task_move_object (game, object, var2, var3);
338 }
339 break;
340
341 case 2: /* Ref object */
342 object = var_get_ref_object (vars);
343 task_move_object (game, object, var2, var3);
344 break;
345
346 default: /* Dynamic object */
347 object = obj_dynamic_object (game, var1 - 3);
348 task_move_object (game, object, var2, var3);
349 break;
350 }
351 }
352
353
354 /*
355 * task_move_npc_to_room()
356 *
357 * Move an NPC to a given room.
358 */
359 static void
360 task_move_npc_to_room (sc_gameref_t game, sc_int npc, sc_int room)
361 {
362 if (task_trace)
363 sc_trace ("Task: moving NPC %ld to room %ld\n", npc, room);
364
365 /* Update the NPC's state. */
366 if (room < gs_room_count (game))
367 gs_set_npc_location (game, npc, room + 1);
368 else
369 gs_set_npc_location (game, npc,
370 lib_random_roomgroup_member (game,
371 room - gs_room_count (game)) + 1);
372
373 gs_set_npc_parent (game, npc, -1);
374 gs_set_npc_position (game, npc, 0);
375 }
376
377
378 /*
379 * task_run_move_npc_action()
380 *
381 * Move player or NPC.
382 */
383 static void
384 task_run_move_npc_action (sc_gameref_t game,
385 sc_int var1, sc_int var2, sc_int var3)
386 {
387 const sc_var_setref_t vars = gs_get_vars (game);
388 sc_int npc, room, ref_npc = -1;
389
390 /* Player or NPC? */
391 if (var1 == 0)
392 {
393 /* Player -- decide where to move player to. */
394 switch (var2)
395 {
396 case 0: /* To room */
397 gs_move_player_to_room (game, var3);
398 return;
399
400 case 1: /* To roomgroup part */
401 if (task_trace)
402 {
403 sc_trace ("Task: moving player to random room in group %ld\n",
404 var3);
405 }
406
407 gs_move_player_to_room (game,
408 lib_random_roomgroup_member (game, var3));
409 return;
410
411 case 2: /* To same room as... */
412 switch (var3)
413 {
414 case 0: /* ...player! */
415 return;
416 case 1: /* ...referenced NPC */
417 npc = var_get_ref_character (vars);
418 break;
419 default: /* ...specified NPC */
420 npc = var3 - 2;
421 break;
422 }
423
424 if (task_trace)
425 sc_trace ("Task: moving player to same room as NPC %ld\n", npc);
426
427 room = gs_npc_location (game, npc) - 1;
428 if (room < 0)
429 {
430 if (task_trace)
431 sc_trace ("Task: silently suppressed player move to hidden\n");
432 }
433 else
434 gs_move_player_to_room (game, room);
435 return;
436
437 case 3: /* To standing on */
438 gs_set_playerposition (game, 0);
439 gs_set_playerparent (game, obj_standable_object (game, var3 - 1));
440 return;
441
442 case 4: /* To sitting on */
443 gs_set_playerposition (game, 1);
444 gs_set_playerparent (game, obj_standable_object (game, var3 - 1));
445 return;
446
447 case 5: /* To lying on */
448 gs_set_playerposition (game, 2);
449 gs_set_playerparent (game, obj_lieable_object (game, var3 - 1));
450 return;
451
452 default:
453 sc_fatal ("task_run_move_npc_action:"
454 " unknown player move type, %ld\n", var2);
455 return;
456 }
457 }
458 else
459 {
460 /* NPC -- first find which NPC to move about. */
461 if (var1 == 1)
462 npc = var_get_ref_character (vars);
463 else
464 npc = var1 - 2;
465
466 /* Decide where to move the NPC to. */
467 switch (var2)
468 {
469 case 0: /* To room */
470 task_move_npc_to_room (game, npc, var3 - 1);
471 return;
472
473 case 1: /* To roomgroup part */
474 if (task_trace)
475 {
476 sc_trace ("Task: moving NPC %ld to random room in group %ld\n",
477 npc, var3);
478 }
479
480 task_move_npc_to_room (game, npc,
481 lib_random_roomgroup_member (game, var3));
482 return;
483
484 case 2: /* To same room as... */
485 switch (var3)
486 {
487 case 0: /* ...player */
488 if (task_trace)
489 {
490 sc_trace ("Task: moving NPC %ld to same room as player\n",
491 npc);
492 }
493
494 task_move_npc_to_room (game, npc, gs_playerroom (game));
495 break;
496 case 1: /* ...referenced NPC */
497 ref_npc = var_get_ref_character (vars);
498 if (task_trace)
499 {
500 sc_trace ("Task: moving NPC %ld to"
501 " same room as referenced NPC %ld\n", npc, ref_npc);
502 }
503
504 room = gs_npc_location (game, ref_npc) - 1;
505 task_move_npc_to_room (game, npc, room);
506 break;
507 default: /* ...specified NPC */
508 ref_npc = var3 - 2;
509 if (task_trace)
510 {
511 sc_trace ("Task: moving NPC %ld to"
512 " same room as NPC %ld\n", npc, ref_npc);
513 }
514
515 room = gs_npc_location (game, ref_npc) - 1;
516 task_move_npc_to_room (game, npc, room);
517 break;
518 }
519 return;
520
521 case 3: /* To standing on */
522 gs_set_npc_position (game, npc, 0);
523 gs_set_npc_parent (game, npc, obj_standable_object (game, var3));
524 return;
525
526 case 4: /* To sitting on */
527 gs_set_npc_position (game, npc, 1);
528 gs_set_npc_parent (game, npc, obj_standable_object (game, var3));
529 return;
530
531 case 5: /* To lying on */
532 gs_set_npc_position (game, npc, 2);
533 gs_set_npc_parent (game, npc, obj_lieable_object (game, var3));
534 return;
535
536 default:
537 sc_fatal ("task_run_move_npc_action:"
538 " unknown NPC move type, %ld\n", var2);
539 return;
540 }
541 }
542 }
543
544
545 /*
546 * task_run_change_object_status()
547 *
548 * Change the status of an object.
549 */
550 static void
551 task_run_change_object_status (sc_gameref_t game, sc_int var1, sc_int var2)
552 {
553 const sc_prop_setref_t bundle = gs_get_bundle (game);
554 sc_vartype_t vt_key[3];
555 sc_int object, openable, lockable;
556
557 if (task_trace)
558 {
559 sc_trace ("Task: setting status of stateful object %ld to %ld\n",
560 var1, var2);
561 }
562
563 /* Identify the target object. */
564 object = obj_stateful_object (game, var1);
565
566 /* See if openable. */
567 vt_key[0].string = "Objects";
568 vt_key[1].integer = object;
569 vt_key[2].string = "Openable";
570 openable = prop_get_integer (bundle, "I<-sis", vt_key);
571 if (openable > 0)
572 {
573 /* See if lockable. */
574 vt_key[2].string = "Key";
575 lockable = prop_get_integer (bundle, "I<-sis", vt_key);
576 if (lockable >= 0)
577 {
578 /* Lockable. */
579 if (var2 <= 2)
580 gs_set_object_openness (game, object, var2 + 5);
581 else
582 gs_set_object_state (game, object, var2 - 2);
583 }
584 else
585 {
586 /* Not lockable, though openable. */
587 if (var2 <= 1)
588 gs_set_object_openness (game, object, var2 + 5);
589 else
590 gs_set_object_state (game, object, var2 - 1);
591 }
592 }
593 else
594 /* Not openable. */
595 gs_set_object_state (game, object, var2 + 1);
596
597 if (task_trace)
598 {
599 sc_trace ("Task: openness of object %ld is now %ld\n",
600 object, gs_object_openness (game, object));
601 sc_trace ("Task: state of object %ld is now %ld\n",
602 object, gs_object_state (game, object));
603 }
604 }
605
606
607 /*
608 * task_run_change_variable_action()
609 *
610 * Change a variable's value in inscrutable ways.
611 */
612 static void
613 task_run_change_variable_action (sc_gameref_t game,
614 sc_int var1, sc_int var2, sc_int var3,
615 const sc_char *expr, sc_int var5)
616 {
617 const sc_filterref_t filter = gs_get_filter (game);
618 const sc_prop_setref_t bundle = gs_get_bundle (game);
619 const sc_var_setref_t vars = gs_get_vars (game);
620 sc_vartype_t vt_key[3];
621 const sc_char *name, *string;
622 sc_char *mutable_string;
623 sc_int type, value;
624
625 /*
626 * At this point, we need to checkpoint the filter. We're about to change
627 * a variable value, so interpolating here before doing that ensures that
628 * any currently buffered text gets the values that were set when the text
629 * was buffered.
630 */
631 pf_checkpoint (filter, vars, bundle);
632
633 /* Get the name and type of the variable being addressed. */
634 vt_key[0].string = "Variables";
635 vt_key[1].integer = var1;
636 vt_key[2].string = "Name";
637 name = prop_get_string (bundle, "S<-sis", vt_key);
638 vt_key[2].string = "Type";
639 type = prop_get_integer (bundle, "I<-sis", vt_key);
640
641 /* Select first based on variable type. */
642 switch (type)
643 {
644 case TAFVAR_NUMERIC: /* Integer */
645
646 /* Select again based on action type. */
647 switch (var2)
648 {
649 case 0: /* Var = */
650 if (task_trace)
651 sc_trace ("Task: variable %ld (%s) = %ld\n", var1, name, var3);
652
653 var_put_integer (vars, name, var3);
654 return;
655
656 case 1: /* Var += */
657 if (task_trace)
658 sc_trace ("Task: variable %ld (%s) += %ld\n", var1, name, var3);
659
660 value = var_get_integer (vars, name) + var3;
661 var_put_integer (vars, name, value);
662 return;
663
664 case 2: /* Var = rnd(range) */
665 if (task_trace)
666 {
667 sc_trace ("Task: variable %ld (%s) = random(%ld,%ld)\n",
668 var1, name, var3, var5);
669 }
670
671 value = sc_randomint (var3, var5);
672 var_put_integer (vars, name, value);
673 return;
674
675 case 3: /* Var += rnd(range) */
676 if (task_trace)
677 {
678 sc_trace ("Task: variable %ld (%s) += random(%ld,%ld)\n",
679 var1, name, var3, var5);
680 }
681
682 value = var_get_integer (vars, name) + sc_randomint (var3, var5);
683 var_put_integer (vars, name, value);
684 return;
685
686 case 4: /* Var = ref */
687 value = var_get_ref_number (vars);
688 if (task_trace)
689 {
690 sc_trace ("Task: variable %ld (%s) = ref, %ld\n",
691 var1, name, value);
692 }
693
694 var_put_integer (vars, name, value);
695 return;
696
697 case 5: /* Var = expr */
698 if (!expr_eval_numeric_expression (expr, vars, &value))
699 {
700 sc_error ("task_run_change_variable_action:"
701 " invalid expression, %s\n", expr);
702 value = 0;
703 }
704 if (task_trace)
705 {
706 sc_trace ("Task: variable %ld (%s) = %s, %ld\n",
707 var1, name, expr, value);
708 }
709
710 var_put_integer (vars, name, value);
711 return;
712
713 default:
714 sc_fatal ("task_run_change_variable_action:"
715 " unknown integer change type, %ld\n", var2);
716 }
717
718 case TAFVAR_STRING: /* String */
719
720 /* Select again based on action type. */
721 switch (var2)
722 {
723 case 0: /* Var = text literal */
724 if (task_trace)
725 {
726 sc_trace ("Task: variable %ld (%s) = \"%s\"\n",
727 var1, name, expr);
728 }
729
730 var_put_string (vars, name, expr);
731 return;
732
733 case 1: /* Var = ref */
734 string = var_get_ref_text (vars);
735 if (task_trace)
736 {
737 sc_trace ("Task: variable %ld (%s) = ref, \"%s\"\n",
738 var1, name, string);
739 }
740
741 var_put_string (vars, name, string);
742 return;
743
744 case 2: /* Var = expr */
745 if (!expr_eval_string_expression (expr, vars, &mutable_string))
746 {
747 sc_error ("task_run_change_variable_action:"
748 " invalid string expression, %s\n", expr);
749 mutable_string = sc_malloc (strlen ("[expr error]") + 1);
750 strcpy (mutable_string, "[expr error]");
751 }
752 if (task_trace)
753 {
754 sc_trace ("Task: variable %ld (%s) = %s, %s\n",
755 var1, name, expr, mutable_string);
756 }
757
758 var_put_string (vars, name, mutable_string);
759 sc_free (mutable_string);
760 return;
761
762 default:
763 sc_fatal ("task_run_change_variable_action:"
764 " unknown string change type, %ld\n", var2);
765 }
766
767 default:
768 sc_fatal ("task_run_change_variable_action:"
769 " invalid variable type, %ld\n", type);
770 }
771 }
772
773
774 /*
775 * task_run_change_score_action()
776 *
777 * Change game score.
778 */
779 static void
780 task_run_change_score_action (sc_gameref_t game, sc_int task, sc_int var1)
781 {
782 const sc_prop_setref_t bundle = gs_get_bundle (game);
783
784 /* Increasing or decreasing the score? */
785 if (var1 > 0)
786 {
787 sc_bool increase_score;
788
789 /* See if this task is already scored. */
790 increase_score = !gs_task_scored (game, task);
791 if (!increase_score)
792 {
793 sc_vartype_t vt_key[3];
794 sc_int version;
795
796 if (task_trace)
797 sc_trace ("Task: already scored task %ld\n", var1);
798
799 /* Version 3.8 games permit tasks to rescore. */
800 vt_key[0].string = "Version";
801 version = prop_get_integer (bundle, "I<-s", vt_key);
802 if (version == TAF_VERSION_380)
803 {
804 vt_key[0].string = "Tasks";
805 vt_key[1].integer = task;
806 vt_key[2].string = "SingleScore";
807 increase_score = !prop_get_boolean (bundle, "B<-sis", vt_key);
808
809 if (increase_score)
810 {
811 if (task_trace)
812 sc_trace ("Task: rescoring version 3.8 task anyway\n");
813 }
814 }
815 }
816
817 /*
818 * Increase the score if not yet scored or a version 3.8 multiple
819 * scoring task, and note as a scored task.
820 */
821 if (increase_score)
822 {
823 if (task_trace)
824 sc_trace ("Task: increased score by %ld\n", var1);
825
826 game->score += var1;
827 gs_set_task_scored (game, task, TRUE);
828 }
829 }
830 else if (var1 < 0)
831 {
832 /* Decrease the score. */
833 if (task_trace)
834 sc_trace ("Task: decreased score by %ld\n", -(var1));
835
836 game->score += var1;
837 }
838 }
839
840
841 /*
842 * task_run_set_task_action()
843 *
844 * Redirect to another task.
845 */
846 static sc_bool
847 task_run_set_task_action (sc_gameref_t game, sc_int var1, sc_int var2)
848 {
849 sc_bool status = FALSE;
850
851 /* Select based on var1. */
852 if (var1 == 0)
853 {
854 /* Redirect forwards. */
855 if (task_can_run_task_directional (game, var2, TRUE))
856 {
857 if (task_trace)
858 sc_trace ("Task: redirecting to task %ld\n", var2);
859
860 status = task_run_task (game, var2, TRUE);
861 }
862 else
863 {
864 if (task_trace)
865 sc_trace ("Task: can't redirect to task %ld\n", var2);
866 }
867 }
868 else
869 {
870 /* Undo task. */
871 gs_set_task_done (game, var2, FALSE);
872 if (task_trace)
873 sc_trace ("Task: reversing task %ld\n", var2);
874 }
875
876 return status;
877 }
878
879
880 /*
881 * task_run_end_game_action()
882 *
883 * End of game task action.
884 */
885 static sc_bool
886 task_run_end_game_action (sc_gameref_t game, sc_int var1)
887 {
888 const sc_filterref_t filter = gs_get_filter (game);
889 const sc_prop_setref_t bundle = gs_get_bundle (game);
890 sc_bool status = FALSE;
891
892 /* Print a message based on var1. */
893 switch (var1)
894 {
895 case 0:
896 {
897 sc_vartype_t vt_key[2];
898 const sc_char *wintext;
899
900 /* Get game WinText. */
901 vt_key[0].string = "Header";
902 vt_key[1].string = "WinText";
903 wintext = prop_get_string (bundle, "S<-ss", vt_key);
904
905 /* Print WinText, if any defined, otherwise a default. */
906 if (!sc_strempty (wintext))
907 {
908 pf_buffer_string (filter, wintext);
909 pf_buffer_character (filter, '\n');
910 }
911 else
912 pf_buffer_string (filter, "Congratulations!\n");
913
914 /* Handle any associated WinRes resource. */
915 vt_key[0].string = "Globals";
916 vt_key[1].string = "WinRes";
917 res_handle_resource (game, "ss", vt_key);
918
919 status = TRUE;
920 break;
921 }
922
923 case 1:
924 pf_buffer_string (filter, "Better luck next time.\n");
925 status = TRUE;
926 break;
927
928 case 2:
929 pf_buffer_string (filter, "I'm afraid you are dead!\n");
930 status = TRUE;
931 break;
932
933 case 3:
934 break;
935
936 default:
937 sc_fatal ("task_run_end_game_action: invalid type, %ld\n", var1);
938 }
939
940 /* Stop the game, and note that it's not resumeable. */
941 game->is_running = FALSE;
942 game->has_completed = TRUE;
943
944 return status;
945 }
946
947
948 /*
949 * task_run_task_action()
950 *
951 * Demultiplexer for task actions.
952 */
953 static sc_bool
954 task_run_task_action (sc_gameref_t game, sc_int task, sc_int action)
955 {
956 const sc_prop_setref_t bundle = gs_get_bundle (game);
957 sc_vartype_t vt_key[5];
958 sc_int type, var1, var2, var3, var5;
959 const sc_char *expr;
960 sc_bool status = FALSE;
961
962 /* Get the task action type. */
963 vt_key[0].string = "Tasks";
964 vt_key[1].integer = task;
965 vt_key[2].string = "Actions";
966 vt_key[3].integer = action;
967 vt_key[4].string = "Type";
968 type = prop_get_integer (bundle, "I<-sisis", vt_key);
969
970 /* Demultiplex depending on type. */
971 switch (type)
972 {
973 case 0: /* Move object. */
974 vt_key[4].string = "Var1";
975 var1 = prop_get_integer (bundle, "I<-sisis", vt_key);
976 vt_key[4].string = "Var2";
977 var2 = prop_get_integer (bundle, "I<-sisis", vt_key);
978 vt_key[4].string = "Var3";
979 var3 = prop_get_integer (bundle, "I<-sisis", vt_key);
980 task_run_move_object_action (game, var1, var2, var3);
981 break;
982
983 case 1: /* Move player/NPC. */
984 vt_key[4].string = "Var1";
985 var1 = prop_get_integer (bundle, "I<-sisis", vt_key);
986 vt_key[4].string = "Var2";
987 var2 = prop_get_integer (bundle, "I<-sisis", vt_key);
988 vt_key[4].string = "Var3";
989 var3 = prop_get_integer (bundle, "I<-sisis", vt_key);
990 task_run_move_npc_action (game, var1, var2, var3);
991 break;
992
993 case 2: /* Change object status. */
994 vt_key[4].string = "Var1";
995 var1 = prop_get_integer (bundle, "I<-sisis", vt_key);
996 vt_key[4].string = "Var2";
997 var2 = prop_get_integer (bundle, "I<-sisis", vt_key);
998 task_run_change_object_status (game, var1, var2);
999 break;
1000
1001 case 3: /* Change variable. */
1002 vt_key[4].string = "Var1";
1003 var1 = prop_get_integer (bundle, "I<-sisis", vt_key);
1004 vt_key[4].string = "Var2";
1005 var2 = prop_get_integer (bundle, "I<-sisis", vt_key);
1006 vt_key[4].string = "Var3";
1007 var3 = prop_get_integer (bundle, "I<-sisis", vt_key);
1008 vt_key[4].string = "Expr";
1009 expr = prop_get_string (bundle, "S<-sisis", vt_key);
1010 vt_key[4].string = "Var5";
1011 var5 = prop_get_integer (bundle, "I<-sisis", vt_key);
1012 task_run_change_variable_action (game, var1, var2, var3, expr, var5);
1013 break;
1014
1015 case 4: /* Change score. */
1016 vt_key[4].string = "Var1";
1017 var1 = prop_get_integer (bundle, "I<-sisis", vt_key);
1018 task_run_change_score_action (game, task, var1);
1019 break;
1020
1021 case 5: /* Execute/unset task. */
1022 vt_key[4].string = "Var1";
1023 var1 = prop_get_integer (bundle, "I<-sisis", vt_key);
1024 vt_key[4].string = "Var2";
1025 var2 = prop_get_integer (bundle, "I<-sisis", vt_key);
1026 status = task_run_set_task_action (game, var1, var2);
1027 break;
1028
1029 case 6: /* End game. */
1030 vt_key[4].string = "Var1";
1031 var1 = prop_get_integer (bundle, "I<-sisis", vt_key);
1032 status = task_run_end_game_action (game, var1);
1033 break;
1034
1035 case 7: /* Battle options, ignored for now... */
1036 break;
1037
1038 default:
1039 sc_fatal ("task_run_task_action: unknown action type %ld\n", type);
1040 }
1041
1042 return status;
1043 }
1044
1045
1046 /*
1047 * task_run_task_actions()
1048 *
1049 * Run every task action associated with the task. If any action ends the
1050 * game, return immediately. Returns TRUE if any action ran and itself
1051 * returned TRUE.
1052 */
1053 static sc_bool
1054 task_run_task_actions (sc_gameref_t game, sc_int task)
1055 {
1056 const sc_filterref_t filter = gs_get_filter (game);
1057 const sc_prop_setref_t bundle = gs_get_bundle (game);
1058 sc_vartype_t vt_key[3];
1059 sc_int action_count, action;
1060 sc_bool status, muted;
1061
1062 /* Get the count of task actions. */
1063 vt_key[0].string = "Tasks";
1064 vt_key[1].integer = task;
1065 vt_key[2].string = "Actions";
1066 action_count = prop_get_child_count (bundle, "I<-sis", vt_key);
1067
1068 if (action_count > 0)
1069 {
1070 if (task_trace)
1071 {
1072 sc_trace ("Task: task %ld running %ld action%s\n",
1073 task, action_count, action_count == 1 ? "" : "s");
1074 }
1075 }
1076
1077 /*
1078 * Run all task actions, capturing any TRUE status returned. If any task
1079 * ends the game, run the remaining tasks silently.
1080 *
1081 * This seems a little counterintuitive; a more conventional thing would be
1082 * to just exit the actions loop early. However, Adrift appears to plough
1083 * on, and there may be an action that changes the score in here somewhere,
1084 * so we'll do the same.
1085 */
1086 status = FALSE;
1087 muted = FALSE;
1088 for (action = 0; action < action_count; action++)
1089 {
1090 sc_bool was_running;
1091
1092 was_running = game->is_running;
1093 status |= task_run_task_action (game, task, action);
1094
1095 /* Did this action end the game? */
1096 if (was_running && !game->is_running)
1097 {
1098 if (task_trace)
1099 {
1100 sc_trace ("Task: task %ld action %ld ended game\n",
1101 task, action);
1102 }
1103
1104 /* Mute the filter, and note that we did it, but continue. */
1105 pf_mute (filter);
1106 muted = TRUE;
1107 }
1108 }
1109
1110 /* If this stack frame muted the filter, un-mute it now. */
1111 if (muted)
1112 pf_clear_mute (filter);
1113
1114 /* Return TRUE if any task action returned TRUE. */
1115 return status;
1116 }
1117
1118
1119 /*
1120 * task_start_npc_walks()
1121 *
1122 * Start NPC walks based on alerts.
1123 */
1124 static void
1125 task_start_npc_walks (sc_gameref_t game, sc_int task)
1126 {
1127 const sc_prop_setref_t bundle = gs_get_bundle (game);
1128 sc_vartype_t vt_key[4];
1129 sc_int alert_count, alert;
1130
1131 /* Get a count of NPC walk alerts. */
1132 vt_key[0].string = "Tasks";
1133 vt_key[1].integer = task;
1134 vt_key[2].string = "NPCWalkAlert";
1135 alert_count = prop_get_child_count (bundle, "I<-sis", vt_key);
1136
1137 /* Check alerts, and start any walks that need starting. */
1138 for (alert = 0; alert < alert_count; alert += 2)
1139 {
1140 sc_int npc, walk;
1141
1142 vt_key[3].integer = alert;
1143 npc = prop_get_integer (bundle, "I<-sisi", vt_key);
1144 vt_key[3].integer = alert + 1;
1145 walk = prop_get_integer (bundle, "I<-sisi", vt_key);
1146 npc_start_npc_walk (game, npc, walk);
1147 }
1148 }
1149
1150
1151 /*
1152 * task_run_task_unrestricted()
1153 *
1154 * Run a task, providing restrictions permit, in the given direction. Return
1155 * TRUE if the task ran, or we handled it in some complete way, for example by
1156 * outputting a message describing what prevented it, or why it couldn't be
1157 * done.
1158 */
1159 static sc_bool
1160 task_run_task_unrestricted (sc_gameref_t game, sc_int task, sc_bool forwards)
1161 {
1162 const sc_filterref_t filter = gs_get_filter (game);
1163 const sc_prop_setref_t bundle = gs_get_bundle (game);
1164 sc_vartype_t vt_key[3];
1165 const sc_char *completetext, *additionalmessage;
1166 sc_int action_count, showroomdesc;
1167 sc_bool status;
1168
1169 /* Start considering task output tracking. */
1170 status = FALSE;
1171
1172 /*
1173 * If reversing, print any reverse message for the task, and undo the task,
1174 * then return.
1175 */
1176 if (!forwards)
1177 {
1178 const sc_char *reversemessage;
1179
1180 /* If not yet done, we can hardly reverse it. */
1181 if (gs_task_done (game, task))
1182 {
1183 vt_key[0].string = "Tasks";
1184 vt_key[1].integer = task;
1185 vt_key[2].string = "ReverseMessage";
1186 reversemessage = prop_get_string (bundle, "S<-sis", vt_key);
1187 if (!sc_strempty (reversemessage))
1188 {
1189 pf_buffer_string (filter, reversemessage);
1190 pf_buffer_character (filter, '\n');
1191 status |= TRUE;
1192 }
1193
1194 /* Undo the task. */
1195 gs_set_task_done (game, task, FALSE);
1196 }
1197
1198 /* Return status of undo. */
1199 return status;
1200 }
1201
1202 /* See if we are trying to repeat a task that's not repeatable. */
1203 if (gs_task_done (game, task))
1204 {
1205 sc_bool repeatable;
1206
1207 vt_key[0].string = "Tasks";
1208 vt_key[1].integer = task;
1209 vt_key[2].string = "Repeatable";
1210 repeatable = prop_get_boolean (bundle, "B<-sis", vt_key);
1211 if (!repeatable)
1212 {
1213 const sc_char *repeattext;
1214
1215 vt_key[2].string = "RepeatText";
1216 repeattext = prop_get_string (bundle, "S<-sis", vt_key);
1217 if (!sc_strempty (repeattext))
1218 {
1219 if (task_trace)
1220 {
1221 sc_trace ("Task:"
1222 " trying to repeat completed action, aborting\n");
1223 }
1224
1225 pf_buffer_string (filter, repeattext);
1226 pf_buffer_character (filter, '\n');
1227 status |= TRUE;
1228 return status;
1229 }
1230
1231 /*
1232 * Task done, yet not repeatable, so don't consider this case
1233 * handled.
1234 */
1235 return status;
1236 }
1237 }
1238
1239 /* Mark the task as done. */
1240 gs_set_task_done (game, task, TRUE);
1241
1242 /* Print any task completion text. */
1243 vt_key[0].string = "Tasks";
1244 vt_key[1].integer = task;
1245 vt_key[2].string = "CompleteText";
1246 completetext = prop_get_string (bundle, "S<-sis", vt_key);
1247 if (!sc_strempty (completetext))
1248 {
1249 pf_buffer_string (filter, completetext);
1250 pf_buffer_character (filter, '\n');
1251 status |= TRUE;
1252 }
1253
1254 /* Handle any task completion resource. */
1255 vt_key[2].string = "Res";
1256 res_handle_resource (game, "sis", vt_key);
1257
1258 /*
1259 * Things get slightly tricky here. We need to filter the completion text
1260 * for the task using any final variable values generated or modified by
1261 * task actions, but other task text, run by actions, according to the
1262 * variable value in effect when it runs.
1263 *
1264 * To do this, we take a local copy of the filter's current buffer at this
1265 * point, remove it from the filter, run task actions with checkpointing,
1266 * then prepend it back into the filter after all the actions are done.
1267 *
1268 * As an optimization, we can avoid doing this if there are no task actions.
1269 */
1270 vt_key[2].string = "Actions";
1271 action_count = prop_get_child_count (bundle, "I<-sis", vt_key);
1272 if (action_count > 0)
1273 {
1274 sc_char *buffer;
1275
1276 /*
1277 * Take ownership of the current filter buffer text, then start NPC
1278 * walks based on alerts, and run any and all task actions. Note that
1279 * the buffer transferred out of the filter may be NULL if there is no
1280 * text currently in the filter.
1281 */
1282 buffer = pf_transfer_buffer (filter);
1283 task_start_npc_walks (game, task);
1284 status |= task_run_task_actions (game, task);
1285
1286 /* Prepend the saved buffer data back onto the front of the filter. */
1287 if (buffer)
1288 {
1289 pf_prepend_string (filter, buffer);
1290 sc_free (buffer);
1291 }
1292 }
1293 else
1294 {
1295 /* Start NPC walks only; there are no task actions. */
1296 task_start_npc_walks (game, task);
1297 }
1298
1299 /* Append any room description and additional message for the task. */
1300 vt_key[2].string = "ShowRoomDesc";
1301 showroomdesc = prop_get_integer (bundle, "I<-sis", vt_key);
1302 if (showroomdesc != 0)
1303 {
1304 lib_print_room_name (game, showroomdesc - 1);
1305 lib_print_room_description (game, showroomdesc - 1);
1306 status |= TRUE;
1307 }
1308
1309 vt_key[2].string = "AdditionalMessage";
1310 additionalmessage = prop_get_string (bundle, "S<-sis", vt_key);
1311 if (!sc_strempty (additionalmessage))
1312 {
1313 pf_buffer_string (filter, additionalmessage);
1314 pf_buffer_character (filter, '\n');
1315 status |= TRUE;
1316 }
1317
1318 /* Return status -- TRUE if matched and we output something. */
1319 return status;
1320 }
1321
1322
1323 /*
1324 * task_run_task()
1325 *
1326 * Run a task, providing restrictions permit, in the given direction. At the
1327 * same time, check for signs of an infinite loop in game tasks, and fail the
1328 * task with an error message if we seem to be in one. Checked by counting
1329 * the call depth.
1330 */
1331 sc_bool
1332 task_run_task (sc_gameref_t game, sc_int task, sc_bool forwards)
1333 {
1334 static sc_int recursion_depth = 0;
1335
1336 const sc_filterref_t filter = gs_get_filter (game);
1337 const sc_char *fail_message;
1338 sc_bool restrictions_passed, status;
1339
1340 if (task_trace)
1341 {
1342 sc_trace ("Task: running task %ld %s, depth %ld\n",
1343 task, forwards ? "forwards" : "backwards", recursion_depth);
1344 }
1345
1346 /* Check restrictions. */
1347 if (!restr_eval_task_restrictions (game, task,
1348 &restrictions_passed, &fail_message))
1349 {
1350 sc_error ("task_run_task: restrictions error, %ld\n", task);
1351 return FALSE;
1352 }
1353 if (!restrictions_passed)
1354 {
1355 if (task_trace)
1356 {
1357 sc_trace ("Task: restrictions failed, task %s\n",
1358 fail_message ? "failed" : "aborted");
1359 }
1360
1361 if (fail_message)
1362 {
1363 /*
1364 * Print a message, and return TRUE since we can consider this task
1365 * "done" (more accurately, we've output text, so the task command
1366 * searching in the main run loop can exit...).
1367 */
1368 pf_buffer_string (filter, fail_message);
1369 pf_buffer_character (filter, '\n');
1370 return TRUE;
1371 }
1372
1373 /* Task not done; look for more possibilities. */
1374 return FALSE;
1375 }
1376
1377 /* Check for infinite recursion. */
1378 if (recursion_depth > TASK_MAXIMUM_RECURSION)
1379 {
1380 sc_error ("task_run_task: maximum recursion depth exceeded --"
1381 " game task loop?\n");
1382 return FALSE;
1383 }
1384
1385 /* Increment depth, run the task, then decrement depth. */
1386 recursion_depth++;
1387 status = task_run_task_unrestricted (game, task, forwards);
1388 recursion_depth--;
1389
1390 if (task_trace)
1391 {
1392 sc_trace ("Task: task %ld finished, return %s, depth %ld\n",
1393 task, status ? "true" : "false", recursion_depth);
1394 }
1395
1396 /* Return the task's status. */
1397 return status;
1398 }
1399
1400
1401 /*
1402 * task_debug_trace()
1403 *
1404 * Set task tracing on/off.
1405 */
1406 void
1407 task_debug_trace (sc_bool flag)
1408 {
1409 task_trace = flag;
1410 }