diff --git a/core/src/mindustry/ai/UnitGroup.java b/core/src/mindustry/ai/UnitGroup.java index 7ccddcad50..dbb55312f3 100644 --- a/core/src/mindustry/ai/UnitGroup.java +++ b/core/src/mindustry/ai/UnitGroup.java @@ -19,7 +19,22 @@ public class UnitGroup{ public int collisionLayer; public volatile float[] positions, originalPositions; public volatile boolean valid; + public long lastSpeedUpdate = -1; public float minSpeed = 999999f; + + public void updateMinSpeed(){ + if(lastSpeedUpdate == Vars.state.updateId) return; + + lastSpeedUpdate = Vars.state.updateId; + + for(Unit unit : units){ + //don't factor in the floor speed multiplier + Floor on = unit.isFlying() ? Blocks.air.asFloor() : unit.floorOn(); + minSpeed = Math.min(unit.speed() / on.speedMultiplier, minSpeed); + } + + if(Float.isInfinite(minSpeed) || Float.isNaN(minSpeed)) minSpeed = 999999f; + } public void calculateFormation(Vec2 dest, int collisionLayer){ this.collisionLayer = collisionLayer; @@ -33,19 +48,16 @@ public class UnitGroup{ cy /= units.size; positions = new float[units.size * 2]; + //all positions are relative to the center for(int i = 0; i < units.size; i ++){ Unit unit = units.get(i); positions[i * 2] = unit.x - cx; positions[i * 2 + 1] = unit.y - cy; unit.command().groupIndex = i; - - //don't factor in the floor speed multiplier - Floor on = unit.isFlying() ? Blocks.air.asFloor() : unit.floorOn(); - minSpeed = Math.min(unit.speed() / on.speedMultiplier, minSpeed); } - if(Float.isInfinite(minSpeed) || Float.isNaN(minSpeed)) minSpeed = 999999f; + updateMinSpeed(); //run on new thread to prevent stutter Vars.mainExecutor.submit(() -> { diff --git a/core/src/mindustry/ai/types/CommandAI.java b/core/src/mindustry/ai/types/CommandAI.java index bedc78fc14..692aff98c8 100644 --- a/core/src/mindustry/ai/types/CommandAI.java +++ b/core/src/mindustry/ai/types/CommandAI.java @@ -148,6 +148,10 @@ public class CommandAI extends AIController{ } } + if(group != null){ + group.updateMinSpeed(); + } + if(!net.client() && command == UnitCommand.enterPayloadCommand && unit.buildOn() != null && (targetPos == null || (world.buildWorld(targetPos.x, targetPos.y) != null && world.buildWorld(targetPos.x, targetPos.y) == unit.buildOn()))){ var build = unit.buildOn(); tmpPayload.unit = unit;