Skip to content

Commit a5b6184

Browse files
committed
Display broken assertion message
1 parent c543af0 commit a5b6184

File tree

4 files changed

+95
-3
lines changed

4 files changed

+95
-3
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package cardillan.mlogassertions;
2+
3+
import arc.Events;
4+
import arc.graphics.Color;
5+
import arc.graphics.g2d.Draw;
6+
import arc.graphics.g2d.Font;
7+
import arc.graphics.g2d.GlyphLayout;
8+
import arc.scene.ui.layout.Scl;
9+
import arc.struct.GridMap;
10+
import arc.util.Align;
11+
import arc.util.pooling.Pools;
12+
import mindustry.game.EventType;
13+
import mindustry.ui.Fonts;
14+
import mindustry.world.blocks.logic.LogicBlock.LogicBuild;
15+
16+
import static mindustry.Vars.tilesize;
17+
18+
public class Assertions {
19+
final static GridMap<LogicBuild> blocks = new GridMap<>();
20+
final static GridMap<String> messages = new GridMap<>();
21+
final static Color textColor = Color.brick; //.a(0.8f);
22+
23+
static int removeX = -1;
24+
static int removeY = -1;
25+
26+
public static void add(LogicBuild block, String message) {
27+
int x = (int) block.getX();
28+
int y = (int) block.getY();
29+
if (blocks.get(x, y) != block) {
30+
blocks.put(x, y, block);
31+
messages.put(x, y, message);
32+
}
33+
}
34+
35+
public static void remove(int x, int y) {
36+
blocks.remove(x, y);
37+
messages.remove(x, y);
38+
}
39+
40+
public static void init() {
41+
Events.on(EventType.ConfigEvent.class, e -> remove((int) e.tile.x, (int) e.tile.y));
42+
43+
Events.run(EventType.Trigger.drawOver, () -> {
44+
blocks.values().forEach(Assertions::draw);
45+
if (removeX != -1) {
46+
remove(removeX, removeY);
47+
removeX = -1;
48+
removeY = -1;
49+
}
50+
});
51+
}
52+
53+
private static void draw(LogicBuild block) {
54+
if (block.tile().build != block) {
55+
removeX = (int) block.getX();
56+
removeY = (int) block.getY();
57+
return;
58+
}
59+
60+
String message = messages.get((int) block.getX(), (int) block.getY());
61+
62+
float x = block.getX();
63+
float y = block.getY() + block.block.size * tilesize / 2f + 1f;
64+
65+
Font font = Fonts.outline;
66+
GlyphLayout l = Pools.obtain(GlyphLayout.class, GlyphLayout::new);
67+
boolean ints = font.usesIntegerPositions();
68+
font.getData().setScale(1 / 4f / Scl.scl(1f));
69+
font.setUseIntegerPositions(false);
70+
71+
l.setText(font, message, textColor, 90f, Align.left, false);
72+
73+
// Draw.color(0f, 0f, 0f, 0.2f);
74+
// Fill.rect(x, y+2f, l.width+4f, l.height+2f);
75+
76+
Draw.color();
77+
font.setColor(textColor);
78+
font.draw(message, x - l.width/2f, y + l.height, 90f, Align.left, false);
79+
font.setUseIntegerPositions(ints);
80+
81+
font.getData().setScale(1f);
82+
83+
Pools.free(l);
84+
}
85+
}

src/cardillan/mlogassertions/Main.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ public void init(){
1010
super.init();
1111

1212
AssertLogic.init();
13+
Assertions.init();
1314
}
1415
}

src/cardillan/mlogassertions/logic/AssertLStatements.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void build(Table t) {
6767
table.add("").growX();
6868
});
6969
t.row();
70-
t.add("message: ").padLeft(6);
70+
t.add("message: ").maxTextLength(0).padLeft(6);
7171
field(t, message, str -> message = str).width(0f).growX().padRight(3);
7272
}
7373

src/cardillan/mlogassertions/logic/AssertsLInstructions.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package cardillan.mlogassertions.logic;
22

3+
import cardillan.mlogassertions.Assertions;
4+
import mindustry.gen.Building;
35
import mindustry.logic.LExecutor;
46
import mindustry.logic.LExecutor.Var;
7+
import mindustry.world.blocks.logic.LogicBlock;
58

69
public class AssertsLInstructions {
710

@@ -35,8 +38,11 @@ public final void run(LExecutor exec) {
3538
boolean maxAssert = opMax.function.get(num(exec,value), num(exec,max));
3639

3740
if (!typeAssert || !minAssert || !maxAssert) {
41+
Building building = exec.building( LExecutor.varThis);
42+
Var message = var(exec, this.message);
43+
Assertions.add((LogicBlock.LogicBuild) building, LExecutor.PrintI.toString(message.objval));
44+
3845
//skip back to self.
39-
// TODO enter broken assertion state
4046
exec.counter.numval--;
4147
//exec.yield = true;
4248
}
@@ -45,7 +51,7 @@ public final void run(LExecutor exec) {
4551
// For easier switching between v7 and v8 logic
4652

4753
private Var var(LExecutor exec, int var) {
48-
return exec.var(value);
54+
return exec.var(var);
4955
}
5056

5157
private double num(LExecutor exec, int var) {

0 commit comments

Comments
 (0)