1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.zmpp.vm;
24
25 import org.zmpp.base.MemoryAccess;
26
27 /***
28 * This class represents the object table.
29 *
30 * @author Wei-ju Wu
31 * @version 1.0
32 */
33 public class Objects implements ObjectTree {
34
35 /***
36 * The memory access object.
37 */
38 private MemoryAccess memaccess;
39
40 /***
41 * The object table's start address.
42 */
43 private int address;
44
45 /***
46 * Constructor.
47 *
48 * @param memaccess the memory access object
49 * @param address the object table's start address
50 */
51 public Objects(MemoryAccess memaccess, int address) {
52
53 this.memaccess = memaccess;
54 this.address = address;
55 }
56
57 /***
58 * {@inheritDoc}
59 */
60 public short getPropertyDefault(int propertyNum) {
61
62 int index = propertyNum - 1;
63 return memaccess.readShort(address + index * 2);
64 }
65
66 /***
67 * {@inheritDoc}
68 */
69 public int getNumObjects() {
70
71
72
73
74
75 return (getObject((short) 1).getPropertyTableAddress()
76 - getObjectTreeStart()) / getObjectEntrySize();
77 }
78
79 /***
80 * {@inheritDoc}
81 */
82 public ZObject getObject(int objectNum) {
83
84 int objTreeStart = address + getPropertyDefaultsSize();
85
86
87 return new DefaultZObject(memaccess,
88 objTreeStart + (objectNum - 1)* getObjectEntrySize());
89 }
90
91 /***
92 * {@inheritDoc}
93 */
94 public void removeObject(int objectNum) {
95
96 ZObject obj = getObject(objectNum);
97 ZObject parentObj = getObject(obj.getParent());
98 obj.setParent((short) 0);
99
100 if (parentObj.getChild() == objectNum) {
101
102 parentObj.setChild(obj.getSibling());
103
104 } else {
105
106
107
108
109 ZObject currentChild = getObject(parentObj.getChild());
110 int sibling = currentChild.getSibling();
111
112 while (sibling != objectNum) {
113
114 currentChild = getObject(sibling);
115 sibling = currentChild.getSibling();
116 }
117 currentChild.setSibling(obj.getSibling());
118 }
119 obj.setSibling((short) 0);
120 }
121
122 /***
123 * {@inheritDoc}
124 */
125 public void insertObject(int parentNum, int objectNum) {
126
127 ZObject parent = getObject(parentNum);
128 ZObject child = getObject(objectNum);
129
130
131 if (child.getParent() > 0) {
132
133 removeObject(objectNum);
134 }
135
136 int oldChild = parent.getChild();
137
138 child.setParent(parentNum);
139 parent.setChild(objectNum);
140 child.setSibling(oldChild);
141 }
142
143 /***
144 * {@inheritDoc}
145 */
146 public int getPropertyLength(int propertyAddress) {
147
148 if (propertyAddress == 0) return 0;
149
150 short sizebyte = memaccess.readUnsignedByte(propertyAddress - 1);
151 return sizebyte / 32 + 1;
152 }
153
154 /***
155 * The size of the property defaults section.
156 *
157 * @return the property defaults section
158 */
159 private int getPropertyDefaultsSize() {
160
161 return 31 * 2;
162 }
163
164 /***
165 * Returns the start address of the object tree section.
166 *
167 * @return the object tree's start address
168 */
169 private int getObjectTreeStart() {
170
171 return address + getPropertyDefaultsSize();
172 }
173
174 /***
175 * The object entry size in version 3 is always 9.
176 *
177 * @return the size of an object entry
178 */
179 private int getObjectEntrySize() {
180
181 return 9;
182 }
183 }