Automatic retreat AI for builders/repair units

This commit is contained in:
Anuken
2021-02-14 20:44:40 -05:00
parent f2468f0b3d
commit e16622afcc
3 changed files with 46 additions and 2 deletions

View File

@@ -13,9 +13,12 @@ import mindustry.world.blocks.ConstructBlock.*;
import static mindustry.Vars.*; import static mindustry.Vars.*;
public class BuilderAI extends AIController{ public class BuilderAI extends AIController{
float buildRadius = 1500; public static float buildRadius = 1500, retreatDst = 110f, fleeRange = 370f, retreatDelay = Time.toSeconds * 2f;
boolean found = false; boolean found = false;
@Nullable Unit following; @Nullable Unit following;
@Nullable Teamc enemy;
float retreatTimer;
@Override @Override
public void updateMovement(){ public void updateMovement(){
@@ -27,6 +30,7 @@ public class BuilderAI extends AIController{
unit.updateBuilding = true; unit.updateBuilding = true;
if(following != null){ if(following != null){
retreatTimer = 0f;
//try to follow and mimic someone //try to follow and mimic someone
//validate follower //validate follower
@@ -39,9 +43,25 @@ public class BuilderAI extends AIController{
//set to follower's first build plan, whatever that is //set to follower's first build plan, whatever that is
unit.plans.clear(); unit.plans.clear();
unit.plans.addFirst(following.buildPlan()); 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){ if(unit.buildPlan() != null){
retreatTimer = 0f;
//approach request if building //approach request if building
BuildPlan req = unit.buildPlan(); BuildPlan req = unit.buildPlan();

View File

@@ -1,11 +1,16 @@
package mindustry.ai.types; package mindustry.ai.types;
import arc.util.*;
import mindustry.entities.*; import mindustry.entities.*;
import mindustry.entities.units.*; import mindustry.entities.units.*;
import mindustry.gen.*; import mindustry.gen.*;
import mindustry.world.blocks.ConstructBlock.*; import mindustry.world.blocks.ConstructBlock.*;
public class RepairAI extends AIController{ public class RepairAI extends AIController{
public static float retreatDst = 160f, fleeRange = 310f, retreatDelay = Time.toSeconds * 3f;
@Nullable Teamc avoid;
float retreatTimer;
@Override @Override
protected void updateMovement(){ protected void updateMovement(){
@@ -29,6 +34,25 @@ public class RepairAI extends AIController{
unit.lookAt(target); 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 @Override

View File

@@ -15,7 +15,7 @@ import static mindustry.Vars.*;
public class AIController implements UnitController{ public class AIController implements UnitController{
protected static final Vec2 vec = new Vec2(); 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 Unit unit;
protected Interval timer = new Interval(4); protected Interval timer = new Interval(4);