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

997 lines
31 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-09-27 17:15:31 -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-11-10 08:57:30 -05:00
import mindustry.graphics.*;
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-10-06 11:26:25 -04:00
import mindustry.world.meta.*;
2020-08-07 16:11:02 -04:00
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
2020-11-10 08:57:30 -05:00
public Color color(){
return Pal.logicControl;
2020-08-09 12:46:44 -04:00
}
@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
2020-11-10 08:57:30 -05:00
public Color color(){
return Pal.logicOperations;
2020-08-10 14:19:24 -04:00
}
@Override
public LInstruction build(LAssembler builder){
return new NoopI();
}
}
2020-08-23 10:14:04 -04:00
@RegisterStatement("read")
public static class ReadStatement extends LStatement{
public String output = "result", target = "cell1", address = "0";
2020-08-13 10:20:29 -04:00
@Override
public void build(Table table){
2020-08-23 10:14:04 -04:00
table.add(" read ");
2020-08-13 10:20:29 -04:00
field(table, output, str -> output = str);
2020-08-23 10:14:04 -04:00
table.add(" = ");
fields(table, target, str -> target = str);
row(table);
table.add(" at ");
2020-08-13 10:20:29 -04:00
field(table, address, str -> address = str);
}
@Override
2020-11-10 08:57:30 -05:00
public Color color(){
return Pal.logicIo;
2020-08-13 10:20:29 -04:00
}
@Override
public LInstruction build(LAssembler builder){
2020-08-23 10:14:04 -04:00
return new ReadI(builder.var(target), builder.var(address), builder.var(output));
2020-08-13 10:20:29 -04:00
}
}
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
2020-11-10 08:57:30 -05:00
public Color color(){
return Pal.logicIo;
2020-08-08 14:14:33 -04:00
}
@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
}
}
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";
}
if(type == GraphicsType.image){
p1 = "@copper";
p2 = "32";
p3 = "0";
}
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){
2020-09-30 17:21:05 -04:00
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);
2020-09-30 17:21:05 -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-09-30 17:21:05 -04:00
}
case stroke -> {
2020-08-09 15:10:13 -04:00
s.add().width(4);
fields(s, x, v -> x = v);
2020-09-30 17:21:05 -04:00
}
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-09-30 17:21:05 -04:00
}
case rect, 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-09-30 17:21:05 -04:00
}
case poly, 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-09-30 17:21:05 -04:00
}
case triangle -> {
2020-08-11 12:54:39 -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-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);
2020-09-30 17:21:05 -04:00
}
case image -> {
fields(s, "x", x, v -> x = v);
fields(s, "y", y, v -> y = v);
row(s);
fields(s, "image", p1, v -> p1 = v);
fields(s, "size", p2, v -> p2 = v);
row(s);
fields(s, "rotation", p3, v -> p3 = v);
}
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
2020-11-10 08:57:30 -05:00
public Color color(){
return Pal.logicIo;
2020-08-09 12:46:44 -04:00
}
@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-23 10:14:04 -04:00
@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
2020-11-10 08:57:30 -05:00
public Color color(){
return Pal.logicIo;
2020-08-23 10:14:04 -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
2020-11-10 08:57:30 -05:00
public Color color(){
return Pal.logicBlocks;
2020-08-09 15:10:13 -04:00
}
@Override
public LInstruction build(LAssembler builder){
2020-08-10 14:19:24 -04:00
return new DrawFlushI(builder.var(target));
}
}
2020-08-23 10:14:04 -04:00
@RegisterStatement("printflush")
public static class PrintFlushStatement extends LStatement{
public String target = "message1";
2020-08-10 14:19:24 -04:00
@Override
public void build(Table table){
2020-08-23 10:14:04 -04:00
table.add(" to ");
field(table, target, str -> target = str);
2020-08-10 14:19:24 -04:00
}
@Override
2020-11-10 08:57:30 -05:00
public Color color(){
return Pal.logicBlocks;
2020-08-10 14:19:24 -04:00
}
@Override
2020-08-23 10:14:04 -04:00
public LInstruction build(LAssembler builder){
return new PrintFlushI(builder.var(target));
2020-08-10 14:19:24 -04:00
}
}
2020-08-23 10:14:04 -04:00
@RegisterStatement("getlink")
public static class GetLinkStatement extends LStatement{
public String output = "result", address = "0";
2020-08-10 14:19:24 -04:00
@Override
public void build(Table table){
2020-08-23 10:14:04 -04:00
field(table, output, str -> output = str);
table.add(" = link# ");
field(table, address, str -> address = str);
2020-08-10 14:19:24 -04:00
}
@Override
2020-11-10 08:57:30 -05:00
public Color color(){
return Pal.logicBlocks;
2020-08-10 14:19:24 -04:00
}
@Override
public LInstruction build(LAssembler builder){
2020-08-23 10:14:04 -04:00
return new GetLinkI(builder.var(output), builder.var(address));
2020-08-10 14:19:24 -04:00
}
}
@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;
2020-10-05 15:42:37 -04:00
for(int i = 0; i < type.params.length; i++){
2020-08-10 14:19:24 -04:00
2020-10-05 15:42:37 -04:00
fields(table, type.params[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-10 14:19:24 -04:00
2020-08-14 10:48:29 -04:00
if(++c % 2 == 0) row(table);
2020-08-10 14:19:24 -04:00
}
}
@Override
2020-11-10 08:57:30 -05:00
public Color color(){
return Pal.logicBlocks;
2020-08-10 14:19:24 -04:00
}
@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;
2021-01-02 17:38:04 -05:00
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();
2020-10-05 15:42:37 -04:00
if(buildFrom()){
table.add(" from ");
2020-08-10 23:04:52 -04:00
2020-10-05 15:42:37 -04:00
fields(table, radar, v -> radar = v);
2020-08-10 23:04:52 -04:00
2020-10-05 15:42:37 -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);
}
2020-10-05 15:42:37 -04:00
public boolean buildFrom(){
return true;
}
2020-08-10 23:04:52 -04:00
@Override
2020-11-10 08:57:30 -05:00
public Color color(){
return Pal.logicBlocks;
2020-08-10 23:04:52 -04:00
}
@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]);
2020-12-05 10:58:30 -05:00
t.parent.parent.pack();
t.parent.parent.invalidateHierarchy();
2021-02-09 12:10:15 -05:00
}).height(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
2020-11-10 08:57:30 -05:00
public Color color(){
return Pal.logicBlocks;
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
2020-11-10 08:57:30 -05:00
public Color color(){
return Pal.logicOperations;
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 23:01:40 -04:00
return new SetI(builder.var(from), builder.var(to));
2020-08-07 16:11:02 -04:00
}
}
2020-08-23 10:14:04 -04:00
@RegisterStatement("op")
public static class OperationStatement extends LStatement{
public LogicOp op = LogicOp.add;
public String dest = "result", a = "a", b = "b";
2020-08-07 16:11:02 -04:00
@Override
public void build(Table table){
2020-08-23 10:14:04 -04:00
rebuild(table);
}
void rebuild(Table table){
table.clearChildren();
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-23 10:14:04 -04:00
if(op.unary){
opButton(table);
2020-08-07 16:11:02 -04:00
2020-08-23 10:14:04 -04:00
field(table, a, str -> a = str);
}else{
row(table);
2020-08-07 16:11:02 -04:00
2021-02-09 09:34:58 -05:00
//"function"-type operations have the name at the left and arguments on the right
if(op.func){
2021-02-09 12:10:15 -05:00
if(LCanvas.useRows()){
table.left();
table.row();
table.table(c -> {
c.color.set(color());
c.left();
funcs(c);
}).colspan(2).left();
}else{
funcs(table);
}
2021-02-09 09:34:58 -05:00
}else{
field(table, a, str -> a = str);
2020-08-07 16:11:02 -04:00
2021-02-09 09:34:58 -05:00
opButton(table);
field(table, b, str -> b = str);
}
2020-08-23 10:14:04 -04:00
}
2020-08-07 16:11:02 -04:00
}
2021-02-09 12:10:15 -05:00
void funcs(Table table){
opButton(table);
field(table, a, str -> a = str);
field(table, b, str -> b = str);
}
2020-08-23 10:14:04 -04:00
void opButton(Table table){
table.button(b -> {
b.label(() -> op.symbol);
2020-08-23 10:14:04 -04:00
b.clicked(() -> showSelect(b, LogicOp.all, op, o -> {
op = o;
rebuild(table);
}));
2021-02-09 12:10:15 -05:00
}, Styles.logict, () -> {}).size(64f, 40f).pad(4f).color(table.color);
}
@Override
public LInstruction build(LAssembler builder){
2020-08-23 10:14:04 -04:00
return new OpI(op,builder.var(a), builder.var(b), builder.var(dest));
}
@Override
2020-11-10 08:57:30 -05:00
public Color color(){
return Pal.logicOperations;
}
}
//TODO untested
//@RegisterStatement("wait")
public static class WaitStatement extends LStatement{
public String value = "0.5";
@Override
public void build(Table table){
field(table, value, str -> value = str);
table.add(" sec");
}
@Override
public Color color(){
return Pal.logicOperations;
}
@Override
public LInstruction build(LAssembler builder){
return new WaitI(builder.var(value));
}
}
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
2020-11-10 08:57:30 -05:00
public Color color(){
return Pal.logicControl;
2020-08-07 16:11:02 -04:00
}
}
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-09-27 17:15:31 -04:00
private static Color last = new Color();
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);
2020-09-27 17:15:31 -04:00
last = table.color;
table.table(this::rebuild);
table.add().growX();
table.add(new JumpButton(() -> dest, s -> dest = s)).size(30).right().padLeft(-8);
}
void rebuild(Table table){
table.clearChildren();
table.setColor(last);
if(op != ConditionOp.always) field(table, value, str -> value = str);
table.button(b -> {
b.label(() -> op.symbol);
2020-09-27 17:15:31 -04:00
b.clicked(() -> showSelect(b, ConditionOp.all, op, o -> {
op = o;
rebuild(table);
}));
}, Styles.logict, () -> {}).size(op == ConditionOp.always ? 80f : 48f, 40f).pad(4f).color(table.color);
2020-08-08 18:01:54 -04:00
2020-09-27 17:15:31 -04:00
if(op != ConditionOp.always) field(table, compare, str -> compare = str);
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
2020-11-10 08:57:30 -05:00
public Color color(){
return Pal.logicControl;
2020-08-07 16:11:02 -04:00
}
}
2020-10-05 15:42:37 -04:00
@RegisterStatement("ubind")
public static class UnitBindStatement extends LStatement{
public String type = "@poly";
2020-10-05 15:42:37 -04:00
@Override
public void build(Table table){
table.add(" type ");
2020-10-05 19:59:15 -04:00
TextField field = field(table, type, str -> type = str).get();
table.button(b -> {
b.image(Icon.pencilSmall);
b.clicked(() -> showSelectTable(b, (t, hide) -> {
t.row();
t.table(i -> {
i.left();
int c = 0;
for(UnitType item : Vars.content.units()){
if(!item.unlockedNow() || item.isHidden()) continue;
i.button(new TextureRegionDrawable(item.icon(Cicon.small)), Styles.cleari, () -> {
type = "@" + item.name;
field.setText(type);
hide.run();
2020-10-06 11:26:25 -04:00
}).size(40f).get().resizeImage(Cicon.small.size);
2020-10-05 19:59:15 -04:00
if(++c % 6 == 0) i.row();
}
}).colspan(3).width(240f).left();
}));
}, Styles.logict, () -> {}).size(40f).padLeft(-2).color(table.color);
2020-10-05 15:42:37 -04:00
}
@Override
2020-11-10 08:57:30 -05:00
public Color color(){
return Pal.logicUnits;
2020-10-05 15:42:37 -04:00
}
@Override
public LInstruction build(LAssembler builder){
return new UnitBindI(builder.var(type));
}
}
@RegisterStatement("ucontrol")
public static class UnitControlStatement extends LStatement{
public LUnitControl type = LUnitControl.move;
2020-10-25 09:23:13 -04:00
public String p1 = "0", p2 = "0", p3 = "0", p4 = "0", p5 = "0";
2020-10-05 15:42:37 -04:00
@Override
public void build(Table table){
rebuild(table);
}
void rebuild(Table table){
table.clearChildren();
table.left();
table.add(" ");
table.button(b -> {
b.label(() -> type.name());
b.clicked(() -> showSelect(b, LUnitControl.all, type, t -> {
type = t;
rebuild(table);
}, 2, cell -> cell.size(120, 50)));
}, Styles.logict, () -> {}).size(120, 40).color(table.color).left().padLeft(2);
row(table);
//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.params.length; i++){
2020-10-25 09:23:13 -04:00
fields(table, type.params[i], i == 0 ? p1 : i == 1 ? p2 : i == 2 ? p3 : i == 3 ? p4 : p5, i == 0 ? v -> p1 = v : i == 1 ? v -> p2 = v : i == 2 ? v -> p3 = v : i == 3 ? v -> p4 = v : v -> p5 = v).width(100f);
2020-10-05 15:42:37 -04:00
if(++c % 2 == 0) row(table);
2020-10-25 09:23:13 -04:00
if(i == 3){
table.row();
}
2020-10-05 15:42:37 -04:00
}
}
@Override
2020-11-10 08:57:30 -05:00
public Color color(){
return Pal.logicUnits;
2020-10-05 15:42:37 -04:00
}
@Override
public LInstruction build(LAssembler builder){
2020-10-24 10:21:57 -04:00
return new UnitControlI(type, builder.var(p1), builder.var(p2), builder.var(p3), builder.var(p4), builder.var(p5));
2020-10-05 15:42:37 -04:00
}
}
@RegisterStatement("uradar")
public static class UnitRadarStatement extends RadarStatement{
public UnitRadarStatement(){
radar = "0";
}
2020-10-05 15:42:37 -04:00
@Override
public boolean buildFrom(){
//do not build the "from" section
return false;
}
@Override
2020-11-10 08:57:30 -05:00
public Color color(){
return Pal.logicUnits;
2020-10-05 15:42:37 -04:00
}
@Override
public LInstruction build(LAssembler builder){
return new RadarI(target1, target2, target3, sort, LExecutor.varUnit, builder.var(sortOrder), builder.var(output));
}
}
2020-10-06 11:26:25 -04:00
@RegisterStatement("ulocate")
public static class UnitLocateStatement extends LStatement{
public LLocate locate = LLocate.building;
public BlockFlag flag = BlockFlag.core;
public String enemy = "true", ore = "@copper";
2020-10-20 14:02:10 -04:00
public String outX = "outx", outY = "outy", outFound = "found", outBuild = "building";
2020-10-06 11:26:25 -04:00
@Override
public void build(Table table){
rebuild(table);
}
void rebuild(Table table){
table.clearChildren();
table.add(" find ").left();
table.button(b -> {
b.label(() -> locate.name());
b.clicked(() -> showSelect(b, LLocate.all, locate, t -> {
locate = t;
rebuild(table);
}, 2, cell -> cell.size(110, 50)));
}, Styles.logict, () -> {}).size(110, 40).color(table.color).left().padLeft(2);
switch(locate){
case building -> {
row(table);
table.add(" type ").left();
table.button(b -> {
b.label(() -> flag.name());
b.clicked(() -> showSelect(b, BlockFlag.all, flag, t -> flag = t, 2, cell -> cell.size(110, 50)));
}, Styles.logict, () -> {}).size(110, 40).color(table.color).left().padLeft(2);
row(table);
table.add(" enemy ").left();
fields(table, enemy, str -> enemy = str);
table.row();
}
case ore -> {
table.add(" ore ").left();
table.table(ts -> {
ts.color.set(table.color);
2020-10-24 10:21:57 -04:00
fields(ts, ore, str -> ore = str);
2020-10-06 11:26:25 -04:00
ts.button(b -> {
b.image(Icon.pencilSmall);
b.clicked(() -> showSelectTable(b, (t, hide) -> {
t.row();
t.table(i -> {
i.left();
int c = 0;
for(Item item : Vars.content.items()){
if(!item.unlockedNow()) continue;
i.button(new TextureRegionDrawable(item.icon(Cicon.small)), Styles.cleari, () -> {
ore = "@" + item.name;
rebuild(table);
hide.run();
}).size(40f).get().resizeImage(Cicon.small.size);
if(++c % 6 == 0) i.row();
}
}).colspan(3).width(240f).left();
}));
}, Styles.logict, () -> {}).size(40f).padLeft(-2).color(table.color);
});
table.row();
}
2020-10-11 19:10:33 -04:00
case spawn, damaged -> {
2020-10-06 11:26:25 -04:00
table.row();
}
}
table.add(" outX ").left();
fields(table, outX, str -> outX = str);
table.add(" outY ").left();
fields(table, outY, str -> outY = str);
row(table);
table.add(" found ").left();
fields(table, outFound, str -> outFound = str);
2020-10-24 10:21:57 -04:00
if(locate != LLocate.ore){
table.add(" building ").left();
fields(table, outBuild, str -> outBuild = str);
}
2020-10-06 11:26:25 -04:00
}
@Override
2020-11-10 08:57:30 -05:00
public Color color(){
return Pal.logicUnits;
2020-10-06 11:26:25 -04:00
}
@Override
public LInstruction build(LAssembler builder){
2020-10-20 14:02:10 -04:00
return new UnitLocateI(locate, flag, builder.var(enemy), builder.var(ore), builder.var(outX), builder.var(outY), builder.var(outFound), builder.var(outBuild));
2020-10-06 11:26:25 -04:00
}
}
2020-08-07 16:11:02 -04:00
}