Better Flare/Avert targeting / Fixed targets resetting on save load

This commit is contained in:
Anuken
2023-12-10 08:32:49 -05:00
parent 484d89f022
commit e494806cc8
7 changed files with 39 additions and 2 deletions

View File

@@ -30,6 +30,8 @@ public class CommandAI extends AIController{
public int groupIndex = 0;
/** All encountered unreachable buildings of this AI. Why a sequence? Because contains() is very rarely called on it. */
public IntSeq unreachableBuildings = new IntSeq(8);
/** ID of unit read as target. This is set up after reading. Do not access! */
public int readAttackTarget = -1;
protected boolean stopAtTarget, stopWhenInRange;
protected Vec2 lastTargetPos;
@@ -284,7 +286,7 @@ public class CommandAI extends AIController{
attackTarget = null;
}
if(unit.isFlying() && move){
if(unit.isFlying() && move && (attackTarget == null || !unit.within(attackTarget, unit.type.range))){
unit.lookAt(vecMovePos);
}else{
faceTarget();
@@ -349,6 +351,14 @@ public class CommandAI extends AIController{
}
}
@Override
public void afterRead(Unit unit){
if(readAttackTarget != -1){
attackTarget = Groups.unit.getByID(readAttackTarget);
readAttackTarget = -1;
}
}
@Override
public float prefSpeed(){
return group == null ? super.prefSpeed() : Math.min(group.minSpeed, unit.speed());

View File

@@ -66,4 +66,9 @@ abstract class EntityComp{
void afterRead(){
}
/** Called after *all* entities are read. */
void afterAllRead(){
}
}

View File

@@ -490,6 +490,11 @@ abstract class UnitComp implements Healthc, Physicsc, Hitboxc, Statusc, Teamc, I
}
}
@Override
public void afterAllRead(){
controller.afterRead(self());
}
@Override
public void add(){
team.data().updateCount(type, 1);

View File

@@ -55,6 +55,11 @@ public class AIController implements UnitController{
return false;
}
@Override
public void afterRead(Unit unit){
}
@Override
public boolean isLogicControllable(){
return true;

View File

@@ -27,6 +27,10 @@ public interface UnitController{
}
default void afterRead(Unit unit){
}
default boolean isBeingControlled(Unit player){
return false;
}

View File

@@ -437,6 +437,8 @@ public abstract class SaveVersion extends SaveFileReader{
//entityMapping is null in older save versions, so use the default
var mapping = this.entityMapping == null ? EntityMapping.idMap : this.entityMapping;
Seq<Entityc> entities = new Seq<>();
int amount = stream.readInt();
for(int j = 0; j < amount; j++){
readChunk(stream, true, in -> {
@@ -449,12 +451,17 @@ public abstract class SaveVersion extends SaveFileReader{
int id = in.readInt();
Entityc entity = (Entityc)mapping[typeid].get();
entities.add(entity);
EntityGroup.checkNextId(id);
entity.id(id);
entity.read(Reads.get(in));
entity.add();
});
}
for(var e : entities){
e.afterAllRead();
}
}
public void readEntityMapping(DataInput stream) throws IOException{

View File

@@ -563,13 +563,14 @@ public class TypeIO{
ai.targetPos = null;
}
ai.setupLastPos();
ai.readAttackTarget = -1;
if(hasAttack){
byte entityType = read.b();
if(entityType == 1){
ai.attackTarget = world.build(read.i());
}else{
ai.attackTarget = Groups.unit.getByID(read.i());
ai.attackTarget = Groups.unit.getByID(ai.readAttackTarget = read.i());
}
}else{
ai.attackTarget = null;