Exemple #1
0
    public void SetRoadCell(int hash, RoadCell rc)
    {
        bool changed = false;

        if (rc.type > 0)
        {
            if (m_Road.ContainsKey(hash))
            {
                RoadCell old = m_Road[hash];
                if (!rc.Equals(old))
                {
                    m_Road[hash] = rc;
                    changed      = true;
                }
            }
            else
            {
                m_Road.Add(hash, rc);
                changed = true;
            }
        }
        else
        {
            if (m_Road.ContainsKey(hash))
            {
                m_Road.Remove(hash);
                changed = true;
            }
        }
        if (changed && OnTileDot != null)
        {
            OnTileDot(hash, rc);
        }
    }
Exemple #2
0
    public void AlterRoadCell(Vector3 world_pos, RoadCell rc)
    {
        int tile_hash = RSTile.QueryTile(world_pos).hash;
        int cell_hash = RSTile.WorldPosToRoadCellPos_s(tile_hash, world_pos).hash;

        AlterRoadCell(tile_hash, cell_hash, rc);
    }
 private void RenderPathToMap(RoadCell pathCell)
 {
     while (pathCell != null)
     {
         RenderVectorSquare(pathCell, 255, 0, 0);
         pathCell = pathCell.Previous;
     }
 }
        private void GenerateRoad(RoadCell pointA, RoadCell pointB, RoadCellMap roadMap)
        {
            var end = roadMap.BuildPath(pointA, pointB).Result;

            //var path = roadMap.BuildPathUsingAStar(pointA, pointB);
            RenderPathToMap(pointB);
            RenderVectorSquare(pointA, 0, 255, 0);
            RenderVectorSquare(pointB, 0, 255, 0);
        }
        private bool ConsiderRoute(
            RoadCell pointA,
            RoadCell pointB)
        {
            try
            {
                if (pointA.Previous == pointB)
                {
                    return(false); //We've backtracked completely
                }
                if ((int)pointB.DistanceFromOrigin == 0)
                {
                    pointB.DistanceFromOrigin = GetDirectLineDistance(_origin, pointB);
                }
                if (pointB != _destination && pointB.DistanceFromOrigin <= pointA.DistanceFromOrigin)
                {
                    return(false); //We're circling back
                }
                //_logger.Info($"At [{pointB.X},{pointB.Z}] {pointB.DistanceFromOrigin} from Origin");
                var costFromOrigin = pointA.CostFromOrigin + _getPathCost(pointA, pointB);
                if (costFromOrigin > _destination.CostFromOrigin)
                {
                    return(false);
                }

                // PointB has already been hit on different route, only redirect when the
                // cost is lower.
                if (pointB.Previous != null && pointB.CostFromOrigin <= costFromOrigin)
                {
                    return(false);
                }

                // PointA already has a route assigned, only update it when the cost is lower
                if (pointA.Next != null && pointA.Next.CostFromOrigin <= costFromOrigin)
                {
                    return(false);
                }

                pointB.Previous       = pointA;
                pointA.Next           = pointB;
                pointB.CostFromOrigin = costFromOrigin;

                if (pointB == _destination)
                {
                    _maximumCost = costFromOrigin;
                    //_logger.Warn($"Found a faster route to destination [{pointA.X},{pointA.Z}] Cost: {_maximumCost}");
                    return(false);
                }

                return(pointA != pointB);
            }
            catch (Exception exp)
            {
                _logger.Error(exp);
                throw;
            }
        }
    public void DotTextures(int hash, RoadCell rc)
    {
        if (!m_Started)
        {
            return;
        }
        int mask = WriteRoadCellToTextures(hash, rc);

        ApplyTexturesChange(mask);
    }
        public async Task <RoadCell> BuildPath(RoadCell from, RoadCell to)
        {
            _origin      = from;
            _destination = to;
            _getPathCost = (cell, roadCell) => WalkDirectEstimate(cell, roadCell, GetWeightedUnitDistance);
            _destination.CostFromOrigin = double.MaxValue;
            _maximumCost   = _getPathCost(_origin, _destination);
            _getNeighbours = Get4RadiusNeighbours;
            await PathFindingUsingLinkedCells(from);

            return(to);
        }
