Exemplo n.º 1
0
		public void Tick(Squad owner)
		{
			if (!owner.IsValid)
				return;

			if (!owner.IsTargetValid)
			{
				var t = owner.Bot.FindClosestEnemy(owner.Units.FirstOrDefault().CenterPosition);
				if (t == null) return;
				owner.TargetActor = t;
			}

			var enemyUnits = owner.World.FindActorsInCircle(owner.TargetActor.CenterPosition, WDist.FromCells(10))
				.Where(unit => owner.Bot.Player.Stances[unit.Owner] == Stance.Enemy).ToList();

			if (enemyUnits.Any())
			{
				if (owner.AttackOrFleeFuzzy.CanAttack(owner.Units, enemyUnits))
				{
					foreach (var u in owner.Units)
						owner.Bot.QueueOrder(new Order("AttackMove", u, false) { TargetLocation = owner.TargetActor.Location });

					// We have gathered sufficient units. Attack the nearest enemy unit.
					owner.FuzzyStateMachine.ChangeState(owner, new GroundUnitsAttackMoveState(), true);
					return;
				}
				else
					owner.FuzzyStateMachine.ChangeState(owner, new GroundUnitsFleeState(), true);
			}
		}
Exemplo n.º 2
0
 protected static CPos RandomBuildingLocation(Squad squad)
 {
     var location = squad.Bot.GetRandomBaseCenter();
     var buildings = squad.World.ActorsWithTrait<Building>()
         .Where(a => a.Actor.Owner == squad.Bot.Player).Select(a => a.Actor).ToList();
     if (buildings.Count > 0)
         location = buildings.Random(squad.Random).Location;
     return location;
 }
Exemplo n.º 3
0
        public void Tick(Squad owner)
        {
            if (!owner.IsValid)
                return;

            if (!owner.IsTargetValid)
            {
                var closestEnemy = owner.Bot.FindClosestEnemy(owner.Units.Random(owner.Random).CenterPosition);
                if (closestEnemy != null)
                    owner.TargetActor = closestEnemy;
                else
                {
                    owner.FuzzyStateMachine.ChangeState(owner, new GroundUnitsFleeState(), true);
                    return;
                }
            }

            var leader = owner.Units.ClosestTo(owner.TargetActor.CenterPosition);
            if (leader == null)
                return;
            var ownUnits = owner.World.FindActorsInCircle(leader.CenterPosition, WDist.FromCells(owner.Units.Count) / 3)
                .Where(a => a.Owner == owner.Units.FirstOrDefault().Owner && owner.Units.Contains(a)).ToHashSet();
            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, WDist.FromCells(12))
                    .Where(a1 => !a1.Disposed && !a1.IsDead);
                var enemynearby = enemies.Where(a1 => a1.HasTrait<ITargetable>() && leader.Owner.Stances[a1.Owner] == Stance.Enemy);
                var target = enemynearby.ClosestTo(leader.CenterPosition);
                if (target != null)
                {
                    owner.TargetActor = target;
                    owner.FuzzyStateMachine.ChangeState(owner, new GroundUnitsAttackState(), true);
                    return;
                }
                else
                    foreach (var a in owner.Units)
                        owner.World.IssueOrder(new Order("AttackMove", a, false) { TargetLocation = owner.TargetActor.Location });
            }

            if (ShouldFlee(owner))
            {
                owner.FuzzyStateMachine.ChangeState(owner, new GroundUnitsFleeState(), true);
                return;
            }
        }
Exemplo n.º 4
0
        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.TargetActor = closestEnemy;
                else
                {
                    owner.FuzzyStateMachine.ChangeState(owner, new AirFleeState(), true);
                    return;
                }
            }

            if (!NearToPosSafely(owner, owner.TargetActor.CenterPosition))
            {
                owner.FuzzyStateMachine.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.TargetActor.HasTrait<ITargetable>() && CanAttackTarget(a, owner.TargetActor))
                    owner.World.IssueOrder(new Order("Attack", a, false) { TargetActor = owner.TargetActor });
            }
        }
Exemplo n.º 5
0
		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, WDist.FromCells(DangerRadius)).ToList();
			var ownBaseBuildingAround = units.Where(unit => unit.Owner == squad.Bot.Player && unit.Info.HasTraitInfo<BuildingInfo>());
			if (ownBaseBuildingAround.Any())
				return false;

			var enemyAroundUnit = units.Where(unit => squad.Bot.Player.Stances[unit.Owner] == Stance.Enemy && unit.Info.HasTraitInfo<AttackBaseInfo>());
			if (!enemyAroundUnit.Any())
				return false;

			return flee(enemyAroundUnit);
		}
