From 276245bf3ce7f39b1a8bcef5f5f7ff591b211654 Mon Sep 17 00:00:00 2001 From: Anuken Date: Sat, 23 Sep 2023 17:10:19 -0400 Subject: [PATCH] Commands are now content --- .../annotations/entity/EntityIO.java | 2 +- core/src/mindustry/ai/UnitCommand.java | 90 ++++++++++--------- core/src/mindustry/ai/UnitStance.java | 36 ++++---- core/src/mindustry/core/ContentLoader.java | 27 ++++++ core/src/mindustry/ctype/ContentType.java | 5 +- core/src/mindustry/io/TypeIO.java | 9 +- core/src/mindustry/mod/ContentParser.java | 4 +- 7 files changed, 107 insertions(+), 66 deletions(-) diff --git a/annotations/src/main/java/mindustry/annotations/entity/EntityIO.java b/annotations/src/main/java/mindustry/annotations/entity/EntityIO.java index a21717e227..4d9ce94869 100644 --- a/annotations/src/main/java/mindustry/annotations/entity/EntityIO.java +++ b/annotations/src/main/java/mindustry/annotations/entity/EntityIO.java @@ -227,7 +227,7 @@ public class EntityIO{ if(BaseProcessor.isPrimitive(type)){ s(type.equals("boolean") ? "bool" : type.charAt(0) + "", field); - }else if(instanceOf(type, "mindustry.ctype.Content")){ + }else if(instanceOf(type, "mindustry.ctype.Content") && !type.equals("mindustry.ai.UnitStance") && !type.equals("mindustry.ai.UnitCommand")){ if(write){ s("s", field + ".id"); }else{ diff --git a/core/src/mindustry/ai/UnitCommand.java b/core/src/mindustry/ai/UnitCommand.java index 507dbfa2f0..cc0d04d116 100644 --- a/core/src/mindustry/ai/UnitCommand.java +++ b/core/src/mindustry/ai/UnitCommand.java @@ -6,54 +6,19 @@ import arc.scene.style.*; import arc.struct.*; import arc.util.*; import mindustry.ai.types.*; +import mindustry.ctype.*; import mindustry.entities.units.*; import mindustry.gen.*; import mindustry.input.*; /** Defines a pattern of behavior that an RTS-controlled unit should follow. Shows up in the command UI. */ -public class UnitCommand{ - /** List of all commands by ID. */ +public class UnitCommand extends MappableContent{ + /** @deprecated now a content type, use the methods in Vars.content instead */ + @Deprecated public static final Seq all = new Seq<>(); - public static final UnitCommand + public static UnitCommand moveCommand, repairCommand, rebuildCommand, assistCommand, mineCommand, boostCommand, loadUnitsCommand, loadBlocksCommand, unloadPayloadCommand; - moveCommand = new UnitCommand("move", "right", Binding.unit_command_move, null){{ - drawTarget = true; - resetTarget = false; - }}, - repairCommand = new UnitCommand("repair", "modeSurvival", Binding.unit_command_repair, u -> new RepairAI()), - rebuildCommand = new UnitCommand("rebuild", "hammer", Binding.unit_command_rebuild, u -> new BuilderAI()), - assistCommand = new UnitCommand("assist", "players", Binding.unit_command_assist, u -> { - var ai = new BuilderAI(); - ai.onlyAssist = true; - return ai; - }), - mineCommand = new UnitCommand("mine", "production", Binding.unit_command_mine, u -> new MinerAI()), - boostCommand = new UnitCommand("boost", "up", Binding.unit_command_boost, u -> new BoostAI()){{ - switchToMove = false; - drawTarget = true; - resetTarget = false; - }}, - loadUnitsCommand = new UnitCommand("loadUnits", "download", Binding.unit_command_load_units, null){{ - switchToMove = false; - drawTarget = true; - resetTarget = false; - }}, - loadBlocksCommand = new UnitCommand("loadBlocks", "down", Binding.unit_command_load_blocks, null){{ - switchToMove = false; - drawTarget = true; - resetTarget = false; - }}, - unloadPayloadCommand = new UnitCommand("unloadPayload", "upload", Binding.unit_command_unload_payload, null){{ - switchToMove = false; - drawTarget = true; - resetTarget = false; - }}; - - /** Unique ID number. */ - public final int id; - /** Named used for tooltip/description. */ - public final String name; /** Name of UI icon (from Icon class). */ public final String icon; /** Controller that this unit will use when this command is used. Return null for "default" behavior. */ @@ -68,11 +33,11 @@ public class UnitCommand{ public @Nullable Binding keybind = null; public UnitCommand(String name, String icon, Func controller){ - this.name = name; + super(name); + this.icon = icon; this.controller = controller == null ? u -> null : controller; - id = all.size; all.add(this); } @@ -93,8 +58,49 @@ public class UnitCommand{ return (char) Iconc.codes.get(icon, Iconc.cancel); } + @Override + public ContentType getContentType(){ + return ContentType.unitCommand; + } + @Override public String toString(){ return "UnitCommand:" + name; } + + public static void loadAll(){ + + moveCommand = new UnitCommand("move", "right", Binding.unit_command_move, null){{ + drawTarget = true; + resetTarget = false; + }}; + repairCommand = new UnitCommand("repair", "modeSurvival", Binding.unit_command_repair, u -> new RepairAI()); + rebuildCommand = new UnitCommand("rebuild", "hammer", Binding.unit_command_rebuild, u -> new BuilderAI()); + assistCommand = new UnitCommand("assist", "players", Binding.unit_command_assist, u -> { + var ai = new BuilderAI(); + ai.onlyAssist = true; + return ai; + }); + mineCommand = new UnitCommand("mine", "production", Binding.unit_command_mine, u -> new MinerAI()); + boostCommand = new UnitCommand("boost", "up", Binding.unit_command_boost, u -> new BoostAI()){{ + switchToMove = false; + drawTarget = true; + resetTarget = false; + }}; + loadUnitsCommand = new UnitCommand("loadUnits", "download", Binding.unit_command_load_units, null){{ + switchToMove = false; + drawTarget = true; + resetTarget = false; + }}; + loadBlocksCommand = new UnitCommand("loadBlocks", "down", Binding.unit_command_load_blocks, null){{ + switchToMove = false; + drawTarget = true; + resetTarget = false; + }}; + unloadPayloadCommand = new UnitCommand("unloadPayload", "upload", Binding.unit_command_unload_payload, null){{ + switchToMove = false; + drawTarget = true; + resetTarget = false; + }}; + } } diff --git a/core/src/mindustry/ai/UnitStance.java b/core/src/mindustry/ai/UnitStance.java index a76668d307..8d4f59bb2e 100644 --- a/core/src/mindustry/ai/UnitStance.java +++ b/core/src/mindustry/ai/UnitStance.java @@ -4,37 +4,27 @@ import arc.*; import arc.scene.style.*; import arc.struct.*; import arc.util.*; +import mindustry.ctype.*; import mindustry.gen.*; import mindustry.input.*; -public class UnitStance{ - /** List of all stances by ID. */ +public class UnitStance extends MappableContent{ + /** @deprecated now a content type, use the methods in Vars.content instead */ + @Deprecated public static final Seq all = new Seq<>(); - public static final UnitStance + public static UnitStance stop, shoot, holdFire, pursueTarget, patrol, ram; - stop = new UnitStance("stop", "cancel", Binding.cancel_orders), //not a real stance, cannot be selected, just cancels ordewrs - shoot = new UnitStance("shoot", "commandAttack", Binding.unit_stance_shoot), - holdFire = new UnitStance("holdfire", "none", Binding.unit_stance_hold_fire), - pursueTarget = new UnitStance("pursuetarget", "right", Binding.unit_stance_pursue_target), - patrol = new UnitStance("patrol", "refresh", Binding.unit_stance_patrol), - ram = new UnitStance("ram", "rightOpen", Binding.unit_stance_ram); - - /** Unique ID number. */ - public final int id; - /** Named used for tooltip/description. */ - public final String name; /** Name of UI icon (from Icon class). */ public final String icon; /** Key to press for this stance. */ public @Nullable Binding keybind = null; public UnitStance(String name, String icon, Binding keybind){ - this.name = name; + super(name); this.icon = icon; this.keybind = keybind; - id = all.size; all.add(this); } @@ -50,8 +40,22 @@ public class UnitStance{ return (char) Iconc.codes.get(icon, Iconc.cancel); } + @Override + public ContentType getContentType(){ + return ContentType.unitStance; + } + @Override public String toString(){ return "UnitStance:" + name; } + + public static void loadAll(){ + stop = new UnitStance("stop", "cancel", Binding.cancel_orders); + shoot = new UnitStance("shoot", "commandAttack", Binding.unit_stance_shoot); + holdFire = new UnitStance("holdfire", "none", Binding.unit_stance_hold_fire); + pursueTarget = new UnitStance("pursuetarget", "right", Binding.unit_stance_pursue_target); + patrol = new UnitStance("patrol", "refresh", Binding.unit_stance_patrol); + ram = new UnitStance("ram", "rightOpen", Binding.unit_stance_ram); + } } diff --git a/core/src/mindustry/core/ContentLoader.java b/core/src/mindustry/core/ContentLoader.java index 0fabcc5ce8..91e9c80824 100644 --- a/core/src/mindustry/core/ContentLoader.java +++ b/core/src/mindustry/core/ContentLoader.java @@ -6,6 +6,7 @@ import arc.func.*; import arc.graphics.*; import arc.struct.*; import arc.util.*; +import mindustry.ai.*; import mindustry.content.*; import mindustry.ctype.*; import mindustry.entities.bullet.*; @@ -40,6 +41,8 @@ public class ContentLoader{ /** Creates all base types. */ public void createBaseContent(){ + UnitCommand.loadAll(); + UnitStance.loadAll(); TeamEntries.load(); Items.load(); StatusEffects.load(); @@ -310,4 +313,28 @@ public class ContentLoader{ public Planet planet(String name){ return getByName(ContentType.planet, name); } + + public Seq unitStances(){ + return getBy(ContentType.unitStance); + } + + public UnitStance unitStance(int id){ + return getByID(ContentType.unitStance, id); + } + + public UnitStance unitStance(String name){ + return getByName(ContentType.unitStance, name); + } + + public Seq unitCommands(){ + return getBy(ContentType.unitCommand); + } + + public UnitCommand unitCommand(int id){ + return getByID(ContentType.unitCommand, id); + } + + public UnitCommand unitCommand(String name){ + return getByName(ContentType.unitCommand, name); + } } diff --git a/core/src/mindustry/ctype/ContentType.java b/core/src/mindustry/ctype/ContentType.java index eb67a52003..136e5257e0 100644 --- a/core/src/mindustry/ctype/ContentType.java +++ b/core/src/mindustry/ctype/ContentType.java @@ -1,6 +1,7 @@ package mindustry.ctype; import arc.util.*; +import mindustry.ai.*; import mindustry.entities.bullet.*; import mindustry.type.*; import mindustry.world.*; @@ -22,7 +23,9 @@ public enum ContentType{ error(null), planet(Planet.class), ammo_UNUSED(null), - team(TeamEntry.class); + team(TeamEntry.class), + unitCommand(UnitCommand.class), + unitStance(UnitStance.class); public static final ContentType[] all = values(); diff --git a/core/src/mindustry/io/TypeIO.java b/core/src/mindustry/io/TypeIO.java index 047f329d82..bbb3b916fc 100644 --- a/core/src/mindustry/io/TypeIO.java +++ b/core/src/mindustry/io/TypeIO.java @@ -203,7 +203,7 @@ public class TypeIO{ for(int i = 0; i < objlen; i++) objs[i] = readObjectBoxed(read, box); yield objs; } - case 23 -> UnitCommand.all.get(read.us()); + case 23 -> content.unitCommand(read.us()); default -> throw new IllegalArgumentException("Unknown object type: " + type); }; } @@ -311,7 +311,7 @@ public class TypeIO{ public static @Nullable UnitCommand readCommand(Reads read){ int val = read.ub(); - return val == 255 || val >= UnitCommand.all.size ? null : UnitCommand.all.get(val); + return val == 255 ? null : content.unitCommand(val); } public static void writeStance(Writes write, @Nullable UnitStance stance){ @@ -321,7 +321,7 @@ public class TypeIO{ public static UnitStance readStance(Reads read){ int val = read.ub(); //never returns null - return val == 255 || val >= UnitStance.all.size ? UnitStance.shoot : UnitStance.all.get(val); + return val == 255 || val >= content.unitStances().size ? UnitStance.shoot : content.unitStance(val); } public static void writeEntity(Writes write, Entityc entity){ @@ -576,7 +576,8 @@ public class TypeIO{ if(type == 6 || type == 7 || type == 8){ byte id = read.b(); - ai.command = id < 0 ? null : UnitCommand.all.get(id); + ai.command = id < 0 ? null : content.unitCommand(id); + if(ai.command == null) ai.command = UnitCommand.moveCommand; } //command queue only in type 7 diff --git a/core/src/mindustry/mod/ContentParser.java b/core/src/mindustry/mod/ContentParser.java index aa18db1675..d02b4e4142 100644 --- a/core/src/mindustry/mod/ContentParser.java +++ b/core/src/mindustry/mod/ContentParser.java @@ -112,7 +112,7 @@ public class ContentParser{ }); put(UnitCommand.class, (type, data) -> { if(data.isString()){ - var cmd = UnitCommand.all.find(u -> u.name.equals(data.asString())); + var cmd = content.unitCommand(data.asString()); if(cmd != null){ return cmd; }else{ @@ -124,7 +124,7 @@ public class ContentParser{ }); put(UnitStance.class, (type, data) -> { if(data.isString()){ - var cmd = UnitStance.all.find(u -> u.name.equals(data.asString())); + var cmd = content.unitStance(data.asString()); if(cmd != null){ return cmd; }else{