Exemple #8
0
        RoadCell CreateCell(Cell cellData)
        {
            RoadCell roadcell = DataController.Instance.GetRoadCell();

            roadcell.transform.SetParent(grid.transform, false);
            roadcell.setMaterial(cellData.material);
            roadcell.name = cellData.name;

            roadDirections = new bool[cellData.directions.Length];
            Array.Copy(cellData.directions, roadDirections, roadDirections.Length);

            return(roadcell);
        }
        private Vector2 <float> GetWalkerFactors(RoadCell origin, RoadCell destination)
        {
            var xDistance = destination.X - origin.X;
            var zDistance = destination.Z - origin.Z;
            var factors   = new Vector2 <float>(Math.Sign(xDistance), Math.Sign(zDistance));

            if (Math.Abs(xDistance) > Math.Abs(zDistance))
            {
                factors.Z = (float)zDistance / (float)Math.Abs(xDistance);
            }
            if (Math.Abs(zDistance) > Math.Abs(xDistance))
            {
                factors.X = (float)xDistance / (float)Math.Abs(zDistance);
            }
            return(factors);
        }
Exemple #10
0
 public override bool Equals(object obj)
 {
     if (null == obj)
     {
         return(false);
     }
     if (obj is RoadCell)
     {
         RoadCell rc = (RoadCell)obj;
         return(color_type.r == rc.color_type.r &&
                color_type.g == rc.color_type.g &&
                color_type.b == rc.color_type.b &&
                color_type.a == rc.color_type.a);
     }
     return(false);
 }
 private IEnumerable <RoadCell> GetRadiusNeighbours(RoadCell origin, List <Vector2 <int> > offSets, double backTrackDistanceCheck)
 {
     try
     {
         var neighbours = offSets
                          .Select(vector2 => new Vector2 <int>(vector2.X + origin.X, vector2.Z + origin.Z));
         return(neighbours
                .Where(vector2 => vector2.X > 0 && vector2.X < _size &&
                       vector2.Z > 0 && vector2.Z < _size)
                .Select(vector2 => _map[vector2.X, vector2.Z]));
     }
     catch (Exception exp)
     {
         _logger.Error(exp);
         throw;
     }
 }
