Skip to content

Commit

Permalink
Most of the default start can be done from json now
Browse files Browse the repository at this point in the history
  • Loading branch information
behindcurtain3 committed Apr 1, 2024
1 parent c7dd9aa commit 4ac5f92
Show file tree
Hide file tree
Showing 46 changed files with 257 additions and 7 deletions.
8 changes: 8 additions & 0 deletions Pulsar4X/GameEngine/Data/basemod/defaultStart.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"systems": [
"sol/"
],
"factions": [
"defaultStart/uef.json"
]
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"id": "default-design-cargo-hold",
"name": "General Cargo Hold",
"templateId": "general-cargo-hold",
"attributes": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"id": "default-design-factory",
"name": "Factory",
"templateId": "factory"
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"id": "default-design-logistics-office",
"name": "Logistics Office",
"templateId": "logistics-office"
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"id": "default-design-passive-sensor",
"name": "Passive Scanner",
"templateId": "passive-sensor",
"attributes": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"id": "default-design-refinery",
"name": "Refinery",
"templateId": "refinery"
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"id": "default-design-shipyard",
"name": "Ship Yard",
"templateId": "shipyard"
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"id": "default-design-university",
"name": "University",
"templateId": "university"
}
41 changes: 41 additions & 0 deletions Pulsar4X/GameEngine/Data/basemod/defaultStart/uef.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"name": "UEF",
"componentDesigns": [
"componentDesigns/"
],
"ordnanceDesigns": [
"ordnanceDesigns/"
],
"shipDesigns": [
"shipDesigns/cargoCourier.json",
"shipDesigns/cargoship.json",
"shipDesigns/gunship.json",
"shipDesigns/obennDropship.json",
"shipDesigns/starship.json"
],
"species": [
"species-humans.json"
],
"colonies": [
{
"systemId": "sol",
"location": "Earth",
"species": {
"name": "Human",
"population": 8.2E9
},
"installations": [
{ "id": "default-design-passive-sensor", "amount": 1 },
{ "id": "default-design-mine", "amount": 1 },
{ "id": "default-design-cargo-hold", "amount": 1 },
{ "id": "default-design-fuel-tank-1000", "amount": 1 },
{ "id": "default-design-refinery", "amount": 1 },
{ "id": "default-design-factory", "amount": 1 },
{ "id": "default-design-shipyard", "amount": 1 },
{ "id": "default-design-university", "amount": 1 },
{ "id": "default-design-logistics-office", "amount": 1 },
{ "id": "default-design-ordnance-rack-2.5t", "amount": 1 }
]
}
]
}
1 change: 1 addition & 0 deletions Pulsar4X/GameEngine/Data/basemod/sol/systemInfo.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"id": "sol",
"name": "Sol",
"stars": [
"sol.json"
Expand Down
54 changes: 54 additions & 0 deletions Pulsar4X/GameEngine/Engine/Factories/DefaultStartFactory.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Newtonsoft.Json.Linq;
using Pulsar4X.Blueprints;
using Pulsar4X.Components;
using Pulsar4X.Datablobs;
Expand Down Expand Up @@ -43,6 +45,58 @@ public static class DefaultStartFactory
private static ComponentDesign _geoSurveyor;
private static ComponentDesign _jpSurveyor;

public static (Entity?, string) LoadFromJson(Game game, string filePath)
{
ComponentDesigner.StartResearched = true;

string fileContents = File.ReadAllText(filePath);
var rootDirectory = (string?)Path.GetDirectoryName(filePath) ?? "Data/basemod/";
var rootJson = JObject.Parse(fileContents);

StarSystemFactory starSystemFactory = new StarSystemFactory(game);
StarSystem? startingSystem = null;
Entity? playerFaction = null;

var systemsToLoad = (JArray?)rootJson["systems"];
foreach(var systemToLoad in systemsToLoad)
{
var system = starSystemFactory.LoadSystemFromJson(game, Path.Combine(rootDirectory, systemToLoad.ToString()));

// TODO: allow the json to set this
if(startingSystem == null)
startingSystem = system;
}

var factionsToLoad = (JArray?)rootJson["factions"];
foreach(var factionToLoad in factionsToLoad)
{
var faction = FactionFactory.LoadFromJson(game, Path.Combine(rootDirectory, factionToLoad.ToString()));

faction.FactionOwnerID = faction.Id;
faction.GetDataBlob<FactionInfoDB>().KnownSystems.Add(startingSystem.Guid);

// TODO: allow the json to set this
if(playerFaction == null)
playerFaction = faction;
}

var pow = startingSystem.GetAllEntitiesWithDataBlob<EnergyGenAbilityDB>();
foreach (var entityItem in pow)
{
game.ProcessorManager.GetInstanceProcessor(nameof(EnergyGenProcessor)).ProcessEntity(entityItem, game.TimePulse.GameGlobalDateTime);

}

var entitiesWithSensors = startingSystem.GetAllEntitiesWithDataBlob<SensorAbilityDB>();
foreach (var entityItem in entitiesWithSensors)
{
game.ProcessorManager.GetInstanceProcessor(nameof(SensorScan)).ProcessEntity(entityItem, game.TimePulse.GameGlobalDateTime);
}

ComponentDesigner.StartResearched = false;
return (playerFaction, startingSystem.Guid);
}

public static Entity DefaultHumans(Game game, string name)
{

Expand Down
139 changes: 139 additions & 0 deletions Pulsar4X/GameEngine/Engine/Factories/FactionFactory.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json.Linq;
using Pulsar4X.Datablobs;
using Pulsar4X.Engine.Auth;
using Pulsar4X.Engine.Factories;
using Pulsar4X.Events;
using Pulsar4X.Extensions;
using Pulsar4X.Interfaces;

namespace Pulsar4X.Engine
Expand Down Expand Up @@ -39,6 +44,140 @@ public static class FactionFactory
*
*/

public static Entity LoadFromJson(Game game, string filePath)
{
string fileContents = File.ReadAllText(filePath);
var rootDirectory = (string?)Path.GetDirectoryName(filePath) ?? "Data/basemod/";
var rootJson = JObject.Parse(fileContents);

var name = rootJson["name"].ToString();
var faction = CreateFaction(game, name);
var factionInfoDB = faction.GetDataBlob<FactionInfoDB>();
var factionDataStore = factionInfoDB.Data;

var componentDesignsToLoad = (JArray?)rootJson["componentDesigns"];
foreach(var componentDesignToLoad in componentDesignsToLoad)
{
string path = componentDesignToLoad.ToString();
string fullPath = Path.Combine(rootDirectory, path);

if(Directory.Exists(fullPath))
{
var files = Directory.GetFiles(fullPath, "*.json", SearchOption.AllDirectories);
foreach(var file in files)
{
ComponentDesignFromJson.Create(faction, factionDataStore, file);
}
}
else
{
ComponentDesignFromJson.Create(faction, factionDataStore, fullPath);
}
}

var ordnanceDesignsToLoad = (JArray?)rootJson["ordnanceDesigns"];
foreach(var ordnanceDesignToLoad in ordnanceDesignsToLoad)
{
string path = ordnanceDesignToLoad.ToString();
string fullPath = Path.Combine(rootDirectory, path);

if(Directory.Exists(fullPath))
{
var files = Directory.GetFiles(fullPath, "*.json", SearchOption.AllDirectories);
foreach(var file in files)
{
OrdnanceDesignFromJson.Create(faction, file);
}
}
else
{
OrdnanceDesignFromJson.Create(faction, fullPath);
}
}

var shipDesignsToLoad = (JArray?)rootJson["shipDesigns"];
foreach(var shipDesignToLoad in shipDesignsToLoad)
{
string path = shipDesignToLoad.ToString();
string fullPath = Path.Combine(rootDirectory, path);

if(Directory.Exists(fullPath))
{
var files = Directory.GetFiles(fullPath, "*.json", SearchOption.AllDirectories);
foreach(var file in files)
{
ShipDesignFromJson.Create(faction, factionDataStore, file);
}
}
else
{
ShipDesignFromJson.Create(faction, factionDataStore, fullPath);
}
}

var speciesToLoad = (JArray?)rootJson["species"];
foreach(var toLoad in speciesToLoad)
{
string path = toLoad.ToString();
string fullPath = Path.Combine(rootDirectory, path);

if(Directory.Exists(fullPath))
{
var files = Directory.GetFiles(fullPath, "*.json", SearchOption.AllDirectories);
foreach(var file in files)
{
SpeciesFactory.CreateFromJson(faction, game.GlobalManager, file);
}
}
else
{
SpeciesFactory.CreateFromJson(faction, game.GlobalManager, fullPath);
}
}

var coloniesToLoad = (JArray?)rootJson["colonies"];
foreach(var colonyToLoad in coloniesToLoad)
{
var systemId = colonyToLoad["systemId"].ToString();

var system = game.Systems.Find(s => s.Guid.Equals(systemId));
if(system == null) throw new NullReferenceException("invalid systemId in json");
var location = NameLookup.GetFirstEntityWithName(system, colonyToLoad["location"].ToString());

// Mark the colony location as geo surveyed
if(location.TryGetDatablob<GeoSurveyableDB>(out var geoSurveyableDB))
{
geoSurveyableDB.GeoSurveyStatus[faction.Id] = 0;
}

var speciesName = colonyToLoad["species"]["name"].ToString();
var species = faction.GetDataBlob<FactionInfoDB>().Species.Find(s => s.GetOwnersName().Equals(speciesName));
if(species == null) throw new NullReferenceException("invalid species name in json");
var population = (long?)colonyToLoad["species"]["population"] ?? 0;

var colony = ColonyFactory.CreateColony(faction, species, location, population);

var installationsToAdd = (JArray?)colonyToLoad["installations"];
if(installationsToAdd != null)
{
foreach(var install in installationsToAdd)
{
var installId = install["id"].ToString();
var amount = (int?)install["amount"] ?? 1;

colony.AddComponent(
factionInfoDB.InternalComponentDesigns[installId],
amount
);
}
}

ReCalcProcessor.ReCalcAbilities(colony);
}

return faction;
}


public static Entity CreateFaction(Game game, string factionName)
{
Expand Down
3 changes: 2 additions & 1 deletion Pulsar4X/GameEngine/Engine/Factories/StarSystemFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,11 +310,12 @@ public StarSystem LoadSystemFromJson(Game game, string folder)
{
string fileContents = File.ReadAllText(Path.Combine(folder, "systemInfo.json"));
var rootJson = JObject.Parse(fileContents);
var id = rootJson["id"].ToString();
var systemName = rootJson["name"].ToString();
var rngSeed = (int?)rootJson["seed"] ?? -1;

StarSystem system = new StarSystem();
system.Initialize(game, systemName, rngSeed);
system.Initialize(game, systemName, rngSeed, id);

var stars = (JArray?)rootJson["stars"];
Entity? rootStar = null;
Expand Down
3 changes: 1 addition & 2 deletions Pulsar4X/Pulsar4X.Client/EmpireManagement/EconomicsWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ internal static EconomicsWindow GetInstance()
{
thisitem = new EconomicsWindow()
{
// FIXME: this will crash if there are no colonies
SelectedEntity = _uiState.StarSystemStates.First().Value.EntityStatesColonies.First().Value
SelectedEntity = null
};
}
thisitem = (EconomicsWindow)_uiState.LoadedWindows[typeof(EconomicsWindow)];
Expand Down
8 changes: 4 additions & 4 deletions Pulsar4X/Pulsar4X.Client/GameManagment/NewGameOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,19 @@ void CreateNewGame()
var factionName = ImGuiSDL2CSHelper.StringFromBytes(_factionInputBuffer);
var factionPasswd = ImGuiSDL2CSHelper.StringFromBytes(_passInputBuffer);

var newGameFaction = Pulsar4X.Engine.DefaultStartFactory.DefaultHumans(game, factionName);
var (newGameFaction, systemId) = Pulsar4X.Engine.DefaultStartFactory.LoadFromJson(game, "Data/basemod/defaultStart.json");

if(newGameFaction == null) return;

//TODO: Tidyup: new Game(gameSettings) doesn't currently create a default faction as per the settings.
//this should probilby be fixed, either we create it there or we... dont.
//_uiState.Game = new ECSLib.Game(gameSettings);
_uiState.Game = game;



// var factionEntity = DefaultStartFactory.DefaultHumans(game, factionName);
// AuthProcessor.StorePasswordAsHash(StaticRefLib.Game, factionEntity, factionPasswd);
_uiState.SetFaction(newGameFaction, true);
_uiState.SetActiveSystem(newGameFaction.GetDataBlob<FactionInfoDB>().KnownSystems[0]);
_uiState.SetActiveSystem(systemId);

DebugWindow.GetInstance().SetGameEvents();
IsActive = false;
Expand Down

0 comments on commit 4ac5f92

Please sign in to comment.