public static StarData CreateSystemPlanets(StarData curSys) { PlanetGenerationData pgList = new PlanetGenerationData(); SpotPlanetGenerationTable pgTable = new SpotPlanetGenerationTable(); bool planetFound = false; try { pgList = planetGenerationDataList.Find(p => p.starType == curSys.SpectralClass); } catch (Exception ex) { Debug.LogError("Could not lookup " + curSys.SpectralClass.ToString() + " table for system " + curSys.Name + "! Error:" + ex.ToString()); return null; // try again with the next star } for (int x = 0; x < MaxPlanetsPerSystem; x++) // 6 spots to generate { pgTable = pgList.planetGenerationTable[x]; planetFound = false; // reset planet flag int generationChance = 0; int planetTypeChance = 0; // Step 1: check to see if there is a chance of a planet generationChance = UnityEngine.Random.Range(0, 100); if (generationChance <= pgTable.baseChance + (curSys.pMaterial * pgTable.materialMultiplier) - ((int)curSys.starMultipleType * pgTable.companionModifier)) // Step 2: look up the planet generation tables for this type of star and determine which planet type is generated { planetTypeChance = UnityEngine.Random.Range(0, 100); for (int y = 0; y < pgTable.chanceData.Count; y++) // look up planet data on each table { if (!planetFound) { int curChance = pgTable.chanceData[y]; if (planetTypeChance <= curChance) { PlanetData curPlanetData = new PlanetData(); curPlanetData = GeneratePlanet((PlanetData.ePlanetType)pgTable.typeData[y],curSys,x+1); curPlanetData.SystemID = curSys.ID; galaxyDataRef.AddPlanetDataToList(curPlanetData); //curSys.PlanetList.Add(curPlanetData); // a planet is born! Generate it now curSys.PlanetSpots[x] = curPlanetData; // stick it in the correct 'spot' (probably will rewrite) planetFound = true; } } } } } return curSys; }
public static void PopulatePlanetGenerationTables() { try { string line = null; string path = Application.dataPath + "/Resources/planetGenerationData.txt"; bool fileEmpty = false; PlanetGenerationData pGenData = new PlanetGenerationData(); int spotIdx = 0; int typeIdx = 0; System.IO.TextReader readFile = new StreamReader(path); while (!fileEmpty) // until the line hits a null object { line = readFile.ReadLine(); if (line != null) { try { if (line != "//") // don't carriage return, keep reading! { int[] minChance = new int[10]; int[] planetType = new int[10]; int colonFound; // moves carriage return past the colon string editedLine; // the part of the data that is used after the colon if (line.StartsWith("CLASS")) // get the class from the text { colonFound = line.IndexOf(":"); editedLine = line.Substring(colonFound + 1); int starClass = int.Parse(editedLine); pGenData.starType = (StarData.eSpectralClass)starClass; } if (line.StartsWith("SPOT")) // this is the line that gives the spot # and base generation chances { colonFound = line.IndexOf(":"); editedLine = line.Substring(colonFound + 1); string[] planetBaseChanceString = editedLine.Split(new Char[] { ',' }); int planetSpot = int.Parse(planetBaseChanceString[0]); int basePercent = int.Parse(planetBaseChanceString[1]); float multiplier = float.Parse(planetBaseChanceString[2]); int compMult = int.Parse(planetBaseChanceString[3]); pGenData.planetGenerationTable.Add(new SpotPlanetGenerationTable()); // need to add a new blank table per spot pGenData.planetGenerationTable[spotIdx].planetSpot = planetSpot; pGenData.planetGenerationTable[spotIdx].baseChance = basePercent; pGenData.planetGenerationTable[spotIdx].materialMultiplier = multiplier; pGenData.planetGenerationTable[spotIdx].companionModifier = compMult; } if (line.StartsWith(":")) // this is the line that gives chance and type of planet { colonFound = line.IndexOf(":"); editedLine = line.Substring(colonFound + 1); string[] planetTypePercent = editedLine.Split(new Char[] { ',' }); minChance[typeIdx] = int.Parse(planetTypePercent[0]); planetType[typeIdx] = int.Parse(planetTypePercent[1]); pGenData.planetGenerationTable[spotIdx].chanceData.Add(minChance[typeIdx]); pGenData.planetGenerationTable[spotIdx].typeData.Add(planetType[typeIdx]); typeIdx += 1; } if (line.StartsWith("/")) { spotIdx += 1; // space means move to the next spot definition typeIdx = 0; // reset the type index } } else { planetGenerationDataList.Add(pGenData); // add the current data table to the pgData list pGenData = new PlanetGenerationData(); // clear out the old object spotIdx = 0; typeIdx = 0; } } catch(IOException ex) { Debug.LogError("Issue with formatting in file " + path + " Error:" + ex.ToString()); } } else fileEmpty = true; } readFile.Close(); readFile = null; Debug.Log("Planet Generation Tables successfully read!"); } catch (IOException ex) { Debug.LogError("Could not read Planet Generation Table file; error:" + ex.ToString()); } }