aboutsummaryrefslogtreecommitdiff
path: root/engines/glk/adrift/scobjcts.cpp
blob: 9bef9952e3cb341f51234d157dd6671a5f21c5bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#include "glk/adrift/scare.h"
#include "glk/adrift/scprotos.h"
#include "glk/adrift/scgamest.h"

namespace Glk {
namespace Adrift {

/* Assorted definitions and constants. */
static const sc_char NUL = '\0';

/* Trace flag, set before running. */
static sc_bool obj_trace = FALSE;


/*
 * obj_is_static()
 * obj_is_surface()
 * obj_is_container()
 *
 * Convenience functions to return TRUE for given object attributes.
 */
sc_bool
obj_is_static (sc_gameref_t game, sc_int object)
{
  const sc_prop_setref_t bundle = gs_get_bundle (game);
  sc_vartype_t vt_key[3];
  sc_bool bstatic;

  vt_key[0].string = "Objects";
  vt_key[1].integer = object;
  vt_key[2].string = "Static";
  bstatic = prop_get_boolean (bundle, "B<-sis", vt_key);
  return bstatic;
}

sc_bool
obj_is_container (sc_gameref_t game, sc_int object)
{
  const sc_prop_setref_t bundle = gs_get_bundle (game);
  sc_vartype_t vt_key[3];
  sc_bool is_container;

  vt_key[0].string = "Objects";
  vt_key[1].integer = object;
  vt_key[2].string = "Container";
  is_container = prop_get_boolean (bundle, "B<-sis", vt_key);
  return is_container;
}

sc_bool
obj_is_surface (sc_gameref_t game, sc_int object)
{
  const sc_prop_setref_t bundle = gs_get_bundle (game);
  sc_vartype_t vt_key[3];
  sc_bool is_surface;

  vt_key[0].string = "Objects";
  vt_key[1].integer = object;
  vt_key[2].string = "Surface";
  is_surface = prop_get_boolean (bundle, "B<-sis", vt_key);
  return is_surface;
}


/*
 * obj_container_object()
 *
 * Return the index of the n'th container object found.
 */
sc_int
obj_container_object (sc_gameref_t game, sc_int n)
{
  sc_int object, count;

  /* Progress through objects until n containers found. */
  count = n;
  for (object = 0; object < gs_object_count (game) && count >= 0; object++)
    {
      if (obj_is_container (game, object))
        count--;
    }
  return object - 1;
}


/*
 * obj_container_index()
 *
 * Return index such that obj_container_object(index) == objnum.
 */
sc_int
obj_container_index (sc_gameref_t game, sc_int objnum)
{
  sc_int object, count;

  /* Progress through objects up to objnum. */
  count = 0;
  for (object = 0; object < objnum; object++)
    {
      if (obj_is_container (game, object))
        count++;
    }
  return count;
}


/*
 * obj_surface_object()
 *
 * Return the index of the n'th surface object found.
 */
sc_int
obj_surface_object (sc_gameref_t game, sc_int n)
{
  sc_int object, count;

  /* Progress through objects until n surfaces found. */
  count = n;
  for (object = 0; object < gs_object_count (game) && count >= 0; object++)
    {
      if (obj_is_surface (game, object))
        count--;
    }
  return object - 1;
}


/*
 * obj_surface_index()
 *
 * Return index such that obj_surface_object(index) == objnum.
 */
sc_int
obj_surface_index (sc_gameref_t game, sc_int objnum)
{
  sc_int object, count;

  /* Progress through objects up to objnum. */
  count = 0;
  for (object = 0; object < objnum; object++)
    {
      if (obj_is_surface (game, object))
        count++;
    }
  return count;
}


/*
 * obj_stateful_object()
 *
 * Return the index of the n'th openable or statussed object found.
 */
sc_int
obj_stateful_object (sc_gameref_t game, sc_int n)
{
  const sc_prop_setref_t bundle = gs_get_bundle (game);
  sc_int object, count;

  /* Progress through objects until n matches found. */
  count = n;
  for (object = 0; object < gs_object_count (game) && count >= 0; object++)
    {
      sc_vartype_t vt_key[3];
      sc_bool is_openable, is_statussed;

      vt_key[0].string = "Objects";
      vt_key[1].integer = object;
      vt_key[2].string = "Openable";
      is_openable = prop_get_integer (bundle, "I<-sis", vt_key) != 0;
      vt_key[2].string = "CurrentState";
      is_statussed = prop_get_integer (bundle, "I<-sis", vt_key) != 0;
      if (is_openable || is_statussed)
        count--;
    }
  return object - 1;
}


/*
 * obj_stateful_index()
 *
 * Return index such that obj_stateful_object(index) == objnum.
 */
sc_int
obj_stateful_index (sc_gameref_t game, sc_int objnum)
{
  const sc_prop_setref_t bundle = gs_get_bundle (game);
  sc_int object, count;

  /* Progress through objects up to objnum. */
  count = 0;
  for (object = 0; object < objnum; object++)
    {
      sc_vartype_t vt_key[3];
      sc_bool is_openable, is_statussed;

      vt_key[0].string = "Objects";
      vt_key[1].integer = object;
      vt_key[2].string = "Openable";
      is_openable = prop_get_integer (bundle, "I<-sis", vt_key) != 0;
      vt_key[2].string = "CurrentState";
      is_statussed = prop_get_integer (bundle, "I<-sis", vt_key) != 0;
      if (is_openable || is_statussed)
        count++;
    }
  return count;
}


/*
 * obj_state_name()
 *
 * Return the string name of the state of a given stateful object.  The
 * string is malloc'ed, and needs to be freed by the caller.  Returns NULL
 * if no valid state string found.
 */
sc_char *
obj_state_name (sc_gameref_t game, sc_int objnum)
{
  const sc_prop_setref_t bundle = gs_get_bundle (game);
  sc_vartype_t vt_key[3];
  const sc_char *states;
  sc_int length, state, count, first, last;
  sc_char *string;

  /* Get the list of state strings for the object. */
  vt_key[0].string = "Objects";
  vt_key[1].integer = objnum;
  vt_key[2].string = "States";
  states = prop_get_string (bundle, "S<-sis", vt_key);

  /* Find the start of the element for the current state. */
  state = gs_object_state (game, objnum);
  length = strlen (states);
  for (first = 0, count = state; first < length && count > 1; first++)
    {
      if (states[first] == '|')
        count--;
    }
  if (count != 1)
    return NULL;

  /* Find the end of the state string. */
  for (last = first; last < length; last++)
    {
      if (states[last] == '|')
        break;
    }

  /* Allocate and take a copy of the state string. */
  string = (sc_char *)sc_malloc (last - first + 1);
  memcpy (string, states + first, last - first);
  string[last - first] = NUL;

  return string;
}


/*
 * obj_dynamic_object()
 *
 * Return the index of the n'th non-static object found.
 */
sc_int
obj_dynamic_object (sc_gameref_t game, sc_int n)
{
  sc_int object, count;

  /* Progress through objects until n matches found. */
  count = n;
  for (object = 0; object < gs_object_count (game) && count >= 0; object++)
    {
      if (!obj_is_static (game, object))
        count--;
    }
  return object - 1;
}


/*
 * obj_wearable_object()
 *
 * Return the index of the n'th wearable object found.
 */
sc_int
obj_wearable_object (sc_gameref_t game, sc_int n)
{
  const sc_prop_setref_t bundle = gs_get_bundle (game);
  sc_int object, count;

  /* Progress through objects until n matches found. */
  count = n;
  for (object = 0; object < gs_object_count (game) && count >= 0; object++)
    {
      if (!obj_is_static (game, object))
        {
          sc_vartype_t vt_key[3];

          vt_key[0].string = "Objects";
          vt_key[1].integer = object;
          vt_key[2].string = "Wearable";
          if (prop_get_boolean (bundle, "B<-sis", vt_key))
            count--;
        }
    }
  return object - 1;
}


/*
 * Size is held in the ten's digit of SizeWeight, and weight in the units.
 * Size and weight are multipliers -- the relative size and weight of objects
 * rises by a factor of three for each incremental multiplier.  These factors
 * are also used for the maximum size of object that can fit in a container,
 * and the number of these that fit.
 */
enum
{ OBJ_DIMENSION_DIVISOR = 10,
  OBJ_DIMENSION_MULTIPLE = 3
};

/*
 * obj_get_size()
 * obj_get_weight()
 *
 * Return the relative size and weight of an object.  For containers, the
 * weight includes the weight of each contained object.
 *
 * TODO It's possible to have static objects in the player inventory, moved
 * by events -- how should these be handled, as they have no SizeWeight?
 */
sc_int
obj_get_size (sc_gameref_t game, sc_int object)
{
  const sc_prop_setref_t bundle = gs_get_bundle (game);
  sc_vartype_t vt_key[3];
  sc_int size, count;

  /* TODO For now, give static objects no size. */
  if (obj_is_static (game, object))
    return 0;

  /* Size is the 'tens' component of SizeWeight. */
  vt_key[0].string = "Objects";
  vt_key[1].integer = object;
  vt_key[2].string = "SizeWeight";
  count = prop_get_integer (bundle, "I<-sis", vt_key) / OBJ_DIMENSION_DIVISOR;

  /*
   * Calculate base object size.  Unlike weights below, we take this as simply
   * being the maximum size; that is, when a container carries other objects
   * its weight increases by the sum of objects carried, but its size remains
   * constant.
   */
  size = 1;
  for (; count > 0; count--)
    size *= OBJ_DIMENSION_MULTIPLE;

  if (obj_trace)
    sc_trace ("Object: object %ld is size %ld\n", object, size);

  /* Return total size. */
  return size;
}

sc_int
obj_get_weight (sc_gameref_t game, sc_int object)
{
  const sc_prop_setref_t bundle = gs_get_bundle (game);
  sc_vartype_t vt_key[3];
  sc_int weight, count;

  /* TODO For now, give static objects no weight. */
  if (obj_is_static (game, object))
    return 0;

  /* Weight is the 'units' component of SizeWeight. */
  vt_key[0].string = "Objects";
  vt_key[1].integer = object;
  vt_key[2].string = "SizeWeight";
  count = prop_get_integer (bundle, "I<-sis", vt_key) % OBJ_DIMENSION_DIVISOR;

  /* Calculate base object weight. */
  weight = 1;
  for (; count > 0; count--)
    weight *= OBJ_DIMENSION_MULTIPLE;

  /* If this is a container or a surface, add weights of parented objects. */
  if (obj_is_container (game, object) || obj_is_surface (game, object))
    {
      sc_int other;

      /* Find and add contained or surface objects. */
      for (other = 0; other < gs_object_count (game); other++)
        {
          if ((gs_object_position (game, other) == OBJ_IN_OBJECT
               || gs_object_position (game, other) == OBJ_ON_OBJECT)
              && gs_object_parent (game, other) == object)
            {
              weight += obj_get_weight (game, other);
            }
        }
    }

  if (obj_trace)
    sc_trace ("Object: object %ld is weight %ld\n", object, weight);

  /* Return total weight. */
  return weight;
}


/*
 * obj_convert_player_limit()
 * obj_get_player_size_limit()
 * obj_get_player_weight_limit()
 *
 * Return the limits set on the sizes and weights a player can handle.  Not
 * really object-related except that they deal with sizing multiples.
 */
static sc_int
obj_convert_player_limit (sc_int value)
{
  sc_int retval, index_;

  /* 'Tens' of value multiplied by 3 to the power 'units' of value. */
  retval = value / OBJ_DIMENSION_DIVISOR;
  for (index_ = 0; index_ < value % OBJ_DIMENSION_DIVISOR; index_++)
    retval *= OBJ_DIMENSION_MULTIPLE;

  return retval;
}

sc_int
obj_get_player_size_limit (sc_gameref_t game)
{
  const sc_prop_setref_t bundle = gs_get_bundle (game);
  sc_vartype_t vt_key[2];
  sc_int max_size;

  vt_key[0].string = "Globals";
  vt_key[1].string = "MaxSize";
  max_size = prop_get_integer (bundle, "I<-ss", vt_key);

  return obj_convert_player_limit (max_size);
}

sc_int
obj_get_player_weight_limit (sc_gameref_t game)
{
  const sc_prop_setref_t bundle = gs_get_bundle (game);
  sc_vartype_t vt_key[2];
  sc_int max_weight;

  vt_key[0].string = "Globals";
  vt_key[1].string = "MaxWt";
  max_weight = prop_get_integer (bundle, "I<-ss", vt_key);

  return obj_convert_player_limit (max_weight);
}


/*
 * obj_get_container_maxsize()
 * obj_get_container_capacity()
 *
 * Return the maximum size of an object that can be placed in a container,
 * and the number that will fit.
 */
sc_int
obj_get_container_maxsize (sc_gameref_t game, sc_int object)
{
  const sc_prop_setref_t bundle = gs_get_bundle (game);
  sc_vartype_t vt_key[3];
  sc_int maxsize, count;

  /* Maxsize is found from the 'units' component of Capacity. */
  vt_key[0].string = "Objects";
  vt_key[1].integer = object;
  vt_key[2].string = "Capacity";
  count = prop_get_integer (bundle, "I<-sis", vt_key) % OBJ_DIMENSION_DIVISOR;

  /* Calculate and return maximum size. */
  maxsize = 1;
  for (; count > 0; count--)
    maxsize *= OBJ_DIMENSION_MULTIPLE;

  if (obj_trace)
    sc_trace ("Object: object %ld has max size %ld\n", object, maxsize);

  return maxsize;
}

sc_int
obj_get_container_capacity (sc_gameref_t game, sc_int object)
{
  const sc_prop_setref_t bundle = gs_get_bundle (game);
  sc_vartype_t vt_key[3];
  sc_int capacity;

  /* The count of objects is in the 'tens' component of Capacity. */
  vt_key[0].string = "Objects";
  vt_key[1].integer = object;
  vt_key[2].string = "Capacity";
  capacity = prop_get_integer (bundle, "I<-sis", vt_key)
             / OBJ_DIMENSION_DIVISOR;

  if (obj_trace)
    sc_trace ("Object: object %ld has capacity %ld\n", object, capacity);

  return capacity;
}


/* Sit/lie bit mask enumerations. */
enum
{ OBJ_STANDABLE_MASK = 1 << 0,
  OBJ_LIEABLE_MASK = 1 << 1
};

/*
 * obj_standable_object()
 *
 * Return the index of the n'th standable object found.
 */
sc_int
obj_standable_object (sc_gameref_t game, sc_int n)
{
  const sc_prop_setref_t bundle = gs_get_bundle (game);
  sc_int object, count;

  /* Progress through objects until n standable found. */
  count = n;
  for (object = 0; object < gs_object_count (game) && count >= 0; object++)
    {
      sc_vartype_t vt_key[3];
      sc_int sit_lie_flags;

      vt_key[0].string = "Objects";
      vt_key[1].integer = object;
      vt_key[2].string = "SitLie";
      sit_lie_flags = prop_get_integer (bundle, "I<-sis", vt_key);
      if (sit_lie_flags & OBJ_STANDABLE_MASK)
        count--;
    }
  return object - 1;
}


/*
 * obj_lieable_object()
 *
 * Return the index of the n'th lieable object found.
 */
sc_int
obj_lieable_object (sc_gameref_t game, sc_int n)
{
  const sc_prop_setref_t bundle = gs_get_bundle (game);
  sc_int object, count;

  /* Progress through objects until n lieable found. */
  count = n;
  for (object = 0; object < gs_object_count (game) && count >= 0; object++)
    {
      sc_vartype_t vt_key[3];
      sc_int sit_lie_flags;

      vt_key[0].string = "Objects";
      vt_key[1].integer = object;
      vt_key[2].string = "SitLie";
      sit_lie_flags = prop_get_integer (bundle, "I<-sis", vt_key);
      if (sit_lie_flags & OBJ_LIEABLE_MASK)
        count--;
    }
  return object - 1;
}


/*
 * obj_appears_plural()
 *
 * Return TRUE if the object appears to be plural.  Adrift makes a guess at
 * this to produce "... is on.." or "... are on...".  It's not clear how it
 * does it, but it looks something like: singular if prefix is "a" or "an"
 * or ""; plural if prefix is "the" or "some" and short name ends with 's'
 * that is not preceded by 'u'.
 */
sc_bool
obj_appears_plural (sc_gameref_t game, sc_int object)
{
  const sc_prop_setref_t bundle = gs_get_bundle (game);
  sc_vartype_t vt_key[3];
  const sc_char *prefix, *name;

  /* Check prefix for "a", "an", or empty. */
  vt_key[0].string = "Objects";
  vt_key[1].integer = object;
  vt_key[2].string = "Prefix";
  prefix = prop_get_string (bundle, "S<-sis", vt_key);

  if (!(sc_strempty (prefix)
        || sc_compare_word (prefix, "a", 1)
        || sc_compare_word (prefix, "an", 2)))
    {
      sc_int length;

      /* Check name for ending in 's', but not 'us'. */
      vt_key[2].string = "Short";
      name = prop_get_string (bundle, "S<-sis", vt_key);
      length = strlen (name);

      if (!sc_strempty (name)
          && sc_tolower (name[length - 1]) == 's'
          && (length < 2 || sc_tolower (name[length - 2]) != 'u'))
        return TRUE;
    }

  /* Doesn't look plural. */
  return FALSE;
}


/*
 * obj_directly_in_room_internal()
 * obj_directly_in_room()
 *
 * Return TRUE if a given object is currently on the floor of a given room.
 */
static sc_bool
obj_directly_in_room_internal (sc_gameref_t game, sc_int object, sc_int room)
{
  const sc_prop_setref_t bundle = gs_get_bundle (game);

  /* See if the object is static or dynamic. */
  if (obj_is_static (game, object))
    {
      sc_vartype_t vt_key[5];
      sc_int type;

      /* Static object moved to player or room by event? */
      if (!gs_object_static_unmoved (game, object))
        {
          if (gs_object_position (game, object) == OBJ_HELD_PLAYER)
            return FALSE;
          else
            return gs_object_position (game, object) - 1 == room;
        }

      /* Check and return the room list for the object. */
      vt_key[0].string = "Objects";
      vt_key[1].integer = object;
      vt_key[2].string = "Where";
      vt_key[3].string = "Type";
      type = prop_get_integer (bundle, "I<-siss", vt_key);
      switch (type)
        {
        case ROOMLIST_ALL_ROOMS:
          return TRUE;
        case ROOMLIST_NO_ROOMS:
        case ROOMLIST_NPC_PART:
          return FALSE;

        case ROOMLIST_ONE_ROOM:
          vt_key[3].string = "Room";
          return prop_get_integer (bundle, "I<-siss", vt_key) == room + 1;

        case ROOMLIST_SOME_ROOMS:
          vt_key[3].string = "Rooms";
          vt_key[4].integer = room + 1;
          return prop_get_boolean (bundle, "B<-sissi", vt_key);

        default:
          sc_fatal ("obj_directly_in_room_internal:"
                    " invalid type, %ld\n", type);
          return FALSE;
        }
    }
  else
    return gs_object_position (game, object) == room + 1;
}

sc_bool
obj_directly_in_room (sc_gameref_t game, sc_int object, sc_int room)
{
  sc_bool result;

  /* Check, trace result, and return. */
  result = obj_directly_in_room_internal (game, object, room);

  if (obj_trace)
    {
      sc_trace ("Object: checking for object %ld directly in room %ld, %s\n",
                object, room, result ? "true" : "false");
    }

  return result;
}


/*
 * obj_indirectly_in_room_internal()
 * obj_indirectly_in_room()
 *
 * Return TRUE if a given object is currently in a given room, either
 * directly, on an object indirectly, in an open object indirectly, or
 * carried by an NPC in the room.
 */
static sc_bool
obj_indirectly_in_room_internal (sc_gameref_t game, sc_int object, sc_int room)
{
  const sc_prop_setref_t bundle = gs_get_bundle (game);

  /* See if the object is static or dynamic. */
  if (obj_is_static (game, object))
    {
      sc_vartype_t vt_key[5];
      sc_int type;

      /* Static object moved to player or room by event? */
      if (!gs_object_static_unmoved (game, object))
        {
          if (gs_object_position (game, object) == OBJ_HELD_PLAYER)
            return gs_player_in_room (game, room);
          else
            return gs_object_position (game, object) - 1 == room;
        }

      /* Check and return the room list for the object. */
      vt_key[0].string = "Objects";
      vt_key[1].integer = object;
      vt_key[2].string = "Where";
      vt_key[3].string = "Type";
      type = prop_get_integer (bundle, "I<-siss", vt_key);
      switch (type)
        {
        case ROOMLIST_ALL_ROOMS:
          return TRUE;
        case ROOMLIST_NO_ROOMS:
          return FALSE;

        case ROOMLIST_ONE_ROOM:
          vt_key[3].string = "Room";
          return prop_get_integer (bundle, "I<-siss", vt_key) == room + 1;

        case ROOMLIST_SOME_ROOMS:
          vt_key[3].string = "Rooms";
          vt_key[4].integer = room + 1;
          return prop_get_boolean (bundle, "B<-sissi", vt_key);

        case ROOMLIST_NPC_PART:
          {
            sc_int npc;

            vt_key[2].string = "Parent";
            npc = prop_get_integer (bundle, "I<-sis", vt_key);
            if (npc == 0)
              return gs_player_in_room (game, room);
            else
              return npc_in_room (game, npc - 1, room);
          }

        default:
          sc_fatal ("obj_indirectly_in_room_internal:"
                    " invalid type, %ld\n", type);
          return FALSE;
        }
    }
  else
    {
      sc_int parent, position;

      /* Get dynamic object's parent and position. */
      parent = gs_object_parent (game, object);
      position = gs_object_position (game, object);

      /* Decide depending on positioning. */
      switch (position)
        {
        case OBJ_HIDDEN:       /* Hidden. */
          return FALSE;

        case OBJ_HELD_PLAYER:  /* Held by player. */
        case OBJ_WORN_PLAYER:  /* Worn by player. */
          return gs_player_in_room (game, room);

        case OBJ_HELD_NPC:     /* Held by NPC. */
        case OBJ_WORN_NPC:     /* Worn by NPC. */
          return npc_in_room (game, parent, room);

        case OBJ_IN_OBJECT:    /* In another object. */
          {
            sc_int openness;

            openness = gs_object_openness (game, parent);
            switch (openness)
              {
              case OBJ_WONTCLOSE:
              case OBJ_OPEN:
                return obj_indirectly_in_room (game, parent, room);
              default:
                return FALSE;
              }
          }

        case OBJ_ON_OBJECT:    /* On another object. */
          return obj_indirectly_in_room (game, parent, room);

        default:               /* Within a room. */
          if (position > gs_room_count (game) + 1)
            {
              sc_error ("sc_object_indirectly_in_room:"
                        " position out of bounds, %ld\n", position);
            }
          return position - 1 == room;
        }
    }
}

sc_bool
obj_indirectly_in_room (sc_gameref_t game,
                        sc_int object, sc_int room)
{
  sc_bool result;

  /* Check, trace result, and return. */
  result = obj_indirectly_in_room_internal (game, object, room);

  if (obj_trace)
    {
      sc_trace ("Object: checking for object %ld indirectly in room %ld, %s\n",
                object, room, result ? "true" : "false");
    }

  return result;
}


/*
 * obj_indirectly_held_by_player_internal()
 * obj_indirectly_held_by_player()
 *
 * Return TRUE if a given object is currently held by the player, either
 * directly, on an object indirectly, or in an open object indirectly.
 */
static sc_bool
obj_indirectly_held_by_player_internal (sc_gameref_t game,
                                        sc_int object)
{
  /* See if the object is static or dynamic. */
  if (obj_is_static (game, object))
    {
      /* Static object moved to player or room by event? */
      if (!gs_object_static_unmoved (game, object))
        {
          if (gs_object_position (game, object) == OBJ_HELD_PLAYER)
            return TRUE;
          else
            return FALSE;
        }

      /* An unmoved static object is not held by the player. */
      return FALSE;
    }
  else
    {
      sc_int parent, position;

      /* Get dynamic object's parent and position. */
      parent = gs_object_parent (game, object);
      position = gs_object_position (game, object);

      /* Decide depending on positioning. */
      switch (position)
        {
        case OBJ_HIDDEN:       /* Hidden. */
          return FALSE;

        case OBJ_HELD_PLAYER:  /* Held by player. */
        case OBJ_WORN_PLAYER:  /* Worn by player. */
          return TRUE;

        case OBJ_HELD_NPC:     /* Held by NPC. */
        case OBJ_WORN_NPC:     /* Worn by NPC. */
          return FALSE;

        case OBJ_IN_OBJECT:    /* In another object. */
          {
            sc_int openness;

            openness = gs_object_openness (game, parent);
            switch (openness)
              {
              case OBJ_WONTCLOSE:
              case OBJ_OPEN:
                return obj_indirectly_held_by_player (game, parent);
              default:
                return FALSE;
              }
          }

        case OBJ_ON_OBJECT:    /* On another object. */
          return obj_indirectly_held_by_player (game, parent);

        default:               /* Within a room. */
          return FALSE;
        }
    }
}

sc_bool
obj_indirectly_held_by_player (sc_gameref_t game, sc_int object)
{
  sc_bool result;

  /* Check, trace result, and return. */
  result = obj_indirectly_held_by_player_internal (game, object);

  if (obj_trace)
    {
      sc_trace ("Object: checking for object %ld indirectly"
                " held by player, %s\n", object, result ? "true" : "false");
    }

  return result;
}


/*
 * sc_obj_shows_initial_description()
 *
 * Return TRUE if this object should be listed as room content.
 */
sc_bool
obj_shows_initial_description (sc_gameref_t game, sc_int object)
{
  const sc_prop_setref_t bundle = gs_get_bundle (game);
  sc_vartype_t vt_key[3];
  sc_int onlywhennotmoved;

  /* Get only when moved property. */
  vt_key[0].string = "Objects";
  vt_key[1].integer = object;
  vt_key[2].string = "OnlyWhenNotMoved";
  onlywhennotmoved = prop_get_integer (bundle, "I<-sis", vt_key);

  /* Combine this with game in mysterious ways. */
  switch (onlywhennotmoved)
    {
    case 0:
      return TRUE;

    case 1:
      return gs_object_unmoved (game, object);

    case 2:
      {
        sc_int initialposition;

        if (gs_object_unmoved (game, object))
          return TRUE;

        vt_key[2].string = "InitialPosition";
        initialposition = prop_get_integer (bundle, "I<-sis", vt_key) - 3;
        return gs_object_position (game, object) == initialposition;
      }
    }

  /* What you talkin' 'bout, Willis? */
  return FALSE;
}


/*
 * obj_turn_update()
 * obj_setup_initial()
 *
 * Set initial values for object states, and update after a turn.
 */
void
obj_turn_update (sc_gameref_t game)
{
  sc_int index_;

  /* Update object seen flag to current state. */
  for (index_ = 0; index_ < gs_object_count (game); index_++)
    {
      if (!gs_object_seen (game, index_)
          && obj_indirectly_in_room (game, index_, gs_playerroom (game)))
        gs_set_object_seen (game, index_, TRUE);
    }
}

void
obj_setup_initial (sc_gameref_t game)
{
  /* Set initial seen states for objects. */
  obj_turn_update (game);
}


/*
 * obj_debug_trace()
 *
 * Set object tracing on/off.
 */
void
obj_debug_trace (sc_bool flag)
{
  obj_trace = flag;
}

} // End of namespace Adrift
} // End of namespace Glk