Debian Codegraph @

terps/alan3/instance.c

raw symbols SCIP
1 #include "instance.h"
2
3 /* IMPORTS */
4 #include "memory.h"
5 #include "syserr.h"
6 #include "attribute.h"
7 #include "current.h"
8 #include "Container.h"
9 #include "options.h"
10 #include "debug.h"
11 #include "checkentry.h"
12 #include "inter.h"
13 #include "output.h"
14 #include "class.h"
15 #include "msg.h"
16 #include "actor.h"
17 #include "literal.h"
18 #include "dictionary.h"
19 #include "Location.h"
20 #include "compatibility.h"
21
22
23 /* PUBLIC DATA */
24
25 InstanceEntry *instances; /* Instance table pointer */
26
27 AdminEntry *admin; /* Administrative data about instances */
28 AttributeEntry *attributes; /* Dynamic attribute values */
29
30 /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
31
32 /* Instance query methods */
33
34 /*======================================================================*/
35 bool isA(int instance, int ancestor)
36 {
37 int parent;
38
39 if (isLiteral(instance))
40 parent = literals[instance-header->instanceMax].class;
41 else
42 parent = instances[instance].parent;
43 while (parent != 0 && parent != ancestor)
44 parent = classes[parent].parent;
45
46 return (parent != 0);
47 }
48
49
50 bool isAObject(int instance)
51 {
52 return isA(instance, OBJECT);
53 }
54
55 bool isAContainer(int instance)
56 {
57 return instance != 0 && !isLiteral(instance) && (instances[instance].container != 0);
58 }
59
60 bool isAActor(int instance)
61 {
62 return isA(instance, ACTOR);
63 }
64
65 bool isALocation(int instance)
66 {
67 return isA(instance, LOCATION);
68 }
69
70
71 bool isLiteral(int instance)
72 {
73 return instance > header->instanceMax;
74 }
75
76 bool isANumeric(int instance)
77 {
78 return isLiteral(instance) && literals[literalFromInstance(instance)].type == NUMERIC_LITERAL;
79 }
80
81 bool isAString(int instance)
82 {
83 return isLiteral(instance) && literals[literalFromInstance(instance)].type == STRING_LITERAL;
84 }
85
86
87 /*======================================================================*/
88 bool isOpaque(int container) {
89 return getInstanceAttribute(container, OPAQUEATTRIBUTE);
90 }
91
92
93 /*======================================================================*/
94 void setInstanceAttribute(int instance, int attribute, Aptr value)
95 {
96 char str[80];
97
98 if (instance > 0 && instance <= header->instanceMax) {
99 setAttribute(admin[instance].attributes, attribute, value);
100 if (isALocation(instance) && attribute != VISITSATTRIBUTE)
101 /* If it wasn't the VISITSATTRIBUTE the location may have
102 changed so describe next time */
103 admin[instance].visitsCount = 0;
104 } else {
105 sprintf(str, "Can't SET/MAKE instance (%d).", instance);
106 syserr(str);
107 }
108 }
109
110
111 /*======================================================================*/
112 void setInstanceStringAttribute(int instance, int attribute, char *string)
113 {
114 deallocate(fromAptr(getInstanceAttribute(instance, attribute)));
115 setInstanceAttribute(instance, attribute, toAptr(string));
116 }
117
118
119 /*======================================================================*/
120 void setInstanceSetAttribute(int instance, int attribute, Aptr set)
121 {
122 freeSet((Set *)fromAptr(getInstanceAttribute(instance, attribute)));
123 setInstanceAttribute(instance, attribute, set);
124 }
125
126
127 /*----------------------------------------------------------------------*/
128 static Aword getLiteralAttribute(int literal, int attribute)
129 {
130 if (isPreBeta3(header->version)) {
131 if (attribute == 1)
132 return literals[literalFromInstance(literal)].value;
133 else
134 return 0;
135 } else {
136 if (attribute == 0)
137 return literals[literalFromInstance(literal)].value;
138 else
139 return getAttribute(admin[header->instanceMax].attributes, attribute);
140 }
141 return(EOF);
142 }
143
144
145 /*======================================================================*/
146 Aword getInstanceAttribute(int instance, int attribute)
147 {
148 char str[80];
149
150 if (isLiteral(instance))
151 return getLiteralAttribute(instance, attribute);
152 else {
153 if (instance > 0 && instance <= header->instanceMax) {
154 if (attribute == -1)
155 return locationOf(instance);
156 else
157 return getAttribute(admin[instance].attributes, attribute);
158 } else {
159 sprintf(str, "Can't ATTRIBUTE instance %d.", instance);
160 syserr(str);
161 // No return from syserr()
162 return EOF;
163 }
164 }
165 }
166
167
168 /*======================================================================*/
169 bool hasAttribute(Aid instance, Aid attribute) {
170 return attributeExists(admin[instance].attributes, attribute);
171 }
172
173
174 /*======================================================================*/
175 char *getInstanceStringAttribute(int instance, int attribute)
176 {
177 return strdup((char *)fromAptr(getInstanceAttribute(instance, attribute)));
178 }
179
180
181 /*======================================================================*/
182 Set *getInstanceSetAttribute(int instance, int attribute)
183 {
184 return copySet((Set *)fromAptr(getInstanceAttribute(instance, attribute)));
185 }
186
187
188 /*----------------------------------------------------------------------*/
189 static void verifyInstance(int instance, char *action) {
190 char message[200];
191
192 if (instance == 0) {
193 sprintf(message, "Can't %s instance (%d).", action, instance);
194 syserr(message);
195 } else if (instance > header->instanceMax) {
196 sprintf(message, "Can't %s instance (%d > instanceMax).", action, instance);
197 syserr(message);
198 }
199 }
200
201
202 /*======================================================================*/
203 bool isHere(int id, ATrans transitivity)
204 {
205 verifyInstance(id, "HERE");
206
207 return isAt(id, current.location, transitivity);
208 }
209
210
211 /*======================================================================*/
212 bool isNearby(int instance, ATrans transitivity)
213 {
214 verifyInstance(instance, "NEARBY");
215
216 if (isALocation(instance))
217 return exitto(current.location, instance);
218 else
219 return exitto(current.location, where(instance, transitivity));
220 }
221
222
223 /*======================================================================*/
224 bool isNear(int instance, int other, ATrans trans)
225 {
226 Aint l1, l2;
227
228 verifyInstance(instance, "NEAR");
229
230 if (isALocation(instance))
231 l1 = instance;
232 else
233 l1 = where(instance, trans);
234 if (isALocation(other))
235 l2 = other;
236 else
237 l2 = where(other, trans);
238 return exitto(l2, l1);
239 }
240
241
242 /*======================================================================*/
243 /* Look in a container to see if the instance is in it. */
244 bool isIn(int instance, int container, ATrans trans)
245 {
246 int loc;
247
248 if (!isAContainer(container))
249 syserr("IN in a non-container.");
250
251 if (trans == DIRECT)
252 return admin[instance].location == container;
253 else {
254 loc = admin[instance].location;
255 if (trans == INDIRECT && loc != 0 && !isA(loc, LOCATION))
256 loc = admin[loc].location;
257 while (loc != 0 && !isA(loc, LOCATION))
258 if (loc == container)
259 return true;
260 else
261 loc = admin[loc].location;
262 return false;
263 }
264 }
265
266
267
268 /*======================================================================*/
269 /* Look see if an instance is AT another. */
270 bool isAt(int instance, int other, ATrans trans)
271 {
272 if (instance == 0 || other == 0) return false;
273
274 if (isALocation(instance)) {
275 /* Nested locations */
276 /* TODO - What if the other is not a location? */
277 int current = admin[instance].location;
278 switch (trans) {
279 case DIRECT:
280 return admin[instance].location == other;
281 case INDIRECT:
282 if (current == other)
283 return false;
284 current = admin[current].location;
285 case TRANSITIVE:
286 while (current != 0) {
287 if (current == other)
288 return true;
289 else
290 current = admin[current].location;
291 }
292 return false;
293 }
294 syserr("Unexpected value in switch in isAt() for location");
295 return false;
296 } else if (isALocation(other)) {
297 /* Instance is not a location but other is */
298 switch (trans) {
299 case DIRECT:
300 return admin[instance].location == other;
301 case INDIRECT: {
302 if (admin[instance].location == other)
303 return false; /* Directly, so not Indirectly */
304 /* Fall through to transitive handling of the location */
305 }
306 case TRANSITIVE: {
307 int location = locationOf(instance);
308 int current = other;
309 while (current != 0) {
310 if (current == location)
311 return true;
312 else
313 current = admin[current].location;
314 }
315 return false;
316 }
317 }
318 syserr("Unexpected value in switch in isAt() for non-location");
319 return false;
320 } else {
321 /* Other is also not a location */
322 switch (trans) {
323 case DIRECT:
324 return positionOf(instance) == admin[other].location;
325 case INDIRECT: {
326 int location = locationOf(instance);
327 int current = other;
328 if (location == current)
329 return false;
330 else
331 current = admin[current].location;
332 while (current != 0) {
333 if (current == location)
334 return true;
335 else
336 current = admin[current].location;
337 }
338 return false;
339 }
340 case TRANSITIVE: {
341 int location = locationOf(other);
342 int current = locationOf(instance);
343 bool ok = false;
344 while (current != 0 && !ok) {
345 if (current == location)
346 ok = true;
347 else
348 current = admin[current].location;
349 }
350 return ok;
351 }
352 }
353 syserr("Unexpected value in switch in isAt() for non-location");
354 return false;
355 }
356 }
357
358
359 /*======================================================================*/
360 /* Return the *location* of an instance, transitively, i.e. the first
361 location instance found when traversing the containment/position
362 links. If that didn't turn up a location see if it was in a
363 container that is somewhere, or a THING that is nowhere. It might
364 also be an ENTITY which is always everywhere so take that to mean
365 where the hero is. */
366 int locationOf(int instance)
367 {
368 int position;
369 int container = 0;
370
371 verifyInstance(instance, "get LOCATION of");
372
373 position = admin[instance].location;
374 while (position != 0 && !isALocation(position)) {
375 container = position; /* Remember innermost container */
376 position = admin[position].location;
377 }
378 if (position > NOWHERE) /* It was a location so return that */
379 return position;
380 else {
381 /* If we did not find a location then it might be in a container */
382 if (container != 0)
383 instance = container;
384 /* If the instance or the container it was in is a THING then its nowhere. */
385 if (isA(instance, THING))
386 return NOWHERE; /* #nowhere */
387 else if (isALocation(instance))
388 return NO_LOCATION; /* No location */
389 else
390 return locationOf(HERO);
391 }
392 }
393
394
395 /*======================================================================*/
396 /* Return the current position of an instance, directly or not */
397 /* TODO: this will be a possible duplicate of where() */
398 int positionOf(int instance)
399 {
400 return admin[instance].location;
401 }
402
403
404 /*======================================================================*/
405 /* Return the current position of an instance, directly or not */
406 int where(int instance, ATrans trans)
407 {
408 verifyInstance(instance, "WHERE");
409
410 if (isALocation(instance))
411 return 0;
412 else if (trans == DIRECT)
413 return admin[instance].location;
414 else
415 return locationOf(instance);
416 }
417
418
419 /*----------------------------------------------------------------------*/
420 static bool executeInheritedMentioned(int class) {
421 if (class == 0) return false;
422
423 if (classes[class].mentioned) {
424 interpret(classes[class].mentioned);
425 return true;
426 } else
427 return executeInheritedMentioned(classes[class].parent);
428 }
429
430
431 /*----------------------------------------------------------------------*/
432 static bool mention(int instance) {
433 if (instances[instance].mentioned) {
434 interpret(instances[instance].mentioned);
435 return true;
436 } else
437 return executeInheritedMentioned(instances[instance].parent);
438 }
439
440
441 /*======================================================================*/
442 void sayInstance(int instance)
443 {
444 #ifdef SAY_INSTANCE_WITH_PLAYER_WORDS_IF_PARAMETER
445 int p, i;
446
447 /* Find the id in the parameters... */
448 if (params != NULL)
449 for (p = 0; params[p].code != EOF; p++)
450 if (params[p].code == instance) {
451 /* Found it so.. */
452 if (params[p].firstWord == EOF) /* Any words he used? */
453 break; /* No... */
454 else { /* Yes, so use them... */
455 char *capitalized;
456 /* Assuming the noun is the last word we can simply output the adjectives... */
457 for (i = params[p].firstWord; i <= params[p].lastWord-1; i++)
458 output((char *)pointerTo(dict[wrds[i]].wrd));
459 /* ... and then the noun, capitalized if necessary */
460 if (header->capitalizeNouns) {
461 capitalized = strdup((char *)pointerTo(dict[wrds[params[p].lastWord]].wrd));
462 capitalized[0] = toUpper(capitalized[0]);
463 output(capitalized);
464 deallocate(capitalized);
465 } else
466 output((char *)pointerTo(dict[wrds[params[p].lastWord]].wrd));
467 }
468 return;
469 }
470 #endif
471 if (!mention(instance))
472 interpret(instances[instance].name);
473 }
474
475
476 /*======================================================================*/
477 void sayInteger(int value)
478 {
479 char buf[25];
480
481 if (isHere(HERO, false)) {
482 sprintf(buf, "%d", value);
483 output(buf);
484 }
485 }
486
487
488 /*======================================================================*/
489 void sayString(char *string)
490 {
491 if (isHere(HERO, false))
492 output(string);
493 deallocate(string);
494 }
495
496
497 /*----------------------------------------------------------------------*/
498 static void sayLiteral(int literal)
499 {
500 char *str;
501
502 if (isANumeric(literal))
503 sayInteger(literals[literal-header->instanceMax].value);
504 else {
505 str = (char *)strdup((char *)fromAptr(literals[literal-header->instanceMax].value));
506 sayString(str);
507 }
508 }
509
510
511 /*----------------------------------------------------------------------*/
512 static char *wordWithCode(int classBit, int code) {
513 int w;
514 char str[50];
515
516 for (w = 0; w < dictionarySize; w++)
517 if (dictionary[w].code == code && ((classBit&dictionary[w].classBits) != 0))
518 return pointerTo(dictionary[w].string);
519 sprintf(str, "Could not find word of class %d with code %d.", classBit, code);
520 syserr(str);
521 return NULL;
522 }
523
524
525 /*----------------------------------------------------------------------*/
526 static bool sayInheritedDefiniteForm(int class) {
527 if (class == 0) {
528 syserr("No default definite article");
529 return false;
530 } else {
531 if (classes[class].definite.address) {
532 interpret(classes[class].definite.address);
533 return classes[class].definite.isForm;
534 } else
535 return sayInheritedDefiniteForm(classes[class].parent);
536 }
537 }
538
539
540 /*----------------------------------------------------------------------*/
541 static void sayDefinite(int instance) {
542 if (instances[instance].definite.address) {
543 interpret(instances[instance].definite.address);
544 if (!instances[instance].definite.isForm)
545 sayInstance(instance);
546 } else
547 if (!sayInheritedDefiniteForm(instances[instance].parent))
548 sayInstance(instance);
549 }
550
551
552 /*----------------------------------------------------------------------*/
553 static bool sayInheritedIndefiniteForm(int class) {
554 if (class == 0) {
555 syserr("No default indefinite article");
556 return false;
557 } else {
558 if (classes[class].indefinite.address) {
559 interpret(classes[class].indefinite.address);
560 return classes[class].indefinite.isForm;
561 } else
562 return sayInheritedIndefiniteForm(classes[class].parent);
563 }
564 }
565
566
567 /*----------------------------------------------------------------------*/
568 static void sayIndefinite(int instance) {
569 if (instances[instance].indefinite.address) {
570 interpret(instances[instance].indefinite.address);
571 if (!instances[instance].indefinite.isForm)
572 sayInstance(instance);
573 } else
574 if (!sayInheritedIndefiniteForm(instances[instance].parent))
575 sayInstance(instance);
576 }
577
578
579 /*----------------------------------------------------------------------*/
580 static bool sayInheritedNegativeForm(int class) {
581 if (class == 0) {
582 syserr("No default negative form");
583 return false;
584 } else {
585 if (classes[class].negative.address) {
586 interpret(classes[class].negative.address);
587 return classes[class].negative.isForm;
588 } else
589 return sayInheritedNegativeForm(classes[class].parent);
590 }
591 }
592
593
594 /*----------------------------------------------------------------------*/
595 static void sayNegative(int instance) {
596 if (instances[instance].negative.address) {
597 interpret(instances[instance].negative.address);
598 if (!instances[instance].negative.isForm)
599 sayInstance(instance);
600 } else
601 if (!sayInheritedNegativeForm(instances[instance].parent))
602 sayInstance(instance);
603 }
604
605
606 /*----------------------------------------------------------------------*/
607 static void sayInheritedPronoun(int instance) {
608 if (instance == 0)
609 syserr("No default pronoun");
610 else {
611 if (classes[instance].pronoun != 0)
612 output(wordWithCode(PRONOUN_BIT, classes[instance].pronoun));
613 else
614 sayInheritedPronoun(classes[instance].parent);
615 }
616 }
617
618
619 /*----------------------------------------------------------------------*/
620 static void sayPronoun(int instance) {
621 if (instances[instance].pronoun != 0)
622 output(wordWithCode(PRONOUN_BIT, instances[instance].pronoun));
623 else
624 sayInheritedPronoun(instances[instance].parent);
625 }
626
627
628 /*----------------------------------------------------------------------*/
629 static void sayArticleOrForm(int id, SayForm form)
630 {
631 if (!isLiteral(id))
632 switch (form) {
633 case SAY_DEFINITE:
634 sayDefinite(id);
635 break;
636 case SAY_INDEFINITE:
637 sayIndefinite(id);
638 break;
639 case SAY_NEGATIVE:
640 sayNegative(id);
641 break;
642 case SAY_PRONOUN:
643 sayPronoun(id);
644 break;
645 case SAY_SIMPLE:
646 say(id);
647 break;
648 default:
649 syserr("Unexpected form in 'sayArticleOrForm()'");
650 }
651 else
652 say(id);
653 }
654
655
656 /*======================================================================*/
657 void say(int instance)
658 {
659 Aword previousInstance = current.instance;
660 current.instance = instance;
661
662 if (isHere(HERO, false)) {
663 if (isLiteral(instance))
664 sayLiteral(instance);
665 else {
666 verifyInstance(instance, "SAY");
667 sayInstance(instance);
668 }
669 }
670 current.instance = previousInstance;
671 }
672
673
674 /*======================================================================*/
675 void sayForm(int instance, SayForm form)
676 {
677 Aword previousInstance = current.instance;
678 current.instance = instance;
679
680 sayArticleOrForm(instance, form);
681
682 current.instance = previousInstance;
683 }
684
685
686 /*======================================================================*/
687 bool isDescribable(int instance) {
688 return isAObject(instance) || isAActor(instance);
689 }
690
691
692 /*----------------------------------------------------------------------*/
693 static bool inheritsDescriptionFrom(int class)
694 {
695 if (classes[class].description != 0)
696 return true;
697 else if (classes[class].parent != 0)
698 return inheritsDescriptionFrom(classes[class].parent);
699 else
700 return false;
701 }
702
703
704 /*======================================================================*/
705 bool hasDescription(int instance)
706 {
707 if (instances[instance].description != 0)
708 return true;
709 else if (instances[instance].parent != 0)
710 return inheritsDescriptionFrom(instances[instance].parent);
711 else
712 return false;
713 }
714
715
716 /*----------------------------------------------------------------------*/
717 static void describeClass(int instance)
718 {
719 if (classes[instance].description != 0) {
720 /* This class has a description, run it */
721 interpret(classes[instance].description);
722 } else {
723 /* Search up the inheritance tree, if any, to find a description */
724 if (classes[instance].parent != 0)
725 describeClass(classes[instance].parent);
726 }
727 }
728
729
730 /*======================================================================*/
731 void describeAnything(int instance)
732 {
733 if (instances[instance].description != 0) {
734 /* This instance has its own description, run it */
735 interpret(instances[instance].description);
736 } else {
737 /* Search up the inheritance tree to find a description */
738 if (instances[instance].parent != 0)
739 describeClass(instances[instance].parent);
740 }
741 admin[instance].alreadyDescribed = true;
742 }
743
744
745 /*----------------------------------------------------------------------*/
746 static void describeObject(int object)
747 {
748 if (hasDescription(object))
749 describeAnything(object);
750 else {
751 printMessageWithInstanceParameter(M_SEE_START, object);
752 printMessage(M_SEE_END);
753 if (instances[object].container != 0)
754 describeContainer(object);
755 }
756 admin[object].alreadyDescribed = true;
757 }
758
759
760 /*----------------------------------------------------------------------*/
761 static bool inheritedDescriptionCheck(int class)
762 {
763 if (class == 0) return true;
764 if (!inheritedDescriptionCheck(classes[class].parent)) return false;
765 if (classes[class].descriptionChecks == 0) return true;
766 return !checksFailed(classes[class].descriptionChecks, true);
767 }
768
769 /*----------------------------------------------------------------------*/
770 static bool descriptionCheck(int instance)
771 {
772 int previousInstance = current.instance;
773 bool r;
774
775 current.instance = instance;
776 if (inheritedDescriptionCheck(instances[instance].parent)) {
777 if (instances[instance].checks == 0)
778 r = true;
779 else
780 r = !checksFailed(instances[instance].checks, true);
781 } else
782 r = false;
783 current.instance = previousInstance;
784 return r;
785 }
786
787
788 /*======================================================================*/
789 void describeInstances(void)
790 {
791 int i;
792 int lastInstanceFound = 0;
793 int found = 0;
794
795 /* First describe every object here with its own description */
796 for (i = 1; i <= header->instanceMax; i++)
797 if (admin[i].location == current.location && isAObject(i) &&
798 !admin[i].alreadyDescribed && hasDescription(i))
799 describe(i);
800
801 /* Then list all things without a description */
802 for (i = 1; i <= header->instanceMax; i++)
803 if (admin[i].location == current.location
804 && !admin[i].alreadyDescribed
805 && isAObject(i)
806 && descriptionCheck(i)) {
807 if (found == 0)
808 printMessageWithInstanceParameter(M_SEE_START, i);
809 else if (found > 1)
810 printMessageWithInstanceParameter(M_SEE_COMMA, lastInstanceFound);
811 admin[i].alreadyDescribed = true;
812
813 // TODO : isOpaque()
814 if (instances[i].container && containerSize(i, DIRECT) > 0 && !getInstanceAttribute(i, OPAQUEATTRIBUTE)) {
815 if (found > 0)
816 printMessageWithInstanceParameter(M_SEE_AND, i);
817 printMessage(M_SEE_END);
818 describeContainer(i);
819 found = 0;
820 continue; /* Actually start another list. */
821 }
822 found++;
823 lastInstanceFound = i;
824 }
825
826 if (found > 0) {
827 if (found > 1) {
828 printMessageWithInstanceParameter(M_SEE_AND, lastInstanceFound);
829 }
830 printMessage(M_SEE_END);
831 }
832
833 /* Finally all actors with a separate description */
834 for (i = 1; i <= header->instanceMax; i++)
835 if (admin[i].location == current.location && i != HERO && isAActor(i)
836 && !admin[i].alreadyDescribed)
837 describe(i);
838
839 /* Clear the describe flag for all instances */
840 for (i = 1; i <= header->instanceMax; i++)
841 admin[i].alreadyDescribed = false;
842 }
843
844
845 /*======================================================================*/
846 bool describe(int instance)
847 {
848 bool descriptionOk;
849 int previousInstance = current.instance;
850
851 current.instance = instance;
852 verifyInstance(instance, "DESCRIBE");
853 if (descriptionCheck(instance)) {
854 descriptionOk = true;
855 if (isAObject(instance)) {
856 describeObject(instance);
857 } else if (isAActor(instance)) {
858 describeActor(instance);
859 } else
860 describeAnything(instance);
861 } else
862 descriptionOk = false;
863 current.instance = previousInstance;
864 return descriptionOk;
865 }
866
867
868 /*----------------------------------------------------------------------*/
869 static void locateIntoContainer(Aword theInstance, Aword theContainer) {
870 if (!isA(theInstance, containers[instances[theContainer].container].class))
871 printMessageUsing2InstanceParameters(M_CANNOTCONTAIN, theContainer, theInstance);
872 else if (passesContainerLimits(theContainer, theInstance))
873 admin[theInstance].location = theContainer;
874 else
875 abortPlayerCommand();
876 }
877
878
879 /*----------------------------------------------------------------------*/
880 static void locateLocation(Aword loc, Aword whr)
881 {
882 Aint l = whr;
883
884 /* Ensure this does not create a recursive location chain */
885 while (l != 0) {
886 if (admin[l].location == loc)
887 apperr("Locating a location that would create a recursive loop of locations containing each other.");
888 else
889 l = admin[l].location;
890 }
891 admin[loc].location = whr;
892 }
893
894
895 /*----------------------------------------------------------------------*/
896 static void locateObject(Aword obj, Aword whr)
897 {
898 if (isAContainer(whr)) { /* Into a container */
899 locateIntoContainer(obj, whr);
900 } else {
901 admin[obj].location = whr;
902 /* Make sure the location is described since it's changed */
903 admin[whr].visitsCount = 0;
904 }
905 }
906
907
908 /*----------------------------------------------------------------------*/
909 static void traceEnteredClass(Aint theClass, bool empty) {
910 printf("\n<ENTERED in class ");
911 printf("%s", idOfClass(theClass));
912 printf("[%d]%s>\n", theClass, empty?" is empty":":");
913 }
914
915
916 /*----------------------------------------------------------------------*/
917 static void traceEnteredInstance(Aint instance, bool empty) {
918 printf("\n<ENTERED in instance ");
919 traceSay(instance);
920 printf("[%d]%s>\n", instance, empty?" is empty":"");
921 }
922
923
924 /*----------------------------------------------------------------------*/
925 static void executeInheritedEntered(Aint theClass) {
926 if (theClass == 0) return;
927 executeInheritedEntered(classes[theClass].parent);
928 if (traceSectionOption)
929 traceEnteredClass(theClass, classes[theClass].entered == 0);
930 if (classes[theClass].entered) {
931 interpret(classes[theClass].entered);
932 }
933 }
934
935
936 /*----------------------------------------------------------------------*/
937 static void executeEntered(Aint instance) {
938 int currentInstance = current.instance;
939 current.instance = instance;
940 if (admin[instance].location != 0)
941 executeEntered(admin[instance].location);
942 executeInheritedEntered(instances[instance].parent);
943 if (traceSectionOption)
944 traceEnteredInstance(instance, instances[instance].entered == 0);
945 if (instances[instance].entered != 0) {
946 interpret(instances[instance].entered);
947 }
948 current.instance = currentInstance;
949 }
950
951
952 /*----------------------------------------------------------------------*/
953 static int getVisits(int location) {
954 return getInstanceAttribute(location, VISITSATTRIBUTE);
955 }
956
957
958 /*----------------------------------------------------------------------*/
959 static void incrementVisits(int location) {
960 setInstanceAttribute(location, VISITSATTRIBUTE, getVisits(location)+1);
961 if (admin[location].location != 0)
962 /* Nested location, so increment that too */
963 incrementVisits(admin[location].location);
964 }
965
966
967 /*----------------------------------------------------------------------*/
968 static void revisited(void) {
969 if (anyOutput)
970 para();
971 say(where(HERO, DIRECT));
972 printMessage(M_AGAIN);
973 newline();
974 describeInstances();
975 }
976
977
978 /*----------------------------------------------------------------------*/
979 static bool shouldBeDescribed(void) {
980 if (!isPreBeta5(header->version))
981 return getVisits(admin[HERO].location) % (current.visits+1) == 0
982 || admin[admin[HERO].location].visitsCount == 0;
983 else
984 return admin[admin[HERO].location].visitsCount % (current.visits+1) == 0;
985 }
986
987
988 /*----------------------------------------------------------------------*/
989 static void locateActor(Aint movingActor, Aint whr)
990 {
991 Aint previousCurrentLocation = current.location;
992 Aint previousActorLocation = admin[movingActor].location;
993 Aint previousActor = current.actor;
994 Aint previousInstance = current.instance;
995
996 /* Before leaving, remember that we visited the location */
997 if (!isPreBeta5(header->version))
998 if (movingActor == HERO)
999 incrementVisits(where(HERO, DIRECT));
1000
1001 /* TODO Actors locating into containers is dubious, anyway as it
1002 is now it allows the hero to be located into a container. And what
1003 happens with current location if so... */
1004 if (isAContainer(whr))
1005 locateIntoContainer(movingActor, whr);
1006 else {
1007 current.location = whr;
1008 admin[movingActor].location = whr;
1009 }
1010
1011 /* Now we have moved, so show what is needed... */
1012 current.instance = current.location;
1013
1014 /* Execute possible entered */
1015 current.actor = movingActor;
1016 if (previousActorLocation != current.location) {
1017 executeEntered(current.location);
1018 }
1019 current.instance = previousInstance;
1020 current.actor = previousActor;
1021
1022 if (movingActor == HERO) {
1023 if (shouldBeDescribed())
1024 look();
1025 else
1026 revisited();
1027 admin[where(HERO, DIRECT)].visitsCount++;
1028 } else
1029 /* Ensure that the location will be described to the hero next time */
1030 admin[whr].visitsCount = 0;
1031
1032 if (current.actor != movingActor)
1033 current.location = previousCurrentLocation;
1034
1035 current.instance = previousInstance;
1036 }
1037
1038
1039 /*----------------------------------------------------------------------*/
1040 static void traceExtract(int instance, int containerId, char *what) {
1041 if (traceSectionOption) {
1042 printf("\n<EXTRACT from ");
1043 traceSay(instance);
1044 printf("[%d, container %d], %s:>\n", instance, containerId, what);
1045 }
1046 }
1047
1048
1049 /*----------------------------------------------------------------------*/
1050 static void containmentLoopError(int instance, int whr) {
1051 ParameterArray parameters = newParameterArray();
1052 if (isPreBeta4(header->version))
1053 output("That would be to put something inside itself.");
1054 else if (whr == instance) {
1055 addParameterForInstance(parameters, instance);
1056 printMessageWithParameters(M_CONTAINMENT_LOOP, parameters);
1057 } else {
1058 addParameterForInstance(parameters, instance);
1059 addParameterForInstance(parameters, whr);
1060 printMessageWithParameters(M_CONTAINMENT_LOOP2, parameters);
1061 }
1062 free(parameters);
1063 error(NO_MSG);
1064 }
1065
1066
1067 /*----------------------------------------------------------------------*/
1068 static void runExtractStatements(int instance, int containerId) {
1069 ContainerEntry *theContainer = &containers[containerId];
1070
1071 if (theContainer->extractStatements != 0) {
1072 traceExtract(instance, containerId, "Executing");
1073 interpret(theContainer->extractStatements);
1074 }
1075 }
1076
1077
1078 /*----------------------------------------------------------------------*/
1079 static bool runExtractChecks(int instance, int containerId) {
1080 ContainerEntry *theContainer = &containers[containerId];
1081
1082 if (theContainer->extractChecks != 0) {
1083 traceExtract(instance, containerId, "Checking");
1084 if (checksFailed(theContainer->extractChecks, EXECUTE_CHECK_BODY_ON_FAIL)) {
1085 fail = true;
1086 return false; /* Failed! */
1087 }
1088 }
1089 return true; /* Passed! */
1090 }
1091
1092
1093 /*======================================================================*/
1094 void locate(int instance, int whr)
1095 {
1096 int containerId;
1097 int previousInstance = current.instance;
1098
1099 verifyInstance(instance, "LOCATE");
1100 verifyInstance(whr, "LOCATE AT");
1101
1102 /* Will this create a containment loop? */
1103 if (whr == instance || (isAContainer(instance) && isIn(whr, instance, TRANSITIVE)))
1104 containmentLoopError(instance, whr);
1105
1106 /* First check if the instance is in a container, if so run extract checks */
1107 if (isAContainer(admin[instance].location)) { /* In something? */
1108 int loc = admin[instance].location;
1109
1110 /* Run all nested extraction checks */
1111 while (isAContainer(loc)) {
1112 current.instance = loc;
1113 containerId = instances[loc].container;
1114
1115 if (!runExtractChecks(instance, containerId)) {
1116 current.instance = previousInstance;
1117 return;
1118 }
1119 runExtractStatements(instance, containerId);
1120 loc = admin[loc].location;
1121 }
1122 current.instance = previousInstance;
1123 }
1124
1125 if (isAActor(instance))
1126 locateActor(instance, whr);
1127 else if (isALocation(instance))
1128 locateLocation(instance, whr);
1129 else
1130 locateObject(instance, whr);
1131
1132 gameStateChanged = true;
1133 }