コード例 #1
0
ファイル: GalaxyCell.cs プロジェクト: polytronicgr/GalaxyGen
        public GalaxyCell(GalaxyGenerator generator, int cellX, int cellY, bool fullGeneration = true)
        {
            this.generator = generator;
            this.cellX     = cellX;
            this.cellY     = cellY;

            Rand64 random = new Rand64((ulong)(cellY * GalaxyGenerator.galaxySize + cellX + GalaxyGenerator.galaxySeed));

            int numStars = random.Range(1, 10);

            for (int n = 0; n < numStars; n++)
            {
                StarSystemId id   = new StarSystemId(cellX, cellY, n);
                StarSystem   star = new StarSystem(id, random);
                starSystems.Add(star);
            }

            if (cellY > 0)
            {
                PickConnector(random, StarSystem.Attributes.NorthConnector);
            }
            if (cellX < GalaxyGenerator.galaxySize - 1)
            {
                PickConnector(random, StarSystem.Attributes.EastConnector);
            }
            if (cellY < GalaxyGenerator.galaxySize - 1)
            {
                PickConnector(random, StarSystem.Attributes.SouthConnector);
            }
            if (cellX > 0)
            {
                PickConnector(random, StarSystem.Attributes.WestConnector);
            }

            ConstrainPositions();

            if (fullGeneration)
            {
                if (cellY > 0)
                {
                    jumpGates.Add(new JumpGateLink(GetSystemWithAttribute(StarSystem.Attributes.NorthConnector).id,
                                                   GetCell(cellX, cellY - 1).GetSystemWithAttribute(StarSystem.Attributes.SouthConnector).id));
                }
                if (cellX < GalaxyGenerator.galaxySize - 1)
                {
                    jumpGates.Add(new JumpGateLink(GetSystemWithAttribute(StarSystem.Attributes.EastConnector).id,
                                                   GetCell(cellX + 1, cellY).GetSystemWithAttribute(StarSystem.Attributes.WestConnector).id));
                }
                if (cellY < GalaxyGenerator.galaxySize - 1)
                {
                    jumpGates.Add(new JumpGateLink(GetSystemWithAttribute(StarSystem.Attributes.SouthConnector).id,
                                                   GetCell(cellX, cellY + 1).GetSystemWithAttribute(StarSystem.Attributes.NorthConnector).id));
                }
                if (cellX > 0)
                {
                    jumpGates.Add(new JumpGateLink(GetSystemWithAttribute(StarSystem.Attributes.WestConnector).id,
                                                   GetCell(cellX - 1, cellY).GetSystemWithAttribute(StarSystem.Attributes.EastConnector).id));
                }

                if (!IsFullyConnected())
                {
                    TryConnectClosest();
                }

                int itCount = 0;
                while (!IsFullyConnected())
                {
                    bool avoidIntersections = itCount < 100;
                    TryAddJumpGate(starSystems[random.Range(0, starSystems.Count)].id, starSystems[random.Range(0, starSystems.Count)].id, avoidIntersections);
                    itCount++;
                }

                AssignRegions();

                for (int n = 0; n < starSystems.Count; n++)
                {
                    Region region = generator.GetRegion(starSystems[n].regionId);

                    if ((starSystems[n].attributes & StarSystem.Attributes.Capital) != 0)
                    {
                        starSystems[n].name = generator.GenerateCapitalName(region, random);
                    }
                    else
                    {
                        starSystems[n].name = generator.GenerateName(region.languageIndex, random);
                    }
                }
            }
        }
コード例 #2
0
ファイル: GalaxyCell.cs プロジェクト: polytronicgr/GalaxyGen
        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;
                    }
                }
            }
        }