Basic unit boost command support

This commit is contained in:
Anuken
2023-02-11 17:40:57 -05:00
parent 946e36c66e
commit ec8262418f
9 changed files with 81 additions and 21 deletions

View File

@@ -14,7 +14,10 @@ public class UnitCommand{
public static final UnitCommand
moveCommand = new UnitCommand("move", "right", u -> null),
moveCommand = new UnitCommand("move", "right", u -> null){{
drawTarget = true;
resetTarget = false;
}},
repairCommand = new UnitCommand("repair", "modeSurvival", u -> new RepairAI()),
rebuildCommand = new UnitCommand("rebuild", "hammer", u -> new BuilderAI()),
assistCommand = new UnitCommand("assist", "players", u -> {
@@ -22,7 +25,12 @@ public class UnitCommand{
ai.onlyAssist = true;
return ai;
}),
mineCommand = new UnitCommand("mine", "production", u -> new MinerAI());
mineCommand = new UnitCommand("mine", "production", u -> new MinerAI()),
boostCommand = new UnitCommand("boost", "up", u -> new BoostAI()){{
switchToMove = false;
drawTarget = true;
resetTarget = false;
}};
/** Unique ID number. */
public final int id;
@@ -32,6 +40,12 @@ public class UnitCommand{
public final String icon;
/** Controller that this unit will use when this command is used. Return null for "default" behavior. */
public final Func<Unit, AIController> controller;
/** If true, this unit will automatically switch away to the move command when given a position. */
public boolean switchToMove = true;
/** Whether to draw the movement/attack target. */
public boolean drawTarget = false;
/** Whether to reset targets when switching to or from this command. */
public boolean resetTarget = true;
public UnitCommand(String name, String icon, Func<Unit, AIController> controller){
this.name = name;

View File

@@ -0,0 +1,21 @@
package mindustry.ai.types;
import mindustry.ai.*;
import mindustry.entities.units.*;
//not meant to be used outside RTS-AI-controlled units
public class BoostAI extends AIController{
@Override
public void updateUnit(){
if(unit.controller() instanceof CommandAI ai){
ai.defaultBehavior();
unit.updateBoosting(true);
//auto land when near target
if(ai.attackTarget != null && unit.within(ai.attackTarget, unit.range())){
unit.command().command(UnitCommand.moveCommand);
}
}
}
}

View File

@@ -35,8 +35,8 @@ public class CommandAI extends AIController{
/** Last command type assigned. Used for detecting command changes. */
protected @Nullable UnitCommand lastCommand;
public @Nullable UnitCommand currentCommand(){
return command;
public UnitCommand currentCommand(){
return command == null ? UnitCommand.moveCommand : command;
}
/** Attempts to assign a command to this unit. If not supported by the unit type, does nothing. */
@@ -62,7 +62,7 @@ public class CommandAI extends AIController{
}
//update command controller based on index.
var curCommand = currentCommand();
var curCommand = command;
if(lastCommand != curCommand){
lastCommand = curCommand;
commandController = (curCommand == null ? null : curCommand.controller.get(unit));
@@ -72,8 +72,14 @@ public class CommandAI extends AIController{
if(commandController != null){
if(commandController.unit() != unit) commandController.unit(unit);
commandController.updateUnit();
return;
}else{
defaultBehavior();
//boosting control is not supported, so just don't.
unit.updateBoosting(false);
}
}
public void defaultBehavior(){
//acquiring naval targets isn't supported yet, so use the fallback dumb AI
if(unit.team.isAI() && unit.team.rules().rtsAi && unit.type.naval){
@@ -162,10 +168,10 @@ public class CommandAI extends AIController{
circleAttack(80f);
}else{
moveTo(vecOut,
attackTarget != null && unit.within(attackTarget, engageRange) ? engageRange :
unit.isGrounded() ? 0f :
attackTarget != null ? engageRange :
0f, unit.isFlying() ? 40f : 100f, false, null, targetPos.epsilonEquals(vecOut, 4.1f));
attackTarget != null && unit.within(attackTarget, engageRange) ? engageRange :
unit.isGrounded() ? 0f :
attackTarget != null ? engageRange :
0f, unit.isFlying() ? 40f : 100f, false, null, targetPos.epsilonEquals(vecOut, 4.1f));
}
}
@@ -207,9 +213,6 @@ public class CommandAI extends AIController{
}else if(target != null){
faceTarget();
}
//boosting control is not supported, so just don't.
unit.updateBoosting(false);
}
@Override
@@ -249,8 +252,12 @@ public class CommandAI extends AIController{
lastTargetPos = targetPos;
}
@Override
public void commandPosition(Vec2 pos){
commandPosition(pos, false);
if(commandController != null){
commandController.commandPosition(pos);
}
}
public void commandPosition(Vec2 pos, boolean stopWhenInRange){
@@ -261,8 +268,12 @@ public class CommandAI extends AIController{
this.stopWhenInRange = stopWhenInRange;
}
@Override
public void commandTarget(Teamc moveTo){
commandTarget(moveTo, false);
if(commandController != null){
commandController.commandTarget(moveTo);
}
}
public void commandTarget(Teamc moveTo, boolean stopAtTarget){