From 1de3217738c8930f76acd06410f3a5ce22447fcb Mon Sep 17 00:00:00 2001 From: Anuken Date: Thu, 13 Aug 2020 10:20:29 -0400 Subject: [PATCH] GetLink instruction --- core/src/mindustry/logic/LExecutor.java | 22 ++++++++++++++++ core/src/mindustry/logic/LStatements.java | 25 ++++++++++++++++++- .../world/blocks/logic/LogicBlock.java | 13 +++++++++- 3 files changed, 58 insertions(+), 2 deletions(-) diff --git a/core/src/mindustry/logic/LExecutor.java b/core/src/mindustry/logic/LExecutor.java index 754f669798..1115904aea 100644 --- a/core/src/mindustry/logic/LExecutor.java +++ b/core/src/mindustry/logic/LExecutor.java @@ -29,6 +29,7 @@ public class LExecutor{ public LongSeq graphicsBuffer = new LongSeq(); public StringBuilder textBuffer = new StringBuilder(); + public Building[] links = {}; public boolean initialized(){ return instructions != null && vars != null && instructions.length > 0; @@ -161,6 +162,27 @@ public class LExecutor{ } } + public static class GetLinkI implements LInstruction{ + public int output, index; + + public GetLinkI(int output, int index){ + this.index = index; + this.output = output; + } + + public GetLinkI(){ + } + + @Override + public void run(LExecutor exec){ + int address = exec.numi(index); + + if(address >= 0 && address < exec.links.length){ + exec.setobj(output, exec.links[address]); + } + } + } + public static class ReadI implements LInstruction{ public int target, position, output; diff --git a/core/src/mindustry/logic/LStatements.java b/core/src/mindustry/logic/LStatements.java index 7a5d7449c4..42636cb505 100644 --- a/core/src/mindustry/logic/LStatements.java +++ b/core/src/mindustry/logic/LStatements.java @@ -56,6 +56,30 @@ public class LStatements{ } } + @RegisterStatement("read") + 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)); + } + } + @RegisterStatement("write") public static class WriteStatement extends LStatement{ public String input = "result", target = "cell1", address = "0"; @@ -120,7 +144,6 @@ public class LStatements{ } } - @RegisterStatement("draw") public static class DrawStatement extends LStatement{ public GraphicsType type = GraphicsType.clear; diff --git a/core/src/mindustry/world/blocks/logic/LogicBlock.java b/core/src/mindustry/world/blocks/logic/LogicBlock.java index a47d4fe229..ee1c3ef2be 100644 --- a/core/src/mindustry/world/blocks/logic/LogicBlock.java +++ b/core/src/mindustry/world/blocks/logic/LogicBlock.java @@ -262,11 +262,22 @@ public class LogicBlock extends Block{ //store connections for(LogicLink link : links){ - if(link.active && validLink(world.build(link.x, link.y))){ + if(link.active && (link.valid = validLink(world.build(link.x, link.y)))){ asm.putConst(link.name, world.build(link.x, link.y)); } } + //store link objects + executor.links = new Building[links.count(l -> l.valid && l.active)]; + int index = 0; + for(LogicLink link : links){ + if(link.active && link.valid){ + executor.links[index ++] = world.build(link.x, link.y); + } + } + + asm.putConst("@links", executor.links.length); + //store any older variables for(Var var : executor.vars){ if(!var.constant){