Files
Mindustry/core/src/mindustry/logic/LStatements.java

698 lines
21 KiB
Java
Raw Normal View History

2020-08-07 16:11:02 -04:00
package mindustry.logic;
2020-08-10 23:04:52 -04:00
import arc.func.*;
2020-08-07 16:11:02 -04:00
import arc.graphics.*;
2020-08-09 10:03:02 -04:00
import arc.scene.style.*;
import arc.scene.ui.*;
2020-08-07 16:11:02 -04:00
import arc.scene.ui.layout.*;
2020-08-09 10:03:02 -04:00
import mindustry.*;
2020-08-07 23:01:40 -04:00
import mindustry.annotations.Annotations.*;
2020-08-09 10:03:02 -04:00
import mindustry.gen.*;
2020-08-07 16:11:02 -04:00
import mindustry.logic.LCanvas.*;
2020-08-08 14:14:33 -04:00
import mindustry.logic.LExecutor.*;
2020-08-09 10:03:02 -04:00
import mindustry.type.*;
2020-08-07 16:11:02 -04:00
import mindustry.ui.*;
2020-08-09 12:46:44 -04:00
import static mindustry.world.blocks.logic.LogicDisplay.*;
2020-08-07 16:11:02 -04:00
public class LStatements{
2020-08-09 12:46:44 -04:00
//TODO broken
//@RegisterStatement("#")
public static class CommentStatement extends LStatement{
public String comment = "";
@Override
public void build(Table table){
table.area(comment, Styles.nodeArea, v -> comment = v).growX().height(90f).padLeft(2).padRight(6).color(table.color);
}
@Override
public LCategory category(){
return LCategory.control;
}
@Override
public LInstruction build(LAssembler builder){
return null;
}
}
2020-08-10 14:19:24 -04:00
@RegisterStatement("noop")
public static class InvalidStatement extends LStatement{
@Override
public void build(Table table){
}
@Override
public LCategory category(){
return LCategory.operations;
}
@Override
public LInstruction build(LAssembler builder){
return new NoopI();
}
}
2020-08-13 14:59:27 -04:00
@RegisterStatement("getlink")
2020-08-13 10:20:29 -04:00
public static class GetLinkStatement extends LStatement{
public String output = "result", address = "0";
@Override
public void build(Table table){
field(table, output, str -> output = str);
table.add(" = link# ");
field(table, address, str -> address = str);
}
@Override
public LCategory category(){
return LCategory.io;
}
@Override
public LInstruction build(LAssembler builder){
return new GetLinkI(builder.var(output), builder.var(address));
}
}
2020-08-08 14:14:33 -04:00
@RegisterStatement("write")
public static class WriteStatement extends LStatement{
2020-08-12 18:51:50 -04:00
public String input = "result", target = "cell1", address = "0";
2020-08-08 14:14:33 -04:00
@Override
public void build(Table table){
2020-08-11 10:50:12 -04:00
table.add(" write ");
2020-08-08 14:14:33 -04:00
2020-08-11 10:50:12 -04:00
field(table, input, str -> input = str);
table.add(" to ");
fields(table, target, str -> target = str);
2020-08-14 10:48:29 -04:00
row(table);
2020-08-11 10:50:12 -04:00
table.add(" at ");
2020-08-08 14:14:33 -04:00
2020-08-11 10:50:12 -04:00
field(table, address, str -> address = str);
2020-08-08 14:14:33 -04:00
}
@Override
public LCategory category(){
return LCategory.io;
}
@Override
public LInstruction build(LAssembler builder){
2020-08-11 10:50:12 -04:00
return new WriteI(builder.var(target), builder.var(address), builder.var(input));
2020-08-08 14:14:33 -04:00
}
}
@RegisterStatement("read")
public static class ReadStatement extends LStatement{
2020-08-12 18:51:50 -04:00
public String output = "result", target = "cell1", address = "0";
2020-08-08 14:14:33 -04:00
@Override
public void build(Table table){
2020-08-11 10:50:12 -04:00
table.add(" read ");
field(table, output, str -> output = str);
table.add(" = ");
fields(table, target, str -> target = str);
2020-08-14 10:48:29 -04:00
row(table);
2020-08-08 14:14:33 -04:00
2020-08-11 10:50:12 -04:00
table.add(" at ");
2020-08-08 14:14:33 -04:00
2020-08-11 10:50:12 -04:00
field(table, address, str -> address = str);
2020-08-08 14:14:33 -04:00
}
@Override
public LCategory category(){
return LCategory.io;
}
@Override
public LInstruction build(LAssembler builder){
2020-08-11 10:50:12 -04:00
return new ReadI(builder.var(target), builder.var(address), builder.var(output));
2020-08-08 14:14:33 -04:00
}
}
2020-08-09 15:10:13 -04:00
@RegisterStatement("draw")
public static class DrawStatement extends LStatement{
2020-08-11 12:54:39 -04:00
public GraphicsType type = GraphicsType.clear;
public String x = "0", y = "0", p1 = "0", p2 = "0", p3 = "0", p4 = "0";
2020-08-09 12:46:44 -04:00
@Override
public void build(Table table){
rebuild(table);
}
void rebuild(Table table){
table.clearChildren();
2020-08-09 15:10:13 -04:00
table.left();
2020-08-09 12:46:44 -04:00
table.button(b -> {
b.label(() -> type.name());
2020-08-10 14:19:24 -04:00
b.clicked(() -> showSelect(b, GraphicsType.all, type, t -> {
2020-08-09 12:46:44 -04:00
type = t;
2020-08-17 20:49:24 -04:00
if(type == GraphicsType.color){
p2 = "255";
}
2020-08-09 12:46:44 -04:00
rebuild(table);
}, 2, cell -> cell.size(100, 50)));
2020-08-09 15:10:13 -04:00
}, Styles.logict, () -> {}).size(90, 40).color(table.color).left().padLeft(2);
2020-08-09 12:46:44 -04:00
2020-08-10 14:19:24 -04:00
if(type != GraphicsType.stroke){
2020-08-14 10:48:29 -04:00
row(table);
2020-08-09 15:10:13 -04:00
}
2020-08-09 12:46:44 -04:00
2020-08-09 15:10:13 -04:00
table.table(s -> {
s.left();
s.setColor(table.color);
2020-08-09 12:46:44 -04:00
switch(type){
case clear:
2020-08-17 20:49:24 -04:00
fields(s, "r", x, v -> x = v);
fields(s, "g", y, v -> y = v);
fields(s, "b", p1, v -> p1 = v);
break;
2020-08-09 12:46:44 -04:00
case color:
2020-08-09 15:10:13 -04:00
fields(s, "r", x, v -> x = v);
fields(s, "g", y, v -> y = v);
fields(s, "b", p1, v -> p1 = v);
2020-08-17 20:49:24 -04:00
row(s);
fields(s, "a", p2, v -> p2 = v);
2020-08-09 12:46:44 -04:00
break;
case stroke:
2020-08-09 15:10:13 -04:00
s.add().width(4);
fields(s, x, v -> x = v);
2020-08-09 12:46:44 -04:00
break;
case line:
2020-08-09 15:10:13 -04:00
fields(s, "x", x, v -> x = v);
fields(s, "y", y, v -> y = v);
2020-08-14 10:48:29 -04:00
row(s);
2020-08-09 15:10:13 -04:00
fields(s, "x2", p1, v -> p1 = v);
fields(s, "y2", p2, v -> p2 = v);
2020-08-09 12:46:44 -04:00
break;
case rect:
case lineRect:
2020-08-09 15:10:13 -04:00
fields(s, "x", x, v -> x = v);
fields(s, "y", y, v -> y = v);
2020-08-14 10:48:29 -04:00
row(s);
2020-08-09 15:10:13 -04:00
fields(s, "width", p1, v -> p1 = v);
fields(s, "height", p2, v -> p2 = v);
2020-08-09 12:46:44 -04:00
break;
case poly:
case linePoly:
2020-08-09 15:10:13 -04:00
fields(s, "x", x, v -> x = v);
fields(s, "y", y, v -> y = v);
2020-08-14 10:48:29 -04:00
row(s);
2020-08-09 15:10:13 -04:00
fields(s, "sides", p1, v -> p1 = v);
fields(s, "radius", p2, v -> p2 = v);
2020-08-14 10:48:29 -04:00
row(s);
2020-08-09 15:10:13 -04:00
fields(s, "rotation", p3, v -> p3 = v);
2020-08-09 12:46:44 -04:00
break;
2020-08-11 12:54:39 -04:00
case triangle:
fields(s, "x", x, v -> x = v);
fields(s, "y", y, v -> y = v);
2020-08-14 10:48:29 -04:00
row(s);
2020-08-11 12:54:39 -04:00
fields(s, "x2", p1, v -> p1 = v);
fields(s, "y2", p2, v -> p2 = v);
2020-08-14 10:48:29 -04:00
row(s);
2020-08-11 12:54:39 -04:00
fields(s, "x3", p3, v -> p3 = v);
fields(s, "y3", p4, v -> p4 = v);
break;
2020-08-09 12:46:44 -04:00
}
2020-08-09 15:10:13 -04:00
}).expand().left();
2020-08-17 20:49:24 -04:00
}
2020-08-09 15:10:13 -04:00
2020-08-17 20:49:24 -04:00
@Override
public void afterRead(){
//0 constant alpha for colors is not allowed
if(type == GraphicsType.color && p2.equals("0")){
p2 = "255";
}
2020-08-09 12:46:44 -04:00
}
@Override
public LCategory category(){
return LCategory.io;
}
@Override
public LInstruction build(LAssembler builder){
2020-08-11 12:54:39 -04:00
return new DrawI((byte)type.ordinal(), 0, builder.var(x), builder.var(y), builder.var(p1), builder.var(p2), builder.var(p3), builder.var(p4));
2020-08-09 15:10:13 -04:00
}
}
2020-08-10 14:19:24 -04:00
@RegisterStatement("drawflush")
public static class DrawFlushStatement extends LStatement{
2020-08-12 18:51:50 -04:00
public String target = "display1";
2020-08-09 15:10:13 -04:00
@Override
public void build(Table table){
table.add(" to ");
field(table, target, str -> target = str);
}
@Override
public LCategory category(){
return LCategory.blocks;
}
@Override
public LInstruction build(LAssembler builder){
2020-08-10 14:19:24 -04:00
return new DrawFlushI(builder.var(target));
}
}
@RegisterStatement("print")
public static class PrintStatement extends LStatement{
public String value = "\"frog\"";
@Override
public void build(Table table){
field(table, value, str -> value = str).width(0f).growX().padRight(3);
}
@Override
public LInstruction build(LAssembler builder){
return new PrintI(builder.var(value));
}
@Override
public LCategory category(){
return LCategory.control;
}
}
@RegisterStatement("printflush")
public static class PrintFlushStatement extends LStatement{
2020-08-12 18:51:50 -04:00
public String target = "message1";
2020-08-10 14:19:24 -04:00
@Override
public void build(Table table){
table.add(" to ");
field(table, target, str -> target = str);
}
@Override
public LCategory category(){
return LCategory.blocks;
}
@Override
public LInstruction build(LAssembler builder){
return new PrintFlushI(builder.var(target));
}
}
@RegisterStatement("control")
public static class ControlStatement extends LStatement{
public LAccess type = LAccess.enabled;
2020-08-12 18:51:50 -04:00
public String target = "block1", p1 = "0", p2 = "0", p3 = "0", p4 = "0";
2020-08-10 14:19:24 -04:00
@Override
public void build(Table table){
rebuild(table);
}
void rebuild(Table table){
table.clearChildren();
table.left();
2020-08-10 23:04:52 -04:00
table.add(" set ");
2020-08-10 14:19:24 -04:00
table.button(b -> {
b.label(() -> type.name());
b.clicked(() -> showSelect(b, LAccess.controls, type, t -> {
type = t;
rebuild(table);
}, 2, cell -> cell.size(100, 50)));
2020-08-10 16:10:18 -04:00
}, Styles.logict, () -> {}).size(90, 40).color(table.color).left().padLeft(2);
2020-08-10 14:19:24 -04:00
table.add(" of ");
field(table, target, v -> target = v);
2020-08-14 10:48:29 -04:00
row(table);
2020-08-10 14:19:24 -04:00
//Q: why don't you just use arrays for this?
//A: arrays aren't as easy to serialize so the code generator doesn't handle them
int c = 0;
for(int i = 0; i < type.parameters.length; i++){
fields(table, type.parameters[i], i == 0 ? p1 : i == 1 ? p2 : i == 2 ? p3 : p4, i == 0 ? v -> p1 = v : i == 1 ? v -> p2 = v : i == 2 ? v -> p3 = v : v -> p4 = v);
2020-08-14 10:48:29 -04:00
if(++c % 2 == 0) row(table);
2020-08-10 14:19:24 -04:00
}
}
@Override
public LCategory category(){
return LCategory.blocks;
}
@Override
public LInstruction build(LAssembler builder){
return new ControlI(type, builder.var(target), builder.var(p1), builder.var(p2), builder.var(p3), builder.var(p4));
2020-08-09 12:46:44 -04:00
}
}
2020-08-10 23:04:52 -04:00
@RegisterStatement("radar")
public static class RadarStatement extends LStatement{
public RadarTarget target1 = RadarTarget.enemy, target2 = RadarTarget.any, target3 = RadarTarget.any;
public RadarSort sort = RadarSort.distance;
public String radar = "turret1", sortOrder = "1", output = "result";
2020-08-10 23:04:52 -04:00
@Override
public void build(Table table){
table.defaults().left();
table.add(" from ");
fields(table, radar, v -> radar = v);
2020-08-14 10:48:29 -04:00
row(table);
2020-08-10 23:04:52 -04:00
for(int i = 0; i < 3; i++){
int fi = i;
Prov<RadarTarget> get = () -> (fi == 0 ? target1 : fi == 1 ? target2 : target3);
table.add(i == 0 ? " target " : " and ");
table.button(b -> {
b.label(() -> get.get().name());
b.clicked(() -> showSelect(b, RadarTarget.all, get.get(), t -> {
if(fi == 0) target1 = t; else if(fi == 1) target2 = t; else target3 = t;
}, 2, cell -> cell.size(100, 50)));
}, Styles.logict, () -> {}).size(90, 40).color(table.color).left().padLeft(2);
if(i == 1){
2020-08-14 10:48:29 -04:00
row(table);
2020-08-10 23:04:52 -04:00
}
}
table.add(" order ");
fields(table, sortOrder, v -> sortOrder = v);
2020-08-18 00:52:29 -04:00
table.row();
table.add(" sort ");
table.button(b -> {
b.label(() -> sort.name());
b.clicked(() -> showSelect(b, RadarSort.all, sort, t -> {
sort = t;
}, 2, cell -> cell.size(100, 50)));
}, Styles.logict, () -> {}).size(90, 40).color(table.color).left().padLeft(2);
2020-08-10 23:04:52 -04:00
table.add(" output ");
fields(table, output, v -> output = v);
}
@Override
public LCategory category(){
return LCategory.blocks;
}
@Override
public LInstruction build(LAssembler builder){
return new RadarI(target1, target2, target3, sort, builder.var(radar), builder.var(sortOrder), builder.var(output));
}
}
2020-08-08 14:14:33 -04:00
@RegisterStatement("sensor")
public static class SensorStatement extends LStatement{
public String to = "result";
2020-08-12 18:51:50 -04:00
public String from = "block1", type = "@copper";
2020-08-08 14:14:33 -04:00
2020-08-09 10:03:02 -04:00
private transient int selected = 0;
private transient TextField tfield;
2020-08-08 14:14:33 -04:00
@Override
public void build(Table table){
2020-08-08 18:01:54 -04:00
field(table, to, str -> to = str);
2020-08-08 14:14:33 -04:00
table.add(" = ");
2020-08-14 10:48:29 -04:00
row(table);
2020-08-08 18:01:54 -04:00
2020-08-09 10:03:02 -04:00
tfield = field(table, type, str -> type = str).padRight(0f).get();
table.button(b -> {
b.image(Icon.pencilSmall);
//240
b.clicked(() -> showSelectTable(b, (t, hide) -> {
Table[] tables = {
//items
new Table(i -> {
2020-08-09 12:46:44 -04:00
i.left();
2020-08-09 10:03:02 -04:00
int c = 0;
for(Item item : Vars.content.items()){
2020-08-09 12:46:44 -04:00
if(!item.unlockedNow()) continue;
2020-08-09 10:03:02 -04:00
i.button(new TextureRegionDrawable(item.icon(Cicon.small)), Styles.cleari, () -> {
stype("@" + item.name);
hide.run();
}).size(40f);
if(++c % 6 == 0) i.row();
}
}),
//liquids
new Table(i -> {
2020-08-09 12:46:44 -04:00
i.left();
2020-08-09 10:03:02 -04:00
int c = 0;
for(Liquid item : Vars.content.liquids()){
2020-08-09 12:46:44 -04:00
if(!item.unlockedNow()) continue;
2020-08-09 10:03:02 -04:00
i.button(new TextureRegionDrawable(item.icon(Cicon.small)), Styles.cleari, () -> {
stype("@" + item.name);
hide.run();
}).size(40f);
if(++c % 6 == 0) i.row();
}
}),
//sensors
new Table(i -> {
2020-08-10 14:19:24 -04:00
for(LAccess sensor : LAccess.senseable){
2020-08-09 10:03:02 -04:00
i.button(sensor.name(), Styles.cleart, () -> {
stype("@" + sensor.name());
hide.run();
}).size(240f, 40f).row();
}
})
};
Drawable[] icons = {Icon.box, Icon.liquid, Icon.tree};
Stack stack = new Stack(tables[selected]);
ButtonGroup<Button> group = new ButtonGroup<>();
for(int i = 0; i < tables.length; i++){
int fi = i;
t.button(icons[i], Styles.clearTogglei, () -> {
selected = fi;
stack.clearChildren();
stack.addChild(tables[selected]);
t.pack();
2020-08-11 12:48:17 -04:00
}).size(80f, 50f).growX().checked(selected == fi).group(group);
2020-08-09 10:03:02 -04:00
}
t.row();
2020-08-11 12:48:17 -04:00
t.add(stack).colspan(3).width(240f).left();
2020-08-09 10:03:02 -04:00
}));
2020-08-09 15:10:13 -04:00
}, Styles.logict, () -> {}).size(40f).padLeft(-1).color(table.color);
2020-08-08 14:14:33 -04:00
table.add(" in ");
2020-08-08 18:01:54 -04:00
field(table, from, str -> from = str);
2020-08-08 14:14:33 -04:00
}
2020-08-09 10:03:02 -04:00
private void stype(String text){
tfield.setText(text);
this.type = text;
}
2020-08-08 14:14:33 -04:00
@Override
public LCategory category(){
2020-08-09 20:47:35 -04:00
return LCategory.blocks;
2020-08-08 14:14:33 -04:00
}
@Override
public LInstruction build(LAssembler builder){
return new SenseI(builder.var(from), builder.var(to), builder.var(type));
}
}
2020-08-07 23:01:40 -04:00
@RegisterStatement("set")
public static class SetStatement extends LStatement{
2020-08-07 16:11:02 -04:00
public String to = "result";
public String from = "0";
@Override
public void build(Table table){
2020-08-08 18:01:54 -04:00
field(table, to, str -> to = str);
2020-08-07 16:11:02 -04:00
table.add(" = ");
2020-08-08 18:01:54 -04:00
field(table, from, str -> from = str);
2020-08-07 16:11:02 -04:00
}
@Override
public LCategory category(){
return LCategory.operations;
}
@Override
2020-08-07 17:50:54 -04:00
public LInstruction build(LAssembler builder){
2020-08-07 23:01:40 -04:00
return new SetI(builder.var(from), builder.var(to));
2020-08-07 16:11:02 -04:00
}
}
@RegisterStatement("bop")
public static class BinaryOpStatement extends LStatement{
2020-08-07 17:50:54 -04:00
public BinaryOp op = BinaryOp.add;
2020-08-07 16:11:02 -04:00
public String a = "a", b = "b", dest = "result";
@Override
public void build(Table table){
2020-08-08 18:01:54 -04:00
field(table, dest, str -> dest = str);
2020-08-07 16:11:02 -04:00
table.add(" = ");
2020-08-14 10:48:29 -04:00
row(table);
2020-08-07 16:11:02 -04:00
2020-08-08 18:01:54 -04:00
field(table, a, str -> a = str);
2020-08-07 16:11:02 -04:00
table.button(b -> {
b.label(() -> op.symbol);
b.clicked(() -> showSelect(b, BinaryOp.all, op, o -> op = o));
2020-08-10 23:21:14 -04:00
}, Styles.logict, () -> {}).size(60f, 40f).pad(4f).color(table.color);
2020-08-07 16:11:02 -04:00
2020-08-08 18:01:54 -04:00
field(table, b, str -> b = str);
2020-08-07 16:11:02 -04:00
}
@Override
2020-08-07 17:50:54 -04:00
public LInstruction build(LAssembler builder){
2020-08-07 16:11:02 -04:00
return new BinaryOpI(op,builder.var(a), builder.var(b), builder.var(dest));
}
@Override
public LCategory category(){
return LCategory.operations;
}
}
@RegisterStatement("uop")
public static class UnaryOpStatement extends LStatement{
public UnaryOp op = UnaryOp.negate;
public String value = "b", dest = "result";
@Override
public void build(Table table){
field(table, dest, str -> dest = str);
table.add(" = ");
table.button(b -> {
b.label(() -> op.symbol);
b.clicked(() -> showSelect(b, UnaryOp.all, op, o -> op = o));
2020-08-09 15:10:13 -04:00
}, Styles.logict, () -> {}).size(50f, 40f).pad(3f).color(table.color);
field(table, value, str -> value = str);
}
@Override
public LInstruction build(LAssembler builder){
return new UnaryOpI(op, builder.var(value), builder.var(dest));
}
@Override
public LCategory category(){
return LCategory.operations;
}
}
2020-08-07 23:01:40 -04:00
@RegisterStatement("end")
2020-08-07 16:11:02 -04:00
public static class EndStatement extends LStatement{
@Override
public void build(Table table){
}
@Override
2020-08-07 17:50:54 -04:00
public LInstruction build(LAssembler builder){
2020-08-07 16:11:02 -04:00
return new EndI();
}
@Override
public LCategory category(){
return LCategory.control;
}
}
2020-08-07 23:01:40 -04:00
@RegisterStatement("jump")
2020-08-07 16:11:02 -04:00
public static class JumpStatement extends LStatement{
2020-08-07 17:50:54 -04:00
public transient StatementElem dest;
2020-08-07 18:30:03 -04:00
2020-08-07 17:50:54 -04:00
public int destIndex;
public ConditionOp op = ConditionOp.notEqual;
public String value = "x", compare = "false";
2020-08-07 16:11:02 -04:00
@Override
public void build(Table table){
table.add("if ").padLeft(4);
field(table, value, str -> value = str);
table.button(b -> {
b.label(() -> op.symbol);
b.clicked(() -> showSelect(b, ConditionOp.all, op, o -> op = o));
}, Styles.logict, () -> {}).size(48f, 40f).pad(4f).color(table.color);
field(table, compare, str -> compare = str);
2020-08-08 18:01:54 -04:00
2020-08-07 16:11:02 -04:00
table.add().growX();
table.add(new JumpButton(Color.white, () -> dest, s -> dest = s)).size(30).right().padLeft(-8);
2020-08-07 16:11:02 -04:00
}
2020-08-07 17:50:54 -04:00
//elements need separate conversion logic
2020-08-07 16:11:02 -04:00
@Override
2020-08-07 18:30:03 -04:00
public void setupUI(){
2020-08-11 12:48:17 -04:00
if(elem != null && destIndex >= 0 && destIndex < elem.parent.getChildren().size){
2020-08-07 18:30:03 -04:00
dest = (StatementElem)elem.parent.getChildren().get(destIndex);
2020-08-07 17:50:54 -04:00
}
}
@Override
2020-08-07 18:30:03 -04:00
public void saveUI(){
if(elem != null){
destIndex = dest == null ? -1 : dest.parent.getChildren().indexOf(dest);
}
2020-08-07 17:50:54 -04:00
}
@Override
public LInstruction build(LAssembler builder){
return new JumpI(op, builder.var(value), builder.var(compare), destIndex);
2020-08-07 16:11:02 -04:00
}
@Override
public LCategory category(){
return LCategory.control;
}
}
}