using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using TransportGame.Model.Road;
using TransportGame.Utils;
namespace TransportGame.Model
{
[XmlRoot("map")]
public class Map
{
private float[,] heightmap;
private float[,] population;
#region Properties
///
/// Gets or sets the biome
///
[XmlElement("biome")]
public Biome Biome { get; set; }
///
/// Gets or sets the water level
///
[XmlElement("waterLevel")]
public float WaterLevel { get; set; }
///
/// Gets the heights array in range [0,1]
///
[XmlIgnore()]
public float[,] Heightmap
{
get
{
return heightmap;
}
}
///
/// Gets or sets the heights as raw bytes
///
[XmlElement("heightmap")]
public byte[] HeightmapRaw
{
get
{
return heightmap.ToByteArray();
}
set
{
heightmap = value.GetFloatMatrix();
}
}
///
/// Gets width of heightmap
///
[XmlIgnore]
public int Width { get { return (heightmap == null) ? 0 : heightmap.GetLength(0); } }
///
/// Gets height of heightmap
///
[XmlIgnore]
public int Height { get { return (heightmap == null) ? 0 : heightmap.GetLength(1); } }
///
/// Gets the population map
///
[XmlIgnore()]
public float[,] Population
{
get
{
return population;
}
set
{
population = value;
}
}
///
/// Gets or sets the population as raw bytes
///
[XmlElement("population")]
public byte[] PopulationRaw
{
get
{
return population.ToByteArray();
}
set
{
population = value.GetFloatMatrix();
}
}
///
/// Gets width of population map
///
[XmlIgnore]
public int PopulationWidth { get { return (population == null) ? 0 : population.GetLength(0); } }
///
/// Gets height of population map
///
[XmlIgnore]
public int PopulationHeight { get { return (population == null) ? 0 : population.GetLength(1); } }
///
/// Gets or sets the articulation road network
///
[XmlElement("roadNetwork")]
public RoadNetwork RoadNetwork { get; set; }
#endregion
#region Constructors
///
/// Initializes the map
///
///
/// Warning: heights array will be null.
///
public Map()
{
}
///
/// Initializes the map
///
/// Width
/// Height
public Map(int width, int height)
{
heightmap = new float[width, height];
}
#endregion
///
/// Gets the cell at specified position in range [0, Biome.Height]
///
/// X
/// Y
/// Value
public float GetHeight(int x, int y)
{
return heightmap[x, y] * Biome.Height;
}
///
/// Sets the height at specified position in range [0, Biome.Height]
///
/// X
/// Y
/// Value
public void SetHeight(int x, int y, float value)
{
heightmap[x, y] = value / Biome.Height;
}
///
/// Returns true if specified cell is a water cell
///
/// X
/// Y
///
public bool IsWater(int x, int y)
{
return GetHeight(x, y) <= WaterLevel;
}
///
/// Returns true if given coordinates is inside the map
///
/// X
/// Y
/// True if coordinates are inside the map
public bool IsInside(int x, int y)
{
return x >= 0 && y >= 0 && x < Width && y < Height;
}
///
/// Gets steepness in specified point
///
/// X
/// Y
/// Steepness
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;
}
}
}