Commander unit component

This commit is contained in:
Anuken
2020-05-02 21:34:37 -04:00
parent dce8b8faa1
commit cb6920b8be
12 changed files with 207 additions and 59 deletions

View File

@@ -0,0 +1,51 @@
package mindustry.ai.formations;
import arc.math.*;
import arc.math.geom.*;
import arc.struct.*;
public class DistanceAssignmentStrategy implements SlotAssignmentStrategy{
private final Vec3 vec = new Vec3();
private final FormationPattern form;
public DistanceAssignmentStrategy(FormationPattern form){
this.form = form;
}
@Override
public void updateSlotAssignments(Array<SlotAssignment> assignments){
IntArray slots = IntArray.range(0, assignments.size);
for(SlotAssignment slot : assignments){
int mindex = 0;
float mcost = Float.MAX_VALUE;
for(int i = 0; i < slots.size; i++){
float cost = cost(slot.member, slots.get(i));
if(cost < mcost){
mcost = cost;
mindex = i;
}
}
slot.slotNumber = slots.get(mindex);
slots.removeIndex(mindex);
}
}
@Override
public int calculateNumberOfSlots(Array<SlotAssignment> assignments){
return assignments.size;
}
@Override
public void removeSlotAssignment(Array<SlotAssignment> assignments, int index){
assignments.remove(index);
}
float cost(FormationMember member, int slot){
form.calculateSlotLocation(vec, slot);
return Mathf.dst2(member.formationPos().x, member.formationPos().y, vec.x, vec.y);
}
}

View File

@@ -19,7 +19,7 @@ import arc.struct.*;
public class Formation{
/** A list of slots assignments. */
Array<SlotAssignment> slotAssignments;
public Array<SlotAssignment> slotAssignments;
/** The anchor point of this formation. */
public Vec3 anchor;
@@ -81,6 +81,8 @@ public class Formation{
/** Updates the assignment of members to slots */
public void updateSlotAssignments(){
pattern.slots = slotAssignments.size;
// Apply the strategy to update slot assignments
slotAssignmentStrategy.updateSlotAssignments(slotAssignments);
@@ -114,6 +116,21 @@ public class Formation{
return false;
}
/** Much more efficient than adding a single member.
* @return number of members added. */
public int addMembers(Iterable<? extends FormationMember> members){
int added = 0;
for(FormationMember member : members){
if(pattern.supportsSlots(slotAssignments.size + 1)){
slotAssignments.add(new SlotAssignment(member, slotAssignments.size));
added ++;
}
}
updateSlotAssignments();
return added;
}
/**
* Adds a new member to the first available slot and updates slot assignments if the number of member is supported by the
* current pattern.
@@ -121,13 +138,11 @@ public class Formation{
* @return {@code false} if no more slots are available; {@code true} otherwise.
*/
public boolean addMember(FormationMember member){
// Find out how many slots we have occupied
int occupiedSlots = slotAssignments.size;
// Check if the pattern supports one more slot
if(pattern.supportsSlots(occupiedSlots + 1)){
if(pattern.supportsSlots(slotAssignments.size + 1)){
// Add a new slot assignment
slotAssignments.add(new SlotAssignment(member, occupiedSlots));
slotAssignments.add(new SlotAssignment(member, slotAssignments.size));
// Update the slot assignments and return success
updateSlotAssignments();

View File

@@ -1,68 +1,46 @@
package mindustry.ai.types;
import arc.*;
import arc.math.geom.*;
import arc.util.ArcAnnotate.*;
import mindustry.ai.formations.*;
import mindustry.ai.formations.patterns.*;
import mindustry.entities.units.*;
import mindustry.gen.*;
public class FormationAI extends AIController implements FormationMember{
public @Nullable Unitc leader;
public Unitc leader;
private transient Vec3 target = new Vec3();
private Vec3 target = new Vec3();
private Formation formation;
public FormationAI(@Nullable Unitc leader){
public FormationAI(Unitc leader, Formation formation){
this.leader = leader;
this.formation = formation;
}
static Formation formation;
static Vec2 vec = new Vec2();
@Override
public void init(){
if(formation == null){
Vec3 vec = new Vec3();
formation = new Formation(vec, new SquareFormation());
Core.app.addListener(new ApplicationListener(){
@Override
public void update(){
formation.updateSlots();
vec.set(leader.x(), leader.y(), leader.rotation());
}
});
}
formation.addMember(this);
target.set(unit.x(), unit.y(), 0);
}
@Override
public void update(){
if(leader != null){
unit.controlWeapons(leader.isRotate(), leader.isShooting());
// unit.moveAt(Tmp.v1.set(deltaX, deltaY).limit(unit.type().speed));
if(leader.isShooting()){
unit.aimLook(leader.aimX(), leader.aimY());
}else{
unit.controlWeapons(leader.isRotate(), leader.isShooting());
// unit.moveAt(Tmp.v1.set(deltaX, deltaY).limit(unit.type().speed));
if(leader.isShooting()){
unit.aimLook(leader.aimX(), leader.aimY());
}else{
unit.lookAt(leader.rotation());
if(!unit.vel().isZero(0.001f)){
// unit.lookAt(unit.vel().angle());
}
unit.lookAt(leader.rotation());
if(!unit.vel().isZero(0.001f)){
// unit.lookAt(unit.vel().angle());
}
unit.moveAt(vec.set(target).sub(unit).limit2(unit.type().speed));
}
unit.moveAt(vec.set(target).sub(unit).limit2(unit.type().speed));
}
@Override
public boolean isFollowing(Playerc player){
return leader == player.unit();
public boolean isBeingControlled(Unitc player){
return leader == player;
}
@Override

View File

@@ -19,7 +19,7 @@ public class GroundAI extends AIController{
target = null;
//TODO this is hacky, cleanup
if(unit instanceof Legsc){
if(unit instanceof Legsc && unit.moving()){
unit.lookAt(((Legsc)unit).baseRotation());
}
}