Logic unit control

This commit is contained in:
Anuken
2020-10-05 15:42:37 -04:00
parent 8e49d73765
commit 7088ae89b3
37 changed files with 661 additions and 202 deletions

View File

@@ -65,7 +65,7 @@ public class Units{
/** @return whether a new instance of a unit of this team can be created. */
public static boolean canCreate(Team team, UnitType type){
return teamIndex.countType(team, type) < getCap(team);
return team.data().countType(type) < getCap(team);
}
public static int getCap(Team team){
@@ -284,7 +284,7 @@ public class Units{
/** Iterates over all units in a rectangle. */
public static void nearby(Team team, float x, float y, float width, float height, Cons<Unit> cons){
teamIndex.tree(team).intersect(x, y, width, height, cons);
team.data().tree().intersect(x, y, width, height, cons);
}
/** Iterates over all units in a circle around this position. */
@@ -316,7 +316,7 @@ public class Units{
//inactive teams have no cache, check everything
//TODO cache all teams with units OR blocks
for(Team other : Team.all){
if(other != team && teamIndex.count(other) > 0){
if(other != team && other.data().unitCount > 0){
nearby(other, x, y, width, height, cons);
}
}

View File

@@ -31,12 +31,12 @@ abstract class BulletComp implements Timedc, Damagec, Hitboxc, Teamc, Posc, Draw
public void getCollisions(Cons<QuadTree> consumer){
if(team.active()){
for(Team team : team.enemies()){
consumer.get(teamIndex.tree(team));
consumer.get(team.data().tree());
}
}else{
for(Team other : Team.all){
if(other != team && teamIndex.count(other) > 0){
consumer.get(teamIndex.tree(other));
if(other != team && team.data().unitCount > 0){
consumer.get(team.data().tree());
}
}
}

View File

@@ -0,0 +1,9 @@
package mindustry.entities.comp;
import mindustry.annotations.Annotations.*;
import mindustry.gen.*;
//dummy target definition
@EntityDef(value = Teamc.class, genio = false, isFinal = false)
public class PosTeamDef{
}

View File

@@ -27,7 +27,7 @@ import mindustry.world.blocks.environment.*;
import static mindustry.Vars.*;
@Component(base = true)
abstract class UnitComp implements Healthc, Physicsc, Hitboxc, Statusc, Teamc, Itemsc, Rotc, Unitc, Weaponsc, Drawc, Boundedc, Syncc, Shieldc, Displayable, Senseable{
abstract class UnitComp implements Healthc, Physicsc, Hitboxc, Statusc, Teamc, Itemsc, Rotc, Unitc, Weaponsc, Drawc, Boundedc, Syncc, Shieldc, Displayable, Senseable, Ranged{
@Import boolean hovering, dead;
@Import float x, y, rotation, elevation, maxHealth, drag, armor, hitSize, health, ammo;
@@ -38,6 +38,9 @@ abstract class UnitComp implements Healthc, Physicsc, Hitboxc, Statusc, Teamc, I
private UnitType type;
boolean spawnedByCore;
//TODO mark as non-transient when done
transient double flag;
transient Seq<Ability> abilities = new Seq<>(0);
private transient float resupplyTime = Mathf.random(10f);
@@ -63,6 +66,7 @@ abstract class UnitComp implements Healthc, Physicsc, Hitboxc, Statusc, Teamc, I
return type.hasWeapons();
}
@Override
public float range(){
return type.range;
}
@@ -76,6 +80,7 @@ abstract class UnitComp implements Healthc, Physicsc, Hitboxc, Statusc, Teamc, I
public double sense(LAccess sensor){
return switch(sensor){
case totalItems -> stack().amount;
case itemCapacity -> type.itemCapacity;
case rotation -> rotation;
case health -> health;
case maxHealth -> maxHealth;
@@ -85,6 +90,7 @@ abstract class UnitComp implements Healthc, Physicsc, Hitboxc, Statusc, Teamc, I
case shooting -> isShooting() ? 1 : 0;
case shootX -> aimX();
case shootY -> aimY();
case flag -> flag;
default -> 0;
};
}
@@ -93,6 +99,7 @@ abstract class UnitComp implements Healthc, Physicsc, Hitboxc, Statusc, Teamc, I
public Object senseObject(LAccess sensor){
return switch(sensor){
case type -> type;
case name -> controller instanceof Player p ? p.name : null;
default -> noSensed;
};
@@ -182,7 +189,7 @@ abstract class UnitComp implements Healthc, Physicsc, Hitboxc, Statusc, Teamc, I
}
public int count(){
return teamIndex.countType(team, type);
return team.data().countType(type);
}
public int cap(){
@@ -224,13 +231,13 @@ abstract class UnitComp implements Healthc, Physicsc, Hitboxc, Statusc, Teamc, I
//check if over unit cap
if(count() > cap() && !spawnedByCore && !dead){
Call.unitCapDeath(self());
teamIndex.updateCount(team, type, -1);
team.data().updateCount(type, -1);
}
}
@Override
public void remove(){
teamIndex.updateCount(team, type, -1);
team.data().updateCount(type, -1);
controller.removed(self());
}

View File

@@ -63,6 +63,10 @@ public class AIController implements UnitController{
}
}
protected boolean invalid(Teamc target){
return Units.invalidateTarget(target, unit.team, unit.x, unit.y);
}
protected void updateWeapons(){
if(targets.length != unit.mounts.length) targets = new Teamc[unit.mounts.length];
@@ -73,7 +77,7 @@ public class AIController implements UnitController{
target = findTarget(unit.x, unit.y, unit.range(), unit.type().targetAir, unit.type().targetGround);
}
if(Units.invalidateTarget(target, unit.team, unit.x, unit.y)){
if(invalid(target)){
target = null;
}
@@ -99,13 +103,11 @@ public class AIController implements UnitController{
boolean shoot = false;
if(targets[i] != null){
shoot = targets[i].within(mountX, mountY, weapon.bullet.range());
shoot = targets[i].within(mountX, mountY, weapon.bullet.range()) && shouldShoot();
if(shoot){
Vec2 to = Predict.intercept(unit, targets[i], weapon.bullet.speed);
mount.aimX = to.x;
mount.aimY = to.y;
}
Vec2 to = Predict.intercept(unit, targets[i], weapon.bullet.speed);
mount.aimX = to.x;
mount.aimY = to.y;
}
mount.shoot = shoot;
@@ -113,6 +115,10 @@ public class AIController implements UnitController{
}
}
protected boolean shouldShoot(){
return true;
}
protected Teamc targetFlag(float x, float y, BlockFlag flag, boolean enemy){
Tile target = Geometry.findClosest(x, y, enemy ? indexer.getEnemy(unit.team, flag) : indexer.getAllied(unit.team, flag));
return target == null ? null : target.build;
@@ -157,11 +163,15 @@ public class AIController implements UnitController{
}
protected void moveTo(Position target, float circleLength){
moveTo(target, circleLength, 100f);
}
protected void moveTo(Position target, float circleLength, float smooth){
if(target == null) return;
vec.set(target).sub(unit);
float length = circleLength <= 0.001f ? 1f : Mathf.clamp((unit.dst(target) - circleLength) / 100f, -1f, 1f);
float length = circleLength <= 0.001f ? 1f : Mathf.clamp((unit.dst(target) - circleLength) / smooth, -1f, 1f);
vec.setLength(unit.type().speed * length);
if(length < -0.5f){