Loop unit transfer command
This commit is contained in:
@@ -17,7 +17,7 @@ public class UnitCommand extends MappableContent{
|
||||
@Deprecated
|
||||
public static final Seq<UnitCommand> all = new Seq<>();
|
||||
|
||||
public static UnitCommand moveCommand, repairCommand, rebuildCommand, assistCommand, mineCommand, boostCommand, enterPayloadCommand, loadUnitsCommand, loadBlocksCommand, unloadPayloadCommand;
|
||||
public static UnitCommand moveCommand, repairCommand, rebuildCommand, assistCommand, mineCommand, boostCommand, enterPayloadCommand, loadUnitsCommand, loadBlocksCommand, unloadPayloadCommand, loopPayloadCommand;
|
||||
|
||||
/** Name of UI icon (from Icon class). */
|
||||
public final String icon;
|
||||
@@ -110,5 +110,10 @@ public class UnitCommand extends MappableContent{
|
||||
drawTarget = true;
|
||||
resetTarget = false;
|
||||
}};
|
||||
loopPayloadCommand = new UnitCommand("loopPayload", "resize", Binding.unit_command_loop_payload, null){{
|
||||
switchToMove = false;
|
||||
drawTarget = true;
|
||||
resetTarget = false;
|
||||
}};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,11 +20,12 @@ public class CommandAI extends AIController{
|
||||
protected static final Vec2 vecOut = new Vec2(), vecMovePos = new Vec2();
|
||||
protected static final boolean[] noFound = {false};
|
||||
protected static final UnitPayload tmpPayload = new UnitPayload(null);
|
||||
protected static final int transferStateNone = 0, transferStateLoad = 1, transferStateUnload = 2;
|
||||
|
||||
public Seq<Position> commandQueue = new Seq<>(5);
|
||||
public @Nullable Vec2 targetPos;
|
||||
public @Nullable Teamc attackTarget;
|
||||
/** Group of units that were all commanded to reach the same point.. */
|
||||
/** Group of units that were all commanded to reach the same point. */
|
||||
public @Nullable UnitGroup group;
|
||||
public int groupIndex = 0;
|
||||
/** All encountered unreachable buildings of this AI. Why a sequence? Because contains() is very rarely called on it. */
|
||||
@@ -36,6 +37,7 @@ public class CommandAI extends AIController{
|
||||
protected Vec2 lastTargetPos;
|
||||
protected boolean blockingUnit;
|
||||
protected float timeSpentBlocked;
|
||||
protected int transferState = transferStateNone;
|
||||
|
||||
/** Stance, usually related to firing mode. */
|
||||
public UnitStance stance = UnitStance.shoot;
|
||||
@@ -113,6 +115,13 @@ public class CommandAI extends AIController{
|
||||
attackTarget = null;
|
||||
}
|
||||
|
||||
void tryPickupUnit(Payloadc pay){
|
||||
Unit target = Units.closest(unit.team, unit.x, unit.y, unit.type.hitSize * 2f, u -> u.isAI() && u != unit && u.isGrounded() && pay.canPickup(u) && u.within(unit, u.hitSize + unit.hitSize));
|
||||
if(target != null){
|
||||
Call.pickedUnitPayload(unit, target);
|
||||
}
|
||||
}
|
||||
|
||||
public void defaultBehavior(){
|
||||
|
||||
if(!net.client() && unit instanceof Payloadc pay){
|
||||
@@ -123,10 +132,7 @@ public class CommandAI extends AIController{
|
||||
|
||||
//try to pick up what's under it
|
||||
if(command == UnitCommand.loadUnitsCommand){
|
||||
Unit target = Units.closest(unit.team, unit.x, unit.y, unit.type.hitSize * 2f, u -> u.isAI() && u != unit && u.isGrounded() && pay.canPickup(u) && u.within(unit, u.hitSize + unit.hitSize));
|
||||
if(target != null){
|
||||
Call.pickedUnitPayload(unit, target);
|
||||
}
|
||||
tryPickupUnit(pay);
|
||||
}
|
||||
|
||||
//try to pick up a block
|
||||
@@ -223,7 +229,8 @@ public class CommandAI extends AIController{
|
||||
//TODO: should the unit stop when it finds a target?
|
||||
if(
|
||||
(stance == UnitStance.patrol && target != null && unit.within(target, unit.type.range - 2f) && !unit.type.circleTarget) ||
|
||||
(command == UnitCommand.enterPayloadCommand && unit.within(targetPos, 4f) || (targetBuild != null && unit.within(targetBuild, targetBuild.block.size * tilesize/2f * 0.9f)))
|
||||
(command == UnitCommand.enterPayloadCommand && unit.within(targetPos, 4f) || (targetBuild != null && unit.within(targetBuild, targetBuild.block.size * tilesize/2f * 0.9f))) ||
|
||||
(command == UnitCommand.loopPayloadCommand && unit.within(targetPos, 10f))
|
||||
){
|
||||
move = false;
|
||||
}
|
||||
@@ -330,6 +337,46 @@ public class CommandAI extends AIController{
|
||||
return;
|
||||
}
|
||||
|
||||
if(!net.client() && command == UnitCommand.loopPayloadCommand && unit instanceof Payloadc pay){
|
||||
|
||||
if(transferState == transferStateNone){
|
||||
transferState = pay.hasPayload() ? transferStateUnload : transferStateLoad;
|
||||
}
|
||||
|
||||
if(transferState == transferStateUnload){
|
||||
//drop until there's a failure
|
||||
int prev = -1;
|
||||
while(pay.hasPayload() && prev != pay.payloads().size){
|
||||
prev = pay.payloads().size;
|
||||
Call.payloadDropped(unit, unit.x, unit.y);
|
||||
}
|
||||
|
||||
//wait for everything to unload before running code below
|
||||
if(pay.hasPayload()){
|
||||
return;
|
||||
}
|
||||
}else if(transferState == transferStateLoad){
|
||||
//pick up units until there's a failure
|
||||
int prev = -1;
|
||||
while(prev != pay.payloads().size){
|
||||
prev = pay.payloads().size;
|
||||
tryPickupUnit(pay);
|
||||
}
|
||||
|
||||
//wait to load things before running code below
|
||||
if(!pay.hasPayload()){
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
//it will never finish
|
||||
if(commandQueue.size == 0){
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
transferState = transferStateNone;
|
||||
|
||||
Vec2 prev = targetPos;
|
||||
targetPos = null;
|
||||
|
||||
@@ -341,7 +388,7 @@ public class CommandAI extends AIController{
|
||||
commandPosition(position);
|
||||
}
|
||||
|
||||
if(prev != null && stance == UnitStance.patrol){
|
||||
if(prev != null && (stance == UnitStance.patrol || command == UnitCommand.loopPayloadCommand)){
|
||||
commandQueue.add(prev.cpy());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user