Skip to content

Commit

Permalink
Add factory to load system bodies from JSON
Browse files Browse the repository at this point in the history
Migrate Mars, Luna and Halley's comet to the new JSON loader
  • Loading branch information
behindcurtain3 committed Mar 29, 2024
1 parent 51d6eac commit a1314b9
Show file tree
Hide file tree
Showing 6 changed files with 282 additions and 7 deletions.
22 changes: 22 additions & 0 deletions Pulsar4X/GameEngine/Data/basemod/bodies/halleyscomet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "Halleys Comet",
"colonizeable": false,
"info": {
"gravity": 0,
"type": "comet",
"albedo": 0.04,
"mass": 2.2E14,
"radius": 11
},
"orbit": {
"semiMajorAxis": 2.6679E9,
"eccentricity": 0.96714,
"eclipticInclination": 180,
"LoAN": 58.42,
"AoP": 111.33,
"meanAnomaly": 38.38
},
"geoSurvey": {
"pointsRequired": 575
}
}
22 changes: 22 additions & 0 deletions Pulsar4X/GameEngine/Data/basemod/bodies/luna.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "Luna",
"parent": "Earth",
"colonizeable": true,
"info": {
"gravity": 1.625,
"type": "moon",
"mass": 7.34767309E22,
"radius": 6378.1
},
"orbit": {
"semiMajorAxis": 0.3844E6,
"eccentricity": 0.0549,
"eclipticInclination": 0,
"LoAN": 125.08,
"AoP": 318.0634,
"meanAnomaly": 115.3654
},
"geoSurvey": {
"pointsRequired": 250
}
}
43 changes: 43 additions & 0 deletions Pulsar4X/GameEngine/Data/basemod/bodies/mars.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"name": "Mars",
"colonizeable": true,
"info": {
"gravity": 3.72076,
"type": "terrestrial",
"albedo": 0.25,
"mass": 0.64174E24,
"radius": 3396.2,
"tectonics": "earth-like"
},
"orbit": {
"semiMajorAxis": 227.92E6,
"eccentricity": 0.0934,
"eclipticInclination": 0,
"LoAN": 49.57854,
"AoP": 336.04084,
"meanAnomaly": 355.45332
},
"atmosphere": {
"pressure": 0.87,
"hydrosphere": false,
"hyrdoExtent": 0,
"greenhouseFactor": 0,
"greenhousePressure": 0,
"surfaceTemperature": -63,
"gases": [
{ "symbol": "CO2", "percent": 0.9597 },
{ "symbol": "Ar", "percent": 0.0193 },
{ "symbol": "N2", "percent": 0.0189 },
{ "symbol": "O2", "percent": 0.00146 },
{ "symbol": "CO", "percent": 0.000557 },
{ "symbol": "H2O", "percent": 0.00021 },
{ "symbol": "NO", "percent": 0.0001 },
{ "symbol": "Ne", "percent": 0.0000025 },
{ "symbol": "Kr", "percent": 0.0000003 },
{ "symbol": "Xe", "percent": 0.0000001 }
]
},
"geoSurvey": {
"pointsRequired": 575
}
}
10 changes: 7 additions & 3 deletions Pulsar4X/GameEngine/Engine/Factories/StarSystemFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Pulsar4X.Extensions;
using Pulsar4X.Engine.Sensors;
using Pulsar4X.Engine.Sol;
using Pulsar4X.Engine.Factories;

