From e16622afccaa34a5e0245ee2563ede9d4fb72aea Mon Sep 17 00:00:00 2001 From: Anuken Date: Sun, 14 Feb 2021 20:44:40 -0500 Subject: [PATCH] Automatic retreat AI for builders/repair units --- core/src/mindustry/ai/types/BuilderAI.java | 22 ++++++++++++++++- core/src/mindustry/ai/types/RepairAI.java | 24 +++++++++++++++++++ .../entities/units/AIController.java | 2 +- 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/core/src/mindustry/ai/types/BuilderAI.java b/core/src/mindustry/ai/types/BuilderAI.java index ce8b5ae92d..8106e6731a 100644 --- a/core/src/mindustry/ai/types/BuilderAI.java +++ b/core/src/mindustry/ai/types/BuilderAI.java @@ -13,9 +13,12 @@ import mindustry.world.blocks.ConstructBlock.*; import static mindustry.Vars.*; public class BuilderAI extends AIController{ - float buildRadius = 1500; + public static float buildRadius = 1500, retreatDst = 110f, fleeRange = 370f, retreatDelay = Time.toSeconds * 2f; + boolean found = false; @Nullable Unit following; + @Nullable Teamc enemy; + float retreatTimer; @Override public void updateMovement(){ @@ -27,6 +30,7 @@ public class BuilderAI extends AIController{ unit.updateBuilding = true; if(following != null){ + retreatTimer = 0f; //try to follow and mimic someone //validate follower @@ -39,9 +43,25 @@ public class BuilderAI extends AIController{ //set to follower's first build plan, whatever that is unit.plans.clear(); unit.plans.addFirst(following.buildPlan()); + }else if(unit.buildPlan() == null){ + //not following anyone or building + if(timer.get(timerTarget4, 40)){ + enemy = target(unit.x, unit.y, fleeRange, true, true); + } + + //fly away from enemy when not doing anything, but only after a delay + if((retreatTimer += Time.delta) >= retreatDelay){ + if(enemy != null){ + var core = unit.closestCore(); + if(!unit.within(core, retreatDst)){ + moveTo(core, retreatDst); + } + } + } } if(unit.buildPlan() != null){ + retreatTimer = 0f; //approach request if building BuildPlan req = unit.buildPlan(); diff --git a/core/src/mindustry/ai/types/RepairAI.java b/core/src/mindustry/ai/types/RepairAI.java index db20cc7d60..24b6ac424b 100644 --- a/core/src/mindustry/ai/types/RepairAI.java +++ b/core/src/mindustry/ai/types/RepairAI.java @@ -1,11 +1,16 @@ package mindustry.ai.types; +import arc.util.*; import mindustry.entities.*; import mindustry.entities.units.*; import mindustry.gen.*; import mindustry.world.blocks.ConstructBlock.*; public class RepairAI extends AIController{ + public static float retreatDst = 160f, fleeRange = 310f, retreatDelay = Time.toSeconds * 3f; + + @Nullable Teamc avoid; + float retreatTimer; @Override protected void updateMovement(){ @@ -29,6 +34,25 @@ public class RepairAI extends AIController{ unit.lookAt(target); } + + //not repairing + if(!(target instanceof Building)){ + if(timer.get(timerTarget4, 40)){ + avoid = target(unit.x, unit.y, fleeRange, true, true); + } + + if((retreatTimer += Time.delta) >= retreatDelay){ + //fly away from enemy when not doing anything + if(avoid != null){ + var core = unit.closestCore(); + if(!unit.within(core, retreatDst)){ + moveTo(core, retreatDst); + } + } + } + }else{ + retreatTimer = 0f; + } } @Override diff --git a/core/src/mindustry/entities/units/AIController.java b/core/src/mindustry/entities/units/AIController.java index b42c5efdb7..71bde595ce 100644 --- a/core/src/mindustry/entities/units/AIController.java +++ b/core/src/mindustry/entities/units/AIController.java @@ -15,7 +15,7 @@ import static mindustry.Vars.*; public class AIController implements UnitController{ protected static final Vec2 vec = new Vec2(); - protected static final int timerTarget = 0, timerTarget2 = 1, timerTarget3 = 2; + protected static final int timerTarget = 0, timerTarget2 = 1, timerTarget3 = 2, timerTarget4 = 2; protected Unit unit; protected Interval timer = new Interval(4);