Ejemplo n.º 1
0
        public StarSystem GetStarSystem(StarSystemId id)
        {
            GalaxyCell cell = GetCell(id.cellX, id.cellY);

            if (cell != null)
            {
                return(cell.starSystems[id.systemIndex]);
            }
            return(null);
        }
Ejemplo n.º 2
0
        int CountJumpGateLinks(StarSystemId system)
        {
            int count = 0;

            for (int n = 0; n < jumpGates.Count; n++)
            {
                if (jumpGates[n].start == system || jumpGates[n].end == system)
                {
                    count++;
                }
            }

            return(count);
        }
Ejemplo n.º 3
0
        void TryConnectClosest()
        {
            for (int n = 0; n < starSystems.Count; n++)
            {
                if (CountJumpGateLinks(starSystems[n].id) == 0)
                {
                    TryConnectClosest(starSystems[n], new List <StarSystem>(starSystems), true);
                }
            }

            StarSystemId           firstConnected   = jumpGates[0].start;
            HashSet <StarSystemId> connectedSystems = new HashSet <StarSystemId>();

            CheckConnections(connectedSystems, firstConnected);

            const int numIterations = 10;

            for (int it = 0; it < numIterations && connectedSystems.Count < starSystems.Count; it++)
            {
                for (int n = 0; n < starSystems.Count; n++)
                {
                    if (!connectedSystems.Contains(starSystems[n].id))
                    {
                        List <StarSystem> toConnect = new List <StarSystem>();
                        for (int i = 0; i < starSystems.Count; i++)
                        {
                            if (connectedSystems.Contains(starSystems[i].id))
                            {
                                toConnect.Add(starSystems[i]);
                            }
                        }

                        if (TryConnectClosest(starSystems[n], toConnect, it < numIterations / 2))
                        {
                            connectedSystems.Clear();
                            CheckConnections(connectedSystems, firstConnected);
                        }
                    }
                }
            }
        }
Ejemplo n.º 4
0
        void CheckConnections(HashSet <StarSystemId> connectedSystems, StarSystemId checkSystem)
        {
            connectedSystems.Add(checkSystem);

            for (int n = 0; n < jumpGates.Count; n++)
            {
                JumpGateLink link = jumpGates[n];
                if (link.start.cellX == cellX && link.start.cellY == cellY &&
                    link.end.cellX == cellX && link.end.cellY == cellY)
                {
                    if (link.start == checkSystem && !connectedSystems.Contains(link.end))
                    {
                        CheckConnections(connectedSystems, link.end);
                    }
                    else if (link.end == checkSystem && !connectedSystems.Contains(link.start))
                    {
                        CheckConnections(connectedSystems, link.start);
                    }
                }
            }
        }
Ejemplo n.º 5
0
        bool TryAddJumpGate(StarSystemId a, StarSystemId b, bool avoidIntersections)
        {
            if (a == b)
            {
                return(false);
            }

            // Check duplicates
            for (int n = 0; n < jumpGates.Count; n++)
            {
                if ((jumpGates[n].start == a && jumpGates[n].end == b) ||
                    (jumpGates[n].start == b && jumpGates[n].end == a))
                {
                    return(false);
                }
            }

            bool intersected = false;

//            if (avoidIntersections)
            {
                float checkStartX, checkStartY;
                float checkEndX, checkEndY;

                GetStarSystem(a).GetRelativePosition(this, out checkStartX, out checkStartY);
                GetStarSystem(b).GetRelativePosition(this, out checkEndX, out checkEndY);

                for (int n = 0; n < jumpGates.Count; n++)
                {
                    if (jumpGates[n].start != a && jumpGates[n].start != b &&
                        jumpGates[n].end != a && jumpGates[n].end != b)
                    {
                        float startX, startY;
                        float endX, endY;
                        GetStarSystem(jumpGates[n].start).GetRelativePosition(this, out startX, out startY);
                        GetStarSystem(jumpGates[n].end).GetRelativePosition(this, out endX, out endY);

                        if (DoIntersect(startX, startY, endX, endY, checkStartX, checkStartY, checkEndX, checkEndY))
                        {
                            if (avoidIntersections)
                            {
                                return(false);
                            }
                            else
                            {
                                intersected = true;
                            }
                        }
                    }
                }
            }

//            int connectionCount = GetConnectionCount();

            JumpGateLink newLink = new JumpGateLink(a, b);

            newLink.didIntersect = intersected;

            jumpGates.Add(newLink);

            //if(GetConnectionCount() == connectionCount)
            //{
            //    jumpGates.RemoveAt(jumpGates.Count - 1);
            //}

            return(true);
        }
Ejemplo n.º 6
0
 StarSystem GetStarSystem(StarSystemId id)
 {
     return(GetCell(id.cellX, id.cellY).starSystems[id.systemIndex]);
 }
Ejemplo n.º 7
0
        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);
                    }
                }
            }
        }
Ejemplo n.º 8
0
 public StarSystem(StarSystemId id, Rand64 random)
 {
     this.id = id;
     x       = random.Range(0.0f, 1.0f);
     y       = random.Range(0.0f, 1.0f);
 }
Ejemplo n.º 9
0
 public JumpGateLink(StarSystemId start, StarSystemId end)
 {
     this.start   = start;
     this.end     = end;
     didIntersect = false;
 }