summaryrefslogtreecommitdiff
path: root/NET/worlds/scape/PosableDrone.java
blob: d6b6ec31fef8f035ffa6cfd01713e516223a4d23 (plain) (blame)
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
package NET.worlds.scape;

import NET.worlds.console.BBAnimateDroneCommand;
import NET.worlds.console.BlackBox;
import NET.worlds.console.Console;
import NET.worlds.core.IniFile;
import NET.worlds.network.ObjID;
import NET.worlds.network.URL;
import NET.worlds.network.WorldServer;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Enumeration;
import java.util.Vector;

public class PosableDrone extends Drone implements FrameHandler {
   static PosableDroneLoader droneLoader = new PosableDroneLoader();
   public static boolean threadDroneLoads = IniFile.gamma().getIniInt("ThreadDroneLoading", 0) == 1;
   static boolean droneLoaderStarted = false;
   private PosableShape pendingShape = null;
   private static Object classCookie = new Object();

   public void SetPendingShape(PosableShape ps) {
      this.pendingShape = ps;
   }

   @Override
   public boolean handle(FrameEvent evt) {
      if (this.pendingShape != null) {
         PosableShape ps = this.getInternalPosableShape();
         if (ps != null) {
            ps.detach();
         }

         this.add(this.pendingShape);
         this.pendingShape = null;
      }

      return super.handle(evt);
   }

   public PosableDrone(ObjID objID, WorldServer serv, URL url) {
      super(objID, serv);
      this.makeFigure(url);
   }

   public PosableDrone(ObjID objID, WorldServer serv) {
      super(objID, serv);
      this.loadInit();
   }

   public PosableDrone() {
   }

   @Override
   public void loadInit() {
      this.makeFigure(PosableShape.getDefaultURL());
   }

   public synchronized URL getPosableShapeURL() {
      PosableShape ps = this.getInternalPosableShape();
      return ps == null ? null : ps.getURL();
   }

   @Override
   public Drone setAvatarNow(URL url) {
      if (!this.shouldBeMuted() && (url.endsWith(".rwx") || url.endsWith(".rwg"))) {
         if (this.shouldBeForcedHuman()) {
            url = PosableShape.getHuman(url);
         }

         url = PosableShape.getPermitted(url, this.getWorld());
         PosableShape ps = this.getInternalPosableShape();
         if (ps != null) {
            if (url.equals(ps.getURL())) {
               return this;
            }

            ps.detach();
         }

         this.makeFigure(url);
         return this;
      } else {
         return super.setAvatarNow(url);
      }
   }

   public void makeFigure(URL url) {
      boolean isPilot = false;
      if (Console.getActive() != null && Console.getActive().getPilot() != null && Console.getActive().getPilot().hasContents()) {
         isPilot = Console.getActive().getPilot().contentsContain(this);
      }

      if (!isPilot) {
         isPilot = this.isPilotDrone(url);
      }

      boolean loadAsynchronously = false;

      try {
         if (!PosableDroneLoader.avatarExistsLocally(url)) {
            loadAsynchronously = true;
         } else {
            loadAsynchronously = !isPilot && !url.toString().equals(PosableShape.getDefaultURL().toString());
         }
      } catch (MalformedURLException var5) {
         loadAsynchronously = false;
      }

      if (loadAsynchronously && threadDroneLoads) {
         if (!droneLoaderStarted) {
            droneLoaderStarted = true;
            Thread t = new Thread(droneLoader, "DroneLoaderDaemon");
            t.setDaemon(true);
            t.setPriority(1);
            t.start();
         }

         droneLoader.load(this, url);
      } else {
         PosableShape ps;
         if (VehicleShape.isVehicle(url)) {
            ps = new VehicleShape(url);
         } else {
            ps = new PosableShape(url);
         }

         ps.setVisible(true);
         ps.setBumpable(false);
         if (!this.isPilotDrone(url)) {
            ps.enableLOD(true);
         }

         this.add(ps);
      }
   }

   public PosableShape getInternalPosableShape() {
      Enumeration e = this.getContents();

      while (e.hasMoreElements()) {
         Object o = e.nextElement();
         if (o instanceof PosableShape) {
            return (PosableShape)o;
         }
      }

      return null;
   }

   public boolean isPilotDrone(URL url) {
      return Console.getActive() != null && Console.getActive().pendingPilot.equals(url.toString());
   }

   @Override
   public float animate(String action) {
      super.animate(action);
      BlackBox.getInstance().submitEvent(new BBAnimateDroneCommand(this.getName(), action));
      PosableShape ps = this.getInternalPosableShape();
      return ps != null ? ps.animate(action) : 0.0F;
   }

   @Override
   public Vector getAnimationList() {
      PosableShape ps = this.getInternalPosableShape();
      return ps != null ? ps.getAnimationList() : super.getAnimationList();
   }

   @Override
   public void saveState(Saver s) throws IOException {
      s.saveVersion(2, classCookie);
      super.saveState(s);
   }

   @Override
   public void restoreState(Restorer r) throws IOException, TooNewException {
      int vers = r.restoreVersion(classCookie);
      switch (vers) {
         case 0:
         case 1:
            if (vers == 0) {
               this.restoreStateDrone(r);
            } else {
               super.restoreState(r);
            }

            r.setOldFlag();
            this.makeFigure(URL.make("avatar:" + r.restoreString() + ".rwx"));
            break;
         case 2:
            super.restoreState(r);
            break;
         default:
            throw new TooNewException();
      }
   }
}