Skip to content

Commit

Permalink
Face prevailing wind based on ERA5 data
Browse files Browse the repository at this point in the history
  • Loading branch information
jetelain committed Sep 24, 2023
1 parent 739f9c5 commit 78d0e75
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 13 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@
[submodule "gemini"]
path = gemini
url = https://github.com/jetelain/gemini.git
[submodule "weatherstats"]
path = weatherstats
url = https://github.com/jetelain/weatherstats.git
2 changes: 1 addition & 1 deletion GameRealisticMap.Arma3/Builtin/CentralEurope.grma3a
Original file line number Diff line number Diff line change
Expand Up @@ -2410,7 +2410,7 @@
"Objects": [
{
"Model": "a3\\structures_f\\ind\\windpowerplant\\wpp_turbine_v2_f.p3d",
"Transform": [-1,0,-8.742278E-08,0,1,0,8.742278E-08,0,-1,-0.049999904,21.466734,1.1]
"Transform": [1,0,-1.7484557E-07,0,1,0,1.7484557E-07,0,1,0.049999807,21.466734,-1.1]
}
]
}
Expand Down
28 changes: 16 additions & 12 deletions GameRealisticMap.Arma3/ManMade/BuildingGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
using System;
using System.Diagnostics;
using System.Numerics;
using System.Numerics;
using GameRealisticMap.Algorithms;
using GameRealisticMap.Arma3.Assets;
using GameRealisticMap.Arma3.TerrainBuilder;
using GameRealisticMap.Geometries;
using GameRealisticMap.ManMade.Buildings;
using GameRealisticMap.ManMade.Roads;
using GameRealisticMap.Nature.Weather;
using GameRealisticMap.Reporting;

