Implemented population center generator
This commit is contained in:
		@@ -48,11 +48,14 @@
 | 
			
		||||
  <ItemGroup>
 | 
			
		||||
     <Compile Include="Assets\Scripts\Business\BiomeManager.cs" />
 | 
			
		||||
     <Compile Include="Assets\Scripts\Business\ConfigurationManager.cs" />
 | 
			
		||||
     <Compile Include="Assets\Scripts\Generator\CityGenerator.cs" />
 | 
			
		||||
     <Compile Include="Assets\Scripts\Generator\PopulationCentersGenerator.cs" />
 | 
			
		||||
     <Compile Include="Assets\Scripts\Generator\RoadGenerator.cs" />
 | 
			
		||||
     <Compile Include="Assets\Scripts\Generator\TerrainGenerator.cs" />
 | 
			
		||||
     <Compile Include="Assets\Scripts\Model\Biome.cs" />
 | 
			
		||||
     <Compile Include="Assets\Scripts\Model\Config\TerrainGeneratorConfig.cs" />
 | 
			
		||||
     <Compile Include="Assets\Scripts\Model\Map.cs" />
 | 
			
		||||
     <Compile Include="Assets\Scripts\Model\Point.cs" />
 | 
			
		||||
     <Compile Include="Assets\Scripts\Model\Road\RoadNetwork.cs" />
 | 
			
		||||
     <Compile Include="Assets\Scripts\Model\Road\RoadNode.cs" />
 | 
			
		||||
     <Compile Include="Assets\Scripts\Model\Road\RoadSegment.cs" />
 | 
			
		||||
@@ -71,7 +74,6 @@
 | 
			
		||||
     <Compile Include="Assets\Scripts\Utils\SerializationHelper.cs" />
 | 
			
		||||
     <Compile Include="Assets\Scripts\Utils\Task.cs" />
 | 
			
		||||
     <Compile Include="Assets\Scripts\Utils\Texture2DExtensions.cs" />
 | 
			
		||||
     <Compile Include="Assets\Scripts\Utils\XmlHelper.cs" />
 | 
			
		||||
     <None Include="Assets\Data\Config\tergen.xml" />
 | 
			
		||||
     <None Include="Assets\Data\Biomes\Mountain.xml" />
 | 
			
		||||
     <None Include="Assets\Standard Assets\Environment\Water (Basic)\Shaders\FXWaterBasic.shader" />
 | 
			
		||||
 
 | 
			
		||||
@@ -48,11 +48,14 @@
 | 
			
		||||
  <ItemGroup>
 | 
			
		||||
     <Compile Include="Assets\Scripts\Business\BiomeManager.cs" />
 | 
			
		||||
     <Compile Include="Assets\Scripts\Business\ConfigurationManager.cs" />
 | 
			
		||||
     <Compile Include="Assets\Scripts\Generator\CityGenerator.cs" />
 | 
			
		||||
     <Compile Include="Assets\Scripts\Generator\PopulationCentersGenerator.cs" />
 | 
			
		||||
     <Compile Include="Assets\Scripts\Generator\RoadGenerator.cs" />
 | 
			
		||||
     <Compile Include="Assets\Scripts\Generator\TerrainGenerator.cs" />
 | 
			
		||||
     <Compile Include="Assets\Scripts\Model\Biome.cs" />
 | 
			
		||||
     <Compile Include="Assets\Scripts\Model\Config\TerrainGeneratorConfig.cs" />
 | 
			
		||||
     <Compile Include="Assets\Scripts\Model\Map.cs" />
 | 
			
		||||
     <Compile Include="Assets\Scripts\Model\Point.cs" />
 | 
			
		||||
     <Compile Include="Assets\Scripts\Model\Road\RoadNetwork.cs" />
 | 
			
		||||
     <Compile Include="Assets\Scripts\Model\Road\RoadNode.cs" />
 | 
			
		||||
     <Compile Include="Assets\Scripts\Model\Road\RoadSegment.cs" />
 | 
			
		||||
@@ -71,7 +74,6 @@
 | 
			
		||||
     <Compile Include="Assets\Scripts\Utils\SerializationHelper.cs" />
 | 
			
		||||
     <Compile Include="Assets\Scripts\Utils\Task.cs" />
 | 
			
		||||
     <Compile Include="Assets\Scripts\Utils\Texture2DExtensions.cs" />
 | 
			
		||||
     <Compile Include="Assets\Scripts\Utils\XmlHelper.cs" />
 | 
			
		||||
     <None Include="Assets\Data\Config\tergen.xml" />
 | 
			
		||||
     <None Include="Assets\Data\Biomes\Mountain.xml" />
 | 
			
		||||
     <None Include="Assets\Standard Assets\Environment\Water (Basic)\Shaders\FXWaterBasic.shader" />
 | 
			
		||||
 
 | 
			
		||||
@@ -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
 | 
			
		||||
 
 | 
			
		||||
@@ -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));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										43
									
								
								Game/Assets/Scripts/Generator/CityGenerator.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										43
									
								
								Game/Assets/Scripts/Generator/CityGenerator.cs
									
									
									
									
									
										Normal 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;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										12
									
								
								Game/Assets/Scripts/Generator/CityGenerator.cs.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								Game/Assets/Scripts/Generator/CityGenerator.cs.meta
									
									
									
									
									
										Normal 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: 
 | 
			
		||||
							
								
								
									
										36
									
								
								Game/Assets/Scripts/Generator/PopulationCentersGenerator.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										36
									
								
								Game/Assets/Scripts/Generator/PopulationCentersGenerator.cs
									
									
									
									
									
										Normal 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));
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -0,0 +1,12 @@
 | 
			
		||||
fileFormatVersion: 2
 | 
			
		||||
guid: 186756c7c690a3d4c8e633e3007523e0
 | 
			
		||||
timeCreated: 1432200350
 | 
			
		||||
licenseType: Free
 | 
			
		||||
MonoImporter:
 | 
			
		||||
  serializedVersion: 2
 | 
			
		||||
  defaultReferences: []
 | 
			
		||||
  executionOrder: 0
 | 
			
		||||
  icon: {instanceID: 0}
 | 
			
		||||
  userData: 
 | 
			
		||||
  assetBundleName: 
 | 
			
		||||
  assetBundleVariant: 
 | 
			
		||||
@@ -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();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										23
									
								
								Game/Assets/Scripts/Model/Point.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								Game/Assets/Scripts/Model/Point.cs
									
									
									
									
									
										Normal 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;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										12
									
								
								Game/Assets/Scripts/Model/Point.cs.meta
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								Game/Assets/Scripts/Model/Point.cs.meta
									
									
									
									
									
										Normal 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: 
 | 
			
		||||
@@ -23,7 +23,7 @@ public class TerrainGeneratorScript : MonoBehaviour
 | 
			
		||||
 | 
			
		||||
	private void GenerateTerrainThread()
 | 
			
		||||
	{
 | 
			
		||||
		TerrainGenerator generator = new TerrainGenerator();
 | 
			
		||||
		CityGenerator generator = new CityGenerator();
 | 
			
		||||
		map = generator.Generate(TerrainWidth, TerrainHeight);
 | 
			
		||||
	}
 | 
			
		||||
    
 | 
			
		||||
 
 | 
			
		||||
@@ -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));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -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();
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
										
											Binary file not shown.
										
									
								
							@@ -72,11 +72,14 @@
 | 
			
		||||
  <ItemGroup>
 | 
			
		||||
    <Compile Include="Assets\Scripts\Business\BiomeManager.cs" />
 | 
			
		||||
    <Compile Include="Assets\Scripts\Business\ConfigurationManager.cs" />
 | 
			
		||||
    <Compile Include="Assets\Scripts\Generator\CityGenerator.cs" />
 | 
			
		||||
    <Compile Include="Assets\Scripts\Generator\PopulationCentersGenerator.cs" />
 | 
			
		||||
    <Compile Include="Assets\Scripts\Generator\RoadGenerator.cs" />
 | 
			
		||||
    <Compile Include="Assets\Scripts\Generator\TerrainGenerator.cs" />
 | 
			
		||||
    <Compile Include="Assets\Scripts\Model\Biome.cs" />
 | 
			
		||||
    <Compile Include="Assets\Scripts\Model\Config\TerrainGeneratorConfig.cs" />
 | 
			
		||||
    <Compile Include="Assets\Scripts\Model\Map.cs" />
 | 
			
		||||
    <Compile Include="Assets\Scripts\Model\Point.cs" />
 | 
			
		||||
    <Compile Include="Assets\Scripts\Model\Road\RoadNetwork.cs" />
 | 
			
		||||
    <Compile Include="Assets\Scripts\Model\Road\RoadNode.cs" />
 | 
			
		||||
    <Compile Include="Assets\Scripts\Model\Road\RoadSegment.cs" />
 | 
			
		||||
 
 | 
			
		||||
