2015-03-13 15:36:12 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Xml.Serialization;
|
2015-05-20 08:26:46 +00:00
|
|
|
|
using TransportGame.Model.Road;
|
|
|
|
|
using TransportGame.Utils;
|
2015-05-22 08:26:29 +00:00
|
|
|
|
using UnityEngine;
|
2015-03-13 15:36:12 +00:00
|
|
|
|
|
|
|
|
|
namespace TransportGame.Model
|
|
|
|
|
{
|
|
|
|
|
[XmlRoot("map")]
|
|
|
|
|
public class Map
|
|
|
|
|
{
|
2015-05-21 08:49:04 +00:00
|
|
|
|
private float[,] heightmap;
|
2015-03-13 15:36:12 +00:00
|
|
|
|
|
2015-05-20 08:26:46 +00:00
|
|
|
|
#region Properties
|
|
|
|
|
|
2015-03-13 15:36:12 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the biome
|
|
|
|
|
/// </summary>
|
|
|
|
|
[XmlElement("biome")]
|
|
|
|
|
public Biome Biome { get; set; }
|
|
|
|
|
|
2015-05-20 08:26:46 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the water level
|
|
|
|
|
/// </summary>
|
|
|
|
|
[XmlElement("waterLevel")]
|
|
|
|
|
public float WaterLevel { get; set; }
|
|
|
|
|
|
2015-03-13 15:36:12 +00:00
|
|
|
|
/// <summary>
|
2015-03-23 19:17:09 +00:00
|
|
|
|
/// Gets the heights array in range [0,1]
|
2015-03-13 15:36:12 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
[XmlIgnore()]
|
2015-05-20 08:26:46 +00:00
|
|
|
|
public float[,] Heightmap
|
2015-03-13 15:36:12 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2015-05-21 08:49:04 +00:00
|
|
|
|
return heightmap;
|
2015-03-13 15:36:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the heights as raw bytes
|
|
|
|
|
/// </summary>
|
2015-05-20 08:26:46 +00:00
|
|
|
|
[XmlElement("heightmap")]
|
|
|
|
|
public byte[] HeightmapRaw
|
2015-03-13 15:36:12 +00:00
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2015-05-21 08:49:04 +00:00
|
|
|
|
return heightmap.ToByteArray();
|
2015-05-20 08:26:46 +00:00
|
|
|
|
}
|
|
|
|
|
set
|
|
|
|
|
{
|
2015-05-21 08:49:04 +00:00
|
|
|
|
heightmap = value.GetFloatMatrix();
|
2015-05-20 08:26:46 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2015-03-13 15:36:12 +00:00
|
|
|
|
|
2015-05-20 08:26:46 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets width of heightmap
|
|
|
|
|
/// </summary>
|
|
|
|
|
[XmlIgnore]
|
2015-05-21 08:49:04 +00:00
|
|
|
|
public int Width { get { return (heightmap == null) ? 0 : heightmap.GetLength(0); } }
|
2015-03-13 15:36:12 +00:00
|
|
|
|
|
2015-05-20 08:26:46 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets height of heightmap
|
|
|
|
|
/// </summary>
|
|
|
|
|
[XmlIgnore]
|
2015-05-21 08:49:04 +00:00
|
|
|
|
public int Height { get { return (heightmap == null) ? 0 : heightmap.GetLength(1); } }
|
2015-05-20 08:26:46 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
2015-05-22 08:26:29 +00:00
|
|
|
|
/// Gets or sets the population map
|
2015-05-20 08:26:46 +00:00
|
|
|
|
/// </summary>
|
2015-05-22 08:26:29 +00:00
|
|
|
|
[XmlArray("populationCenters")]
|
|
|
|
|
[XmlArrayItem("center")]
|
2015-05-29 16:03:08 +00:00
|
|
|
|
public List<Vector2> PopulationCenters { get; set; }
|
|
|
|
|
|
2015-06-10 08:49:43 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the range of one population center (distance how far it influences)
|
|
|
|
|
/// </summary>
|
2015-05-29 16:03:08 +00:00
|
|
|
|
public float PopulationCenterRange { get; set; }
|
2015-05-20 08:26:46 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the articulation road network
|
|
|
|
|
/// </summary>
|
|
|
|
|
[XmlElement("roadNetwork")]
|
|
|
|
|
public RoadNetwork RoadNetwork { get; set; }
|
|
|
|
|
|
2015-06-03 20:54:22 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the building lots
|
|
|
|
|
/// </summary>
|
|
|
|
|
[XmlElement("lots")]
|
|
|
|
|
public List<BuildingLot> BuildingLots { get; set; }
|
|
|
|
|
|
2015-06-10 08:49:43 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the buildings
|
|
|
|
|
/// </summary>
|
|
|
|
|
[XmlElement("buildings")]
|
|
|
|
|
public List<Building> Buildings { get; set; }
|
|
|
|
|
|
2015-05-20 08:26:46 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Constructors
|
|
|
|
|
|
2015-03-13 15:36:12 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes the map
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <remarks>
|
|
|
|
|
/// Warning: heights array will be null.
|
|
|
|
|
/// </remarks>
|
|
|
|
|
public Map()
|
|
|
|
|
{
|
2015-05-29 16:03:08 +00:00
|
|
|
|
PopulationCenters = new List<Vector2>();
|
2015-03-13 15:36:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Initializes the map
|
|
|
|
|
/// </summary>
|
2015-05-20 08:26:46 +00:00
|
|
|
|
/// <param name="width">Width</param>
|
|
|
|
|
/// <param name="height">Height</param>
|
2015-03-13 15:36:12 +00:00
|
|
|
|
public Map(int width, int height)
|
|
|
|
|
{
|
2015-05-21 08:49:04 +00:00
|
|
|
|
heightmap = new float[width, height];
|
2015-05-29 16:03:08 +00:00
|
|
|
|
PopulationCenters = new List<Vector2>();
|
2015-03-13 15:36:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
2015-05-20 08:26:46 +00:00
|
|
|
|
#endregion
|
|
|
|
|
|
2015-03-13 15:36:12 +00:00
|
|
|
|
/// <summary>
|
2015-05-20 08:26:46 +00:00
|
|
|
|
/// Gets the cell at specified position in range [0, Biome.Height]
|
2015-03-13 15:36:12 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="x">X</param>
|
|
|
|
|
/// <param name="y">Y</param>
|
2015-05-20 08:26:46 +00:00
|
|
|
|
/// <returns>Value</returns>
|
|
|
|
|
public float GetHeight(int x, int y)
|
2015-03-13 15:36:12 +00:00
|
|
|
|
{
|
2015-05-21 08:49:04 +00:00
|
|
|
|
return heightmap[x, y] * Biome.Height;
|
2015-03-13 15:36:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2015-05-20 08:26:46 +00:00
|
|
|
|
/// Sets the height at specified position in range [0, Biome.Height]
|
2015-03-13 15:36:12 +00:00
|
|
|
|
/// </summary>
|
2015-05-20 08:26:46 +00:00
|
|
|
|
/// <param name="x">X</param>
|
|
|
|
|
/// <param name="y">Y</param>
|
|
|
|
|
/// <param name="value">Value</param>
|
|
|
|
|
public void SetHeight(int x, int y, float value)
|
|
|
|
|
{
|
2015-05-21 08:49:04 +00:00
|
|
|
|
heightmap[x, y] = value / Biome.Height;
|
2015-05-20 08:26:46 +00:00
|
|
|
|
}
|
2015-03-13 15:36:12 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns true if specified cell is a water cell
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="x">X</param>
|
|
|
|
|
/// <param name="y">Y</param>
|
|
|
|
|
/// <returns></returns>
|
2015-05-29 16:03:08 +00:00
|
|
|
|
public bool IsWater(float x, float y)
|
|
|
|
|
{
|
|
|
|
|
return GetHeight((int)x, (int)y) <= WaterLevel;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns true if specified cell is a water cell
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="p">Position vector</param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public bool IsWater(Vector2 p)
|
2015-03-13 15:36:12 +00:00
|
|
|
|
{
|
2015-05-29 16:03:08 +00:00
|
|
|
|
return IsWater(p.X, p.Y);
|
2015-03-23 19:17:09 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns true if given coordinates is inside the map
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="x">X</param>
|
|
|
|
|
/// <param name="y">Y</param>
|
|
|
|
|
/// <returns>True if coordinates are inside the map</returns>
|
2015-05-29 16:03:08 +00:00
|
|
|
|
public bool IsInside(float x, float y)
|
2015-03-23 19:17:09 +00:00
|
|
|
|
{
|
|
|
|
|
return x >= 0 && y >= 0 && x < Width && y < Height;
|
2015-03-13 15:36:12 +00:00
|
|
|
|
}
|
2015-05-21 08:49:04 +00:00
|
|
|
|
|
2015-05-29 16:03:08 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Returns true if given coordinates is inside the map
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="p">Position vector</param>
|
|
|
|
|
/// <returns>True if coordinates are inside the map</returns>
|
|
|
|
|
public bool IsInside(Vector2 p)
|
|
|
|
|
{
|
|
|
|
|
return IsInside(p.X, p.Y);
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-21 08:49:04 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets steepness in specified point
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="x">X</param>
|
|
|
|
|
/// <param name="y">Y</param>
|
|
|
|
|
/// <returns>Steepness</returns>
|
|
|
|
|
public float GetSteepness(int x, int y)
|
|
|
|
|
{
|
|
|
|
|
if (x == 0) x++;
|
|
|
|
|
if (y == 0) y++;
|
|
|
|
|
|
|
|
|
|
float dx = GetHeight(x - 1, y) - GetHeight(x, y);
|
|
|
|
|
float dy = GetHeight(x, y - 1) - GetHeight(x, y);
|
|
|
|
|
|
|
|
|
|
return dx * dx + dy * dy;
|
|
|
|
|
}
|
2015-05-22 08:26:29 +00:00
|
|
|
|
|
2015-05-29 16:03:08 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets steepness in specified point
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="x">X</param>
|
|
|
|
|
/// <param name="y">Y</param>
|
|
|
|
|
/// <returns>Steepness</returns>
|
|
|
|
|
public float GetSteepness(float x, float y)
|
|
|
|
|
{
|
|
|
|
|
return GetSteepness((int)x, (int)y);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets steepness in specified point
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="pos">Position</param>
|
|
|
|
|
/// <returns>Steepness</returns>
|
|
|
|
|
public float GetSteepness(Vector2 pos)
|
|
|
|
|
{
|
|
|
|
|
return GetSteepness((int)pos.X, (int)pos.Y);
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-22 08:26:29 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets population using terrain coordinates
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="x">X</param>
|
|
|
|
|
/// <param name="y">Y</param>
|
|
|
|
|
/// <returns>Population</returns>
|
2015-05-29 16:03:08 +00:00
|
|
|
|
public float GetPopulation(float x, float y)
|
|
|
|
|
{
|
|
|
|
|
return GetPopulation(new Vector2(x, y));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets population using terrain coordinates
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="p">Position</param>
|
|
|
|
|
/// <returns>Population</returns>
|
|
|
|
|
public float GetPopulation(Vector2 p)
|
2015-05-22 08:26:29 +00:00
|
|
|
|
{
|
2015-05-29 16:03:08 +00:00
|
|
|
|
float value = 0.05f;
|
2015-05-22 08:26:29 +00:00
|
|
|
|
|
|
|
|
|
foreach (var point in PopulationCenters)
|
|
|
|
|
{
|
2015-05-29 16:03:08 +00:00
|
|
|
|
Vector2 diff = p - point;
|
|
|
|
|
float dist = diff.LengthSq;
|
2015-05-22 08:26:29 +00:00
|
|
|
|
|
2015-05-29 16:03:08 +00:00
|
|
|
|
if (dist < PopulationCenterRange * PopulationCenterRange)
|
2015-05-22 08:26:29 +00:00
|
|
|
|
{
|
2015-05-29 16:03:08 +00:00
|
|
|
|
float influence = 1 - (float)dist / (float)(PopulationCenterRange * PopulationCenterRange);
|
|
|
|
|
influence = Mathf.Pow(influence, 6) * 0.7f; // Ease
|
2015-05-22 08:26:29 +00:00
|
|
|
|
value = Mathf.Clamp01(value + influence);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return value;
|
|
|
|
|
}
|
2015-03-13 15:36:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|