Beispiel #1
0
        private AnimalActor findClosestEnemy()
        {
            AnimalActor closestEnemy     = null;
            double      closestEnemyDist = double.PositiveInfinity;

            // We prioritize units by their proximity to the position
            foreach (Engine.Actor act in platoon.world.getActorsInCone(posVec, attackRadius * 32, new Engine.Vector2(0, 0), 360))
            {
                if (!(act is AnimalActor))
                {
                    continue;
                }

                AnimalActor target = (AnimalActor)act;
                Console.WriteLine("Found animal actor at (" + target.position.x + "," + target.position.y + "), team is " + target.team);
                if (target.team == platoon.team)
                {
                    continue;
                }


                // Figure out if this enemey is closer to the the tile.
                double dist = position.manhattan((GameTile)target.curTile);
                Console.WriteLine("Found enemy at (" + target.position.x + "," + target.position.y + "), distance from center is " + dist);
                if (dist < closestEnemyDist)
                {
                    closestEnemyDist = dist;
                    closestEnemy     = target;
                }
            }
            return(closestEnemy);
        }
        // Move the camera with a nice jump for the user to see
        protected bool moveUnit(AnimalActor unit, GameTile target)
        {
            if (!unit.canMoveTo(target))
            {
                return(false);
            }

            platoon.world.cameraManager.moveCamera(unit.position, true);
            //Thread.Sleep(CAM_SLEEP_TIME);
            for (int i = 0; i < 30; i++)
            {
                platoon.world.engine.graphicsComponent.draw();
                Thread.Sleep(CAM_SLEEP_TIME);
                platoon.world.Update();
            }

            unit.moveTile(target);
            platoon.world.engine.graphicsComponent.draw();
            // Sleep to let the user adjust
            //Thread.Sleep(CAM_SLEEP_TIME);
            for (int i = 0; i < 30; i++)
            {
                platoon.world.engine.graphicsComponent.draw();
                Thread.Sleep(CAM_SLEEP_TIME);
                platoon.world.Update();
            }
            return(true);
        }
 /*
  * Surround whatever we can find within a square w/ radius radius centered on center.
  */
 public SurroundOrder2(Platoon platoon, GameTile center, int radius)
     : base(platoon)
 {
     this.target = null;
     radius     *= GameTile.size;
     zone        = new RectangleF(center.x - radius, center.y - radius, 2 * radius, 2 * radius);
 }
        public override void execute()
        {
            foreach (AnimalActor aiActor in platoon.units)
            {
                GameTile best      = null;
                double   best_dist = double.NegativeInfinity;
                foreach (GameTile target in aiActor.findPaths())
                {
                    // Check that the target tile is empty
                    AnimalActor actor_at_target = platoon.world.getAnimalOnTile(target);
                    if (actor_at_target != null)
                    {
                        continue;
                    }

                    double dist = 0;
                    foreach (Engine.Actor enemyActor in platoon.world.actors)
                    {
                        // Ignore the player in the same team
                        if (enemyActor.color == aiActor.color)
                        {
                            continue;
                        }

                        //
                        dist += Math.Log10(Math.Abs(enemyActor.curTile.x - target.x) + Math.Abs(enemyActor.curTile.y - target.y));
                    }
                    if (dist > best_dist)
                    {
                        best = target;
                    }
                }
                moveUnit(aiActor, best);
            }
        }
