diff --git a/core/src/mindustry/ai/types/CommandAI.java b/core/src/mindustry/ai/types/CommandAI.java index 0cf650ccc2..bedc78fc14 100644 --- a/core/src/mindustry/ai/types/CommandAI.java +++ b/core/src/mindustry/ai/types/CommandAI.java @@ -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()); diff --git a/core/src/mindustry/entities/comp/EntityComp.java b/core/src/mindustry/entities/comp/EntityComp.java index aaacdc521b..fdba35a737 100644 --- a/core/src/mindustry/entities/comp/EntityComp.java +++ b/core/src/mindustry/entities/comp/EntityComp.java @@ -66,4 +66,9 @@ abstract class EntityComp{ void afterRead(){ } + + /** Called after *all* entities are read. */ + void afterAllRead(){ + + } } diff --git a/core/src/mindustry/entities/comp/UnitComp.java b/core/src/mindustry/entities/comp/UnitComp.java index 4ff6b3168f..828c9880d7 100644 --- a/core/src/mindustry/entities/comp/UnitComp.java +++ b/core/src/mindustry/entities/comp/UnitComp.java @@ -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); diff --git a/core/src/mindustry/entities/units/AIController.java b/core/src/mindustry/entities/units/AIController.java index 6b688eb60a..963fb67933 100644 --- a/core/src/mindustry/entities/units/AIController.java +++ b/core/src/mindustry/entities/units/AIController.java @@ -55,6 +55,11 @@ public class AIController implements UnitController{ return false; } + @Override + public void afterRead(Unit unit){ + + } + @Override public boolean isLogicControllable(){ return true; diff --git a/core/src/mindustry/entities/units/UnitController.java b/core/src/mindustry/entities/units/UnitController.java index ff861b805d..b77a857a2f 100644 --- a/core/src/mindustry/entities/units/UnitController.java +++ b/core/src/mindustry/entities/units/UnitController.java @@ -27,6 +27,10 @@ public interface UnitController{ } + default void afterRead(Unit unit){ + + } + default boolean isBeingControlled(Unit player){ return false; } diff --git a/core/src/mindustry/io/SaveVersion.java b/core/src/mindustry/io/SaveVersion.java index a1089b7dd3..e45d510893 100644 --- a/core/src/mindustry/io/SaveVersion.java +++ b/core/src/mindustry/io/SaveVersion.java @@ -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 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{ diff --git a/core/src/mindustry/io/TypeIO.java b/core/src/mindustry/io/TypeIO.java index d2db6b1e70..4a80eca3e9 100644 --- a/core/src/mindustry/io/TypeIO.java +++ b/core/src/mindustry/io/TypeIO.java @@ -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;