Debian Codegraph @

terps/scare/scdebug.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 <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "scare.h"
32 #include "scprotos.h"
33 #include "scgamest.h"
34
35
36 /* Assorted definitions and constants. */
37 static const sc_uint DEBUG_MAGIC = 0xc4584d2e;
38 enum { DEBUG_BUFFER_SIZE = 256 };
39
40 /* Debugging command and command argument type. */
41 typedef enum
42 { DEBUG_NONE = 0, DEBUG_CONTINUE, DEBUG_STEP, DEBUG_BUFFER, DEBUG_RESOURCES,
43 DEBUG_HELP, DEBUG_GAME,
44 DEBUG_PLAYER, DEBUG_ROOMS, DEBUG_OBJECTS, DEBUG_NPCS, DEBUG_EVENTS,
45 DEBUG_TASKS, DEBUG_VARIABLES,
46 DEBUG_OLDPLAYER, DEBUG_OLDROOMS, DEBUG_OLDOBJECTS, DEBUG_OLDNPCS,
47 DEBUG_OLDEVENTS, DEBUG_OLDTASKS, DEBUG_OLDVARIABLES,
48 DEBUG_WATCHPLAYER, DEBUG_WATCHOBJECTS, DEBUG_WATCHNPCS, DEBUG_WATCHEVENTS,
49 DEBUG_WATCHTASKS, DEBUG_WATCHVARIABLES,
50 DEBUG_CLEARPLAYER, DEBUG_CLEAROBJECTS, DEBUG_CLEARNPCS, DEBUG_CLEAREVENTS,
51 DEBUG_CLEARTASKS, DEBUG_CLEARVARIABLES,
52 DEBUG_WATCHALL, DEBUG_CLEARALL, DEBUG_RANDOM,
53 DEBUG_QUIT
54 }
55 sc_command_t;
56
57 typedef enum
58 { COMMAND_QUERY = 0, COMMAND_RANGE, COMMAND_ONE, COMMAND_ALL }
59 sc_command_type_t;
60
61 /* Table connecting debugging command strings to commands. */
62 typedef struct
63 {
64 const sc_char *const command_string;
65 const sc_command_t command;
66 } sc_strings_t;
67 static const sc_strings_t DEBUG_COMMANDS[] = {
68 {"continue", DEBUG_CONTINUE}, {"step", DEBUG_STEP}, {"buffer", DEBUG_BUFFER},
69 {"resources", DEBUG_RESOURCES}, {"help", DEBUG_HELP}, {"game", DEBUG_GAME},
70 {"player", DEBUG_PLAYER}, {"rooms", DEBUG_ROOMS}, {"objects", DEBUG_OBJECTS},
71 {"npcs", DEBUG_NPCS}, {"events", DEBUG_EVENTS}, {"tasks", DEBUG_TASKS},
72 {"variables", DEBUG_VARIABLES},
73 {"oldplayer", DEBUG_OLDPLAYER}, {"oldrooms", DEBUG_OLDROOMS},
74 {"oldobjects", DEBUG_OLDOBJECTS}, {"oldnpcs", DEBUG_OLDNPCS},
75 {"oldevents", DEBUG_OLDEVENTS}, {"oldtasks", DEBUG_OLDTASKS},
76 {"oldvariables", DEBUG_OLDVARIABLES},
77 {"watchplayer", DEBUG_WATCHPLAYER}, {"clearplayer", DEBUG_CLEARPLAYER},
78 {"watchobjects", DEBUG_WATCHOBJECTS}, {"watchnpcs", DEBUG_WATCHNPCS},
79 {"watchevents", DEBUG_WATCHEVENTS}, {"watchtasks", DEBUG_WATCHTASKS},
80 {"watchvariables", DEBUG_WATCHVARIABLES},
81 {"clearobjects", DEBUG_CLEAROBJECTS}, {"clearnpcs", DEBUG_CLEARNPCS},
82 {"clearevents", DEBUG_CLEAREVENTS}, {"cleartasks", DEBUG_CLEARTASKS},
83 {"clearvariables", DEBUG_CLEARVARIABLES}, {"watchall", DEBUG_WATCHALL},
84 {"clearall", DEBUG_CLEARALL}, {"random", DEBUG_RANDOM}, {"quit", DEBUG_QUIT},
85 {NULL, DEBUG_NONE}
86 };
87
88 /*
89 * Debugging control information structure. The structure is created and
90 * added to the game on enabling debug, and removed and destroyed on
91 * disabling debugging.
92 */
93 typedef struct sc_debugger_s
94 {
95 sc_uint magic;
96 sc_bool *watch_objects;
97 sc_bool *watch_npcs;
98 sc_bool *watch_events;
99 sc_bool *watch_tasks;
100 sc_bool *watch_variables;
101 sc_bool watch_player;
102 sc_bool single_step;
103 sc_bool quit_pending;
104 sc_uint elapsed_seconds;
105 } sc_debugger_t;
106
107
108 /*
109 * debug_is_valid()
110 *
111 * Return TRUE if pointer is a valid debugger, FALSE otherwise.
112 */
113 static sc_bool
114 debug_is_valid (sc_debuggerref_t debug)
115 {
116 return debug && debug->magic == DEBUG_MAGIC;
117 }
118
119
120 /*
121 * debug_get_debugger()
122 *
123 * Return the debugger reference from a game, or NULL if none.
124 */
125 static sc_debuggerref_t
126 debug_get_debugger (sc_gameref_t game)
127 {
128 assert (gs_is_game_valid (game));
129
130 return game->debugger;
131 }
132
133
134 /*
135 * debug_variable_count()
136 *
137 * Common helper to return the count of variables defined in a game.
138 */
139 static sc_int
140 debug_variable_count (sc_gameref_t game)
141 {
142 const sc_prop_setref_t bundle = gs_get_bundle (game);
143 sc_vartype_t vt_key;
144 sc_int variable_count;
145
146 /* Find and return the variables count. */
147 vt_key.string = "Variables";
148 variable_count = prop_get_child_count (bundle, "I<-s", &vt_key);
149
150 return variable_count;
151 }
152
153
154 /*
155 * debug_initialize()
156 *
157 * Create a new set of debug control information, and append it to the
158 * game passed in.
159 */
160 static void
161 debug_initialize (sc_gameref_t game)
162 {
163 sc_debuggerref_t debug;
164
165 /* Create the easy bits of the new debugging set. */
166 debug = sc_malloc (sizeof (*debug));
167 debug->magic = DEBUG_MAGIC;
168 debug->watch_player = FALSE;
169 debug->single_step = FALSE;
170 debug->quit_pending = FALSE;
171 debug->elapsed_seconds = 0;
172
173 /* Allocate watchpoints for everything we can watch. */
174 debug->watch_objects = sc_malloc (gs_object_count (game)
175 * sizeof (*debug->watch_objects));
176 debug->watch_npcs = sc_malloc (gs_npc_count (game)
177 * sizeof (*debug->watch_npcs));
178 debug->watch_events = sc_malloc (gs_event_count (game)
179 * sizeof (*debug->watch_events));
180 debug->watch_tasks = sc_malloc (gs_task_count (game)
181 * sizeof (*debug->watch_tasks));
182 debug->watch_variables = sc_malloc (debug_variable_count (game)
183 * sizeof (*debug->watch_variables));
184
185 /* Clear all watchpoint arrays. */
186 memset (debug->watch_objects, FALSE,
187 gs_object_count (game) * sizeof (*debug->watch_objects));
188 memset (debug->watch_npcs, FALSE,
189 gs_npc_count (game) * sizeof (*debug->watch_npcs));
190 memset (debug->watch_events, FALSE,
191 gs_event_count (game) * sizeof (*debug->watch_events));
192 memset (debug->watch_tasks, FALSE,
193 gs_task_count (game) * sizeof (*debug->watch_tasks));
194 memset (debug->watch_variables, FALSE,
195 debug_variable_count (game) * sizeof (*debug->watch_variables));
196
197 /* Append the new debugger set to the game. */
198 assert (!game->debugger);
199 game->debugger = debug;
200 }
201
202
203 /*
204 * debug_finalize()
205 *
206 * Destroy a debug data set, free its heap memory, and remove its reference
207 * from the game.
208 */
209 static void
210 debug_finalize (sc_gameref_t game)
211 {
212 sc_debuggerref_t debug = debug_get_debugger (game);
213 assert (debug_is_valid (debug));
214
215 /* Free all allocated watchpoint arrays. */
216 sc_free (debug->watch_objects);
217 sc_free (debug->watch_npcs);
218 sc_free (debug->watch_events);
219 sc_free (debug->watch_tasks);
220 sc_free (debug->watch_variables);
221
222 /* Poison and free the debugger itself. */
223 memset (debug, 0xaa, sizeof (*debug));
224 sc_free (debug);
225
226 /* Remove the debug reference from the game. */
227 game->debugger = NULL;
228 }
229
230
231 /*
232 * debug_help()
233 *
234 * Print debugging help.
235 */
236 static void
237 debug_help (sc_command_t topic)
238 {
239 /* Is help general, or specific? */
240 if (topic == DEBUG_NONE)
241 {
242 if_print_debug (
243 "The following debugging commands examine game state:\n\n");
244 if_print_debug (
245 " game -- Print general game information,"
246 " and class counts\n"
247 " player -- Show the player location and position\n"
248 " rooms [Range] -- Print information on game rooms\n"
249 " objects [Range] -- Print information on objects in the game\n"
250 " npcs [Range] -- Print information on game NPCs\n"
251 " events [Range] -- Print information on the game's events\n"
252 " tasks [Range] -- Print information on the game's tasks\n"
253 " variables [Range] -- Show variables defined by the game\n\n");
254 if_print_debug (
255 "Most commands take range inputs. This can be a single number, to"
256 " apply the command to just that item, a range such as '0 to 10' (or"
257 " '0 - 10', '0 .. 10', or simply '0 10') to apply to that range of"
258 " items, or '*' to apply the command to all items of the class. If"
259 " omitted, the command is applied only to the items of the class"
260 " 'relevant' to the current game state; see the help for specific"
261 " commands for more on what is 'relevant'.\n\n");
262 if_print_debug (
263 "The 'player', 'objects', 'npcs', 'events', 'tasks', and 'variables'"
264 " commands may be prefixed with 'old', in which case the values"
265 " printed will be those for the previous game turn, rather than the"
266 " current values.\n\n");
267 if_print_debug (
268 "These debugging commands manage watchpoints:\n\n");
269 if_print_debug (
270 "The 'player', 'objects', 'npcs', 'events', 'tasks', and 'variables'"
271 " commands may be prefixed with 'watch', to set watchpoints."
272 " Watchpoints automatically enter the debugger when the item changes"
273 " state during a game turn. For example 'watchobject 10' monitors"
274 " object 10 for changes, and 'watchnpc *' monitors all NPCs. A"
275 " 'watch' command with no range prints out all watchpoints set for"
276 " that class.\n\n");
277 if_print_debug (
278 "Prefix commands with 'clear' to clear watchpoints, for example"
279 " 'clearnpcs *'. Use 'watchall' to obtain a complete list of every"
280 " watchpoint set, and 'clearall' to clear all watchpoints in one go."
281 " A 'clear' command with no range behaves the same as a 'watch'"
282 " command with no range.\n\n");
283 if_print_debug (
284 "These debugging commands print details of game output and control the"
285 " debugger and interpreter:\n\n");
286 if_print_debug (
287 " buffer -- Show the current buffered game text\n"
288 " resources -- Show current and requested game resources\n"
289 " random [Seed] -- Control the random number generator\n"
290 " step -- Run one game turn, then re-enter the debugger\n"
291 " continue -- Leave the debugger and resume the game\n"
292 " quit -- Exit the interpreter main loop\n"
293 " help [Command] -- Print help specific to Command\n\n");
294 if_print_debug (
295 "Debugging commands may be abbreviated to their shortest unambiguous"
296 " form.\n\n");
297 if_print_debug (
298 "Use the 'debug' or '#debug' command in a game, typed at the usual"
299 " game prompt, to return to the debugger.\n");
300 return;
301 }
302
303 /* Command-specific help. */
304 switch (topic)
305 {
306 case DEBUG_HELP:
307 if_print_debug (
308 "Give the name of the command you want help on, for example 'help"
309 " continue'.\n");
310 break;
311
312 case DEBUG_CONTINUE:
313 if_print_debug (
314 "Leave the debugger and resume the game. Use the 'debug' or '#debug'"
315 " command in a game, typed at the usual game prompt, to return to the"
316 " debugger.\n");
317 break;
318
319 case DEBUG_STEP:
320 if_print_debug (
321 "Run one game turn, then re-enter the debugger. Useful for games that"
322 " intercept empty input lines, which otherwise catch the 'debug'"
323 " command before SCARE can get to it.\n");
324 break;
325
326 case DEBUG_QUIT:
327 if_print_debug (
328 "Exit the interpreter main loop. Equivalent to a confirmed 'quit'"
329 " from within the game itself, this ends the interpreter session.\n");
330 break;
331
332 case DEBUG_BUFFER:
333 if_print_debug (
334 "Print the current text that the game has buffered for output. The"
335 " debugger catches games before they have printed their turn output"
336 " -- this is the text that will be filtered and printed on exiting the"
337 " debugger.\n");
338 break;
339
340 case DEBUG_RESOURCES:
341 if_print_debug (
342 "Print any resources currently active, and any requested by the game"
343 " on the current turn. The requested resources will become the active"
344 " ones on exiting the debugger.\n");
345 break;
346
347 case DEBUG_RANDOM:
348 if_print_debug (
349 "If no seed is given, report the current random number generator"
350 " setting. Otherwise, seed the random number generator with the value"
351 " given. This is useful for persuading games with random sections to"
352 " behave predictably. A new seed value of zero is invalid.\n");
353 break;
354
355 case DEBUG_GAME:
356 if_print_debug (
357 "Print general game information, including the number of rooms,"
358 " objects, events, tasks, and variables that the game defines\n");
359 break;
360
361 case DEBUG_PLAYER:
362 if_print_debug (
363 "Print out the current player room and position, and any parent object"
364 " of the player character.\n");
365 break;
366
367 case DEBUG_OLDPLAYER:
368 if_print_debug (
369 "Print out the player room and position from the previous turn, and"
370 " any parent object of the player character.\n");
371 break;
372
373 case DEBUG_ROOMS:
374 if_print_debug (
375 "Print out the name and contents of rooms in the range. If no range,"
376 " print details of the room containing the player.\n");
377 break;
378
379 case DEBUG_OLDROOMS:
380 if_print_debug (
381 "Print out the name and contents of rooms in the range for the"
382 " previous turn. If no range, print details of the room that"
383 " contained the player on the previous turn.\n");
384 break;
385
386 case DEBUG_OBJECTS:
387 if_print_debug (
388 "Print out details of all objects in the range. If no range, print"
389 " details of objects in the room containing the player, and visible to"
390 " the player.\n");
391 break;
392
393 case DEBUG_OLDOBJECTS:
394 if_print_debug (
395 "Print out details of all objects in the range for the previous turn."
396 " If no range, print details of objects in the room that contained"
397 " the player, and were visible to the player.\n");
398 break;
399
400 case DEBUG_NPCS:
401 if_print_debug (
402 "Print out details of all NPCs in the range. If no range, print"
403 " details of only NPCs in the room containing the player.\n");
404 break;
405
406 case DEBUG_OLDNPCS:
407 if_print_debug (
408 "Print out details of all NPCs in the range for the previous turn."
409 " If no range, print details of only NPCs in the room that contained"
410 " the player.\n");
411 break;
412
413 case DEBUG_EVENTS:
414 if_print_debug (
415 "Print out details of all events in the range. If no range, print"
416 " details of only events currently running.\n");
417 break;
418
419 case DEBUG_OLDEVENTS:
420 if_print_debug (
421 "Print out details of all events in the range for the previous turn."
422 " If no range, print details of only events running on the previous"
423 " turn.\n");
424 break;
425
426 case DEBUG_TASKS:
427 if_print_debug (
428 "Print out details of all tasks in the range. If no range, print"
429 " details of only tasks that are runnable, for the current state of"
430 " the game.\n");
431 break;
432
433 case DEBUG_OLDTASKS:
434 if_print_debug (
435 "Print out details of all tasks in the range for the previous turn."
436 " If no range, print details of only tasks that were runnable, for"
437 " the previous state of the game.\n");
438 break;
439
440 case DEBUG_VARIABLES:
441 if_print_debug (
442 "Print out the names, types, and values of all game variables in the"
443 " range. If no range, print details of all variables (equivalent to"
444 " 'variables *').\n");
445 break;
446
447 case DEBUG_OLDVARIABLES:
448 if_print_debug (
449 "Print out the names, types, and values at the previous turn of all"
450 " game variables in the range. If no range, print details of all"
451 " variables (equivalent to 'variables *').\n");
452 break;
453
454 case DEBUG_WATCHPLAYER:
455 if_print_debug (
456 "If no range is given, list any watchpoint on player movement. If"
457 " range '0' is given, set a watchpoint on player movement. Other"
458 " usages of 'watchplayer' behave as if no range is given.\n");
459 break;
460
461 case DEBUG_WATCHOBJECTS:
462 if_print_debug (
463 "Set watchpoints on all objects in the range. If no range, list out"
464 " object watchpoints currently set.\n");
465 break;
466
467 case DEBUG_WATCHNPCS:
468 if_print_debug (
469 "Set watchpoints on all NPCs in the range. If no range, list out NPC"
470 " watchpoints currently set.\n");
471 break;
472
473 case DEBUG_WATCHEVENTS:
474 if_print_debug (
475 "Set watchpoints on all events in the range. If no range, list out"
476 " event watchpoints currently set.\n");
477 break;
478
479 case DEBUG_WATCHTASKS:
480 if_print_debug (
481 "Set watchpoints on all tasks in the range. If no range, list out"
482 " task watchpoints currently set.\n");
483 break;
484
485 case DEBUG_WATCHVARIABLES:
486 if_print_debug (
487 "Set watchpoints on all game variables in the range. If no range,"
488 " list variable watchpoints currently set.\n");
489 break;
490
491 case DEBUG_CLEARPLAYER:
492 if_print_debug (
493 "Clear any watchpoint set on player movements.\n");
494 break;
495
496 case DEBUG_CLEAROBJECTS:
497 if_print_debug (
498 "Clear watchpoints on all objects in the range. If no range, list"
499 " out object watchpoints currently set.\n");
500 break;
501
502 case DEBUG_CLEARNPCS:
503 if_print_debug (
504 "Clear watchpoints on all NPCs in the range. If no range, list out"
505 " NPC watchpoints currently set.\n");
506 break;
507
508 case DEBUG_CLEAREVENTS:
509 if_print_debug (
510 "Clear watchpoints on all events in the range. If no range, list out"
511 " event watchpoints currently set.\n");
512 break;
513
514 case DEBUG_CLEARTASKS:
515 if_print_debug (
516 "Clear watchpoints on all tasks in the range. If no range, list out"
517 " task watchpoints currently set.\n");
518 break;
519
520 case DEBUG_CLEARVARIABLES:
521 if_print_debug (
522 "Clear watchpoints on all game variables in the range. If no range,"
523 " list variable watchpoints currently set.\n");
524 break;
525
526 case DEBUG_WATCHALL:
527 if_print_debug (
528 "Print out a list of all all watchpoints set for all the classes of"
529 " item on which watchpoints can be used.\n");
530 break;
531
532 case DEBUG_CLEARALL:
533 if_print_debug (
534 "Clear all watchpoints set, on all classes of item on which"
535 " watchpoints can be used.\n");
536 break;
537
538 default:
539 if_print_debug (
540 "Sorry, there is no help available on that at the moment.\n");
541 break;
542 }
543 }
544
545
546 /*
547 * debug_print_quoted()
548 * debug_print_player()
549 * debug_print_room()
550 * debug_print_object()
551 * debug_print_npc()
552 * debug_print_event()
553 * debug_print_task()
554 * debug_print_variable()
555 *
556 * Low level output helpers.
557 */
558 static void
559 debug_print_quoted (const sc_char *string)
560 {
561 if_print_debug_character ('"');
562 if_print_debug (string);
563 if_print_debug_character ('"');
564 }
565
566 static void
567 debug_print_player (sc_gameref_t game)
568 {
569 const sc_prop_setref_t bundle = gs_get_bundle (game);
570 sc_vartype_t vt_key[2];
571 const sc_char *playername;
572
573 vt_key[0].string = "Globals";
574 vt_key[1].string = "PlayerName";
575 playername = prop_get_string (bundle, "S<-ss", vt_key);
576 if_print_debug ("Player ");
577 debug_print_quoted (playername);
578 }
579
580 static void
581 debug_print_room (sc_gameref_t game, sc_int room)
582 {
583 const sc_prop_setref_t bundle = gs_get_bundle (game);
584 sc_vartype_t vt_key[3];
585 sc_char buffer[32];
586 const sc_char *name;
587
588 if_print_debug ("Room ");
589 if (room < 0 || room >= gs_room_count (game))
590 {
591 sprintf (buffer, "%ld ", room);
592 if_print_debug (buffer);
593 if_print_debug ("[Out of range]");
594 return;
595 }
596
597 vt_key[0].string = "Rooms";
598 vt_key[1].integer = room;
599 vt_key[2].string = "Short";
600 name = prop_get_string (bundle, "S<-sis", vt_key);
601 sprintf (buffer, "%ld ", room);
602 if_print_debug (buffer);
603 debug_print_quoted (name);
604 }
605
606 static void
607 debug_print_object (sc_gameref_t game, sc_int object)
608 {
609 const sc_prop_setref_t bundle = gs_get_bundle (game);
610 sc_vartype_t vt_key[3];
611 sc_bool bstatic;
612 sc_char buffer[32];
613 const sc_char *prefix, *name;
614
615 if (object < 0 || object >= gs_object_count (game))
616 {
617 if_print_debug ("Object ");
618 sprintf (buffer, "%ld ", object);
619 if_print_debug (buffer);
620 if_print_debug ("[Out of range]");
621 return;
622 }
623
624 vt_key[0].string = "Objects";
625 vt_key[1].integer = object;
626 vt_key[2].string = "Static";
627 bstatic = prop_get_boolean (bundle, "B<-sis", vt_key);
628 vt_key[2].string = "Prefix";
629 prefix = prop_get_string (bundle, "S<-sis", vt_key);
630 vt_key[2].string = "Short";
631 name = prop_get_string (bundle, "S<-sis", vt_key);
632 if (bstatic)
633 if_print_debug ("Static ");
634 else
635 if_print_debug ("Dynamic ");
636 sprintf (buffer, "%ld ", object);
637 if_print_debug (buffer);
638 debug_print_quoted (prefix);
639 if_print_debug_character (' ');
640 debug_print_quoted (name);
641 }
642
643 static void
644 debug_print_npc (sc_gameref_t game, sc_int npc)
645 {
646 const sc_prop_setref_t bundle = gs_get_bundle (game);
647 sc_vartype_t vt_key[3];
648 sc_char buffer[32];
649 const sc_char *prefix, *name;
650
651 if_print_debug ("NPC ");
652 if (npc < 0 || npc >= gs_npc_count (game))
653 {
654 sprintf (buffer, "%ld ", npc);
655 if_print_debug (buffer);
656 if_print_debug ("[Out of range]");
657 return;
658 }
659
660 vt_key[0].string = "NPCs";
661 vt_key[1].integer = npc;
662 vt_key[2].string = "Prefix";
663 prefix = prop_get_string (bundle, "S<-sis", vt_key);
664 vt_key[2].string = "Name";
665 name = prop_get_string (bundle, "S<-sis", vt_key);
666 sprintf (buffer, "%ld ", npc);
667 if_print_debug (buffer);
668 debug_print_quoted (prefix);
669 if_print_debug_character (' ');
670 debug_print_quoted (name);
671 }
672
673 static void
674 debug_print_event (sc_gameref_t game, sc_int event)
675 {
676 const sc_prop_setref_t bundle = gs_get_bundle (game);
677 sc_vartype_t vt_key[3];
678 sc_char buffer[32];
679 const sc_char *name;
680
681 if_print_debug ("Event ");
682 if (event < 0 || event >= gs_event_count (game))
683 {
684 sprintf (buffer, "%ld ", event);
685 if_print_debug (buffer);
686 if_print_debug ("[Out of range]");
687 return;
688 }
689
690 vt_key[0].string = "Events";
691 vt_key[1].integer = event;
692 vt_key[2].string = "Short";
693 name = prop_get_string (bundle, "S<-sis", vt_key);
694 sprintf (buffer, "%ld ", event);
695 if_print_debug (buffer);
696 debug_print_quoted (name);
697 }
698
699 static void
700 debug_print_task (sc_gameref_t game, sc_int task)
701 {
702 const sc_prop_setref_t bundle = gs_get_bundle (game);
703 sc_vartype_t vt_key[4];
704 sc_char buffer[32];
705 const sc_char *command;
706
707 if_print_debug ("Task ");
708 if (task < 0 || task >= gs_task_count (game))
709 {
710 sprintf (buffer, "%ld ", task);
711 if_print_debug (buffer);
712 if_print_debug ("[Out of range]");
713 return;
714 }
715
716 vt_key[0].string = "Tasks";
717 vt_key[1].integer = task;
718 vt_key[2].string = "Command";
719 vt_key[3].integer = 0;
720 command = prop_get_string (bundle, "S<-sisi", vt_key);
721 sprintf (buffer, "%ld ", task);
722 if_print_debug (buffer);
723 debug_print_quoted (command);
724 }
725
726 static void
727 debug_print_variable (sc_gameref_t game, sc_int variable)
728 {
729 const sc_prop_setref_t bundle = gs_get_bundle (game);
730 const sc_var_setref_t vars = gs_get_vars (game);
731 sc_vartype_t vt_key[3], vt_rvalue;
732 sc_char buffer[32];
733 sc_int var_type;
734 const sc_char *name;
735
736 if (variable < 0 || variable >= debug_variable_count (game))
737 {
738 if_print_debug ("Variable ");
739 sprintf (buffer, "%ld ", variable);
740 if_print_debug (buffer);
741 if_print_debug ("[Out of range]");
742 return;
743 }
744
745 vt_key[0].string = "Variables";
746 vt_key[1].integer = variable;
747 vt_key[2].string = "Name";
748 name = prop_get_string (bundle, "S<-sis", vt_key);
749
750 if (var_get (vars, name, &var_type, &vt_rvalue))
751 {
752 switch (var_type)
753 {
754 case VAR_INTEGER:
755 if_print_debug ("Integer ");
756 break;
757 case VAR_STRING:
758 if_print_debug ("String ");
759 break;
760 default:
761 if_print_debug ("[Invalid type] ");
762 break;
763 }
764 }
765 else
766 if_print_debug ("[Invalid variable] ");
767 sprintf (buffer, "%ld ", variable);
768 if_print_debug (buffer);
769 debug_print_quoted (name);
770 }
771
772
773 /*
774 * debug_game()
775 *
776 * Display overall game details.
777 */
778 static void
779 debug_game (sc_gameref_t game, sc_command_type_t type)
780 {
781 const sc_prop_setref_t bundle = gs_get_bundle (game);
782 const sc_debuggerref_t debug = debug_get_debugger (game);
783 sc_vartype_t vt_key[2];
784 const sc_char *version, *gamename, *compiledate, *gameauthor;
785 sc_int perspective, waitturns;
786 sc_bool has_sound, has_graphics, has_battle;
787 sc_char buffer[32];
788 assert (debug_is_valid (debug));
789
790 if (type != COMMAND_QUERY)
791 {
792 if_print_debug ("The Game command takes no arguments.\n");
793 return;
794 }
795
796 if_print_debug ("Game ");
797 vt_key[0].string = "Globals";
798 vt_key[1].string = "GameName";
799 gamename = prop_get_string (bundle, "S<-ss", vt_key);
800 debug_print_quoted (gamename);
801 if_print_debug_character ('\n');
802
803 if_print_debug (" Compiled ");
804 vt_key[0].string = "CompileDate";
805 compiledate = prop_get_string (bundle, "S<-s", vt_key);
806 debug_print_quoted (compiledate);
807
808 if_print_debug (", Author ");
809 vt_key[0].string = "Globals";
810 vt_key[1].string = "GameAuthor";
811 gameauthor = prop_get_string (bundle, "S<-ss", vt_key);
812 debug_print_quoted (gameauthor);
813 if_print_debug_character ('\n');
814
815 vt_key[0].string = "VersionString";
816 version = prop_get_string (bundle, "S<-s", vt_key);
817 if_print_debug (" Version ");
818 if_print_debug (version);
819
820 vt_key[0].string = "Globals";
821 vt_key[1].string = "Perspective";
822 perspective = prop_get_integer (bundle, "I<-ss", vt_key);
823 switch (perspective)
824 {
825 case 0:
826 if_print_debug (", First person");
827 break;
828 case 1:
829 if_print_debug (", Second person");
830 break;
831 case 2:
832 if_print_debug (", Third person");
833 break;
834 default:
835 if_print_debug (", [Unknown perspective]");
836 break;
837 }
838
839 vt_key[0].string = "Globals";
840 vt_key[1].string = "WaitTurns";
841 waitturns = prop_get_integer (bundle, "I<-ss", vt_key);
842 if_print_debug (", Waitturns ");
843 sprintf (buffer, "%ld", waitturns);
844 if_print_debug (buffer);
845
846 vt_key[0].string = "Globals";
847 vt_key[1].string = "Sound";
848 has_sound = prop_get_boolean (bundle, "B<-ss", vt_key);
849 vt_key[1].string = "Graphics";
850 has_graphics = prop_get_boolean (bundle, "B<-ss", vt_key);
851 if (has_sound)
852 if_print_debug (", Sound");
853 if (has_graphics)
854 if_print_debug (", Graphics");
855 if_print_debug_character ('\n');
856
857 vt_key[0].string = "Globals";
858 vt_key[1].string = "BattleSystem";
859 has_battle = prop_get_boolean (bundle, "B<-ss", vt_key);
860 if (has_battle)
861 if_print_debug (" Battle system\n");
862
863 if_print_debug (" Room count ");
864 sprintf (buffer, "%ld", gs_room_count (game));
865 if_print_debug (buffer);
866
867 if_print_debug (", Object count ");
868 sprintf (buffer, "%ld", gs_object_count (game));
869 if_print_debug (buffer);
870
871 if_print_debug (", NPC count ");
872 sprintf (buffer, "%ld", gs_npc_count (game));
873 if_print_debug (buffer);
874 if_print_debug_character ('\n');
875
876 if_print_debug (" Event count ");
877 sprintf (buffer, "%ld", gs_event_count (game));
878 if_print_debug (buffer);
879
880 if_print_debug (", Task count ");
881 sprintf (buffer, "%ld", gs_task_count (game));
882 if_print_debug (buffer);
883
884 if_print_debug (", Variable count ");
885 sprintf (buffer, "%ld", debug_variable_count (game));
886 if_print_debug (buffer);
887 if_print_debug_character ('\n');
888
889 if (game->is_running)
890 if_print_debug (" Running");
891 else
892 if_print_debug (" Not running");
893 if (game->has_completed)
894 if_print_debug (", Completed");
895 else
896 if_print_debug (", Not completed");
897 if (game->verbose)
898 if_print_debug (", Verbose");
899 else
900 if_print_debug (", Not verbose");
901 if (game->bold_room_names)
902 if_print_debug (", Bold");
903 else
904 if_print_debug (", Not bold");
905 if (game->undo_available)
906 if_print_debug (", Undo");
907 else
908 if_print_debug (", No undo");
909 if_print_debug_character ('\n');
910
911 if_print_debug (" Score ");
912 sprintf (buffer, "%ld", game->score);
913 if_print_debug (buffer);
914 if_print_debug (", Turns ");
915 sprintf (buffer, "%ld", game->turns);
916 if_print_debug (buffer);
917 if_print_debug (", Seconds ");
918 sprintf (buffer, "%lu", debug->elapsed_seconds);
919 if_print_debug (buffer);
920 if_print_debug_character ('\n');
921 }
922
923
924 /*
925 * debug_player()
926 *
927 * Print a few brief details about the player status.
928 */
929 static void
930 debug_player (sc_gameref_t game,
931 sc_command_t command, sc_command_type_t type)
932 {
933 if (type != COMMAND_QUERY)
934 {
935 if_print_debug ("The Player command takes no arguments.\n");
936 return;
937 }
938
939 if (command == DEBUG_OLDPLAYER)
940 {
941 if (!game->undo_available)
942 {
943 if_print_debug ("There is no previous game state to examine.\n");
944 return;
945 }
946
947 game = game->undo;
948 assert (gs_is_game_valid (game));
949 }
950
951 debug_print_player (game);
952 if_print_debug_character ('\n');
953
954 if (gs_playerroom (game) == -1)
955 if_print_debug (" Hidden!\n");
956 else
957 {
958 if_print_debug (" In ");
959 debug_print_room (game, gs_playerroom (game));
960 if_print_debug_character ('\n');
961 }
962
963 switch (gs_playerposition (game))
964 {
965 case 0:
966 if_print_debug (" Standing\n");
967 break;
968 case 1:
969 if_print_debug (" Sitting\n");
970 break;
971 case 2:
972 if_print_debug (" Lying\n");
973 break;
974 default:
975 if_print_debug (" [Invalid position]\n");
976 break;
977 }
978
979 if (gs_playerparent (game) != -1)
980 {
981 if_print_debug (" Parent is ");
982 debug_print_object (game, gs_playerparent (game));
983 if_print_debug_character ('\n');
984 }
985 }
986
987
988 /*
989 * debug_normalize_arguments()
990 *
991 * Normalize a set of arguments parsed from a debugger command line, for
992 * debug commands that take ranges.
993 */
994 static sc_bool
995 debug_normalize_arguments (sc_command_type_t type,
996 sc_int *arg1, sc_int *arg2, sc_int limit)
997 {
998 sc_int low = 0, high = 0;
999
1000 /* Set range low and high depending on the command type. */
1001 switch (type)
1002 {
1003 case COMMAND_QUERY:
1004 case COMMAND_ALL:
1005 low = 0;
1006 high = limit - 1;
1007 break;
1008 case COMMAND_ONE:
1009 low = *arg1;
1010 high = *arg1;
1011 break;
1012 case COMMAND_RANGE:
1013 low = *arg1;
1014 high = *arg2;
1015 break;
1016 default:
1017 sc_fatal ("debug_normalize_arguments: bad command type\n");
1018 }
1019
1020 /* If range is valid, copy out and return TRUE. */
1021 if (low >= 0 && low < limit && high >= 0 && high < limit && high >= low)
1022 {
1023 *arg1 = low;
1024 *arg2 = high;
1025 return TRUE;
1026 }
1027
1028 /* Input range is invalid. */
1029 return FALSE;
1030 }
1031
1032
1033 /*
1034 * debug_filter_room()
1035 * debug_dump_room()
1036 *
1037 * Print details of rooms and their direct contents.
1038 */
1039 static sc_bool
1040 debug_filter_room (sc_gameref_t game, sc_int room)
1041 {
1042 return room == gs_playerroom (game);
1043 }
1044
1045 static void
1046 debug_dump_room (sc_gameref_t game, sc_int room)
1047 {
1048 sc_int object, npc;
1049
1050 debug_print_room (game, room);
1051 if_print_debug_character ('\n');
1052
1053 if (gs_room_seen (game, room))
1054 if_print_debug (" Visited\n");
1055 else
1056 if_print_debug (" Not visited\n");
1057
1058 if (gs_playerroom (game) == room)
1059 {
1060 if_print_debug (" ");
1061 debug_print_player (game);
1062 if_print_debug_character ('\n');
1063 }
1064
1065 for (object = 0; object < gs_object_count (game); object++)
1066 {
1067 if (obj_indirectly_in_room (game, object, room))
1068 {
1069 if_print_debug (" ");
1070 debug_print_object (game, object);
1071 if_print_debug_character ('\n');
1072 }
1073 }
1074
1075 for (npc = 0; npc < gs_npc_count (game); npc++)
1076 {
1077 if (npc_in_room (game, npc, room))
1078 {
1079 if_print_debug (" ");
1080 debug_print_npc (game, npc);
1081 if_print_debug_character ('\n');
1082 }
1083 }
1084 }
1085
1086
1087 /*
1088 * debug_filter_object()
1089 * debug_dump_object()
1090 *
1091 * Print the changeable details of game objects.
1092 */
1093 static sc_bool
1094 debug_filter_object (sc_gameref_t game, sc_int object)
1095 {
1096 return obj_indirectly_in_room (game, object, gs_playerroom (game));
1097 }
1098
1099 static void
1100 debug_dump_object (sc_gameref_t game, sc_int object)
1101 {
1102 const sc_prop_setref_t bundle = gs_get_bundle (game);
1103 sc_int openness;
1104 sc_vartype_t vt_key[3];
1105 sc_bool bstatic, is_statussed;
1106 sc_int position, parent;
1107
1108 debug_print_object (game, object);
1109 if_print_debug_character ('\n');
1110
1111 vt_key[0].string = "Objects";
1112 vt_key[1].integer = object;
1113 vt_key[2].string = "Static";
1114 bstatic = prop_get_boolean (bundle, "B<-sis", vt_key);
1115
1116 if (gs_object_seen (game, object))
1117 if_print_debug (" Seen");
1118 else
1119 if_print_debug (" Not seen");
1120 if (bstatic)
1121 {
1122 if (gs_object_static_unmoved (game, object))
1123 if_print_debug (", Not relocated");
1124 else
1125 if_print_debug (", Relocated");
1126 }
1127 else
1128 {
1129 vt_key[2].string = "OnlyWhenNotMoved";
1130 if (prop_get_integer (bundle, "I<-sis", vt_key) == 1)
1131 {
1132 if (gs_object_unmoved (game, object))
1133 if_print_debug (", Not moved");
1134 else
1135 if_print_debug (", Moved");
1136 }
1137 }
1138 openness = gs_object_openness (game, object);
1139 switch (openness)
1140 {
1141 case OBJ_OPEN:
1142 if_print_debug (", Open");
1143 break;
1144 case OBJ_CLOSED:
1145 if_print_debug (", Closed");
1146 break;
1147 case OBJ_LOCKED:
1148 if_print_debug (", Locked");
1149 break;
1150 }
1151 if_print_debug_character ('\n');
1152
1153 position = gs_object_position (game, object);
1154 parent = gs_object_parent (game, object);
1155 switch (position)
1156 {
1157 case OBJ_HIDDEN:
1158 if (bstatic)
1159 if_print_debug (" Static default\n");
1160 else
1161 if_print_debug (" Hidden\n");
1162 break;
1163 case OBJ_HELD_PLAYER:
1164 if_print_debug (" Held by ");
1165 debug_print_player (game);
1166 if_print_debug_character ('\n');
1167 break;
1168 case OBJ_HELD_NPC:
1169 if_print_debug (" Held by ");
1170 debug_print_npc (game, parent);
1171 if_print_debug_character ('\n');
1172 break;
1173 case OBJ_WORN_PLAYER:
1174 if_print_debug (" Worn by ");
1175 debug_print_player (game);
1176 if_print_debug_character ('\n');
1177 break;
1178 case OBJ_WORN_NPC:
1179 if_print_debug (" Worn by ");
1180 debug_print_npc (game, parent);
1181 if_print_debug_character ('\n');
1182 break;
1183 case OBJ_PART_NPC:
1184 if_print_debug (" Part of ");
1185 if (parent == -1)
1186 debug_print_player (game);
1187 else
1188 debug_print_npc (game, parent);
1189 if_print_debug_character ('\n');
1190 break;
1191 case OBJ_ON_OBJECT:
1192 if_print_debug (" On ");
1193 debug_print_object (game, parent);
1194 if_print_debug_character ('\n');
1195 break;
1196 case OBJ_IN_OBJECT:
1197 if_print_debug (" Inside ");
1198 debug_print_object (game, parent);
1199 if_print_debug_character ('\n');
1200 break;
1201 default:
1202 if_print_debug (" In ");
1203 debug_print_room (game, position - 1);
1204 if_print_debug_character ('\n');
1205 break;
1206 }
1207
1208 vt_key[0].string = "Objects";
1209 vt_key[1].integer = object;
1210 vt_key[2].string = "CurrentState";
1211 is_statussed = prop_get_integer (bundle, "I<-sis", vt_key) != 0;
1212 if (is_statussed)
1213 {
1214 sc_char buffer[32];
1215 const sc_char *states;
1216
1217 if_print_debug (" State ");
1218 sprintf (buffer, "%ld", gs_object_state (game, object));
1219 if_print_debug (buffer);
1220
1221 vt_key[2].string = "States";
1222 states = prop_get_string (bundle, "S<-sis", vt_key);
1223 if_print_debug (" of ");
1224 debug_print_quoted (states);
1225 if_print_debug_character ('\n');
1226 }
1227 }
1228
1229
1230 /*
1231 * debug_filter_npc()
1232 * debug_dump_npc()
1233 *
1234 * Print stuff about NPCs.
1235 */
1236 static sc_bool
1237 debug_filter_npc (sc_gameref_t game, sc_int npc)
1238 {
1239 return npc_in_room (game, npc, gs_playerroom (game));
1240 }
1241
1242 static void
1243 debug_dump_npc (sc_gameref_t game, sc_int npc)
1244 {
1245 debug_print_npc (game, npc);
1246 if_print_debug_character ('\n');
1247
1248 if (gs_npc_seen (game, npc))
1249 if_print_debug (" Seen\n");
1250 else
1251 if_print_debug (" Not seen\n");
1252
1253 if (gs_npc_location (game, npc) - 1 == -1)
1254 if_print_debug (" Hidden\n");
1255 else
1256 {
1257 if_print_debug (" In ");
1258 debug_print_room (game, gs_npc_location (game, npc) - 1);
1259 if_print_debug_character ('\n');
1260 }
1261
1262 switch (gs_npc_position (game, npc))
1263 {
1264 case 0:
1265 if_print_debug (" Standing\n");
1266 break;
1267 case 1:
1268 if_print_debug (" Sitting\n");
1269 break;
1270 case 2:
1271 if_print_debug (" Lying\n");
1272 break;
1273 default:
1274 if_print_debug (" [Invalid position]\n");
1275 break;
1276 }
1277
1278 if (gs_npc_parent (game, npc) != -1)
1279 {
1280 if_print_debug (" Parent is ");
1281 debug_print_object (game, gs_npc_parent (game, npc));
1282 if_print_debug_character ('\n');
1283 }
1284
1285 if (gs_npc_walkstep_count (game, npc) > 0)
1286 {
1287 sc_char buffer[32];
1288 sc_int walk;
1289
1290 if_print_debug (" Walkstep count ");
1291 sprintf (buffer, "%ld", gs_npc_walkstep_count (game, npc));
1292 if_print_debug (buffer);
1293 if_print_debug (", Walks { ");
1294 for (walk = 0; walk < gs_npc_walkstep_count (game, npc); walk++)
1295 {
1296 sprintf (buffer, "%ld", gs_npc_walkstep (game, npc, walk));
1297 if_print_debug (buffer);
1298 if_print_debug_character (' ');
1299 }
1300 if_print_debug ("}.\n");
1301 }
1302 }
1303
1304
1305 /*
1306 * debug_filter_event()
1307 * debug_dump_event()
1308 *
1309 * Print stuff about events.
1310 */
1311 static sc_bool
1312 debug_filter_event (sc_gameref_t game, sc_int event)
1313 {
1314 return gs_event_state (game, event) == ES_RUNNING;
1315 }
1316
1317 static void
1318 debug_dump_event (sc_gameref_t game, sc_int event)
1319 {
1320 sc_char buffer[32];
1321
1322 debug_print_event (game, event);
1323 if_print_debug_character ('\n');
1324
1325 switch (gs_event_state (game, event))
1326 {
1327 case ES_WAITING:
1328 if_print_debug (" Waiting\n");
1329 break;
1330 case ES_RUNNING:
1331 if_print_debug (" Running\n");
1332 break;
1333 case ES_AWAITING:
1334 if_print_debug (" Awaiting\n");
1335 break;
1336 case ES_FINISHED:
1337 if_print_debug (" Finished\n");
1338 break;
1339 case ES_PAUSED:
1340 if_print_debug (" Paused\n");
1341 break;
1342 default:
1343 if_print_debug (" [Invalid state]\n");
1344 break;
1345 }
1346
1347 if_print_debug (" Time ");
1348 sprintf (buffer, "%ld\n", gs_event_time (game, event));
1349 if_print_debug (buffer);
1350 }
1351
1352
1353 /*
1354 * debug_filter_task()
1355 * debug_dump_task()
1356 *
1357 * Print stuff about tasks.
1358 */
1359 static sc_bool
1360 debug_filter_task (sc_gameref_t game, sc_int task)
1361 {
1362 return task_can_run_task (game, task);
1363 }
1364
1365 static void
1366 debug_dump_task (sc_gameref_t game, sc_int task)
1367 {
1368 debug_print_task (game, task);
1369 if_print_debug_character ('\n');
1370
1371 if (task_can_run_task (game, task))
1372 if_print_debug (" Runnable");
1373 else
1374 if_print_debug (" Not runnable");
1375 if (gs_task_done (game, task))
1376 if_print_debug (", Done");
1377 else
1378 if_print_debug (", Not done");
1379 if (gs_task_scored (game, task))
1380 if_print_debug (", Scored\n");
1381 else
1382 if_print_debug (", Not scored\n");
1383 }
1384
1385
1386 /*
1387 * debug_dump_variable()
1388 *
1389 * Print stuff about variables.
1390 */
1391 static void
1392 debug_dump_variable (sc_gameref_t game, sc_int variable)
1393 {
1394 const sc_prop_setref_t bundle = gs_get_bundle (game);
1395 const sc_var_setref_t vars = gs_get_vars (game);
1396 sc_vartype_t vt_key[3], vt_rvalue;
1397 const sc_char *name;
1398 sc_int var_type;
1399
1400 debug_print_variable (game, variable);
1401 if_print_debug_character ('\n');
1402
1403 vt_key[0].string = "Variables";
1404 vt_key[1].integer = variable;
1405 vt_key[2].string = "Name";
1406 name = prop_get_string (bundle, "S<-sis", vt_key);
1407
1408 if_print_debug (" Value = ");
1409 if (var_get (vars, name, &var_type, &vt_rvalue))
1410 {
1411 switch (var_type)
1412 {
1413 case VAR_INTEGER:
1414 {
1415 sc_char buffer[32];
1416
1417 sprintf (buffer, "%ld", vt_rvalue.integer);
1418 if_print_debug (buffer);
1419 break;
1420 }
1421 case VAR_STRING:
1422 debug_print_quoted (vt_rvalue.string);
1423 break;
1424 default:
1425 if_print_debug ("[Unknown]");
1426 break;
1427 }
1428 }
1429 else
1430 if_print_debug ("[Unknown]");
1431 if_print_debug_character ('\n');
1432 }
1433
1434
1435 /*
1436 * debug_dump_common()
1437 *
1438 * Common handler for iterating dumps of classes.
1439 */
1440 static void
1441 debug_dump_common (sc_gameref_t game, sc_command_t command,
1442 sc_command_type_t type, sc_int arg1, sc_int arg2)
1443 {
1444 sc_int low = arg1, high = arg2;
1445 sc_int limit, index_;
1446 const sc_char *class_;
1447 sc_bool (*filter_function) (sc_gameref_t, sc_int);
1448 void (*dumper_function) (sc_gameref_t, sc_int);
1449 sc_bool printed = FALSE;
1450
1451 /* Initialize variables to avoid gcc warnings. */
1452 limit = 0;
1453 class_ = NULL;
1454 filter_function = NULL;
1455 dumper_function = NULL;
1456
1457 /* Switch to undo game on relevant commands. */
1458 switch (command)
1459 {
1460 case DEBUG_OLDROOMS:
1461 case DEBUG_OLDOBJECTS:
1462 case DEBUG_OLDNPCS:
1463 case DEBUG_OLDEVENTS:
1464 case DEBUG_OLDTASKS:
1465 case DEBUG_OLDVARIABLES:
1466 if (!game->undo_available)
1467 {
1468 if_print_debug ("There is no previous game state to examine.\n");
1469 return;
1470 }
1471
1472 game = game->undo;
1473 assert (gs_is_game_valid (game));
1474 break;
1475
1476 default:
1477 break;
1478 }
1479
1480 /* Demultiplex dump command. */
1481 switch (command)
1482 {
1483 case DEBUG_ROOMS:
1484 case DEBUG_OLDROOMS:
1485 class_ = "Room";
1486 filter_function = debug_filter_room;
1487 dumper_function = debug_dump_room;
1488 limit = gs_room_count (game);
1489 break;
1490 case DEBUG_OBJECTS:
1491 case DEBUG_OLDOBJECTS:
1492 class_ = "Object";
1493 filter_function = debug_filter_object;
1494 dumper_function = debug_dump_object;
1495 limit = gs_object_count (game);
1496 break;
1497 case DEBUG_NPCS:
1498 case DEBUG_OLDNPCS:
1499 class_ = "NPC";
1500 filter_function = debug_filter_npc;
1501 dumper_function = debug_dump_npc;
1502 limit = gs_npc_count (game);
1503 break;
1504 case DEBUG_EVENTS:
1505 case DEBUG_OLDEVENTS:
1506 class_ = "Event";
1507 filter_function = debug_filter_event;
1508 dumper_function = debug_dump_event;
1509 limit = gs_event_count (game);
1510 break;
1511 case DEBUG_TASKS:
1512 case DEBUG_OLDTASKS:
1513 class_ = "Task";
1514 filter_function = debug_filter_task;
1515 dumper_function = debug_dump_task;
1516 limit = gs_task_count (game);
1517 break;
1518 case DEBUG_VARIABLES:
1519 case DEBUG_OLDVARIABLES:
1520 class_ = "Variable";
1521 filter_function = NULL;
1522 dumper_function = debug_dump_variable;
1523 limit = debug_variable_count (game);
1524 break;
1525 default:
1526 sc_fatal ("debug_dump_common: invalid command\n");
1527 }
1528
1529 /* Normalize to this limit. */
1530 if (!debug_normalize_arguments (type, &low, &high, limit))
1531 {
1532 if (limit == 0)
1533 {
1534 if_print_debug ("There is nothing of type ");
1535 debug_print_quoted (class_);
1536 if_print_debug (" to print.\n");
1537 }
1538 else
1539 {
1540 if_print_debug ("Invalid item or range for ");
1541 debug_print_quoted (class_);
1542 if (limit == 1)
1543 if_print_debug ("; only 0 is valid.\n");
1544 else
1545 {
1546 sc_char buffer[32];
1547
1548 if_print_debug ("; valid values are 0 to ");
1549 sprintf (buffer, "%ld", limit - 1);
1550 if_print_debug (buffer);
1551 if_print_debug (".\n");
1552 }
1553 }
1554 return;
1555 }
1556
1557 /* Print each item of the class, filtering on query commands. */
1558 for (index_ = low; index_ <= high; index_++)
1559 {
1560 if (type == COMMAND_QUERY
1561 && filter_function && !filter_function (game, index_))
1562 continue;
1563
1564 if (printed)
1565 if_print_debug_character ('\n');
1566 dumper_function (game, index_);
1567 printed = TRUE;
1568 }
1569 if (!printed)
1570 {
1571 if_print_debug ("Nothing of type ");
1572 debug_print_quoted (class_);
1573 if_print_debug (" is relevant.\nTry \"");
1574 if_print_debug (class_);
1575 if_print_debug (" *\" to show all items of this type.\n");
1576 }
1577 }
1578
1579
1580 /*
1581 * debug_buffer()
1582 *
1583 * Print the current raw printfilter contents.
1584 */
1585 static void
1586 debug_buffer (sc_gameref_t game, sc_command_type_t type)
1587 {
1588 const sc_filterref_t filter = gs_get_filter (game);
1589 const sc_char *buffer;
1590
1591 if (type != COMMAND_QUERY)
1592 {
1593 if_print_debug ("The Buffer command takes no arguments.\n");
1594 return;
1595 }
1596
1597 buffer = pf_get_buffer (filter);
1598 if (buffer)
1599 if_print_debug (buffer);
1600 else
1601 if_print_debug ("There is no game text buffered.\n");
1602 }
1603
1604
1605 /*
1606 * debug_print_resource()
1607 *
1608 * Helper for debug_resources().
1609 */
1610 static void
1611 debug_print_resource (const sc_resource_t *resource)
1612 {
1613 sc_char buffer[32];
1614
1615 debug_print_quoted (resource->name);
1616 if_print_debug (", offset ");
1617 sprintf (buffer, "%ld", resource->offset);
1618 if_print_debug (buffer);
1619 if_print_debug (", length ");
1620 sprintf (buffer, "%ld", resource->length);
1621 if_print_debug (buffer);
1622 }
1623
1624
1625 /*
1626 * debug_resources()
1627 *
1628 * Print any active and requested resources.
1629 */
1630 static void
1631 debug_resources (sc_gameref_t game, sc_command_type_t type)
1632 {
1633 sc_bool printed = FALSE;
1634
1635 if (type != COMMAND_QUERY)
1636 {
1637 if_print_debug ("The Resources command takes no arguments.\n");
1638 return;
1639 }
1640
1641 if (game->stop_sound)
1642 {
1643 if_print_debug ("Sound stop");
1644 if (strlen (game->requested_sound.name) > 0)
1645 if_print_debug (" before new sound");
1646 if_print_debug (" requested");
1647 if (game->sound_active)
1648 if_print_debug (", sound active");
1649 if_print_debug (".\n");
1650 printed = TRUE;
1651 }
1652 if (!res_compare_resource (&game->requested_sound,
1653 &game->playing_sound))
1654 {
1655 if_print_debug ("Requested Sound ");
1656 debug_print_resource (&game->requested_sound);
1657 if_print_debug (".\n");
1658 printed = TRUE;
1659 }
1660 if (!res_compare_resource (&game->requested_graphic,
1661 &game->displayed_graphic))
1662 {
1663 if_print_debug ("Requested Graphic ");
1664 debug_print_resource (&game->requested_graphic);
1665 if_print_debug (".\n");
1666 printed = TRUE;
1667 }
1668
1669 if (strlen (game->playing_sound.name) > 0)
1670 {
1671 if_print_debug ("Playing Sound ");
1672 debug_print_resource (&game->playing_sound);
1673 if_print_debug (".\n");
1674 printed = TRUE;
1675 }
1676 if (strlen (game->displayed_graphic.name) > 0)
1677 {
1678 if_print_debug ("Displaying Graphic ");
1679 debug_print_resource (&game->displayed_graphic);
1680 if_print_debug (".\n");
1681 printed = TRUE;
1682 }
1683
1684 if (!printed)
1685 if_print_debug ("There is no game resource activity.\n");
1686 }
1687
1688
1689 /*
1690 * debug_random()
1691 *
1692 * Report the PRNG in use, and seed the random number generator to the
1693 * given value.
1694 */
1695 static void
1696 debug_random (sc_command_type_t type, sc_int new_seed)
1697 {
1698 const sc_char *random_type;
1699 sc_char buffer[32];
1700
1701 if (type != COMMAND_ONE && type != COMMAND_QUERY)
1702 {
1703 if_print_debug ("The Random command takes either one argument or"
1704 " no arguments.\n");
1705 return;
1706 }
1707
1708 random_type = sc_is_congruential_random () ? "congruential" : "platform";
1709
1710 if (type == COMMAND_QUERY)
1711 {
1712 if_print_debug ("The ");
1713 if_print_debug (random_type);
1714 if_print_debug (" random number generator is selected.\n");
1715 return;
1716 }
1717
1718 if (new_seed == 0)
1719 {
1720 if_print_debug ("The seed value may not be zero.\n");
1721 return;
1722 }
1723
1724 sc_seed_random (new_seed);
1725
1726 if_print_debug ("Set seed ");
1727 sprintf (buffer, "%ld", new_seed);
1728 if_print_debug (buffer);
1729 if_print_debug (" for the ");
1730 if_print_debug (random_type);
1731 if_print_debug (" random number generator.\n");
1732 }
1733
1734
1735 /*
1736 * debug_watchpoint_common()
1737 *
1738 * Common handler for setting and clearing watchpoints.
1739 */
1740 static void
1741 debug_watchpoint_common (sc_gameref_t game, sc_command_t command,
1742 sc_command_type_t type, sc_int arg1, sc_int arg2)
1743 {
1744 const sc_debuggerref_t debug = debug_get_debugger (game);
1745 sc_int low = arg1, high = arg2;
1746 sc_int limit, index_;
1747 const sc_char *class_;
1748 sc_bool *watchpoints, action;
1749 sc_char buffer[32];
1750 assert (debug_is_valid (debug));
1751
1752 /* Initialize variables to avoid gcc warnings. */
1753 limit = 0;
1754 class_ = NULL;
1755 watchpoints = NULL;
1756 action = FALSE;
1757
1758 /* Set action to TRUE or FALSE, for setting/clearing watchpoints. */
1759 switch (command)
1760 {
1761 case DEBUG_WATCHPLAYER:
1762 case DEBUG_WATCHOBJECTS:
1763 case DEBUG_WATCHNPCS:
1764 case DEBUG_WATCHEVENTS:
1765 case DEBUG_WATCHTASKS:
1766 case DEBUG_WATCHVARIABLES:
1767 action = TRUE;
1768 break;
1769 case DEBUG_CLEARPLAYER:
1770 case DEBUG_CLEAROBJECTS:
1771 case DEBUG_CLEARNPCS:
1772 case DEBUG_CLEAREVENTS:
1773 case DEBUG_CLEARTASKS:
1774 case DEBUG_CLEARVARIABLES:
1775 action = FALSE;
1776 break;
1777 default:
1778 sc_fatal ("debug_watchpoint_common: invalid command\n");
1779 }
1780
1781 /* Handle player watchpoint setting. */
1782 if (command == DEBUG_WATCHPLAYER || command == DEBUG_CLEARPLAYER)
1783 {
1784 if (command == DEBUG_CLEARPLAYER)
1785 {
1786 debug->watch_player = action;
1787 if_print_debug ("Cleared Player watchpoint.\n");
1788 }
1789 else if (type == COMMAND_ONE && arg1 == 0)
1790 {
1791 debug->watch_player = action;
1792 if_print_debug ("Set Player watchpoint.\n");
1793 }
1794 else
1795 {
1796 if (debug->watch_player)
1797 if_print_debug ("Player watchpoint is set.\n");
1798 else
1799 if_print_debug ("No Player watchpoint is set; to set one, use"
1800 " \"Watchplayer 0\".\n");
1801 }
1802 return;
1803 }
1804
1805 /* Demultiplex watchpoint command. */
1806 switch (command)
1807 {
1808 case DEBUG_WATCHOBJECTS:
1809 case DEBUG_CLEAROBJECTS:
1810 class_ = "Object";
1811 watchpoints = debug->watch_objects;
1812 limit = gs_object_count (game);
1813 break;
1814 case DEBUG_WATCHNPCS:
1815 case DEBUG_CLEARNPCS:
1816 class_ = "NPC";
1817 watchpoints = debug->watch_npcs;
1818 limit = gs_npc_count (game);
1819 break;
1820 case DEBUG_WATCHEVENTS:
1821 case DEBUG_CLEAREVENTS:
1822 class_ = "Event";
1823 watchpoints = debug->watch_events;
1824 limit = gs_event_count (game);
1825 break;
1826 case DEBUG_WATCHTASKS:
1827 case DEBUG_CLEARTASKS:
1828 class_ = "Task";
1829 watchpoints = debug->watch_tasks;
1830 limit = gs_task_count (game);
1831 break;
1832 case DEBUG_WATCHVARIABLES:
1833 case DEBUG_CLEARVARIABLES:
1834 class_ = "Variable";
1835 watchpoints = debug->watch_variables;
1836 limit = debug_variable_count (game);
1837 break;
1838 default:
1839 sc_fatal ("debug_watchpoint_common: invalid command\n");
1840 }
1841
1842 /* Normalize to this limit. */
1843 if (!debug_normalize_arguments (type, &low, &high, limit))
1844 {
1845 if (limit == 0)
1846 {
1847 if_print_debug ("There is nothing of type ");
1848 debug_print_quoted (class_);
1849 if_print_debug (" to watch.\n");
1850 }
1851 else
1852 {
1853 if_print_debug ("Invalid item or range for ");
1854 debug_print_quoted (class_);
1855 if (limit == 1)
1856 if_print_debug ("; only 0 is valid.\n");
1857 else
1858 {
1859 if_print_debug ("; valid values are 0 to ");
1860 sprintf (buffer, "%ld", limit - 1);
1861 if_print_debug (buffer);
1862 if_print_debug (".\n");
1863 }
1864 }
1865 return;
1866 }
1867
1868 /* On query, search the array for set flags, and print out. */
1869 if (type == COMMAND_QUERY)
1870 {
1871 sc_bool printed = FALSE;
1872
1873 /* Scan for set watchpoints, and list each found. */
1874 for (index_ = low; index_ <= high; index_++)
1875 {
1876 if (watchpoints[index_])
1877 {
1878 if (!printed)
1879 {
1880 if_print_debug ("Watchpoints are set for ");
1881 if_print_debug (class_);
1882 if_print_debug (" { ");
1883 }
1884 sprintf (buffer, "%ld", index_);
1885 if_print_debug (buffer);
1886 if_print_debug_character (' ');
1887 printed = TRUE;
1888 }
1889 }
1890 if (printed)
1891 if_print_debug ("}.\n");
1892 else
1893 {
1894 if_print_debug ("No ");
1895 if_print_debug (class_);
1896 if_print_debug (" watchpoints are set.\n");
1897 }
1898 return;
1899 }
1900
1901 /*
1902 * For non-queries, set watchpoint flags as defined in action for
1903 * the range determined, and print confirmation.
1904 */
1905 for (index_ = low; index_ <= high; index_++)
1906 watchpoints[index_] = action;
1907
1908 if (action)
1909 if_print_debug ("Set ");
1910 else
1911 if_print_debug ("Cleared ");
1912 sprintf (buffer, "%ld ", high - low + 1);
1913 if_print_debug (buffer);
1914 if_print_debug (class_);
1915 if (high == low)
1916 if_print_debug (" watchpoint.\n");
1917 else
1918 if_print_debug (" watchpoints.\n");
1919 }
1920
1921
1922 /*
1923 * debug_watchall_common()
1924 *
1925 * Common handler to list out and clear all set watchpoints at a stroke.
1926 */
1927 static void
1928 debug_watchall_common (sc_gameref_t game,
1929 sc_command_t command, sc_command_type_t type)
1930 {
1931 const sc_debuggerref_t debug = debug_get_debugger (game);
1932 assert (debug_is_valid (debug));
1933
1934 if (type != COMMAND_QUERY)
1935 {
1936 if (command == DEBUG_WATCHALL)
1937 if_print_debug ("The Watchall command takes no arguments.\n");
1938 else
1939 if_print_debug ("The Clearall command takes no arguments.\n");
1940 return;
1941 }
1942
1943 /* Query all set watchpoints using common watchpoint handler... */
1944 if (command == DEBUG_WATCHALL)
1945 {
1946 debug_watchpoint_common (game,
1947 DEBUG_WATCHPLAYER, COMMAND_QUERY, 0, 0);
1948 debug_watchpoint_common (game,
1949 DEBUG_WATCHOBJECTS, COMMAND_QUERY, 0, 0);
1950 debug_watchpoint_common (game,
1951 DEBUG_WATCHNPCS, COMMAND_QUERY, 0, 0);
1952 debug_watchpoint_common (game,
1953 DEBUG_WATCHEVENTS, COMMAND_QUERY, 0, 0);
1954 debug_watchpoint_common (game,
1955 DEBUG_WATCHTASKS, COMMAND_QUERY, 0, 0);
1956 debug_watchpoint_common (game,
1957 DEBUG_WATCHVARIABLES, COMMAND_QUERY, 0, 0);
1958 return;
1959 }
1960
1961 /* ...but reset all the fast way, with memset(). */
1962 assert (command == DEBUG_CLEARALL);
1963 debug->watch_player = FALSE;
1964 memset (debug->watch_objects, FALSE,
1965 gs_object_count (game) * sizeof (*debug->watch_objects));
1966 memset (debug->watch_npcs, FALSE,
1967 gs_npc_count (game) * sizeof (*debug->watch_npcs));
1968 memset (debug->watch_events, FALSE,
1969 gs_event_count (game) * sizeof (*debug->watch_events));
1970 memset (debug->watch_tasks, FALSE,
1971 gs_task_count (game) * sizeof (*debug->watch_tasks));
1972 memset (debug->watch_variables, FALSE,
1973 debug_variable_count (game) * sizeof (*debug->watch_variables));
1974 if_print_debug ("Cleared all watchpoints.\n");
1975 }
1976
1977
1978 /*
1979 * debug_compare_object()
1980 *
1981 * Compare two objects, and return TRUE if the same.
1982 */
1983 static sc_bool
1984 debug_compare_object (sc_gameref_t from, sc_gameref_t with, sc_int object)
1985 {
1986 const sc_objectstate_t *from_object = from->objects + object;
1987 const sc_objectstate_t *with_object = with->objects + object;
1988
1989 return from_object->unmoved == with_object->unmoved
1990 && from_object->static_unmoved == with_object->static_unmoved
1991 && from_object->position == with_object->position
1992 && from_object->parent == with_object->parent
1993 && from_object->openness == with_object->openness
1994 && from_object->state == with_object->state
1995 && from_object->seen == with_object->seen;
1996 }
1997
1998
1999 /*
2000 * debug_compare_npc()
2001 *
2002 * Compare two NPCs, and return TRUE if the same.
2003 */
2004 static sc_bool
2005 debug_compare_npc (sc_gameref_t from, sc_gameref_t with, sc_int npc)
2006 {
2007 const sc_npcstate_t *from_npc = from->npcs + npc;
2008 const sc_npcstate_t *with_npc = with->npcs + npc;
2009
2010 if (from_npc->walkstep_count != with_npc->walkstep_count)
2011 sc_fatal ("debug_compare_npc: walkstep count error\n");
2012
2013 return from_npc->location == with_npc->location
2014 && from_npc->position == with_npc->position
2015 && from_npc->parent == with_npc->parent
2016 && from_npc->seen == with_npc->seen
2017 && memcmp (from_npc->walksteps, with_npc->walksteps,
2018 from_npc->walkstep_count
2019 * sizeof (*from_npc->walksteps)) == 0;
2020 }
2021
2022
2023 /*
2024 * debug_compare_event()
2025 *
2026 * Compare two events, and return TRUE if the same.
2027 */
2028 static sc_bool
2029 debug_compare_event (sc_gameref_t from, sc_gameref_t with, sc_int event)
2030 {
2031 const sc_eventstate_t *from_event = from->events + event;
2032 const sc_eventstate_t *with_event = with->events + event;
2033
2034 return from_event->state == with_event->state
2035 && from_event->time == with_event->time;
2036 }
2037
2038
2039 /*
2040 * debug_compare_task()
2041 *
2042 * Compare two tasks, and return TRUE if the same.
2043 */
2044 static sc_bool
2045 debug_compare_task (sc_gameref_t from, sc_gameref_t with, sc_int task)
2046 {
2047 const sc_taskstate_t *from_task = from->tasks + task;
2048 const sc_taskstate_t *with_task = with->tasks + task;
2049
2050 return from_task->done == with_task->done
2051 && from_task->scored == with_task->scored;
2052 }
2053
2054
2055 /*
2056 * debug_compare_variable()
2057 *
2058 * Compare two variables, and return TRUE if the same.
2059 */
2060 static sc_bool
2061 debug_compare_variable (sc_gameref_t from, sc_gameref_t with, sc_int variable)
2062 {
2063 const sc_prop_setref_t bundle = from->bundle;
2064 const sc_var_setref_t from_var = from->vars;
2065 const sc_var_setref_t with_var = with->vars;
2066 sc_vartype_t vt_key[3], vt_rvalue, vt_rvalue2;
2067 const sc_char *name;
2068 sc_int var_type, var_type2;
2069 sc_bool equal = FALSE;
2070
2071 if (from->bundle != with->bundle)
2072 sc_fatal ("debug_compare_variable: property sharing malfunction\n");
2073
2074 vt_key[0].string = "Variables";
2075 vt_key[1].integer = variable;
2076 vt_key[2].string = "Name";
2077 name = prop_get_string (bundle, "S<-sis", vt_key);
2078
2079 if (!var_get (from_var, name, &var_type, &vt_rvalue)
2080 || !var_get (with_var, name, &var_type2, &vt_rvalue2))
2081 sc_fatal ("debug_compare_variable: can't find variable %s\n", name);
2082 else if (var_type != var_type2)
2083 sc_fatal ("debug_compare_variable: variable type mismatch %s\n", name);
2084
2085 switch (var_type)
2086 {
2087 case VAR_INTEGER:
2088 equal = (vt_rvalue.integer == vt_rvalue2.integer);
2089 break;
2090 case VAR_STRING:
2091 equal = !strcmp (vt_rvalue.string, vt_rvalue2.string);
2092 break;
2093 default:
2094 sc_fatal ("debug_compare_variable:"
2095 " invalid variable type, %ld\n", var_type);
2096 }
2097
2098 return equal;
2099 }
2100
2101
2102 /*
2103 * debug_check_class()
2104 *
2105 * Central handler for checking watchpoints. Compares a number of items
2106 * of a class using the comparison function given, where indicated by a
2107 * watchpoints flags array. Prints entries that differ, and returns TRUE
2108 * if any differed.
2109 */
2110 static sc_bool
2111 debug_check_class (sc_gameref_t from, sc_gameref_t with,
2112 const sc_char *class_, sc_int class_count,
2113 const sc_bool *watchpoints,
2114 sc_bool (*const compare_function)
2115 (sc_gameref_t, sc_gameref_t, sc_int))
2116 {
2117 sc_int index_;
2118 sc_bool triggered = FALSE;
2119
2120 /*
2121 * Scan the watchpoints array for set watchpoints, comparing classes
2122 * where the watchpoint flag is set.
2123 */
2124 for (index_ = 0; index_ < class_count; index_++)
2125 {
2126 if (!watchpoints[index_])
2127 continue;
2128
2129 if (!compare_function (from, with, index_))
2130 {
2131 sc_char buffer[32];
2132
2133 if (!triggered)
2134 {
2135 if_print_debug ("--- ");
2136 if_print_debug (class_);
2137 if_print_debug (" watchpoint triggered { ");
2138 }
2139 sprintf (buffer, "%ld ", index_);
2140 if_print_debug (buffer);
2141 triggered = TRUE;
2142 }
2143 }
2144 if (triggered)
2145 if_print_debug ("}.\n");
2146
2147 /* Return TRUE if anything differed. */
2148 return triggered;
2149 }
2150
2151
2152 /*
2153 * debug_check_watchpoints()
2154 *
2155 * Checks the game against the undo game for all set watchpoints. Returns
2156 * TRUE if any triggered, FALSE if none (or if the undo game isn't available,
2157 * in which case no check is possible).
2158 */
2159 static sc_bool
2160 debug_check_watchpoints (sc_gameref_t game)
2161 {
2162 const sc_debuggerref_t debug = debug_get_debugger (game);
2163 const sc_gameref_t undo = game->undo;
2164 sc_bool triggered;
2165 assert (debug_is_valid (debug) && gs_is_game_valid (undo));
2166
2167 /* If no undo is present, no check is possible. */
2168 if (!game->undo_available)
2169 return FALSE;
2170
2171 /* Check first for player watchpoint. */
2172 triggered = FALSE;
2173 if (debug->watch_player)
2174 {
2175 if (gs_playerroom (game) != gs_playerroom (undo)
2176 || gs_playerposition (game) != gs_playerposition (undo)
2177 || gs_playerparent (game) != gs_playerparent (undo))
2178 {
2179 if_print_debug ("--- Player watchpoint triggered.\n");
2180 triggered |= TRUE;
2181 }
2182 }
2183
2184 /* Now check other classes of watchpoint. */
2185 triggered |= debug_check_class (game, undo,
2186 "Object", gs_object_count (game),
2187 debug->watch_objects, debug_compare_object);
2188 triggered |= debug_check_class (game, undo,
2189 "NPC", gs_npc_count (game),
2190 debug->watch_npcs, debug_compare_npc);
2191 triggered |= debug_check_class (game, undo,
2192 "Event", gs_event_count (game),
2193 debug->watch_events, debug_compare_event);
2194 triggered |= debug_check_class (game, undo,
2195 "Task", gs_task_count (game),
2196 debug->watch_tasks, debug_compare_task);
2197 triggered |= debug_check_class (game, undo,
2198 "Variable", debug_variable_count (game),
2199 debug->watch_variables,
2200 debug_compare_variable);
2201
2202 return triggered;
2203 }
2204
2205
2206 /*
2207 * debug_parse_command()
2208 *
2209 * Given a debugging command string, try to parse it and return the
2210 * appropriate command and its arguments. Returns DEBUG_NONE if the parse
2211 * fails.
2212 */
2213 static sc_command_t
2214 debug_parse_command (const sc_char *command_string,
2215 sc_command_type_t *type,
2216 sc_int *arg1, sc_int *arg2, sc_command_t *help_topic)
2217 {
2218 sc_command_t return_command;
2219 sc_command_type_t return_type;
2220 sc_int val1, val2, converted, matches;
2221 sc_char *help, *string, junk, wildcard;
2222 sc_bool is_help, is_parsed, is_wildcard;
2223 const sc_strings_t *entry;
2224
2225 /* Allocate temporary strings long enough to take a copy of the input. */
2226 string = sc_malloc (strlen (command_string) + 1);
2227 help = sc_malloc (strlen (command_string) + 1);
2228
2229 /*
2230 * Parse the input line, in a very simplistic fashion. The argument count
2231 * is one less than sscanf converts.
2232 */
2233 is_parsed = is_wildcard = is_help = FALSE;
2234 val1 = val2 = 0;
2235 converted = sscanf (command_string, " %s %s %c", help, string, &junk);
2236 if (converted == 2 && sc_strcasecmp (help, "help") == 0)
2237 {
2238 is_help = TRUE;
2239 is_parsed = TRUE;
2240 }
2241 sc_free (help);
2242 if (!is_parsed)
2243 {
2244 converted = sscanf (command_string,
2245 " %s %ld to %ld %c", string, &val1, &val2, &junk);
2246 if (converted != 3)
2247 converted = sscanf (command_string,
2248 " %s %ld - %ld %c", string, &val1, &val2, &junk);
2249 if (converted != 3)
2250 converted = sscanf (command_string,
2251 " %s %ld .. %ld %c", string, &val1, &val2, &junk);
2252 if (converted != 3)
2253 converted = sscanf (command_string,
2254 " %s %ld %ld %c", string, &val1, &val2, &junk);
2255 is_parsed |= converted == 3;
2256 }
2257 if (!is_parsed)
2258 {
2259 converted = sscanf (command_string,
2260 " %s %ld %c", string, &val1, &junk);
2261 is_parsed |= converted == 2;
2262 }
2263 if (!is_parsed)
2264 {
2265 converted = sscanf (command_string,
2266 " %s %c %c", string, &wildcard, &junk);
2267 if (converted == 2 && wildcard == '*')
2268 {
2269 is_wildcard = TRUE;
2270 is_parsed = TRUE;
2271 }
2272 else
2273 is_parsed |= converted == 1;
2274 }
2275 if (!is_parsed)
2276 {
2277 if_print_debug ("Invalid debug command.");
2278 if_print_debug (" Type 'help' for a list of valid commands.\n");
2279 sc_free (string);
2280 return DEBUG_NONE;
2281 }
2282
2283 /* Decide on a command type based on the parse. */
2284 if (is_wildcard)
2285 return_type = COMMAND_ALL;
2286 else if (converted == 3)
2287 return_type = COMMAND_RANGE;
2288 else if (converted == 2)
2289 return_type = COMMAND_ONE;
2290 else
2291 return_type = COMMAND_QUERY;
2292
2293 /*
2294 * Find the first unambiguous command matching the string. If none,
2295 * return DEBUG_NONE.
2296 */
2297 matches = 0;
2298 return_command = DEBUG_NONE;
2299 for (entry = DEBUG_COMMANDS; entry->command_string; entry++)
2300 {
2301 if (sc_strncasecmp (string, entry->command_string, strlen (string)) == 0)
2302 {
2303 matches++;
2304 return_command = entry->command;
2305 }
2306 }
2307 if (matches != 1)
2308 {
2309 if (matches > 1)
2310 if_print_debug ("Ambiguous debug command.");
2311 else
2312 if_print_debug ("Unrecognized debug command.");
2313 if_print_debug (" Type 'help' for a list of valid commands.\n");
2314 sc_free (string);
2315 return DEBUG_NONE;
2316 }
2317
2318 /* Done with temporary command parse area. */
2319 sc_free (string);
2320
2321 /*
2322 * Return the command type, arguments, and the debugging command. For help
2323 * <topic>, the command is help, with the command on which help requested
2324 * in *help_topic. All clear, then?
2325 */
2326 *type = return_type;
2327 *arg1 = val1;
2328 *arg2 = val2;
2329 *help_topic = is_help ? return_command : DEBUG_NONE;
2330 return is_help ? DEBUG_HELP : return_command;
2331 }
2332
2333
2334 /*
2335 * debug_dispatch()
2336 *
2337 * Dispatch a debugging command to the appropriate handler.
2338 */
2339 static void
2340 debug_dispatch (sc_gameref_t game,
2341 sc_command_t command, sc_command_type_t type,
2342 sc_int arg1, sc_int arg2, sc_command_t help_topic)
2343 {
2344 /* Demultiplex debugging command, and call handlers. */
2345 switch (command)
2346 {
2347 case DEBUG_HELP:
2348 debug_help (help_topic);
2349 break;
2350 case DEBUG_BUFFER:
2351 debug_buffer (game, type);
2352 break;
2353 case DEBUG_RESOURCES:
2354 debug_resources (game, type);
2355 break;
2356 case DEBUG_RANDOM:
2357 debug_random (type, arg1);
2358 break;
2359 case DEBUG_GAME:
2360 debug_game (game, type);
2361 break;
2362 case DEBUG_PLAYER:
2363 case DEBUG_OLDPLAYER:
2364 debug_player (game, command, type);
2365 break;
2366 case DEBUG_ROOMS:
2367 case DEBUG_OBJECTS:
2368 case DEBUG_NPCS:
2369 case DEBUG_EVENTS:
2370 case DEBUG_TASKS:
2371 case DEBUG_VARIABLES:
2372 case DEBUG_OLDROOMS:
2373 case DEBUG_OLDOBJECTS:
2374 case DEBUG_OLDNPCS:
2375 case DEBUG_OLDEVENTS:
2376 case DEBUG_OLDTASKS:
2377 case DEBUG_OLDVARIABLES:
2378 debug_dump_common (game, command, type, arg1, arg2);
2379 break;
2380 case DEBUG_WATCHPLAYER:
2381 case DEBUG_WATCHOBJECTS:
2382 case DEBUG_WATCHNPCS:
2383 case DEBUG_WATCHEVENTS:
2384 case DEBUG_WATCHTASKS:
2385 case DEBUG_WATCHVARIABLES:
2386 case DEBUG_CLEARPLAYER:
2387 case DEBUG_CLEAROBJECTS:
2388 case DEBUG_CLEARNPCS:
2389 case DEBUG_CLEAREVENTS:
2390 case DEBUG_CLEARTASKS:
2391 case DEBUG_CLEARVARIABLES:
2392 debug_watchpoint_common (game, command, type, arg1, arg2);
2393 break;
2394 case DEBUG_WATCHALL:
2395 case DEBUG_CLEARALL:
2396 debug_watchall_common (game, command, type);
2397 break;
2398 case DEBUG_NONE:
2399 break;
2400 default:
2401 sc_fatal ("debug_dispatch: invalid debug command\n");
2402 }
2403 }
2404
2405
2406 /*
2407 * debug_dialog()
2408 *
2409 * Create a small debugging dialog with the user.
2410 */
2411 static void
2412 debug_dialog (sc_gameref_t game)
2413 {
2414 const sc_filterref_t filter = gs_get_filter (game);
2415 const sc_debuggerref_t debug = debug_get_debugger (game);
2416 const sc_var_setref_t vars = gs_get_vars (game);
2417 assert (debug_is_valid (debug));
2418
2419 /*
2420 * Note elapsed seconds, so time stands still while debugging, and clear
2421 * any pending game quit left over from prior dialogs (just in case).
2422 */
2423 debug->elapsed_seconds = var_get_elapsed_seconds (vars);
2424 debug->quit_pending = FALSE;
2425
2426 /* Handle debug commands until debugger quit or game quit. */
2427 while (TRUE)
2428 {
2429 sc_char buffer[DEBUG_BUFFER_SIZE];
2430 sc_command_t command, help_topic;
2431 sc_command_type_t type;
2432 sc_int arg1, arg2;
2433
2434 /* Get a debugging command string from the user. */
2435 do
2436 {
2437 if_read_debug (buffer, sizeof (buffer));
2438 }
2439 while (sc_strempty (buffer));
2440
2441 /* Parse the command read, and handle dialog exit commands. */
2442 command = debug_parse_command (buffer,
2443 &type, &arg1, &arg2, &help_topic);
2444 if (command == DEBUG_CONTINUE || command == DEBUG_STEP)
2445 {
2446 if (!game->is_running)
2447 {
2448 if_print_debug ("The game is no longer running.\n");
2449 continue;
2450 }
2451
2452 debug->single_step = (command == DEBUG_STEP);
2453 break;
2454 }
2455 else if (command == DEBUG_QUIT)
2456 {
2457 /*
2458 * If the game is not running, we can't halt it, and we don't need
2459 * to confirm the quit (either the player "quit" or the game
2460 * completed), so leave the dialog loop.
2461 */
2462 if (!game->is_running)
2463 break;
2464
2465 /*
2466 * The game is still running, so confirm quit by requiring a repeat,
2467 * or if this is the confirmation, force the game to a halt.
2468 */
2469 if (!debug->quit_pending)
2470 {
2471 if_print_debug ("Use 'quit' again to confirm, or another"
2472 " debugger command to cancel.\n");
2473 debug->quit_pending = TRUE;
2474 continue;
2475 }
2476
2477 /* Drop printfilter contents and quit the game. */
2478 pf_empty (filter);
2479 run_quit (game);
2480
2481 /* Just in case... */
2482 if_print_debug ("Unable to quit from the game. Sorry.\n");
2483 continue;
2484 }
2485
2486 /* Dispatch the remaining debugging commands, and clear quit flag. */
2487 debug_dispatch (game, command, type, arg1, arg2, help_topic);
2488 debug->quit_pending = FALSE;
2489 }
2490
2491 /* Restart time, and clear any pending game quit. */
2492 var_set_elapsed_seconds (vars, debug->elapsed_seconds);
2493 debug->quit_pending = FALSE;
2494 }
2495
2496
2497 /*
2498 * debug_run_command()
2499 *
2500 * Handle a single debugging command line from the outside world. Returns
2501 * TRUE if valid, FALSE if invalid (parse failed, not understood).
2502 */
2503 sc_bool
2504 debug_run_command (sc_gameref_t game, const sc_char *debug_command)
2505 {
2506 const sc_debuggerref_t debug = debug_get_debugger (game);
2507 sc_command_t command, help_topic;
2508 sc_command_type_t type;
2509 sc_int arg1, arg2;
2510
2511 /* If debugging disallowed (not initialized), refuse the call. */
2512 if (debug)
2513 {
2514 /*
2515 * Parse the command string passed in, and return FALSE if the parse
2516 * fails, or if it returns DEBUG_CONTINUE, DEBUG_STEP, or DEBUG_QUIT,
2517 * none of which make any sense in this context.
2518 */
2519 command = debug_parse_command (debug_command,
2520 &type, &arg1, &arg2, &help_topic);
2521 if (command == DEBUG_NONE
2522 || command == DEBUG_CONTINUE || command == DEBUG_STEP
2523 || command == DEBUG_QUIT)
2524 return FALSE;
2525
2526 /* Dispatch the remaining debugging commands, return successfully. */
2527 debug_dispatch (game, command, type, arg1, arg2, help_topic);
2528 return TRUE;
2529 }
2530
2531 return FALSE;
2532 }
2533
2534
2535 /*
2536 * debug_cmd_debugger()
2537 *
2538 * Called by the run main loop on user "debug" command request. Prints
2539 * a polite refusal if debugging is not enabled, otherwise runs a debugging
2540 * dialog. Uses if_print_string() as this isn't debug output.
2541 */
2542 sc_bool
2543 debug_cmd_debugger (sc_gameref_t game)
2544 {
2545 const sc_debuggerref_t debug = debug_get_debugger (game);
2546
2547 /* If debugging disallowed (not initialized), ignore the call. */
2548 if (debug)
2549 debug_dialog (game);
2550 else
2551 if_print_string ("SCARE's game debugger is not enabled. Sorry.\n");
2552
2553 /*
2554 * Set as administrative command, so as not to consume a game turn, and
2555 * return successfully.
2556 */
2557 game->is_admin = TRUE;
2558 return TRUE;
2559 }
2560
2561
2562 /*
2563 * debug_game_started()
2564 * debug_game_ended()
2565 *
2566 * The first is called on entry to the game main loop, and gives us a chance
2567 * to look at things before any turns are run, and to set watchpoints to
2568 * catch things in games that use catch-all command tasks on startup (The PK
2569 * Girl, for example).
2570 *
2571 * The second is called on exit from the game, and may make a final sweep for
2572 * watchpoints and offer the debug dialog one last time.
2573 */
2574 void
2575 debug_game_started (sc_gameref_t game)
2576 {
2577 const sc_debuggerref_t debug = debug_get_debugger (game);
2578
2579 /* If debugging disallowed (not initialized), ignore the call. */
2580 if (debug)
2581 {
2582 /* Starting a new game, or a restore or undo of an old one? */
2583 if (!gs_room_seen (game, gs_playerroom (game)))
2584 {
2585 /*
2586 * It's a new game starting or restarting. Print a banner, and
2587 * run the debugger dialog.
2588 */
2589 if_print_debug ("\n--- SCARE " SCARE_VERSION SCARE_PATCH_LEVEL
2590 " Game Debugger\n"
2591 "--- Type 'help' for a list of commands.\n");
2592 debug_dialog (game);
2593 }
2594 else
2595 {
2596 /*
2597 * It's a restore or undo through memos, so run the dialog only if
2598 * single-stepping; no need to check watchpoints for this case as
2599 * none can be set -- no undo.
2600 */
2601 if (debug->single_step)
2602 debug_dialog (game);
2603 }
2604 }
2605 }
2606
2607 void
2608 debug_game_ended (sc_gameref_t game)
2609 {
2610 const sc_debuggerref_t debug = debug_get_debugger (game);
2611
2612 /* If debugging disallowed (not initialized), ignore the call. */
2613 if (debug)
2614 {
2615 /*
2616 * Using our carnal knowledge of the run main loop, we know here that
2617 * if the loop exited with do_restart or do_restore, we'll get a call to
2618 * debug_game_start() when the loop restarts. So in this case, ignore
2619 * the call (even if single stepping).
2620 */
2621 if (game->do_restart || game->do_restore)
2622 return;
2623
2624 /*
2625 * Check for any final watchpoints, and print a message describing why
2626 * we're here. Suppress the check for watchpoints if the user exited
2627 * the game, as it'll only be a repeat of any found last turn update.
2628 */
2629 if (!game->is_running)
2630 {
2631 if (game->has_completed)
2632 {
2633 debug_check_watchpoints (game);
2634 if_print_debug ("\n--- The game has completed.\n");
2635 }
2636 else
2637 if_print_debug ("\n--- The game has exited.\n");
2638 }
2639 else
2640 {
2641 debug_check_watchpoints (game);
2642 if_print_debug ("\n--- The game is still running!\n");
2643 }
2644
2645 /* Run a final dialog. */
2646 debug_dialog (game);
2647 }
2648 }
2649
2650
2651 /*
2652 * debug_turn_update()
2653 *
2654 * Called after each turn by the main game loop. Checks for any set
2655 * watchpoints, and triggers a debug dialog when any fire.
2656 */
2657 void
2658 debug_turn_update (sc_gameref_t game)
2659 {
2660 const sc_debuggerref_t debug = debug_get_debugger (game);
2661
2662 /* If debugging disallowed (not initialized), ignore the call. */
2663 if (debug)
2664 {
2665 /*
2666 * Again using carnal knowledge of the run main loop, if we're in
2667 * mid-wait, ignore the call. Also, ignore the call if the game is
2668 * no longer running, as we'll see a debug_game_ended() call come
2669 * along to handle that.
2670 */
2671 if (game->waitcounter > 0 || !game->is_running)
2672 return;
2673
2674 /*
2675 * Run debugger dialog if any watchpoints triggered, or if single
2676 * stepping (even if none triggered).
2677 */
2678 if (debug_check_watchpoints (game) || debug->single_step)
2679 debug_dialog (game);
2680 }
2681 }
2682
2683
2684 /*
2685 * debug_set_enabled()
2686 * debug_get_enabled()
2687 *
2688 * Enable/disable debugging, and return debugging status. Debugging is
2689 * enabled when there is a debugger reference in the game, and disabled
2690 * when it's NULL -- that's the flag. To avoid lugging about all the
2691 * watchpoint memory with a game, debugger data is allocated on enabling,
2692 * and free'd on disabling; as a result, any set watchpoints are lost on
2693 * disabling.
2694 */
2695 void
2696 debug_set_enabled (sc_gameref_t game, sc_bool enable)
2697 {
2698 const sc_debuggerref_t debug = debug_get_debugger (game);
2699
2700 /*
2701 * If enabling and not already enabled, or disabling and not already
2702 * disabled, either initialize or finalize..
2703 */
2704 if ((enable && !debug) || (!enable && debug))
2705 {
2706 /* Initialize or finalize debugging, as appropriate. */
2707 if (enable)
2708 debug_initialize (game);
2709 else
2710 debug_finalize (game);
2711 }
2712 }
2713
2714 sc_bool
2715 debug_get_enabled (sc_gameref_t game)
2716 {
2717 const sc_debuggerref_t debug = debug_get_debugger (game);
2718
2719 return debug != NULL;
2720 }