Пример #1
0
        public Form1()
        {
            InitializeComponent();

            MainPictureBox.Paint += MainPictureBox_Paint;

            MainPictureBox.Refresh();

            hScrollBar1.Maximum = (GalaxyGenerator.galaxySize * renderer.cellSize);
            vScrollBar1.Maximum = (GalaxyGenerator.galaxySize * renderer.cellSize);

            vScrollBar1.Scroll += (sender, e) => { MainPictureBox.Invalidate(); };
            hScrollBar1.Scroll += (sender, e) => { MainPictureBox.Invalidate(); };

            KeyPress += Form1_KeyPress;

            MainPictureBox.MouseClick += View_MouseClick;
            MainPictureBox.MouseDown  += View_MouseDown;
            MainPictureBox.MouseUp    += View_MouseUp;
            MainPictureBox.MouseMove  += View_MouseMove;
            MainPictureBox.MouseWheel += View_MouseWheel;


            Rand64     rand  = new Rand64((ulong)new Random().Next());
            int        cellX = rand.Range(0, GalaxyGenerator.galaxySize);
            int        cellY = rand.Range(0, GalaxyGenerator.galaxySize);
            GalaxyCell cell  = galaxyGenerator.GetCell(cellX, cellY);

            if (cell != null)
            {
                StarSystem chosenSystem = cell.starSystems[rand.Range(0, cell.starSystems.Count)];
                SelectSystem(chosenSystem);
                CenterOnSystem(chosenSystem);
            }
        }
Пример #2
0
 public Region(RegionId id, Rand64 random)
 {
     this.id       = id;
     colorIndex    = random.Range(0, 1000);
     languageIndex = random.Range(0, 1000);
     x             = random.Range(0.0f, 1.0f);
     y             = random.Range(0.0f, 1.0f);
 }
Пример #3
0
 void NameRegions(Rand64 random)
 {
     for (int n = 0; n < regions.Count; n++)
     {
         regions[n].baseName    = generator.GenerateName(regions[n].languageIndex, random);
         regions[n].displayName = generator.GenerateRegionName(regions[n].baseName, random);
     }
 }
Пример #4
0
        void PickConnector(Rand64 random, StarSystem.Attributes connectorType)
        {
            const int iterations = 5;
            int       pick       = 0;

            for (int n = 0; n < iterations; n++)
            {
                pick = random.Range(0, starSystems.Count);
                if ((starSystems[pick].attributes & (StarSystem.Attributes.NorthConnector | StarSystem.Attributes.EastConnector | StarSystem.Attributes.SouthConnector | StarSystem.Attributes.WestConnector)) == 0)
                {
                    break;
                }
            }
            starSystems[pick].attributes |= connectorType;
        }
Пример #5
0
        public string GenerateCapitalName(Region region, Rand64 random)
        {
            string baseName = region.baseName;

            if (random.Range(0.0f, 1.0f) < 0.5f)
            {
                baseName = GenerateName(region.languageIndex, random);
            }
            if (random.Range(0.0f, 1.0f) < 1.0f)
            {
                string nameFormat = capitalNamingRules[random.Range(0, capitalNamingRules.Length)];
                return(string.Format(nameFormat, baseName));
            }
            return(baseName);
        }
Пример #6
0
        public override string Generate(Rand64 random)
        {
            int attempts = 100;

            while (attempts > 0)
            {
                attempts--;
                string name;

                if (AttemptGenerate(random, out name))
                {
                    return(name);
                }
            }
            return("");
        }
Пример #7
0
            public Node PickRandomChild(Rand64 random)
            {
                int childFrequency = 0;

                foreach (Node child in nodes.Values)
                {
                    childFrequency += child.frequency;
                }

                int pick = random.Range(0, childFrequency);

                foreach (Node child in nodes.Values)
                {
                    pick -= child.frequency;
                    if (pick <= 0)
                    {
                        return(child);
                    }
                }
                return(null);
            }
Пример #8
0
        public RegionCell(GalaxyGenerator generator, int cellX, int cellY)
        {
            this.generator = generator;
            this.cellX     = cellX;
            this.cellY     = cellY;

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

            int numRegions = random.Range(5, 10);

            for (int n = 0; n < numRegions; n++)
            {
                RegionId id     = new RegionId(cellX, cellY, n);
                Region   region = new Region(id, random);
                regions.Add(region);
            }

            ConstrainPositions();

            NameRegions(random);
        }
Пример #9
0
 public abstract string Generate(Rand64 random);
Пример #10
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);
                    }
                }
            }
        }
Пример #11
0
 public StarSystem(StarSystemId id, Rand64 random)
 {
     this.id = id;
     x       = random.Range(0.0f, 1.0f);
     y       = random.Range(0.0f, 1.0f);
 }
Пример #12
0
        bool AttemptGenerate(Rand64 random, out string outName)
        {
            string result = "[";
            Node   node   = null;

            for (int n = 0; n < 100; n++)
            {
                bool found = false;

                float linkSizeRoll = random.Range(0.0f, 1.0f);
                int   linkSize     = linkSizeRoll < 0.05f ? 1 : linkSizeRoll < 0.15f ? 3 : 2;

                for (int l = linkSize; l > 0 && !found; l--)
                {
                    if (result.Length >= l)
                    {
                        string sub = result.Substring(result.Length - l, l);

                        if (rootNode.nodes.TryGetValue(sub, out node))
                        {
                            node = node.PickRandomChild(random);

                            if (node != null)
                            {
                                if (result.Length + node.key.Length >= (2 + MinLength) || !node.key.Contains("]"))
                                {
                                    if (l > 1 || node.key != "]")
                                    {
                                        result += node.key;
                                        found   = true;
                                    }
                                }
                            }
                        }
                    }
                }

                if (!found)
                {
                    outName = null;
                    return(false);
                }

                if (result.EndsWith("]"))
                {
                    break;
                }
            }

            if (excludeBaseNames && baseNames.Contains(result))
            {
                outName = null;
                return(false);
            }

            result = result.Trim('[', ']');

            outName = result;

            if (outName.Length > MaxLength || outName.Length < MinLength)
            {
                return(false);
            }

            outName = char.ToUpper(outName[0]).ToString() + outName.Substring(1);

            return(true);
        }
Пример #13
0
        public string GenerateRegionName(string baseName, Rand64 random)
        {
            string nameFormat = regionNamingRules[random.Range(0, regionNamingRules.Length)];

            return(string.Format(nameFormat, baseName));
        }
Пример #14
0
 public string GenerateName(int genType, Rand64 random)
 {
     return(nameGenerators[genType % nameGenerators.Count].Generate(random));
 }