namespace GameRealisticMap.Arma3.ManMade
Expand Down Expand Up @@ -54,12 +53,18 @@ public IEnumerable<TerrainBuilderObject> Generate(IArma3MapConfig config, IConte
var buildings = context.GetData<BuildingsData>().Buildings;
var roads = context.GetData<RoadsData>().Roads;

var prevailingWind = 0f;
if (buildings.Any(b => b.TypeId == BuildingTypeId.WindTurbine))
{
prevailingWind = context.GetData<WeatherData>().GetPrevailingWindAngle();
}

var nonefits = 0;
using var report = progress.CreateStep("PlaceBuildings", buildings.Count);
foreach (var building in buildings)
{
if (!TryPlaceBuilding(result, roads, building, 0.5f, 1.25f)
&& !TryPlaceBuildingIfNotOverlapping(buildings, result, roads, building, 1.25f, 10f))
if (!TryPlaceBuilding(result, roads, building, 0.5f, 1.25f, prevailingWind)
&& !TryPlaceBuildingIfNotOverlapping(buildings, result, roads, building, 1.25f, 10f, prevailingWind))
{
nonefits++;
}
Expand All @@ -69,13 +74,13 @@ public IEnumerable<TerrainBuilderObject> Generate(IArma3MapConfig config, IConte
return result.SelectMany(p => p.ToObjects());
}

private bool TryPlaceBuildingIfNotOverlapping(List<Building> wanted, List<Placed> buildings, List<Road> roads, Building building, float min, float max)
private bool TryPlaceBuildingIfNotOverlapping(List<Building> wanted, List<Placed> buildings, List<Road> roads, Building building, float min, float max, float prevailingWind)
{
var candidates = GetBuildings(building, min, max);
if (candidates.Count > 0)
{
var obj = PickOne(building, candidates);
var realbox = RealBoxAdjustedToRoad(roads, obj.Size, GetFitBox(building, min, max, obj));
var realbox = RealBoxAdjustedToRoad(roads, obj.Size, GetFitBox(building, min, max, obj, prevailingWind));
if (!wanted.Where(b => b != building).Any(b => b.Box.Polygon.Intersects(realbox.Polygon))
&& !buildings.Any(b => b.Box.Polygon.Intersects(realbox.Polygon)))
{
Expand All @@ -87,13 +92,13 @@ private bool TryPlaceBuildingIfNotOverlapping(List<Building> wanted, List<Placed
}


private bool TryPlaceBuilding(List<Placed> buildings, List<Road> roads, Building building, float min, float max)
private bool TryPlaceBuilding(List<Placed> buildings, List<Road> roads, Building building, float min, float max, float prevailingWind)
{
var candidates = GetBuildings(building, min, max);
if (candidates.Count > 0)
{
var obj = PickOne(building, candidates);
var box = GetFitBox(building, min, max, obj);
var box = GetFitBox(building, min, max, obj, prevailingWind);
if (box.Width < obj.Size.X || box.Height < obj.Size.Y) // Object is larger than wanted box
{
box = RealBoxAdjustedToRoad(roads, obj.Size, box);
Expand All @@ -104,12 +109,11 @@ private bool TryPlaceBuilding(List<Placed> buildings, List<Road> roads, Building
return false;
}

internal BoundingBox GetFitBox(Building building, float min, float max, BuildingDefinition obj)
internal BoundingBox GetFitBox(Building building, float min, float max, BuildingDefinition obj, float prevailingWind)
{
if (building.TypeId == BuildingTypeId.WindTurbine)
{
// TODO: Must face dominant wind (hardcoded to WEST)
return new BoundingBox(building.Box.Center, obj.Size.X, obj.Size.Y, 90);
return new BoundingBox(building.Box.Center, obj.Size.X, obj.Size.Y, prevailingWind);
}

// By convention, building entrance is at SOUTH (Bottom) of model
Expand Down
2 changes: 2 additions & 0 deletions GameRealisticMap/BuildersCatalog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using GameRealisticMap.Nature.Surfaces;
using GameRealisticMap.Nature.Trees;
using GameRealisticMap.Nature.Watercourses;
using GameRealisticMap.Nature.Weather;
using GameRealisticMap.Reporting;
using GameRealisticMap.Satellite;

Expand Down Expand Up @@ -70,6 +71,7 @@ public BuildersCatalog(IProgressSystem progress, IBuildersConfig config)
Register(new DefaultAgriculturalAreasBuilder(progress));
Register(new ConditionEvaluatorBuilder());
Register(new ElevationContourBuilder(progress));
Register(new WeatherBuilder(progress));
}

public void Register<TData>(IDataBuilder<TData> builder)
Expand Down
1 change: 1 addition & 0 deletions GameRealisticMap/GameRealisticMap.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
<ItemGroup>
<ProjectReference Include="..\HugeImages\HugeImages\HugeImages.csproj" />
<ProjectReference Include="..\mapkit\MapToolkit\MapToolkit.csproj" />
<ProjectReference Include="..\weatherstats\WeatherStats\WeatherStats.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
31 changes: 31 additions & 0 deletions GameRealisticMap/Nature/Weather/WeatherBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using GameRealisticMap.Reporting;
using WeatherStats.Databases;

namespace GameRealisticMap.Nature.Weather
{
internal class WeatherBuilder : IDataBuilder<WeatherData>
{
private readonly IProgressSystem progress;

public WeatherBuilder(IProgressSystem progress)
{
this.progress = progress;
}

public WeatherData Build(IBuildContext context)
{
var db = WeatherStatsDatabase.Create("https://weatherdata.pmad.net/ERA5AVG/");

var center = context.Area.TerrainPointToLatLng(
new Geometries.TerrainPoint(
context.Area.SizeInMeters / 2,
context.Area.SizeInMeters / 2));

using var report = progress.CreateStep("WeatherStats", 1);

var data = db.GetStats(center.Y, center.X).Result;

return new WeatherData(data);
}
}
}
49 changes: 49 additions & 0 deletions GameRealisticMap/Nature/Weather/WeatherData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using WeatherStats;
using WeatherStats.Stats;

namespace GameRealisticMap.Nature.Weather
{
public class WeatherData
{
public WeatherData(YearWeatherStatsPoint? data)
{
Data = data;
}

public YearWeatherStatsPoint? Data { get; }

public float GetPrevailingWindAngle()
{
if ( Data == null )
{
return ToAngle(WindDirection8.West);
}
var perMonth = Data.Months.Select(m => m.WindDirection.Prevailing).ToList();
return ToAngle(perMonth.Distinct().OrderByDescending(m => perMonth.Count(e => e == m)).First());
}

private static float ToAngle(WindDirection8 direction)
{
switch (direction)
{
case WindDirection8.North:
return 0;
case WindDirection8.NorthEast:
return -45;
case WindDirection8.East:
return -90;
case WindDirection8.SouthEast:
return -135;
case WindDirection8.South:
return 180;
case WindDirection8.NorthWest:
return 45;
case WindDirection8.West:
return 90;
case WindDirection8.SouthWest:
return 135;
}
return -90;
}
}
}
1 change: 1 addition & 0 deletions weatherstats
Submodule weatherstats added at 861c54

0 comments on commit 78d0e75

Please sign in to comment.