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

@ -33,12 +33,8 @@ namespace TransportGame.Business
{
try
{
// Open file
var stream = File.OpenRead(file);
// Try to deserialize biome
XmlSerializer serializer = new XmlSerializer(typeof(Biome));
var biome = (Biome)serializer.Deserialize(stream);
var biome = SerializationHelper.DeserializeXml<Biome>(file);
biome.FileName = file;
// Add it to biome list

View File

@ -21,7 +21,7 @@ namespace TransportGame.Business
public static void LoadConfiguration()
{
// Load terrgen config
TerrGenConfig = SerializationHelper.Deserialize<TerrainGeneratorConfig>(Path.Combine(ConfigurationDirectory, TerrGenConfigFile));
TerrGenConfig = SerializationHelper.DeserializeXml<TerrainGeneratorConfig>(Path.Combine(ConfigurationDirectory, TerrGenConfigFile));
}
}
}

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();
}
}
}
}

View File

@ -5,6 +5,7 @@ using System.Text;
using System.Xml.Serialization;
using TransportGame.Model.Road;
using TransportGame.Utils;
using UnityEngine;
namespace TransportGame.Model
{
@ -12,7 +13,6 @@ namespace TransportGame.Model
public class Map
{
private float[,] heightmap;
private float[,] population;
#region Properties
@ -69,48 +69,11 @@ namespace TransportGame.Model
public int Height { get { return (heightmap == null) ? 0 : heightmap.GetLength(1); } }
/// <summary>
/// Gets the population map
/// Gets or sets the population map
/// </summary>
[XmlIgnore()]
public float[,] Population
{
get
{
return population;
}
set
{
population = value;
}
}
/// <summary>
/// Gets or sets the population as raw bytes
/// </summary>
[XmlElement("population")]
public byte[] PopulationRaw
{
get
{
return population.ToByteArray();
}
set
{
population = value.GetFloatMatrix();
}
}
/// <summary>
/// Gets width of population map
/// </summary>
[XmlIgnore]
public int PopulationWidth { get { return (population == null) ? 0 : population.GetLength(0); } }
/// <summary>
/// Gets height of population map
/// </summary>
[XmlIgnore]
public int PopulationHeight { get { return (population == null) ? 0 : population.GetLength(1); } }
[XmlArray("populationCenters")]
[XmlArrayItem("center")]
public List<Point> PopulationCenters { get; set; }
/// <summary>
/// Gets or sets the articulation road network
@ -130,6 +93,7 @@ namespace TransportGame.Model
/// </remarks>
public Map()
{
PopulationCenters = new List<Point>();
}
/// <summary>
@ -140,6 +104,7 @@ namespace TransportGame.Model
public Map(int width, int height)
{
heightmap = new float[width, height];
PopulationCenters = new List<Point>();
}
#endregion
@ -204,5 +169,33 @@ namespace TransportGame.Model
return dx * dx + dy * dy;
}
/// <summary>
/// Gets population using terrain coordinates
/// </summary>
/// <param name="x">X</param>
/// <param name="y">Y</param>
/// <returns>Population</returns>
public float GetPopulation(int x, int y)
{
const int maxDistance = 400;
float value = 0;
foreach (var point in PopulationCenters)
{
int x1 = x - point.X;
int y1 = y - point.Y;
int dist = x1 * x1 + y1 * y1;
if (dist < maxDistance * maxDistance)
{
float influence = 1 - (float)dist / (float)(maxDistance * maxDistance);
influence = Mathf.Pow(influence, 3); // Ease
value = Mathf.Clamp01(value + influence);
}
}
return value;
}
}
}

View File

@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TransportGame.Model
{
public class Point
{
public int X { get; set; }
public int Y { get; set; }
public Point()
{
}
public Point(int x, int y)
{
X = x;
Y = y;
}
}
}

View File

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

View File

@ -23,7 +23,7 @@ public class TerrainGeneratorScript : MonoBehaviour
private void GenerateTerrainThread()
{
TerrainGenerator generator = new TerrainGenerator();
CityGenerator generator = new CityGenerator();
map = generator.Generate(TerrainWidth, TerrainHeight);
}

View File

@ -3,7 +3,9 @@ using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using TransportGame.Business;
using TransportGame.Model;
namespace TransportGame.Utils
{
@ -84,5 +86,10 @@ namespace TransportGame.Utils
{
Log(Level.Critical, "{0}: {1}\nStack trace:{2}", ex.GetType().ToString(), ex.Message, ex.StackTrace);
}
public static void DumpMap(Map map, string filename)
{
map.SerializeXml(Path.Combine(LogsDirectory, filename));
}
}
}

View File

@ -86,11 +86,31 @@ namespace TransportGame.Utils
/// <typeparam name="T">Type to deserialize</typeparam>
/// <param name="filename">File name</param>
/// <returns>Deserialized object</returns>
public static T Deserialize<T>(string filename)
public static T DeserializeXml<T>(string filename)
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
var stream = new StreamReader(filename);
return (T)serializer.Deserialize(stream);
using (var stream = new StreamReader(filename))
{
T data = (T)serializer.Deserialize(stream);
stream.Close();
return data;
}
}
/// <summary>
/// Serializes an object to a file
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="data"></param>
public static void SerializeXml<T>(this T data, string filename)
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
using (StreamWriter writer = new StreamWriter(filename))
{
serializer.Serialize(writer, data);
writer.Close();
}
}
}
}