summaryrefslogtreecommitdiff
path: root/NET/worlds/scape/Trigger.java
blob: a056535496527d64ee159d04eb7a291b954000ab (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
package NET.worlds.scape;

import NET.worlds.console.Console;
import java.io.IOException;
import java.text.MessageFormat;

public class Trigger extends SwitchableBehavior implements FrameHandler, Persister, MouseDownHandler, BumpHandler {
   public static TriggeredSwitchableBehavior[] TriggeredSwitchableBehaviorList = new TriggeredSwitchableBehavior[1000];
   public static int TriggeredSwitchableBehaviorListCount = 0;
   public String trigger = new String("none");
   public String targetTriggerTag = new String("");
   public String[][] targetTriggerTagArray = new String[20][10];
   public int targetTriggerTagCount = 0;
   public int[] finishedTriggerTagSequenceNo = new int[50];
   public int[] finishedTriggerTagEventNo = new int[50];
   public int finishedTriggerTagCount = 0;

   private void addTargetTriggerTagArray(String[] sequentialTargetTriggerTagArray, String targetTriggerTag) {
      int currentIndex = 1;
      int currentSeparator = 0;
      int nextSeparator = 0;
      int lastSeparator = targetTriggerTag.lastIndexOf("+");
      if (lastSeparator == -1 && targetTriggerTag.length() == 0) {
         int var8 = 0;
         sequentialTargetTriggerTagArray[0] = String.valueOf((int)var8);
      } else if (lastSeparator == -1) {
         sequentialTargetTriggerTagArray[currentIndex] = targetTriggerTag;
         sequentialTargetTriggerTagArray[0] = String.valueOf(currentIndex);
      } else {
         nextSeparator = targetTriggerTag.indexOf("+");
         sequentialTargetTriggerTagArray[currentIndex] = targetTriggerTag.substring(0, nextSeparator);
         currentIndex++;

         while (nextSeparator != lastSeparator) {
            currentSeparator = nextSeparator;
            nextSeparator = targetTriggerTag.indexOf("+", nextSeparator + 1);
            sequentialTargetTriggerTagArray[currentIndex] = targetTriggerTag.substring(currentSeparator + 1, nextSeparator);
            currentIndex++;
         }

         sequentialTargetTriggerTagArray[currentIndex] = targetTriggerTag.substring(nextSeparator + 1);
         sequentialTargetTriggerTagArray[0] = String.valueOf(currentIndex);
      }
   }

   public void preprocessTargetTriggerTag(String targetTriggerTag) {
      int currentIndex = 1;
      int currentSeparator = 0;
      int nextSeparator = 0;
      int lastSeparator = targetTriggerTag.lastIndexOf(" ");
      if (lastSeparator != -1 || targetTriggerTag.length() != 0) {
         if (lastSeparator == -1) {
            this.addTargetTriggerTagArray(this.targetTriggerTagArray[currentIndex], targetTriggerTag);
            this.targetTriggerTagCount = currentIndex;
         } else {
            nextSeparator = targetTriggerTag.indexOf(" ");
            this.addTargetTriggerTagArray(this.targetTriggerTagArray[currentIndex], targetTriggerTag.substring(0, nextSeparator));
            currentIndex++;

            while (nextSeparator != lastSeparator) {
               currentSeparator = nextSeparator;
               nextSeparator = targetTriggerTag.indexOf(" ", nextSeparator + 1);
               this.addTargetTriggerTagArray(this.targetTriggerTagArray[currentIndex], targetTriggerTag.substring(currentSeparator + 1, nextSeparator));
               currentIndex++;
            }

            this.addTargetTriggerTagArray(this.targetTriggerTagArray[currentIndex], targetTriggerTag.substring(nextSeparator + 1));
            this.targetTriggerTagCount = currentIndex;
         }
      }
   }

   public void activateTrigger() {
      for (int j = 1; j <= this.targetTriggerTagCount; j++) {
         boolean match = false;

         for (int i = 0; !match && i < TriggeredSwitchableBehaviorListCount; i++) {
            if (this.targetTriggerTagArray[j][1].equals(TriggeredSwitchableBehaviorList[i].externalTriggerTag) && TriggeredSwitchableBehaviorList[i] != null) {
               TriggeredSwitchableBehaviorList[i].ExternalTrigger(this, j, 1);
            }
         }
      }
   }

   public void activateSequenceTrigger() {
      for (int j = this.finishedTriggerTagCount; j > 0; j--) {
         int sequence_no = this.finishedTriggerTagSequenceNo[j];
         int event_no = this.finishedTriggerTagEventNo[j];
         if (Integer.valueOf(this.targetTriggerTagArray[sequence_no][0]) > event_no) {
            boolean match = false;

            for (int i = 0; !match && i < TriggeredSwitchableBehaviorListCount; i++) {
               if (this.targetTriggerTagArray[sequence_no][event_no + 1].equals(TriggeredSwitchableBehaviorList[i].externalTriggerTag)
                  && TriggeredSwitchableBehaviorList[i] != null) {
                  TriggeredSwitchableBehaviorList[i].ExternalTrigger(this, sequence_no, event_no + 1);
               }
            }
         }
      }

      this.finishedTriggerTagCount = 0;
   }

   public synchronized void registerFinishedTriggerTag(int sequence_no, int event_no) {
      this.finishedTriggerTagCount++;
      this.finishedTriggerTagSequenceNo[this.finishedTriggerTagCount] = sequence_no;
      this.finishedTriggerTagEventNo[this.finishedTriggerTagCount] = event_no;
   }

   @Override
   public boolean handle(FrameEvent e) {
      if (this.enabled && this.finishedTriggerTagCount > 0) {
         this.activateSequenceTrigger();
      }

      return true;
   }

   @Override
   public boolean handle(MouseDownEvent e) {
      if (this.enabled && this.trigger.equals("click")) {
         this.activateTrigger();
      }

      return true;
   }

   @Override
   public boolean handle(BumpEventTemp e) {
      if (this.enabled && this.trigger.equals("bump")) {
         this.activateTrigger();
      }

      return true;
   }

   @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 = StringPropertyEditor.make(new Property(this, index, "Trigger"));
            } else if (mode == 1) {
               ret = new String(this.trigger);
            } else if (mode == 2) {
               this.trigger = ((String)value).toString().trim();
            }
            break;
         case 1:
            if (mode == 0) {
               ret = StringPropertyEditor.make(new Property(this, index, "Target Trigger Tag"));
            } else if (mode == 1) {
               ret = new String(this.targetTriggerTag);
            } else if (mode == 2) {
               this.targetTriggerTag = ((String)value).toString().trim();
               this.preprocessTargetTriggerTag(this.targetTriggerTag);
            }
            break;
         default:
            ret = super.properties(index, offset + 2, mode, value);
      }

      return ret;
   }

   @Override
   public String toString() {
      return super.toString() + "[enabled " + this.enabled + ", trigger " + this.trigger + ", targetTriggerTag " + this.targetTriggerTag + "]";
   }

   @Override
   public void saveState(Saver s) throws IOException {
      s.saveString(this.trigger);
      s.saveString(this.targetTriggerTag);
   }

   @Override
   public void restoreState(Restorer r) throws IOException {
      this.trigger = r.restoreString();
      this.targetTriggerTag = r.restoreString();
      this.preprocessTargetTriggerTag(this.targetTriggerTag);
   }

   @Override
   public void postRestore(int version) {
      String name = this.getName();
      String arg1 = name == null ? "<null>" : name;
      SuperRoot owner = this.getOwner();
      String oname = "";
      if (owner != null) {
         oname = owner.getName();
      }

      String arg2 = oname == null ? "<null>" : oname;
      Object[] arguments = new Object[]{new String(arg1), new String(arg2)};
      Console.println(MessageFormat.format(Console.message("Trigger-obs"), arguments));
   }
}