Skip to content

Commit f913ba9

Browse files
authored
More interpreter native mocks (#1199)
1 parent e7532e1 commit f913ba9

9 files changed

Lines changed: 358 additions & 6 deletions

File tree

de.peeeq.wurstscript/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ dependencies {
109109
implementation 'com.github.albfernandez:juniversalchardet:2.4.0'
110110
implementation 'org.xerial:sqlite-jdbc:3.46.1.3'
111111
implementation 'com.github.inwc3:jmpq3:e28f6999c0'
112-
implementation 'com.github.inwc3:wc3libs:548f34a424'
112+
implementation 'com.github.inwc3:wc3libs:ac41f780a5'
113113
implementation 'com.github.wurstscript:wurst-project-config:348fcd4ef5'
114114
implementation 'org.slf4j:slf4j-api:2.0.17'
115115
implementation 'ch.qos.logback:logback-classic:1.5.20'

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/jassinterpreter/mocks/DestructableMock.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class DestructableMock {
1010
public ILconstReal face;
1111
public ILconstReal scale;
1212
public ILconstInt variation;
13+
public ILconstReal life = ILconstReal.create(100);
1314

1415
public DestructableMock(ILconstInt objectId, ILconstReal x, ILconstReal y, ILconstReal face, ILconstReal scale, ILconstInt variation) {
1516
this.objectId = objectId;

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/jassinterpreter/mocks/PlayerMock.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ public class PlayerMock {
1010
public final ILconstInt id;
1111
public ILconst playerColor = ILconstNull.instance();
1212
public final HashMap<Integer, ILconstInt> techMaxAllowed = new HashMap<>();
13+
public final HashMap<String, ILconstInt> playerStates = new HashMap<>();
1314

1415
public PlayerMock(ILconstInt p) {
1516
this.id = p;

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/jassinterpreter/mocks/UnitMock.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,28 @@
44
import de.peeeq.wurstscript.intermediatelang.ILconstReal;
55
import de.peeeq.wurstscript.intermediatelang.IlConstHandle;
66

7+
import java.util.HashMap;
8+
79
public class UnitMock {
810
public IlConstHandle owner;
911
public ILconstInt unitid;
1012
public ILconstReal x;
1113
public ILconstReal y;
1214
public ILconstReal face;
15+
public boolean removed;
16+
public final HashMap<String, ILconstReal> states = new HashMap<>();
17+
public final HashMap<Integer, ILconstInt> abilityLevels = new HashMap<>();
18+
public ILconstInt currentOrder = ILconstInt.create(0);
1319

1420
public UnitMock(IlConstHandle owner, ILconstInt unitid, ILconstReal x, ILconstReal y, ILconstReal face) {
1521
this.owner = owner;
1622
this.unitid = unitid;
1723
this.x = x;
1824
this.y = y;
1925
this.face = face;
26+
states.put("unitstate0", ILconstReal.create(100));
27+
states.put("unitstate1", ILconstReal.create(100));
28+
states.put("unitstate2", ILconstReal.create(0));
29+
states.put("unitstate3", ILconstReal.create(0));
2030
}
2131
}

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/jassinterpreter/providers/ConversionProvider.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,21 @@
44
import de.peeeq.wurstscript.intermediatelang.IlConstHandle;
55
import de.peeeq.wurstscript.intermediatelang.interpreter.AbstractInterpreter;
66

7+
import java.util.HashMap;
78
import java.util.LinkedHashSet;
9+
import java.util.Map;
810

911
public class ConversionProvider extends Provider {
12+
private static final Map<String, IlConstHandle> enumHandles = new HashMap<>();
13+
1014
public ConversionProvider(AbstractInterpreter interpreter) {
1115
super(interpreter);
1216
}
1317

18+
public static IlConstHandle enumHandle(String typeName, int value) {
19+
return enumHandles.computeIfAbsent(typeName + value, key -> new IlConstHandle(key, value));
20+
}
21+
1422
public IlConstHandle ConvertRace(ILconstInt i) {
1523
return new IlConstHandle("race" + i, new LinkedHashSet<>());
1624
}
@@ -32,7 +40,7 @@ public IlConstHandle ConvertFGameState(ILconstInt i) {
3240
}
3341

3442
public IlConstHandle ConvertPlayerState(ILconstInt i) {
35-
return new IlConstHandle("playerstate" + i, new LinkedHashSet<>());
43+
return enumHandle("playerstate", i.getVal());
3644
}
3745

3846
public IlConstHandle ConvertPlayerScore(ILconstInt i) {
@@ -44,7 +52,7 @@ public IlConstHandle ConvertPlayerGameResult(ILconstInt i) {
4452
}
4553

4654
public IlConstHandle ConvertUnitState(ILconstInt i) {
47-
return new IlConstHandle("unitstate" + i, new LinkedHashSet<>());
55+
return enumHandle("unitstate", i.getVal());
4856
}
4957

5058
public IlConstHandle ConvertUnitIntegerField(ILconstInt i) {
@@ -128,15 +136,15 @@ public IlConstHandle ConvertMapDensity(ILconstInt i) {
128136
}
129137

130138
public IlConstHandle ConvertMapControl(ILconstInt i) {
131-
return new IlConstHandle("mapcontrol" + i, new LinkedHashSet<>());
139+
return enumHandle("mapcontrol", i.getVal());
132140
}
133141

134142
public IlConstHandle ConvertPlayerColor(ILconstInt i) {
135143
return new IlConstHandle("playercolor" + i, new LinkedHashSet<>());
136144
}
137145

138146
public IlConstHandle ConvertPlayerSlotState(ILconstInt i) {
139-
return new IlConstHandle("playerslotstate" + i, new LinkedHashSet<>());
147+
return enumHandle("playerslotstate", i.getVal());
140148
}
141149

142150
public IlConstHandle ConvertVolumeGroup(ILconstInt i) {

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/jassinterpreter/providers/DestructableProvider.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,22 @@ public void RemoveDestructable(IlConstHandle destructable) {
1919
}
2020

2121
public void KillDestructable(IlConstHandle destructable) {
22+
DestructableMock destructableMock = destructableOrNull(destructable);
23+
if (destructableMock != null) {
24+
destructableMock.life = ILconstReal.create(0);
25+
}
26+
}
27+
28+
public void SetDestructableLife(IlConstHandle destructable, ILconstReal life) {
29+
DestructableMock destructableMock = destructableOrNull(destructable);
30+
if (destructableMock != null) {
31+
destructableMock.life = life;
32+
}
33+
}
34+
35+
public ILconstReal GetDestructableLife(IlConstHandle destructable) {
36+
DestructableMock destructableMock = destructableOrNull(destructable);
37+
return destructableMock == null ? ILconstReal.create(0) : destructableMock.life;
2238
}
2339

2440
public ILconstReal GetDestructableX(IlConstHandle destructable) {
@@ -28,4 +44,11 @@ public ILconstReal GetDestructableX(IlConstHandle destructable) {
2844
public ILconstReal GetDestructableY(IlConstHandle destructable) {
2945
return ((DestructableMock)destructable.getObj()).y;
3046
}
47+
48+
private DestructableMock destructableOrNull(IlConstHandle destructable) {
49+
if (destructable == null || !(destructable.getObj() instanceof DestructableMock)) {
50+
return null;
51+
}
52+
return (DestructableMock) destructable.getObj();
53+
}
3154
}

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/jassinterpreter/providers/PlayerProvider.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,28 @@ public ILconstInt GetPlayerId(IlConstHandle p) {
2323
return p != null ? ((PlayerMock) p.getObj()).id : ILconstInt.create(-1);
2424
}
2525

26+
public void SetPlayerState(IlConstHandle player, IlConstHandle playerstate, ILconstInt value) {
27+
if (player == null || playerstate == null) {
28+
return;
29+
}
30+
((PlayerMock) player.getObj()).playerStates.put(playerstate.print(), value);
31+
}
32+
33+
public ILconstInt GetPlayerState(IlConstHandle player, IlConstHandle playerstate) {
34+
if (player == null || playerstate == null) {
35+
return ILconstInt.create(0);
36+
}
37+
return ((PlayerMock) player.getObj()).playerStates.getOrDefault(playerstate.print(), ILconstInt.create(0));
38+
}
39+
40+
public IlConstHandle GetPlayerSlotState(IlConstHandle player) {
41+
return ConversionProvider.enumHandle("playerslotstate", 1);
42+
}
43+
44+
public IlConstHandle GetPlayerController(IlConstHandle player) {
45+
return ConversionProvider.enumHandle("mapcontrol", 0);
46+
}
47+
2648
public ILconstInt GetPlayerNeutralPassive() {
2749
// fake value
2850
return new ILconstInt(31);

de.peeeq.wurstscript/src/main/java/de/peeeq/wurstio/jassinterpreter/providers/UnitProvider.java

Lines changed: 164 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package de.peeeq.wurstio.jassinterpreter.providers;
22

33
import de.peeeq.wurstio.objectreader.ObjectHelper;
4+
import de.peeeq.wurstio.jassinterpreter.mocks.DestructableMock;
45
import de.peeeq.wurstio.jassinterpreter.mocks.UnitMock;
6+
import de.peeeq.wurstscript.intermediatelang.ILconst;
57
import de.peeeq.wurstscript.intermediatelang.ILconstBool;
68
import de.peeeq.wurstscript.intermediatelang.ILconstInt;
9+
import de.peeeq.wurstscript.intermediatelang.ILconstNull;
710
import de.peeeq.wurstscript.intermediatelang.ILconstReal;
811
import de.peeeq.wurstscript.intermediatelang.ILconstString;
912
import de.peeeq.wurstscript.intermediatelang.IlConstHandle;
@@ -22,8 +25,43 @@ public IlConstHandle CreateUnit(IlConstHandle owner, ILconstInt unitid, ILconstR
2225
return new IlConstHandle(NameProvider.getRandomName("unit"), new UnitMock(owner, unitid, x, y, face));
2326
}
2427

28+
public ILconst GetOwningPlayer(IlConstHandle unit) {
29+
UnitMock unitMock = unitOrNull(unit);
30+
return unitMock == null ? ILconstNull.instance() : unitMock.owner;
31+
}
32+
2533
public ILconstInt GetUnitTypeId(IlConstHandle unit) {
26-
return ((UnitMock)unit.getObj()).unitid;
34+
UnitMock unitMock = unitOrNull(unit);
35+
return unitMock == null ? ILconstInt.create(0) : unitMock.unitid;
36+
}
37+
38+
public ILconstReal GetUnitX(IlConstHandle unit) {
39+
UnitMock unitMock = unitOrNull(unit);
40+
return unitMock == null ? ILconstReal.create(0) : unitMock.x;
41+
}
42+
43+
public ILconstReal GetUnitY(IlConstHandle unit) {
44+
UnitMock unitMock = unitOrNull(unit);
45+
return unitMock == null ? ILconstReal.create(0) : unitMock.y;
46+
}
47+
48+
public void SetUnitX(IlConstHandle unit, ILconstReal x) {
49+
UnitMock unitMock = unitOrNull(unit);
50+
if (unitMock != null) {
51+
unitMock.x = x;
52+
}
53+
}
54+
55+
public void SetUnitY(IlConstHandle unit, ILconstReal y) {
56+
UnitMock unitMock = unitOrNull(unit);
57+
if (unitMock != null) {
58+
unitMock.y = y;
59+
}
60+
}
61+
62+
public ILconstReal GetUnitFacing(IlConstHandle unit) {
63+
UnitMock unitMock = unitOrNull(unit);
64+
return unitMock == null ? ILconstReal.create(0) : unitMock.face;
2765
}
2866

2967
public ILconstString GetUnitName(IlConstHandle unit) {
@@ -67,9 +105,107 @@ public ILconstBool IsUnitType(IlConstHandle whichUnit, IlConstHandle whichUnitTy
67105
}
68106

69107
public void RemoveUnit(IlConstHandle unit) {
108+
UnitMock unitMock = unitOrNull(unit);
109+
if (unitMock != null) {
110+
unitMock.removed = true;
111+
}
70112
userDataMap.remove(unit);
71113
}
72114

115+
public void KillUnit(IlConstHandle unit) {
116+
UnitMock unitMock = unitOrNull(unit);
117+
if (unitMock != null) {
118+
unitMock.states.put("unitstate0", ILconstReal.create(0));
119+
}
120+
}
121+
122+
public ILconstReal GetUnitState(IlConstHandle unit, IlConstHandle unitstate) {
123+
UnitMock unitMock = unitOrNull(unit);
124+
if (unitMock == null) {
125+
return ILconstReal.create(0);
126+
}
127+
return unitMock.states.getOrDefault(unitStateKey(unitstate), ILconstReal.create(0));
128+
}
129+
130+
public void SetUnitState(IlConstHandle unit, IlConstHandle unitstate, ILconstReal value) {
131+
UnitMock unitMock = unitOrNull(unit);
132+
if (unitMock != null) {
133+
unitMock.states.put(unitStateKey(unitstate), value);
134+
}
135+
}
136+
137+
public ILconstReal GetWidgetLife(IlConstHandle widget) {
138+
UnitMock unitMock = unitOrNull(widget);
139+
if (unitMock != null) {
140+
return unitMock.states.getOrDefault("unitstate0", ILconstReal.create(0));
141+
}
142+
DestructableMock destructableMock = destructableOrNull(widget);
143+
return destructableMock == null ? ILconstReal.create(0) : destructableMock.life;
144+
}
145+
146+
public void SetWidgetLife(IlConstHandle widget, ILconstReal newLife) {
147+
UnitMock unitMock = unitOrNull(widget);
148+
if (unitMock != null) {
149+
unitMock.states.put("unitstate0", newLife);
150+
return;
151+
}
152+
DestructableMock destructableMock = destructableOrNull(widget);
153+
if (destructableMock != null) {
154+
destructableMock.life = newLife;
155+
}
156+
}
157+
158+
public ILconstBool UnitAddAbility(IlConstHandle unit, ILconstInt abilityId) {
159+
UnitMock unitMock = unitOrNull(unit);
160+
if (unitMock == null) {
161+
return ILconstBool.FALSE;
162+
}
163+
unitMock.abilityLevels.putIfAbsent(abilityId.getVal(), ILconstInt.create(1));
164+
return ILconstBool.TRUE;
165+
}
166+
167+
public ILconstBool UnitRemoveAbility(IlConstHandle unit, ILconstInt abilityId) {
168+
UnitMock unitMock = unitOrNull(unit);
169+
if (unitMock == null) {
170+
return ILconstBool.FALSE;
171+
}
172+
return ILconstBool.instance(unitMock.abilityLevels.remove(abilityId.getVal()) != null);
173+
}
174+
175+
public ILconstInt GetUnitAbilityLevel(IlConstHandle unit, ILconstInt abilityId) {
176+
UnitMock unitMock = unitOrNull(unit);
177+
if (unitMock == null) {
178+
return ILconstInt.create(0);
179+
}
180+
return unitMock.abilityLevels.getOrDefault(abilityId.getVal(), ILconstInt.create(0));
181+
}
182+
183+
public ILconstInt SetUnitAbilityLevel(IlConstHandle unit, ILconstInt abilityId, ILconstInt level) {
184+
UnitMock unitMock = unitOrNull(unit);
185+
if (unitMock == null) {
186+
return ILconstInt.create(0);
187+
}
188+
unitMock.abilityLevels.put(abilityId.getVal(), level);
189+
return level;
190+
}
191+
192+
public ILconstBool IssueImmediateOrderById(IlConstHandle unit, ILconstInt orderId) {
193+
return issueOrder(unit, orderId);
194+
}
195+
196+
public ILconstBool IssuePointOrderById(IlConstHandle unit, ILconstInt orderId, ILconstReal x, ILconstReal y) {
197+
return issueOrder(unit, orderId);
198+
}
199+
200+
public ILconstBool IssueTargetOrderById(IlConstHandle unit, ILconstInt orderId, IlConstHandle target) {
201+
return issueOrder(unit, orderId);
202+
}
203+
204+
public ILconstInt GetUnitCurrentOrder(IlConstHandle unit) {
205+
UnitMock unitMock = unitOrNull(unit);
206+
return unitMock == null ? ILconstInt.create(0) : unitMock.currentOrder;
207+
}
208+
73209
public ILconstInt GetUnitUserData(IlConstHandle unit) {
74210
return unit == null ? ILconstInt.create(0) : userDataMap.getOrDefault(unit, ILconstInt.create(0));
75211
}
@@ -80,4 +216,31 @@ public void SetUnitUserData(IlConstHandle unit, ILconstInt userData) {
80216
}
81217
userDataMap.put(unit, userData);
82218
}
219+
220+
private ILconstBool issueOrder(IlConstHandle unit, ILconstInt orderId) {
221+
UnitMock unitMock = unitOrNull(unit);
222+
if (unitMock == null) {
223+
return ILconstBool.FALSE;
224+
}
225+
unitMock.currentOrder = orderId;
226+
return ILconstBool.TRUE;
227+
}
228+
229+
private UnitMock unitOrNull(IlConstHandle unit) {
230+
if (unit == null || !(unit.getObj() instanceof UnitMock)) {
231+
return null;
232+
}
233+
return (UnitMock) unit.getObj();
234+
}
235+
236+
private DestructableMock destructableOrNull(IlConstHandle destructable) {
237+
if (destructable == null || !(destructable.getObj() instanceof DestructableMock)) {
238+
return null;
239+
}
240+
return (DestructableMock) destructable.getObj();
241+
}
242+
243+
private String unitStateKey(IlConstHandle unitstate) {
244+
return unitstate == null ? "unitstate0" : unitstate.print();
245+
}
83246
}

0 commit comments

Comments
 (0)