Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ if(ECOSIM_BUILD_APP)
src/scene/Scene.cpp
src/scene/Scene_Menu.cpp
src/scene/Scene_Simulation.cpp
src/simulation/SimulationClock.cpp
src/simulation/SimulationWorld.cpp
)

Expand Down Expand Up @@ -96,6 +97,8 @@ if(BUILD_TESTING)
add_executable(
EcoSimEngineFoundationTests
tests/foundation_tests.cpp
src/simulation/SimulationClock.cpp
src/simulation/SimulationWorld.cpp
)
target_compile_features(EcoSimEngineFoundationTests PRIVATE cxx_std_20)
target_include_directories(EcoSimEngineFoundationTests PRIVATE include)
Expand Down
6 changes: 6 additions & 0 deletions include/EcoSimEngine/scene/Scene_Simulation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,21 @@

#include "EcoSimEngine/component/Components.hpp"
#include "EcoSimEngine/scene/Scene.hpp"
#include "EcoSimEngine/simulation/SimulationClock.hpp"
#include "EcoSimEngine/simulation/SimulationWorld.hpp"

class Scene_Simulation : public Scene {
private:
std::string m_simKey;

SimulationWorld m_world;
SimulationClock m_simulationClock;

const std::string m_defaultSimulationPath{ "resources/defaults/default_simulation.json" };

std::unordered_map<std::string, sf::Color> m_speciesColors;

// measure real time elapsed between frames for simulation update
sf::Clock m_clock;

protected:
Expand Down
43 changes: 43 additions & 0 deletions include/EcoSimEngine/simulation/SimulationClock.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#pragma once

#include <cstdint>

class SimulationClock
{
static constexpr double EPSILON = 1e-12;

double m_fixedStep{1.0 / 60.0};
double m_accumulator{0.0};
double m_speed{1.0};

std::uint64_t m_tick{0};
std::uint64_t m_maxTicks{100000};

public:
SimulationClock() = default;

SimulationClock(
double fixedStep,
std::uint64_t maxTicks);

void configure(
double fixedStep,
std::uint64_t maxTicks);

void reset() noexcept;

void addElapsed(double realDeltaSeconds);

[[nodiscard]] bool canStep() const noexcept;
void consumeStep();

void setSpeed(double speed);

[[nodiscard]] double fixedStep() const noexcept;
[[nodiscard]] double speed() const noexcept;

[[nodiscard]] std::uint64_t tick() const noexcept;
[[nodiscard]] std::uint64_t maxTicks() const noexcept;

[[nodiscard]] bool finished() const noexcept;
};
15 changes: 12 additions & 3 deletions include/EcoSimEngine/simulation/SimulationWorld.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#pragma once

#include <cstdint>
#include <random>

#include "EcoSimEngine/component/ComponentManager.hpp"
#include "EcoSimEngine/ecs/EntityManager.hpp"
#include "EcoSimEngine/system/SystemManager.hpp"
Expand All @@ -11,12 +14,18 @@ class SimulationWorld
ComponentManager m_componentManager;
EntityManager m_entityManager;

std::mt19937 m_rng{std::mt19937::default_seed};

public:
SimulationWorld();

void update(float dt);

EntityManager& entityManager() noexcept;
ComponentManager& componentManager() noexcept;
SystemManager& systemManager() noexcept;
void reseed(std::uint32_t seed);

[[nodiscard]] std::mt19937& rng() noexcept;

[[nodiscard]] EntityManager& entityManager() noexcept;
[[nodiscard]] ComponentManager& componentManager() noexcept;
[[nodiscard]] SystemManager& systemManager() noexcept;
};
42 changes: 26 additions & 16 deletions include/EcoSimEngine/system/AISystem.hpp
Original file line number Diff line number Diff line change
@@ -1,35 +1,45 @@
#pragma once

#include "EcoSimEngine/system/System.hpp"
#include <random>

