Implemented population center generator

This commit is contained in:
2015-05-22 11:26:29 +03:00
parent e51c74944d
commit b6b2dce32e
23 changed files with 277 additions and 98 deletions

View File

@ -0,0 +1,43 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TransportGame.Generator;
using TransportGame.Model;
using TransportGame.Utils;
namespace TransportGame.Generator
{
/// <summary>
/// Complete city generator. Generates everything, from terrain to buildings
/// </summary>
public class CityGenerator
{
/// <summary>
/// Generates a city
/// </summary>
/// <param name="width">Width</param>
/// <param name="height">Height</param>
/// <returns>City</returns>
public Map Generate(int width, int height)
{
Map map;
// Generate terrain
TerrainGenerator terrainGen = new TerrainGenerator();
map = terrainGen.Generate(width, height);
// Generate population map
PopulationCentersGenerator populationGen = new PopulationCentersGenerator();
populationGen.Generate(map);
// Generate roads
// TODO: Generate roads
Logger.DumpMap(map, "withpop.map");
// Done
return map;
}
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 723d5ad7f5932f649971961655fff523
timeCreated: 1432200350
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -0,0 +1,36 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TransportGame.Model;
using TransportGame.Noise;
using UnityEngine;
namespace TransportGame.Generator
{
public class PopulationCentersGenerator
{
System.Random random = new System.Random();
public void Generate(Map map)
{
// Generate a number of points
int maxPoints = map.Width * map.Height / (1024 * 512);
int points = random.Next(maxPoints / 4, maxPoints);
for (int i = 0; i < points; ++i)
{
int px, py;
do
{
px = random.Next(map.Width);
py = random.Next(map.Height);
}
while (map.IsWater(px, py));
map.PopulationCenters.Add(new Point(px, py));
}
}
}
}

View File

@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 186756c7c690a3d4c8e633e3007523e0
timeCreated: 1432200350
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -50,8 +50,6 @@ namespace TransportGame.Generator
float waterAmount = random.NextSingle(map.Biome.Moisture.Minimum, map.Biome.Moisture.Maximum);
map.WaterLevel = Mathf.Pow(waterAmount, ConfigurationManager.TerrGenConfig.WaterNonLinearPower) * map.Biome.Height;
DumpData(map, "dump.map");
return map;
}
@ -69,16 +67,5 @@ namespace TransportGame.Generator
for (int y = 0; y < map.Height; ++y)
map.Heightmap[x, y] = Noise.Generate(x, y, 0, 1);
}
private void DumpData(Map map, string filename)
{
XmlSerializer serializer = new XmlSerializer(typeof(Map));
using (StreamWriter writer = new StreamWriter(Path.Combine(Logger.LogsDirectory, filename)))
{
serializer.Serialize(writer, map);
writer.Close();
}
}
}
}