Beispiel #1
0
        internal override bool Enter()
        {
            moveTimer = Owner.WalkSpeed;
             walkDistance = Owner.WalkSpeed;
             curNodeIdx = -1;
             Path = Owner.CurrentMap.CalcPath(Owner.TileX, Owner.TileY, targetTileX, targetTileY);

             startPosX = Owner.X;
             startPosY = Owner.Y;

             if (Path != null)
             {
            curNodeIdx = 0;

            IMapPathNode initialNode = Path[curNodeIdx++];
            if (initialNode == null)
               return false;

            targetPosX = initialNode.X;
            targetPosY = initialNode.Y;

            if (Owner is Unit)
            {
               Unit unit = (Unit)Owner;
               unit.Sprite.SetCurrentAnimationByName("Walk");
               unit.Orientation = Unit.OrientationFromDiff((targetPosX - startPosX), (targetPosY - startPosY));
            }
             }
             else
             {
            return false;
             }

             return true;
        }
Beispiel #2
0
 internal StateAttackMove(Entity Owner, int X, int Y)
     : base(Owner)
 {
     Path = null;
      TargetPath = null;
      destX = X;
      destY = Y;
      bUpdatePath = true;
 }
Beispiel #3
0
 internal StateAttackMove(Entity Owner, int X, int Y)
     : base(Owner)
 {
     Path        = null;
     TargetPath  = null;
     destX       = X;
     destY       = Y;
     bUpdatePath = true;
 }
Beispiel #4
0
        internal StateAttack(Entity Owner, Entity Target)
            : base(Owner)
        {
            if (Target == null)
            throw new ArgumentNullException("Target");

             Owner.CurrentTarget = Target;
             Path = null;
        }
Beispiel #5
0
        internal StateAttack(Entity Owner, Entity Target)
            : base(Owner)
        {
            if (Target == null)
            {
                throw new ArgumentNullException("Target");
            }

            Owner.CurrentTarget = Target;
            Path = null;
        }
Beispiel #6
0
        internal override void Update(GameTime gameTime)
        {
            moveTimer -= gameTime.ElapsedGameTime.TotalSeconds;
            if (moveTimer > 0)
            {
                return;
            }

            moveTimer = 1.0f;

            this.Owner.UpdateHateList();

            HateListEntry entry = this.Owner.HateList.GetHighestHateListEntry();

            if (entry.Target == null)
            {
                Log.AI(this.Owner?.ToString(), "No enemy ... moving!");

                if (bUpdatePath)
                {
                    Path = Owner.CurrentMap.CalcPath(Owner.TileX, Owner.TileY, destX, destY);
                    if (Path != null)
                    {
                        curNodeIdx  = 0;
                        bUpdatePath = false;
                    }
                }

                if (curNodeIdx == -1 || Path == null)
                {
                    return;
                }

                if (curNodeIdx >= Path.Count)
                {
                    this.Owner.Idle();
                    return;
                }

                IMapPathNode node = Path[curNodeIdx++];
                // TODO!!! Move, not Set
                Owner.SetPosition(node.X, node.Y);
                return;
            }

            this.Owner.CurrentTarget = entry.Target;

            Log.AI(this.Owner?.ToString(), "Enemy spotted! Attacking " + entry.Target.Name + entry.Target.UniqueID);

            bUpdatePath = true;
            Path        = null;

            MoveOrAttack(entry.Target);
        }
Beispiel #7
0
        internal override bool Enter()
        {
            moveTimer = 1.0f;
             curNodeIdx = -1;
             Path = Owner.CurrentMap.CalcPath(Owner.TileX, Owner.TileY, destX, destY);
             if (Path == null)
            return false;

             curNodeIdx = 0;
             bUpdatePath = false;

             return true;
        }
Beispiel #8
0
        private void MoveOrAttack(Entity ent)
        {
            float offx = ent.X - this.Owner.X;
            float offy = ent.Y - this.Owner.Y;

            double sqr_dist = (offx * offx + offy * offy);

            double sqr_meleerange = ent.AttackRange * ent.AttackRange;

            if (sqr_dist < sqr_meleerange)
            {
                // Target is in range -> Perform an attack
                if (this.Owner.PerformAttack(ent))
                {
                    if (this.Owner is Unit)
                    {
                        Unit unit = (Unit)this.Owner;
                        unit.Orientation = Unit.OrientationFromDiff(offx, offy);
                        unit.Sprite.CurrentAnimation.Reset();
                    }
                }
            }
            else
            {
                // Target is out of range -> Move towards it

                // If no path has been calculated yet or if the target has moved, calculate new path
                if (Path == null || ent.TileX != targetX || ent.TileY != targetY)
                {
                    targetX = ent.TileX;
                    targetY = ent.TileY;
                    Path    = Owner.CurrentMap.CalcPath(Owner.TileX, Owner.TileY, targetX, targetY);
                    if (Path == null)
                    {
                        return;
                    }
                    curNodeIdx = 0;
                }

                if (curNodeIdx < Path.Count)
                {
                    IMapPathNode node = Path[curNodeIdx++];
                    // TODO!!! Move, not Set
                    Owner.SetPosition(node.X, node.Y);
                }
                else
                {
                    Log.Error("Error calculating path");
                }
            }
        }
Beispiel #9
0
        internal override void Update(GameTime gameTime)
        {
            moveTimer -= gameTime.ElapsedGameTime.TotalSeconds;
             if (moveTimer > 0)
            return;

             moveTimer = 1.0f;

             this.Owner.UpdateHateList();

             HateListEntry entry = this.Owner.HateList.GetHighestHateListEntry();
             if (entry.Target == null)
             {
            Log.AI(this.Owner, "No enemy ... moving!");

            if (bUpdatePath)
            {
               Path = Owner.CurrentMap.CalcPath(Owner.TileX, Owner.TileY, destX, destY);
               if (Path != null)
               {
                  curNodeIdx = 0;
                  bUpdatePath = false;
               }
            }

            if (curNodeIdx == -1 || Path == null)
               return;

            if (curNodeIdx >= Path.Count)
            {
               this.Owner.Idle();
               return;
            }

            IMapPathNode node = Path[curNodeIdx++];
            // TODO!!! Move, not Set
            Owner.SetPosition (node.X, node.Y);
            return;
             }

             this.Owner.CurrentTarget = entry.Target;

             Log.AI(this.Owner, "Enemy spotted! Attacking " + entry.Target.Name + entry.Target.UniqueID);

             bUpdatePath = true;
             Path = null;

             MoveOrAttack(entry.Target);
        }
Beispiel #10
0
        internal override bool Enter()
        {
            moveTimer  = 1.0f;
            curNodeIdx = -1;
            Path       = Owner.CurrentMap.CalcPath(Owner.TileX, Owner.TileY, destX, destY);
            if (Path == null)
            {
                return(false);
            }

            curNodeIdx  = 0;
            bUpdatePath = false;

            return(true);
        }
Beispiel #11
0
        private void MoveOrAttack(Entity ent)
        {
            float offx = this.Owner.X - ent.X;
            float offy = this.Owner.Y - ent.Y;

            double sqr_dist = (offx * offx + offy * offy);

            double sqr_meleerange = ent.AttackRange * ent.AttackRange;

            if (sqr_dist < sqr_meleerange)
            {
                if (this.Owner.CanAttack && ent.ShouldBeAttacked)
                {
                    // Target is in range -> Perform an attack
                    this.Owner.PerformAttack(ent);
                }
            }
            else
            {
                // Target is out of range -> Move towards it

                Log.AI(this.Owner?.ToString(), "Target is out of range ... moving towards it!");

                // If no path has been calculated yet or if the target has moved, calculate new path
                if (TargetPath == null || ent.X != targetX || ent.Y != targetY)
                {
                    targetX    = ent.TileX;
                    targetY    = ent.TileY;
                    TargetPath = Owner.CurrentMap.CalcPath(Owner.TileX, Owner.TileY, targetX, targetY);
                    if (TargetPath == null)
                    {
                        return;
                    }
                    curNodeIdx = 0;
                }

                IMapPathNode node = TargetPath[curNodeIdx++];
                // TODO!!! Move, not Set
                Owner.SetPosition(node.X, node.Y);
            }
        }
