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

541 lines
16 KiB
Java
Raw Normal View History

2020-08-07 16:11:02 -04:00
package mindustry.logic;
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-08 14:14:33 -04:00
@RegisterStatement("write")
public static class WriteStatement extends LStatement{
public String to = "0";
public String from = "result";
@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-08 18:01:54 -04:00
field(table, from, str -> from = str);
2020-08-08 14:14:33 -04:00
}
@Override
public LCategory category(){
return LCategory.io;
}
@Override
public LInstruction build(LAssembler builder){
return new WriteI(builder.var(from), builder.var(to));
}
}
@RegisterStatement("read")
public static class ReadStatement extends LStatement{
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-08 14:14:33 -04:00
table.add(" = mem:: ");
2020-08-08 18:01:54 -04:00
field(table, from, str -> from = str);
2020-08-08 14:14:33 -04:00
}
@Override
public LCategory category(){
return LCategory.io;
}
@Override
public LInstruction build(LAssembler builder){
return new ReadI(builder.var(from), builder.var(to));
}
}
2020-08-09 12:46:44 -04:00
2020-08-09 15:10:13 -04:00
@RegisterStatement("draw")
public static class DrawStatement extends LStatement{
2020-08-09 12:46:44 -04:00
public CommandType type = CommandType.line;
public String x = "0", y = "0", p1 = "0", p2 = "0", p3 = "0";
@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-09 15:10:13 -04:00
b.clicked(() -> showSelect(b, CommandType.allNormal, type, t -> {
2020-08-09 12:46:44 -04:00
type = t;
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-09 15:10:13 -04:00
if(type != CommandType.stroke){
table.row();
}
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:
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-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);
s.row();
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);
s.row();
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);
s.row();
fields(s, "sides", p1, v -> p1 = v);
fields(s, "radius", p2, v -> p2 = v);
s.row();
fields(s, "rotation", p3, v -> p3 = v);
2020-08-09 12:46:44 -04:00
break;
}
2020-08-09 15:10:13 -04:00
}).expand().left();
2020-08-09 12:46:44 -04:00
}
@Override
public LCategory category(){
return LCategory.io;
}
@Override
public LInstruction build(LAssembler builder){
2020-08-09 15:10:13 -04:00
return new DisplayI((byte)type.ordinal(), 0, builder.var(x), builder.var(y), builder.var(p1), builder.var(p2), builder.var(p3));
}
}
@RegisterStatement("flush")
public static class FlushStatement extends LStatement{
public String target = "display";
@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 DisplayI(commandFlush, builder.var(target), 0, 0, 0, 0, 0);
2020-08-09 12:46:44 -04:00
}
}
2020-08-08 14:14:33 -04:00
@RegisterStatement("sensor")
public static class SensorStatement extends LStatement{
public String to = "result";
public String from = "@0", type = "@copper";
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-08 18:01:54 -04:00
table.row();
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 -> {
for(LSensor sensor : LSensor.all){
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();
}).size(80f, 50f).checked(selected == fi).group(group);
}
t.row();
2020-08-09 12:46:44 -04:00
t.add(stack).colspan(3).expand().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(){
return LCategory.operations;
}
@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
}
}
2020-08-07 23:01:40 -04:00
@RegisterStatement("enable")
public static class EnableStatement extends LStatement{
2020-08-07 16:11:02 -04:00
public String target = "result";
public String value = "0";
@Override
public void build(Table table){
2020-08-08 18:01:54 -04:00
field(table, target, str -> target = str);
2020-08-07 16:11:02 -04:00
table.add(" -> ");
2020-08-08 18:01:54 -04:00
field(table, value, str -> value = str);
2020-08-07 16:11:02 -04:00
}
@Override
public LCategory category(){
return LCategory.operations;
}
@Override
2020-08-07 23:01:40 -04:00
public LInstruction build(LAssembler builder){
return new EnableI(builder.var(target), builder.var(value));
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-08 18:01:54 -04:00
table.row();
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-09 15:10:13 -04:00
}, Styles.logict, () -> {}).size(50f, 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("print")
2020-08-07 18:30:03 -04:00
public static class PrintStatement extends LStatement{
public String value = "\"frog\"";
2020-08-08 14:14:33 -04:00
public String target = "result";
2020-08-07 18:30:03 -04:00
@Override
public void build(Table table){
2020-08-08 18:01:54 -04:00
field(table, value, str -> value = str);
2020-08-08 14:14:33 -04:00
table.add(" to ");
2020-08-08 18:01:54 -04:00
field(table, target, str -> target = str);
2020-08-07 18:30:03 -04:00
}
@Override
public LInstruction build(LAssembler builder){
2020-08-08 14:14:33 -04:00
return new PrintI(builder.var(value), builder.var(target));
2020-08-07 18:30:03 -04:00
}
@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(){
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;
}
}
2020-08-08 14:14:33 -04:00
//disabled until further notice - bypasses the network
/*
2020-08-07 23:01:40 -04:00
@RegisterStatement("getbuild")
public static class getBuildStatement extends LStatement{
2020-08-07 16:11:02 -04:00
public String x = "0", y = "0", dest = "result";
@Override
public void build(Table table){
table.field(dest, Styles.nodeField, str -> dest = str)
.size(100f, 40f).pad(2f).color(table.color);
table.add(" = ");
table.field(x, Styles.nodeField, str -> x = str)
.size(90f, 40f).pad(2f).color(table.color);
table.add(", ");
table.field(y, Styles.nodeField, str -> y = str)
.size(90f, 40f).pad(2f).color(table.color);
}
@Override
2020-08-07 17:50:54 -04:00
public LInstruction build(LAssembler builder){
2020-08-07 23:01:40 -04:00
return new GetBuildI(builder.var(dest), builder.var(x), builder.var(y));
2020-08-07 16:11:02 -04:00
}
@Override
public LCategory category(){
return LCategory.blocks;
}
2020-08-08 14:14:33 -04:00
}*/
2020-08-07 16:11:02 -04:00
}