From 67d3a9feb00571fd9f29ed43daca0daa4cfd0cb2 Mon Sep 17 00:00:00 2001 From: Anuken Date: Fri, 6 Feb 2026 22:46:17 -0500 Subject: [PATCH] Added field for running logic code upon objective completion --- .../mindustry/editor/MapObjectivesDialog.java | 4 ++++ core/src/mindustry/game/MapObjectives.java | 14 ++++++++++-- core/src/mindustry/logic/LExecutor.java | 22 +++++++++++++++++++ .../mindustry/maps/filters/LogicFilter.java | 19 +--------------- 4 files changed, 39 insertions(+), 20 deletions(-) diff --git a/core/src/mindustry/editor/MapObjectivesDialog.java b/core/src/mindustry/editor/MapObjectivesDialog.java index 408a09a25d..ae82444014 100644 --- a/core/src/mindustry/editor/MapObjectivesDialog.java +++ b/core/src/mindustry/editor/MapObjectivesDialog.java @@ -47,6 +47,10 @@ public class MapObjectivesDialog extends BaseDialog{ if(field != null && field.isAnnotationPresent(Multiline.class)){ cont.area(get.get(), set).height(100f).growX(); + }else if(field != null && field.isAnnotationPresent(LogicCode.class)){ + cont.button(b -> b.image(Icon.pencil).size(iconSmall), () -> { + ui.logic.show(get.get(), null, true, set::get); + }).pad(4f); }else{ cont.field(get.get(), set).growX(); } diff --git a/core/src/mindustry/game/MapObjectives.java b/core/src/mindustry/game/MapObjectives.java index 333adf616b..8c1f602754 100644 --- a/core/src/mindustry/game/MapObjectives.java +++ b/core/src/mindustry/game/MapObjectives.java @@ -179,6 +179,7 @@ public class MapObjectives implements Iterable, Eachable, Eachable, Eachable, Eachable unitTimeouts.clear()); } + public static void runLogicScript(String code){ + runLogicScript(code, 100_000, false); + } + + public static void runLogicScript(String code, int maxInstructions, boolean loop){ + LExecutor executor = new LExecutor(); + executor.privileged = true; + + try{ + //assembler has no variables, all the standard ones are null + executor.load(LAssembler.assemble(code, true)); + }catch(Throwable ignored){ + return; + } + + //executions are limited to prevent a game freeze + for(int i = 1; i < maxInstructions; i++){ + if((!loop && executor.counter.numval >= executor.instructions.length || executor.counter.numval < 0) || executor.yield) break; + executor.runOnce(); + } + } + boolean timeoutDone(Unit unit, float delay){ return Time.time >= unitTimeouts.get(unit.id) + delay; } diff --git a/core/src/mindustry/maps/filters/LogicFilter.java b/core/src/mindustry/maps/filters/LogicFilter.java index 1346d1a52e..a455ccd4fa 100644 --- a/core/src/mindustry/maps/filters/LogicFilter.java +++ b/core/src/mindustry/maps/filters/LogicFilter.java @@ -1,7 +1,6 @@ package mindustry.maps.filters; import arc.scene.ui.layout.*; -import mindustry.*; import mindustry.gen.*; import mindustry.logic.*; import mindustry.maps.filters.FilterOption.*; @@ -38,26 +37,10 @@ public class LogicFilter extends GenerateFilter{ @Override public void apply(Tiles tiles, GenerateInput in){ - LExecutor executor = new LExecutor(); - executor.privileged = true; - - try{ - //assembler has no variables, all the standard ones are null - executor.load(LAssembler.assemble(code, true)); - }catch(Throwable ignored){ - //if loading code - return; - } - //this updates map width/height global variables logicVars.update(); - //NOTE: all tile operations will call setNet for tiles, but that should have no overhead during world loading - //executions are limited to prevent infinite generation - for(int i = 1; i < maxInstructionsExecution; i++){ - if(!loop && (executor.counter.numval >= executor.instructions.length || executor.counter.numval < 0)) break; - executor.runOnce(); - } + LExecutor.runLogicScript(code, maxInstructionsExecution, loop); } @Override