Exemple #12
0
        void SetCellToTheGrid()
        {
            if (isAvailableToSet)
            {
                //set up previous cell
                currentCell.Location.name = currentCell.name;
                currentCell.Location.SetAchievableNeighbors(roadDirections);
                ActivateLocationsIfNeeded();

                //get new cell
                currentCell = GetRandomCell();

                if (currentCell == null)
                {
                    GameManager.Instance.RunNextStrategy();
                }
            }
        }
        public RoadCell GetCellMetrics(int x, int z)
        {
            var mapX         = x * _cellSize;
            var mapZ         = z * _cellSize;
            var cellBoundary = _cellSize - 1;
            var metrics      = new RoadCell()
            {
                Avg = ((
                           _heightMap[mapX + cellBoundary, mapZ] +
                           _heightMap[mapX + cellBoundary, mapZ + cellBoundary] +
                           _heightMap[mapX, mapZ + cellBoundary] +
                           _heightMap[mapX, mapZ]) / 4),
                X = x,
                Z = z
            };

            return(metrics);
        }
        private double GetWeightedUnitDistance(RoadCell pointA, RoadCell pointB)
        {
            //var directLineDistance = GetDirectLineDistance(pointA, pointB);
            var heightDifference = Math.Abs(pointA.Avg - pointB.Avg);

            //return heightDifference;

            /*
             * var weightedDifference = Math.Pow(2, Math.Min(32, heightDifference / 124));
             * return weightedDifference;// * (directLineDistance / distanceFactor);
             */
            if (heightDifference < _elevationFactor)
            {
                return(heightDifference);
            }

            var heightFactor = 1 + Math.Min(8, Math.Floor(heightDifference / 256));

            return(heightFactor * heightDifference);
        }
        private IEnumerable <RoadCell> Get8Neighbours(RoadCell fromCell)
        {
            var neighbours = new List <RoadCell>();

            if (fromCell.X > 0)
            {
                neighbours.Add(_map[fromCell.X - 1, fromCell.Z]);
                if (fromCell.Z > 0)
                {
                    neighbours.Add(_map[fromCell.X - 1, fromCell.Z - 1]);
                }
                if (fromCell.Z < _size - 1)
                {
                    neighbours.Add(_map[fromCell.X - 1, fromCell.Z + 1]);
                }
            }

            if (fromCell.X < _size - 1)
            {
                neighbours.Add(_map[fromCell.X + 1, fromCell.Z]);
                if (fromCell.Z > 0)
                {
                    neighbours.Add(_map[fromCell.X + 1, fromCell.Z - 1]);
                }
                if (fromCell.Z < _size - 1)
                {
                    neighbours.Add(_map[fromCell.X + 1, fromCell.Z + 1]);
                }
            }

            if (fromCell.Z > 0)
            {
                neighbours.Add(_map[fromCell.X, fromCell.Z - 1]);
            }
            if (fromCell.Z < _size - 1)
            {
                neighbours.Add(_map[fromCell.X, fromCell.Z + 1]);
            }

            return(neighbours);
        }
 public async Task PathFindingUsingLinkedCells(
     RoadCell waypoint
     )
 {
     try
     {
         if (waypoint.CostFromOrigin > _maximumCost)
         {
             _logger.Info("Too Costly - Bailing out");
         }
         else if (GetDirectLineDistance(waypoint, _destination) < distanceFactor)
         {
             ConsiderRoute(waypoint, _destination);
         }
         else
         {
             var neighbors = _getNeighbours(waypoint).ToList();
             foreach (var neighbor in neighbors)
             {
                 if (neighbor != null)
                 {
                     if (ConsiderRoute(waypoint, neighbor))
                     {
                         await PathFindingUsingLinkedCells(neighbor);
                     }
                 }
                 else
                 {
                     _logger.Info("Neighbor is null?");
                 }
             }
         }
     }
     catch (Exception exp)
     {
         _logger.Error(exp);
         throw;
     }
 }
 private double WalkDirectEstimate(RoadCell arg, RoadCell destination, Func <RoadCell, RoadCell, double> getRouteCost)
 {
     try
     {
         var    walker           = new Vector2 <int>(arg.X, arg.Z);
         var    previousCell     = arg;
         var    factors          = GetWalkerFactors(arg, destination);
         double totalWalkingCost = 0;
         int    stepCounter      = 1;
         do
         {
             walker.X          = previousCell.X + (int)Math.Round(factors.X * stepCounter, 0);
             walker.Z          = previousCell.Z + (int)Math.Round(factors.Z * stepCounter, 0);
             totalWalkingCost += getRouteCost(previousCell, _map[walker.X, walker.Z]);
             stepCounter++;
         } while (walker.X != destination.X && walker.Z != destination.Z);
         return(totalWalkingCost);
     }
     catch (Exception exp)
     {
         _logger.Error(exp);
     }
     return(double.MaxValue);
 }
Exemple #18
0
 public void AlterRoadCell(int tile_hash, int cell_hash, RoadCell rc)
 {
     if (rc.type > 0)
     {
         if (!m_Tiles.ContainsKey(tile_hash))
         {
             m_Tiles.Add(tile_hash, new RSTile(tile_hash, true));
         }
         m_Tiles[tile_hash].SetRoadCell(cell_hash, rc);
     }
     else
     {
         if (m_Tiles.ContainsKey(tile_hash))
         {
             RSTile tile = m_Tiles[tile_hash];
             tile.SetRoadCell(cell_hash, rc);
             if (tile.Road.Count == 0)
             {
                 tile.Destroy();
                 m_Tiles.Remove(tile_hash);
             }
         }
     }
 }
Exemple #19
0
 public override void Start()
 {
     base.Start();
     currentCell = GetRandomCell();
 }
 private void RenderVectorSquare(RoadCell vector, byte Red, byte Green, byte Blue)
 {
     RenderVectorSquare(new Vector2 <int> {
         X = vector.X * cellSize, Z = vector.Z * cellSize
     }, Red, Green, Blue);
 }
