Limit formation speed

This commit is contained in:
Anuken 2023-09-24 08:11:37 -04:00
parent 7d0bb3f1c8
commit 235c0222bf
4 changed files with 20 additions and 6 deletions

View File

@ -16,6 +16,7 @@ import mindustry.gen.*;
public class UnitGroup{ public class UnitGroup{
public Seq<Unit> units = new Seq<>(); public Seq<Unit> units = new Seq<>();
public float[] positions; public float[] positions;
public float minSpeed = 999999f;
public volatile boolean valid; public volatile boolean valid;
public void calculateFormation(Vec2 dest, int collisionLayer){ public void calculateFormation(Vec2 dest, int collisionLayer){
@ -35,6 +36,7 @@ public class UnitGroup{
positions[i * 2] = unit.x - cx; positions[i * 2] = unit.x - cx;
positions[i * 2 + 1] = unit.y - cy; positions[i * 2 + 1] = unit.y - cy;
unit.command().groupIndex = i; unit.command().groupIndex = i;
minSpeed = Math.min(unit.speed(), minSpeed);
} }
//run on new thread to prevent stutter //run on new thread to prevent stutter

View File

@ -297,6 +297,11 @@ public class CommandAI extends AIController{
} }
} }
@Override
public float prefSpeed(){
return group == null ? super.prefSpeed() : group.minSpeed;
}
@Override @Override
public boolean shouldFire(){ public boolean shouldFire(){
return stance != UnitStance.holdFire; return stance != UnitStance.holdFire;

View File

@ -124,7 +124,7 @@ public class AIController implements UnitController{
if(tile == targetTile || (costType == Pathfinder.costNaval && !targetTile.floor().isLiquid)) return; if(tile == targetTile || (costType == Pathfinder.costNaval && !targetTile.floor().isLiquid)) return;
unit.movePref(vec.trns(unit.angleTo(targetTile.worldx(), targetTile.worldy()), unit.speed())); unit.movePref(vec.trns(unit.angleTo(targetTile.worldx(), targetTile.worldy()), prefSpeed()));
} }
public void updateWeapons(){ public void updateWeapons(){
@ -270,13 +270,13 @@ public class AIController implements UnitController{
vec.setAngle(Angles.moveToward(unit.vel().angle(), vec.angle(), 6f)); vec.setAngle(Angles.moveToward(unit.vel().angle(), vec.angle(), 6f));
} }
vec.setLength(unit.speed()); vec.setLength(prefSpeed());
unit.moveAt(vec); unit.moveAt(vec);
} }
public void circle(Position target, float circleLength){ public void circle(Position target, float circleLength){
circle(target, circleLength, unit.speed()); circle(target, circleLength, prefSpeed());
} }
public void circle(Position target, float circleLength, float speed){ public void circle(Position target, float circleLength, float speed){
@ -308,15 +308,17 @@ public class AIController implements UnitController{
public void moveTo(Position target, float circleLength, float smooth, boolean keepDistance, @Nullable Vec2 offset, boolean arrive){ public void moveTo(Position target, float circleLength, float smooth, boolean keepDistance, @Nullable Vec2 offset, boolean arrive){
if(target == null) return; if(target == null) return;
float speed = prefSpeed();
vec.set(target).sub(unit); vec.set(target).sub(unit);
float length = circleLength <= 0.001f ? 1f : Mathf.clamp((unit.dst(target) - circleLength) / smooth, -1f, 1f); float length = circleLength <= 0.001f ? 1f : Mathf.clamp((unit.dst(target) - circleLength) / smooth, -1f, 1f);
vec.setLength(unit.speed() * length); vec.setLength(speed * length);
if(arrive){ if(arrive){
Tmp.v3.set(-unit.vel.x / unit.type.accel * 2f, -unit.vel.y / unit.type.accel * 2f).add((target.getX() - unit.x), (target.getY() - unit.y)); Tmp.v3.set(-unit.vel.x / unit.type.accel * 2f, -unit.vel.y / unit.type.accel * 2f).add((target.getX() - unit.x), (target.getY() - unit.y));
vec.add(Tmp.v3).limit(unit.speed() * length); vec.add(Tmp.v3).limit(speed * length);
} }
if(length < -0.5f){ if(length < -0.5f){
@ -331,7 +333,7 @@ public class AIController implements UnitController{
if(offset != null){ if(offset != null){
vec.add(offset); vec.add(offset);
vec.setLength(unit.speed() * length); vec.setLength(speed * length);
} }
//do not move when infinite vectors are used or if its zero. //do not move when infinite vectors are used or if its zero.
@ -348,6 +350,10 @@ public class AIController implements UnitController{
} }
} }
public float prefSpeed(){
return unit.speed();
}
@Override @Override
public void unit(Unit unit){ public void unit(Unit unit){
if(this.unit == unit) return; if(this.unit == unit) return;

View File

@ -277,6 +277,7 @@ public abstract class InputHandler implements InputProcessor, GestureListener{
} }
unit.lastCommanded = player.coloredName(); unit.lastCommanded = player.coloredName();
ai.group = null;
//remove when other player command //remove when other player command
if(!headless && player != Vars.player){ if(!headless && player != Vars.player){