Exemplo n.º 6
0
		protected static CPos? FindSafePlace(Squad owner, out Actor detectedEnemyTarget, bool needTarget)
		{
			var map = owner.World.Map;
			detectedEnemyTarget = null;
			var x = (map.MapSize.X % DangerRadius) == 0 ? map.MapSize.X : map.MapSize.X + DangerRadius;
			var y = (map.MapSize.Y % DangerRadius) == 0 ? map.MapSize.Y : 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, map.CenterOfCell(pos), out detectedEnemyTarget))
					{
						if (needTarget && detectedEnemyTarget == null)
							continue;

						return pos;
					}
				}
			}

			return null;
		}
Exemplo n.º 7
0
        public void Tick(Squad owner)
        {
            if (!owner.IsValid)
                return;

            if (ShouldFlee(owner))
            {
                owner.FuzzyStateMachine.ChangeState(owner, new AirFleeState(), true);
                return;
            }

            var e = FindDefenselessTarget(owner);
            if (e == null)
                return;

            owner.TargetActor = e;
            owner.FuzzyStateMachine.ChangeState(owner, new AirAttackState(), true);
        }
Exemplo n.º 8
0
        public void Tick(Squad owner)
        {
            if (!owner.IsValid)
            {
                return;
            }

            if (!owner.IsTargetValid)
            {
                var closestEnemy = FindClosestEnemy(owner);
                if (closestEnemy != null)
                {
                    owner.TargetActor = closestEnemy;
                }
                else
                {
                    owner.FuzzyStateMachine.ChangeState(owner, new NavyUnitsFleeState(), true);
                    return;
                }
            }

            var leader = owner.Units.ClosestTo(owner.TargetActor.CenterPosition);

            if (leader == null)
            {
                return;
            }

            var ownUnits = owner.World.FindActorsInCircle(leader.CenterPosition, WDist.FromCells(owner.Units.Count) / 3)
                           .Where(a => a.Owner == owner.Units.First().Owner&& owner.Units.Contains(a)).ToHashSet();

            if (ownUnits.Count < owner.Units.Count)
            {
                // Since units have different movement speeds, they get separated while approaching the target.
                // Let them regroup into tighter formation.
                owner.Bot.QueueOrder(new Order("Stop", leader, false));
                foreach (var unit in owner.Units.Where(a => !ownUnits.Contains(a)))
                {
                    owner.Bot.QueueOrder(new Order("AttackMove", unit, false)
                    {
                        TargetLocation = leader.Location
                    });
                }
            }
            else
            {
                var enemies = owner.World.FindActorsInCircle(leader.CenterPosition, WDist.FromCells(12))
                              .Where(a => !a.IsDead && leader.Owner.Stances[a.Owner] == Stance.Enemy && a.Info.HasTraitInfo <ITargetableInfo>());
                var target = enemies.ClosestTo(leader.CenterPosition);
                if (target != null)
                {
                    owner.TargetActor = target;
                    owner.FuzzyStateMachine.ChangeState(owner, new NavyUnitsAttackState(), true);
                }
                else
                {
                    foreach (var a in owner.Units)
                    {
                        owner.Bot.QueueOrder(new Order("AttackMove", a, false)
                        {
                            TargetLocation = owner.TargetActor.Location
                        });
                    }
                }
            }

            if (ShouldFlee(owner))
            {
                owner.FuzzyStateMachine.ChangeState(owner, new NavyUnitsFleeState(), true);
            }
        }
Exemplo n.º 9
0
		Squad RegisterNewSquad(SquadType type, Actor target = null)
		{
			var ret = new Squad(this, type, target);
			squads.Add(ret);
			return ret;
		}
Exemplo n.º 10
0
 protected virtual bool ShouldFlee(Squad owner)
 {
     return(base.ShouldFlee(owner, enemies => !owner.AttackOrFleeFuzzy.CanAttack(owner.Units, enemies)));
 }
Exemplo n.º 11
0
 protected static void GoToRandomOwnBuilding(Squad squad)
 {
     var loc = RandomBuildingLocation(squad);
     foreach (var a in squad.Units)
         squad.Bot.QueueOrder(new Order("Move", a, false) { TargetLocation = loc });
 }
Exemplo n.º 12
0
        public void Tick(Squad owner)
        {
            if (!owner.IsValid)
            {
                return;
            }

            if (!owner.IsTargetValid)
            {
                var a            = owner.Units.Random(owner.Random);
                var closestEnemy = owner.Bot.FindClosestEnemy(a.CenterPosition);
                if (closestEnemy != null)
                {
                    owner.TargetActor = closestEnemy;
                }
                else
                {
                    owner.FuzzyStateMachine.ChangeState(owner, new AirFleeState(), true);
                    return;
                }
            }

            if (!NearToPosSafely(owner, owner.TargetActor.CenterPosition))
            {
                owner.FuzzyStateMachine.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.Bot.QueueOrder(new Order("ReturnToBase", a, false));
                        continue;
                    }

                    if (IsRearm(a))
                    {
                        continue;
                    }
                }

                if (owner.TargetActor.Info.HasTraitInfo <ITargetableInfo>() && CanAttackTarget(a, owner.TargetActor))
                {
                    owner.Bot.QueueOrder(new Order("Attack", a, false)
                    {
                        TargetActor = owner.TargetActor
                    });
                }
            }
        }