Beispiel #12
0
        internal override bool Enter()
        {
            moveTimer    = Owner.WalkSpeed;
            walkDistance = Owner.WalkSpeed;
            curNodeIdx   = -1;
            Path         = Owner.CurrentMap.CalcPath(Owner.TileX, Owner.TileY, targetTileX, targetTileY);

            startPosX = Owner.X;
            startPosY = Owner.Y;

            if (Path != null)
            {
                curNodeIdx = 0;

                IMapPathNode initialNode = Path[curNodeIdx++];
                if (initialNode == null)
                {
                    return(false);
                }

                targetPosX = initialNode.X;
                targetPosY = initialNode.Y;

                if (Owner is Unit)
                {
                    Unit unit = (Unit)Owner;
                    unit.Sprite.SetCurrentAnimationByName("Walk");
                    unit.Orientation = Unit.OrientationFromDiff((targetPosX - startPosX), (targetPosY - startPosY));
                }
            }
            else
            {
                return(false);
            }

            return(true);
        }
Beispiel #13
0
        internal MapPath CalcPath(int startX, int startY, int endX, int endY)
        {
            Performance.Push("Calculate path from " + startX + "," + startY + " to " + endX + "," + endY);
            try
            {
                Log.Status("Map: Calculating path from " + startX + "," +
                           startY + " to " + endX + "," + endY + "...");

                MapPath path = Pathfinder.FindPath(startX, startY, endX, endY);
                if (path != null)
                {
                    Log.Status("... success (" + path.Count + " Nodes)!");
                    return(path);
                }

                Log.Status("... failed!");

                return(null);
            }
            finally
            {
                Performance.Pop();
            }
        }
Beispiel #14
0
        private void MoveOrAttack(Entity ent)
        {
            float offx = this.Owner.X - ent.X;
             float offy = this.Owner.Y - ent.Y;

             double sqr_dist = (offx * offx + offy * offy);

             double sqr_meleerange = ent.AttackRange * ent.AttackRange;

             if (sqr_dist < sqr_meleerange)
             {
            if (this.Owner.CanAttack && ent.ShouldBeAttacked)
            {
               // Target is in range -> Perform an attack
               this.Owner.PerformAttack(ent);
            }
             }
             else
             {
            // Target is out of range -> Move towards it

            Log.AI(this.Owner, "Target is out of range ... moving towards it!");

            // If no path has been calculated yet or if the target has moved, calculate new path
            if (TargetPath == null || ent.X != targetX || ent.Y != targetY)
            {
               targetX = ent.TileX;
               targetY = ent.TileY;
               TargetPath = Owner.CurrentMap.CalcPath(Owner.TileX, Owner.TileY, targetX, targetY);
               if (TargetPath == null)
                  return;
               curNodeIdx = 0;
            }

            IMapPathNode node = TargetPath[curNodeIdx++];
            // TODO!!! Move, not Set
            Owner.SetPosition (node.X, node.Y);
             }
        }
