Basic AI defending

This commit is contained in:
Anuken
2022-02-18 11:27:20 -05:00
parent 520b60770c
commit 027d037233
7 changed files with 210 additions and 57 deletions

View File

@@ -3,6 +3,7 @@ package mindustry.ai;
import arc.*;
import arc.graphics.g2d.*;
import arc.math.*;
import arc.math.geom.*;
import arc.struct.*;
import arc.util.*;
import mindustry.*;
@@ -12,11 +13,17 @@ import mindustry.game.Teams.*;
import mindustry.gen.*;
import mindustry.graphics.*;
import mindustry.ui.*;
import mindustry.world.*;
import mindustry.world.blocks.defense.turrets.Turret.*;
import mindustry.world.blocks.storage.CoreBlock.*;
import mindustry.world.meta.*;
public class RtsAI{
static final Seq<Building> targets = new Seq<>();
static final Seq<Unit> squad = new Seq<>(false);
static final IntSet used = new IntSet();
static final IntSet assignedTargets = new IntSet();
static final float squadRadius = 120f;
static final int timeUpdate = 0;
static final float minWeight = 0.9f;
@@ -24,73 +31,181 @@ public class RtsAI{
static final BlockFlag[] flags = {BlockFlag.generator, BlockFlag.factory, BlockFlag.core, BlockFlag.battery};
static final ObjectFloatMap<Building> weights = new ObjectFloatMap<>();
static final int minSquadSize = 4;
static boolean debug = true;
//TODO max squad size
static final boolean debug = true;
final Interval timer = new Interval(10);
final TeamData data;
final ObjectSet<Building> damagedSet = new ObjectSet<>();
final Seq<Building> damaged = new Seq<>(false);
//must be static, as this class can get instantiated many times; event listeners are hard to clean up
static{
Events.on(BuildDamageEvent.class, e -> {
if(e.build.team.rules().rtsAi){
var ai = e.build.team.data().rtsAi;
if(ai != null){
ai.damagedSet.add(e.build);
}
}
});
}
public RtsAI(TeamData data){
this.data = data;
timer.reset(0, Mathf.random(60f * 2f));
//TODO remove: debugging!
Events.run(Trigger.draw, () -> {
if(!debug) return;
if(debug){
Events.run(Trigger.draw, () -> {
Draw.draw(Layer.overlayUI, () -> {
Draw.draw(Layer.overlayUI, () -> {
float s = Fonts.outline.getScaleX();
Fonts.outline.getData().setScale(0.5f);
for(var target : targets){
if(weights.containsKey(target)){
float w = weights.get(target, 0f);
Fonts.outline.draw("[sky]" + Strings.fixed(w, 2), target.x, target.y, Align.center);
float s = Fonts.outline.getScaleX();
Fonts.outline.getData().setScale(0.5f);
for(var target : weights){
Fonts.outline.draw("[sky]" + Strings.fixed(target.value, 2), target.key.x, target.key.y, Align.center);
}
}
Fonts.outline.getData().setScale(s);
});
Fonts.outline.getData().setScale(s);
});
});
});
}
}
public void update(){
if(timer.get(timeUpdate, 60f * 2f)){
var build = findAttackPoint();
assignSquads();
}
}
if(build != null){
for(var unit : data.units){
if(unit.isCommandable() && !unit.command().hasCommand()){
unit.command().commandTarget(build);
void assignSquads(){
assignedTargets.clear();
used.clear();
damaged.addAll(damagedSet);
damagedSet.clear();
boolean didDefend = false;
for(var unit : data.units){
if(unit.isCommandable() && !unit.command().hasCommand() && used.add(unit.id)){
squad.clear();
data.tree().intersect(unit.x - squadRadius/2f, unit.y - squadRadius/2f, squadRadius, squadRadius, squad);
//remove overlapping squads
squad.removeAll(u -> (u != unit && used.contains(u.id)) || !u.isCommandable() || u.command().hasCommand());
//mark used so other squads can't steal them
for(var item : squad){
used.add(item.id);
}
//TODO flawed, squads
if(handleSquad(squad, !didDefend)){
didDefend = true;
}
}
}
damaged.clear();
}
boolean handleSquad(Seq<Unit> units, boolean noDefenders){
float health = 0f, dps = 0f;
float ax = 0f, ay = 0f;
for(var unit : units){
ax += unit.x;
ay += unit.y;
health += unit.health;
dps += unit.type.dpsEstimate;
}
ax /= units.size;
ay /= units.size;
if(debug){
Vars.ui.showLabel("Squad: " + units.size, 2f, ax, ay);
}
Building defend = null;
//there is something to defend, see if it's worth the time
if(damaged.size > 0){
//TODO do the weights matter at all?
//for(var build : damaged){
//float w = estimateStats(ax, ay, dps, health);
//weights.put(build, w);
//}
//screw you java
float aax = ax, aay = ay;
Building best = damaged.min(b -> {
//rush to core IMMEDIATELY
if(b instanceof CoreBuild){
return -999999f;
}
return b.dst(aax, aay);
});
//defend when close, or this is the only squad defending
if(best instanceof CoreBuild || (units.size >= minSquadSize && (noDefenders || best.within(ax, ay, 400f)))){
defend = best;
if(debug){
Vars.ui.showLabel("Defend, dst = " + (int)(best.dst(ax, ay)), 8f, best.x, best.y);
}
}
}
//find aggressor, or else, the thing being attacked
Vec2 defendPos = null;
Teamc defendTarget = null;
if(defend != null){
//TODO could be made faster by storing bullet shooter
Unit aggressor = Units.closestEnemy(data.team, defend.x, defend.y, 250f, u -> true);
if(aggressor != null){
defendTarget = aggressor;
}else if(false){ //TODO currently ignored, no use defending against nothing
//should it even go there if there's no aggressor found?
Tile closest = defend.findClosestEdge(units.first(), Tile::solid);
if(closest != null){
defendPos = new Vec2(closest.worldx(), closest.worldy());
}
}
}
boolean anyDefend = defendPos != null || defendTarget != null;
var build = anyDefend ? null : findTarget(ax, ay, units.size, dps, health);
if(build != null || anyDefend){
for(var unit : units){
if(unit.isCommandable() && !unit.command().hasCommand()){
if(defendPos != null){
unit.command().commandPosition(defendPos);
}else{
unit.command().commandTarget(defendTarget == null ? build : defendTarget);
}
}
}
}
return anyDefend;
}
@Nullable Building findAttackPoint(){
float health = 0f, dps = 0f;
int total = 0;
for(var unit : data.units){
if(unit.isCommandable() && !unit.command().hasCommand()){
health += unit.health;
dps += unit.type.dpsEstimate;
total ++;
}
}
@Nullable Building findTarget(float x, float y, int total, float dps, float health){
if(total < minSquadSize) return null;
//flag priority?
//1. generator
//2. factor
//2. factory
//3. core
//TODO split into "squads" that each find the best target for them
if(total < minSquadSize) return null;
targets.clear();
for(var flag : flags){
targets.addAll(Vars.indexer.getEnemy(data.team, flag));
}
targets.removeAll(b -> assignedTargets.contains(b.id));
if(targets.size == 0) return null;
@@ -100,18 +215,21 @@ public class RtsAI{
weights.put(target, estimateStats(target.x, target.y, dps, health));
}
var result = targets.min(b -> {
float w = 1f - weights.get(b, 0f);
//TODO more weighting, e.g. distance
return w;
});
var result = targets.min(
Structs.comps(
//weight is most important?
Structs.comparingFloat(b -> (1f - weights.get(b, 0f)) + b.dst(x, y)/10000f),
//then distance TODO why weight above
Structs.comparingFloat(b -> b.dst2(x, y))
)
);
float weight = weights.get(result, 0f);
if(weight < minWeight && total < Units.getCap(data.team)){
return null;
}
assignedTargets.add(result.id);
return result;
}