From 7482e2a988b9d38eb5d9eb233e95f744e86e8f10 Mon Sep 17 00:00:00 2001 From: Anuken Date: Wed, 5 Aug 2020 19:52:09 -0400 Subject: [PATCH] Bugfixes --- core/assets/bundles/bundle.properties | 2 +- .../src/mindustry/editor/MapEditorDialog.java | 6 +- core/src/mindustry/game/Saves.java | 9 +- core/src/mindustry/input/InputHandler.java | 4 +- core/src/mindustry/logic/BinaryOp.java | 31 ++++ core/src/mindustry/logic/LogicCanvas.java | 140 ++++++++++++++---- core/src/mindustry/logic/LogicExecutor.java | 114 -------------- core/src/mindustry/logic/LogicNode.java | 27 +++- 8 files changed, 176 insertions(+), 157 deletions(-) create mode 100644 core/src/mindustry/logic/BinaryOp.java delete mode 100644 core/src/mindustry/logic/LogicExecutor.java diff --git a/core/assets/bundles/bundle.properties b/core/assets/bundles/bundle.properties index 344101e0e6..4b8026df80 100644 --- a/core/assets/bundles/bundle.properties +++ b/core/assets/bundles/bundle.properties @@ -385,7 +385,7 @@ editor.exportimage = Export Terrain Image editor.exportimage.description = Export an image file containing only basic terrain editor.loadimage = Import Terrain editor.saveimage = Export Terrain -editor.unsaved = [scarlet]You have unsaved changes![]\nAre you sure you want to exit? +editor.unsaved = Are you sure you want to exit?\n[scarlet]Any unsaved changes will be lost. editor.resizemap = Resize Map editor.mapname = Map Name: editor.overwrite = [accent]Warning!\nThis overwrites an existing map. diff --git a/core/src/mindustry/editor/MapEditorDialog.java b/core/src/mindustry/editor/MapEditorDialog.java index 045d0b724e..1805df28d0 100644 --- a/core/src/mindustry/editor/MapEditorDialog.java +++ b/core/src/mindustry/editor/MapEditorDialog.java @@ -649,11 +649,7 @@ public class MapEditorDialog extends Dialog implements Disposable{ } private void tryExit(){ - if(!saved){ - ui.showConfirm("@confirm", "@editor.unsaved", this::hide); - }else{ - hide(); - } + ui.showConfirm("@confirm", "@editor.unsaved", this::hide); } private void addBlockSelection(Table table){ diff --git a/core/src/mindustry/game/Saves.java b/core/src/mindustry/game/Saves.java index 8dc89a47fd..bf9dd2ae95 100644 --- a/core/src/mindustry/game/Saves.java +++ b/core/src/mindustry/game/Saves.java @@ -165,9 +165,10 @@ public class Saves{ } public void deleteAll(){ - saves.clear(); - for(Fi file : saveDirectory.list()){ - file.delete(); + for(SaveSlot slot : saves.copy()){ + if(!slot.isSector()){ + slot.delete(); + } } } @@ -187,7 +188,7 @@ public class Saves{ current = this; totalPlaytime = meta.timePlayed; savePreview(); - }catch(Exception e){ + }catch(Throwable e){ throw new SaveException(e); } } diff --git a/core/src/mindustry/input/InputHandler.java b/core/src/mindustry/input/InputHandler.java index 40e50e4601..4d7f27fa1b 100644 --- a/core/src/mindustry/input/InputHandler.java +++ b/core/src/mindustry/input/InputHandler.java @@ -262,7 +262,7 @@ public abstract class InputHandler implements InputProcessor, GestureListener{ if(commander.isCommanding()){ commander.clearCommand(); }else{ - FormationPattern pattern = new SquareFormation(); + SquareFormation pattern = new SquareFormation(); Formation formation = new Formation(new Vec3(player.x, player.y, player.unit().rotation), pattern); formation.slotAssignmentStrategy = new DistanceAssignmentStrategy(pattern); @@ -278,6 +278,8 @@ public abstract class InputHandler implements InputProcessor, GestureListener{ units.sort(u -> u.dst2(player.unit())); units.truncate(player.unit().type().commandLimit); + if(units.any()) pattern.spacing = units.max(u -> u.hitSize).hitSize * 2.4f; + commander.command(formation, units); } diff --git a/core/src/mindustry/logic/BinaryOp.java b/core/src/mindustry/logic/BinaryOp.java new file mode 100644 index 0000000000..8a0161878f --- /dev/null +++ b/core/src/mindustry/logic/BinaryOp.java @@ -0,0 +1,31 @@ +package mindustry.logic; + +public enum BinaryOp{ + add("+", (a, b) -> a + b), + sub("-", (a, b) -> a - b), + mul("*", (a, b) -> a * b), + div("/", (a, b) -> a / b), + mod("%", (a, b) -> a % b), + pow("^", Math::pow), + shl(">>", (a, b) -> (int)a >> (int)b), + shr("<<", (a, b) -> (int)a << (int)b), + or("or", (a, b) -> (int)a | (int)b), + and("and", (a, b) -> (int)a & (int)b), + xor("xor", (a, b) -> (int)a ^ (int)b); + + final OpLambda function; + final String symbol; + + BinaryOp(String symbol, OpLambda function){ + this.symbol = symbol; + this.function = function; + } + + public double get(double a, double b){ + return function.get(a, b); + } + + interface OpLambda{ + double get(double a, double b); + } +} diff --git a/core/src/mindustry/logic/LogicCanvas.java b/core/src/mindustry/logic/LogicCanvas.java index 429d65c3cf..3b80dbb734 100644 --- a/core/src/mindustry/logic/LogicCanvas.java +++ b/core/src/mindustry/logic/LogicCanvas.java @@ -4,7 +4,9 @@ import arc.*; import arc.graphics.*; import arc.graphics.g2d.*; import arc.input.*; +import arc.math.*; import arc.math.geom.*; +import arc.scene.*; import arc.scene.event.*; import arc.scene.ui.*; import arc.scene.ui.layout.*; @@ -18,13 +20,17 @@ public class LogicCanvas extends WidgetGroup{ private static final Color backgroundCol = Color.black, gridCol = Pal.accent.cpy().mul(0.2f); private static final Color outCol = Pal.place, inCol = Pal.remove; + private Element selected; + private Element entered; private Seq nodes = new Seq<>(); { - LogicElement e = new LogicElement(); - e.setPosition(Core.graphics.getWidth()/2f, Core.graphics.getHeight()/2f); - addChild(e); - e.pack(); + for(int i = 0; i < 3; i++){ + LogicElement e = new LogicElement(); + e.setPosition(Core.graphics.getWidth()/2f, Core.graphics.getHeight()/2f); + addChild(e); + e.pack(); + } } @Override @@ -53,17 +59,45 @@ public class LogicCanvas extends WidgetGroup{ Draw.reset(); super.draw(); + + for(Element e : getChildren()){ + if(e instanceof LogicElement){ + LogicElement l = (LogicElement)e; + + for(NodeField field : l.fields){ + field.drawConnection(); + } + } + } + + if(selected != null){ + NodeField field = (NodeField)selected.userObject; + Vec2 dest = selected.localToStageCoordinates(Tmp.v1.set(selected.getWidth()/2f, selected.getHeight()/2f)); + Vec2 mouse = Core.input.mouse(); + drawCurve(dest.x, dest.y, mouse.x, mouse.y, field.color); + } } - static class LogicElement extends Table{ + void drawCurve(float x, float y, float x2, float y2, Color color){ + Lines.stroke(4f, color); + Lines.curve( + x, y, + x2, y, + x, y2, + x2, y2, + Math.max(3, (int)(Mathf.dst(x, y, x2, y2) / 5)) + ); + + Draw.reset(); + } + + class LogicElement extends Table{ LogicNode node; NodeField[] fields = {new NodeField(true, "input 1"), new NodeField(true, "input 2"), new NodeField(false, "output 1"), new NodeField(false, "output 2")}; LogicElement(){ background(Tex.whitePane); setColor(Pal.accent.cpy().mul(0.9f).shiftSaturation(-0.3f)); - touchable = Touchable.enabled; - margin(0f); table(Tex.whiteui, t -> { @@ -72,6 +106,7 @@ public class LogicCanvas extends WidgetGroup{ }); t.margin(8f); + t.touchable = Touchable.enabled; t.add("Node").style(Styles.outlineLabel).color(color); t.add().growX(); @@ -79,6 +114,27 @@ public class LogicCanvas extends WidgetGroup{ //TODO disconnect things remove(); }); + t.addListener(new InputListener(){ + float lastx, lasty; + + @Override + public boolean touchDown(InputEvent event, float x, float y, int pointer, KeyCode button){ + Vec2 v = localToStageCoordinates(Tmp.v1.set(x, y)); + lastx = v.x; + lasty = v.y; + toFront(); + return true; + } + + @Override + public void touchDragged(InputEvent event, float x, float y, int pointer){ + Vec2 v = localToStageCoordinates(Tmp.v1.set(x, y)); + + moveBy(v.x - lastx, v.y - lasty); + lastx = v.x; + lasty = v.y; + } + }); }).growX().padBottom(6); row(); @@ -91,28 +147,6 @@ public class LogicCanvas extends WidgetGroup{ } marginBottom(7); - - addListener(new InputListener(){ - float lastx, lasty; - - @Override - public boolean touchDown(InputEvent event, float x, float y, int pointer, KeyCode button){ - Vec2 v = localToStageCoordinates(Tmp.v1.set(x, y)); - lastx = v.x; - lasty = v.y; - toFront(); - return true; - } - - @Override - public void touchDragged(InputEvent event, float x, float y, int pointer){ - Vec2 v = localToStageCoordinates(Tmp.v1.set(x, y)); - - moveBy(v.x - lastx, v.y - lasty); - lastx = v.x; - lasty = v.y; - } - }); } @Override @@ -128,13 +162,15 @@ public class LogicCanvas extends WidgetGroup{ } } - static class NodeField extends Table{ + class NodeField extends Table{ boolean input; ImageButton button; + Element connection; NodeField(boolean input, String name){ this.input = input; - setColor(input ? inCol : outCol); + //TODO color should depend on data type + setColor(outCol); float marg = 24f; @@ -154,6 +190,15 @@ public class LogicCanvas extends WidgetGroup{ } } + void drawConnection(){ + if(connection != null){ + Vec2 from = localToStageCoordinates(Tmp.v2.set(button.getX() + button.getWidth()/2f, button.getY() + button.getHeight()/2f)); + Vec2 to = connection.localToStageCoordinates(Tmp.v1.set(connection.getWidth()/2f, connection.getHeight()/2f)); + + drawCurve(from.x, from.y, to.x, to.y, color); + } + } + void addIcon(){ float s = 30f; Cell c = button(Tex.logicNode, Styles.colori, () -> { @@ -171,6 +216,39 @@ public class LogicCanvas extends WidgetGroup{ }else{ c.padRight(-pad); } + + button.addListener(new InputListener(){ + @Override + public boolean touchDown(InputEvent event, float x, float y, int pointer, KeyCode code){ + if(selected == null){ + selected = button; + } + return true; + } + + @Override + public void enter(InputEvent event, float x, float y, int pointer, Element fromActor){ + entered = button; + } + + @Override + public void touchUp(InputEvent event, float x, float y, int pointer, KeyCode code){ + localToStageCoordinates(Tmp.v1.set(x, y)); + Element element = entered; + + if(element != null && element.userObject instanceof NodeField){ + NodeField field = (NodeField)element.userObject; + if(field != NodeField.this && field.input != input){ + connection = element; + //field.connection = button; + } + } + + if(selected == button){ + selected = null; + } + } + }); } } } diff --git a/core/src/mindustry/logic/LogicExecutor.java b/core/src/mindustry/logic/LogicExecutor.java deleted file mode 100644 index 11f44bc26d..0000000000 --- a/core/src/mindustry/logic/LogicExecutor.java +++ /dev/null @@ -1,114 +0,0 @@ -package mindustry.logic; - -import mindustry.gen.*; - -import java.nio.*; - -public class LogicExecutor{ - Instruction[] instructions; - ByteBuffer memory = ByteBuffer.allocate(1024 * 512); - int counter; - - void step(){ - if(instructions.length == 0) return; - - instructions[counter].exec(); - counter ++; - - //loop counter - if(counter >= instructions.length) counter = 0; - } - - Building device(short id){ - return null; //TODO - } - - interface Instruction{ - void exec(); - } - - static class RegisterI implements Instruction{ - /** operation to perform */ - Op op; - /** destination memory */ - int dest; - /** memory to take data from. -1 for immediate values. */ - int left, right; - /** left/right immediate values, only used if no registers are present. */ - double ileft, iright; - - @Override - public void exec(){ - //memory.putDouble(dest, op.function.get(left == -1 ? ileft : registers[left], right == -1 ? iright : registers[right])); - } - } - - static class ReadI implements Instruction{ - /** register to write result to */ - short dest; - /** device to read from */ - short device; - /** the type of data to be read */ - ReadOp op; - /** any additional read parameters */ - int parameter; - - @Override - public void exec(){ - //registers[dest] = op.function.get(device(device), parameter); - } - } - - static class WriteI implements Instruction{ - - @Override - public void exec(){ - - } - } - - enum ReadOp{ - item((tile, id) -> tile.items == null ? 0 : tile.items.get(id)), - itemTotal((tile, param) -> tile.items == null ? 0 : tile.items.total()); - - final ReadOpLambda function; - final String symbol; - - ReadOp(ReadOpLambda function){ - this.symbol = name(); - this.function = function; - } - - interface ReadOpLambda{ - int get(Building tile, int parameter); - } - } - - enum Op{ - add("+", (a, b) -> a + b), - sub("-", (a, b) -> a - b), - mul("*", (a, b) -> a * b), - div("/", (a, b) -> a / b), - mod("%", (a, b) -> a % b), - pow("^", Math::pow), - shl(">>", (a, b) -> (int)a >> (int)b), - shr("<<", (a, b) -> (int)a << (int)b), - or("or", (a, b) -> (int)a | (int)b), - and("and", (a, b) -> (int)a & (int)b), - xor("xor", (a, b) -> (int)a ^ (int)b); - - final OpLambda function; - final String symbol; - - Op(String symbol, OpLambda function){ - this.symbol = symbol; - this.function = function; - } - - interface OpLambda{ - double get(double a, double b); - } - } - - -} diff --git a/core/src/mindustry/logic/LogicNode.java b/core/src/mindustry/logic/LogicNode.java index 1e9e2cc7dd..b5346d032c 100644 --- a/core/src/mindustry/logic/LogicNode.java +++ b/core/src/mindustry/logic/LogicNode.java @@ -1,4 +1,29 @@ package mindustry.logic; -public class LogicNode{ +import arc.func.*; + +public abstract class LogicNode{ + + public void run(){} + + public NodeInput[] inputs(){ + return new NodeInput[0]; + } + + public static class BinaryOpNode{ + public BinaryOp op = BinaryOp.add; + public double a, b; + } + + public static class NodeInput{ + public boolean num; + public String name; + public SetNum setNum; + public Cons setObject; + } + + public interface SetNum{ + void set(double val); + } + }