From 8ad11975221b1448865400cab14a1560b71c6ade Mon Sep 17 00:00:00 2001 From: Aki Date: Sat, 7 May 2022 23:51:13 +0200 Subject: Added support for sample dump loading --- CMakeLists.txt | 8 ++++- DumpSource.cpp | 89 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ DumpSource.h | 16 ++++++++++ ExampleSource.cpp | 10 +++++-- ExampleSource.h | 4 +-- Grid.h | 15 ++++++++++ Killmail.h | 2 ++ LongVector3.h | 9 ++++++ Source.h | 4 +-- View.cpp | 11 +++++-- View.h | 2 ++ main.cpp | 4 +-- 12 files changed, 162 insertions(+), 12 deletions(-) create mode 100644 DumpSource.cpp create mode 100644 DumpSource.h create mode 100644 Grid.h create mode 100644 LongVector3.h 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 +#include +#include + +#include +#include + +#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& 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(), + info["victim"]["position"]["y"].get(), + info["victim"]["position"]["z"].get(), + }; + 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((km.original.x - average.x) * SCALE), + static_cast((km.original.y - average.y) * SCALE), + static_cast((km.original.z - average.z) * SCALE), + }; + } + grid.origin = average; + } +} + + +std::vector +DumpSource::grids() const +{ + return m_grids; +} + + +Grid& +find_grid_for(std::vector& 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 + +#include "Grid.h" +#include "Source.h" + + +class DumpSource : public Source +{ +public: + explicit DumpSource(const char* filename); + std::vector grids() const override; +private: + std::vector 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 +#include "Grid.h" #include "Killmail.h" @@ -20,8 +21,8 @@ ExampleSource::ExampleSource() : } -std::vector -ExampleSource::all() const +std::vector +ExampleSource::grids() const { std::vector 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 -#include "Killmail.h" +#include "Grid.h" #include "Source.h" @@ -10,7 +10,7 @@ class ExampleSource : public Source { public: ExampleSource(); - std::vector all() const override; + std::vector 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 + +#include + +#include "Killmail.h" +#include "LongVector3.h" + + +struct Grid +{ + LongVector3 origin; + std::vector killmails; +}; diff --git a/Killmail.h b/Killmail.h index 64cf972..cc0f06b 100644 --- a/Killmail.h +++ b/Killmail.h @@ -3,6 +3,7 @@ #include #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 -#include "Killmail.h" +#include "Grid.h" struct Source { virtual ~Source() = default; - virtual std::vector all() const = 0; // maybe an immutable view instead of a copy? + virtual std::vector 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) : 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) : 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 m_source; std::vector 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 -#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()); + View view(std::make_unique("sample.json")); while (!WindowShouldClose()) { view.update(GetFrameTime()); -- cgit v1.1