summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2023-01-05 19:35:33 +0100
committerAki <please@ignore.pl>2023-01-05 19:35:33 +0100
commit99cfb30eeaafac7b11ea8562202e1fece5d3c363 (patch)
treeb900bf4d8726268dc4fa52182399c3d79b391eff
parent728f73f97657cab299da26793741e405f554fab6 (diff)
downloadkurator-99cfb30eeaafac7b11ea8562202e1fece5d3c363.zip
kurator-99cfb30eeaafac7b11ea8562202e1fece5d3c363.tar.gz
kurator-99cfb30eeaafac7b11ea8562202e1fece5d3c363.tar.bz2
Naively implemented JSON universe Repository
-rw-r--r--sim/src/BaseBattle.cpp2
-rw-r--r--universe/resources/universe/ship_types.json17
-rw-r--r--universe/resources/universe/turret_types.json29
-rw-r--r--universe/src/JsonRepository.cpp80
-rw-r--r--universe/src/JsonRepository.h6
5 files changed, 127 insertions, 7 deletions
diff --git a/sim/src/BaseBattle.cpp b/sim/src/BaseBattle.cpp
index 17df990..224c16c 100644
--- a/sim/src/BaseBattle.cpp
+++ b/sim/src/BaseBattle.cpp
@@ -29,7 +29,7 @@ BaseBattle::BaseBattle(const campaign::Scenario& scenario) :
_registry {},
spawner {scenario.last_team(), scenario.radius, 0.1}
{
- const auto repo = universe::load_sample();
+ const auto repo = universe::load_json("resources/universe");
Builder build {_registry, spawner};
for (const auto& ship : scenario.ships) {
const auto entity = build(repo->ship_type(ship.type), ship.team);
diff --git a/universe/resources/universe/ship_types.json b/universe/resources/universe/ship_types.json
new file mode 100644
index 0000000..d5bc1e8
--- /dev/null
+++ b/universe/resources/universe/ship_types.json
@@ -0,0 +1,17 @@
+[
+ {
+ "name": "Anvil",
+ "base_health_points": 600.0,
+ "max_speed": 218.0
+ },
+ {
+ "name": "Eclipse",
+ "base_health_points": 600.0,
+ "max_speed": 263.0
+ },
+ {
+ "name": "Warbringer",
+ "base_health_points": 600.0,
+ "max_speed": 336.0
+ }
+]
diff --git a/universe/resources/universe/turret_types.json b/universe/resources/universe/turret_types.json
new file mode 100644
index 0000000..5b862b1
--- /dev/null
+++ b/universe/resources/universe/turret_types.json
@@ -0,0 +1,29 @@
+[
+ {
+ "name": "ChargeLaser",
+ "rounds": 1,
+ "base_damage": 85.0,
+ "rate_of_fire": 4.5,
+ "reload": 0.0,
+ "optimal_range": 7000.0,
+ "falloff_modifier": 0.05
+ },
+ {
+ "name": "BurstLaser",
+ "rounds": 3,
+ "base_damage": 21.0,
+ "rate_of_fire": 0.25,
+ "reload": 2.75,
+ "optimal_range": 3500.0,
+ "falloff_modifier": 0.05
+ },
+ {
+ "name": "GaussCannon",
+ "rounds": 2,
+ "base_damage": 55.0,
+ "rate_of_fire": 0.0,
+ "reload": 5.5,
+ "optimal_range": 12000.0,
+ "falloff_modifier": 0.2
+ }
+]
diff --git a/universe/src/JsonRepository.cpp b/universe/src/JsonRepository.cpp
index ddbbfcf..a4abfe5 100644
--- a/universe/src/JsonRepository.cpp
+++ b/universe/src/JsonRepository.cpp
@@ -1,47 +1,117 @@
#include "JsonRepository.h"
+#include <fstream>
#include <functional>
+#include <map>
+#include <stdexcept>
#include <string>
+#include <nlohmann/json.hpp>
+
#include <kurator/universe/NotFound.h>
#include <kurator/universe/ShipType.h>
#include <kurator/universe/TurretType.h>
+using json = nlohmann::json;
+
+
namespace kurator
{
namespace universe
{
-JsonRepository::JsonRepository(const char*)
+void
+from_json(const json& item, ShipType& ship)
{
+ item.at("name").get_to(ship.name);
+ item.at("base_health_points").get_to(ship.base_health_points);
+ item.at("max_speed").get_to(ship.max_speed);
+}
+
+
+void
+from_json(const json& item, TurretType& turret)
+{
+ item.at("name").get_to(turret.name);
+ if (item.contains("base_damage"))
+ item.at("base_damage").get_to(turret.base_damage);
+ if (item.contains("rounds"))
+ item.at("rounds").get_to(turret.rounds);
+ if (item.contains("rate_of_fire"))
+ item.at("rate_of_fire").get_to(turret.rate_of_fire);
+ if (item.contains("reload"))
+ item.at("reload").get_to(turret.reload);
+ if (item.contains("optimal_range"))
+ item.at("optimal_range").get_to(turret.optimal_range);
+ if (item.contains("falloff_modifier"))
+ item.at("falloff_modifier").get_to(turret.falloff_modifier);
+ if (item.contains("falloff_intensity"))
+ item.at("falloff_intensity").get_to(turret.falloff_intensity);
+}
+
+
+template<typename Type>
+std::map<std::string, Type>
+parse(const std::string& path)
+try {
+ std::fstream file{path, file.binary | file.in}; // std::filesystem?
+ if (!file.is_open())
+ throw "could not load universe part"; // FIXME: proper errors and handling above
+ std::map<std::string, Type> map;
+ const auto root = json::parse(file);
+ for (const auto& item : root) {
+ auto def = item.get<Type>();
+ map[def.name] = def;
+ }
+ return map;
+}
+catch (const json::parse_error&) {
+ throw;
+}
+
+
+JsonRepository::JsonRepository(const std::string& path)
+{
+ ships = parse<ShipType>(path + "/ship_types.json");
+ turrets = parse<TurretType>(path + "/turret_types.json");
}
ShipType
JsonRepository::ship_type(const std::string& id) const
-{
+try {
+ return ships.at(id);
+}
+catch (const std::out_of_range&) {
throw NotFound(id);
}
TurretType
JsonRepository::turret_type(const std::string& id) const
-{
+try {
+ return turrets.at(id);
+}
+catch (const std::out_of_range&) {
throw NotFound(id);
}
void
-JsonRepository::for_ship_types(std::function<void(const ShipType&)>) const
+JsonRepository::for_ship_types(std::function<void(const ShipType&)> func) const
{
+ for (const auto& [_, type] : ships)
+ func(type);
}
void
-JsonRepository::for_turret_types(std::function<void(const TurretType&)>) const
+JsonRepository::for_turret_types(std::function<void(const TurretType&)> func) const
{
+ for (const auto& [_, type] : turrets)
+ func(type);
}
diff --git a/universe/src/JsonRepository.h b/universe/src/JsonRepository.h
index dc6e7f1..ea6bf46 100644
--- a/universe/src/JsonRepository.h
+++ b/universe/src/JsonRepository.h
@@ -1,6 +1,7 @@
#pragma once
#include <functional>
+#include <map>
#include <string>
#include <kurator/universe/Repository.h>
@@ -17,11 +18,14 @@ namespace universe
class JsonRepository : public Repository
{
public:
- explicit JsonRepository(const char* path);
+ explicit JsonRepository(const std::string& path);
ShipType ship_type(const std::string& id) const override;
TurretType turret_type(const std::string& id) const override;
void for_ship_types(std::function<void(const ShipType&)> func) const;
void for_turret_types(std::function<void(const TurretType&)> func) const;
+private:
+ std::map<std::string, ShipType> ships;
+ std::map<std::string, TurretType> turrets;
};