@@ -12,7 +12,8 @@ namespace TransportGame.MapViewer
 | 
			
		||||
        {
 | 
			
		||||
            None = 0,
 | 
			
		||||
            Elevation = 1,
 | 
			
		||||
            All = Elevation 
 | 
			
		||||
            Population = 2,
 | 
			
		||||
            All = Elevation | Population
 | 
			
		||||
        };
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
@@ -36,7 +37,7 @@ namespace TransportGame.MapViewer
 | 
			
		||||
 | 
			
		||||
        private readonly Color WaterColor = Colors.Navy;
 | 
			
		||||
        private readonly Color ElevationTerrainColor = Colors.White;
 | 
			
		||||
        private readonly Color TerrainColor = Colors.Gray;
 | 
			
		||||
        private readonly Color TerrainColor = Colors.Silver;
 | 
			
		||||
 | 
			
		||||
        public MapRenderer()
 | 
			
		||||
        {
 | 
			
		||||
@@ -57,14 +58,24 @@ namespace TransportGame.MapViewer
 | 
			
		||||
                Bitmap24 bitmap = new Bitmap24(Convert.ToInt32(map.Width * Scale), Convert.ToInt32(map.Height * Scale));
 | 
			
		||||
 | 
			
		||||
                // First layer - cells
 | 
			
		||||
                DrawCells(bitmap, map, (layers & Layers.Elevation) > 0);
 | 
			
		||||
                DrawPixels(bitmap, map, (layers & Layers.Elevation) > 0, (layers & Layers.Population) > 0);
 | 
			
		||||
 | 
			
		||||
                // Done
 | 
			
		||||
                return bitmap;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void DrawCells(Bitmap24 bitmap, Map map, bool elevation)
 | 
			
		||||
        private Color AlphaBlend(Color top, Color dst)
 | 
			
		||||
        {
 | 
			
		||||
            float A = top.ScA + dst.ScA * (1 - top.ScA);
 | 
			
		||||
            float R = (top.ScR * top.ScA + dst.ScR * (1 - top.ScA)) / A;
 | 
			
		||||
            float G = (top.ScG * top.ScA + dst.ScG * (1 - top.ScA)) / A;
 | 
			
		||||
            float B = (top.ScB * top.ScA + dst.ScB * (1 - top.ScA)) / A;
 | 
			
		||||
 | 
			
		||||
            return Color.FromScRgb(A, R, G, B);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        private void DrawPixels(Bitmap24 bitmap, Map map, bool elevation, bool population)
 | 
			
		||||
        {
 | 
			
		||||
            for (int x = 0; x < bitmap.Width; x++)
 | 
			
		||||
                for (int y = 0; y < bitmap.Height; y++)
 | 
			
		||||
@@ -76,17 +87,24 @@ namespace TransportGame.MapViewer
 | 
			
		||||
                    if (map.IsWater(mapX, mapY))
 | 
			
		||||
                        bitmap[x, y] = WaterColor;
 | 
			
		||||
 | 
			
		||||
                    else
 | 
			
		||||
                    {
 | 
			
		||||
                        // Draw elevation
 | 
			
		||||
                    else if (elevation)
 | 
			
		||||
                        if (elevation)
 | 
			
		||||
                        {
 | 
			
		||||
                            float alpha = map.Heightmap[mapX, mapY]; // map.Heights range is [0,1]
 | 
			
		||||
                            bitmap[x, y] = Color.Multiply(ElevationTerrainColor, alpha);
 | 
			
		||||
                        }
 | 
			
		||||
 | 
			
		||||
                    // Draw terrain
 | 
			
		||||
                    else
 | 
			
		||||
                        // No elevation, just put a terrain color
 | 
			
		||||
                        else bitmap[x, y] = TerrainColor;
 | 
			
		||||
 | 
			
		||||
                        // Population
 | 
			
		||||
                        if (population)
 | 
			
		||||
                        {
 | 
			
		||||
                        bitmap[x, y] = TerrainColor;
 | 
			
		||||
                            Color popColor = Color.FromScRgb(map.GetPopulation(mapX, mapY) * 0.7f, 1, 0, 1);
 | 
			
		||||
                            bitmap[x, y] = AlphaBlend(popColor, bitmap[x, y]);
 | 
			
		||||
                        }
 | 
			
		||||
                    }
 | 
			
		||||
                }
 | 
			
		||||
        }
 | 
			
		||||
 
 | 
			
		||||
@@ -61,9 +61,7 @@
 | 
			
		||||
                <StackPanel Orientation="Vertical">
 | 
			
		||||
                    <CheckBox IsChecked="{Binding LayersAll}">(All)</CheckBox>
 | 
			
		||||
                    <CheckBox IsChecked="{Binding LayerElevation}">Elevation</CheckBox>
 | 
			
		||||
                    <CheckBox>Roads</CheckBox>
 | 
			
		||||
                    <CheckBox>Buildings</CheckBox>
 | 
			
		||||
                    <CheckBox>Vegetation</CheckBox>
 | 
			
		||||
                    <CheckBox IsChecked="{Binding LayerPopulation}">Population</CheckBox>
 | 
			
		||||
                    
 | 
			
		||||
                    <Button Name="buttonRender" Grid.Column="1" Content="Render" Click="buttonRender_Click" />
 | 
			
		||||
                    
 | 
			
		||||
 
 | 
			
		||||
@@ -109,10 +109,10 @@ namespace TransportGame.MapViewer
 | 
			
		||||
        {
 | 
			
		||||
            get
 | 
			
		||||
            {
 | 
			
		||||
                if (LayerElevation)
 | 
			
		||||
                if (LayerElevation && LayerPopulation)
 | 
			
		||||
                    return true;
 | 
			
		||||
 | 
			
		||||
                if (!LayerElevation)
 | 
			
		||||
                if (!LayerElevation && !LayerPopulation)
 | 
			
		||||
                    return false;
 | 
			
		||||
 | 
			
		||||
                return null;
 | 
			
		||||
@@ -122,12 +122,13 @@ namespace TransportGame.MapViewer
 | 
			
		||||
                if (value.HasValue)
 | 
			
		||||
                {
 | 
			
		||||
                    LayerElevation = value.Value;
 | 
			
		||||
                    LayerPopulation = value.Value;
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Gets or sets the layer elevation layer flag
 | 
			
		||||
        /// Gets or sets the elevation layer flag
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        public bool LayerElevation
 | 
			
		||||
        {
 | 
			
		||||
@@ -148,6 +149,28 @@ namespace TransportGame.MapViewer
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
        /// Gets or sets the population layer flag
 | 
			
		||||
        /// </summary>
 | 
			
		||||
        public bool LayerPopulation
 | 
			
		||||
        {
 | 
			
		||||
            get
 | 
			
		||||
            {
 | 
			
		||||
                return _layers.HasFlag(MapRenderer.Layers.Population);
 | 
			
		||||
            }
 | 
			
		||||
            set
 | 
			
		||||
            {
 | 
			
		||||
                if (value) _layers |= MapRenderer.Layers.Population;
 | 
			
		||||
                else _layers &= ~MapRenderer.Layers.Population;
 | 
			
		||||
 | 
			
		||||
                if (PropertyChanged != null)
 | 
			
		||||
                {
 | 
			
		||||
                    PropertyChanged(this, new PropertyChangedEventArgs("LayerPopulation"));
 | 
			
		||||
                    PropertyChanged(this, new PropertyChangedEventArgs("LayersAll"));
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        #endregion
 | 
			
		||||
 | 
			
		||||
        /// <summary>
 | 
			
		||||
 
 | 
			
		||||
@@ -6,6 +6,7 @@ using System.Text;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
using System.Xml.Serialization;
 | 
			
		||||
using TransportGame.Model;
 | 
			
		||||
using TransportGame.Utils;
 | 
			
		||||
 | 
			
		||||
namespace TransportGame.MapViewer.Storage
 | 
			
		||||
{
 | 
			
		||||
@@ -13,16 +14,7 @@ namespace TransportGame.MapViewer.Storage
 | 
			
		||||
    {
 | 
			
		||||
        public static Map Read(string file)
 | 
			
		||||
        {
 | 
			
		||||
            XmlSerializer serializer = new XmlSerializer(typeof(Map));
 | 
			
		||||
            Map map;
 | 
			
		||||
 | 
			
		||||
            using (StreamReader reader = new StreamReader(file))
 | 
			
		||||
            {
 | 
			
		||||
                map = (Map)serializer.Deserialize(reader);
 | 
			
		||||
                reader.Close();
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            return map;
 | 
			
		||||
            return SerializationHelper.DeserializeXml<Map>(file);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user