From efbc87196723007be1b823fa0e32dc9c915d0e88 Mon Sep 17 00:00:00 2001 From: Anuken Date: Mon, 5 Oct 2020 17:09:27 -0400 Subject: [PATCH] Pathfind command --- core/src/mindustry/ai/types/GroundAI.java | 16 ++-------- core/src/mindustry/ai/types/LogicAI.java | 32 +++++++++++++++++-- core/src/mindustry/ai/types/SuicideAI.java | 4 +-- .../entities/units/AIController.java | 14 ++++++++ core/src/mindustry/logic/LAssembler.java | 2 ++ core/src/mindustry/logic/LExecutor.java | 7 +++- core/src/mindustry/logic/LUnitControl.java | 1 + .../world/blocks/logic/LogicBlock.java | 2 +- 8 files changed, 58 insertions(+), 20 deletions(-) diff --git a/core/src/mindustry/ai/types/GroundAI.java b/core/src/mindustry/ai/types/GroundAI.java index bd6b13a25c..5eda85c0fd 100644 --- a/core/src/mindustry/ai/types/GroundAI.java +++ b/core/src/mindustry/ai/types/GroundAI.java @@ -34,14 +34,14 @@ public class GroundAI extends AIController{ if(spawner != null && unit.within(spawner, state.rules.dropZoneRadius + 120f)) move = false; } - if(move) moveTo(Pathfinder.fieldCore); + if(move) pathfind(Pathfinder.fieldCore); } if(command() == UnitCommand.rally){ Teamc target = targetFlag(unit.x, unit.y, BlockFlag.rally, false); if(target != null && !unit.within(target, 70f)){ - moveTo(Pathfinder.fieldRally); + pathfind(Pathfinder.fieldRally); } } @@ -72,16 +72,4 @@ public class GroundAI extends AIController{ } }*/ } - - protected void moveTo(int pathTarget){ - int costType = unit.pathType(); - - Tile tile = unit.tileOn(); - if(tile == null) return; - Tile targetTile = pathfinder.getTargetTile(tile, pathfinder.getField(unit.team, costType, pathTarget)); - - if(tile == targetTile || (costType == Pathfinder.costWater && !targetTile.floor().isLiquid)) return; - - unit.moveAt(vec.trns(unit.angleTo(targetTile), unit.type().speed)); - } } diff --git a/core/src/mindustry/ai/types/LogicAI.java b/core/src/mindustry/ai/types/LogicAI.java index ab902b3e50..47e1510a2e 100644 --- a/core/src/mindustry/ai/types/LogicAI.java +++ b/core/src/mindustry/ai/types/LogicAI.java @@ -2,20 +2,26 @@ package mindustry.ai.types; import arc.struct.*; import arc.util.*; +import mindustry.ai.*; import mindustry.entities.units.*; import mindustry.gen.*; import mindustry.logic.LExecutor.*; import mindustry.logic.*; +import mindustry.world.*; +import mindustry.world.meta.*; + +import static mindustry.Vars.*; public class LogicAI extends AIController{ /** Minimum delay between item transfers. */ public static final float transferDelay = 60f * 2f; /** Time after which the unit resets its controlled and reverts to a normal unit. */ - public static final float logicControlTimeout = 15f * 60f; + public static final float logicControlTimeout = 10f * 60f; public LUnitControl control = LUnitControl.stop; public float moveX, moveY, moveRad; public float itemTimer, controlTimer = logicControlTimeout, targetTimer; + public Building controller; //type of aiming to use public LUnitControl aimControl = LUnitControl.stop; @@ -43,7 +49,7 @@ public class LogicAI extends AIController{ } //timeout when not controlled by logic for a while - if(controlTimer > 0){ + if(controlTimer > 0 && controller != null && controller.isValid()){ controlTimer -= Time.delta; }else{ unit.resetController(); @@ -57,6 +63,28 @@ public class LogicAI extends AIController{ case approach -> { moveTo(Tmp.v1.set(moveX, moveY), moveRad, 10f); } + case pathfind -> { + Building core = unit.closestEnemyCore(); + + if((core == null || !unit.within(core, unit.range() * 0.5f)) && command() == UnitCommand.attack){ + boolean move = true; + + if(state.rules.waves && unit.team == state.rules.defaultTeam){ + Tile spawner = getClosestSpawner(); + if(spawner != null && unit.within(spawner, state.rules.dropZoneRadius + 120f)) move = false; + } + + if(move) pathfind(Pathfinder.fieldCore); + } + + if(command() == UnitCommand.rally){ + Teamc target = targetFlag(unit.x, unit.y, BlockFlag.rally, false); + + if(target != null && !unit.within(target, 70f)){ + pathfind(Pathfinder.fieldRally); + } + } + } } //look where moving if there's nothing to aim at diff --git a/core/src/mindustry/ai/types/SuicideAI.java b/core/src/mindustry/ai/types/SuicideAI.java index 93ab98a7c0..7278062605 100644 --- a/core/src/mindustry/ai/types/SuicideAI.java +++ b/core/src/mindustry/ai/types/SuicideAI.java @@ -67,10 +67,10 @@ public class SuicideAI extends GroundAI{ Teamc target = targetFlag(unit.x, unit.y, BlockFlag.rally, false); if(target != null && !unit.within(target, 70f)){ - moveTo(Pathfinder.fieldRally); + pathfind(Pathfinder.fieldRally); } }else if(command() == UnitCommand.attack && core != null){ - moveTo(Pathfinder.fieldCore); + pathfind(Pathfinder.fieldCore); } if(unit.moving()) unit.lookAt(unit.vel().angle()); diff --git a/core/src/mindustry/entities/units/AIController.java b/core/src/mindustry/entities/units/AIController.java index 52b7dc3b2a..7e84a163d6 100644 --- a/core/src/mindustry/entities/units/AIController.java +++ b/core/src/mindustry/entities/units/AIController.java @@ -4,6 +4,7 @@ import arc.math.*; import arc.math.geom.*; import arc.util.*; import mindustry.*; +import mindustry.ai.*; import mindustry.entities.*; import mindustry.gen.*; import mindustry.type.*; @@ -67,6 +68,19 @@ public class AIController implements UnitController{ return Units.invalidateTarget(target, unit.team, unit.x, unit.y); } + + protected void pathfind(int pathTarget){ + int costType = unit.pathType(); + + Tile tile = unit.tileOn(); + if(tile == null) return; + Tile targetTile = pathfinder.getTargetTile(tile, pathfinder.getField(unit.team, costType, pathTarget)); + + if(tile == targetTile || (costType == Pathfinder.costWater && !targetTile.floor().isLiquid)) return; + + unit.moveAt(vec.trns(unit.angleTo(targetTile), unit.type().speed)); + } + protected void updateWeapons(){ if(targets.length != unit.mounts.length) targets = new Teamc[unit.mounts.length]; diff --git a/core/src/mindustry/logic/LAssembler.java b/core/src/mindustry/logic/LAssembler.java index 016724f9ba..bd3690110b 100644 --- a/core/src/mindustry/logic/LAssembler.java +++ b/core/src/mindustry/logic/LAssembler.java @@ -27,6 +27,8 @@ public class LAssembler{ putConst("@time", 0); //currently controlled unit putConst("@unit", null); + //reference to self + putConst("@this", null); //add default constants putConst("false", 0); diff --git a/core/src/mindustry/logic/LExecutor.java b/core/src/mindustry/logic/LExecutor.java index 9188adba2e..18581018c6 100644 --- a/core/src/mindustry/logic/LExecutor.java +++ b/core/src/mindustry/logic/LExecutor.java @@ -27,7 +27,8 @@ public class LExecutor{ public static final int varCounter = 0, varTime = 1, - varUnit = 2; + varUnit = 2, + varThis = 3; public static final int maxGraphicsBuffer = 256, @@ -215,6 +216,7 @@ public class LExecutor{ if(unitObj instanceof Unit unit && exec.obj(varUnit) == unit && unit.team == exec.team && !unit.isPlayer() && !(unit.controller() instanceof FormationAI)){ if(!(unit.controller() instanceof LogicAI)){ unit.controller(new LogicAI()); + ((LogicAI)unit.controller()).controller = exec.building(varThis); //clear old state if(unit instanceof Minerc miner){ @@ -250,6 +252,9 @@ public class LExecutor{ ai.moveRad = exec.numf(p3); } } + case pathfind -> { + ai.control = type; + } case target -> { ai.posTarget.set(exec.numf(p1), exec.numf(p2)); ai.aimControl = type; diff --git a/core/src/mindustry/logic/LUnitControl.java b/core/src/mindustry/logic/LUnitControl.java index 6c11b21d5e..b943e638d5 100644 --- a/core/src/mindustry/logic/LUnitControl.java +++ b/core/src/mindustry/logic/LUnitControl.java @@ -4,6 +4,7 @@ public enum LUnitControl{ stop, move("x", "y"), approach("x", "y", "radius"), + pathfind(), target("x", "y", "shoot"), targetp("unit", "shoot"), itemDrop("to", "amount"), diff --git a/core/src/mindustry/world/blocks/logic/LogicBlock.java b/core/src/mindustry/world/blocks/logic/LogicBlock.java index 5f6fabc983..8c14ec987f 100644 --- a/core/src/mindustry/world/blocks/logic/LogicBlock.java +++ b/core/src/mindustry/world/blocks/logic/LogicBlock.java @@ -304,7 +304,7 @@ public class LogicBlock extends Block{ assemble.get(asm); } - asm.putConst("@this", this); + asm.getVar("@this").value = this; asm.putConst("@thisx", x); asm.putConst("@thisy", y);