mirror of
https://github.com/Anuken/Mindustry.git
synced 2025-07-08 23:07:33 +07:00
Pathfind command
This commit is contained in:
@ -34,14 +34,14 @@ public class GroundAI extends AIController{
|
|||||||
if(spawner != null && unit.within(spawner, state.rules.dropZoneRadius + 120f)) move = false;
|
if(spawner != null && unit.within(spawner, state.rules.dropZoneRadius + 120f)) move = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(move) moveTo(Pathfinder.fieldCore);
|
if(move) pathfind(Pathfinder.fieldCore);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(command() == UnitCommand.rally){
|
if(command() == UnitCommand.rally){
|
||||||
Teamc target = targetFlag(unit.x, unit.y, BlockFlag.rally, false);
|
Teamc target = targetFlag(unit.x, unit.y, BlockFlag.rally, false);
|
||||||
|
|
||||||
if(target != null && !unit.within(target, 70f)){
|
if(target != null && !unit.within(target, 70f)){
|
||||||
moveTo(Pathfinder.fieldRally);
|
pathfind(Pathfinder.fieldRally);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -72,16 +72,4 @@ public class GroundAI extends AIController{
|
|||||||
}
|
}
|
||||||
}*/
|
}*/
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void moveTo(int pathTarget){
|
|
||||||
int costType = unit.pathType();
|
|
||||||
|
|
||||||
Tile tile = unit.tileOn();
|
|
||||||
if(tile == null) return;
|
|
||||||
Tile targetTile = pathfinder.getTargetTile(tile, pathfinder.getField(unit.team, costType, pathTarget));
|
|
||||||
|
|
||||||
if(tile == targetTile || (costType == Pathfinder.costWater && !targetTile.floor().isLiquid)) return;
|
|
||||||
|
|
||||||
unit.moveAt(vec.trns(unit.angleTo(targetTile), unit.type().speed));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -2,20 +2,26 @@ package mindustry.ai.types;
|
|||||||
|
|
||||||
import arc.struct.*;
|
import arc.struct.*;
|
||||||
import arc.util.*;
|
import arc.util.*;
|
||||||
|
import mindustry.ai.*;
|
||||||
import mindustry.entities.units.*;
|
import mindustry.entities.units.*;
|
||||||
import mindustry.gen.*;
|
import mindustry.gen.*;
|
||||||
import mindustry.logic.LExecutor.*;
|
import mindustry.logic.LExecutor.*;
|
||||||
import mindustry.logic.*;
|
import mindustry.logic.*;
|
||||||
|
import mindustry.world.*;
|
||||||
|
import mindustry.world.meta.*;
|
||||||
|
|
||||||
|
import static mindustry.Vars.*;
|
||||||
|
|
||||||
public class LogicAI extends AIController{
|
public class LogicAI extends AIController{
|
||||||
/** Minimum delay between item transfers. */
|
/** Minimum delay between item transfers. */
|
||||||
public static final float transferDelay = 60f * 2f;
|
public static final float transferDelay = 60f * 2f;
|
||||||
/** Time after which the unit resets its controlled and reverts to a normal unit. */
|
/** Time after which the unit resets its controlled and reverts to a normal unit. */
|
||||||
public static final float logicControlTimeout = 15f * 60f;
|
public static final float logicControlTimeout = 10f * 60f;
|
||||||
|
|
||||||
public LUnitControl control = LUnitControl.stop;
|
public LUnitControl control = LUnitControl.stop;
|
||||||
public float moveX, moveY, moveRad;
|
public float moveX, moveY, moveRad;
|
||||||
public float itemTimer, controlTimer = logicControlTimeout, targetTimer;
|
public float itemTimer, controlTimer = logicControlTimeout, targetTimer;
|
||||||
|
public Building controller;
|
||||||
|
|
||||||
//type of aiming to use
|
//type of aiming to use
|
||||||
public LUnitControl aimControl = LUnitControl.stop;
|
public LUnitControl aimControl = LUnitControl.stop;
|
||||||
@ -43,7 +49,7 @@ public class LogicAI extends AIController{
|
|||||||
}
|
}
|
||||||
|
|
||||||
//timeout when not controlled by logic for a while
|
//timeout when not controlled by logic for a while
|
||||||
if(controlTimer > 0){
|
if(controlTimer > 0 && controller != null && controller.isValid()){
|
||||||
controlTimer -= Time.delta;
|
controlTimer -= Time.delta;
|
||||||
}else{
|
}else{
|
||||||
unit.resetController();
|
unit.resetController();
|
||||||
@ -57,6 +63,28 @@ public class LogicAI extends AIController{
|
|||||||
case approach -> {
|
case approach -> {
|
||||||
moveTo(Tmp.v1.set(moveX, moveY), moveRad, 10f);
|
moveTo(Tmp.v1.set(moveX, moveY), moveRad, 10f);
|
||||||
}
|
}
|
||||||
|
case pathfind -> {
|
||||||
|
Building core = unit.closestEnemyCore();
|
||||||
|
|
||||||
|
if((core == null || !unit.within(core, unit.range() * 0.5f)) && command() == UnitCommand.attack){
|
||||||
|
boolean move = true;
|
||||||
|
|
||||||
|
if(state.rules.waves && unit.team == state.rules.defaultTeam){
|
||||||
|
Tile spawner = getClosestSpawner();
|
||||||
|
if(spawner != null && unit.within(spawner, state.rules.dropZoneRadius + 120f)) move = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(move) pathfind(Pathfinder.fieldCore);
|
||||||
|
}
|
||||||
|
|
||||||
|
if(command() == UnitCommand.rally){
|
||||||
|
Teamc target = targetFlag(unit.x, unit.y, BlockFlag.rally, false);
|
||||||
|
|
||||||
|
if(target != null && !unit.within(target, 70f)){
|
||||||
|
pathfind(Pathfinder.fieldRally);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//look where moving if there's nothing to aim at
|
//look where moving if there's nothing to aim at
|
||||||
|
@ -67,10 +67,10 @@ public class SuicideAI extends GroundAI{
|
|||||||
Teamc target = targetFlag(unit.x, unit.y, BlockFlag.rally, false);
|
Teamc target = targetFlag(unit.x, unit.y, BlockFlag.rally, false);
|
||||||
|
|
||||||
if(target != null && !unit.within(target, 70f)){
|
if(target != null && !unit.within(target, 70f)){
|
||||||
moveTo(Pathfinder.fieldRally);
|
pathfind(Pathfinder.fieldRally);
|
||||||
}
|
}
|
||||||
}else if(command() == UnitCommand.attack && core != null){
|
}else if(command() == UnitCommand.attack && core != null){
|
||||||
moveTo(Pathfinder.fieldCore);
|
pathfind(Pathfinder.fieldCore);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(unit.moving()) unit.lookAt(unit.vel().angle());
|
if(unit.moving()) unit.lookAt(unit.vel().angle());
|
||||||
|
@ -4,6 +4,7 @@ import arc.math.*;
|
|||||||
import arc.math.geom.*;
|
import arc.math.geom.*;
|
||||||
import arc.util.*;
|
import arc.util.*;
|
||||||
import mindustry.*;
|
import mindustry.*;
|
||||||
|
import mindustry.ai.*;
|
||||||
import mindustry.entities.*;
|
import mindustry.entities.*;
|
||||||
import mindustry.gen.*;
|
import mindustry.gen.*;
|
||||||
import mindustry.type.*;
|
import mindustry.type.*;
|
||||||
@ -67,6 +68,19 @@ public class AIController implements UnitController{
|
|||||||
return Units.invalidateTarget(target, unit.team, unit.x, unit.y);
|
return Units.invalidateTarget(target, unit.team, unit.x, unit.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
protected void pathfind(int pathTarget){
|
||||||
|
int costType = unit.pathType();
|
||||||
|
|
||||||
|
Tile tile = unit.tileOn();
|
||||||
|
if(tile == null) return;
|
||||||
|
Tile targetTile = pathfinder.getTargetTile(tile, pathfinder.getField(unit.team, costType, pathTarget));
|
||||||
|
|
||||||
|
if(tile == targetTile || (costType == Pathfinder.costWater && !targetTile.floor().isLiquid)) return;
|
||||||
|
|
||||||
|
unit.moveAt(vec.trns(unit.angleTo(targetTile), unit.type().speed));
|
||||||
|
}
|
||||||
|
|
||||||
protected void updateWeapons(){
|
protected void updateWeapons(){
|
||||||
if(targets.length != unit.mounts.length) targets = new Teamc[unit.mounts.length];
|
if(targets.length != unit.mounts.length) targets = new Teamc[unit.mounts.length];
|
||||||
|
|
||||||
|
@ -27,6 +27,8 @@ public class LAssembler{
|
|||||||
putConst("@time", 0);
|
putConst("@time", 0);
|
||||||
//currently controlled unit
|
//currently controlled unit
|
||||||
putConst("@unit", null);
|
putConst("@unit", null);
|
||||||
|
//reference to self
|
||||||
|
putConst("@this", null);
|
||||||
|
|
||||||
//add default constants
|
//add default constants
|
||||||
putConst("false", 0);
|
putConst("false", 0);
|
||||||
|
@ -27,7 +27,8 @@ public class LExecutor{
|
|||||||
public static final int
|
public static final int
|
||||||
varCounter = 0,
|
varCounter = 0,
|
||||||
varTime = 1,
|
varTime = 1,
|
||||||
varUnit = 2;
|
varUnit = 2,
|
||||||
|
varThis = 3;
|
||||||
|
|
||||||
public static final int
|
public static final int
|
||||||
maxGraphicsBuffer = 256,
|
maxGraphicsBuffer = 256,
|
||||||
@ -215,6 +216,7 @@ public class LExecutor{
|
|||||||
if(unitObj instanceof Unit unit && exec.obj(varUnit) == unit && unit.team == exec.team && !unit.isPlayer() && !(unit.controller() instanceof FormationAI)){
|
if(unitObj instanceof Unit unit && exec.obj(varUnit) == unit && unit.team == exec.team && !unit.isPlayer() && !(unit.controller() instanceof FormationAI)){
|
||||||
if(!(unit.controller() instanceof LogicAI)){
|
if(!(unit.controller() instanceof LogicAI)){
|
||||||
unit.controller(new LogicAI());
|
unit.controller(new LogicAI());
|
||||||
|
((LogicAI)unit.controller()).controller = exec.building(varThis);
|
||||||
|
|
||||||
//clear old state
|
//clear old state
|
||||||
if(unit instanceof Minerc miner){
|
if(unit instanceof Minerc miner){
|
||||||
@ -250,6 +252,9 @@ public class LExecutor{
|
|||||||
ai.moveRad = exec.numf(p3);
|
ai.moveRad = exec.numf(p3);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
case pathfind -> {
|
||||||
|
ai.control = type;
|
||||||
|
}
|
||||||
case target -> {
|
case target -> {
|
||||||
ai.posTarget.set(exec.numf(p1), exec.numf(p2));
|
ai.posTarget.set(exec.numf(p1), exec.numf(p2));
|
||||||
ai.aimControl = type;
|
ai.aimControl = type;
|
||||||
|
@ -4,6 +4,7 @@ public enum LUnitControl{
|
|||||||
stop,
|
stop,
|
||||||
move("x", "y"),
|
move("x", "y"),
|
||||||
approach("x", "y", "radius"),
|
approach("x", "y", "radius"),
|
||||||
|
pathfind(),
|
||||||
target("x", "y", "shoot"),
|
target("x", "y", "shoot"),
|
||||||
targetp("unit", "shoot"),
|
targetp("unit", "shoot"),
|
||||||
itemDrop("to", "amount"),
|
itemDrop("to", "amount"),
|
||||||
|
@ -304,7 +304,7 @@ public class LogicBlock extends Block{
|
|||||||
assemble.get(asm);
|
assemble.get(asm);
|
||||||
}
|
}
|
||||||
|
|
||||||
asm.putConst("@this", this);
|
asm.getVar("@this").value = this;
|
||||||
asm.putConst("@thisx", x);
|
asm.putConst("@thisx", x);
|
||||||
asm.putConst("@thisy", y);
|
asm.putConst("@thisy", y);
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user