From 730cb14f6aab42a3831950be70ab3a34b3a679fb Mon Sep 17 00:00:00 2001 From: notrealn <56411504+notrealn@users.noreply.github.com> Date: Tue, 20 Jul 2021 09:00:19 -0400 Subject: [PATCH] Fix turret shooting priority when there are multiple teams (#5299) --- core/src/mindustry/ai/BlockIndexer.java | 23 +++++++++++++++++------ core/src/mindustry/entities/Units.java | 2 +- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/core/src/mindustry/ai/BlockIndexer.java b/core/src/mindustry/ai/BlockIndexer.java index e23d270175..aae2ca6467 100644 --- a/core/src/mindustry/ai/BlockIndexer.java +++ b/core/src/mindustry/ai/BlockIndexer.java @@ -300,19 +300,30 @@ public class BlockIndexer{ breturnArray.size = 0; } - public Building findEnemyTile(@Nullable Team team, float x, float y, float range, Boolf pred){ + public Building findEnemyTile(Team team, float x, float y, float range, Boolf pred){ + Building target = null; + float targetDist = 0; + for(int i = 0; i < activeTeams.size; i++){ Team enemy = activeTeams.items[i]; - if(enemy == team || (team == Team.derelict && !state.rules.coreCapture)) continue; - Building entity = indexer.findTile(enemy, x, y, range, pred, true); - if(entity != null){ - return entity; + Building candidate = indexer.findTile(enemy, x, y, range, pred, true); + if(candidate == null) continue; + + //if a block has the same priority, the closer one should be targeted + float dist = candidate.dst(x, y) - candidate.hitSize() / 2f; + if(target == null || + //if its closer and is at least equal priority + (dist < targetDist && candidate.block.priority.ordinal() >= target.block.priority.ordinal()) || + // block has higher priority (so range doesnt matter) + (candidate.block.priority.ordinal() > target.block.priority.ordinal())){ + target = candidate; + targetDist = dist; } } - return null; + return target; } public Building findTile(Team team, float x, float y, float range, Boolf pred){ diff --git a/core/src/mindustry/entities/Units.java b/core/src/mindustry/entities/Units.java index d0fd94a63c..6fa7889d8e 100644 --- a/core/src/mindustry/entities/Units.java +++ b/core/src/mindustry/entities/Units.java @@ -184,7 +184,7 @@ public class Units{ } } - /** Returns the closest target enemy. First, units are checked, then tile entities. */ + /** Returns the closest target enemy. First, units are checked, then buildings. */ public static Teamc bestTarget(Team team, float x, float y, float range, Boolf unitPred, Boolf tilePred, Sortf sort){ if(team == Team.derelict) return null;