Debian Codegraph @

terps/alan3/Container.c

raw symbols SCIP
1 #include "Container.h"
2
3 #include "instance.h"
4 #include "syserr.h"
5 #include "inter.h"
6 #include "lists.h"
7 #include "memory.h"
8 #include "current.h"
9 #include "msg.h"
10 #include "output.h"
11
12
13 /* PUBLIC DATA */
14 ContainerEntry *containers; /* Container table pointer */
15
16
17 /*----------------------------------------------------------------------*/
18 static int countInContainer(Aid containerIndex) /* IN - the container to count in */
19 {
20 int j = 0;
21
22 for (int instanceIndex = 1; instanceIndex <= header->instanceMax; instanceIndex++)
23 if (isIn(instanceIndex, containerIndex, DIRECT))
24 /* Then it's in this container also */
25 j++;
26 return j;
27 }
28
29
30 /*----------------------------------------------------------------------*/
31 static int sumAttributeInContainer(
32 Aid containerIndex, /* IN - the container to sum */
33 Aid attributeIndex /* IN - the attribute to sum over */
34 ) {
35 int sum = 0;
36
37 for (int instanceIndex = 1; instanceIndex <= header->instanceMax; instanceIndex++)
38 if (isIn(instanceIndex, containerIndex, TRANSITIVE)) {
39 /* We might find surprises in nested containers... */
40 if (hasAttribute(instanceIndex, attributeIndex))
41 sum = sum + getInstanceAttribute(instanceIndex, attributeIndex);
42 }
43 return sum;
44 }
45
46
47 /*----------------------------------------------------------------------*/
48 static bool containerIsEmpty(int container)
49 {
50 for (int i = 1; i <= header->instanceMax; i++)
51 if (isDescribable(i) && isIn(i, container, TRANSITIVE))
52 return false;
53 return true;
54 }
55
56
57 /*======================================================================*/
58 void describeContainer(int container)
59 {
60 if (!containerIsEmpty(container) && !isOpaque(container))
61 list(container);
62 }
63
64
65 /*======================================================================*/
66 bool passesContainerLimits(Aint theContainerInstance, Aint theAddedInstance) {
67 Aint containerProps;
68
69 if (!isAContainer(theContainerInstance))
70 syserr("Checking limits for a non-container.");
71
72 /* Find the container properties */
73 containerProps = instances[theContainerInstance].container;
74
75 if (containers[containerProps].limits != 0) { /* Any limits at all? */
76 for (LimitEntry *limit = (LimitEntry *) pointerTo(containers[containerProps].limits); !isEndOfArray(limit); limit++)
77 if (limit->atr == 1-I_COUNT) { /* TODO This is actually some encoding of the attribute number, right? */
78 if (countInContainer(theContainerInstance) >= limit->val) {
79 interpret(limit->stms);
80 return false;
81 }
82 } else {
83 /* First check nested containers... */
84 int currentSum = sumAttributeInContainer(theContainerInstance, limit->atr);
85 int addedSum = getInstanceAttribute(theAddedInstance, limit->atr);
86 if (isAContainer(theAddedInstance))
87 addedSum += sumAttributeInContainer(theAddedInstance, limit->atr);
88 if (currentSum + addedSum > limit->val) {
89 interpret(limit->stms);
90 return false;
91 }
92 }
93 }
94 /* Then check any possible containing containers upwards using recursion... */
95 Aid location = where(theContainerInstance, DIRECT);
96 if (isAContainer(location))
97 return passesContainerLimits(location, theAddedInstance);
98 return true;
99 }
100
101
102 /*======================================================================*/
103 int containerSize(int container, ATrans trans) {
104 Aint count = 0;
105
106 for (Aid i = 1; i <= header->instanceMax; i++) {
107 if (isIn(i, container, trans))
108 count++;
109 }
110 return count;
111 }
112
113 /*======================================================================*/
114 void list(int container)
115 {
116 Aword containerProps;
117 Aword foundInstance[2] = {0,0};
118 int found = 0;
119 Aint previousThis = current.instance;
120
121 current.instance = container;
122
123 /* Find container table entry */
124 containerProps = instances[container].container;
125 if (containerProps == 0) syserr("Trying to list something not a container.");
126
127 for (Aid i = 1; i <= header->instanceMax; i++) {
128 if (isDescribable(i)) {
129 /* We can only see objects and actors directly in this container... */
130 if (admin[i].location == container) { /* Yes, it's in this container */
131 if (found == 0) {
132 if (containers[containerProps].header != 0)
133 interpret(containers[containerProps].header);
134 else {
135 if (isAActor(containers[containerProps].owner))
136 printMessageWithInstanceParameter(M_CARRIES, containers[containerProps].owner);
137 else
138 printMessageWithInstanceParameter(M_CONTAINS, containers[containerProps].owner);
139 }
140 foundInstance[0] = i;
141 } else if (found == 1)
142 foundInstance[1] = i;
143 else {
144 printMessageWithInstanceParameter(M_CONTAINS_COMMA, i);
145 }
146 found++;
147 }
148 }
149 }
150
151 if (found > 0) {
152 if (found > 1)
153 printMessageWithInstanceParameter(M_CONTAINS_AND, foundInstance[1]);
154 printMessageWithInstanceParameter(M_CONTAINS_END, foundInstance[0]);
155 } else {
156 if (containers[containerProps].empty != 0)
157 interpret(containers[containerProps].empty);
158 else {
159 if (isAActor(containers[containerProps].owner))
160 printMessageWithInstanceParameter(M_EMPTYHANDED, containers[containerProps].owner);
161 else
162 printMessageWithInstanceParameter(M_EMPTY, containers[containerProps].owner);
163 }
164 }
165 needSpace = true;
166 current.instance = previousThis;
167 }