Ejemplo n.º 1
0
        /// <summary>
        /// Check all possible spawn locations around a building.
        /// Check surrounding area around the building for units, if there is a free location this will be returned.
        /// </summary>
        /// <returns>The free field.</returns>
        /// <param name="position">IPosition of the current selected building..</param>
        /// <param name="regionManagerC">Region manager c.</param>
        private PositionI GetFreeField(PositionI position, RegionManagerController regionManagerC)
        {
            foreach (var surpos in LogicRules.GetSurroundedFields(position))
            {
                var td = regionManagerC.GetRegion(surpos.RegionPosition).GetTerrain(surpos.CellPosition);
                var ed = regionManagerC.GetRegion(surpos.RegionPosition).GetEntity(surpos.CellPosition);

                if (td.Walkable && ed == null)
                {
                    return(surpos);
                }
            }
            return(null);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Gets the adjacent regions, which can be affected.
        /// </summary>
        /// <returns>The adjacent regions.</returns>
        /// <param name="regionManagerC">Region manager c.</param>
        /// <param name="position">Current Position od the selected building.</param>
        /// <param name="buildpoint">PositionI from the new unit.</param>
        private ConcurrentBag <RegionPosition> GetAdjacentRegions(RegionManagerController regionManagerC, RegionPosition position, PositionI buildpoint)
        {
            var list        = new ConcurrentBag <RegionPosition>();
            var surlist     = LogicRules.SurroundRegions;
            var regionSizeX = Constants.REGION_SIZE_X;
            var regionSizeY = Constants.REGION_SIZE_Y;

            var currentpos    = LogicRules.GetSurroundedFields(buildpoint);
            var currentregion = regionManagerC.RegionManager.GetRegion(buildpoint.RegionPosition);

            foreach (var checkingpos in currentpos)
            {
                if (regionManagerC.GetRegion(checkingpos.RegionPosition) != currentregion)
                {
                    list.Add(regionManagerC.RegionManager.GetRegion(checkingpos.RegionPosition).RegionPosition);
                }
            }
            return(list);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Gets the surrounded positions.
        /// </summary>
        /// <returns>The surrounded positions.</returns>
        /// <param name="startPoint">Start point.</param>
        /// <param name="accountID">Account ID.</param>
        /// <param name="moves">Moves of the entity.</param>
        private HashSet <PositionI> GetSurroundedPositions(PositionI startPoint, int accountID, int moves)
        {
            var fieldSet       = new HashSet <PositionI>();
            var fieldSetHelper = new HashSet <PositionI>();

            fieldSet.Add(startPoint);
            fieldSetHelper.Add(startPoint);

            for (int index = 0; index != moves; ++index)
            {
                var tempfieldSet = new HashSet <PositionI>();
                foreach (var item in fieldSetHelper)
                {
                    var surroundedTiles = LogicRules.GetSurroundedFields(item);
                    foreach (var tile in surroundedTiles)
                    {
                        if (!fieldSet.Contains(tile))
                        {
                            var region            = World.Instance.RegionManager.GetRegion(tile.RegionPosition);
                            var terrainDefinition = region.GetTerrain(tile.CellPosition);
                            if (terrainDefinition.Walkable)
                            {
                                var unit = region.GetEntity(tile.CellPosition);
                                if (unit == null)
                                {
                                    fieldSet.Add(tile);
                                    tempfieldSet.Add(tile);
                                }
                            }
                        }
                    }
                }
                fieldSetHelper = tempfieldSet;
            }
            return(fieldSet);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Returns any nodes that are adjacent to <paramref name="fromNode"/> and may be considered to form the next step in the path
        /// </summary>
        /// <param name="fromNode">The node from which to return the next possible nodes in the path</param>
        /// <returns>A list of next possible nodes in the path</returns>
        private List <Node> GetAdjacentWalkableNodes(Node fromNode)
        {
            List <Node> walkableNodes = new List <Node>();

            // check surrounded tiles of the current position
            foreach (var newPosition in LogicRules.GetSurroundedFields(fromNode.Location))
            {
                // gather environment information
                var region            = World.Instance.RegionManager.GetRegion(newPosition.RegionPosition);
                var terrainDefinition = region.GetTerrain(newPosition.CellPosition);
                // check terrain for walkable and other units in the path
                if (terrainDefinition.Walkable)
                {
                    var unit = region.GetEntity(newPosition.CellPosition);
                    // field is free from everything
                    if (unit == null)
                    {
                        if (m_nodes.ContainsKey(newPosition))
                        {
                            // use the dictionary and get the Node at the positonI
                            var node = m_nodes[newPosition];
                            if (node.State == NodeState.Open)
                            {
                                // calculate the travel cost to the next tile
                                double traversalCost = terrainDefinition.TravelCost;
                                double temp          = node.G + traversalCost;
                                if (temp < node.G)
                                {
                                    node.ParentNode = fromNode;
                                    walkableNodes.Add(node);
                                }
                            }
                        }
                        else
                        {
                            // if the Node was not open insert a new one in the dictionary
                            var newNode = new Node(newPosition, searchParameters.EndLocation);
                            newNode.ParentNode = fromNode;
                            walkableNodes.Add(newNode);
                            m_nodes[newPosition] = newNode;
                        }
                    }
                    else if (unit != null && newPosition == searchParameters.EndLocation && unit.OwnerID != searchParameters.AccountID)
                    {
                        if (m_nodes.ContainsKey(newPosition))
                        {
                            // use the dictionary and get the Node at the positonI
                            var node = m_nodes[newPosition];
                            if (node.State == NodeState.Open)
                            {
                                // calculate the travel cost to the next tile
                                double traversalCost = terrainDefinition.TravelCost;
                                double temp          = node.G + traversalCost;
                                if (temp < node.G)
                                {
                                    node.ParentNode = fromNode;
                                    walkableNodes.Add(node);
                                }
                            }
                        }
                        else
                        {
                            // if the Node was not open insert a new one in the dictionary
                            var newNode = new Node(newPosition, searchParameters.EndLocation);
                            newNode.ParentNode = fromNode;
                            walkableNodes.Add(newNode);
                            m_nodes[newPosition] = newNode;
                        }
                    }
                }
            }
            return(walkableNodes);
        }