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")]
|
|
|
|
|
public List<Point> PopulationCenters { 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; }
|
|
|
|
|
|
|
|
|
|
#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-22 08:26:29 +00:00
|
|
|
|
PopulationCenters = new List<Point>();
|
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-22 08:26:29 +00:00
|
|
|
|
PopulationCenters = new List<Point>();
|
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>
|
|
|
|
|
public bool IsWater(int x, int y)
|
|
|
|
|
{
|
2015-05-20 08:26:46 +00:00
|
|
|
|
return GetHeight(x, y) <= WaterLevel;
|
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>
|
|
|
|
|
public bool IsInside(int x, int y)
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
/// <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
|
|
|
|
|
|
|
|
|
/// <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;
|
|
|
|
|
}
|
2015-03-13 15:36:12 +00:00
|
|
|
|
}
|
|
|
|
|
}
|