-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add factory to load system bodies from JSON
Migrate Mars, Luna and Halley's comet to the new JSON loader
- Loading branch information
1 parent
51d6eac
commit a1314b9
Showing
6 changed files
with
282 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
187 changes: 187 additions & 0 deletions
187
Pulsar4X/GameEngine/Engine/Factories/SystemBodyFromJsonFactory.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters