public RegionCell GetRegionCell(int x, int y)
        {
            if (x < 0 || y < 0 || x >= galaxySize / RegionCell.galaxyCellsPerRegion || y >= galaxySize / RegionCell.galaxyCellsPerRegion)
            {
                return(null);
            }

            for (int n = 0; n < cachedRegionCells.Count; n++)
            {
                if (cachedRegionCells[n].cellX == x && cachedRegionCells[n].cellY == y)
                {
                    RegionCell result = cachedRegionCells[n];
                    cachedRegionCells.RemoveAt(n);
                    cachedRegionCells.Add(result);
                    return(result);
                }
            }

            RegionCell newCell = new RegionCell(this, x, y);

            cachedRegionCells.Add(newCell);
            if (cachedRegionCells.Count > maxCacheSize)
            {
                cachedRegionCells.RemoveAt(0);
            }

            return(newCell);
        }
Beispiel #2
0
        void AssignRegions()
        {
            List <Region> regions = new List <Region>();

            int regionCellX = cellX / RegionCell.galaxyCellsPerRegion;
            int regionCellY = cellY / RegionCell.galaxyCellsPerRegion;

            for (int x = regionCellX - 1; x < regionCellX + 1; x++)
            {
                for (int y = regionCellY - 1; y < regionCellY + 1; y++)
                {
                    if (x >= 0 && y >= 0 && x < GalaxyGenerator.galaxySize / RegionCell.galaxyCellsPerRegion && y < GalaxyGenerator.galaxySize / RegionCell.galaxyCellsPerRegion)
                    {
                        RegionCell regionCell = generator.GetRegionCell(x, y);
                        if (regionCell != null)
                        {
                            regions.AddRange(regionCell.regions);
                        }
                    }
                }
            }

            for (int n = 0; n < starSystems.Count; n++)
            {
                StarSystem system             = starSystems[n];
                Region     closest            = null;
                float      closestDistanceSqr = 0.0f;

                for (int r = 0; r < regions.Count; r++)
                {
                    float locX, locY;
                    regions[r].GetRelativePosition(this, out locX, out locY);

                    float distSqr = (locX - system.x) * (locX - system.x) + (locY - system.y) * (locY - system.y);
                    if (closest == null || distSqr < closestDistanceSqr)
                    {
                        closest            = regions[r];
                        closestDistanceSqr = distSqr;
                    }
                }
                starSystems[n].regionId = closest.id;
            }

            for (int n = 0; n < starSystems.Count; n++)
            {
                StarSystem system       = starSystems[n];
                int        numJumpGates = CountJumpGateLinks(system.id);

                if (numJumpGates == 1)
                {
                    for (int j = 0; j < jumpGates.Count; j++)
                    {
                        if (jumpGates[j].start == system.id)
                        {
                            system.regionId = GetStarSystem(jumpGates[j].end).regionId;
                            break;
                        }
                        else if (jumpGates[j].end == system.id)
                        {
                            system.regionId = GetStarSystem(jumpGates[j].start).regionId;
                            break;
                        }
                    }
                    system.attributes |= StarSystem.Attributes.Deadend;
                }
                else if (numJumpGates >= 4 && (system.attributes & StarSystem.Attributes.Capital) == 0)
                {
                    system.attributes |= StarSystem.Attributes.HubWorld;
                }
            }

            // Assign capital
            RegionCell mainRegionCell = generator.GetRegionCell(regionCellX, regionCellY);

            for (int r = 0; r < mainRegionCell.regions.Count; r++)
            {
                Region region = mainRegionCell.regions[r];
                float  locX, locY;
                region.GetRelativePosition(this, out locX, out locY);

                if (locX >= 0 && locY >= 0 && locX <= 1 && locY <= 1)
                {
                    StarSystem closest            = null;
                    float      closestDistanceSqr = 0.0f;

                    for (int n = 0; n < starSystems.Count; n++)
                    {
                        StarSystem system = starSystems[n];

                        if (system.regionId == region.id && CountJumpGateLinks(system.id) >= 3)
                        {
                            float distSqr = (locX - system.x) * (locX - system.x) + (locY - system.y) * (locY - system.y);

                            if (closest == null || distSqr < closestDistanceSqr)
                            {
                                closest            = system;
                                closestDistanceSqr = distSqr;
                            }
                        }
                    }

                    if (closest != null)
                    {
                        closest.attributes |= StarSystem.Attributes.Capital;
                    }
                }
            }
        }
        public Region GetRegion(RegionId id)
        {
            RegionCell cell = GetRegionCell(id.cellX, id.cellY);

            return(cell.regions[id.index]);
        }