From 4cdd0b9646b6c678c6f70fdcf9e5cb4334e09953 Mon Sep 17 00:00:00 2001 From: way-zer Date: Wed, 14 Feb 2024 14:36:27 +0800 Subject: [PATCH 1/6] Extend logic `read` and `write`,support targeting `LogicBuild` --- core/src/mindustry/logic/LExecutor.java | 62 +++++++++++++++++++------ 1 file changed, 48 insertions(+), 14 deletions(-) diff --git a/core/src/mindustry/logic/LExecutor.java b/core/src/mindustry/logic/LExecutor.java index 2e87d6c84b..926b8141f4 100644 --- a/core/src/mindustry/logic/LExecutor.java +++ b/core/src/mindustry/logic/LExecutor.java @@ -60,6 +60,8 @@ public class LExecutor{ //yes, this is a minor memory leak, but it's probably not significant enough to matter protected IntFloatMap unitTimeouts = new IntFloatMap(); + //lookup variable by name, lazy init. + protected ObjectIntMap nameMap; boolean timeoutDone(Unit unit, float delay){ return Time.time >= unitTimeouts.get(unit.id) + delay; @@ -123,6 +125,16 @@ public class LExecutor{ return index < 0 ? logicVars.get(-index) : vars[index]; } + public @Nullable Var optionalVar(String name){ + if(nameMap == null){ + nameMap = new ObjectIntMap<>(); + for(int i = 0; i < vars.length; i++){ + nameMap.put(vars[i].name, i); + } + } + return optionalVar(nameMap.get(name, -1)); + } + /** @return a Var from this processor, never a global constant. May be null if out of bounds. */ public @Nullable Var optionalVar(int index){ return index < 0 || index >= vars.length ? null : vars[index]; @@ -133,6 +145,13 @@ public class LExecutor{ return var(index).isobj && o instanceof Building building ? building : null; } + public @Nullable Building accissibleBuilding(int index){ + Building res = building(index); + if(res == null || privileged) return res; + if(res.block.privileged || res.team != team) return null; + return res; + } + public @Nullable Object obj(int index){ Object o = var(index).objval; return var(index).isobj ? o : null; @@ -331,8 +350,8 @@ public class LExecutor{ cache.found = false; exec.setnum(outFound, 0); } - - if(res != null && res.build != null && + + if(res != null && res.build != null && (unit.within(res.build.x, res.build.y, Math.max(unit.range(), buildingRange)) || res.build.team == exec.team)){ cache.build = res.build; exec.setobj(outBuild, res.build); @@ -660,12 +679,18 @@ public class LExecutor{ @Override public void run(LExecutor exec){ - int address = exec.numi(position); - Building from = exec.building(target); - - if(from instanceof MemoryBuild mem && (exec.privileged || from.team == exec.team)){ - + Building from = exec.accissibleBuilding(target); + if(from instanceof MemoryBuild mem){ + int address = exec.numi(position); exec.setnum(output, address < 0 || address >= mem.memory.length ? 0 : mem.memory[address]); + }else if(from instanceof LogicBuild logic && exec.obj(position) instanceof String name){ + Var fromVar = logic.executor.optionalVar(name); + Var toVar = exec.var(output); + if(fromVar != null && !toVar.constant){ + toVar.objval = fromVar.objval; + toVar.numval = fromVar.numval; + toVar.isobj = fromVar.isobj; + } } } } @@ -684,11 +709,20 @@ public class LExecutor{ @Override public void run(LExecutor exec){ - int address = exec.numi(position); - Building from = exec.building(target); - - if(from instanceof MemoryBuild mem && (exec.privileged || from.team == exec.team) && address >= 0 && address < mem.memory.length){ - mem.memory[address] = exec.num(value); + Building from = exec.accissibleBuilding(target); + if(from instanceof MemoryBuild mem){ + int address = exec.numi(position); + if(address >= 0 && address < mem.memory.length){ + mem.memory[address] = exec.num(value); + } + }else if(from instanceof LogicBuild logic && exec.obj(position) instanceof String name){ + Var toVar = logic.executor.optionalVar(name); + Var fromVar = exec.var(value); + if(toVar != null && !toVar.constant){ + toVar.objval = fromVar.objval; + toVar.numval = fromVar.numval; + toVar.isobj = fromVar.isobj; + } } } } @@ -989,7 +1023,7 @@ public class LExecutor{ //graphics on headless servers are useless. if(Vars.headless) return; - if(exec.building(target) instanceof LogicDisplayBuild d && (d.team == exec.team || exec.privileged)){ + if(exec.accissibleBuilding(target) instanceof LogicDisplayBuild d){ if(d.commands.size + exec.graphicsBuffer.size < maxDisplayBuffer){ for(int i = 0; i < exec.graphicsBuffer.size; i++){ d.commands.addLast(exec.graphicsBuffer.items[i]); @@ -1058,7 +1092,7 @@ public class LExecutor{ @Override public void run(LExecutor exec){ - if(exec.building(target) instanceof MessageBuild d && (d.team == exec.team || exec.privileged)){ + if(exec.accissibleBuilding(target) instanceof MessageBuild d){ d.message.setLength(0); d.message.append(exec.textBuffer, 0, Math.min(exec.textBuffer.length(), maxTextBuffer)); From 95817a955cf6f855556b28f2352ea32e5273e1a0 Mon Sep 17 00:00:00 2001 From: way-zer Date: Wed, 14 Feb 2024 14:36:27 +0800 Subject: [PATCH 2/6] amend --- core/src/mindustry/logic/LExecutor.java | 1 + 1 file changed, 1 insertion(+) diff --git a/core/src/mindustry/logic/LExecutor.java b/core/src/mindustry/logic/LExecutor.java index 62a84d85ab..56699d266f 100644 --- a/core/src/mindustry/logic/LExecutor.java +++ b/core/src/mindustry/logic/LExecutor.java @@ -97,6 +97,7 @@ public class LExecutor{ /** Loads with a specified assembler. Resets all variables. */ public void load(LAssembler builder){ + nameMap = null; vars = new Var[builder.vars.size]; instructions = builder.instructions; iptIndex = -1; From 33faade4265951785435c8ba670c5d89d702fe07 Mon Sep 17 00:00:00 2001 From: way-zer Date: Wed, 14 Feb 2024 16:57:17 +0800 Subject: [PATCH 3/6] typo --- core/src/mindustry/logic/LExecutor.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/core/src/mindustry/logic/LExecutor.java b/core/src/mindustry/logic/LExecutor.java index 56699d266f..5be0b9e555 100644 --- a/core/src/mindustry/logic/LExecutor.java +++ b/core/src/mindustry/logic/LExecutor.java @@ -154,7 +154,7 @@ public class LExecutor{ return var(index).isobj && o instanceof Building building ? building : null; } - public @Nullable Building accissibleBuilding(int index){ + public @Nullable Building accessibleBuilding(int index){ Building res = building(index); if(res == null || privileged) return res; if(res.block.privileged || res.team != team) return null; @@ -699,7 +699,7 @@ public class LExecutor{ @Override public void run(LExecutor exec){ - Building from = exec.accissibleBuilding(target); + Building from = exec.accessibleBuilding(target); if(from instanceof MemoryBuild mem){ int address = exec.numi(position); exec.setnum(output, address < 0 || address >= mem.memory.length ? 0 : mem.memory[address]); @@ -729,7 +729,7 @@ public class LExecutor{ @Override public void run(LExecutor exec){ - Building from = exec.accissibleBuilding(target); + Building from = exec.accessibleBuilding(target); if(from instanceof MemoryBuild mem){ int address = exec.numi(position); if(address >= 0 && address < mem.memory.length){ @@ -1095,7 +1095,7 @@ public class LExecutor{ //graphics on headless servers are useless. if(Vars.headless) return; - if(exec.accissibleBuilding(target) instanceof LogicDisplayBuild d){ + if(exec.accessibleBuilding(target) instanceof LogicDisplayBuild d){ if(d.commands.size + exec.graphicsBuffer.size < maxDisplayBuffer){ for(int i = 0; i < exec.graphicsBuffer.size; i++){ d.commands.addLast(exec.graphicsBuffer.items[i]); @@ -1213,7 +1213,7 @@ public class LExecutor{ @Override public void run(LExecutor exec){ - if(exec.accissibleBuilding(target) instanceof MessageBuild d){ + if(exec.accessibleBuilding(target) instanceof MessageBuild d){ d.message.setLength(0); d.message.append(exec.textBuffer, 0, Math.min(exec.textBuffer.length(), maxTextBuffer)); From 08ad8ad7de00ec064fbf4775a0b5389b4a042b33 Mon Sep 17 00:00:00 2001 From: way-zer Date: Sun, 18 Feb 2024 17:45:18 +0800 Subject: [PATCH 4/6] limit `write` with `@writable` --- core/src/mindustry/logic/LExecutor.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core/src/mindustry/logic/LExecutor.java b/core/src/mindustry/logic/LExecutor.java index 5be0b9e555..d2ea9c6cf9 100644 --- a/core/src/mindustry/logic/LExecutor.java +++ b/core/src/mindustry/logic/LExecutor.java @@ -736,6 +736,8 @@ public class LExecutor{ mem.memory[address] = exec.num(value); } }else if(from instanceof LogicBuild logic && exec.obj(position) instanceof String name){ + Var writable = logic.executor.optionalVar("@writable"); + if(writable == null || (writable.isobj ? writable.objval == null : Math.abs(writable.numval) < 0.00001)) return; Var toVar = logic.executor.optionalVar(name); Var fromVar = exec.var(value); if(toVar != null && !toVar.constant){ From 848249a6dde9cdc8369b5d179e3b70de5a75b911 Mon Sep 17 00:00:00 2001 From: MEEPofFaith <54301439+MEEPofFaith@users.noreply.github.com> Date: Sat, 8 Feb 2025 21:32:34 -0800 Subject: [PATCH 5/6] Consistent engine whites (#10482) --- core/src/mindustry/type/UnitType.java | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/core/src/mindustry/type/UnitType.java b/core/src/mindustry/type/UnitType.java index a8d7ddc846..83fb19f6f5 100644 --- a/core/src/mindustry/type/UnitType.java +++ b/core/src/mindustry/type/UnitType.java @@ -1754,29 +1754,30 @@ public class UnitType extends UnlockableContent implements Senseable{ Tmp.v1.set(x, y).rotate(rot); float ex = Tmp.v1.x, ey = Tmp.v1.y; + float rad = (radius + Mathf.absin(Time.time, 2f, radius / 4f)) * scale; //engine outlines (cursed?) /*float z = Draw.z(); Draw.z(z - 0.0001f); Draw.color(type.outlineColor); Fill.circle( - unit.x + ex, - unit.y + ey, - (type.outlineRadius * Draw.scl + radius + Mathf.absin(Time.time, 2f, radius / 4f)) * scale + unit.x + ex, + unit.y + ey, + (type.outlineRadius * Draw.scl + radius + Mathf.absin(Time.time, 2f, radius / 4f)) * scale ); Draw.z(z);*/ Draw.color(color); Fill.circle( - unit.x + ex, - unit.y + ey, - (radius + Mathf.absin(Time.time, 2f, radius / 4f)) * scale + unit.x + ex, + unit.y + ey, + rad ); Draw.color(type.engineColorInner); Fill.circle( - unit.x + ex - Angles.trnsx(rot + rotation, 1f), - unit.y + ey - Angles.trnsy(rot + rotation, 1f), - (radius + Mathf.absin(Time.time, 2f, radius / 4f)) / 2f * scale + unit.x + ex - Angles.trnsx(rot + rotation, rad / 4f), + unit.y + ey - Angles.trnsy(rot + rotation, rad / 4f), + rad / 2f ); } From 44cd05f8b6f65568ba164b651239a4a70d154527 Mon Sep 17 00:00:00 2001 From: Anuken Date: Sun, 9 Feb 2025 00:40:29 -0500 Subject: [PATCH 6/6] Privileged read/write fix --- core/src/mindustry/logic/LExecutor.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/core/src/mindustry/logic/LExecutor.java b/core/src/mindustry/logic/LExecutor.java index eb629ec9e7..1ba96cc93a 100644 --- a/core/src/mindustry/logic/LExecutor.java +++ b/core/src/mindustry/logic/LExecutor.java @@ -62,7 +62,7 @@ public class LExecutor{ //yes, this is a minor memory leak, but it's probably not significant enough to matter protected static IntFloatMap unitTimeouts = new IntFloatMap(); //lookup variable by name, lazy init. - protected ObjectIntMap nameMap; + protected @Nullable ObjectIntMap nameMap; static{ Events.on(ResetEvent.class, e -> unitTimeouts.clear()); @@ -571,7 +571,7 @@ public class LExecutor{ if(from instanceof MemoryBuild mem && (exec.privileged || (from.team == exec.team && !mem.block.privileged))){ output.setnum(address < 0 || address >= mem.memory.length ? 0 : mem.memory[address]); - }else if(from instanceof LogicBuild logic && (exec.privileged || from.team == exec.team) && position.isobj && position.objval instanceof String name){ + }else if(from instanceof LogicBuild logic && (exec.privileged || (from.team == exec.team && !from.block.privileged)) && position.isobj && position.objval instanceof String name){ LVar fromVar = logic.executor.optionalVar(name); if(fromVar != null && !output.constant){ output.objval = fromVar.objval; @@ -601,10 +601,7 @@ public class LExecutor{ if(from instanceof MemoryBuild mem && (exec.privileged || (from.team == exec.team && !mem.block.privileged)) && address >= 0 && address < mem.memory.length){ mem.memory[address] = value.num(); - }else if(from instanceof LogicBuild logic && (exec.privileged || from.team == exec.team) && position.isobj && position.objval instanceof String name){ - //limit write, need target processor set writable. - LVar writable = logic.executor.optionalVar("@writable"); - if(writable == null || !writable.bool()) return; + }else if(from instanceof LogicBuild logic && (exec.privileged || (from.team == exec.team && !from.block.privileged)) && position.isobj && position.objval instanceof String name){ LVar toVar = logic.executor.optionalVar(name); if(toVar != null && !toVar.constant){ toVar.objval = value.objval;