Beispiel #15
0
        private void MoveOrAttack(Entity ent)
        {
            float offx = ent.X - this.Owner.X;
             float offy = ent.Y - this.Owner.Y;

             double sqr_dist = (offx * offx + offy * offy);

             double sqr_meleerange = ent.AttackRange * ent.AttackRange;

             if (sqr_dist < sqr_meleerange)
             {
            // Target is in range -> Perform an attack
            if (this.Owner.PerformAttack (ent))
            {
               if (this.Owner is Unit)
               {
                  Unit unit = (Unit)this.Owner;
                  unit.Orientation = Unit.OrientationFromDiff (offx, offy);
                  unit.Sprite.CurrentAnimation.Reset ();
               }
            }
             }
             else
             {
            // Target is out of range -> Move towards it

            // If no path has been calculated yet or if the target has moved, calculate new path
            if (Path == null || ent.TileX != targetX || ent.TileY != targetY)
            {
               targetX = ent.TileX;
               targetY = ent.TileY;
               Path = Owner.CurrentMap.CalcPath(Owner.TileX, Owner.TileY, targetX, targetY);
               if (Path == null)
                  return;
               curNodeIdx = 0;
            }

            if (curNodeIdx < Path.Count)
            {
               IMapPathNode node = Path[curNodeIdx++];
               // TODO!!! Move, not Set
               Owner.SetPosition (node.X, node.Y);
            }
            else
               Log.Error("Error calculating path");
             }
        }
Beispiel #16
0
        public MapPath FindPath(int startX, int startY, int endX, int endY, bool useHeuristic = true)
        {
            if (field == null)
            {
                return(null);
            }

            UseHeuristic = useHeuristic;

            List <AStarNode>       Closed   = new List <AStarNode>();
            BinaryHeap <AStarNode> OpenHeap = new BinaryHeap <AStarNode>(fieldWidth * fieldHeight);
            AStarNode Root;

            Root        = new AStarNode();
            Root.parent = null;
            Root.X      = startX;
            Root.Y      = startY;
            Root.G      = 0;
            int offX = Math.Abs(Root.X - endX);
            int offY = Math.Abs(Root.Y - endY);

            if (UseHeuristic == false)
            {
                Root.H = 0;
            }
            else
            {
                if (offX > offY)
                {
                    Root.H = 14 * offY + 10 * (offX - offY);
                }
                else
                {
                    Root.H = 14 * offX + 10 * (offY - offX);
                }
            }
            OpenHeap.Add(Root.F, Root);

            int res = ProcessLowestNode(endX, endY, Closed, OpenHeap);

            while (res == 0)
            {
                res = ProcessLowestNode(endX, endY, Closed, OpenHeap);
            }

            if (res == -1)
            {
                return(null);
            }

            AStarNode node = null;

            if (res == 1)
            {
                node = GetClosedNode(endX, endY, Closed);
            }
            else if (res == 2)
            {
                node = Closed[Closed.Count - 1];
            }

            MapPath result = new MapPath();

            result.BuildFromFinalAStarNode(node);

            return(result);
        }
Beispiel #17
0
        public MapPath FindPath(int startX, int startY, int endX, int endY, bool useHeuristic = true)
        {
            if (field == null)
                return null;

             UseHeuristic = useHeuristic;

             List<AStarNode> Closed = new List<AStarNode>();
             BinaryHeap<AStarNode> OpenHeap = new BinaryHeap<AStarNode>(fieldWidth * fieldHeight);
             AStarNode Root;

            Root = new AStarNode();
            Root.parent = null;
            Root.X = startX;
            Root.Y = startY;
            Root.G = 0;
            int offX = Math.Abs(Root.X - endX);
            int offY = Math.Abs(Root.Y - endY);
             if (UseHeuristic == false)
             {
            Root.H = 0;
             }
             else
             {
            if (offX > offY)
               Root.H = 14 * offY + 10 * (offX - offY);
            else
               Root.H = 14 * offX + 10 * (offY - offX);
             }
            OpenHeap.Add(Root.F, Root);

             int res = ProcessLowestNode (endX, endY, Closed, OpenHeap);
            while (res == 0)
            {
                res = ProcessLowestNode(endX, endY, Closed, OpenHeap);
            }

            if (res == -1)
                return null;

             AStarNode node = null;
             if (res == 1)
            node = GetClosedNode(endX, endY, Closed);
             else if (res == 2)
            node = Closed[Closed.Count - 1];

             MapPath result = new MapPath ();
             result.BuildFromFinalAStarNode (node);

             return result;
        }