|
| 1 | +import java.util.*; |
| 2 | + |
| 3 | +/** |
| 4 | + * P3DE Logic-Script (P3DES) Engine |
| 5 | + * Supports multi-threaded, frame-by-frame script execution with wait, goto, and variables. |
| 6 | + */ |
| 7 | + |
| 8 | +class ScriptContext { |
| 9 | + String scriptName; |
| 10 | + String[] lines; |
| 11 | + int pc = 0; // Program Counter |
| 12 | + |
| 13 | + HashMap<String, Float> variables = new HashMap<String, Float>(); |
| 14 | + HashMap<String, Integer> labels = new HashMap<String, Integer>(); |
| 15 | + Stack<Integer> loopStack = new Stack<Integer>(); |
| 16 | + |
| 17 | + long waitUntil = 0; |
| 18 | + boolean terminated = false; |
| 19 | + Entity targetEntity = null; // "self" |
| 20 | + |
| 21 | + ScriptContext(String name, String[] rawLines) { |
| 22 | + this.scriptName = name; |
| 23 | + this.lines = rawLines; |
| 24 | + preScanLabels(); |
| 25 | + } |
| 26 | + |
| 27 | + void preScanLabels() { |
| 28 | + for (int i = 0; i < lines.length; i++) { |
| 29 | + String line = lines[i].trim(); |
| 30 | + if (line.startsWith(":")) { |
| 31 | + labels.put(line.substring(1).toLowerCase(), i); |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + void update(CommandInterpreter interpreter) { |
| 37 | + if (terminated || pc >= lines.length) { |
| 38 | + terminated = true; |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + if (p3deditor.this.millis() < waitUntil) return; |
| 43 | + |
| 44 | + // Process one or more lines per frame until a WAIT or script end |
| 45 | + // To prevent infinite loops, we cap executions per frame |
| 46 | + int burstCap = 100; |
| 47 | + while (pc < lines.length && p3deditor.this.millis() >= waitUntil && burstCap > 0) { |
| 48 | + String rawLine = lines[pc].trim(); |
| 49 | + String line = substituteVariables(rawLine); |
| 50 | + pc++; |
| 51 | + burstCap--; |
| 52 | + |
| 53 | + if (line.isEmpty() || line.startsWith("#") || line.startsWith(":")) continue; |
| 54 | + |
| 55 | + // Logic Commands |
| 56 | + String[] parts = line.split("\\s+"); |
| 57 | + if (parts.length == 0) continue; |
| 58 | + String cmd = parts[0].toLowerCase(); |
| 59 | + |
| 60 | + if (cmd.equals("if")) { |
| 61 | + if (parts.length < 4) continue; |
| 62 | + boolean condition = evaluateCondition(parts[1], parts[2], parts[3]); |
| 63 | + if (!condition) { |
| 64 | + int depth = 1; |
| 65 | + while (pc < lines.length && depth > 0) { |
| 66 | + String l = lines[pc].trim().toLowerCase(); |
| 67 | + if (l.startsWith("if")) depth++; |
| 68 | + if (l.startsWith("endif")) depth--; |
| 69 | + if (depth == 1 && l.startsWith("else")) { pc++; break; } |
| 70 | + pc++; |
| 71 | + } |
| 72 | + } |
| 73 | + } else if (cmd.equals("else")) { |
| 74 | + int depth = 1; |
| 75 | + while (pc < lines.length && depth > 0) { |
| 76 | + String l = lines[pc].trim().toLowerCase(); |
| 77 | + if (l.startsWith("if")) depth++; |
| 78 | + if (l.startsWith("endif")) depth--; |
| 79 | + pc++; |
| 80 | + } |
| 81 | + } else if (cmd.equals("endif")) { |
| 82 | + // Just a marker |
| 83 | + } else if (cmd.equals("while")) { |
| 84 | + if (parts.length < 4) continue; |
| 85 | + loopStack.push(pc - 1); // Record the WHILE line |
| 86 | + boolean condition = evaluateCondition(parts[1], parts[2], parts[3]); |
| 87 | + if (!condition) { |
| 88 | + loopStack.pop(); |
| 89 | + int depth = 1; |
| 90 | + while (pc < lines.length && depth > 0) { |
| 91 | + String l = lines[pc].trim().toLowerCase(); |
| 92 | + if (l.startsWith("while")) depth++; |
| 93 | + if (l.startsWith("endwhile")) depth--; |
| 94 | + pc++; |
| 95 | + } |
| 96 | + } |
| 97 | + } else if (cmd.equals("endwhile")) { |
| 98 | + if (!loopStack.isEmpty()) { |
| 99 | + pc = loopStack.pop(); // Jump back to WHILE line |
| 100 | + } |
| 101 | + } else if (cmd.equals("for")) { |
| 102 | + if (parts.length < 5) continue; |
| 103 | + String vName = parts[1]; |
| 104 | + float startV = Float.parseFloat(parts[2]); |
| 105 | + float endV = Float.parseFloat(parts[3]); |
| 106 | + float stepV = Float.parseFloat(parts[4]); |
| 107 | + |
| 108 | + if (!variables.containsKey(vName)) variables.put(vName, startV); |
| 109 | + float curV = variables.get(vName); |
| 110 | + |
| 111 | + loopStack.push(pc - 1); |
| 112 | + if ((stepV > 0 && curV >= endV) || (stepV < 0 && curV <= endV)) { |
| 113 | + loopStack.pop(); |
| 114 | + variables.remove(vName); // Cleanup |
| 115 | + int depth = 1; |
| 116 | + while (pc < lines.length && depth > 0) { |
| 117 | + String l = lines[pc].trim().toLowerCase(); |
| 118 | + if (l.startsWith("for")) depth++; |
| 119 | + if (l.startsWith("endfor")) depth--; |
| 120 | + pc++; |
| 121 | + } |
| 122 | + } |
| 123 | + } else if (cmd.equals("endfor")) { |
| 124 | + if (!loopStack.isEmpty()) { |
| 125 | + int forPc = loopStack.pop(); |
| 126 | + String forLine = substituteVariables(lines[forPc].trim()); |
| 127 | + String[] fParts = forLine.split("\\s+"); |
| 128 | + String vName = fParts[1]; |
| 129 | + float stepV = Float.parseFloat(fParts[4]); |
| 130 | + variables.put(vName, variables.get(vName) + stepV); |
| 131 | + pc = forPc; // Jump back to FOR line for condition check |
| 132 | + } |
| 133 | + } else if (cmd.equals("wait")) { |
| 134 | + if (parts.length > 1) { |
| 135 | + waitUntil = p3deditor.this.millis() + (long)Float.parseFloat(parts[1]); |
| 136 | + return; |
| 137 | + } |
| 138 | + } else if (cmd.equals("goto")) { |
| 139 | + if (parts.length > 1) { |
| 140 | + String lab = parts[1].toLowerCase(); |
| 141 | + if (labels.containsKey(lab)) pc = labels.get(lab); |
| 142 | + } |
| 143 | + } else if (cmd.equals("set")) { |
| 144 | + if (parts.length > 2) { |
| 145 | + variables.put(parts[1], Float.parseFloat(parts[2])); |
| 146 | + } |
| 147 | + } else if (cmd.equals("add") || cmd.equals("sub") || cmd.equals("mul") || cmd.equals("div")) { |
| 148 | + if (parts.length > 2) { |
| 149 | + float cur = variables.getOrDefault(parts[1], 0f); |
| 150 | + float val = Float.parseFloat(parts[2]); |
| 151 | + if (cmd.equals("add")) cur += val; |
| 152 | + if (cmd.equals("sub")) cur -= val; |
| 153 | + if (cmd.equals("mul")) cur *= val; |
| 154 | + if (cmd.equals("div") && val != 0) cur /= val; |
| 155 | + variables.put(parts[1], cur); |
| 156 | + } |
| 157 | + } else { |
| 158 | + try { |
| 159 | + String result = interpreter.execute(line); |
| 160 | + if (result.startsWith("Error")) { |
| 161 | + terminated = true; |
| 162 | + p3deditor.this.ui.debugConsole.addLog("Script Terminated: " + result, 3); |
| 163 | + } else if (line.trim().toLowerCase().startsWith("echo") || line.trim().toLowerCase().startsWith("print")) { |
| 164 | + // Explicitly show echo messages in the terminal |
| 165 | + p3deditor.this.ui.debugConsole.addLog(result, 1); |
| 166 | + } |
| 167 | + } catch (Exception e) { |
| 168 | + terminated = true; |
| 169 | + p3deditor.this.ui.debugConsole.addLog("Script Crash: " + e.getMessage(), 3); |
| 170 | + } |
| 171 | + } |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + boolean evaluateCondition(String left, String op, String right) { |
| 176 | + try { |
| 177 | + float l = Float.parseFloat(left); |
| 178 | + float r = Float.parseFloat(right); |
| 179 | + if (op.equals("==")) return l == r; |
| 180 | + if (op.equals("!=")) return l != r; |
| 181 | + if (op.equals(">")) return l > r; |
| 182 | + if (op.equals("<")) return l < r; |
| 183 | + if (op.equals(">=")) return l >= r; |
| 184 | + if (op.equals("<=")) return l <= r; |
| 185 | + } catch (Exception e) {} |
| 186 | + return false; |
| 187 | + } |
| 188 | + |
| 189 | + String substituteVariables(String text) { |
| 190 | + if (!text.contains("$")) return text; |
| 191 | + // Sort keys by length descending to prevent partial matches ($xPos vs $x) |
| 192 | + String[] keys = variables.keySet().toArray(new String[0]); |
| 193 | + Arrays.sort(keys, (a, b) -> Integer.compare(b.length(), a.length())); |
| 194 | + |
| 195 | + for (String key : keys) { |
| 196 | + float val = variables.get(key); |
| 197 | + String valStr = (val == (long)val) ? String.valueOf((long)val) : String.valueOf(val); |
| 198 | + text = text.replace("$" + key, valStr); |
| 199 | + } |
| 200 | + return text; |
| 201 | + } |
| 202 | +} |
| 203 | + |
| 204 | +class ScriptManager { |
| 205 | + ArrayList<ScriptContext> activeScripts = new ArrayList<ScriptContext>(); |
| 206 | + CommandInterpreter interpreter; |
| 207 | + |
| 208 | + ScriptManager(CommandInterpreter interpreter) { |
| 209 | + this.interpreter = interpreter; |
| 210 | + } |
| 211 | + |
| 212 | + void runScript(String name, String content) { |
| 213 | + String[] lines = content.split("\\r?\\n"); |
| 214 | + activeScripts.add(new ScriptContext(name, lines)); |
| 215 | + System.out.println("P3DES: Started script " + name); |
| 216 | + } |
| 217 | + |
| 218 | + void update() { |
| 219 | + for (int i = activeScripts.size() - 1; i >= 0; i--) { |
| 220 | + ScriptContext ctx = activeScripts.get(i); |
| 221 | + ctx.update(interpreter); |
| 222 | + if (ctx.terminated) { |
| 223 | + activeScripts.remove(i); |
| 224 | + System.out.println("P3DES: Finished script " + ctx.scriptName); |
| 225 | + } |
| 226 | + } |
| 227 | + } |
| 228 | + |
| 229 | + void stopAll() { |
| 230 | + activeScripts.clear(); |
| 231 | + } |
| 232 | +} |
0 commit comments