summaryrefslogtreecommitdiff
path: root/NET/worlds/scape/AnimatedActionManager.java
blob: 5ab97a913627582621e4131f9d7e5e8c22aa1df3 (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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
package NET.worlds.scape;

import NET.worlds.core.IniFile;
import NET.worlds.network.NetUpdate;
import NET.worlds.network.URL;
import java.awt.Menu;
import java.awt.MenuItem;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.Enumeration;
import java.util.StringTokenizer;
import java.util.Vector;

public class AnimatedActionManager implements ActionListener, BGLoaded {
   private AnimatedAction _currentAction = null;
   static final String actionFile = "actions/actions.dat";
   static final String localActionFile = "actions.dat";
   static final String version = "VERSION";
   static final String action = "ACTION";
   private Vector actionList;
   private Vector currentMenuActions;
   private static AnimatedActionManager theAnimatedActionManager = null;

   public static AnimatedActionManager get() {
      if (theAnimatedActionManager == null) {
         theAnimatedActionManager = new AnimatedActionManager();
      }

      return theAnimatedActionManager;
   }

   private Vector getActionList(URL avatar, URL object) {
      if (this.actionList != null && avatar != null && object != null) {
         String av = PosableShape.getBodyType(avatar);
         if (av == null) {
            return null;
         } else {
            String ob = object.toString();
            int dot = ob.lastIndexOf(".");
            if (dot != -1) {
               ob = ob.substring(0, dot);
            }

            Vector ret = new Vector();
            Enumeration e = this.actionList.elements();

            while (e.hasMoreElements()) {
               AnimatedAction a = (AnimatedAction)e.nextElement();
               if (this.vectorCheck(av, a.avatarNames) && this.vectorCheck(ob, a.targetObjects)) {
                  ret.addElement(a);
               }
            }

            return ret;
         }
      } else {
         return null;
      }
   }

   private boolean vectorCheck(String s, Vector v) {
      Enumeration e = v.elements();

      while (e.hasMoreElements()) {
         String element = (String)e.nextElement();
         if (s.toLowerCase().endsWith(element.toLowerCase())) {
            return true;
         }

         if (element.equals("any")) {
            return true;
         }
      }

      return false;
   }

   public boolean buildActionMenu(Menu m, Shape target) {
      assert m != null;

      if (this._currentAction != null) {
         return false;
      } else {
         SuperRoot ultimateOwner = target;

         while (ultimateOwner.getOwner() != null) {
            ultimateOwner = ultimateOwner.getOwner();
            if (ultimateOwner instanceof PosableShape) {
               target = (Shape)ultimateOwner;
               break;
            }
         }

         Pilot p = Pilot.getActive();
         if (p == null) {
            return false;
         } else if (!(p instanceof HoloPilot)) {
            return false;
         } else {
            HoloPilot hp = (HoloPilot)p;
            if (hp.getInternalDrone().getRoom() != target.getRoom()) {
               return false;
            } else {
               Vector v = this.getActionList(hp.getInternalDrone().getCurrentURL(), target.getURL());
               this.currentMenuActions = v;
               if (v == null) {
                  return false;
               } else if (v.size() == 0) {
                  return false;
               } else {
                  Enumeration e = v.elements();

                  while (e.hasMoreElements()) {
                     AnimatedAction a = (AnimatedAction)e.nextElement();
                     a.setTargetShape(target);
                     int idx = a.actionName.indexOf("\\");
                     if (idx != -1) {
                        String action = a.actionName.substring(idx + 1);
                        String submenu = a.actionName.substring(0, idx);
                        int items = m.getItemCount();
                        boolean inserted = false;

                        for (int i = 0; i < items; i++) {
                           MenuItem item = m.getItem(i);
                           if (item.getLabel().equals(submenu) && item instanceof Menu) {
                              ((Menu)item).add(action);
                              inserted = true;
                              break;
                           }
                        }

                        if (!inserted) {
                           Menu sm = new Menu(submenu);
                           sm.addActionListener(this);
                           sm.add(action);
                           m.add(sm);
                        }
                     } else {
                        m.add(a.actionName);
                     }
                  }

                  return true;
               }
            }
         }
      }
   }

   public boolean processMenuClick(String actionName) {
      if (this.currentMenuActions == null) {
         return false;
      } else {
         Enumeration e = this.currentMenuActions.elements();

         while (e.hasMoreElements()) {
            AnimatedAction a = (AnimatedAction)e.nextElement();
            String thisAction = a.actionName;
            int idx = thisAction.indexOf(92);
            if (idx != -1) {
               thisAction = thisAction.substring(idx + 1);
            }

            if (thisAction.equals(actionName)) {
               Pilot p = Pilot.getActive();
               if (p == null) {
                  return false;
               }

               if (!(p instanceof HoloPilot)) {
                  return false;
               }

               HoloPilot hp = (HoloPilot)p;
               if (this._currentAction != null) {
                  this._currentAction.abort();
               }

               this._currentAction = a;
               a.execute(hp);
               return true;
            }
         }

         return false;
      }
   }

   public void actionCompleted(AnimatedAction a) {
      assert a == this._currentAction;

      this._currentAction = null;
   }

   @Override
   public void actionPerformed(ActionEvent e) {
      this.processMenuClick(e.getActionCommand());
   }

   private AnimatedActionManager() {
      System.out.println("Loading action manager");
      this.actionList = new Vector();
      this.loadActionsFile();
   }

   private void loadActionsFile() {
      File f = new File("actions.dat");
      if (f.exists()) {
         this.parseActionFile("actions.dat");
      }

      String serv = NetUpdate.getUpgradeServerURL();
      String s = IniFile.override().getIniString("actionFile", "actions/actions.dat");
      BackgroundLoader.get(this, URL.make(serv + s));
   }

   @Override
   public synchronized Object asyncBackgroundLoad(String localName, URL remoteURL) {
      return localName;
   }

   @Override
   public boolean syncBackgroundLoad(Object obj, URL remoteURL) {
      System.out.println("Got actions file ");
      String localName = (String)obj;
      if (localName != null && new File(localName).exists()) {
         this.parseActionFile(localName);
      }

      return false;
   }

   @Override
   public Room getBackgroundLoadRoom() {
      return null;
   }

   private String getLine(RandomAccessFile fIn) throws IOException {
      while (fIn.getFilePointer() < fIn.length()) {
         String line = fIn.readLine().trim();
         if (line == null) {
            return null;
         }

         if (!line.startsWith("//") && line.length() != 0) {
            return line;
         }
      }

      return null;
   }

   private void parseActionFile(String fileName) {
      int verNum = 0;

      assert this.actionList != null;

      this.actionList.removeAllElements();

      try {
         RandomAccessFile fIn = new RandomAccessFile(fileName, "r");

         String line;
         while ((line = this.getLine(fIn)) != null) {
            StringTokenizer tok = new StringTokenizer(line);
            String firstWord = tok.nextToken();
            if (firstWord.equals("VERSION")) {
               String ver = tok.nextToken();
               if (ver.equals("1")) {
                  verNum = 1;
               } else {
                  if (!ver.equals("2")) {
                     throw new Exception("Bad version.");
                  }

                  verNum = 2;
               }
            } else if (firstWord.equals("ACTION")) {
               if (verNum == 0) {
                  throw new Exception("Actions file version not specified.");
               }

               AnimatedAction a = new AnimatedAction();
               a.actionName = line.substring("ACTION".length() + 1);
               this.readVector(fIn, a.avatarNames);
               this.readVector(fIn, a.targetObjects);
               if (verNum > 1) {
                  a.consentRequired = this.getLine(fIn).toLowerCase().equals("consent");
               }

               StringTokenizer tok2 = new StringTokenizer(this.getLine(fIn));
               Float x = new Float(tok2.nextToken());
               Float y = new Float(tok2.nextToken());
               a.targetRelPosition = new Point2(x, y);
               Float yaw = new Float(this.getLine(fIn));
               a.targetRelYaw = yaw.floatValue();
               a.preAnimationAction = AnimatedAction.getAction(this.getLine(fIn));
               a.preAnimationParameter = this.getLine(fIn);
               a.actionName1 = this.getLine(fIn);
               if (verNum > 1) {
                  a.simultaneousAction = AnimatedAction.getAction(this.getLine(fIn));
                  a.simultaneousParameter = this.getLine(fIn);
               }

               a.postAnimationAction = AnimatedAction.getAction(this.getLine(fIn));
               a.postAnimationParameter = this.getLine(fIn);
               a.actionName2 = this.getLine(fIn);
               this.actionList.addElement(a);
            }
         }

         fIn.close();
      } catch (Exception var12) {
         System.out.println("Error reading actions.dat: " + var12.toString());
      }
   }

   private void readVector(RandomAccessFile fIn, Vector v) throws IOException {
      String line = this.getLine(fIn);
      StringTokenizer tok = new StringTokenizer(line);

      while (tok.hasMoreTokens()) {
         v.addElement(tok.nextToken());
      }
   }
}