public void Tick(Squad owner) { if (!owner.IsValid) return; if (!owner.TargetIsValid) { var t = owner.bot.FindClosestEnemy(owner.units.FirstOrDefault().CenterPosition); if (t == null) return; owner.Target = t; } var enemyUnits = owner.world.FindActorsInCircle(owner.Target.CenterPosition, WRange.FromCells(10)) .Where(unit => owner.bot.p.Stances[unit.Owner] == Stance.Enemy).ToList(); if (enemyUnits.Any()) { if (owner.attackOrFleeFuzzy.CanAttack(owner.units, enemyUnits)) { foreach (var u in owner.units) owner.world.IssueOrder(new Order("AttackMove", u, false) { TargetLocation = owner.Target.Location }); // We have gathered sufficient units. Attack the nearest enemy unit. owner.fsm.ChangeState(owner, new GroundUnitsAttackMoveState(), true); return; } else owner.fsm.ChangeState(owner, new GroundUnitsFleeState(), true); } }
public void Tick(Squad owner) { if (!owner.IsValid) return; GoToRandomOwnBuilding(owner); owner.fsm.ChangeState(owner, new UnitsForProtectionIdleState(), true); }
protected static CPos RandomBuildingLocation(Squad squad) { var location = squad.bot.baseCenter; var buildings = squad.world.ActorsWithTrait<Building>() .Where(a => a.Actor.Owner == squad.bot.p).Select(a => a.Actor).ToArray(); if (buildings.Length > 0) location = buildings.Random(squad.random).Location; return location; }
public void Tick(Squad owner) { if (!owner.IsValid) return; if (!owner.TargetIsValid) { var closestEnemy = owner.bot.FindClosestEnemy(owner.units.Random(owner.random).CenterPosition); if (closestEnemy != null) owner.Target = closestEnemy; else { owner.fsm.ChangeState(owner, new GroundUnitsFleeState(), true); return; } } var leader = owner.units.ClosestTo(owner.Target.CenterPosition); if (leader == null) return; var ownUnits = owner.world.FindActorsInCircle(leader.CenterPosition, WRange.FromCells(owner.units.Count) / 3) .Where(a => a.Owner == owner.units.FirstOrDefault().Owner && owner.units.Contains(a)).ToList(); if (ownUnits.Count < owner.units.Count) { owner.world.IssueOrder(new Order("Stop", leader, false)); foreach (var unit in owner.units.Where(a => !ownUnits.Contains(a))) owner.world.IssueOrder(new Order("AttackMove", unit, false) { TargetLocation = leader.Location }); } else { var enemies = owner.world.FindActorsInCircle(leader.CenterPosition, WRange.FromCells(12)) .Where(a1 => !a1.Destroyed && !a1.IsDead()).ToList(); var enemynearby = enemies.Where(a1 => a1.HasTrait<ITargetable>() && leader.Owner.Stances[a1.Owner] == Stance.Enemy).ToList(); if (enemynearby.Any()) { owner.Target = enemynearby.ClosestTo(leader.CenterPosition); owner.fsm.ChangeState(owner, new GroundUnitsAttackState(), true); return; } else foreach (var a in owner.units) owner.world.IssueOrder(new Order("AttackMove", a, false) { TargetLocation = owner.Target.Location }); } if (ShouldFlee(owner)) { owner.fsm.ChangeState(owner, new GroundUnitsFleeState(), true); return; } }
public void Tick(Squad owner) { if (!owner.IsValid) return; if (!owner.TargetIsValid) { var a = owner.units.Random(owner.random); var closestEnemy = owner.bot.FindClosestEnemy(a.CenterPosition); if (closestEnemy != null) owner.Target = closestEnemy; else { owner.fsm.ChangeState(owner, new AirFleeState(), true); return; } } if (!NearToPosSafely(owner, owner.Target.CenterPosition)) { owner.fsm.ChangeState(owner, new AirFleeState(), true); return; } foreach (var a in owner.units) { if (BusyAttack(a)) continue; if (!ReloadsAutomatically(a)) { if (!HasAmmo(a)) { if (IsRearm(a)) continue; owner.world.IssueOrder(new Order("ReturnToBase", a, false)); continue; } if (IsRearm(a)) continue; } if (owner.Target.HasTrait<ITargetable>() && CanAttackTarget(a, owner.Target)) owner.world.IssueOrder(new Order("Attack", a, false) { TargetActor = owner.Target }); } }
protected virtual bool ShouldFlee(Squad squad, Func<IEnumerable<Actor>, bool> flee) { if (!squad.IsValid) return false; var u = squad.units.Random(squad.random); var units = squad.world.FindActorsInCircle(u.CenterPosition, WRange.FromCells(DangerRadius)).ToList(); var ownBaseBuildingAround = units.Where(unit => unit.Owner == squad.bot.p && unit.HasTrait<Building>()); if (ownBaseBuildingAround.Any()) return false; var enemyAroundUnit = units.Where(unit => squad.bot.p.Stances[unit.Owner] == Stance.Enemy && unit.HasTrait<AttackBase>()); if (!enemyAroundUnit.Any()) return false; return flee(enemyAroundUnit); }
public void Tick(Squad owner) { if (!owner.IsValid) return; if (!owner.TargetIsValid) { owner.Target = owner.bot.FindClosestEnemy(owner.CenterPosition, WRange.FromCells(8)); if (owner.Target == null) { owner.fsm.ChangeState(owner, new UnitsForProtectionFleeState(), true); return; } } foreach (var a in owner.units) owner.world.IssueOrder(new Order("AttackMove", a, false) { TargetLocation = owner.Target.Location }); }
protected static CPos? FindSafePlace(Squad owner, out Actor detectedEnemyTarget, bool needTarget) { var world = owner.world; detectedEnemyTarget = null; var x = (world.Map.MapSize.X % DangerRadius) == 0 ? world.Map.MapSize.X : world.Map.MapSize.X + DangerRadius; var y = (world.Map.MapSize.Y % DangerRadius) == 0 ? world.Map.MapSize.Y : world.Map.MapSize.Y + DangerRadius; for (var i = 0; i < x; i += DangerRadius * 2) { for (var j = 0; j < y; j += DangerRadius * 2) { var pos = new CPos(i, j); if (NearToPosSafely(owner, owner.world.Map.CenterOfCell(pos), out detectedEnemyTarget)) { if (needTarget && detectedEnemyTarget == null) continue; return pos; } } } return null; }
protected static bool NearToPosSafely(Squad owner, WPos loc) { Actor a; return NearToPosSafely(owner, loc, out a); }
protected static Actor FindDefenselessTarget(Squad owner) { Actor target = null; FindSafePlace(owner, out target, true); return target; }
public void Tick(Squad owner) { if (!owner.IsValid) return; if (ShouldFlee(owner)) { owner.fsm.ChangeState(owner, new AirFleeState(), true); return; } var e = FindDefenselessTarget(owner); if (e == null) return; owner.Target = e; owner.fsm.ChangeState(owner, new AirAttackState(), true); }
public void Deactivate(Squad owner) { }
protected virtual bool ShouldFlee(Squad owner) { return base.ShouldFlee(owner, enemies => !owner.attackOrFleeFuzzy.CanAttack(owner.units, enemies)); }
public void Deactivate(Squad owner) { owner.units.Clear(); }
public void Tick(Squad owner) { if (!owner.IsValid) return; if (!owner.TargetIsValid) { var closestEnemy = owner.bot.FindClosestEnemy(owner.units.Random(owner.random).CenterPosition); if (closestEnemy != null) owner.Target = closestEnemy; else { owner.fsm.ChangeState(owner, new GroundUnitsFleeState(), true); return; } } foreach (var a in owner.units) if (!BusyAttack(a)) owner.world.IssueOrder(new Order("Attack", a, false) { TargetActor = owner.bot.FindClosestEnemy(a.CenterPosition) }); if (ShouldFlee(owner)) { owner.fsm.ChangeState(owner, new GroundUnitsFleeState(), true); return; } }
public void Tick(Squad owner) { owner.fsm.ChangeState(owner, new UnitsForProtectionAttackState(), true); }
public void Activate(Squad owner) { }
protected static bool NearToPosSafely(Squad owner, WPos loc, out Actor detectedEnemyTarget) { detectedEnemyTarget = null; var unitsAroundPos = owner.world.FindActorsInCircle(loc, WRange.FromCells(DangerRadius)) .Where(unit => owner.bot.p.Stances[unit.Owner] == Stance.Enemy).ToList(); if (!unitsAroundPos.Any()) return true; if (CountAntiAirUnits(unitsAroundPos) * MissileUnitMultiplier < owner.units.Count) { detectedEnemyTarget = unitsAroundPos.Random(owner.random); return true; } return false; }
// Checks the number of anti air enemies around units protected virtual bool ShouldFlee(Squad owner) { return base.ShouldFlee(owner, enemies => CountAntiAirUnits(enemies) * MissileUnitMultiplier > owner.units.Count); }
protected static void GoToRandomOwnBuilding(Squad squad) { var loc = RandomBuildingLocation(squad); foreach (var a in squad.units) squad.world.IssueOrder(new Order("Move", a, false) { TargetLocation = loc }); }
public void Tick(Squad owner) { if (!owner.IsValid) { return; } if (!owner.TargetIsValid) { var targeter = owner.units.Random(owner.random); var closestEnemy = owner.bot.FindClosestEnemy(targeter, targeter.CenterPosition); if (closestEnemy != null) { owner.Target = closestEnemy; } else { owner.fsm.ChangeState(owner, new GroundUnitsFleeState(), true); return; } } Actor leader = owner.units.ClosestTo(owner.Target.CenterPosition); if (leader == null) { return; } var ownUnits = owner.world.FindActorsInCircle(leader.CenterPosition, WRange.FromCells(owner.units.Count) / 3) .Where(a => a.Owner == owner.units.FirstOrDefault().Owner&& owner.units.Contains(a)).ToList(); if (ownUnits.Count < owner.units.Count) { owner.world.IssueOrder(new Order("Stop", leader, false)); foreach (var unit in owner.units.Where(a => !ownUnits.Contains(a))) { owner.world.IssueOrder(new Order("AttackMove", unit, false) { TargetLocation = leader.CenterPosition.ToCPos() }); } } else { var enemys = owner.world.FindActorsInCircle(leader.CenterPosition, WRange.FromCells(12)) .Where(a1 => !a1.Destroyed && !a1.IsDead()).ToList(); var enemynearby = enemys.Where(a1 => a1.HasTrait <ITargetable>() && leader.Owner.Stances[a1.Owner] == Stance.Enemy).ToList(); if (enemynearby.Any()) { owner.Target = enemynearby.ClosestTo(leader.CenterPosition); owner.fsm.ChangeState(owner, new GroundUnitsAttackState(), true); return; } else { foreach (var a in owner.units) { owner.world.IssueOrder(new Order("AttackMove", a, false) { TargetLocation = owner.Target.Location }); } } } if (ShouldFlee(owner)) { owner.fsm.ChangeState(owner, new GroundUnitsFleeState(), true); return; } }
public void Tick(Squad owner) { if (!owner.IsValid) return; foreach (var a in owner.units) { if (!ReloadsAutomatically(a) && !FullAmmo(a)) { if (IsRearm(a)) continue; owner.world.IssueOrder(new Order("ReturnToBase", a, false)); continue; } owner.world.IssueOrder(new Order("Move", a, false) { TargetLocation = RandomBuildingLocation(owner) }); } owner.fsm.ChangeState(owner, new AirIdleState(), true); }
protected virtual bool ShouldFlee(Squad owner) { return(base.ShouldFlee(owner, enemies => !owner.attackOrFleeFuzzy.CanAttack(owner.units, enemies))); }