Debian Codegraph @

terps/scare/scgamest.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 <stdlib.h>
28 #include <string.h>
29
30 #include "scare.h"
31 #include "scprotos.h"
32 #include "scgamest.h"
33
34
35 /* Assorted definitions and constants. */
36 static const sc_uint GAME_MAGIC = 0x35aed26e;
37
38
39 /*
40 * gs_move_player_to_room()
41 * gs_player_in_room()
42 *
43 * Move the player to a given room, and check presence in a given room.
44 */
45 void
46 gs_move_player_to_room (sc_gameref_t game, sc_int room)
47 {
48 assert (gs_is_game_valid (game));
49
50 if (room < 0)
51 {
52 sc_fatal ("gs_move_player_to_room: invalid room, %ld\n", room);
53 return;
54 }
55 else if (room < game->room_count)
56 game->playerroom = room;
57 else
58 game->playerroom = lib_random_roomgroup_member (game,
59 room - game->room_count);
60
61 game->playerparent = -1;
62 game->playerposition = 0;
63 }
64
65 sc_bool
66 gs_player_in_room (sc_gameref_t game, sc_int room)
67 {
68 assert (gs_is_game_valid (game));
69 return game->playerroom == room;
70 }
71
72
73 /*
74 * gs_in_range()
75 *
76 * Helper for event, room, object, and npc range assertions.
77 */
78 static sc_bool
79 gs_in_range (sc_int value, sc_int limit)
80 {
81 return value >= 0 && value < limit;
82 }
83
84
85 /*
86 * gs_*()
87 *
88 * Game accessors and mutators.
89 */
90 sc_var_setref_t
91 gs_get_vars (sc_gameref_t gs)
92 {
93 assert (gs_is_game_valid (gs));
94 return gs->vars;
95 }
96
97 sc_prop_setref_t
98 gs_get_bundle (sc_gameref_t gs)
99 {
100 assert (gs_is_game_valid (gs));
101 return gs->bundle;
102 }
103
104 sc_filterref_t
105 gs_get_filter (sc_gameref_t gs)
106 {
107 assert (gs_is_game_valid (gs));
108 return gs->filter;
109 }
110
111 sc_memo_setref_t
112 gs_get_memento (sc_gameref_t gs)
113 {
114 assert (gs_is_game_valid (gs));
115 return gs->memento;
116 }
117
118
119 /*
120 * Game accessors and mutators for the player.
121 */
122 void
123 gs_set_playerroom (sc_gameref_t gs, sc_int room)
124 {
125 assert (gs_is_game_valid (gs));
126 gs->playerroom = room;
127 }
128
129 void
130 gs_set_playerposition (sc_gameref_t gs, sc_int position)
131 {
132 assert (gs_is_game_valid (gs));
133 gs->playerposition = position;
134 }
135
136 void
137 gs_set_playerparent (sc_gameref_t gs, sc_int parent)
138 {
139 assert (gs_is_game_valid (gs));
140 gs->playerparent = parent;
141 }
142
143 sc_int
144 gs_playerroom (sc_gameref_t gs)
145 {
146 assert (gs_is_game_valid (gs));
147 return gs->playerroom;
148 }
149
150 sc_int
151 gs_playerposition (sc_gameref_t gs)
152 {
153 assert (gs_is_game_valid (gs));
154 return gs->playerposition;
155 }
156
157 sc_int
158 gs_playerparent (sc_gameref_t gs)
159 {
160 assert (gs_is_game_valid (gs));
161 return gs->playerparent;
162 }
163
164
165 /*
166 * Game accessors and mutators for events.
167 */
168 sc_int
169 gs_event_count (sc_gameref_t gs)
170 {
171 assert (gs_is_game_valid (gs));
172 return gs->event_count;
173 }
174
175 void
176 gs_set_event_state (sc_gameref_t gs, sc_int event, sc_int state)
177 {
178 assert (gs_is_game_valid (gs) && gs_in_range (event, gs->event_count));
179 gs->events[event].state = state;
180 }
181
182 void
183 gs_set_event_time (sc_gameref_t gs, sc_int event, sc_int etime)
184 {
185 assert (gs_is_game_valid (gs) && gs_in_range (event, gs->event_count));
186 gs->events[event].time = etime;
187 }
188
189 sc_int
190 gs_event_state (sc_gameref_t gs, sc_int event)
191 {
192 assert (gs_is_game_valid (gs) && gs_in_range (event, gs->event_count));
193 return gs->events[event].state;
194 }
195
196 sc_int
197 gs_event_time (sc_gameref_t gs, sc_int event)
198 {
199 assert (gs_is_game_valid (gs) && gs_in_range (event, gs->event_count));
200 return gs->events[event].time;
201 }
202
203 void
204 gs_decrement_event_time (sc_gameref_t gs, sc_int event)
205 {
206 assert (gs_is_game_valid (gs) && gs_in_range (event, gs->event_count));
207 gs->events[event].time--;
208 }
209
210
211 /*
212 * Game accessors and mutators for rooms.
213 */
214 sc_int
215 gs_room_count (sc_gameref_t gs)
216 {
217 assert (gs_is_game_valid (gs));
218 return gs->room_count;
219 }
220
221 void
222 gs_set_room_seen (sc_gameref_t gs, sc_int room, sc_bool seen)
223 {
224 assert (gs_is_game_valid (gs) && gs_in_range (room, gs->room_count));
225 gs->rooms[room].visited = seen;
226 }
227
228 sc_bool
229 gs_room_seen (sc_gameref_t gs, sc_int room)
230 {
231 assert (gs_is_game_valid (gs) && gs_in_range (room, gs->room_count));
232 return gs->rooms[room].visited;
233 }
234
235
236 /*
237 * Game accessors and mutators for tasks.
238 */
239 sc_int
240 gs_task_count (sc_gameref_t gs)
241 {
242 assert (gs_is_game_valid (gs));
243 return gs->task_count;
244 }
245
246 void
247 gs_set_task_done (sc_gameref_t gs, sc_int task, sc_bool done)
248 {
249 assert (gs_is_game_valid (gs) && gs_in_range (task, gs->task_count));
250 gs->tasks[task].done = done;
251 }
252
253 void
254 gs_set_task_scored (sc_gameref_t gs, sc_int task, sc_bool scored)
255 {
256 assert (gs_is_game_valid (gs) && gs_in_range (task, gs->task_count));
257 gs->tasks[task].scored = scored;
258 }
259
260 sc_bool
261 gs_task_done (sc_gameref_t gs, sc_int task)
262 {
263 assert (gs_is_game_valid (gs) && gs_in_range (task, gs->task_count));
264 return gs->tasks[task].done;
265 }
266
267 sc_bool
268 gs_task_scored (sc_gameref_t gs, sc_int task)
269 {
270 assert (gs_is_game_valid (gs) && gs_in_range (task, gs->task_count));
271 return gs->tasks[task].scored;
272 }
273
274
275 /*
276 * Game accessors and mutators for objects.
277 */
278 sc_int
279 gs_object_count (sc_gameref_t gs)
280 {
281 assert (gs_is_game_valid (gs));
282 return gs->object_count;
283 }
284
285 void
286 gs_set_object_openness (sc_gameref_t gs, sc_int object, sc_int openness)
287 {
288 assert (gs_is_game_valid (gs) && gs_in_range (object, gs->object_count));
289 gs->objects[object].openness = openness;
290 }
291
292 void
293 gs_set_object_state (sc_gameref_t gs, sc_int object, sc_int state)
294 {
295 assert (gs_is_game_valid (gs) && gs_in_range (object, gs->object_count));
296 gs->objects[object].state = state;
297 }
298
299 void
300 gs_set_object_seen (sc_gameref_t gs, sc_int object, sc_bool seen)
301 {
302 assert (gs_is_game_valid (gs) && gs_in_range (object, gs->object_count));
303 gs->objects[object].seen = seen;
304 }
305
306 void
307 gs_set_object_unmoved (sc_gameref_t gs, sc_int object, sc_bool unmoved)
308 {
309 assert (gs_is_game_valid (gs) && gs_in_range (object, gs->object_count));
310 gs->objects[object].unmoved = unmoved;
311 }
312
313 void
314 gs_set_object_static_unmoved (sc_gameref_t gs, sc_int object, sc_bool unmoved)
315 {
316 assert (gs_is_game_valid (gs) && gs_in_range (object, gs->object_count));
317 gs->objects[object].static_unmoved = unmoved;
318 }
319
320 sc_int
321 gs_object_openness (sc_gameref_t gs, sc_int object)
322 {
323 assert (gs_is_game_valid (gs) && gs_in_range (object, gs->object_count));
324 return gs->objects[object].openness;
325 }
326
327 sc_int
328 gs_object_state (sc_gameref_t gs, sc_int object)
329 {
330 assert (gs_is_game_valid (gs) && gs_in_range (object, gs->object_count));
331 return gs->objects[object].state;
332 }
333
334 sc_bool
335 gs_object_seen (sc_gameref_t gs, sc_int object)
336 {
337 assert (gs_is_game_valid (gs) && gs_in_range (object, gs->object_count));
338 return gs->objects[object].seen;
339 }
340
341 sc_bool
342 gs_object_unmoved (sc_gameref_t gs, sc_int object)
343 {
344 assert (gs_is_game_valid (gs) && gs_in_range (object, gs->object_count));
345 return gs->objects[object].unmoved;
346 }
347
348 sc_bool
349 gs_object_static_unmoved (sc_gameref_t gs, sc_int object)
350 {
351 assert (gs_is_game_valid (gs) && gs_in_range (object, gs->object_count));
352 return gs->objects[object].static_unmoved;
353 }
354
355 sc_int
356 gs_object_position (sc_gameref_t gs, sc_int object)
357 {
358 assert (gs_is_game_valid (gs) && gs_in_range (object, gs->object_count));
359 return gs->objects[object].position;
360 }
361
362 sc_int
363 gs_object_parent (sc_gameref_t gs, sc_int object)
364 {
365 assert (gs_is_game_valid (gs) && gs_in_range (object, gs->object_count));
366 return gs->objects[object].parent;
367 }
368
369 static void
370 gs_object_move_onto_unchecked (sc_gameref_t gs, sc_int object, sc_int onto)
371 {
372 assert (gs_is_game_valid (gs) && gs_in_range (object, gs->object_count));
373 gs->objects[object].position = OBJ_ON_OBJECT;
374 gs->objects[object].parent = onto;
375 }
376
377 static void
378 gs_object_move_into_unchecked (sc_gameref_t gs, sc_int object, sc_int into)
379 {
380 assert (gs_is_game_valid (gs) && gs_in_range (object, gs->object_count));
381 gs->objects[object].position = OBJ_IN_OBJECT;
382 gs->objects[object].parent = into;
383 }
384
385 static void
386 gs_object_make_hidden_unchecked (sc_gameref_t gs, sc_int object)
387 {
388 assert (gs_is_game_valid (gs) && gs_in_range (object, gs->object_count));
389 gs->objects[object].position = OBJ_HIDDEN;
390 gs->objects[object].parent = -1;
391 }
392
393 static void
394 gs_object_player_get_unchecked (sc_gameref_t gs, sc_int object)
395 {
396 assert (gs_is_game_valid (gs) && gs_in_range (object, gs->object_count));
397 gs->objects[object].position = OBJ_HELD_PLAYER;
398 gs->objects[object].parent = -1;
399 }
400
401 static void
402 gs_object_npc_get_unchecked (sc_gameref_t gs, sc_int object, sc_int npc)
403 {
404 assert (gs_is_game_valid (gs) && gs_in_range (object, gs->object_count));
405 gs->objects[object].position = OBJ_HELD_NPC;
406 gs->objects[object].parent = npc;
407 }
408
409 static void
410 gs_object_player_wear_unchecked (sc_gameref_t gs, sc_int object)
411 {
412 assert (gs_is_game_valid (gs) && gs_in_range (object, gs->object_count));
413 gs->objects[object].position = OBJ_WORN_PLAYER;
414 gs->objects[object].parent = 0;
415 }
416
417 static void
418 gs_object_npc_wear_unchecked (sc_gameref_t gs, sc_int object, sc_int npc)
419 {
420 assert (gs_is_game_valid (gs) && gs_in_range (object, gs->object_count));
421 gs->objects[object].position = OBJ_WORN_NPC;
422 gs->objects[object].parent = npc;
423 }
424
425 static void
426 gs_object_to_room_unchecked (sc_gameref_t gs, sc_int object, sc_int room)
427 {
428 assert (gs_is_game_valid (gs) && gs_in_range (object, gs->object_count));
429 gs->objects[object].position = room + 1;
430 gs->objects[object].parent = -1;
431 }
432
433 void
434 gs_object_move_onto (sc_gameref_t gs, sc_int object, sc_int onto)
435 {
436 assert (gs_is_game_valid (gs) && gs_in_range (object, gs->object_count));
437 if (gs->objects[object].position != OBJ_ON_OBJECT
438 || gs->objects[object].parent != onto)
439 {
440 gs_object_move_onto_unchecked (gs, object, onto);
441 gs->objects[object].unmoved = FALSE;
442 }
443 }
444
445 void
446 gs_object_move_into (sc_gameref_t gs, sc_int object, sc_int into)
447 {
448 assert (gs_is_game_valid (gs) && gs_in_range (object, gs->object_count));
449 if (gs->objects[object].position != OBJ_IN_OBJECT
450 || gs->objects[object].parent != into)
451 {
452 gs_object_move_into_unchecked (gs, object, into);
453 gs->objects[object].unmoved = FALSE;
454 }
455 }
456
457 void
458 gs_object_make_hidden (sc_gameref_t gs, sc_int object)
459 {
460 assert (gs_is_game_valid (gs) && gs_in_range (object, gs->object_count));
461 if (gs->objects[object].position != OBJ_HIDDEN)
462 {
463 gs_object_make_hidden_unchecked (gs, object);
464 gs->objects[object].unmoved = FALSE;
465 }
466 }
467
468 void
469 gs_object_player_get (sc_gameref_t gs, sc_int object)
470 {
471 assert (gs_is_game_valid (gs) && gs_in_range (object, gs->object_count));
472 if (gs->objects[object].position != OBJ_HELD_PLAYER)
473 {
474 gs_object_player_get_unchecked (gs, object);
475 gs->objects[object].unmoved = FALSE;
476 }
477 }
478
479 void
480 gs_object_npc_get (sc_gameref_t gs, sc_int object, sc_int npc)
481 {
482 assert (gs_is_game_valid (gs) && gs_in_range (object, gs->object_count));
483 if (gs->objects[object].position != OBJ_HELD_NPC
484 || gs->objects[object].parent != npc)
485 {
486 gs_object_npc_get_unchecked (gs, object, npc);
487 gs->objects[object].unmoved = FALSE;
488 }
489 }
490
491 void
492 gs_object_player_wear (sc_gameref_t gs, sc_int object)
493 {
494 assert (gs_is_game_valid (gs) && gs_in_range (object, gs->object_count));
495 if (gs->objects[object].position != OBJ_WORN_PLAYER)
496 {
497 gs_object_player_wear_unchecked (gs, object);
498 gs->objects[object].unmoved = FALSE;
499 }
500 }
501
502 void
503 gs_object_npc_wear (sc_gameref_t gs, sc_int object, sc_int npc)
504 {
505 assert (gs_is_game_valid (gs) && gs_in_range (object, gs->object_count));
506 if (gs->objects[object].position != OBJ_WORN_NPC
507 || gs->objects[object].parent != npc)
508 {
509 gs_object_npc_wear_unchecked (gs, object, npc);
510 gs->objects[object].unmoved = FALSE;
511 }
512 }
513
514 void
515 gs_object_to_room (sc_gameref_t gs, sc_int object, sc_int room)
516 {
517 assert (gs_is_game_valid (gs) && gs_in_range (object, gs->object_count));
518 if (gs->objects[object].position != room + 1)
519 {
520 gs_object_to_room_unchecked (gs, object, room);
521 gs->objects[object].unmoved = FALSE;
522 }
523 }
524
525
526 /*
527 * Game accessors and mutators for NPCs.
528 */
529 sc_int
530 gs_npc_count (sc_gameref_t gs)
531 {
532 assert (gs_is_game_valid (gs));
533 return gs->npc_count;
534 }
535
536 void
537 gs_set_npc_location (sc_gameref_t gs, sc_int npc, sc_int location)
538 {
539 assert (gs_is_game_valid (gs) && gs_in_range (npc, gs->npc_count));
540 gs->npcs[npc].location = location;
541 }
542
543 sc_int
544 gs_npc_location (sc_gameref_t gs, sc_int npc)
545 {
546 assert (gs_is_game_valid (gs) && gs_in_range (npc, gs->npc_count));
547 return gs->npcs[npc].location;
548 }
549
550 void
551 gs_set_npc_position (sc_gameref_t gs, sc_int npc, sc_int position)
552 {
553 assert (gs_is_game_valid (gs) && gs_in_range (npc, gs->npc_count));
554 gs->npcs[npc].position = position;
555 }
556
557 sc_int
558 gs_npc_position (sc_gameref_t gs, sc_int npc)
559 {
560 assert (gs_is_game_valid (gs) && gs_in_range (npc, gs->npc_count));
561 return gs->npcs[npc].position;
562 }
563
564 void
565 gs_set_npc_parent (sc_gameref_t gs, sc_int npc, sc_int parent)
566 {
567 assert (gs_is_game_valid (gs) && gs_in_range (npc, gs->npc_count));
568 gs->npcs[npc].parent = parent;
569 }
570
571 sc_int
572 gs_npc_parent (sc_gameref_t gs, sc_int npc)
573 {
574 assert (gs_is_game_valid (gs) && gs_in_range (npc, gs->npc_count));
575 return gs->npcs[npc].parent;
576 }
577
578 void
579 gs_set_npc_seen (sc_gameref_t gs, sc_int npc, sc_bool seen)
580 {
581 assert (gs_is_game_valid (gs) && gs_in_range (npc, gs->npc_count));
582 gs->npcs[npc].seen = seen;
583 }
584
585 sc_bool
586 gs_npc_seen (sc_gameref_t gs, sc_int npc)
587 {
588 assert (gs_is_game_valid (gs) && gs_in_range (npc, gs->npc_count));
589 return gs->npcs[npc].seen;
590 }
591
592 sc_int
593 gs_npc_walkstep_count (sc_gameref_t gs, sc_int npc)
594 {
595 assert (gs_is_game_valid (gs) && gs_in_range (npc, gs->npc_count));
596 return gs->npcs[npc].walkstep_count;
597 }
598
599 void
600 gs_set_npc_walkstep (sc_gameref_t gs,
601 sc_int npc, sc_int walk, sc_int walkstep)
602 {
603 assert (gs_is_game_valid (gs) && gs_in_range (npc, gs->npc_count)
604 && gs_in_range (walk, gs->npcs[npc].walkstep_count));
605 gs->npcs[npc].walksteps[walk] = walkstep;
606 }
607
608 sc_int
609 gs_npc_walkstep (sc_gameref_t gs, sc_int npc, sc_int walk)
610 {
611 assert (gs_is_game_valid (gs) && gs_in_range (npc, gs->npc_count)
612 && gs_in_range (walk, gs->npcs[npc].walkstep_count));
613 return gs->npcs[npc].walksteps[walk];
614 }
615
616 void
617 gs_decrement_npc_walkstep (sc_gameref_t gs, sc_int npc, sc_int walk)
618 {
619 assert (gs_is_game_valid (gs) && gs_in_range (npc, gs->npc_count)
620 && gs_in_range (walk, gs->npcs[npc].walkstep_count));
621 gs->npcs[npc].walksteps[walk]--;
622 }
623
624
625 /*
626 * Convenience functions for bulk clearance of references.
627 */
628 void
629 gs_clear_npc_references (sc_gameref_t gs)
630 {
631 assert (gs_is_game_valid (gs));
632 memset (gs->npc_references,
633 FALSE, gs->npc_count * sizeof (*gs->npc_references));
634 }
635
636 void
637 gs_clear_object_references (sc_gameref_t gs)
638 {
639 assert (gs_is_game_valid (gs));
640 memset (gs->object_references,
641 FALSE, gs->object_count * sizeof (*gs->object_references));
642 }
643
644 void
645 gs_set_multiple_references (sc_gameref_t gs)
646 {
647 assert (gs_is_game_valid (gs));
648 memset (gs->multiple_references,
649 TRUE, gs->object_count * sizeof (*gs->multiple_references));
650 }
651
652 void
653 gs_clear_multiple_references (sc_gameref_t gs)
654 {
655 assert (gs_is_game_valid (gs));
656 memset (gs->multiple_references,
657 FALSE, gs->object_count * sizeof (*gs->multiple_references));
658 }
659
660
661 /*
662 * gs_create()
663 *
664 * Create and initialize a game state.
665 */
666 sc_gameref_t
667 gs_create (sc_var_setref_t vars,
668 sc_prop_setref_t bundle, sc_filterref_t filter)
669 {
670 sc_gameref_t game;
671 sc_vartype_t vt_key[4];
672 sc_int index_, bytes;
673 assert (vars && bundle && filter);
674
675 /* Create the initial state structure. */
676 game = sc_malloc (sizeof (*game));
677 game->magic = GAME_MAGIC;
678
679 /* Store the variables, properties bundle, and filter references. */
680 game->vars = vars;
681 game->bundle = bundle;
682 game->filter = filter;
683
684 /* Set memento to NULL for now; it's added later. */
685 game->memento = NULL;
686
687 /* Initialize for no debugger. */
688 game->debugger = NULL;
689
690 /* Initialize the undo buffers to NULL for now. */
691 game->temporary = NULL;
692 game->undo = NULL;
693 game->undo_available = FALSE;
694
695 /* Create rooms state array. */
696 vt_key[0].string = "Rooms";
697 game->room_count = prop_get_child_count (bundle, "I<-s", vt_key);
698 game->rooms = sc_malloc (game->room_count * sizeof (*game->rooms));
699
700 /* Set up initial rooms states. */
701 for (index_ = 0; index_ < game->room_count; index_++)
702 gs_set_room_seen (game, index_, FALSE);
703
704 /* Create objects state array. */
705 vt_key[0].string = "Objects";
706 game->object_count = prop_get_child_count (bundle, "I<-s", vt_key);
707 game->objects = sc_malloc (game->object_count * sizeof (*game->objects));
708
709 /* Set up initial object states. */
710 for (index_ = 0; index_ < game->object_count; index_++)
711 {
712 const sc_char *inroomdesc;
713 sc_bool is_static, unmoved;
714
715 vt_key[1].integer = index_;
716
717 vt_key[2].string = "Static";
718 is_static = prop_get_boolean (bundle, "B<-sis", vt_key);
719 if (is_static)
720 {
721 sc_int type;
722
723 vt_key[2].string = "Where";
724 vt_key[3].string = "Type";
725 type = prop_get_integer (bundle, "I<-siss", vt_key);
726 if (type == ROOMLIST_NPC_PART)
727 {
728 sc_int parent;
729
730 game->objects[index_].position = OBJ_PART_NPC;
731
732 vt_key[2].string = "Parent";
733 parent = prop_get_integer (bundle, "I<-sis", vt_key) - 1;
734 game->objects[index_].parent = parent;
735 }
736 else
737 gs_object_make_hidden_unchecked (game, index_);
738 }
739 else
740 {
741 sc_int initialparent, initialposition;
742
743 vt_key[2].string = "Parent";
744 initialparent = prop_get_integer (bundle, "I<-sis", vt_key);
745 vt_key[2].string = "InitialPosition";
746 initialposition = prop_get_integer (bundle, "I<-sis", vt_key);
747 switch (initialposition)
748 {
749 case 0: /* Hidden. */
750 gs_object_make_hidden_unchecked (game, index_);
751 break;
752
753 case 1: /* Held. */
754 if (initialparent == 0) /* By player. */
755 gs_object_player_get_unchecked (game, index_);
756 else /* By NPC. */
757 gs_object_npc_get_unchecked (game, index_, initialparent - 1);
758 break;
759
760 case 2: /* In container. */
761 gs_object_move_into_unchecked (game, index_,
762 obj_container_object (game, initialparent));
763 break;
764
765 case 3: /* On surface. */
766 gs_object_move_onto_unchecked (game, index_,
767 obj_surface_object (game, initialparent));
768 break;
769
770 default: /* In room, or worn by player/NPC. */
771 if (initialposition >= 4
772 && initialposition < 4 + game->room_count)
773 {
774 gs_object_to_room_unchecked (game,
775 index_, initialposition - 4);
776 }
777 else if (initialposition == 4 + game->room_count)
778 {
779 if (initialparent == 0)
780 gs_object_player_wear_unchecked (game, index_);
781 else
782 gs_object_npc_wear_unchecked (game,
783 index_, initialparent - 1);
784 }
785 else
786 {
787 sc_error ("gs_create: object in out of bounds room, %ld\n",
788 initialposition - 4 - game->room_count);
789 gs_object_to_room_unchecked (game, index_, -2);
790 }
791 }
792 }
793
794 vt_key[2].string = "CurrentState";
795 gs_set_object_state (game, index_,
796 prop_get_integer (bundle, "I<-sis", vt_key));
797
798 vt_key[2].string = "Openable";
799 gs_set_object_openness (game, index_,
800 prop_get_integer (bundle, "I<-sis", vt_key));
801
802 gs_set_object_seen (game, index_, FALSE);
803
804 vt_key[2].string = "InRoomDesc";
805 inroomdesc = prop_get_string (bundle, "S<-sis", vt_key);
806 if (!sc_strempty (inroomdesc))
807 {
808 vt_key[2].string = "OnlyWhenNotMoved";
809 if (prop_get_integer (bundle, "I<-sis", vt_key) == 1)
810 unmoved = TRUE;
811 else
812 unmoved = FALSE;
813 }
814 else
815 unmoved = FALSE;
816 gs_set_object_unmoved (game, index_, unmoved);
817 gs_set_object_static_unmoved (game, index_, TRUE);
818 }
819
820 /* Create tasks state array. */
821 vt_key[0].string = "Tasks";
822 game->task_count = prop_get_child_count (bundle, "I<-s", vt_key);
823 game->tasks = sc_malloc (game->task_count * sizeof (*game->tasks));
824
825 /* Set up initial tasks states. */
826 for (index_ = 0; index_ < game->task_count; index_++)
827 {
828 gs_set_task_done (game, index_, FALSE);
829 gs_set_task_scored (game, index_, FALSE);
830 }
831
832 /* Create events state array. */
833 vt_key[0].string = "Events";
834 game->event_count = prop_get_child_count (bundle, "I<-s", vt_key);
835 game->events = sc_malloc (game->event_count * sizeof (*game->events));
836
837 /* Set up initial events states. */
838 for (index_ = 0; index_ < game->event_count; index_++)
839 {
840 sc_int startertype;
841
842 vt_key[1].integer = index_;
843 vt_key[2].string = "StarterType";
844 startertype = prop_get_integer (bundle, "I<-sis", vt_key);
845
846 switch (startertype)
847 {
848 case 1:
849 gs_set_event_state (game, index_, ES_WAITING);
850 gs_set_event_time (game, index_, 0);
851 break;
852
853 case 2:
854 {
855 sc_int start, end;
856
857 gs_set_event_state (game, index_, ES_WAITING);
858 vt_key[2].string = "StartTime";
859 start = prop_get_integer (bundle, "I<-sis", vt_key);
860 vt_key[2].string = "EndTime";
861 end = prop_get_integer (bundle, "I<-sis", vt_key);
862 gs_set_event_time (game, index_, sc_randomint (start, end));
863 break;
864 }
865
866 case 3:
867 gs_set_event_state (game, index_, ES_AWAITING);
868 gs_set_event_time (game, index_, 0);
869 break;
870 }
871 }
872
873 /* Create NPCs state array. */
874 vt_key[0].string = "NPCs";
875 game->npc_count = prop_get_child_count (bundle, "I<-s", vt_key);
876 game->npcs = sc_malloc (game->npc_count * sizeof (*game->npcs));
877
878 /* Set up initial NPCs states. */
879 for (index_ = 0; index_ < game->npc_count; index_++)
880 {
881 sc_int walk, walkstep_count;
882
883 gs_set_npc_position (game, index_, 0);
884 gs_set_npc_parent (game, index_, -1);
885 gs_set_npc_seen (game, index_, FALSE);
886
887 vt_key[1].integer = index_;
888
889 vt_key[2].string = "StartRoom";
890 gs_set_npc_location (game, index_,
891 prop_get_integer (bundle, "I<-sis", vt_key));
892
893 vt_key[2].string = "Walks";
894 walkstep_count = prop_get_child_count (bundle, "I<-sis", vt_key);
895
896 game->npcs[index_].walkstep_count = walkstep_count;
897 game->npcs[index_].walksteps = sc_malloc (walkstep_count
898 * sizeof (*game->npcs[0].walksteps));
899
900 for (walk = 0; walk < walkstep_count; walk++)
901 gs_set_npc_walkstep (game, index_, walk, 0);
902 }
903
904 /* Set up the player portions of the game state. */
905 vt_key[0].string = "Header";
906 vt_key[1].string = "StartRoom";
907 game->playerroom = prop_get_integer (bundle, "I<-ss", vt_key);
908 vt_key[0].string = "Globals";
909 vt_key[1].string = "ParentObject";
910 game->playerparent = prop_get_integer (bundle, "I<-ss", vt_key) - 1;
911 vt_key[1].string = "Position";
912 game->playerposition = prop_get_integer (bundle, "I<-ss", vt_key);
913
914 /* Initialize score notifications from game properties. */
915 vt_key[0].string = "Globals";
916 vt_key[1].string = "NoScoreNotify";
917 game->notify_score_change = !prop_get_boolean (bundle, "B<-ss", vt_key);
918
919 /* Miscellaneous state defaults. */
920 game->turns = 0;
921 game->score = 0;
922 game->bold_room_names = TRUE;
923 game->verbose = FALSE;
924 game->current_room_name = NULL;
925 game->status_line = NULL;
926 game->title = NULL;
927 game->author = NULL;
928 game->hint_text = NULL;
929
930 /* Resource controls. */
931 res_clear_resource (&game->requested_sound);
932 res_clear_resource (&game->requested_graphic);
933 res_clear_resource (&game->playing_sound);
934 res_clear_resource (&game->displayed_graphic);
935 game->stop_sound = FALSE;
936 game->sound_active = FALSE;
937
938 /* Initialize wait turns from game properties. */
939 vt_key[0].string = "Globals";
940 vt_key[1].string = "WaitTurns";
941 game->waitturns = prop_get_integer (bundle, "I<-ss", vt_key);
942
943 /* Non-game conveniences. */
944 game->is_running = FALSE;
945 game->has_notified = FALSE;
946 game->is_admin = FALSE;
947 game->has_completed = FALSE;
948 game->waitcounter = 0;
949 game->do_again = FALSE;
950 game->redo_sequence = 0;
951 game->do_restart = FALSE;
952 game->do_restore = FALSE;
953
954 bytes = game->object_count * sizeof (*game->object_references);
955 game->object_references = sc_malloc (bytes);
956 memset (game->object_references, FALSE, bytes);
957 bytes = game->object_count * sizeof (*game->multiple_references);
958 game->multiple_references = sc_malloc (bytes);
959 memset (game->multiple_references, FALSE, bytes);
960
961 bytes = game->npc_count * sizeof (*game->npc_references);
962 game->npc_references = sc_malloc (bytes);
963 memset (game->npc_references, FALSE, bytes);
964
965 game->it_object = -1;
966 game->him_npc = -1;
967 game->her_npc = -1;
968 game->it_npc = -1;
969
970 /* Clear the quit jump buffer for tidiness. */
971 memset (&game->quitter, 0, sizeof (game->quitter));
972
973 /* Return the constructed game state. */
974 return game;
975 }
976
977
978 /*
979 * gs_is_game_valid()
980 *
981 * Return TRUE if pointer is a valid game, FALSE otherwise.
982 */
983 sc_bool
984 gs_is_game_valid (sc_gameref_t game)
985 {
986 return game && game->magic == GAME_MAGIC;
987 }
988
989
990 /*
991 * gs_string_copy()
992 *
993 * Helper for gs_copy(), copies one malloc'ed string to another, or NULL
994 * if from is NULL, taking care not to leak memory.
995 */
996 static void
997 gs_string_copy (sc_char **to_string, const sc_char *from_string)
998 {
999 /* Free any current contents of to_string. */
1000 sc_free (*to_string);
1001
1002 /* Copy from_string if set, otherwise set to_string to NULL. */
1003 if (from_string)
1004 {
1005 *to_string = sc_malloc (strlen (from_string) + 1);
1006 strcpy (*to_string, from_string);
1007 }
1008 else
1009 *to_string = NULL;
1010 }
1011
1012
1013 /*
1014 * gs_copy()
1015 *
1016 * Deep-copy the dynamic parts of a game onto another existing
1017 * game structure.
1018 */
1019 void
1020 gs_copy (sc_gameref_t to, sc_gameref_t from)
1021 {
1022 const sc_prop_setref_t bundle = from->bundle;
1023 sc_vartype_t vt_key[3];
1024 sc_int var_count, var, npc;
1025 assert (gs_is_game_valid (to) && gs_is_game_valid (from));
1026
1027 /*
1028 * Copy over references to the properties bundle and filter. The debugger
1029 * is specifically excluded, as it's considered to be tied to the game.
1030 */
1031 to->bundle = from->bundle;
1032 to->filter = from->filter;
1033
1034 /* Copy over references to the undo buffers. */
1035 to->temporary = from->temporary;
1036 to->undo = from->undo;
1037 to->undo_available = from->undo_available;
1038
1039 /* Copy over all variables values. */
1040 vt_key[0].string = "Variables";
1041 var_count = prop_get_child_count (bundle, "I<-s", vt_key);
1042
1043 for (var = 0; var < var_count; var++)
1044 {
1045 const sc_char *name;
1046 sc_int var_type;
1047
1048 vt_key[1].integer = var;
1049
1050 vt_key[2].string = "Name";
1051 name = prop_get_string (bundle, "S<-sis", vt_key);
1052 vt_key[2].string = "Type";
1053 var_type = prop_get_integer (bundle, "I<-sis", vt_key);
1054
1055 switch (var_type)
1056 {
1057 case TAFVAR_NUMERIC:
1058 var_put_integer (to->vars, name, var_get_integer (from->vars, name));
1059 break;
1060
1061 case TAFVAR_STRING:
1062 var_put_string (to->vars, name, var_get_string (from->vars, name));
1063 break;
1064
1065 default:
1066 sc_fatal ("gs_copy: unknown variable type, %ld\n", var_type);
1067 }
1068 }
1069
1070 /* Copy over the variable timestamp. */
1071 var_set_elapsed_seconds (to->vars, var_get_elapsed_seconds (from->vars));
1072
1073 /* Copy over room states. */
1074 assert (to->room_count == from->room_count);
1075 memcpy (to->rooms, from->rooms, from->room_count * sizeof (*from->rooms));
1076
1077 /* Copy over object states. */
1078 assert (to->object_count == from->object_count);
1079 memcpy (to->objects, from->objects,
1080 from->object_count * sizeof (*from->objects));
1081
1082 /* Copy over task states. */
1083 assert (to->task_count == from->task_count);
1084 memcpy (to->tasks, from->tasks, from->task_count * sizeof (*from->tasks));
1085
1086 /* Copy over event states. */
1087 assert (to->event_count == from->event_count);
1088 memcpy (to->events, from->events, from->event_count * sizeof (*from->events));
1089
1090 /* Copy over NPC states individually, to avoid walks problems. */
1091 for (npc = 0; npc < from->npc_count; npc++)
1092 {
1093 to->npcs[npc].location = from->npcs[npc].location;
1094 to->npcs[npc].position = from->npcs[npc].position;
1095 to->npcs[npc].parent = from->npcs[npc].parent;
1096 to->npcs[npc].seen = from->npcs[npc].seen;
1097 to->npcs[npc].walkstep_count = from->npcs[npc].walkstep_count;
1098
1099 /* Copy over NPC walks information. */
1100 assert (to->npcs[npc].walkstep_count == from->npcs[npc].walkstep_count);
1101 memcpy (to->npcs[npc].walksteps, from->npcs[npc].walksteps,
1102 from->npcs[npc].walkstep_count
1103 * sizeof (*from->npcs[npc].walksteps));
1104 }
1105
1106 /* Copy over player information. */
1107 to->playerroom = from->playerroom;
1108 to->playerposition = from->playerposition;
1109 to->playerparent = from->playerparent;
1110
1111 /*
1112 * Copy over miscellaneous other details. Specifically exclude bold rooms,
1113 * verbose, and score notification, so that they are invariant across copies,
1114 * particularly undo/restore.
1115 */
1116 to->turns = from->turns;
1117 to->score = from->score;
1118
1119 gs_string_copy (&to->current_room_name, from->current_room_name);
1120 gs_string_copy (&to->status_line, from->status_line);
1121 gs_string_copy (&to->title, from->title);
1122 gs_string_copy (&to->author, from->author);
1123 gs_string_copy (&to->hint_text, from->hint_text);
1124
1125 /*
1126 * Specifically exclude playing sound and displayed graphic from the copy
1127 * so that they remain invariant across game copies.
1128 */
1129 to->requested_sound = from->requested_sound;
1130 to->requested_graphic = from->requested_graphic;
1131 to->stop_sound = from->stop_sound;
1132
1133 to->is_running = from->is_running;
1134 to->has_notified = from->has_notified;
1135 to->is_admin = from->is_admin;
1136 to->has_completed = from->has_completed;
1137
1138 to->waitturns = from->waitturns;
1139
1140 to->waitcounter = from->waitcounter;
1141 to->do_again = from->do_again;
1142 to->redo_sequence = from->redo_sequence;
1143 to->do_restart = from->do_restart;
1144 to->do_restore = from->do_restore;
1145
1146 memcpy (to->object_references, from->object_references,
1147 from->object_count * sizeof (*from->object_references));
1148 memcpy (to->multiple_references, from->multiple_references,
1149 from->object_count * sizeof (*from->multiple_references));
1150 memcpy (to->npc_references, from->npc_references,
1151 from->npc_count * sizeof (*from->npc_references));
1152
1153 to->it_object = from->it_object;
1154 to->him_npc = from->him_npc;
1155 to->her_npc = from->her_npc;
1156 to->it_npc = from->it_npc;
1157
1158 /* Copy over the quit jump buffer. */
1159 memcpy (&to->quitter, &from->quitter, sizeof (from->quitter));
1160 }
1161
1162
1163 /*
1164 * gs_destroy()
1165 *
1166 * Free all the memory associated with a game state.
1167 */
1168 void
1169 gs_destroy (sc_gameref_t game)
1170 {
1171 sc_int npc;
1172 assert (gs_is_game_valid (game));
1173
1174 /* Free the malloc'ed state arrays. */
1175 sc_free (game->rooms);
1176 sc_free (game->objects);
1177 sc_free (game->tasks);
1178 sc_free (game->events);
1179 for (npc = 0; npc < game->npc_count; npc++)
1180 sc_free (game->npcs[npc].walksteps);
1181 sc_free (game->npcs);
1182
1183 /* Free the malloc'ed object and NPC references. */
1184 sc_free (game->object_references);
1185 sc_free (game->multiple_references);
1186 sc_free (game->npc_references);
1187
1188 /* Free malloc'ed game strings. */
1189 sc_free (game->current_room_name);
1190 sc_free (game->status_line);
1191 sc_free (game->title);
1192 sc_free (game->author);
1193 sc_free (game->hint_text);
1194
1195 /* Poison and free the game state itself. */
1196 memset (game, 0xaa, sizeof (*game));
1197 sc_free (game);
1198 }