From fb4787ba5279a1ce6ba95710def2a02b2b7d0b84 Mon Sep 17 00:00:00 2001 From: Anuken Date: Fri, 11 Aug 2023 12:16:40 -0400 Subject: [PATCH] Closes Anuken/Mindustry-Suggestions/issues/4608 --- core/assets/bundles/bundle.properties | 3 ++- .../mindustry/ctype/UnlockableContent.java | 4 ++++ core/src/mindustry/logic/LAccess.java | 1 + core/src/mindustry/logic/LExecutor.java | 1 - core/src/mindustry/type/Item.java | 5 +++-- core/src/mindustry/type/Liquid.java | 5 +++-- core/src/mindustry/type/UnitType.java | 21 ++++++++++++++++++- core/src/mindustry/world/Block.java | 1 + 8 files changed, 34 insertions(+), 7 deletions(-) diff --git a/core/assets/bundles/bundle.properties b/core/assets/bundles/bundle.properties index 657833b630..5803c8de6e 100644 --- a/core/assets/bundles/bundle.properties +++ b/core/assets/bundles/bundle.properties @@ -2237,7 +2237,7 @@ lst.operation = Perform an operation on 1-2 variables. lst.end = Jump to the top of the instruction stack. lst.wait = Wait a certain number of seconds. lst.stop = Halt execution of this processor. -lst.lookup = Look up an item/liquid/unit/block type by ID.\nTotal counts of each type can be accessed with:\n[accent]@unitCount[] / [accent]@itemCount[] / [accent]@liquidCount[] / [accent]@blockCount[] +lst.lookup = Look up an item/liquid/unit/block type by ID.\nTotal counts of each type can be accessed with:\n[accent]@unitCount[] / [accent]@itemCount[] / [accent]@liquidCount[] / [accent]@blockCount[]\nFor the inverse operation, sense [accent]@id[] of the object. lst.jump = Conditionally jump to another statement. lst.unitbind = Bind to the next unit of a type, and store it in [accent]@unit[]. lst.unitcontrol = Control the currently bound unit. @@ -2275,6 +2275,7 @@ laccess.dead = Whether a unit/building is dead or no longer valid. laccess.controlled = Returns:\n[accent]@ctrlProcessor[] if unit controller is processor\n[accent]@ctrlPlayer[] if unit/building controller is player\n[accent]@ctrlCommand[] if unit controller is a player command\nOtherwise, 0. laccess.progress = Action progress, 0 to 1.\nReturns production, turret reload or construction progress. laccess.speed = Top speed of a unit, in tiles/sec. +laccess.id = ID of a unit/block/item/liquid.\nThis is the inverse of the lookup operation. lcategory.unknown = Unknown lcategory.unknown.description = Uncategorized instructions. diff --git a/core/src/mindustry/ctype/UnlockableContent.java b/core/src/mindustry/ctype/UnlockableContent.java index 844f28aefd..bab283dc93 100644 --- a/core/src/mindustry/ctype/UnlockableContent.java +++ b/core/src/mindustry/ctype/UnlockableContent.java @@ -68,6 +68,10 @@ public abstract class UnlockableContent extends MappableContent{ uiIcon = Core.atlas.find(getContentType().name() + "-" + name + "-ui", fullIcon); } + public int getLogicId(){ + return logicVars.lookupLogicId(this); + } + public String displayDescription(){ return minfo.mod == null ? description : description + "\n" + Core.bundle.format("mod.display", minfo.mod.meta.displayName); } diff --git a/core/src/mindustry/logic/LAccess.java b/core/src/mindustry/logic/LAccess.java index 55adfbd405..c5aa8478a9 100644 --- a/core/src/mindustry/logic/LAccess.java +++ b/core/src/mindustry/logic/LAccess.java @@ -46,6 +46,7 @@ public enum LAccess{ name, payloadCount, payloadType, + id, //values with parameters are considered controllable enabled("to"), //"to" is standard for single parameter access diff --git a/core/src/mindustry/logic/LExecutor.java b/core/src/mindustry/logic/LExecutor.java index 69ae7d7b6f..bd4f5cb0f4 100644 --- a/core/src/mindustry/logic/LExecutor.java +++ b/core/src/mindustry/logic/LExecutor.java @@ -1143,7 +1143,6 @@ public class LExecutor{ } } - //TODO inverse lookup public static class LookupI implements LInstruction{ public int dest; public int from; diff --git a/core/src/mindustry/type/Item.java b/core/src/mindustry/type/Item.java index 36bd65ef59..f451b5f1a3 100644 --- a/core/src/mindustry/type/Item.java +++ b/core/src/mindustry/type/Item.java @@ -153,8 +153,9 @@ public class Item extends UnlockableContent implements Senseable{ @Override public double sense(LAccess sensor){ - if(sensor == LAccess.color) return color.toFloatBits(); - return 0; + if(sensor == LAccess.color) return color.toDoubleBits(); + if(sensor == LAccess.id) return getLogicId(); + return Float.NaN; } @Override diff --git a/core/src/mindustry/type/Liquid.java b/core/src/mindustry/type/Liquid.java index 4a578f8382..a524970fcd 100644 --- a/core/src/mindustry/type/Liquid.java +++ b/core/src/mindustry/type/Liquid.java @@ -171,8 +171,9 @@ public class Liquid extends UnlockableContent implements Senseable{ @Override public double sense(LAccess sensor){ - if(sensor == LAccess.color) return color.toFloatBits(); - return 0; + if(sensor == LAccess.color) return color.toDoubleBits(); + if(sensor == LAccess.id) return getLogicId(); + return Double.NaN; } @Override diff --git a/core/src/mindustry/type/UnitType.java b/core/src/mindustry/type/UnitType.java index e03fee7f4f..d4a708d4cf 100644 --- a/core/src/mindustry/type/UnitType.java +++ b/core/src/mindustry/type/UnitType.java @@ -28,6 +28,7 @@ import mindustry.game.*; import mindustry.gen.*; import mindustry.graphics.*; import mindustry.graphics.MultiPacker.*; +import mindustry.logic.*; import mindustry.type.ammo.*; import mindustry.ui.*; import mindustry.world.*; @@ -40,7 +41,7 @@ import mindustry.world.meta.*; import static arc.graphics.g2d.Draw.*; import static mindustry.Vars.*; -public class UnitType extends UnlockableContent{ +public class UnitType extends UnlockableContent implements Senseable{ public static final float shadowTX = -12, shadowTY = -13; private static final Vec2 legOffset = new Vec2(); @@ -1124,6 +1125,24 @@ public class UnitType extends UnlockableContent{ return super.researchRequirements(); } + @Override + public double sense(LAccess sensor){ + return switch(sensor){ + case health, maxHealth -> health; + case size -> hitSize / tilesize; + case itemCapacity -> itemCapacity; + case speed -> speed * 60f / tilesize; + case id -> getLogicId(); + default -> Double.NaN; + }; + } + + @Override + public Object senseObject(LAccess sensor){ + if(sensor == LAccess.name) return name; + return noSensed; + } + @Override public ContentType getContentType(){ return ContentType.unit; diff --git a/core/src/mindustry/world/Block.java b/core/src/mindustry/world/Block.java index fb151dfde5..c1a87f510c 100644 --- a/core/src/mindustry/world/Block.java +++ b/core/src/mindustry/world/Block.java @@ -1374,6 +1374,7 @@ public class Block extends UnlockableContent implements Senseable{ case itemCapacity -> itemCapacity; case liquidCapacity -> liquidCapacity; case powerCapacity -> consPower != null && consPower.buffered ? consPower.capacity : 0f; + case id -> getLogicId(); default -> Double.NaN; }; }