summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2022-05-07 23:51:13 +0200
committerAki <please@ignore.pl>2022-05-07 23:51:13 +0200
commit8ad11975221b1448865400cab14a1560b71c6ade (patch)
treec2f54c25097b6534dd5ec386d37d6f3f0222ec92
parent313b7a531106c623a28883db515f245a74a5fafb (diff)
downloadderelict-8ad11975221b1448865400cab14a1560b71c6ade.zip
derelict-8ad11975221b1448865400cab14a1560b71c6ade.tar.gz
derelict-8ad11975221b1448865400cab14a1560b71c6ade.tar.bz2
Added support for sample dump loading
-rw-r--r--CMakeLists.txt8
-rw-r--r--DumpSource.cpp89
-rw-r--r--DumpSource.h16
-rw-r--r--ExampleSource.cpp10
-rw-r--r--ExampleSource.h4
-rw-r--r--Grid.h15
-rw-r--r--Killmail.h2
-rw-r--r--LongVector3.h9
-rw-r--r--Source.h4
-rw-r--r--View.cpp11
-rw-r--r--View.h2
-rw-r--r--main.cpp4
12 files changed, 162 insertions, 12 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index b93c6f8..14a7b02 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -4,10 +4,16 @@ set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_EXTENSIONS No)
find_package(raylib 3 REQUIRED)
+find_package(nlohmann_json 3.2 REQUIRED)
add_executable(
${PROJECT_NAME}
+ DumpSource.cpp
ExampleSource.cpp
main.cpp
View.cpp
)
-target_link_libraries(${PROJECT_NAME} raylib)
+target_link_libraries(
+ ${PROJECT_NAME}
+ raylib
+ nlohmann_json::nlohmann_json
+)
diff --git a/DumpSource.cpp b/DumpSource.cpp
new file mode 100644
index 0000000..9c913d0
--- /dev/null
+++ b/DumpSource.cpp
@@ -0,0 +1,89 @@
+#include "DumpSource.h"
+
+#include <cmath>
+#include <vector>
+#include <utility>
+
+#include <nlohmann/json.hpp>
+#include <raylib.h>
+
+#include "Grid.h"
+#include "Killmail.h"
+#include "LongVector3.h"
+
+
+using json = nlohmann::json;
+
+
+static constexpr long double SCALE {.0001};
+static constexpr long double EXTENT {1000 * 50000};
+
+
+static Grid& find_grid_for(std::vector<Grid>& grids, const LongVector3& original);
+
+
+DumpSource::DumpSource(const char* filename) :
+ m_grids {}
+{
+ if (!FileExists(filename))
+ throw "File does not exist";
+ char* text = LoadFileText(filename);
+ auto dump = json::parse(text);
+ for (const auto& info : dump["killmails"]) {
+ Killmail km;
+ km.original = {
+ info["victim"]["position"]["x"].get<long double>(),
+ info["victim"]["position"]["y"].get<long double>(),
+ info["victim"]["position"]["z"].get<long double>(),
+ };
+ auto& grid = find_grid_for(m_grids, km.original);
+ grid.killmails.push_back(std::move(km));
+ }
+ UnloadFileText(text);
+ for (auto& grid : m_grids) {
+ LongVector3 average {0, 0, 0};
+ for (const auto& km : grid.killmails) {
+ average.x += km.original.x;
+ average.y += km.original.y;
+ average.z += km.original.z;
+ }
+ const auto killmails = grid.killmails.size();
+ average.x /= killmails;
+ average.y /= killmails;
+ average.z /= killmails;
+ for (auto& km : grid.killmails) {
+ km.position = {
+ static_cast<float>((km.original.x - average.x) * SCALE),
+ static_cast<float>((km.original.y - average.y) * SCALE),
+ static_cast<float>((km.original.z - average.z) * SCALE),
+ };
+ }
+ grid.origin = average;
+ }
+}
+
+
+std::vector<Grid>
+DumpSource::grids() const
+{
+ return m_grids;
+}
+
+
+Grid&
+find_grid_for(std::vector<Grid>& grids, const LongVector3& original)
+{
+ for (auto& grid : grids) {
+ for (auto& km : grid.killmails) {
+ const long double dist =
+ std::sqrt(
+ std::pow(original.x - km.original.x, 2) +
+ std::pow(original.y - km.original.y, 2) +
+ std::pow(original.z - km.original.z, 2));
+ if (dist < EXTENT)
+ return grid;
+ }
+ }
+ grids.push_back(Grid{});
+ return grids.back();
+}
diff --git a/DumpSource.h b/DumpSource.h
new file mode 100644
index 0000000..e90c8af
--- /dev/null
+++ b/DumpSource.h
@@ -0,0 +1,16 @@
+#pragma once
+
+#include <vector>
+
+#include "Grid.h"
+#include "Source.h"
+
+
+class DumpSource : public Source
+{
+public:
+ explicit DumpSource(const char* filename);
+ std::vector<Grid> grids() const override;
+private:
+ std::vector<Grid> m_grids;
+};
diff --git a/ExampleSource.cpp b/ExampleSource.cpp
index fa022e9..6083055 100644
--- a/ExampleSource.cpp
+++ b/ExampleSource.cpp
@@ -6,6 +6,7 @@
#include <raylib.h>
+#include "Grid.h"
#include "Killmail.h"
@@ -20,8 +21,8 @@ ExampleSource::ExampleSource() :
}
-std::vector<Killmail>
-ExampleSource::all() const
+std::vector<Grid>
+ExampleSource::grids() const
{
std::vector<Killmail> killmails;
killmails.reserve(std::pow((MAX - MIN) / STEP, 2));
@@ -39,5 +40,8 @@ ExampleSource::all() const
killmails.push_back(std::move(km));
}
}
- return killmails;
+ Grid grid;
+ grid.origin = {0.0, 0.0, 0.0};
+ grid.killmails = killmails;
+ return {grid};
}
diff --git a/ExampleSource.h b/ExampleSource.h
index 2086ca5..cce3d3f 100644
--- a/ExampleSource.h
+++ b/ExampleSource.h
@@ -2,7 +2,7 @@
#include <vector>
-#include "Killmail.h"
+#include "Grid.h"
#include "Source.h"
@@ -10,7 +10,7 @@ class ExampleSource : public Source
{
public:
ExampleSource();
- std::vector<Killmail> all() const override;
+ std::vector<Grid> grids() const override;
private:
mutable float m_time;
};
diff --git a/Grid.h b/Grid.h
new file mode 100644
index 0000000..8a6c3e7
--- /dev/null
+++ b/Grid.h
@@ -0,0 +1,15 @@
+#pragma once
+
+#include <vector>
+
+#include <raylib.h>
+
+#include "Killmail.h"
+#include "LongVector3.h"
+
+
+struct Grid
+{
+ LongVector3 origin;
+ std::vector<Killmail> killmails;
+};
diff --git a/Killmail.h b/Killmail.h
index 64cf972..cc0f06b 100644
--- a/Killmail.h
+++ b/Killmail.h
@@ -3,6 +3,7 @@
#include <raylib.h>
#include "Location.h"
+#include "LongVector3.h"
#include "Owner.h"
#include "Ship.h"
@@ -13,4 +14,5 @@ struct Killmail
Owner owner;
Ship ship;
Vector3 position;
+ LongVector3 original;
};
diff --git a/LongVector3.h b/LongVector3.h
new file mode 100644
index 0000000..c4faa10
--- /dev/null
+++ b/LongVector3.h
@@ -0,0 +1,9 @@
+#pragma once
+
+
+struct LongVector3
+{
+ long double x;
+ long double y;
+ long double z;
+};
diff --git a/Source.h b/Source.h
index 547fd0e..c1d0876 100644
--- a/Source.h
+++ b/Source.h
@@ -2,11 +2,11 @@
#include <vector>
-#include "Killmail.h"
+#include "Grid.h"
struct Source
{
virtual ~Source() = default;
- virtual std::vector<Killmail> all() const = 0; // maybe an immutable view instead of a copy?
+ virtual std::vector<Grid> grids() const = 0;
};
diff --git a/View.cpp b/View.cpp
index 90bc542..3aba502 100644
--- a/View.cpp
+++ b/View.cpp
@@ -13,7 +13,8 @@
View::View(std::unique_ptr<Source> source) :
m_camera {},
m_source {std::move(source)},
- m_projected {}
+ m_projected {},
+ m_grid {0}
{
m_camera.position = Vector3{10.0f, 10.0f, 10.0f};
m_camera.target = Vector3{0.0f, 0.0f, 0.0f};
@@ -27,10 +28,16 @@ View::View(std::unique_ptr<Source> source) :
void
View::update(const float dt)
{
+ const auto grids = m_source->grids();
+ if (IsKeyPressed(KEY_SPACE)) {
+ m_grid++;
+ if (m_grid >= grids.size())
+ m_grid = 0;
+ }
UpdateCamera(&m_camera);
const int height = GetScreenHeight();
const int width = GetScreenWidth();
- const auto killmails = m_source->all();
+ const auto killmails = grids.at(m_grid).killmails;
m_projected.clear();
m_projected.reserve(killmails.size());
for (const auto& km : killmails) {
diff --git a/View.h b/View.h
index 13eb881..56a50ca 100644
--- a/View.h
+++ b/View.h
@@ -24,4 +24,6 @@ private:
Camera m_camera;
std::unique_ptr<Source> m_source;
std::vector<Projected> m_projected;
+ int m_grid;
+ float m_timer;
};
diff --git a/main.cpp b/main.cpp
index f3c16f6..d1c9b86 100644
--- a/main.cpp
+++ b/main.cpp
@@ -2,7 +2,7 @@
#include <raylib.h>
-#include "ExampleSource.h"
+#include "DumpSource.h"
#include "View.h"
@@ -12,7 +12,7 @@ main(int argc, char* argv[])
InitWindow(800, 600, "Derelict");
SetWindowState(FLAG_WINDOW_RESIZABLE);
SetTargetFPS(60);
- View view(std::make_unique<ExampleSource>());
+ View view(std::make_unique<DumpSource>("sample.json"));
while (!WindowShouldClose())
{
view.update(GetFrameTime());