Debian Codegraph @

terps/scare/scobjcts.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_char NUL = '\0';
37
38 /* Trace flag, set before running. */
39 static sc_bool obj_trace = FALSE;
40
41
42 /*
43 * obj_is_static()
44 * obj_is_surface()
45 * obj_is_container()
46 *
47 * Convenience functions to return TRUE for given object attributes.
48 */
49 sc_bool
50 obj_is_static (sc_gameref_t game, sc_int object)
51 {
52 const sc_prop_setref_t bundle = gs_get_bundle (game);
53 sc_vartype_t vt_key[3];
54 sc_bool bstatic;
55
56 vt_key[0].string = "Objects";
57 vt_key[1].integer = object;
58 vt_key[2].string = "Static";
59 bstatic = prop_get_boolean (bundle, "B<-sis", vt_key);
60 return bstatic;
61 }
62
63 sc_bool
64 obj_is_container (sc_gameref_t game, sc_int object)
65 {
66 const sc_prop_setref_t bundle = gs_get_bundle (game);
67 sc_vartype_t vt_key[3];
68 sc_bool is_container;
69
70 vt_key[0].string = "Objects";
71 vt_key[1].integer = object;
72 vt_key[2].string = "Container";
73 is_container = prop_get_boolean (bundle, "B<-sis", vt_key);
74 return is_container;
75 }
76
77 sc_bool
78 obj_is_surface (sc_gameref_t game, sc_int object)
79 {
80 const sc_prop_setref_t bundle = gs_get_bundle (game);
81 sc_vartype_t vt_key[3];
82 sc_bool is_surface;
83
84 vt_key[0].string = "Objects";
85 vt_key[1].integer = object;
86 vt_key[2].string = "Surface";
87 is_surface = prop_get_boolean (bundle, "B<-sis", vt_key);
88 return is_surface;
89 }
90
91
92 /*
93 * obj_container_object()
94 *
95 * Return the index of the n'th container object found.
96 */
97 sc_int
98 obj_container_object (sc_gameref_t game, sc_int n)
99 {
100 sc_int object, count;
101
102 /* Progress through objects until n containers found. */
103 count = n;
104 for (object = 0; object < gs_object_count (game) && count >= 0; object++)
105 {
106 if (obj_is_container (game, object))
107 count--;
108 }
109 return object - 1;
110 }
111
112
113 /*
114 * obj_container_index()
115 *
116 * Return index such that obj_container_object(index) == objnum.
117 */
118 sc_int
119 obj_container_index (sc_gameref_t game, sc_int objnum)
120 {
121 sc_int object, count;
122
123 /* Progress through objects up to objnum. */
124 count = 0;
125 for (object = 0; object < objnum; object++)
126 {
127 if (obj_is_container (game, object))
128 count++;
129 }
130 return count;
131 }
132
133
134 /*
135 * obj_surface_object()
136 *
137 * Return the index of the n'th surface object found.
138 */
139 sc_int
140 obj_surface_object (sc_gameref_t game, sc_int n)
141 {
142 sc_int object, count;
143
144 /* Progress through objects until n surfaces found. */
145 count = n;
146 for (object = 0; object < gs_object_count (game) && count >= 0; object++)
147 {
148 if (obj_is_surface (game, object))
149 count--;
150 }
151 return object - 1;
152 }
153
154
155 /*
156 * obj_surface_index()
157 *
158 * Return index such that obj_surface_object(index) == objnum.
159 */
160 sc_int
161 obj_surface_index (sc_gameref_t game, sc_int objnum)
162 {
163 sc_int object, count;
164
165 /* Progress through objects up to objnum. */
166 count = 0;
167 for (object = 0; object < objnum; object++)
168 {
169 if (obj_is_surface (game, object))
170 count++;
171 }
172 return count;
173 }
174
175
176 /*
177 * obj_stateful_object()
178 *
179 * Return the index of the n'th openable or statussed object found.
180 */
181 sc_int
182 obj_stateful_object (sc_gameref_t game, sc_int n)
183 {
184 const sc_prop_setref_t bundle = gs_get_bundle (game);
185 sc_int object, count;
186
187 /* Progress through objects until n matches found. */
188 count = n;
189 for (object = 0; object < gs_object_count (game) && count >= 0; object++)
190 {
191 sc_vartype_t vt_key[3];
192 sc_bool is_openable, is_statussed;
193
194 vt_key[0].string = "Objects";
195 vt_key[1].integer = object;
196 vt_key[2].string = "Openable";
197 is_openable = prop_get_integer (bundle, "I<-sis", vt_key) != 0;
198 vt_key[2].string = "CurrentState";
199 is_statussed = prop_get_integer (bundle, "I<-sis", vt_key) != 0;
200 if (is_openable || is_statussed)
201 count--;
202 }
203 return object - 1;
204 }
205
206
207 /*
208 * obj_stateful_index()
209 *
210 * Return index such that obj_stateful_object(index) == objnum.
211 */
212 sc_int
213 obj_stateful_index (sc_gameref_t game, sc_int objnum)
214 {
215 const sc_prop_setref_t bundle = gs_get_bundle (game);
216 sc_int object, count;
217
218 /* Progress through objects up to objnum. */
219 count = 0;
220 for (object = 0; object < objnum; object++)
221 {
222 sc_vartype_t vt_key[3];
223 sc_bool is_openable, is_statussed;
224
225 vt_key[0].string = "Objects";
226 vt_key[1].integer = object;
227 vt_key[2].string = "Openable";
228 is_openable = prop_get_integer (bundle, "I<-sis", vt_key) != 0;
229 vt_key[2].string = "CurrentState";
230 is_statussed = prop_get_integer (bundle, "I<-sis", vt_key) != 0;
231 if (is_openable || is_statussed)
232 count++;
233 }
234 return count;
235 }
236
237
238 /*
239 * obj_state_name()
240 *
241 * Return the string name of the state of a given stateful object. The
242 * string is malloc'ed, and needs to be freed by the caller. Returns NULL
243 * if no valid state string found.
244 */
245 sc_char *
246 obj_state_name (sc_gameref_t game, sc_int objnum)
247 {
248 const sc_prop_setref_t bundle = gs_get_bundle (game);
249 sc_vartype_t vt_key[3];
250 const sc_char *states;
251 sc_int length, state, count, first, last;
252 sc_char *string;
253
254 /* Get the list of state strings for the object. */
255 vt_key[0].string = "Objects";
256 vt_key[1].integer = objnum;
257 vt_key[2].string = "States";
258 states = prop_get_string (bundle, "S<-sis", vt_key);
259
260 /* Find the start of the element for the current state. */
261 state = gs_object_state (game, objnum);
262 length = strlen (states);
263 for (first = 0, count = state; first < length && count > 1; first++)
264 {
265 if (states[first] == '|')
266 count--;
267 }
268 if (count != 1)
269 return NULL;
270
271 /* Find the end of the state string. */
272 for (last = first; last < length; last++)
273 {
274 if (states[last] == '|')
275 break;
276 }
277
278 /* Allocate and take a copy of the state string. */
279 string = sc_malloc (last - first + 1);
280 memcpy (string, states + first, last - first);
281 string[last - first] = NUL;
282
283 return string;
284 }
285
286
287 /*
288 * obj_dynamic_object()
289 *
290 * Return the index of the n'th non-static object found.
291 */
292 sc_int
293 obj_dynamic_object (sc_gameref_t game, sc_int n)
294 {
295 sc_int object, count;
296
297 /* Progress through objects until n matches found. */
298 count = n;
299 for (object = 0; object < gs_object_count (game) && count >= 0; object++)
300 {
301 if (!obj_is_static (game, object))
302 count--;
303 }
304 return object - 1;
305 }
306
307
308 /*
309 * obj_wearable_object()
310 *
311 * Return the index of the n'th wearable object found.
312 */
313 sc_int
314 obj_wearable_object (sc_gameref_t game, sc_int n)
315 {
316 const sc_prop_setref_t bundle = gs_get_bundle (game);
317 sc_int object, count;
318
319 /* Progress through objects until n matches found. */
320 count = n;
321 for (object = 0; object < gs_object_count (game) && count >= 0; object++)
322 {
323 if (!obj_is_static (game, object))
324 {
325 sc_vartype_t vt_key[3];
326
327 vt_key[0].string = "Objects";
328 vt_key[1].integer = object;
329 vt_key[2].string = "Wearable";
330 if (prop_get_boolean (bundle, "B<-sis", vt_key))
331 count--;
332 }
333 }
334 return object - 1;
335 }
336
337
338 /*
339 * Size is held in the ten's digit of SizeWeight, and weight in the units.
340 * Size and weight are multipliers -- the relative size and weight of objects
341 * rises by a factor of three for each incremental multiplier. These factors
342 * are also used for the maximum size of object that can fit in a container,
343 * and the number of these that fit.
344 */
345 enum
346 { OBJ_DIMENSION_DIVISOR = 10,
347 OBJ_DIMENSION_MULTIPLE = 3
348 };
349
350 /*
351 * obj_get_size()
352 * obj_get_weight()
353 *
354 * Return the relative size and weight of an object. For containers, the
355 * weight includes the weight of each contained object.
356 *
357 * TODO It's possible to have static objects in the player inventory, moved
358 * by events -- how should these be handled, as they have no SizeWeight?
359 */
360 sc_int
361 obj_get_size (sc_gameref_t game, sc_int object)
362 {
363 const sc_prop_setref_t bundle = gs_get_bundle (game);
364 sc_vartype_t vt_key[3];
365 sc_int size, count;
366
367 /* TODO For now, give static objects no size. */
368 if (obj_is_static (game, object))
369 return 0;
370
371 /* Size is the 'tens' component of SizeWeight. */
372 vt_key[0].string = "Objects";
373 vt_key[1].integer = object;
374 vt_key[2].string = "SizeWeight";
375 count = prop_get_integer (bundle, "I<-sis", vt_key) / OBJ_DIMENSION_DIVISOR;
376
377 /*
378 * Calculate base object size. Unlike weights below, we take this as simply
379 * being the maximum size; that is, when a container carries other objects
380 * its weight increases by the sum of objects carried, but its size remains
381 * constant.
382 */
383 size = 1;
384 for (; count > 0; count--)
385 size *= OBJ_DIMENSION_MULTIPLE;
386
387 if (obj_trace)
388 sc_trace ("Object: object %ld is size %ld\n", object, size);
389
390 /* Return total size. */
391 return size;
392 }
393
394 sc_int
395 obj_get_weight (sc_gameref_t game, sc_int object)
396 {
397 const sc_prop_setref_t bundle = gs_get_bundle (game);
398 sc_vartype_t vt_key[3];
399 sc_int weight, count;
400
401 /* TODO For now, give static objects no weight. */
402 if (obj_is_static (game, object))
403 return 0;
404
405 /* Weight is the 'units' component of SizeWeight. */
406 vt_key[0].string = "Objects";
407 vt_key[1].integer = object;
408 vt_key[2].string = "SizeWeight";
409 count = prop_get_integer (bundle, "I<-sis", vt_key) % OBJ_DIMENSION_DIVISOR;
410
411 /* Calculate base object weight. */
412 weight = 1;
413 for (; count > 0; count--)
414 weight *= OBJ_DIMENSION_MULTIPLE;
415
416 /* If this is a container or a surface, add weights of parented objects. */
417 if (obj_is_container (game, object) || obj_is_surface (game, object))
418 {
419 sc_int other;
420
421 /* Find and add contained or surface objects. */
422 for (other = 0; other < gs_object_count (game); other++)
423 {
424 if ((gs_object_position (game, other) == OBJ_IN_OBJECT
425 || gs_object_position (game, other) == OBJ_ON_OBJECT)
426 && gs_object_parent (game, other) == object)
427 {
428 weight += obj_get_weight (game, other);
429 }
430 }
431 }
432
433 if (obj_trace)
434 sc_trace ("Object: object %ld is weight %ld\n", object, weight);
435
436 /* Return total weight. */
437 return weight;
438 }
439
440
441 /*
442 * obj_convert_player_limit()
443 * obj_get_player_size_limit()
444 * obj_get_player_weight_limit()
445 *
446 * Return the limits set on the sizes and weights a player can handle. Not
447 * really object-related except that they deal with sizing multiples.
448 */
449 static sc_int
450 obj_convert_player_limit (sc_int value)
451 {
452 sc_int retval, index_;
453
454 /* 'Tens' of value multiplied by 3 to the power 'units' of value. */
455 retval = value / OBJ_DIMENSION_DIVISOR;
456 for (index_ = 0; index_ < value % OBJ_DIMENSION_DIVISOR; index_++)
457 retval *= OBJ_DIMENSION_MULTIPLE;
458
459 return retval;
460 }
461
462 sc_int
463 obj_get_player_size_limit (sc_gameref_t game)
464 {
465 const sc_prop_setref_t bundle = gs_get_bundle (game);
466 sc_vartype_t vt_key[2];
467 sc_int max_size;
468
469 vt_key[0].string = "Globals";
470 vt_key[1].string = "MaxSize";
471 max_size = prop_get_integer (bundle, "I<-ss", vt_key);
472
473 return obj_convert_player_limit (max_size);
474 }
475
476 sc_int
477 obj_get_player_weight_limit (sc_gameref_t game)
478 {
479 const sc_prop_setref_t bundle = gs_get_bundle (game);
480 sc_vartype_t vt_key[2];
481 sc_int max_weight;
482
483 vt_key[0].string = "Globals";
484 vt_key[1].string = "MaxWt";
485 max_weight = prop_get_integer (bundle, "I<-ss", vt_key);
486
487 return obj_convert_player_limit (max_weight);
488 }
489
490
491 /*
492 * obj_get_container_maxsize()
493 * obj_get_container_capacity()
494 *
495 * Return the maximum size of an object that can be placed in a container,
496 * and the number that will fit.
497 */
498 sc_int
499 obj_get_container_maxsize (sc_gameref_t game, sc_int object)
500 {
501 const sc_prop_setref_t bundle = gs_get_bundle (game);
502 sc_vartype_t vt_key[3];
503 sc_int maxsize, count;
504
505 /* Maxsize is found from the 'units' component of Capacity. */
506 vt_key[0].string = "Objects";
507 vt_key[1].integer = object;
508 vt_key[2].string = "Capacity";
509 count = prop_get_integer (bundle, "I<-sis", vt_key) % OBJ_DIMENSION_DIVISOR;
510
511 /* Calculate and return maximum size. */
512 maxsize = 1;
513 for (; count > 0; count--)
514 maxsize *= OBJ_DIMENSION_MULTIPLE;
515
516 if (obj_trace)
517 sc_trace ("Object: object %ld has max size %ld\n", object, maxsize);
518
519 return maxsize;
520 }
521
522 sc_int
523 obj_get_container_capacity (sc_gameref_t game, sc_int object)
524 {
525 const sc_prop_setref_t bundle = gs_get_bundle (game);
526 sc_vartype_t vt_key[3];
527 sc_int capacity;
528
529 /* The count of objects is in the 'tens' component of Capacity. */
530 vt_key[0].string = "Objects";
531 vt_key[1].integer = object;
532 vt_key[2].string = "Capacity";
533 capacity = prop_get_integer (bundle, "I<-sis", vt_key)
534 / OBJ_DIMENSION_DIVISOR;
535
536 if (obj_trace)
537 sc_trace ("Object: object %ld has capacity %ld\n", object, capacity);
538
539 return capacity;
540 }
541
542
543 /* Sit/lie bit mask enumerations. */
544 enum
545 { OBJ_STANDABLE_MASK = 1 << 0,
546 OBJ_LIEABLE_MASK = 1 << 1
547 };
548
549 /*
550 * obj_standable_object()
551 *
552 * Return the index of the n'th standable object found.
553 */
554 sc_int
555 obj_standable_object (sc_gameref_t game, sc_int n)
556 {
557 const sc_prop_setref_t bundle = gs_get_bundle (game);
558 sc_int object, count;
559
560 /* Progress through objects until n standable found. */
561 count = n;
562 for (object = 0; object < gs_object_count (game) && count >= 0; object++)
563 {
564 sc_vartype_t vt_key[3];
565 sc_int sit_lie_flags;
566
567 vt_key[0].string = "Objects";
568 vt_key[1].integer = object;
569 vt_key[2].string = "SitLie";
570 sit_lie_flags = prop_get_integer (bundle, "I<-sis", vt_key);
571 if (sit_lie_flags & OBJ_STANDABLE_MASK)
572 count--;
573 }
574 return object - 1;
575 }
576
577
578 /*
579 * obj_lieable_object()
580 *
581 * Return the index of the n'th lieable object found.
582 */
583 sc_int
584 obj_lieable_object (sc_gameref_t game, sc_int n)
585 {
586 const sc_prop_setref_t bundle = gs_get_bundle (game);
587 sc_int object, count;
588
589 /* Progress through objects until n lieable found. */
590 count = n;
591 for (object = 0; object < gs_object_count (game) && count >= 0; object++)
592 {
593 sc_vartype_t vt_key[3];
594 sc_int sit_lie_flags;
595
596 vt_key[0].string = "Objects";
597 vt_key[1].integer = object;
598 vt_key[2].string = "SitLie";
599 sit_lie_flags = prop_get_integer (bundle, "I<-sis", vt_key);
600 if (sit_lie_flags & OBJ_LIEABLE_MASK)
601 count--;
602 }
603 return object - 1;
604 }
605
606
607 /*
608 * obj_appears_plural()
609 *
610 * Return TRUE if the object appears to be plural. Adrift makes a guess at
611 * this to produce "... is on.." or "... are on...". It's not clear how it
612 * does it, but it looks something like: singular if prefix is "a" or "an"
613 * or ""; plural if prefix is "the" or "some" and short name ends with 's'
614 * that is not preceded by 'u'.
615 */
616 sc_bool
617 obj_appears_plural (sc_gameref_t game, sc_int object)
618 {
619 const sc_prop_setref_t bundle = gs_get_bundle (game);
620 sc_vartype_t vt_key[3];
621 const sc_char *prefix, *name;
622
623 /* Check prefix for "a", "an", or empty. */
624 vt_key[0].string = "Objects";
625 vt_key[1].integer = object;
626 vt_key[2].string = "Prefix";
627 prefix = prop_get_string (bundle, "S<-sis", vt_key);
628
629 if (!(sc_strempty (prefix)
630 || sc_compare_word (prefix, "a", 1)
631 || sc_compare_word (prefix, "an", 2)))
632 {
633 sc_int length;
634
635 /* Check name for ending in 's', but not 'us'. */
636 vt_key[2].string = "Short";
637 name = prop_get_string (bundle, "S<-sis", vt_key);
638 length = strlen (name);
639
640 if (!sc_strempty (name)
641 && sc_tolower (name[length - 1]) == 's'
642 && (length < 2 || sc_tolower (name[length - 2]) != 'u'))
643 return TRUE;
644 }
645
646 /* Doesn't look plural. */
647 return FALSE;
648 }
649
650
651 /*
652 * obj_directly_in_room_internal()
653 * obj_directly_in_room()
654 *
655 * Return TRUE if a given object is currently on the floor of a given room.
656 */
657 static sc_bool
658 obj_directly_in_room_internal (sc_gameref_t game, sc_int object, sc_int room)
659 {
660 const sc_prop_setref_t bundle = gs_get_bundle (game);
661
662 /* See if the object is static or dynamic. */
663 if (obj_is_static (game, object))
664 {
665 sc_vartype_t vt_key[5];
666 sc_int type;
667
668 /* Static object moved to player or room by event? */
669 if (!gs_object_static_unmoved (game, object))
670 {
671 if (gs_object_position (game, object) == OBJ_HELD_PLAYER)
672 return FALSE;
673 else
674 return gs_object_position (game, object) - 1 == room;
675 }
676
677 /* Check and return the room list for the object. */
678 vt_key[0].string = "Objects";
679 vt_key[1].integer = object;
680 vt_key[2].string = "Where";
681 vt_key[3].string = "Type";
682 type = prop_get_integer (bundle, "I<-siss", vt_key);
683 switch (type)
684 {
685 case ROOMLIST_ALL_ROOMS:
686 return TRUE;
687 case ROOMLIST_NO_ROOMS:
688 case ROOMLIST_NPC_PART:
689 return FALSE;
690
691 case ROOMLIST_ONE_ROOM:
692 vt_key[3].string = "Room";
693 return prop_get_integer (bundle, "I<-siss", vt_key) == room + 1;
694
695 case ROOMLIST_SOME_ROOMS:
696 vt_key[3].string = "Rooms";
697 vt_key[4].integer = room + 1;
698 return prop_get_boolean (bundle, "B<-sissi", vt_key);
699
700 default:
701 sc_fatal ("obj_directly_in_room_internal:"
702 " invalid type, %ld\n", type);
703 return FALSE;
704 }
705 }
706 else
707 return gs_object_position (game, object) == room + 1;
708 }
709
710 sc_bool
711 obj_directly_in_room (sc_gameref_t game, sc_int object, sc_int room)
712 {
713 sc_bool result;
714
715 /* Check, trace result, and return. */
716 result = obj_directly_in_room_internal (game, object, room);
717
718 if (obj_trace)
719 {
720 sc_trace ("Object: checking for object %ld directly in room %ld, %s\n",
721 object, room, result ? "true" : "false");
722 }
723
724 return result;
725 }
726
727
728 /*
729 * obj_indirectly_in_room_internal()
730 * obj_indirectly_in_room()
731 *
732 * Return TRUE if a given object is currently in a given room, either
733 * directly, on an object indirectly, in an open object indirectly, or
734 * carried by an NPC in the room.
735 */
736 static sc_bool
737 obj_indirectly_in_room_internal (sc_gameref_t game, sc_int object, sc_int room)
738 {
739 const sc_prop_setref_t bundle = gs_get_bundle (game);
740
741 /* See if the object is static or dynamic. */
742 if (obj_is_static (game, object))
743 {
744 sc_vartype_t vt_key[5];
745 sc_int type;
746
747 /* Static object moved to player or room by event? */
748 if (!gs_object_static_unmoved (game, object))
749 {
750 if (gs_object_position (game, object) == OBJ_HELD_PLAYER)
751 return gs_player_in_room (game, room);
752 else
753 return gs_object_position (game, object) - 1 == room;
754 }
755
756 /* Check and return the room list for the object. */
757 vt_key[0].string = "Objects";
758 vt_key[1].integer = object;
759 vt_key[2].string = "Where";
760 vt_key[3].string = "Type";
761 type = prop_get_integer (bundle, "I<-siss", vt_key);
762 switch (type)
763 {
764 case ROOMLIST_ALL_ROOMS:
765 return TRUE;
766 case ROOMLIST_NO_ROOMS:
767 return FALSE;
768
769 case ROOMLIST_ONE_ROOM:
770 vt_key[3].string = "Room";
771 return prop_get_integer (bundle, "I<-siss", vt_key) == room + 1;
772
773 case ROOMLIST_SOME_ROOMS:
774 vt_key[3].string = "Rooms";
775 vt_key[4].integer = room + 1;
776 return prop_get_boolean (bundle, "B<-sissi", vt_key);
777
778 case ROOMLIST_NPC_PART:
779 {
780 sc_int npc;
781
782 vt_key[2].string = "Parent";
783 npc = prop_get_integer (bundle, "I<-sis", vt_key);
784 if (npc == 0)
785 return gs_player_in_room (game, room);
786 else
787 return npc_in_room (game, npc - 1, room);
788 }
789
790 default:
791 sc_fatal ("obj_indirectly_in_room_internal:"
792 " invalid type, %ld\n", type);
793 return FALSE;
794 }
795 }
796 else
797 {
798 sc_int parent, position;
799
800 /* Get dynamic object's parent and position. */
801 parent = gs_object_parent (game, object);
802 position = gs_object_position (game, object);
803
804 /* Decide depending on positioning. */
805 switch (position)
806 {
807 case OBJ_HIDDEN: /* Hidden. */
808 return FALSE;
809
810 case OBJ_HELD_PLAYER: /* Held by player. */
811 case OBJ_WORN_PLAYER: /* Worn by player. */
812 return gs_player_in_room (game, room);
813
814 case OBJ_HELD_NPC: /* Held by NPC. */
815 case OBJ_WORN_NPC: /* Worn by NPC. */
816 return npc_in_room (game, parent, room);
817
818 case OBJ_IN_OBJECT: /* In another object. */
819 {
820 sc_int openness;
821
822 openness = gs_object_openness (game, parent);
823 switch (openness)
824 {
825 case OBJ_WONTCLOSE:
826 case OBJ_OPEN:
827 return obj_indirectly_in_room (game, parent, room);
828 default:
829 return FALSE;
830 }
831 }
832
833 case OBJ_ON_OBJECT: /* On another object. */
834 return obj_indirectly_in_room (game, parent, room);
835
836 default: /* Within a room. */
837 if (position > gs_room_count (game) + 1)
838 {
839 sc_error ("sc_object_indirectly_in_room:"
840 " position out of bounds, %ld\n", position);
841 }
842 return position - 1 == room;
843 }
844 }
845 }
846
847 sc_bool
848 obj_indirectly_in_room (sc_gameref_t game,
849 sc_int object, sc_int room)
850 {
851 sc_bool result;
852
853 /* Check, trace result, and return. */
854 result = obj_indirectly_in_room_internal (game, object, room);
855
856 if (obj_trace)
857 {
858 sc_trace ("Object: checking for object %ld indirectly in room %ld, %s\n",
859 object, room, result ? "true" : "false");
860 }
861
862 return result;
863 }
864
865
866 /*
867 * obj_indirectly_held_by_player_internal()
868 * obj_indirectly_held_by_player()
869 *
870 * Return TRUE if a given object is currently held by the player, either
871 * directly, on an object indirectly, or in an open object indirectly.
872 */
873 static sc_bool
874 obj_indirectly_held_by_player_internal (sc_gameref_t game,
875 sc_int object)
876 {
877 /* See if the object is static or dynamic. */
878 if (obj_is_static (game, object))
879 {
880 /* Static object moved to player or room by event? */
881 if (!gs_object_static_unmoved (game, object))
882 {
883 if (gs_object_position (game, object) == OBJ_HELD_PLAYER)
884 return TRUE;
885 else
886 return FALSE;
887 }
888
889 /* An unmoved static object is not held by the player. */
890 return FALSE;
891 }
892 else
893 {
894 sc_int parent, position;
895
896 /* Get dynamic object's parent and position. */
897 parent = gs_object_parent (game, object);
898 position = gs_object_position (game, object);
899
900 /* Decide depending on positioning. */
901 switch (position)
902 {
903 case OBJ_HIDDEN: /* Hidden. */
904 return FALSE;
905
906 case OBJ_HELD_PLAYER: /* Held by player. */
907 case OBJ_WORN_PLAYER: /* Worn by player. */
908 return TRUE;
909
910 case OBJ_HELD_NPC: /* Held by NPC. */
911 case OBJ_WORN_NPC: /* Worn by NPC. */
912 return FALSE;
913
914 case OBJ_IN_OBJECT: /* In another object. */
915 {
916 sc_int openness;
917
918 openness = gs_object_openness (game, parent);
919 switch (openness)
920 {
921 case OBJ_WONTCLOSE:
922 case OBJ_OPEN:
923 return obj_indirectly_held_by_player (game, parent);
924 default:
925 return FALSE;
926 }
927 }
928
929 case OBJ_ON_OBJECT: /* On another object. */
930 return obj_indirectly_held_by_player (game, parent);
931
932 default: /* Within a room. */
933 return FALSE;
934 }
935 }
936 }
937
938 sc_bool
939 obj_indirectly_held_by_player (sc_gameref_t game, sc_int object)
940 {
941 sc_bool result;
942
943 /* Check, trace result, and return. */
944 result = obj_indirectly_held_by_player_internal (game, object);
945
946 if (obj_trace)
947 {
948 sc_trace ("Object: checking for object %ld indirectly"
949 " held by player, %s\n", object, result ? "true" : "false");
950 }
951
952 return result;
953 }
954
955
956 /*
957 * sc_obj_shows_initial_description()
958 *
959 * Return TRUE if this object should be listed as room content.
960 */
961 sc_bool
962 obj_shows_initial_description (sc_gameref_t game, sc_int object)
963 {
964 const sc_prop_setref_t bundle = gs_get_bundle (game);
965 sc_vartype_t vt_key[3];
966 sc_int onlywhennotmoved;
967
968 /* Get only when moved property. */
969 vt_key[0].string = "Objects";
970 vt_key[1].integer = object;
971 vt_key[2].string = "OnlyWhenNotMoved";
972 onlywhennotmoved = prop_get_integer (bundle, "I<-sis", vt_key);
973
974 /* Combine this with game in mysterious ways. */
975 switch (onlywhennotmoved)
976 {
977 case 0:
978 return TRUE;
979
980 case 1:
981 return gs_object_unmoved (game, object);
982
983 case 2:
984 {
985 sc_int initialposition;
986
987 if (gs_object_unmoved (game, object))
988 return TRUE;
989
990 vt_key[2].string = "InitialPosition";
991 initialposition = prop_get_integer (bundle, "I<-sis", vt_key) - 3;
992 return gs_object_position (game, object) == initialposition;
993 }
994 }
995
996 /* What you talkin' 'bout, Willis? */
997 return FALSE;
998 }
999
1000
1001 /*
1002 * obj_turn_update()
1003 * obj_setup_initial()
1004 *
1005 * Set initial values for object states, and update after a turn.
1006 */
1007 void
1008 obj_turn_update (sc_gameref_t game)
1009 {
1010 sc_int index_;
1011
1012 /* Update object seen flag to current state. */
1013 for (index_ = 0; index_ < gs_object_count (game); index_++)
1014 {
1015 if (!gs_object_seen (game, index_)
1016 && obj_indirectly_in_room (game, index_, gs_playerroom (game)))
1017 gs_set_object_seen (game, index_, TRUE);
1018 }
1019 }
1020
1021 void
1022 obj_setup_initial (sc_gameref_t game)
1023 {
1024 /* Set initial seen states for objects. */
1025 obj_turn_update (game);
1026 }
1027
1028
1029 /*
1030 * obj_debug_trace()
1031 *
1032 * Set object tracing on/off.
1033 */
1034 void
1035 obj_debug_trace (sc_bool flag)
1036 {
1037 obj_trace = flag;
1038 }