Esempio n. 1
0
        /// <summary>
        /// Execute the mining
        /// </summary>
        public void Execute()
        {
            if (this.currentBlock != null)
            {
                // check if still not mined and still marked -> mine!
                //if (!this.currentBlock.IsMined && FenrirGame.Instance.WorldComponents.World.IsMarked(this.currentBlock.GridPosition))
                if (!this.currentBlock.IsMined)
                {
                    Point unitPosition = FenrirGame.Instance.InGame.Scene.PixelPositionToTilePosition(this.executingUnit.Position);
                    float divX         = Math.Abs(this.currentBlock.GridPosition.X - unitPosition.X);
                    float divY         = Math.Abs(this.currentBlock.GridPosition.Y - unitPosition.Y);

                    // near enough to build?
                    if (divX <= 1 && divY <= 1)
                    {
                        this.currentBlock.Mine(this.executingUnit.Stats.Mining / 10 + 1.0f);
                        this.executingUnit.Stats.Mining++;
                    }
                    else
                    {
                        // we run in an error here -> end this task
                        FenrirGame.Instance.Log(LogLevel.Error, "try to mine a block out of reach!");
                        this.executingUnit.FinishCurrentTask();
                    }
                }
                else
                {
                    // check if we have an adjacent block to the current block that is marked
                    Point markedTop    = new Point(this.currentBlock.GridPosition.X, this.currentBlock.GridPosition.Y + 1);
                    Point markedBottom = new Point(this.currentBlock.GridPosition.X, this.currentBlock.GridPosition.Y - 1);
                    Point markedLeft   = new Point(this.currentBlock.GridPosition.X + 1, this.currentBlock.GridPosition.Y);
                    Point markedRight  = new Point(this.currentBlock.GridPosition.X - 1, this.currentBlock.GridPosition.Y);

                    Point?mineTarget = null;

                    if (FenrirGame.Instance.InGame.Scene.IsMarked(this.currentBlock.GridPosition))
                    {
                        mineTarget = this.currentBlock.GridPosition;
                    }
                    else if (FenrirGame.Instance.InGame.Scene.IsMarked(markedLeft))
                    {
                        mineTarget = markedLeft;
                    }
                    else if (FenrirGame.Instance.InGame.Scene.IsMarked(markedRight))
                    {
                        mineTarget = markedRight;
                    }
                    else if (FenrirGame.Instance.InGame.Scene.IsMarked(markedTop))
                    {
                        mineTarget = markedTop;
                    }
                    else if (FenrirGame.Instance.InGame.Scene.IsMarked(markedBottom))
                    {
                        mineTarget = markedBottom;
                    }

                    // do we have a marked one?
                    if (mineTarget.HasValue)
                    {
                        this.executingUnit.AddProrizedTask(new TaskMove(this.currentBlock.GridPosition));
                        this.currentBlock = FenrirGame.Instance.InGame.Scene.GetBlock(mineTarget.Value);
                    }
                    else
                    {
                        // check if still something to mine
                        if (this.blocks.Count > 0)
                        {
                            this.moveToNextBlock();
                        }
                        else
                        {
                            this.executingUnit.FinishCurrentTask();
                        }
                    }
                }
            }
            else
            {
                this.executingUnit.FinishCurrentTask();
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Execute the mining
        /// </summary>
        public void Execute()
        {
            if (this.currentBlock != null)
            {
                // check if still not mined and still marked -> mine!
                //if (!this.currentBlock.IsMined && FenrirGame.Instance.WorldComponents.World.IsMarked(this.currentBlock.GridPosition))
                if (!this.currentBlock.IsMined)
                {
                    Point unitPosition = FenrirGame.Instance.InGame.Scene.PixelPositionToTilePosition(this.executingUnit.Position);
                    float divX = Math.Abs(this.currentBlock.GridPosition.X - unitPosition.X);
                    float divY = Math.Abs(this.currentBlock.GridPosition.Y - unitPosition.Y);

                    // near enough to build?
                    if (divX <= 1 && divY <= 1)
                    {
                        this.currentBlock.Mine(this.executingUnit.Stats.Mining / 10 + 1.0f);
                        this.executingUnit.Stats.Mining++;
                    }
                    else
                    {
                        // we run in an error here -> end this task
                        FenrirGame.Instance.Log(LogLevel.Error, "try to mine a block out of reach!");
                        this.executingUnit.FinishCurrentTask();
                    }
                }
                else
                {
                    // check if we have an adjacent block to the current block that is marked
                    Point markedTop = new Point(this.currentBlock.GridPosition.X, this.currentBlock.GridPosition.Y + 1);
                    Point markedBottom = new Point(this.currentBlock.GridPosition.X, this.currentBlock.GridPosition.Y - 1);
                    Point markedLeft = new Point(this.currentBlock.GridPosition.X + 1, this.currentBlock.GridPosition.Y);
                    Point markedRight = new Point(this.currentBlock.GridPosition.X - 1, this.currentBlock.GridPosition.Y);

                    Point? mineTarget = null;

                    if (FenrirGame.Instance.InGame.Scene.IsMarked(this.currentBlock.GridPosition))
                        mineTarget = this.currentBlock.GridPosition;
                    else if (FenrirGame.Instance.InGame.Scene.IsMarked(markedLeft))
                        mineTarget = markedLeft;
                    else if (FenrirGame.Instance.InGame.Scene.IsMarked(markedRight))
                        mineTarget = markedRight;
                    else if (FenrirGame.Instance.InGame.Scene.IsMarked(markedTop))
                        mineTarget = markedTop;
                    else if (FenrirGame.Instance.InGame.Scene.IsMarked(markedBottom))
                        mineTarget = markedBottom;

                    // do we have a marked one?
                    if (mineTarget.HasValue)
                    {
                        this.executingUnit.AddProrizedTask(new TaskMove(this.currentBlock.GridPosition));
                        this.currentBlock = FenrirGame.Instance.InGame.Scene.GetBlock(mineTarget.Value);
                    }
                    else
                    {
                        // check if still something to mine
                        if (this.blocks.Count > 0)
                            this.moveToNextBlock();
                        else
                            this.executingUnit.FinishCurrentTask();
                    }
                }
            }
            else
                this.executingUnit.FinishCurrentTask();
        }
Esempio n. 3
0
        /// <summary>
        /// move the unit to the next block
        /// </summary>
        private void moveToNextBlock()
        {
            // filter blocks that have been mined
            List <Point> reducedGroup = new List <Point>();

            foreach (Point entry in this.blocks)
            {
                if (FenrirGame.Instance.InGame.Scene.IsMarked(entry))
                {
                    reducedGroup.Add(entry);
                }
            }

            this.blocks = reducedGroup;

            // find posibble starting blocks
            // entry point -> mining block
            Dictionary <Point, Point> entryPoints = new Dictionary <Point, Point>();

            Point[] pointer = { new Point(), new Point(), new Point(), new Point() };

            foreach (Point pos in reducedGroup)
            {
                pointer[0].Y = pos.Y;
                pointer[0].X = pos.X - 1;

                pointer[1].Y = pos.Y;
                pointer[1].X = pos.X + 1;

                pointer[2].Y = pos.Y - 1;
                pointer[2].X = pos.X;

                pointer[3].Y = pos.Y + 1;
                pointer[3].X = pos.X;

                foreach (Point possibleStartingPoint in pointer)
                {
                    Components.TileState state = FenrirGame.Instance.InGame.Scene.GetBlockState(possibleStartingPoint);
                    if ((state == Components.TileState.Walkable || state == Components.TileState.Climbable) && !entryPoints.ContainsKey(possibleStartingPoint))
                    {
                        entryPoints.Add(possibleStartingPoint, pos);
                    }
                }
            }

            if (entryPoints.Count > 0)
            {
                LinkedList <Point> route = FenrirGame.Instance.InGame.Scene.getPath(FenrirGame.Instance.InGame.Scene.PixelPositionToTilePosition(this.executingUnit.Position), new List <Point>(entryPoints.Keys));

                if (route != null && route.Count > 0)
                {
                    foreach (Point key in entryPoints.Keys)
                    {
                        if (key.X == route.Last.Value.X && key.Y == route.Last.Value.Y)
                        {
                            this.currentBlock = FenrirGame.Instance.InGame.Scene.GetBlock(entryPoints[key]);
                            this.executingUnit.AddProrizedTask(new TaskMove(key));
                            return;
                        }
                    }
                }
            }

            // no block to reach - cancel quest
            this.currentBlock = null;
        }
Esempio n. 4
0
        /// <summary>
        /// move the unit to the next block
        /// </summary>
        private void moveToNextBlock()
        {
            // filter blocks that have been mined
            List<Point> reducedGroup = new List<Point>();

            foreach (Point entry in this.blocks)
                if (FenrirGame.Instance.InGame.Scene.IsMarked(entry))
                    reducedGroup.Add(entry);

            this.blocks = reducedGroup;

            // find posibble starting blocks
            // entry point -> mining block
            Dictionary<Point, Point> entryPoints = new Dictionary<Point, Point>();
            Point[] pointer = { new Point(), new Point(), new Point(), new Point() };

            foreach (Point pos in reducedGroup)
            {
                pointer[0].Y = pos.Y;
                pointer[0].X = pos.X - 1;

                pointer[1].Y = pos.Y;
                pointer[1].X = pos.X + 1;

                pointer[2].Y = pos.Y - 1;
                pointer[2].X = pos.X;

                pointer[3].Y = pos.Y + 1;
                pointer[3].X = pos.X;

                foreach (Point possibleStartingPoint in pointer)
                {
                    Components.TileState state = FenrirGame.Instance.InGame.Scene.GetBlockState(possibleStartingPoint);
                    if ((state == Components.TileState.Walkable || state == Components.TileState.Climbable) && !entryPoints.ContainsKey(possibleStartingPoint))
                    {
                        entryPoints.Add(possibleStartingPoint, pos);
                    }
                }
            }

            if (entryPoints.Count > 0)
            {
                LinkedList<Point> route = FenrirGame.Instance.InGame.Scene.getPath(FenrirGame.Instance.InGame.Scene.PixelPositionToTilePosition(this.executingUnit.Position), new List<Point>(entryPoints.Keys));

                if(route != null && route.Count > 0)
                    foreach(Point key in entryPoints.Keys)
                        if (key.X == route.Last.Value.X && key.Y == route.Last.Value.Y)
                        {
                            this.currentBlock = FenrirGame.Instance.InGame.Scene.GetBlock(entryPoints[key]);
                            this.executingUnit.AddProrizedTask(new TaskMove(key));
                            return;
                        }
            }

            // no block to reach - cancel quest
            this.currentBlock = null;
        }