static public float tileWidth = 9f * Mathf.Sqrt(3) / 2; // width of each tile void Start() { string folderPath = Directory.GetCurrentDirectory().Replace("\\", "/") + "/Assets/ProjectFiles/"; // store errors in a string to be writen to output file string errorDump = "ERRORS:\r\n"; // dictionary which stores the conditional probabilities of seeing each terrain type next to another (given) type distribution = new Dictionary <string, List <double> >(); // read probabilities from file using (StreamReader reader = new StreamReader(folderPath + "TerrainConditionalProbabilities.csv")) { // read first line into terrain types list List <string> terrainTypesFile = reader.ReadLine().Split(',').ToList(); // make sure the list read from file is equal to the given list foreach (string ts in terrainTypesFile) // all terrain types from file { if (ts != "" && !terrainTypes.Contains(ts)) { errorDump += "file has extra terrain type, " + ts + "\r\n"; } } foreach (string ts in terrainTypes) // all given terrain types { if (!terrainTypesFile.Contains(ts)) { errorDump += "file missing terrain type, " + ts + "\r\n"; } } // remove the first (blank) and last (if newline) elements of the list terrainTypes.RemoveAt(0); if (terrainTypes[terrainTypes.Count - 1].Contains("\r") || terrainTypes[terrainTypes.Count - 1].Contains("\n")) // newlines contain either or both depending on OS { terrainTypes.RemoveAt(terrainTypes.Count - 1); } // assign number of terrains numTerrainTypes = terrainTypes.Count; while (!reader.EndOfStream) // while not end of file { // read each line into a list of strings List <string> values = reader.ReadLine().Split(',').ToList(); string currKey = values[0]; // key is first value in the line values.RemoveAt(0); // remove first value (key) from line list if (values[values.Count - 1].Contains("\r") || values[values.Count - 1].Contains("\n")) { values.RemoveAt(values.Count - 1); // remove ending newline char } List <double> numValues = new List <double>(); // list of probabilities foreach (string value in values) // loop through each element remaining in the line list { double numValue = 0; // sets probability to 0 (if the string cannot be convered) try // converting string to double { numValue = Convert.ToDouble(value); } catch (FormatException) // if value cannot be converted to double { errorDump += "failed to convert to double on " + value + " in " + currKey + "\r\n"; } numValues.Add(numValue); // add to current list of probabilities } // add terrain type (key) and list of tuples (value) to the dictionary distribution.Add(currKey, numValues); } } // check if distribution is exhaustive (has all terrain types in both directions) if (!DictionarySquare(distribution)) { errorDump += "dictionary not square\r\n"; } TerrainTile currTile; // empty terrain tile to hold current tile in loop string currType; // will hold type of current tile Coordinate currCoord; string aboveCoordstr, leftCoordstr; // populate 2D array terrainMap int widthStart = mapWidthInTiles / 2 - mapWidthInTiles, heightStart = mapHeightInTiles / 2 - mapHeightInTiles; int widthEnd = widthStart + mapWidthInTiles, heightEnd = heightStart + mapHeightInTiles; for (int i = widthStart; i < widthEnd; i++) { for (int j = heightStart; j < heightEnd; j++) { currCoord = new Coordinate(i, j); // current spot on the map leftCoordstr = (new Coordinate(i, j - 1)).AsString(); aboveCoordstr = (new Coordinate(i - 1, j)).AsString(); if (i == widthStart && j == heightStart) // first tile in map { currTile = new TerrainTile(i, j); } else if (i == widthStart && j != heightStart) { currTile = new TerrainTile(terrainTiles[leftCoordstr].type, currCoord); } else if (i != widthStart && j == heightStart) { currTile = new TerrainTile(terrainTiles[aboveCoordstr].type, currCoord); } else { currTile = new TerrainTile(terrainTiles[aboveCoordstr].type, terrainTiles[leftCoordstr].type, currCoord); } currType = currTile.type; terrainTiles.Add(currCoord.AsString(), currTile); // add current tile to dictionary currTile.PlaceTerrainTile(); // incriment the count recording number of each terrain tile // for testing and balancing purposes if (counts.ContainsKey(currType)) { counts[currType]++; } else { counts[currType] = 1; } } } foreach (string terrain in terrainTypes) { string line = terrain + " not found"; if (counts.ContainsKey(terrain)) { line = terrain + " : " + counts[terrain].ToString(); } File.AppendAllText(folderPath + "testfile.txt", line + "\r\n"); } File.WriteAllText(folderPath + "testfile.txt", errorDump); }