DefenderAI that makes octs follow units (#4757)

This commit is contained in:
MEEP of Faith
2021-03-05 07:21:26 -08:00
committed by GitHub
parent 2bb303e709
commit 0c28bb7dcf
3 changed files with 55 additions and 1 deletions

View File

@@ -0,0 +1,34 @@
package mindustry.ai.types;
import arc.math.*;
import mindustry.entities.*;
import mindustry.entities.Units.*;
import mindustry.entities.units.*;
import mindustry.gen.*;
import mindustry.world.meta.*;
public class DefenderAI extends AIController{
@Override
public void updateMovement(){
if(target != null){
moveTo(target, unit.range(), 5f);
unit.lookAt(target);
}else{
Teamc block = targetFlag(unit.x, unit.y, BlockFlag.rally, false);
if(block == null) block = unit.closestCore();
moveTo(block, 60f);
}
}
@Override
protected void updateTargeting(){
if(retarget()) target = findTarget(unit.x, unit.y, 0f, true, true);
}
@Override
protected Teamc findTarget(float x, float y, float range, boolean air, boolean ground){
//Sort by max health and closer target.
return Units.closest(unit.team, x, y, u -> !u.dead() && u.type != unit.type, (u, tx, ty) -> -u.maxHealth + Mathf.dst2(u.x, u.y, tx, ty) / 800f);
}
}

View File

@@ -1370,6 +1370,8 @@ public class UnitTypes implements ContentList{
}};
oct = new UnitType("oct"){{
defaultController = DefenderAI::new;
armor = 16f;
health = 24000;
speed = 0.8f;

View File

@@ -248,7 +248,7 @@ public class Units{
return result;
}
/** Returns the closest ally of this team. Filter by predicate. */
/** Returns the closest ally of this team in a range. Filter by predicate. */
public static Unit closest(Team team, float x, float y, float range, Boolf<Unit> predicate){
result = null;
cdist = 0f;
@@ -266,6 +266,24 @@ public class Units{
return result;
}
/** Returns the closest ally of this team using a custom comparison function. Filter by predicate. */
public static Unit closest(Team team, float x, float y, Boolf<Unit> predicate, Sortf sort){
result = null;
cdist = 0f;
for(Unit e : Groups.unit){
if(!predicate.get(e) || e.team() != team) continue;
float cost = sort.cost(e, x, y);
if(result == null || cost < cdist){
result = e;
cdist = cost;
}
}
return result;
}
/** Returns the closest ally of this team. Filter by predicate.
* Unlike the closest() function, this only guarantees that unit hitboxes overlap the range. */
public static Unit closestOverlap(Team team, float x, float y, float range, Boolf<Unit> predicate){