Beispiel #5
0
 public void addUnit(AnimalActor unit)
 {
     if (!units.Contains(unit))
     {
         Console.WriteLine("Adding unit at " + unit.position);
         units.Add(unit);
         _unitListChanged = true;
     }
 }
        private void attack(Dictionary <AnimalActor, Dictionary <GameTile, List <AnimalActor> > > targetList)
        {
            AnimalActor        maxEnemy       = null;
            List <GameTile>    maxTiles       = null;
            int                maxAtackNum    = 0;
            List <AnimalActor> maxArrangement = null;

            List <AnimalActor> enemies = new List <AnimalActor>(targetList.Keys);

            for (int i = 0; i < enemies.Count; i++)
            {
                List <List <AnimalActor> > arrangements;
                List <GameTile>            adjTiles     = new List <GameTile>(targetList[enemies[i]].Keys);
                List <List <AnimalActor> > adjTileUnits = new List <List <AnimalActor> >();
                foreach (GameTile tile in adjTiles)
                {
                    adjTileUnits.Add(targetList[enemies[i]][tile]);
                }


                arrangements = recurse(adjTileUnits, null, null);
                if (arrangements == null)
                {
                    continue;
                }
                foreach (List <AnimalActor> arrangement in arrangements)
                {
                    int uniqueCount = countUnique(arrangement);
                    if (uniqueCount > maxAtackNum)
                    {
                        maxEnemy       = enemies[i];
                        maxTiles       = adjTiles;
                        maxArrangement = arrangement;
                        maxAtackNum    = uniqueCount;
                    }
                }
            }

            if (maxArrangement == null)
            {
                return;
            }

            //Attack the max enemy
            List <AnimalActor> moved = new List <AnimalActor>();

            for (int i = 0; i < maxArrangement.Count; i++)
            {
                if (!moved.Contains(maxArrangement[i]))
                {
                    moveUnit(maxArrangement[i], maxTiles[i]);
                    maxArrangement[i].attackTile(maxEnemy.curTile);
                    moved.Add(maxArrangement[i]);
                }
            }
        }
        /*
         * Given an enemy unit and our list of teamTiles, build a list of tiles adjacent to that enemy and which of our units can move there
         */
        private Dictionary <GameTile, List <AnimalActor> > buildAdjList(AnimalActor target, Dictionary <GameTile, List <AnimalActor> > teamTiles)
        {
            Dictionary <GameTile, List <AnimalActor> > adjTileMap = new Dictionary <GameTile, List <AnimalActor> >();

            foreach (GameTile tile in target.curTile.adjacent)
            {
                if (tile == null)
                {
                    continue;
                }
                if (teamTiles.ContainsKey(tile))
                {
                    adjTileMap.Add(tile, teamTiles[tile]);
                }
            }
            return(adjTileMap);
        }
        private void moveRemainingUnit(AnimalActor actor)
        {
            AnimalActor target      = null;
            int         minTeamSize = 999;

            foreach (var team in TeamDictionary.TeamDict.Values)
            {
                if (team.IsActive &&
                    team.Color != platoon.units.First().team &&
                    team.ActorList.Count() < minTeamSize)
                {
                    minTeamSize = team.ActorList.Count();
                    target      = team.ActorList.First();
                }
            }

            if (target == null)
            {
                return;
            }

            var possibleMoves = actor.findPaths();
            var lastPos       = actor.curTile;

            foreach (var tile in possibleMoves)
            {
                if ((target.curTile as GameTile).euclidian(actor.curTile as GameTile) <
                    (tile as GameTile).euclidian(actor.curTile as GameTile))
                {
                    if (moveUnit(actor, tile as GameTile))
                    {
                        break;
                    }
                }
            }

            foreach (var tile in possibleMoves)
            {
                if (actor.curTile != lastPos)
                {
                    break;
                }
                moveUnit(actor, tile as GameTile);
            }
        }
Beispiel #9
0
        public override void execute()
        {
            Console.WriteLine("Executing hold position order");

            if (platoon.unitListChanged)
            {
                updatePosition();
            }

            AnimalActor closestEnemy = findClosestEnemy();

            if (closestEnemy != null)
            {
                // Code to surround and attack that enemy here.  Will probably use Anubhaw and Andrew's Surround Order.
                AttackOrder attack = new AttackOrder(platoon, closestEnemy);
                attack.execute();
            }

            // Cluster any remaining unmoved units
            cluster.execute();
        }
 public void addUnit(AnimalActor unit)
 {
     platoon.addUnit(unit);
 }
Beispiel #11
0
 public AttackOrder(Platoon platoon, AnimalActor enemy) : base(platoon)
 {
     this.enemy = enemy;
 }
 /*
  * Surround, either whatever we can find or a particular unit
  */
 public SurroundOrder2(Platoon platoon, AnimalActor target = null)
     : base(platoon)
 {
     this.target = target;
     this.zone   = null;
 }