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() + "]"; } }