#include "EcoSimEngine/ecs/Entity.hpp"
#include "EcoSimEngine/ecs/EntityManager.hpp"
#include "EcoSimEngine/math/Vec2.hpp"
#include "EcoSimEngine/utils/Utils.hpp"

#include "EcoSimEngine/system/System.hpp"
#include "EcoSimEngine/utils/Random.hpp"

class AISystem : public System {
class AISystem : public System
{
public:
void update(EntityManager& em, ComponentManager& cm, float dt)
void update(
EntityManager &em,
ComponentManager &cm,
float dt,
std::mt19937 &rng)
{
for (EntityId id : mEntities)
{
auto e = em.getEntityById(id);
if (!e || !e->isActive()) continue;
if (!cm.has<CBehavior>(id) || !cm.has<CTransform>(id)) continue;

auto& behavior = cm.get<CBehavior>(id);
auto& t = cm.get<CTransform>(id);
if (!e || !e->isActive())
continue;
if (!cm.has<CBehavior>(id) || !cm.has<CTransform>(id))
continue;

auto &behavior = cm.get<CBehavior>(id);
auto &transform = cm.get<CTransform>(id);

behavior.stateTimer -= dt;
if (behavior.stateTimer <= 0.0f) {
behavior.stateTimer = randomFloat(1.0f, 5.0f);
if (behavior.stateTimer <= 0.0f)
{
behavior.stateTimer = randomFloat(rng, 1.0f, 5.0f);
behavior.current = BehaviorState::Wander;
}

if (behavior.current == BehaviorState::Wander && t.velocity.length() < 0.1f) {
Vec2f dir = randomUnitVector();
t.velocity = dir * behavior.movementSpeed;
if (behavior.current == BehaviorState::Wander && transform.velocity.length() < 0.1f)
{
const Vec2f direction = randomUnitVector(rng);
transform.velocity = direction * behavior.movementSpeed;
}
}
}
};
};
26 changes: 26 additions & 0 deletions include/EcoSimEngine/utils/ColorUtils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include <cmath>

#include <SFML/Graphics/Color.hpp>

inline sf::Color hslToRgb(float h, float s, float l) {
float c = (1.0f - std::fabs(2.0f * l - 1.0f)) * s;
float x = c * (1.0f - std::fabs(std::fmod(h / 60.0f, 2.0f) - 1.0f));
const float m = l - c / 2.0f;
float r = 0.0f, g = 0.0f, b = 0.0f;

if (h < 60.0f) { r = c; g = x; }
else if (h < 120.0f) { r = x; g = c; }
else if (h < 180.0f) { g = c; b = x; }
else if (h < 240.0f) { g = x; b = c; }
else if (h < 300.0f) { r = x; b = c; }
else { r = c; b = x; }

return sf::Color(
static_cast<std::uint8_t>((r + m) * 255.0f),
static_cast<std::uint8_t>((g + m) * 255.0f),
static_cast<std::uint8_t>((b + m) * 255.0f)
);
}

26 changes: 26 additions & 0 deletions include/EcoSimEngine/utils/Random.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include <cmath>
#include <random>

#include "EcoSimEngine/math/Vec2.hpp"

inline float randomFloat(
std::mt19937 &rng,
float min,
float max)
{
std::uniform_real_distribution<float> distribution(min, max);

return distribution(rng);
}

inline Vec2f randomUnitVector(std::mt19937& rng)
{
const float angle = randomFloat(
rng,
0.0f,
2.0f * 3.14159265358979323846f);

return Vec2f{ std::cos(angle), std::sin(angle) };
}
37 changes: 0 additions & 37 deletions include/EcoSimEngine/utils/Utils.hpp

This file was deleted.

5 changes: 3 additions & 2 deletions resources/defaults/default_simulation.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"simulation": {
"seed": 48596,
"initialPopulation": {
"fox": {
"total": 10,
Expand All @@ -17,8 +18,8 @@
"width": 1000,
"height": 1000
},
"timeStep": 1.0,
"timeStep": 0.0166666667,
"maxTicks": 100000
}
}
}
}
Loading