namespace Pulsar4X.Engine
{
Expand Down Expand Up @@ -209,11 +210,13 @@ public StarSystem CreateSol(Game game)
Entity earth = SolEntities.Earth(game, sol, sun, GalaxyGen.Settings.J2000, new SensorProfileDB());
_systemBodyFactory.HomeworldMineralGeneration(game.StartingGameData.Minerals.Values.ToList(), sol, earth);
#region Earth Moon
Entity luna = SolEntities.Luna(game, sol, sun, earth, GalaxyGen.Settings.J2000, new SensorProfileDB());
//Entity luna = SolEntities.Luna(game, sol, sun, earth, GalaxyGen.Settings.J2000, new SensorProfileDB());
var luna = SystemBodyFromJsonFactory.Create(game, sol, sun, GalaxyGen.Settings.J2000, new SensorProfileDB(), "Data/basemod/bodies/luna.json");
_systemBodyFactory.MineralGeneration(game.StartingGameData.Minerals.Values.ToList(), sol, luna);
#endregion

Entity mars = SolEntities.Mars(game, sol, sun, GalaxyGen.Settings.J2000, new SensorProfileDB());
//Entity mars = SolEntities.Mars(game, sol, sun, GalaxyGen.Settings.J2000, new SensorProfileDB());
var mars = SystemBodyFromJsonFactory.Create(game, sol, sun, GalaxyGen.Settings.J2000, new SensorProfileDB(), "Data/basemod/bodies/mars.json");
_systemBodyFactory.MineralGeneration(game.StartingGameData.Minerals.Values.ToList(), sol, mars);

Entity jupiter = SolEntities.Jupiter(game, sol, sun, GalaxyGen.Settings.J2000, new SensorProfileDB());
Expand Down Expand Up @@ -294,7 +297,8 @@ public StarSystem CreateSol(Game game)
_systemBodyFactory.MineralGeneration(game.StartingGameData.Minerals.Values.ToList(), sol, ceres);

// Comets
Entity halleysComet = SolEntities.HalleysComet(game, sol, sun, new System.DateTime(1994, 2, 17), new SensorProfileDB());
//Entity halleysComet = SolEntities.HalleysComet(game, sol, sun, new System.DateTime(1994, 2, 17), new SensorProfileDB());
var halleysComet = SystemBodyFromJsonFactory.Create(game, sol, sun, new System.DateTime(1994, 2, 17), new SensorProfileDB(), "Data/basemod/bodies/halleyscomet.json");
_systemBodyFactory.MineralGeneration(game.StartingGameData.Minerals.Values.ToList(), sol, halleysComet);

// Clean up cached RNG:
Expand Down
187 changes: 187 additions & 0 deletions Pulsar4X/GameEngine/Engine/Factories/SystemBodyFromJsonFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json.Linq;
using Pulsar4X.Datablobs;
using Pulsar4X.DataStructures;
using Pulsar4X.Engine.Sensors;
using Pulsar4X.Extensions;
using Pulsar4X.Orbital;

namespace Pulsar4X.Engine.Factories;

public static class SystemBodyFromJsonFactory
{
public static Entity Create(Game game, StarSystem system, Entity sun, DateTime epoch, SensorProfileDB sensorProfileDB, string filePath)
{
string fileContents = File.ReadAllText(filePath);
var rootJson = JObject.Parse(fileContents);
var info = rootJson["info"];

var blobsToAdd = new List<BaseDataBlob>();
var sunMassVolumeDB = sun.GetDataBlob<MassVolumeDB>();

var nameDb = new NameDB(rootJson["name"].ToString());
blobsToAdd.Add(nameDb);
blobsToAdd.Add(sensorProfileDB);

var systemBodyInfoDB = new SystemBodyInfoDB()
{
Gravity = (double?)info["gravity"] ?? 0,
BodyType = info["type"] != null ? GetBodyType(info["type"].ToString()) : BodyType.Unknown,
Tectonics = info["tectonics"] != null ? GetTectonicsType(info["tectonics"].ToString()) : TectonicActivity.Unknown,
Albedo = info["albedo"] != null ? new PercentValue(float.Parse(info["albedo"].ToString())) : 0,
AxialTilt = (float?)info["axialTilt"] ?? 0,
MagneticField = (float?)info["magneticField"] ?? 0,
BaseTemperature = (float?)info["baseTemperature"] ?? 0,
RadiationLevel = (float?)info["radiationLevel"] ?? 0,
AtmosphericDust = (float?)info["atmosphericDust"] ?? 0,
LengthOfDay = info["lengthOfDay"] != null ? TimeSpan.Parse(info["lengthOfDay"].ToString()) : TimeSpan.Zero
};
blobsToAdd.Add(systemBodyInfoDB);

var massVolumeDB = MassVolumeDB.NewFromMassAndRadius_AU(
(double?)info["mass"] ?? 0,
Distance.KmToAU((double?)info["radius"] ?? 0)
);
blobsToAdd.Add(massVolumeDB);

var orbit = rootJson["orbit"];
double semiMajorAxis_AU = Distance.KmToAU((double?)orbit["semiMajorAxis"] ?? 0);
double eccentricity = (double?)orbit["eccentricity"] ?? 0;
double eclipticInclination = (double?)orbit["eclipticInclination"] ?? 0;
double loAN = (double?)orbit["LoAN"] ?? 0;
double AoP = (double?)orbit["AoP"] ?? 0;
double meanAnomaly = (double?)orbit["meanAnomaly"] ?? 0;

OrbitDB orbitDB;
var parentBody = sun;
var parentMassVolumeDB = sunMassVolumeDB;

if(rootJson["parent"] != null)
{
parentBody = NameLookup.GetFirstEntityWithName(system, rootJson["parent"].ToString());
parentMassVolumeDB = parentBody.GetDataBlob<MassVolumeDB>();
}

switch(systemBodyInfoDB.BodyType)
{
case BodyType.Asteroid:
case BodyType.Comet:
case BodyType.Moon:
orbitDB = OrbitDB.FromAsteroidFormat(
parentBody,
parentMassVolumeDB.MassDry,
massVolumeDB.MassDry,
semiMajorAxis_AU,
eccentricity,
eclipticInclination,
loAN,
AoP,
meanAnomaly,
epoch);
break;
default:
orbitDB = OrbitDB.FromMajorPlanetFormat(
parentBody,
parentMassVolumeDB.MassDry,
massVolumeDB.MassDry,
semiMajorAxis_AU,
eccentricity,
eclipticInclination,
loAN,
AoP,
meanAnomaly,
epoch);
break;
}

systemBodyInfoDB.BaseTemperature = (float)SystemBodyFactory.CalculateBaseTemperatureOfBody(sun, orbitDB);

var positionDB = new PositionDB(
orbitDB.GetPosition(game.TimePulse.GameGlobalDateTime),
system.Guid,
parentBody);
blobsToAdd.Add(positionDB);
blobsToAdd.Add(orbitDB); // orbit needs to be added after position

if(rootJson["atmosphere"] != null)
{
var atmosphere = rootJson["atmosphere"];
var pressure = (float?)atmosphere["pressure"] ?? 0;
var gasesJson = (JArray?)atmosphere["gases"];
var gases = new Dictionary<string, float>();
foreach(var gas in gasesJson)
{
string symbol = gas["symbol"].ToString();
float percent = (float?)gas["percent"] ?? 0;
gases.Add(
game.GetGasBySymbol(symbol).UniqueID,
percent * pressure
);
}

var atmosphereDB = new AtmosphereDB(
pressure,
(bool?)atmosphere["hydrosphere"] ?? false,
(decimal?)atmosphere["hydroExtent"] ?? 0,
(float?)atmosphere["greenhouseFactor"] ?? 0,
(float?)atmosphere["greenhousePressure"] ?? 0,
(float?)atmosphere["surfaceTemperature"] ?? 0,
gases
);
blobsToAdd.Add(atmosphereDB);
}

if(rootJson["geoSurvey"] != null)
{
var geoSurveyableDB = new GeoSurveyableDB()
{
PointsRequired = (uint?)rootJson["geoSurvey"]["pointsRequired"] ?? 1000
};

blobsToAdd.Add(geoSurveyableDB);
}

if(rootJson["colonizeable"] != null)
{
bool isColonizeable = (bool?)rootJson["colonizeable"] ?? false;
if(isColonizeable)
blobsToAdd.Add(new ColonizeableDB());
}

Entity body = Entity.Create();
system.AddEntity(body, blobsToAdd);
SensorTools.PlanetEmmisionSig(sensorProfileDB, systemBodyInfoDB, massVolumeDB);
return body;
}

private static BodyType GetBodyType(string bodyType)
{
return bodyType switch
{
"terrestrial" => BodyType.Terrestrial,
"gasGiant" => BodyType.GasGiant,
"iceGiant" => BodyType.IceGiant,
"dwarfPlanet" => BodyType.DwarfPlanet,
"gasDwarf" => BodyType.GasDwarf,
"moon" => BodyType.Moon,
"asteroid" => BodyType.Asteroid,
"comet" => BodyType.Comet,
_ => BodyType.Unknown
};
}

private static TectonicActivity GetTectonicsType(string tectonics)
{
return tectonics switch
{
"earth-like" => TectonicActivity.EarthLike,
"dead" => TectonicActivity.Dead,
"minor" => TectonicActivity.Minor,
"major" => TectonicActivity.Major,
"na" => TectonicActivity.NA,
_ => TectonicActivity.Unknown
};
}
}
5 changes: 1 addition & 4 deletions Pulsar4X/GameEngine/GameEngine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@
<NoWarn>0414;0649;0169;8601,8602,8618,8714</NoWarn>
</PropertyGroup>
<ItemGroup>
<None Update="Data\basemod\*.*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Data\testingmod\*.*">
<None Update="Data\*.*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
Expand Down

0 comments on commit a1314b9

Please sign in to comment.