Debian Codegraph @

terps/scare/scrunner.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 <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "scare.h"
33 #include "scprotos.h"
34 #include "scgamest.h"
35
36
37 /* Assorted definitions and constants. */
38 enum { LINE_BUFFER_SIZE = 256 };
39 static const sc_char NUL = '\0';
40 static const sc_char SPECIAL_PATTERN = '#';
41 static const sc_char WILDCARD_PATTERN = '*';
42 static const sc_char *const WHITESPACE = "\t\n\v\f\r ";
43 static const sc_char *const SEPARATORS = ".,";
44
45
46 /*
47 * run_is_task_function()
48 *
49 * Check for the presence of a command function in the first task command,
50 * and action it if found. This is a 4.0.42 compatibility hack -- at
51 * present, only getdynfromroom() exists. Returns TRUE if function found
52 * and handled.
53 */
54 static sc_bool
55 run_is_task_function (const sc_char *pattern, sc_gameref_t game)
56 {
57 const sc_prop_setref_t bundle = gs_get_bundle (game);
58 const sc_var_setref_t vars = gs_get_vars (game);
59 sc_vartype_t vt_key[3];
60 sc_int room, object;
61 sc_char *argument;
62
63 /* Simple comparison against the one known task expression. */
64 argument = sc_malloc (strlen (pattern) + 1);
65 if (sscanf (pattern, " # %%object%% = getdynfromroom (%[^)])", argument) == 0)
66 {
67 sc_free (argument);
68 return FALSE;
69 }
70
71 /*
72 * Compare the argument read in against known room names.
73 *
74 * TODO Is this simple room name comparison good enough?
75 */
76 vt_key[0].string = "Rooms";
77 for (room = 0; room < gs_room_count (game); room++)
78 {
79 const sc_char *name;
80
81 vt_key[1].integer = room;
82 vt_key[2].string = "Short";
83 name = prop_get_string (bundle, "S<-sis", vt_key);
84 if (sc_strcasecmp (name, argument) == 0)
85 break;
86 }
87 sc_free (argument);
88 if (room == gs_room_count (game))
89 return FALSE;
90
91 /*
92 * Select a dynamic object from the room.
93 *
94 * TODO What are the selection criteria supposed to be? Here we use "on
95 * the floor".
96 */
97 vt_key[0].string = "Objects";
98 for (object = 0; object < gs_object_count (game); object++)
99 {
100 sc_bool bstatic;
101
102 vt_key[1].integer = object;
103 vt_key[2].string = "Static";
104 bstatic = prop_get_boolean (bundle, "B<-sis", vt_key);
105 if (!bstatic && obj_directly_in_room (game, object, room))
106 break;
107 }
108 if (object == gs_object_count (game))
109 return FALSE;
110
111 /* Set this object reference, unambiguously, as if %object% match. */
112 gs_clear_object_references (game);
113 game->object_references[object] = TRUE;
114 var_set_ref_object (vars, object);
115
116 return TRUE;
117 }
118
119
120 /* Structure used to associate a pattern with a handler function. */
121 typedef struct sc_commands_s
122 {
123 const sc_char *const command;
124 sc_bool (*const handler) (sc_gameref_t game);
125 } sc_commands_t;
126 typedef sc_commands_t *sc_commandsref_t;
127
128 /* Movement commands for the four point compass. */
129 static sc_commands_t MOVE_COMMANDS_4[] = {
130 {"{go {to {the}}} [north/n]", lib_cmd_go_north},
131 {"{go {to {the}}} [east/e]", lib_cmd_go_east},
132 {"{go {to {the}}} [south/s]", lib_cmd_go_south},
133 {"{go {to {the}}} [west/w]", lib_cmd_go_west},
134 {"{go {to {the}}} [up/u]", lib_cmd_go_up},
135 {"{go {to {the}}} [down/d]", lib_cmd_go_down},
136 {"{go {to {the}}} [in]", lib_cmd_go_in},
137 {"{go {to {the}}} [out/o]", lib_cmd_go_out},
138 {NULL, NULL}
139 };
140
141 /* Movement commands for the eight point compass. */
142 static sc_commands_t MOVE_COMMANDS_8[] = {
143 {"{go {to {the}}} [north/n]", lib_cmd_go_north},
144 {"{go {to {the}}} [east/e]", lib_cmd_go_east},
145 {"{go {to {the}}} [south/s]", lib_cmd_go_south},
146 {"{go {to {the}}} [west/w]", lib_cmd_go_west},
147 {"{go {to {the}}} [up/u]", lib_cmd_go_up},
148 {"{go {to {the}}} [down/d]", lib_cmd_go_down},
149 {"{go {to {the}}} [in]", lib_cmd_go_in},
150 {"{go {to {the}}} [out/o]", lib_cmd_go_out},
151 {"{go {to {the}}} [northeast/north-east/ne]", lib_cmd_go_northeast},
152 {"{go {to {the}}} [southeast/south-east/se]", lib_cmd_go_southeast},
153 {"{go {to {the}}} [northwest/north-west/nw]", lib_cmd_go_northwest},
154 {"{go {to {the}}} [southwest/south-west/sw]", lib_cmd_go_southwest},
155 {NULL, NULL}
156 };
157
158 /* "Priority" library commands, may take precedence over the game. */
159 static sc_commands_t PRIORITY_COMMANDS[] = {
160
161 /* Acquisition of and disposal of inventory. */
162 {"[[get/take/remove/extract] [all/everything] from/empty] %object%",
163 lib_cmd_take_all_from},
164 {"[[get/take/remove/extract] [all/everything] from/empty] %object%"
165 " [[except/but] {for}/apart from] %text%",
166 lib_cmd_take_from_except_multiple},
167 {"[get/take/remove/extract] [all/everything]"
168 " [[except/but] {for}/apart from] %text% from %object%",
169 lib_cmd_take_from_except_multiple},
170 {"[get/take/remove/extract] %text% from %object%",
171 lib_cmd_take_from_multiple},
172 {"[get/take] [all/everything] from %character%", lib_cmd_take_all_from_npc},
173 {"[get/take] [all/everything] from %character%"
174 " [[except/but] {for}/apart from] %text%",
175 lib_cmd_take_from_npc_except_multiple},
176 {"[get/take] [all/everything]"
177 " [[except/but] {for}/apart from] %text% from %character%",
178 lib_cmd_take_from_npc_except_multiple},
179 {"[get/take] %text% from %character%", lib_cmd_take_from_npc_multiple},
180 {"[[get/take/pick up] [all/everything]/pick [all/everything] up]",
181 lib_cmd_take_all},
182 {"[get/take/pick up] [all/everything] [[except/but] {for}/apart from] %text%",
183 lib_cmd_take_except_multiple},
184 {"[get/take/pick up] %text%", lib_cmd_take_multiple},
185 {"pick %text% up", lib_cmd_take_multiple},
186 {"[[drop/put down] [all/everything]/put [all/everything] down]",
187 lib_cmd_drop_all},
188 {"[drop/put down] [all/everything] [[except/but] {for}/apart from] %text%",
189 lib_cmd_drop_except_multiple},
190 {"[drop/put down] %text%", lib_cmd_drop_multiple},
191 {"put %text% down", lib_cmd_drop_multiple},
192 {NULL, NULL}
193 };
194
195 /* Standard library commands, other than movement and priority above. */
196 static sc_commands_t STANDARD_COMMANDS[] = {
197
198 /* Inventory, and general investigation of surroundings. */
199 #ifdef SCARE_NO_ABBREVIATIONS
200 {"[inventory/inv]", lib_cmd_inventory},
201 {"[ex/exam/examine/look {at}] {{the} [room/location]}", lib_cmd_look},
202 {"[ex/exam/examine/look {at/in}] %object%", lib_cmd_examine_object},
203 {"[ex/exam/examine/look {at}] %character%", lib_cmd_examine_npc},
204 {"[ex/exam/examine/look {at}] [me/self/myself]", lib_cmd_examine_self},
205 {"[ex/exam/examine/look {at}] all", lib_cmd_examine_all},
206 #else
207 {"[inventory/inv/i]", lib_cmd_inventory},
208 {"[x/ex/exam/examine/l/look {at}] {{the} [room/location]}", lib_cmd_look},
209 {"[x/ex/exam/examine/look {at/in}] %object%", lib_cmd_examine_object},
210 {"[x/ex/exam/examine/look {at}] %character%", lib_cmd_examine_npc},
211 {"[x/ex/exam/examine/look {at}] [me/self/myself]", lib_cmd_examine_self},
212 {"[x/ex/exam/examine/look {at}] all", lib_cmd_examine_all},
213 #endif
214
215 /* Attempted acquisition of and disposal of NPCs. */
216 {"[get/take/pick up] %character%", lib_cmd_take_npc},
217 {"pick %character% up", lib_cmd_take_npc},
218
219 /* Manipulating selected objects. */
220 {"put [all/everything] [in/into/inside {of}] %object%", lib_cmd_put_all_in},
221 {"put [all/everything] [[except/but] {for}/apart from] %text%"
222 " [in/into/inside {of}] %object%", lib_cmd_put_in_except_multiple},
223 {"put %text% [in/into/inside {of}] %object%", lib_cmd_put_in_multiple},
224 {"put [all/everything] [on/onto/on top of] %object%", lib_cmd_put_all_on},
225 {"put [all/everything] [[except/but] {for}/apart from] %text%"
226 " [on/onto/on top of] %object%", lib_cmd_put_on_except_multiple},
227 {"put %text% [on/onto/on top of] %object%", lib_cmd_put_on_multiple},
228 {"open %object%", lib_cmd_open_object},
229 {"close %object%", lib_cmd_close_object},
230 {"unlock %object% with %text%", lib_cmd_unlock_object_with},
231 {"lock %object% with %text%", lib_cmd_lock_object_with},
232 {"unlock %object%", lib_cmd_unlock_object},
233 {"lock %object%", lib_cmd_lock_object},
234 {"read %object%", lib_cmd_read_object},
235 {"read *", lib_cmd_read_other},
236 {"give %object% to %character%", lib_cmd_give_object_npc},
237 {"sit {down/up} [on/in] %object%", lib_cmd_sit_on_object},
238 {"stand {up/down} [on/in] %object%", lib_cmd_stand_on_object},
239 {"[lie/lay] on %object%", lib_cmd_lie_on_object},
240 {"get {down/up} off %object%", lib_cmd_get_off_object},
241 {"get off", lib_cmd_get_off},
242 {"sit {down/up} {[on/in] {the} [ground/floor]}", lib_cmd_sit_on_floor},
243 {"stand {up/down} {[on/in] {the} [ground/floor]}", lib_cmd_stand_on_floor},
244 {"[lie/lay] {down/up} {[on/in] {the} [ground/floor]}", lib_cmd_lie_on_floor},
245 {"eat %object%", lib_cmd_eat_object},
246
247 /* Dressing up, and dressing down. */
248 {"[[wear/put on/don] [all/everything]/put [all/everything] on]",
249 lib_cmd_wear_all},
250 {"[wear/put on/don] [all/everything] [[except/but] {for}/apart from] %text%",
251 lib_cmd_wear_except_multiple},
252 {"[wear/put on/don] %text%", lib_cmd_wear_multiple},
253 {"put %text% on", lib_cmd_wear_multiple},
254 {"[[remove/take off/doff] [all/everything]/take [all/everything] off/strip]",
255 lib_cmd_remove_all},
256 {"[remove/take off/doff] [all/everything]"
257 " [[except/but] {for}/apart from] %text%",
258 lib_cmd_remove_except_multiple},
259 {"[remove/take off/doff] %text%", lib_cmd_remove_multiple},
260 {"take %text% off", lib_cmd_remove_multiple},
261
262 /* Selected NPC interactions and conversation. */
263 {"ask %character% about %text%", lib_cmd_ask_npc_about},
264 {"[attack/hit/kick/slap/shoot/stab] %character% with %object%",
265 lib_cmd_attack_npc_with},
266 {"[attack/shoot] %character%", lib_cmd_attack_npc},
267
268 /* More movement, waiting, and miscellaneous administrative commands. */
269 {"[goto/go {to}] %text%", lib_cmd_go_room},
270 {"[goto/go {to}] *", lib_cmd_print_room_exits},
271 {"[exit/exits/directions/where]", lib_cmd_print_room_exits},
272 #ifdef SCARE_NO_ABBREVIATIONS
273 {"[wait] %number%", lib_cmd_wait_number},
274 {"[wait]", lib_cmd_wait},
275 #else
276 {"[wait/z] %number%", lib_cmd_wait_number},
277 {"[wait/z]", lib_cmd_wait},
278 #endif
279 {"save", lib_cmd_save},
280 {"[restore/load]", lib_cmd_restore},
281 {"restart", lib_cmd_restart},
282 #ifdef SCARE_NO_ABBREVIATIONS
283 {"[again]", lib_cmd_again},
284 #else
285 {"[again/g]", lib_cmd_again},
286 #endif
287 {"[redo /!]%number%", lib_cmd_redo_number},
288 {"[redo /!]%text%", lib_cmd_redo_text},
289 {"[redo/!]", lib_cmd_redo_last},
290 #ifdef SCARE_NO_ABBREVIATIONS
291 {"[quit]", lib_cmd_quit},
292 #else
293 {"[quit/q]", lib_cmd_quit},
294 #endif
295 {"turns", lib_cmd_turns},
296 {"score", lib_cmd_score},
297 {"undo", lib_cmd_undo},
298 {"[hist/history] %number%", lib_cmd_history_number},
299 {"[hist/history]", lib_cmd_history},
300 {"[hint/hints]", lib_cmd_hints},
301 {"verbose", lib_cmd_verbose},
302 {"brief", lib_cmd_brief},
303 {"[notify/notification] %text%", lib_cmd_notify_on_off},
304 {"[notify/notification]", lib_cmd_notify},
305 {"time", lib_cmd_time},
306 {"date", lib_cmd_date},
307 {"[help/commands]", lib_cmd_help},
308 {"[gpl/license]", lib_cmd_license},
309 {"[about/info/information/author]", lib_cmd_information},
310 {"[clear/cls/clr]", lib_cmd_clear},
311 {"status{line}", lib_cmd_statusline},
312 {"version", lib_cmd_version},
313
314 {"[locate/where {is/are}/find] %object%", lib_cmd_locate_object},
315 {"[locate/where {is}/find] %character%", lib_cmd_locate_npc},
316
317 {"[count/num]", lib_cmd_count},
318
319 /* Standard response commands; no real action, just output. */
320 {"[get/take/pick up] *", lib_cmd_get_what},
321 {"open *", lib_cmd_open_what},
322 {"close *", lib_cmd_close_other},
323 {"give %object% *", lib_cmd_give_object},
324 {"give *", lib_cmd_give_what},
325 {"lock %text%", lib_cmd_lock_other},
326 {"lock", lib_cmd_lock_what},
327 {"unlock %text%", lib_cmd_unlock_other},
328 {"unlock", lib_cmd_unlock_what},
329 {"sit {down/up} [on/in] *", lib_cmd_sit_other},
330 {"stand {up/down} [on/in] *", lib_cmd_stand_other},
331 {"[lie/lay] {down/up} [on/in] *", lib_cmd_lie_other},
332 {"[remove/take off/doff] *", lib_cmd_remove_what},
333 {"[drop/put down] *", lib_cmd_drop_what},
334 {"[wear/put on/don] *", lib_cmd_wear_what},
335 {"[shit/fuck/bastard/cunt/crap/hell/shag/bollocks/bollox/bugger] *",
336 lib_cmd_profanity},
337 {"[x/examine/look {at}] *", lib_cmd_examine_other},
338 {"[locate/where {is/are}/find] *", lib_cmd_locate_other},
339 {"[cp/mv/ln/ls] *", lib_cmd_unix_like},
340 {"dir *", lib_cmd_dos_like},
341 {"ask %character% *", lib_cmd_ask_npc},
342 {"ask %object% *", lib_cmd_ask_object},
343 {"ask *", lib_cmd_ask_other},
344 {"block %object% *", lib_cmd_block_object},
345 {"block %text%", lib_cmd_block_other},
346 {"block", lib_cmd_block_what},
347 {"[break/destroy/smash] %object% *", lib_cmd_break_object},
348 {"[break/destroy/smash] %text%", lib_cmd_break_other},
349 {"break", lib_cmd_break_what},
350 {"destroy", lib_cmd_destroy_what},
351 {"smash", lib_cmd_smash_what},
352 {"buy %object% *", lib_cmd_buy_object},
353 {"buy %text%", lib_cmd_buy_other},
354 {"buy", lib_cmd_buy_what},
355 {"clean %object% *", lib_cmd_clean_object},
356 {"clean %text%", lib_cmd_clean_other},
357 {"clean", lib_cmd_clean_what},
358 {"climb %object% *", lib_cmd_climb_object},
359 {"climb %text%", lib_cmd_climb_other},
360 {"climb", lib_cmd_climb_what},
361 {"cry *", lib_cmd_cry},
362 {"cut %object% *", lib_cmd_cut_object},
363 {"cut %text%", lib_cmd_cut_other},
364 {"cut", lib_cmd_cut_what},
365 {"dance *", lib_cmd_dance},
366 {"drink %object% *", lib_cmd_drink_object},
367 {"drink %text%", lib_cmd_drink_other},
368 {"drink", lib_cmd_drink_what},
369 {"eat *", lib_cmd_eat_other},
370 {"feed *", lib_cmd_feed},
371 {"feel *", lib_cmd_feel},
372 {"fight *", lib_cmd_fight},
373 {"fix %object% *", lib_cmd_fix_object},
374 {"fix %text%", lib_cmd_fix_other},
375 {"fix", lib_cmd_fix_what},
376 {"fly *", lib_cmd_fly},
377 {"hint *", lib_cmd_hint},
378 {"hit %character%", lib_cmd_attack_npc},
379 {"hit %object% *", lib_cmd_hit_object},
380 {"hit %text%", lib_cmd_hit_other},
381 {"hit", lib_cmd_hit_what},
382 {"hum *", lib_cmd_hum},
383 {"jump *", lib_cmd_jump},
384 {"kick %character%", lib_cmd_attack_npc},
385 {"kick %object% *", lib_cmd_kick_object},
386 {"kick %text%", lib_cmd_kick_other},
387 {"kick", lib_cmd_kick_what},
388 {"kiss %character% *", lib_cmd_kiss_npc},
389 {"kiss %object% *", lib_cmd_kiss_object},
390 {"kiss *", lib_cmd_kiss_other},
391 {"kill *", lib_cmd_kill_other},
392 {"lift %object% *", lib_cmd_lift_object},
393 {"lift %text%", lib_cmd_lift_other},
394 {"lift", lib_cmd_lift_what},
395 {"light %object% *", lib_cmd_light_object},
396 {"light %text%", lib_cmd_light_other},
397 {"light", lib_cmd_light_what},
398 {"listen *", lib_cmd_listen},
399 {"mend %object% *", lib_cmd_mend_object},
400 {"mend %text%", lib_cmd_mend_other},
401 {"mend", lib_cmd_mend_what},
402 {"move %object% *", lib_cmd_move_object},
403 {"move %text%", lib_cmd_move_other},
404 {"move", lib_cmd_move_what},
405 {"please *", lib_cmd_please},
406 {"press %object% *", lib_cmd_press_object},
407 {"press %text%", lib_cmd_press_other},
408 {"press", lib_cmd_press_what},
409 {"pull %object% *", lib_cmd_pull_object},
410 {"pull %text%", lib_cmd_pull_other},
411 {"pull", lib_cmd_pull_what},
412 {"punch *", lib_cmd_punch},
413 {"push %object% *", lib_cmd_push_object},
414 {"push %text%", lib_cmd_push_other},
415 {"push", lib_cmd_push_what},
416 {"repair %object% *", lib_cmd_repair_object},
417 {"repair %text%", lib_cmd_repair_other},
418 {"repair", lib_cmd_repair_what},
419 {"rub %object% *", lib_cmd_rub_object},
420 {"rub %text%", lib_cmd_rub_other},
421 {"rub", lib_cmd_rub_what},
422 {"run *", lib_cmd_run},
423 {"say *", lib_cmd_say},
424 {"sell %object% *", lib_cmd_sell_object},
425 {"sell %text%", lib_cmd_sell_other},
426 {"sell", lib_cmd_sell_what},
427 {"shake %object% *", lib_cmd_shake_object},
428 {"shake %text%", lib_cmd_shake_other},
429 {"shake", lib_cmd_shake_what},
430 {"shout *", lib_cmd_shout},
431 {"sing *", lib_cmd_sing},
432 {"sleep *", lib_cmd_sleep},
433 {"smell %object% *", lib_cmd_smell_object},
434 {"smell *", lib_cmd_smell_other},
435 {"stop %object% *", lib_cmd_stop_object},
436 {"stop %text%", lib_cmd_stop_other},
437 {"stop", lib_cmd_stop_what},
438 {"suck %object% *", lib_cmd_suck_object},
439 {"suck %text%", lib_cmd_suck_other},
440 {"suck", lib_cmd_suck_what},
441 {"talk *", lib_cmd_talk},
442 {"thank *", lib_cmd_thank},
443 {"turn %object% *", lib_cmd_turn_object},
444 {"turn %text%", lib_cmd_turn_other},
445 {"turn", lib_cmd_turn_what},
446 {"touch %object% *", lib_cmd_touch_object},
447 {"touch %text%", lib_cmd_touch_other},
448 {"touch", lib_cmd_touch_what},
449 {"unblock %object% *", lib_cmd_unblock_object},
450 {"unblock %text%", lib_cmd_unblock_other},
451 {"unblock", lib_cmd_unblock_what},
452 {"wash %object% *", lib_cmd_wash_object},
453 {"wash %text%", lib_cmd_wash_other},
454 {"wash", lib_cmd_wash_what},
455 {"whistle *", lib_cmd_whistle},
456 {"[why/when/what/can/how] *", lib_cmd_interrogation},
457 {"xyzzy *", lib_cmd_xyzzy},
458 {"campbell", lib_cmd_egotistic},
459 {"[yes/no] *", lib_cmd_yes_or_no},
460 {"* %object% *", lib_cmd_verb_object},
461 {"* %character% *", lib_cmd_verb_npc},
462
463 /* SCARE debugger hook command, placed last just in case... */
464 {"{#}debug{ger}", debug_cmd_debugger},
465
466 {NULL, NULL}
467 };
468
469
470 /*
471 * run_priority_commands()
472 * run_standard_commands()
473 *
474 * Compare a user input string against commands recognized by the library,
475 * and action any command. Returns TRUE if the string matched a command
476 * that then ran successfully, FALSE otherwise.
477 *
478 * "Priority" commands are ones that Adrift seems to action no matter what
479 * the game tries to override. For example, a simple game with one "ball"
480 * object and a task "* ball *" should, if the task is restricted, override
481 * "take ball" such that the ball can never be acquired. Adrift lets the
482 * "take" succeed, though (and more curiously, may respond "I don't
483 * understand..." to "drop ball"). This could be an Adrift bug. Shrug.
484 *
485 * For now, I can't find any better way to try to handle it than to make
486 * object acquisition take precedence over game commands.
487 */
488 static sc_bool
489 run_priority_commands (sc_gameref_t game, const sc_char *string)
490 {
491 sc_commandsref_t command;
492
493 for (command = PRIORITY_COMMANDS; command->command; command++)
494 {
495 if (uip_match (command->command, string, game))
496 {
497 if (command->handler (game))
498 return TRUE;
499 }
500 }
501
502 /* Nothing matched match the string. Or if it did, its handler failed. */
503 return FALSE;
504 }
505
506 static sc_bool
507 run_standard_commands (sc_gameref_t game, const sc_char *string)
508 {
509 const sc_prop_setref_t bundle = gs_get_bundle (game);
510 sc_vartype_t vt_key[2];
511 sc_bool eightpointcompass;
512 sc_commandsref_t command;
513
514 /* Select the appropriate movement commands. */
515 vt_key[0].string = "Globals";
516 vt_key[1].string = "EightPointCompass";
517 eightpointcompass = prop_get_boolean (bundle, "B<-ss", vt_key);
518 command = eightpointcompass ? MOVE_COMMANDS_8 : MOVE_COMMANDS_4;
519
520 /*
521 * Search movement commands first, returning TRUE if any matching command
522 * handler succeeded. Then repeat for standard library commands.
523 */
524 for (; command->command; command++)
525 {
526 if (uip_match (command->command, string, game))
527 {
528 if (command->handler (game))
529 return TRUE;
530 }
531 }
532
533 for (command = STANDARD_COMMANDS; command->command; command++)
534 {
535 if (uip_match (command->command, string, game))
536 {
537 if (command->handler (game))
538 return TRUE;
539 }
540 }
541
542 /* Nothing matched match the string. Or if it did, its handler failed. */
543 return FALSE;
544 }
545
546
547 /*
548 * run_update_status()
549 *
550 * Update the game's current room and status line strings.
551 */
552 static void
553 run_update_status (sc_gameref_t game)
554 {
555 const sc_prop_setref_t bundle = gs_get_bundle (game);
556 const sc_var_setref_t vars = gs_get_vars (game);
557 sc_vartype_t vt_key[2];
558 const sc_char *name, *status;
559 sc_char *filtered;
560 sc_bool statusbox;
561
562 /* Get the current room name, and filter and untag it. */
563 name = lib_get_room_name (game, gs_playerroom (game));
564 filtered = pf_filter (name, vars, bundle);
565 pf_strip_tags (filtered);
566
567 /* Free any existing room name, then save this room name. */
568 sc_free (game->current_room_name);
569 game->current_room_name = filtered;
570
571 /* See if the game does a status box. */
572 vt_key[0].string = "Globals";
573 vt_key[1].string = "StatusBox";
574 statusbox = prop_get_boolean (bundle, "B<-ss", vt_key);
575 if (statusbox)
576 {
577 /* Get the status line, and filter and untag it. */
578 vt_key[1].string = "StatusBoxText";
579 status = prop_get_string (bundle, "S<-ss", vt_key);
580 filtered = pf_filter (status, vars, bundle);
581 pf_strip_tags (filtered);
582 }
583 else
584 /* No status line, so use NULL. */
585 filtered = NULL;
586
587 /* Free any existing status line, then save this status text. */
588 sc_free (game->status_line);
589 game->status_line = filtered;
590 }
591
592
593 /*
594 * run_notify_score_change()
595 *
596 * Print an indication of any score change, if appropriate. The change is
597 * detected by comparing against the undo game. Uses if_print_string()
598 * directly for printing, rather than the filter, so that it can place its
599 * output ahead of buffered printfilter text.
600 */
601 static void
602 run_notify_score_change (sc_gameref_t game)
603 {
604 const sc_gameref_t undo = game->undo;
605 sc_char buffer[32];
606 assert (gs_is_game_valid (undo));
607
608 /*
609 * Do nothing if no undo available, or if notification is off, or if we've
610 * already done this once this turn.
611 */
612 if (!game->undo_available
613 || !game->notify_score_change || game->has_notified)
614 return;
615
616 /* Note any change in the score. */
617 if (game->score > undo->score)
618 {
619 if_print_string ("(Your score has increased by ");
620 sprintf (buffer, "%ld", game->score - undo->score);
621 if_print_string (buffer);
622 if_print_string (")\n");
623 }
624 else if (game->score < undo->score)
625 {
626 if_print_string ("(Your score has decreased by ");
627 sprintf (buffer, "%ld", undo->score - game->score);
628 if_print_string (buffer);
629 if_print_string (")\n");
630 }
631 game->has_notified = TRUE;
632 }
633
634
635 /*
636 * run_match_task_common()
637 * run_match_task_commands()
638 * run_match_task_functions()
639 *
640 * Helpers for run_game_commands_common().
641 *
642 * Search task command for a match to the string passed in, returning TRUE
643 * if a task command matches, FALSE otherwise. Ordinary or reverse commands
644 * are selected by 'forwards'.
645 */
646 static sc_bool
647 run_match_task_common (sc_gameref_t game,
648 sc_int task, const sc_char *string, sc_bool forwards,
649 sc_bool is_library, sc_bool is_normal)
650 {
651 const sc_prop_setref_t bundle = gs_get_bundle (game);
652 sc_vartype_t vt_key[4];
653 sc_int command_count, command;
654 sc_bool is_matched;
655
656 /* Get the count of task commands. */
657 vt_key[0].string = "Tasks";
658 vt_key[1].integer = task;
659 vt_key[2].string = forwards ? "Command" : "ReverseCommand";
660 command_count = prop_get_child_count (bundle, "I<-sis", vt_key);
661
662 /* Iterate over commands, looking for patterns that match string. */
663 is_matched = FALSE;
664 for (command = 0; command < command_count; command++)
665 {
666 const sc_char *pattern;
667 sc_int first;
668
669 /* Retrieve the pattern for this command, find its first character. */
670 vt_key[3].integer = command;
671 pattern = prop_get_string (bundle, "S<-sisi", vt_key);
672 first = strspn (pattern, WHITESPACE);
673
674 /* Match using either the parser, or the special function matcher. */
675 if (is_normal)
676 {
677 if (pattern[first] != SPECIAL_PATTERN)
678 {
679 /*
680 * Make a special case of library calls and commands that begin
681 * with a wildcard; these we ignore for this match attempt.
682 */
683 if (is_library && pattern[first] == WILDCARD_PATTERN)
684 is_matched = FALSE;
685 else
686 is_matched = uip_match (pattern, string, game);
687 }
688 }
689 else
690 {
691 if (pattern[first] == SPECIAL_PATTERN)
692 is_matched = run_is_task_function (pattern, game);
693 }
694
695 /* Stop searching if we find a match. */
696 if (is_matched)
697 break;
698 }
699
700 /* Return TRUE if we found a pattern match. */
701 return is_matched;
702 }
703
704 static sc_bool
705 run_match_task_commands (sc_gameref_t game,
706 sc_int task, const sc_char *string,
707 sc_bool forwards, sc_bool is_library)
708 {
709 /*
710 * Match tasks using the normal pattern matcher, with or without any note
711 * about whether the call is from the library.
712 */
713 return run_match_task_common (game, task, string, forwards, is_library, TRUE);
714 }
715
716 static sc_bool
717 run_match_task_functions (sc_gameref_t game,
718 sc_int task, const sc_char *string, sc_bool forwards)
719 {
720 /* Match tasks against "task command functions". */
721 return run_match_task_common (game, task, string, forwards, FALSE, FALSE);
722 }
723
724
725 /*
726 * run_task_is_unrestricted()
727 * run_task_is_loudly_restricted()
728 *
729 * Helpers for run_game_commands_common().
730 *
731 * Adapters for uncovering task restriction state. The first returns TRUE
732 * if the task is unrestricted, and can therefore run unimpeded. The second
733 * returns TRUE iff the task is restricted and has a fail message that
734 * indicates why it fails; such tasks, if run, produce their failure message
735 * and don't change state.
736 */
737 static sc_bool
738 run_task_is_unrestricted (sc_gameref_t game, sc_int task)
739 {
740 sc_bool restrictions_passed;
741 const sc_char *fail_message;
742
743 /*
744 * Evaluate task restrictions, and if they fail to parse for some reason,
745 * return as if restrictions did not pass.
746 */
747 if (!restr_eval_task_restrictions (game, task,
748 &restrictions_passed, &fail_message))
749 {
750 sc_error ("run_task_is_unrestricted: restrictions error, %ld\n", task);
751 return FALSE;
752 }
753
754 /* Return TRUE if the task is unrestricted. */
755 return restrictions_passed;
756 }
757
758 static sc_bool
759 run_task_is_loudly_restricted (sc_gameref_t game, sc_int task)
760 {
761 sc_bool restrictions_passed;
762 const sc_char *fail_message;
763
764 /*
765 * Evaluate task restrictions, and if they fail to parse for some reason,
766 * return as if restrictions did not pass.
767 */
768 if (!restr_eval_task_restrictions (game, task,
769 &restrictions_passed, &fail_message))
770 {
771 sc_error ("run_task_is_loudly_restricted:"
772 " restrictions error, %ld\n", task);
773 return TRUE;
774 }
775
776 /* Return TRUE if the task is restricted and indicates why. */
777 return !restrictions_passed && (fail_message != NULL);
778 }
779
780
781 /*
782 * run_game_commands_common()
783 * run_game_commands_in_parser_context()
784 * run_game_commands_in_library_context()
785 *
786 * The central handler for running, or at least trying to run, game-defined
787 * tasks that have commands that match the input string. Here's the algorithm
788 * as currently understood (and it may not be right, so be warned):
789 *
790 * for each task executable in the current room
791 * for direction in forwards, backwards
792 * for each command string defined by the task for this direction
793 * match against player input
794 * if any command string matched player input
795 * if task restrictions pass
796 * run the task actions in the current direction
797 * if the task actions produced output
798 * return
799 * is_matched := true
800 * break out of all loops
801 *
802 * if not is_matched and we're allowing restrictions to fail tasks
803 * for each task executable in the current room
804 * for direction in forwards, backwards
805 * for each command string defined by the task for this direction
806 * match against player input
807 * if any command string matched player input
808 * if task restrictions fail with an error message
809 * run the task, to persuade it to print this error message
810 * return
811 *
812 * Part of the fun and games is that run_game_task_commands() is called by the
813 * library to try to run "get " and "drop " game commands for standard get/drop
814 * handlers and get_all/drop_all handlers. No pressure, then.
815 */
816 static sc_bool
817 run_game_commands_common (sc_gameref_t game, const sc_char *string,
818 sc_bool include_restrictions, sc_bool is_library)
819 {
820 sc_bool is_matched = FALSE, is_handled = FALSE;
821 sc_bool *is_matching;
822 sc_int task_count, task, direction;
823
824 /*
825 * Matching is expensive, so it helps to use a cache of results from the
826 * first loop in the second. If we're using the second, that is.
827 */
828 task_count = gs_task_count (game);
829 if (include_restrictions)
830 {
831 is_matching = sc_malloc (task_count * sizeof (*is_matching));
832 memset (is_matching, FALSE, task_count * sizeof (*is_matching));
833 }
834 else
835 is_matching = NULL;
836
837 /*
838 * Iterate over every task, ignoring those not runnable. For each runnable
839 * task, try matching task commands, and on matches, check restrictions and
840 * if they pass, try running the task.
841 */
842 for (task = 0; task < task_count; task++)
843 {
844 if (!task_can_run_task (game, task))
845 continue;
846
847 /*
848 * Try matching forwards and reverse commands. If there's a match for
849 * unrestricted tasks, run the task, and if it runs (defined as printing
850 * some game output), we're done; otherwise, note the command match but
851 * keep searching for other possible matches.
852 */
853 for (direction = 0; direction < 2; direction++)
854 {
855 const sc_bool is_forwards = !direction;
856
857 if (task_can_run_task_directional (game, task, is_forwards)
858 && run_match_task_commands (game, task, string,
859 is_forwards, is_library))
860 {
861 if (run_task_is_unrestricted (game, task))
862 {
863 if (task_run_task (game, task, is_forwards))
864 is_handled = TRUE;
865 is_matched = TRUE;
866 break;
867 }
868
869 if (is_matching)
870 is_matching[task] = TRUE;
871 }
872 }
873 if (is_matched)
874 break;
875 }
876
877 /*
878 * If no match, and we've been asked to consider failing restrictions, look
879 * through all of the runnable tasks again, this time searching for
880 * restricted ones with a fail message. Use the cache built above to weed
881 * out matches that are certain to fail.
882 */
883 if (!is_handled && !is_matched && include_restrictions)
884 {
885 for (task = 0; task < task_count; task++)
886 {
887 if (!is_matching[task] || !task_can_run_task (game, task))
888 continue;
889
890 /*
891 * Check matches of forwards and reverse commands. If there's a
892 * match for restricted tasks (ones that have and will print a fail
893 * message if we try to run them), run the task to get the print of
894 * the fail message, and we're done.
895 */
896 for (direction = 0; direction < 2; direction++)
897 {
898 const sc_bool is_forwards = !direction;
899
900 if (task_can_run_task_directional (game, task, is_forwards)
901 && run_match_task_commands (game, task, string,
902 is_forwards, is_library))
903 {
904 if (run_task_is_loudly_restricted (game, task))
905 {
906 if (task_run_task (game, task, is_forwards))
907 {
908 is_handled = TRUE;
909 break;
910 }
911 }
912 }
913 }
914 if (is_handled)
915 break;
916 }
917 }
918
919 /* Return TRUE if any game task handled the command in some way. */
920 sc_free (is_matching);
921 return is_handled;
922 }
923
924 static sc_bool
925 run_game_commands_in_parser_context (sc_gameref_t game, const sc_char *string,
926 sc_bool include_restrictions)
927 {
928 /*
929 * Try game commands, either with or without restrictions, and all full and
930 * complete parse matching (no special case for game commands that begin
931 * with a '*' wildcard).
932 */
933 return run_game_commands_common (game, string, include_restrictions, FALSE);
934 }
935
936 static sc_bool
937 run_game_commands_in_library_context (sc_gameref_t game, const sc_char *string)
938 {
939 /*
940 * Try game commands, including restrictions, and noting that this is a
941 * library call so that the parse matcher can exclude game commands that
942 * begin with a '*' wildcard.
943 */
944 return run_game_commands_common (game, string, TRUE, TRUE);
945 }
946
947
948 /*
949 * run_game_functions()
950 *
951 * Iterate over every task, ignoring those not runnable, searching just for
952 * "task command functions". These seem to happen in addition to any regular
953 * command matches, so we try them as a separate action.
954 */
955 static void
956 run_game_functions (sc_gameref_t game, const sc_char *string)
957 {
958 sc_int task_count, task, direction;
959
960 /* Iterate over every task, ignoring those not runnable. */
961 task_count = gs_task_count (game);
962 for (task = 0; task < task_count; task++)
963 {
964 if (!task_can_run_task (game, task))
965 continue;
966
967 /*
968 * Try matching forwards and reverse commands. I don't know if it's
969 * valid to put a function in a reverse command, but nevertheless...
970 */
971 for (direction = 0; direction < 2; direction++)
972 {
973 const sc_bool is_forwards = !direction;
974
975 if (task_can_run_task_directional (game, task, is_forwards)
976 && run_match_task_functions (game, task, string, is_forwards))
977 {
978 if (run_task_is_unrestricted (game, task))
979 task_run_task (game, task, is_forwards);
980 }
981 }
982 }
983 }
984
985
986 /*
987 * run_all_commands()
988 * run_game_task_commands()
989 *
990 * Alternative facets of run_commands_common(). The first is used by the
991 * main user input handling loop; the latter by the library when looking for
992 * game commands that override standard actions.
993 */
994 static sc_bool
995 run_all_commands (sc_gameref_t game, const sc_char *string)
996 {
997 const sc_prop_setref_t bundle = gs_get_bundle (game);
998 sc_bool status;
999
1000 /*
1001 * Adrift command matching is just weird, perhaps broken. In theory, a
1002 * game can override system commands with a properly constructed task and
1003 * set of command matchers. However, the Runner isn't terribly consistent
1004 * in when this will work and when not, and some games rely on that in-
1005 * consistency. In particular, a game with a "* object" task that has
1006 * failing restrictions will not be able to override the system's "take
1007 * object", whereas a game's "take object", under the same circumstances,
1008 * will. Yet if the restrictions pass, a game's "* object" overrides the
1009 * system's "take object" with no apparent difficulty.
1010 *
1011 * For example, "The Woods Are Dark" has a "* ball *" task with the
1012 * restriction "must be holding ball". Without special casing it, there's
1013 * no way to get the ball in the first place.
1014 *
1015 * Trying to find the right way to do things here, then, has been tricky.
1016 * Here's the current process: First, run game commands, ignoring any
1017 * cases where restrictions fail to let the task run. Next, try "priority"
1018 * system commands; ones that move objects to inventory. These system
1019 * commands will call back into trying game commands for objects taken or
1020 * dropped, and in those tries, allow overrides only if the game task is
1021 * explicit about what it's doing (that is, doesn't start with "*"), and
1022 * handle restrictions in those tries. After that, retry all game commands
1023 * again with restrictions enabled. And finally, try all other standard
1024 * library commands.
1025 *
1026 * TODO This is the fourth or fifth attempt at getting this to match the
1027 * Runner, which is surprisingly inconsistent in this area. What on earth
1028 * is the real behavior supposed to be?
1029 */
1030 status = run_game_commands_in_parser_context (game, string, FALSE);
1031 if (!status)
1032 status = run_priority_commands (game, string);
1033 if (!status)
1034 status = run_game_commands_in_parser_context (game, string, TRUE);
1035 if (!status)
1036 status = run_standard_commands (game, string);
1037
1038 /*
1039 * For version 4.0 games, it seems that if any command succeeded, we need
1040 * need to scan for and run any matching "task command functions", in
1041 * addition to anything done above.
1042 */
1043 if (status && !game->is_admin)
1044 {
1045 sc_vartype_t vt_key;
1046 sc_int version;
1047
1048 /* Check "task command functions" for version 4.0 only. */
1049 vt_key.string = "Version";
1050 version = prop_get_integer (bundle, "I<-s", &vt_key);
1051 if (version == TAF_VERSION_400)
1052 run_game_functions (game, string);
1053 }
1054
1055 return status;
1056 }
1057
1058 sc_bool
1059 run_game_task_commands (sc_gameref_t game, const sc_char *string)
1060 {
1061 return run_game_commands_in_library_context (game, string);
1062 }
1063
1064
1065 /*
1066 * run_player_input()
1067 *
1068 * Take a line of player input and buffer it. Split the line into elements
1069 * separated by periods. For the first element, try to match it to either a
1070 * task or a standard command, and return TRUE if it matched, FALSE otherwise.
1071 *
1072 * On subsequent calls, successively work with the next line element until
1073 * none remain. In this case, prompt for more player input and continue as
1074 * above.
1075 *
1076 * For the case of "again" or "g", rerun the last successful command element.
1077 *
1078 * One extra special special case; if called with a game that is not running,
1079 * this is a signal to reset all noted line input to initial conditions, and
1080 * just return. Sorry about the ugliness.
1081 */
1082 static sc_bool
1083 run_player_input (sc_gameref_t game)
1084 {
1085 static sc_char line_buffer[LINE_BUFFER_SIZE];
1086 static sc_char prior_element[LINE_BUFFER_SIZE];
1087 static sc_char line_element[LINE_BUFFER_SIZE];
1088
1089 const sc_filterref_t filter = gs_get_filter (game);
1090 const sc_prop_setref_t bundle = gs_get_bundle (game);
1091 const sc_var_setref_t vars = gs_get_vars (game);
1092 const sc_memo_setref_t memento = gs_get_memento (game);
1093 sc_bool is_rerunning, was_undo_available, status;
1094 sc_char *filtered, *replaced;
1095 const sc_char *command;
1096
1097 /* Special case; reset statics if the game isn't running. */
1098 if (!game->is_running)
1099 {
1100 memset (line_buffer, NUL, sizeof (line_buffer));
1101 memset (prior_element, NUL, sizeof (prior_element));
1102 memset (line_element, NUL, sizeof (line_element));
1103 return TRUE;
1104 }
1105
1106 /*
1107 * Save the settings of the game's do_again and undo_available flags for
1108 * later checks.
1109 */
1110 is_rerunning = game->do_again;
1111 was_undo_available = game->undo_available;
1112
1113 /* See if the player asked to rerun a command element. */
1114 if (game->do_again)
1115 {
1116 game->do_again = FALSE;
1117
1118 /* Check there is a last element to repeat. */
1119 if (prior_element[0] == NUL)
1120 {
1121 pf_buffer_string (filter, "You can hardly repeat that.\n");
1122 return FALSE;
1123 }
1124
1125 /* Make the last element the current input element. */
1126 strcpy (line_element, prior_element);
1127 }
1128 else
1129 {
1130 sc_int length, extent;
1131
1132 /*
1133 * If there's none buffered, read a new line of player input. Other-
1134 * wise, separate output so far with a newline.
1135 */
1136 if (line_buffer[0] == NUL)
1137 if_read_line (line_buffer, sizeof (line_buffer));
1138 else
1139 if_print_character ('\n');
1140
1141 /*
1142 * Find the length of the next input line element. Unless the line
1143 * buffer is empty, we always take the first character, even if it's a
1144 * separator. This catches odd input like "." and turns it into a
1145 * parser complaint, rather than treating it as two empty commands with
1146 * a separator between them; this makes it close to what Inform does
1147 * with similar inputs.
1148 */
1149 length = (line_buffer[0] == NUL) ? 0 : 1;
1150 while (line_buffer[length] != NUL
1151 && strchr (SEPARATORS, line_buffer[length]) == NULL)
1152 length++;
1153
1154 /*
1155 * Make this the current input element, and remove it, the separator,
1156 * and any trailing whitespace, from the front of the line buffer.
1157 * Removing whitespace prevents "i. ." looking like "i" and ""; it
1158 * instead looks like "i" and ".", and results in a parser complaint.
1159 */
1160 memcpy (line_element, line_buffer, length);
1161 line_element[length] = NUL;
1162
1163 extent = length;
1164 extent += (line_buffer[length] == NUL
1165 || strchr (SEPARATORS, line_buffer[length]) == NULL) ? 0 : 1;
1166 extent += strspn (line_buffer + extent, WHITESPACE);
1167 memmove (line_buffer,
1168 line_buffer + extent, strlen (line_buffer) - extent + 1);
1169 }
1170
1171 /* Copy the current game to the temporary undo buffer. */
1172 gs_copy (game->temporary, game);
1173
1174 /* Filter the input element for synonyms, then for pronouns. */
1175 filtered = pf_filter_input (line_element, bundle);
1176 replaced = uip_replace_pronouns (game, filtered ? filtered : line_element);
1177
1178 /*
1179 * If filtering didn't replace synonyms, or no pronouns were replaced, use
1180 * the original line element.
1181 */
1182 command = replaced ? sc_normalize_string (replaced)
1183 : (filtered ? sc_normalize_string (filtered) : line_element);
1184 if (command != line_element)
1185 {
1186 if_print_tag (SC_TAG_ITALICS, "");
1187 if_print_character ('[');
1188 if_print_string (command);
1189 if_print_character (']');
1190 if_print_tag (SC_TAG_ENDITALICS, "");
1191 if_print_character ('\n');
1192 }
1193
1194 /* Try the command line element against command matchers. */
1195 status = run_all_commands (game, command);
1196 if (!status)
1197 {
1198 /* Only complain on non-empty command input line elements. */
1199 if (!sc_strempty (command))
1200 {
1201 sc_vartype_t vt_key[2];
1202 sc_char *escaped;
1203 const sc_char *message;
1204
1205 /* Command line element not understood. */
1206 escaped = pf_escape (sc_normalize_string (line_element));
1207 var_set_ref_text (vars, escaped);
1208 sc_free (escaped);
1209 vt_key[0].string = "Globals";
1210 vt_key[1].string = "DontUnderstand";
1211 message = prop_get_string (bundle, "S<-ss", vt_key);
1212 pf_buffer_string (filter, message);
1213 pf_buffer_character (filter, '\n');
1214
1215 /*
1216 * On a line element that's not understood, throw out any remaining
1217 * input line elements.
1218 */
1219 line_buffer[0] = NUL;
1220 sc_free (filtered);
1221 sc_free (replaced);
1222 return status;
1223 }
1224 }
1225 else
1226 {
1227 /*
1228 * Unless administrative, back up any valid undo, copy the temporary
1229 * game into the undo buffer, flag the undo buffer as available, and
1230 * assign any pronouns used in the command ready for the next iteration.
1231 */
1232 if (!game->is_admin)
1233 {
1234 if (game->undo_available)
1235 memo_save_game (memento, game->undo);
1236
1237 gs_copy (game->undo, game->temporary);
1238 game->undo_available = TRUE;
1239
1240 uip_assign_pronouns (game, command);
1241 }
1242 }
1243 sc_free (filtered);
1244 sc_free (replaced);
1245
1246 /*
1247 * If do_again is set, we'll come round with the prior command in line
1248 * element in a moment, so save nothing for that case. Otherwise save the
1249 * command in the history.
1250 */
1251 if (!sc_strempty (line_element) && !game->do_again)
1252 {
1253 /*
1254 * If this is a failed redo, redo_sequence will be set but do_again will
1255 * be clear. Suppress the save for this special case; otherwise, failed
1256 * redo commands get into the history, where they can cause problems
1257 * later on.
1258 */
1259 if (game->redo_sequence == 0)
1260 {
1261 sc_int timestamp;
1262
1263 timestamp = var_get_elapsed_seconds (vars);
1264 memo_save_command (memento, line_element, timestamp, game->turns);
1265 }
1266 else
1267 game->redo_sequence = 0;
1268 }
1269
1270 /*
1271 * Special case restart and restore commands; throw out any remaining input
1272 * and return straight away. Do the same if this was an undo, detected by
1273 * noting that undo is no longer available, where it was on entry.
1274 */
1275 if (game->do_restart || game->do_restore
1276 || (was_undo_available && !game->undo_available))
1277 {
1278 line_buffer[0] = NUL;
1279 return status;
1280 }
1281
1282 /* If not empty, consider as saving for "again" calls and in the history. */
1283 if (!sc_strempty (line_element))
1284 {
1285 /*
1286 * Unless "again", note this line element as prior input. "Again" shows
1287 * up as do_again set in the game, where it wasn't when we entered here.
1288 */
1289 if (!game->do_again && !is_rerunning)
1290 strcpy (prior_element, line_element);
1291
1292 /*
1293 * If this was a request to run a command from the history, copy that
1294 * command into the prior_element for the next iteration. The library
1295 * should have verified the value in redo_sequence, so fetching the
1296 * command string should not fail.
1297 */
1298 if (game->do_again && game->redo_sequence != 0)
1299 {
1300 const sc_char *redo_command;
1301
1302 redo_command = memo_find_command (memento, game->redo_sequence);
1303 if (redo_command)
1304 strcpy (prior_element, redo_command);
1305 else
1306 {
1307 sc_error ("run_player_input: invalid redo sequence request\n");
1308 game->do_again = FALSE;
1309 }
1310 game->redo_sequence = 0;
1311 }
1312 }
1313
1314 return status;
1315 }
1316
1317
1318 /*
1319 * run_main_loop()
1320 *
1321 * Main interpreter loop.
1322 */
1323 static void
1324 run_main_loop (sc_gameref_t game)
1325 {
1326 const sc_filterref_t filter = gs_get_filter (game);
1327 const sc_var_setref_t vars = gs_get_vars (game);
1328 const sc_prop_setref_t bundle = gs_get_bundle (game);
1329
1330 /*
1331 * This may not be the very first time this game has been used, for example
1332 * saving a game right at the start, or undo-ing back to the start through
1333 * memos. Caught by looking to see if the player room is marked as seen.
1334 */
1335 if (!gs_room_seen (game, gs_playerroom (game)))
1336 {
1337 sc_vartype_t vt_key[2];
1338 const sc_char *gamename, *startuptext;
1339 sc_bool disp_first_room, battle_system;
1340
1341 /* If battle system and no debugger display a warning. */
1342 vt_key[0].string = "Globals";
1343 vt_key[1].string = "BattleSystem";
1344 battle_system = prop_get_boolean (bundle, "B<-ss", vt_key);
1345 if (battle_system && !debug_get_enabled (game))
1346 {
1347 if_print_tag (SC_TAG_CLS, "");
1348 lib_warn_battle_system ();
1349 }
1350
1351 /* Initial clear screen. */
1352 pf_buffer_tag (filter, SC_TAG_CLS);
1353
1354 /* Print the game name. */
1355 vt_key[0].string = "Globals";
1356 vt_key[1].string = "GameName";
1357 gamename = prop_get_string (bundle, "S<-ss", vt_key);
1358 pf_buffer_string (filter, gamename);
1359 pf_buffer_character (filter, '\n');
1360
1361 /* Print the game header. */
1362 vt_key[0].string = "Header";
1363 vt_key[1].string = "StartupText";
1364 startuptext = prop_get_string (bundle, "S<-ss", vt_key);
1365 pf_buffer_string (filter, startuptext);
1366 pf_buffer_character (filter, '\n');
1367
1368 /* If flagged, describe the initial room. */
1369 vt_key[0].string = "Globals";
1370 vt_key[1].string = "DispFirstRoom";
1371 disp_first_room = prop_get_boolean (bundle, "B<-ss", vt_key);
1372 if (disp_first_room)
1373 lib_cmd_look (game);
1374
1375 /* Handle any introductory resources. */
1376 vt_key[0].string = "Globals";
1377 vt_key[1].string = "IntroRes";
1378 res_handle_resource (game, "ss", vt_key);
1379
1380 /* Set initial values for NPC and object states. */
1381 npc_setup_initial (game);
1382 obj_setup_initial (game);
1383
1384 /* Nudge events and NPCs. */
1385 evt_tick_events (game);
1386 npc_tick_npcs (game);
1387
1388 /*
1389 * Notify the debugger that the game has started. This is a chance to
1390 * set watchpoints to catch game startup actions. Done before setting
1391 * the initial room visited as this is how the debugger differentiates
1392 * restarts from restore or undo back to game start.
1393 */
1394 debug_game_started (game);
1395
1396 /* Note the initial room as visited. */
1397 gs_set_room_seen (game, gs_playerroom (game), TRUE);
1398 }
1399 else
1400 {
1401 /* Notify the debugger that the game has restarted. */
1402 debug_game_started (game);
1403 }
1404
1405 /*
1406 * Game loop, exits either when a command parser handler sets the game
1407 * running flag to FALSE, or by call to run_quit().
1408 */
1409 while (game->is_running)
1410 {
1411 sc_bool status;
1412
1413 /*
1414 * Synchronize any resources in use; do this before flushing so that any
1415 * appropriate graphics/sound appear before waits or waitkey tag delays
1416 * invoked by flushing the printfilter. Also, print any score change
1417 * notifications.
1418 */
1419 res_sync_resources (game);
1420 run_notify_score_change (game);
1421
1422 /*
1423 * Flush printfilter of any accumulated output, and clear any prior
1424 * notion of administrative commands from input.
1425 */
1426 pf_flush (filter, vars, bundle);
1427 game->is_admin = FALSE;
1428
1429 /* If waitcounter is zero, accept and try a command. */
1430 if (game->waitcounter == 0)
1431 {
1432 /* Not waiting, so handle a player input line. */
1433 run_update_status (game);
1434 status = run_player_input (game);
1435
1436 /*
1437 * If waitcounter is now set, decrement it, as this turn counts as
1438 * one of them.
1439 */
1440 if (game->waitcounter > 0)
1441 game->waitcounter--;
1442 }
1443 else
1444 {
1445 /*
1446 * Currently "waiting"; decrement wait turns, then run a turn having
1447 * taken no input.
1448 */
1449 game->waitcounter--;
1450 status = TRUE;
1451 }
1452
1453 /*
1454 * Do usual turn stuff unless either something stopped the game, or the
1455 * last command didn't match, or the last command did match but was
1456 * administrative.
1457 */
1458 if (status && !game->is_admin)
1459 {
1460 /* Increment turn counter, and clear notifications done flag. */
1461 game->turns++;
1462 game->has_notified = FALSE;
1463
1464 if (game->is_running)
1465 {
1466 /* Nudge events and NPCs. */
1467 evt_tick_events (game);
1468 npc_tick_npcs (game);
1469
1470 /* Update NPC and object states. */
1471 npc_turn_update (game);
1472 obj_turn_update (game);
1473
1474 /* Note the current room as visited. */
1475 gs_set_room_seen (game, gs_playerroom (game), TRUE);
1476
1477 /* Give the debugger a chance to catch watchpoints. */
1478 debug_turn_update (game);
1479 }
1480 }
1481 }
1482
1483 /*
1484 * Final status update, for games that vary it on completion, then notify
1485 * the debugger that the game has ended, to let it make a last watchpoint
1486 * scan and offer the dialog if appropriate.
1487 */
1488 run_update_status (game);
1489 debug_game_ended (game);
1490
1491 /*
1492 * Final resource sync, score change notification and printfilter flush
1493 * on game-instigated loop exit.
1494 */
1495 res_sync_resources (game);
1496 run_notify_score_change (game);
1497 pf_flush (filter, vars, bundle);
1498
1499 /*
1500 * Reset static variables inside run_player_input() with a call to it with
1501 * is_running false; this is a special case.
1502 */
1503 assert (!game->is_running);
1504 run_player_input (game);
1505 }
1506
1507
1508 /*
1509 * run_create()
1510 *
1511 * Create a game context from a callback.
1512 */
1513 sc_gameref_t
1514 run_create (sc_read_callbackref_t callback, void *opaque)
1515 {
1516 sc_tafref_t taf;
1517 sc_prop_setref_t bundle;
1518 sc_var_setref_t vars, temporary_vars, undo_vars;
1519 sc_filterref_t filter;
1520 sc_gameref_t game, temporary_game, undo_game;
1521 assert (callback);
1522
1523 /* Create a new TAF using the callback; return NULL if this fails. */
1524 taf = taf_create (callback, opaque);
1525 if (!taf)
1526 return NULL;
1527 else if (if_get_trace_flag (SC_DUMP_TAF))
1528 taf_debug_dump (taf);
1529
1530 /* Create a properties bundle, and parse the TAF data into it. */
1531 bundle = prop_create (taf);
1532 if (!bundle)
1533 {
1534 sc_error ("run_create: error parsing game data\n");
1535 taf_destroy (taf);
1536 return NULL;
1537 }
1538 else if (if_get_trace_flag (SC_DUMP_PROPERTIES))
1539 prop_debug_dump (bundle);
1540
1541 /* Try to set an interpreter locale from the properties bundle. */
1542 loc_detect_game_locale (bundle);
1543 if (if_get_trace_flag (SC_DUMP_LOCALE_TABLES))
1544 loc_debug_dump ();
1545
1546 /* Create a set of variables from the bundle. */
1547 vars = var_create (bundle);
1548 if (if_get_trace_flag (SC_DUMP_VARIABLES))
1549 var_debug_dump (vars);
1550
1551 /* Create a printfilter for the game. */
1552 filter = pf_create ();
1553
1554 /*
1555 * Create an initial game state, and register it with variables. Also,
1556 * create undo buffers, and initialize them in the same way.
1557 */
1558 game = gs_create (vars, bundle, filter);
1559 var_register_game (vars, game);
1560
1561 temporary_vars = var_create (bundle);
1562 temporary_game = gs_create (temporary_vars, bundle, filter);
1563 var_register_game (temporary_vars, temporary_game);
1564
1565 undo_vars = var_create (bundle);
1566 undo_game = gs_create (undo_vars, bundle, filter);
1567 var_register_game (undo_vars, undo_game);
1568
1569 /* Add the undo buffers and memos to the game, and return it. */
1570 game->temporary = temporary_game;
1571 game->undo = undo_game;
1572 game->memento = memo_create ();
1573 return game;
1574 }
1575
1576
1577 /*
1578 * run_restart_handler()
1579 *
1580 * Return a game context to initial states to restart a game.
1581 */
1582 static void
1583 run_restart_handler (sc_gameref_t game)
1584 {
1585 const sc_filterref_t filter = gs_get_filter (game);
1586 const sc_prop_setref_t bundle = gs_get_bundle (game);
1587 sc_gameref_t new_game;
1588 sc_var_setref_t new_vars;
1589
1590 /*
1591 * Create a fresh set of variables from the current game properties,
1592 * then a new game using these variables and existing properties and
1593 * printfilter.
1594 */
1595 new_vars = var_create (bundle);
1596 new_game = gs_create (new_vars, bundle, filter);
1597 var_register_game (new_vars, new_game);
1598
1599 /*
1600 * Overwrite the dynamic parts of the current game with the new one.
1601 */
1602 new_game->temporary = game->temporary;
1603 new_game->undo = game->undo;
1604 gs_copy (game, new_game);
1605
1606 /* Destroy invalid game status strings. */
1607 sc_free (game->current_room_name);
1608 game->current_room_name = NULL;
1609 sc_free (game->status_line);
1610 game->status_line = NULL;
1611
1612 /*
1613 * Now it's safely copied, destroy the temporary new game, and its
1614 * associated variable set.
1615 */
1616 gs_destroy (new_game);
1617 var_destroy (new_vars);
1618
1619 /* Reset resources handling. */
1620 res_cancel_resources (game);
1621 }
1622
1623
1624 /*
1625 * run_restore_handler()
1626 *
1627 * Adjust a game context for continuation after restoring a game.
1628 */
1629 static void
1630 run_restore_handler (sc_gameref_t game)
1631 {
1632 /* Invalidate the undo buffer. */
1633 game->undo_available = FALSE;
1634
1635 /*
1636 * Resources handling? Arguably we should re-offer resources active when
1637 * the game was saved, but I can't see how this can be achieved with Adrift
1638 * the way it is. Canceling is too broad, so I'll go here with just
1639 * stopping sounds (in case looping).
1640 *
1641 * TODO Rationalize what happens here.
1642 */
1643 game->stop_sound = TRUE;
1644 }
1645
1646
1647 /*
1648 * run_quit_handler()
1649 *
1650 * Tidy up printfilter and input statics on game quit.
1651 */
1652 static void
1653 run_quit_handler (sc_gameref_t game)
1654 {
1655 const sc_filterref_t filter = gs_get_filter (game);
1656 const sc_var_setref_t vars = gs_get_vars (game);
1657 const sc_prop_setref_t bundle = gs_get_bundle (game);
1658
1659 /* Flush printfilter and notifications of any dangling output. */
1660 run_notify_score_change (game);
1661 pf_flush (filter, vars, bundle);
1662
1663 /* Cancel any active resources. */
1664 res_cancel_resources (game);
1665
1666 /*
1667 * Make the special call to reset all of the static variables inside
1668 * run_player_input().
1669 */
1670 assert (!game->is_running);
1671 run_player_input (game);
1672 }
1673
1674
1675 /*
1676 * run_interpret()
1677 *
1678 * Intepret the game in a game context.
1679 */
1680 void
1681 run_interpret (sc_gameref_t game)
1682 {
1683 assert (gs_is_game_valid (game));
1684
1685 /* Verify the game is not already running, and is runnable. */
1686 if (game->is_running)
1687 {
1688 sc_error ("run_interpret: game is already running\n");
1689 return;
1690 }
1691 if (game->has_completed)
1692 {
1693 sc_error ("run_interpret: game has already completed\n");
1694 return;
1695 }
1696
1697 /* Refuse to run a game with no rooms. */
1698 if (gs_room_count (game) == 0)
1699 {
1700 sc_error ("run_interpret: game contains no rooms\n");
1701 return;
1702 }
1703
1704 /* Run the main interpreter loop until no more restarts. */
1705 game->is_running = TRUE;
1706 do
1707 {
1708 /* Run the game until some form of halt is requested. */
1709 if (setjmp (game->quitter) == 0)
1710 run_main_loop (game);
1711
1712 /*
1713 * If the halt was a restart or restore, cancel the request, handle
1714 * restart or restore game adjustments, and set the game running
1715 * again.
1716 */
1717 if (game->do_restart)
1718 {
1719 game->do_restart = FALSE;
1720 run_restart_handler (game);
1721 game->is_running = TRUE;
1722 }
1723
1724 if (game->do_restore)
1725 {
1726 game->do_restore = FALSE;
1727 run_restore_handler (game);
1728 game->is_running = TRUE;
1729 }
1730 }
1731 while (game->is_running);
1732
1733 /* Tidy up the printfilter and input statics. */
1734 run_quit_handler (game);
1735 }
1736
1737
1738 /*
1739 * run_destroy()
1740 *
1741 * Destroy a game context, and free all resources.
1742 */
1743 void
1744 run_destroy (sc_gameref_t game)
1745 {
1746 assert (gs_is_game_valid (game));
1747
1748 /* Can't destroy the context of a running game. */
1749 if (game->is_running)
1750 {
1751 sc_error ("run_destroy: game is running, stop it first\n");
1752 return;
1753 }
1754
1755 /*
1756 * Cancel any game state debugger -- this frees its resources. Only the
1757 * primary game may have acquired a debugger.
1758 */
1759 debug_set_enabled (game, FALSE);
1760 assert (!debug_get_enabled (game->temporary));
1761 assert (!debug_get_enabled (game->undo));
1762
1763 /*
1764 * Destroy the game state, variables, properties bundle, memos, undo
1765 * buffers and their variables, and filter. The bundle and printfilter
1766 * are shared by the main game, the undo game, and the temporary game, so
1767 * destroy these only once! The main game has a memento, but it is not
1768 * visible to these other two games, neither of which have one.
1769 */
1770 assert (gs_get_bundle (game->temporary) == gs_get_bundle (game));
1771 assert (gs_get_filter (game->temporary) == gs_get_filter (game));
1772 assert (gs_get_vars (game->temporary) != gs_get_vars (game));
1773 assert (!gs_get_memento (game->temporary));
1774 var_destroy (gs_get_vars (game->temporary));
1775 gs_destroy (game->temporary);
1776
1777 assert (gs_get_bundle (game->undo) == gs_get_bundle (game));
1778 assert (gs_get_filter (game->undo) == gs_get_filter (game));
1779 assert (gs_get_vars (game->undo) != gs_get_vars (game));
1780 assert (!gs_get_memento (game->undo));
1781 var_destroy (gs_get_vars (game->undo));
1782 gs_destroy (game->undo);
1783
1784 prop_destroy (gs_get_bundle (game));
1785 pf_destroy (gs_get_filter (game));
1786 var_destroy (gs_get_vars (game));
1787 memo_destroy (gs_get_memento (game));
1788
1789 gs_destroy (game);
1790 }
1791
1792
1793 /*
1794 * run_quit()
1795 *
1796 * Quits a running game. This function calls a longjump to act as if
1797 * run_main_loop() returned, and so never returns to its caller.
1798 */
1799 void
1800 run_quit (sc_gameref_t game)
1801 {
1802 assert (gs_is_game_valid (game));
1803
1804 /* Disallow quitting a non-running game. */
1805 if (!game->is_running)
1806 {
1807 sc_error ("run_quit: game is not running\n");
1808 return;
1809 }
1810
1811 /* Exit the main loop with a longjump. */
1812 game->is_running = FALSE;
1813 longjmp (game->quitter, 1);
1814 sc_fatal ("run_quit: unable to quit cleanly\n");
1815 }
1816
1817
1818 /*
1819 * run_restart()
1820 *
1821 * Restarts either a running or a stopped game. For running games, this
1822 * function calls a longjump to act as if run_main_loop() returned, and so
1823 * never returns to its caller. For stopped games, it returns.
1824 */
1825 void
1826 run_restart (sc_gameref_t game)
1827 {
1828 assert (gs_is_game_valid (game));
1829
1830 /*
1831 * If the game is running, stop it, request a restart, and exit the main
1832 * loop with a longjump.
1833 */
1834 if (game->is_running)
1835 {
1836 game->is_running = FALSE;
1837 game->do_restart = TRUE;
1838 longjmp (game->quitter, 1);
1839 sc_fatal ("run_restart: unable to restart cleanly\n");
1840 }
1841
1842 /* Restart locally, and ensure that the game remains stopped. */
1843 run_restart_handler (game);
1844 game->is_running = FALSE;
1845 }
1846
1847
1848 /*
1849 * run_save()
1850 * run_save_prompted()
1851 *
1852 * Saves either a running or a stopped game.
1853 */
1854 void
1855 run_save (sc_gameref_t game, sc_write_callbackref_t callback, void *opaque)
1856 {
1857 assert (gs_is_game_valid (game));
1858 assert (callback);
1859
1860 ser_save_game (game, callback, opaque);
1861 }
1862
1863 sc_bool
1864 run_save_prompted (sc_gameref_t game)
1865 {
1866 assert (gs_is_game_valid (game));
1867
1868 return ser_save_game_prompted (game);
1869 }
1870
1871
1872 /*
1873 * run_restore_common()
1874 * run_restore()
1875 * run_restore_prompted()
1876 *
1877 * Restores either a running or a stopped game. For running games, on
1878 * successful restore, these functions call a longjump to act as if
1879 * run_main_loop() returned, and so never return to their caller. On failed
1880 * restore, and for stopped games, they will return, with TRUE if successful,
1881 * FALSE if restore failed.
1882 */
1883 static sc_bool
1884 run_restore_common (sc_gameref_t game,
1885 sc_read_callbackref_t callback, void *opaque)
1886 {
1887 sc_bool is_running, status;
1888
1889 /*
1890 * Save the game running flag, and call the restore appropriate for the
1891 * caller. The indication of a call from run_restore_prompted() is a
1892 * callback of NULL; callback cannot be NULL for run_restore() calls.
1893 */
1894 is_running = game->is_running;
1895 status = callback ? ser_load_game (game, callback, opaque)
1896 : ser_load_game_prompted (game);
1897 if (status)
1898 {
1899 /* Loading a game clears is_running -- restore it here. */
1900 game->is_running = is_running;
1901
1902 /*
1903 * If the game is (was) running, set flags so that the interpreter
1904 * loop cycles, and exit the main loop with a longjump.
1905 */
1906 if (game->is_running)
1907 {
1908 game->is_running = FALSE;
1909 game->do_restore = TRUE;
1910 longjmp (game->quitter, 1);
1911 sc_fatal ("run_restore_common: unable to restart cleanly\n");
1912 }
1913 }
1914
1915 /* Return TRUE on successful restore of a stopped game, FALSE on error. */
1916 return status;
1917 }
1918
1919 sc_bool
1920 run_restore (sc_gameref_t game, sc_read_callbackref_t callback, void *opaque)
1921 {
1922 assert (gs_is_game_valid (game));
1923 assert (callback);
1924
1925 return run_restore_common (game, callback, opaque);
1926 }
1927
1928 sc_bool
1929 run_restore_prompted (sc_gameref_t game)
1930 {
1931 assert (gs_is_game_valid (game));
1932
1933 return run_restore_common (game, NULL, NULL);
1934 }
1935
1936
1937 /*
1938 * run_undo()
1939 *
1940 * Undo a turn in either a running or a stopped game. Returns TRUE on
1941 * successful undo, FALSE if no undo buffer is available.
1942 */
1943 sc_bool
1944 run_undo (sc_gameref_t game)
1945 {
1946 const sc_memo_setref_t memento = gs_get_memento (game);
1947 sc_bool is_running;
1948 assert (gs_is_game_valid (game));
1949
1950 /* Save the game's running state, so we can restore it later. */
1951 is_running = game->is_running;
1952
1953 /* If there's an undo buffer available, restore it. */
1954 if (game->undo_available)
1955 {
1956 /* Restore the undo buffer, and then restore running flag. */
1957 gs_copy (game, game->undo);
1958 game->undo_available = FALSE;
1959 game->is_running = is_running;
1960
1961 /* Location may have changed; update status. */
1962 run_update_status (game);
1963
1964 /* Bring resources into line with the revised game. */
1965 res_sync_resources (game);
1966 return TRUE;
1967 }
1968
1969 /*
1970 * If there is no undo buffer, try to restore one saved previously in a
1971 * memo. Handle as if restoring from a file.
1972 */
1973 if (memo_load_game (memento, game))
1974 {
1975 /* Loading a game clears is_running -- restore it here. */
1976 game->is_running = is_running;
1977
1978 /*
1979 * If the game is (was) running, set flags so that the interpreter
1980 * loop cycles, and exit the main loop with a longjump.
1981 */
1982 if (game->is_running)
1983 {
1984 game->is_running = FALSE;
1985 game->do_restore = TRUE;
1986 longjmp (game->quitter, 1);
1987 sc_fatal ("run_undo: unable to restart cleanly\n");
1988 }
1989
1990 /* Game undo on non-running game accomplished with memos. */
1991 return TRUE;
1992 }
1993
1994 /* No undo buffer and no memos available. */
1995 return FALSE;
1996 }
1997
1998
1999 /*
2000 * run_is_running()
2001 *
2002 * Query the game running state.
2003 */
2004 sc_bool
2005 run_is_running (sc_gameref_t game)
2006 {
2007 assert (gs_is_game_valid (game));
2008
2009 return game->is_running;
2010 }
2011
2012
2013 /*
2014 * run_has_completed()
2015 *
2016 * Query the game completion state. Completed games cannot be resumed,
2017 * since they've run the exit task and thus have nowhere to go.
2018 */
2019 sc_bool
2020 run_has_completed (sc_gameref_t game)
2021 {
2022 assert (gs_is_game_valid (game));
2023
2024 return game->has_completed;
2025 }
2026
2027
2028 /*
2029 * run_is_undo_available()
2030 *
2031 * Query the game turn undo buffer and memo availability.
2032 */
2033 sc_bool
2034 run_is_undo_available (sc_gameref_t game)
2035 {
2036 const sc_memo_setref_t memento = gs_get_memento (game);
2037 assert (gs_is_game_valid (game));
2038
2039 return game->undo_available || memo_is_load_available (memento);
2040 }
2041
2042
2043 /*
2044 * run_get_attributes()
2045 * run_set_attributes()
2046 *
2047 * Get and set selected game attributes.
2048 */
2049 void
2050 run_get_attributes (sc_gameref_t game,
2051 const sc_char **game_name, const sc_char **game_author,
2052 const sc_char **game_compile_date,
2053 sc_int *turns, sc_int *score, sc_int *max_score,
2054 const sc_char **current_room_name,
2055 const sc_char **status_line, const sc_char **preferred_font,
2056 sc_bool *bold_room_names, sc_bool *verbose,
2057 sc_bool *notify_score_change)
2058 {
2059 const sc_prop_setref_t bundle = gs_get_bundle (game);
2060 const sc_var_setref_t vars = gs_get_vars (game);
2061 sc_vartype_t vt_key[2];
2062 assert (gs_is_game_valid (game));
2063
2064 /* Return the game name, author, and compile date if requested. */
2065 if (game_name)
2066 {
2067 if (!game->title)
2068 {
2069 const sc_char *gamename;
2070 sc_char *filtered;
2071
2072 vt_key[0].string = "Globals";
2073 vt_key[1].string = "GameName";
2074 gamename = prop_get_string (bundle, "S<-ss", vt_key);
2075
2076 filtered = pf_filter_for_info (gamename, vars);
2077 pf_strip_tags (filtered);
2078 game->title = filtered;
2079 }
2080 *game_name = game->title;
2081 }
2082 if (game_author)
2083 {
2084 if (!game->author)
2085 {
2086 const sc_char *gameauthor;
2087 sc_char *filtered;
2088
2089 vt_key[0].string = "Globals";
2090 vt_key[1].string = "GameAuthor";
2091 gameauthor = prop_get_string (bundle, "S<-ss", vt_key);
2092
2093 filtered = pf_filter_for_info (gameauthor, vars);
2094 pf_strip_tags (filtered);
2095 game->author = filtered;
2096 }
2097 *game_author = game->author;
2098 }
2099 if (game_compile_date)
2100 {
2101 vt_key[0].string = "CompileDate";
2102 *game_compile_date = prop_get_string (bundle, "S<-s", vt_key);
2103 }
2104
2105 /* Return the current room name and status line if requested. */
2106 if (current_room_name)
2107 *current_room_name = game->current_room_name;
2108 if (status_line)
2109 *status_line = game->status_line;
2110
2111 /* Return any game preferred font, or NULL if none. */
2112 if (preferred_font)
2113 {
2114 vt_key[0].string = "CustomFont";
2115 if (prop_get_boolean (bundle, "B<-s", vt_key))
2116 {
2117 vt_key[0].string = "FontNameSize";
2118 *preferred_font = prop_get_string (bundle, "S<-s", vt_key);
2119 }
2120 else
2121 *preferred_font = NULL;
2122 }
2123
2124 /* Return any other selected game attributes. */
2125 if (turns)
2126 *turns = game->turns;
2127 if (score)
2128 *score = game->score;
2129 if (max_score)
2130 {
2131 vt_key[0].string = "Globals";
2132 vt_key[1].string = "MaxScore";
2133 *max_score = prop_get_integer (bundle, "I<-ss", vt_key);
2134 }
2135 if (bold_room_names)
2136 *bold_room_names = game->bold_room_names;
2137 if (verbose)
2138 *verbose = game->verbose;
2139 if (notify_score_change)
2140 *notify_score_change = game->notify_score_change;
2141 }
2142
2143 void
2144 run_set_attributes (sc_gameref_t game,
2145 sc_bool bold_room_names, sc_bool verbose,
2146 sc_bool notify_score_change)
2147 {
2148 assert (gs_is_game_valid (game));
2149
2150 /* Set game options. */
2151 game->bold_room_names = bold_room_names;
2152 game->verbose = verbose;
2153 game->notify_score_change = notify_score_change;
2154 }
2155
2156
2157 /*
2158 * run_hint_iterate()
2159 *
2160 * Return the next hint appropriate to the game state, or the first if
2161 * hint is NULL. Returns NULL if none, or no more hints. This function
2162 * works with pointers to a task state rather than task indexes so that
2163 * the token passed in and out is a pointer, and readily made opaque to
2164 * the client as a void*.
2165 */
2166 sc_hintref_t
2167 run_hint_iterate (sc_gameref_t game, sc_hintref_t hint)
2168 {
2169 sc_int task;
2170 assert (gs_is_game_valid (game));
2171
2172 /*
2173 * Hint is a pointer to a task state; convert to a task index, adding one
2174 * to move on to the next task, or start at the first task if null.
2175 */
2176 if (!hint)
2177 task = 0;
2178 else
2179 {
2180 /* Convert into pointer, and range check. */
2181 task = hint - game->tasks;
2182 if (task < 0 || task >= gs_task_count (game))
2183 {
2184 sc_error ("run_hint_iterate: invalid iteration hint\n");
2185 return NULL;
2186 }
2187
2188 /* Advance beyond current task. */
2189 task++;
2190 }
2191
2192 /* Scan for the next runnable task that offers a hint. */
2193 for (; task < gs_task_count (game); task++)
2194 {
2195 if (task_can_run_task (game, task) && task_has_hints (game, task))
2196 break;
2197 }
2198
2199 /* Return a pointer to the state of the task identified, or NULL. */
2200 return task < gs_task_count (game) ? game->tasks + task : NULL;
2201 }
2202
2203
2204 /*
2205 * run_get_hint_common()
2206 * run_get_hint_question()
2207 * run_get_subtle_hint()
2208 * run_get_unsubtle_hint()
2209 *
2210 * Return the strings for a hint. Front-ends to task functions. Each
2211 * converts the hint "address" to a task index through pointer arithmetic,
2212 * then filters it and returns a temporary, valid only until the next hint
2213 * call.
2214 *
2215 * Hint strings are NULL if empty (not defined by the game).
2216 */
2217 static const sc_char *
2218 run_get_hint_common (sc_gameref_t game, sc_hintref_t hint,
2219 const sc_char *(*handler) (sc_gameref_t, sc_int))
2220 {
2221 const sc_prop_setref_t bundle = gs_get_bundle (game);
2222 const sc_var_setref_t vars = gs_get_vars (game);
2223 sc_int task;
2224 const sc_char *string;
2225 assert (gs_is_game_valid (game));
2226
2227 /* Verify the caller passed in a valid hint. */
2228 task = hint - game->tasks;
2229 if (task < 0 || task >= gs_task_count (game))
2230 {
2231 sc_error ("run_get_hint_common: invalid iteration hint\n");
2232 return NULL;
2233 }
2234 else if (!task_has_hints (game, task))
2235 {
2236 sc_error ("run_get_hint_common: task has no hint\n");
2237 return NULL;
2238 }
2239
2240 /* Get the required game text by calling the given handler function. */
2241 string = handler (game, task);
2242 if (!sc_strempty (string))
2243 {
2244 sc_char *filtered;
2245
2246 /* Filter and strip tags, note in game. */
2247 filtered = pf_filter (string, vars, bundle);
2248 pf_strip_tags_for_hints (filtered);
2249 sc_free (game->hint_text);
2250 game->hint_text = filtered;
2251 }
2252 else
2253 {
2254 /* Hint text is empty; drop any text noted in game. */
2255 sc_free (game->hint_text);
2256 game->hint_text = NULL;
2257 }
2258
2259 return game->hint_text;
2260 }
2261
2262 const sc_char *
2263 run_get_hint_question (sc_gameref_t game, sc_hintref_t hint)
2264 {
2265 return run_get_hint_common (game, hint, task_get_hint_question);
2266 }
2267
2268 const sc_char *
2269 run_get_subtle_hint (sc_gameref_t game, sc_hintref_t hint)
2270 {
2271 return run_get_hint_common (game, hint, task_get_hint_subtle);
2272 }
2273
2274 const sc_char *
2275 run_get_unsubtle_hint (sc_gameref_t game, sc_hintref_t hint)
2276 {
2277 return run_get_hint_common (game, hint, task_get_hint_unsubtle);
2278 }