Pathfind command

This commit is contained in:
Anuken 2020-10-05 17:09:27 -04:00
parent c24112e405
commit efbc871967
8 changed files with 58 additions and 20 deletions

View File

@ -34,14 +34,14 @@ public class GroundAI extends AIController{
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){
Teamc target = targetFlag(unit.x, unit.y, BlockFlag.rally, false);
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));
}
}

View File

@ -2,20 +2,26 @@ package mindustry.ai.types;
import arc.struct.*;
import arc.util.*;
import mindustry.ai.*;
import mindustry.entities.units.*;
import mindustry.gen.*;
import mindustry.logic.LExecutor.*;
import mindustry.logic.*;
import mindustry.world.*;
import mindustry.world.meta.*;
import static mindustry.Vars.*;
public class LogicAI extends AIController{
/** Minimum delay between item transfers. */
public static final float transferDelay = 60f * 2f;
/** 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 float moveX, moveY, moveRad;
public float itemTimer, controlTimer = logicControlTimeout, targetTimer;
public Building controller;
//type of aiming to use
public LUnitControl aimControl = LUnitControl.stop;
@ -43,7 +49,7 @@ public class LogicAI extends AIController{
}
//timeout when not controlled by logic for a while
if(controlTimer > 0){
if(controlTimer > 0 && controller != null && controller.isValid()){
controlTimer -= Time.delta;
}else{
unit.resetController();
@ -57,6 +63,28 @@ public class LogicAI extends AIController{
case approach -> {
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

View File

@ -67,10 +67,10 @@ public class SuicideAI extends GroundAI{
Teamc target = targetFlag(unit.x, unit.y, BlockFlag.rally, false);
if(target != null && !unit.within(target, 70f)){
moveTo(Pathfinder.fieldRally);
pathfind(Pathfinder.fieldRally);
}
}else if(command() == UnitCommand.attack && core != null){
moveTo(Pathfinder.fieldCore);
pathfind(Pathfinder.fieldCore);
}
if(unit.moving()) unit.lookAt(unit.vel().angle());

View File

@ -4,6 +4,7 @@ import arc.math.*;
import arc.math.geom.*;
import arc.util.*;
import mindustry.*;
import mindustry.ai.*;
import mindustry.entities.*;
import mindustry.gen.*;
import mindustry.type.*;
@ -67,6 +68,19 @@ public class AIController implements UnitController{
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(){
if(targets.length != unit.mounts.length) targets = new Teamc[unit.mounts.length];

View File

@ -27,6 +27,8 @@ public class LAssembler{
putConst("@time", 0);
//currently controlled unit
putConst("@unit", null);
//reference to self
putConst("@this", null);
//add default constants
putConst("false", 0);

View File

@ -27,7 +27,8 @@ public class LExecutor{
public static final int
varCounter = 0,
varTime = 1,
varUnit = 2;
varUnit = 2,
varThis = 3;
public static final int
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(!(unit.controller() instanceof LogicAI)){
unit.controller(new LogicAI());
((LogicAI)unit.controller()).controller = exec.building(varThis);
//clear old state
if(unit instanceof Minerc miner){
@ -250,6 +252,9 @@ public class LExecutor{
ai.moveRad = exec.numf(p3);
}
}
case pathfind -> {
ai.control = type;
}
case target -> {
ai.posTarget.set(exec.numf(p1), exec.numf(p2));
ai.aimControl = type;

View File

@ -4,6 +4,7 @@ public enum LUnitControl{
stop,
move("x", "y"),
approach("x", "y", "radius"),
pathfind(),
target("x", "y", "shoot"),
targetp("unit", "shoot"),
itemDrop("to", "amount"),

View File

@ -304,7 +304,7 @@ public class LogicBlock extends Block{
assemble.get(asm);
}
asm.putConst("@this", this);
asm.getVar("@this").value = this;
asm.putConst("@thisx", x);
asm.putConst("@thisy", y);