Exemplo n.º 13
0
 protected virtual bool ShouldFlee(Squad owner)
 {
     return base.ShouldFlee(owner, enemies => !owner.AttackOrFleeFuzzy.CanAttack(owner.Units, enemies));
 }
Exemplo n.º 14
0
 public void Deactivate(Squad owner)
 {
     owner.Units.Clear();
 }
Exemplo n.º 15
0
 public void Deactivate(Squad owner)
 {
 }
Exemplo n.º 16
0
        public void Tick(Squad owner)
        {
            if (!owner.IsValid)
            {
                return;
            }

            if (!owner.IsTargetValid)
            {
                var closestEnemy = owner.Bot.FindClosestEnemy(owner.Units.Random(owner.Random).CenterPosition);
                if (closestEnemy != null)
                {
                    owner.TargetActor = closestEnemy;
                }
                else
                {
                    owner.FuzzyStateMachine.ChangeState(owner, new GroundUnitsFleeState(), true);
                    return;
                }
            }

            var leader = owner.Units.ClosestTo(owner.TargetActor.CenterPosition);

            if (leader == null)
            {
                return;
            }
            var ownUnits = owner.World.FindActorsInCircle(leader.CenterPosition, WDist.FromCells(owner.Units.Count) / 3)
                           .Where(a => a.Owner == owner.Units.FirstOrDefault().Owner&& owner.Units.Contains(a)).ToHashSet();

            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, WDist.FromCells(12))
                              .Where(a1 => !a1.Disposed && !a1.IsDead);
                var enemynearby = enemies.Where(a1 => a1.HasTrait <ITargetable>() && leader.Owner.Stances[a1.Owner] == Stance.Enemy);
                var target      = enemynearby.ClosestTo(leader.CenterPosition);
                if (target != null)
                {
                    owner.TargetActor = target;
                    owner.FuzzyStateMachine.ChangeState(owner, new GroundUnitsAttackState(), true);
                    return;
                }
                else
                {
                    foreach (var a in owner.Units)
                    {
                        owner.World.IssueOrder(new Order("AttackMove", a, false)
                        {
                            TargetLocation = owner.TargetActor.Location
                        });
                    }
                }
            }

            if (ShouldFlee(owner))
            {
                owner.FuzzyStateMachine.ChangeState(owner, new GroundUnitsFleeState(), true);
                return;
            }
        }
Exemplo n.º 17
0
        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.Player.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;
        }
Exemplo n.º 18
0
        protected static bool NearToPosSafely(Squad owner, WPos loc)
        {
            Actor a;

            return(NearToPosSafely(owner, loc, out a));
        }
Exemplo n.º 19
0
 public void Deactivate(Squad owner)
 {
 }
Exemplo n.º 20
0
 public void Activate(Squad owner)
 {
 }
Exemplo n.º 21
0
 protected static Actor FindDefenselessTarget(Squad owner)
 {
     Actor target = null;
     FindSafePlace(owner, out target, true);
     return target;
 }
 public void Tick(Squad owner)
 {
     owner.FuzzyStateMachine.ChangeState(owner, new UnitsForProtectionAttackState(), true);
 }
Exemplo n.º 23
0
 protected static bool NearToPosSafely(Squad owner, WPos loc)
 {
     Actor a;
     return NearToPosSafely(owner, loc, out a);
 }
Exemplo n.º 24
0
 // 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));
 }
Exemplo n.º 25
0
 // 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);
 }
Exemplo n.º 26
0
        public void Tick(Squad owner)
        {
            if (!owner.IsValid)
                return;

            if (!owner.IsTargetValid)
            {
                var closestEnemy = owner.Bot.FindClosestEnemy(owner.Units.Random(owner.Random).CenterPosition);
                if (closestEnemy != null)
                    owner.TargetActor = closestEnemy;
                else
                {
                    owner.FuzzyStateMachine.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.FuzzyStateMachine.ChangeState(owner, new GroundUnitsFleeState(), true);
                return;
            }
        }
Exemplo n.º 27
0
 public void Activate(Squad owner)
 {
 }
Exemplo n.º 28
0
 public void Deactivate(Squad owner)
 {
     owner.Units.Clear();
 }
Exemplo n.º 29
0
        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.FuzzyStateMachine.ChangeState(owner, new AirIdleState(), true);
        }
Exemplo n.º 30
0
        public void Tick(Squad owner)
        {
            if (!owner.IsValid)
                return;

            GoToRandomOwnBuilding(owner);
            owner.FuzzyStateMachine.ChangeState(owner, new GroundUnitsIdleState(), true);
        }