Exemple #21
0
    public void SetRoadCell(Vector3 world_pos, RoadCell rc)
    {
        int hash = WorldPosToRoadCellPos(world_pos).hash;

        SetRoadCell(hash, rc);
    }
 public Path <RoadCell> BuildPathUsingAStar(RoadCell from, RoadCell to)
 {
     _getPathCost = (cell, roadCell) => WalkDirectEstimate(cell, roadCell, GetWeightedUnitDistance);
     return(PathFinding.AStarPathFindingUsingPriorityQueue(from, to, Get4RadiusNeighbours, _getPathCost,
                                                           _getPathCost));
 }
    private int WriteRoadCellToTextures(int hash, RoadCell rc)
    {
        INTVECTOR3 rcpos = new INTVECTOR3();

        rcpos.hash = hash;
        byte h = (byte)(rcpos.y + 1);

        if (rc.type > 0)
        {
            m_RoadGraph.SetPixel(rcpos.x, rcpos.z, (Color)(rc.color_type));
            Color32 hc = new Color32(0, 0, 0, 0);

            hc = SafeColorConvert(m_HeightGraph0.GetPixel(rcpos.x, rcpos.z));
            if (hc.r == h || hc.g == h || hc.b == h)
            {
                return(-1);
            }
            if (hc.r == 0)
            {
                hc.r = h;
                m_HeightGraph0.SetPixel(rcpos.x, rcpos.z, (Color)(hc));
                return(0);
            }
            if (hc.g == 0)
            {
                hc.g = h;
                m_HeightGraph0.SetPixel(rcpos.x, rcpos.z, (Color)(hc));
                return(0);
            }
            if (hc.b == 0)
            {
                hc.b = h;
                m_HeightGraph0.SetPixel(rcpos.x, rcpos.z, (Color)(hc));
                return(0);
            }
            hc = SafeColorConvert(m_HeightGraph1.GetPixel(rcpos.x, rcpos.z));
            if (hc.r == h || hc.g == h || hc.b == h)
            {
                return(-1);
            }
            if (hc.r == 0)
            {
                hc.r = h;
                m_HeightGraph1.SetPixel(rcpos.x, rcpos.z, (Color)(hc));
                return(1);
            }
            if (hc.g == 0)
            {
                hc.g = h;
                m_HeightGraph1.SetPixel(rcpos.x, rcpos.z, (Color)(hc));
                return(1);
            }
            if (hc.b == 0)
            {
                hc.b = h;
                m_HeightGraph1.SetPixel(rcpos.x, rcpos.z, (Color)(hc));
                return(1);
            }
            return(-1);
        }
        else
        {
            m_RoadGraph.SetPixel(rcpos.x, rcpos.z, new Color(0, 0, 0, 0));
            Color32 hc = new Color32(0, 0, 0, 0);
            hc = SafeColorConvert(m_HeightGraph0.GetPixel(rcpos.x, rcpos.z));
            if (hc.r == h)
            {
                hc.r = 0;
                m_HeightGraph0.SetPixel(rcpos.x, rcpos.z, (Color)(hc));
                return(0);
            }
            if (hc.g == h)
            {
                hc.g = 0;
                m_HeightGraph0.SetPixel(rcpos.x, rcpos.z, (Color)(hc));
                return(0);
            }
            if (hc.b == h)
            {
                hc.b = 0;
                m_HeightGraph0.SetPixel(rcpos.x, rcpos.z, (Color)(hc));
                return(0);
            }
            hc = SafeColorConvert(m_HeightGraph1.GetPixel(rcpos.x, rcpos.z));
            if (hc.r == h)
            {
                hc.r = 0;
                m_HeightGraph1.SetPixel(rcpos.x, rcpos.z, (Color)(hc));
                return(1);
            }
            if (hc.g == h)
            {
                hc.g = 0;
                m_HeightGraph1.SetPixel(rcpos.x, rcpos.z, (Color)(hc));
                return(1);
            }
            if (hc.b == h)
            {
                hc.b = 0;
                m_HeightGraph1.SetPixel(rcpos.x, rcpos.z, (Color)(hc));
                return(1);
            }
            return(-1);
        }
    }
 private IEnumerable <RoadCell> Get12RadiusNeighbours(RoadCell origin)
 {
     return(GetRadiusNeighbours(origin, PathFinding.TwelveRadius16Points, 23));
 }
Exemple #25
0
    public void SetRoadCell(INTVECTOR3 rcpos, RoadCell rc)
    {
        int hash = rcpos.hash;

        SetRoadCell(hash, rc);
    }
        private double GetUnitDistance(RoadCell pointA, RoadCell pointB, int heightFactor)
        {
            var heightDifference = Math.Abs(pointA.Avg - pointB.Avg);

            return(heightFactor * heightDifference);
        }