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
|
package NET.worlds.scape;
import NET.worlds.core.Std;
import java.io.IOException;
import java.util.Enumeration;
public class DPAction extends Action {
private static final int INFINITY = 60000;
protected int loadDist = 2;
protected int unloadDist = 4;
protected DPState state = null;
private static Object classCookie = new Object();
public int getLoadDist() {
return this.loadDist;
}
public int getUnloadDist() {
return this.unloadDist;
}
public void setLoadDist(int v) {
assert v > 0;
if (v > 0) {
this.loadDist = v;
if (this.unloadDist <= this.loadDist) {
this.unloadDist = v + 1;
}
}
}
public void setUnloadDist(int v) {
assert v > 1;
if (v <= this.loadDist) {
this.unloadDist = this.loadDist + 1;
} else {
this.unloadDist = v;
}
}
@Override
public Persister trigger(Event e, Persister seqID) {
if (this.state == null) {
return null;
} else {
if (e != null) {
this.state.setDist(0, e.time);
} else {
this.state.setDist(0, Std.getFastTime());
}
return null;
}
}
public void handleDist(int dist, int time) {
assert this.loadDist < this.unloadDist;
if (this.getOwner() instanceof Portal) {
if (dist <= this.loadDist) {
((Portal)this.getOwner()).triggerLoad();
if (((Portal)this.getOwner()).active()) {
this.informOtherSide(dist + 1, time);
} else {
new DPLoadTracker(this, dist + 1, time);
}
} else if (dist > this.unloadDist) {
((Portal)this.getOwner()).reset();
} else if (((Portal)this.getOwner()).active()) {
this.informOtherSide(dist + 1, time);
}
}
}
void informOtherSide(int distance, int triggerTime) {
DPState s = null;
Portal p = ((Portal)this.getOwner()).farSide();
Enumeration e = p.getActions();
while (e.hasMoreElements()) {
Object a = e.nextElement();
if (a instanceof DPAction) {
s = ((DPAction)a).getState();
assert s != null;
}
}
if (s != null) {
s.setDist(distance, triggerTime);
}
}
@Override
protected void noteAddingTo(SuperRoot owner) {
super.noteAddingTo(owner);
if (this.state == null) {
if (owner instanceof Portal) {
Portal p = (Portal)owner;
World w = owner.getWorld();
if (w != null) {
this.state = this.findState(w);
}
if (this.state == null) {
this.state = new DPState(60000, this);
} else {
this.state.addConnection(this);
}
}
}
}
protected DPState findState(SuperRoot root) {
DPState rVal = null;
Enumeration e = root.getDeepOwned();
while (e.hasMoreElements() && rVal == null) {
SuperRoot sr = (SuperRoot)e.nextElement();
if (sr instanceof DPAction) {
rVal = ((DPAction)sr).getState();
}
}
return rVal;
}
public DPState getState() {
return this.state;
}
@Override
public void detach() {
if (this.state != null) {
this.state.dropConnection(this);
this.state = null;
}
super.detach();
}
@Override
public Object properties(int index, int offset, int mode, Object value) throws NoSuchPropertyException {
Object ret = null;
switch (index - offset) {
case 0:
if (mode == 0) {
ret = IntegerPropertyEditor.make(new Property(this, index, "Load Distance"));
} else if (mode == 1) {
ret = new Integer(this.loadDist);
} else if (mode == 2) {
this.setLoadDist((Integer)value);
}
break;
case 1:
if (mode == 0) {
ret = IntegerPropertyEditor.make(new Property(this, index, "Unload Distance"));
} else if (mode == 1) {
ret = new Integer(this.unloadDist);
} else if (mode == 2) {
this.setUnloadDist((Integer)value);
}
break;
case 2:
if (mode == 0) {
ret = new Property(this, index, "Cell State");
} else if (mode == 1) {
ret = this.state;
}
break;
default:
ret = super.properties(index, offset + 3, mode, value);
}
return ret;
}
@Override
public void saveState(Saver s) throws IOException {
s.saveVersion(2, classCookie);
super.saveState(s);
s.saveInt(this.loadDist);
s.saveInt(this.unloadDist);
s.saveMaybeNull(this.state);
}
@Override
public void restoreState(Restorer r) throws IOException, TooNewException {
switch (r.restoreVersion(classCookie)) {
case 0:
super.restoreState(r);
this.loadDist = r.restoreInt();
this.unloadDist = r.restoreInt();
this.state = (DPState)r.restore();
r.restoreInt();
break;
case 1:
super.restoreState(r);
this.loadDist = r.restoreInt();
this.unloadDist = r.restoreInt();
this.state = (DPState)r.restore();
break;
case 2:
super.restoreState(r);
this.loadDist = r.restoreInt();
this.unloadDist = r.restoreInt();
this.state = (DPState)r.restoreMaybeNull();
break;
default:
throw new TooNewException();
}
}
@Override
public String toString() {
return super.toString() + "[" + this.getOwner().getWorld().getName() + "|" + this.getOwner().getName() + "]";
}
}
|