DefenderAI that makes octs follow units (#4757)
This commit is contained in:
34
core/src/mindustry/ai/types/DefenderAI.java
Normal file
34
core/src/mindustry/ai/types/DefenderAI.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1370,6 +1370,8 @@ public class UnitTypes implements ContentList{
|
|||||||
}};
|
}};
|
||||||
|
|
||||||
oct = new UnitType("oct"){{
|
oct = new UnitType("oct"){{
|
||||||
|
defaultController = DefenderAI::new;
|
||||||
|
|
||||||
armor = 16f;
|
armor = 16f;
|
||||||
health = 24000;
|
health = 24000;
|
||||||
speed = 0.8f;
|
speed = 0.8f;
|
||||||
|
|||||||
@@ -248,7 +248,7 @@ public class Units{
|
|||||||
return result;
|
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){
|
public static Unit closest(Team team, float x, float y, float range, Boolf<Unit> predicate){
|
||||||
result = null;
|
result = null;
|
||||||
cdist = 0f;
|
cdist = 0f;
|
||||||
@@ -266,6 +266,24 @@ public class Units{
|
|||||||
return result;
|
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.
|
/** Returns the closest ally of this team. Filter by predicate.
|
||||||
* Unlike the closest() function, this only guarantees that unit hitboxes overlap the range. */
|
* 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){
|
public static Unit closestOverlap(Team team, float x, float y, float range, Boolf<Unit> predicate){
|
||||||
|
|||||||
Reference in New Issue
Block a user