From 9bdc7ffce8a36358178c1c2626b2221624684767 Mon Sep 17 00:00:00 2001 From: Jean Tampon Date: Thu, 3 Mar 2022 17:27:38 +0100 Subject: [PATCH] Fix wall distance --- include/common/number_generator.hpp | 21 +++++++++++++-------- include/editor/editor_scene.hpp | 2 +- include/render/async_va_renderer.hpp | 1 + include/render/world_renderer.hpp | 3 ++- include/simulation/ant/ant.hpp | 14 +++++++------- include/simulation/ant/worker.hpp | 14 +++++++++----- include/simulation/colony/colony.hpp | 4 ++-- include/simulation/config.hpp | 2 +- include/simulation/simulation.hpp | 1 + include/simulation/world/world.hpp | 1 + include/simulation/world/world_grid.hpp | 14 ++++++++------ src/main.cpp | 3 +++ 12 files changed, 49 insertions(+), 31 deletions(-) diff --git a/include/common/number_generator.hpp b/include/common/number_generator.hpp index 4c64e3fe..ffe8e1f5 100644 --- a/include/common/number_generator.hpp +++ b/include/common/number_generator.hpp @@ -58,37 +58,42 @@ template class RNG { private: - static RealNumberGenerator gen; + static RealNumberGenerator* gen; public: + static void initialize() + { + gen = new RealNumberGenerator(); + } + static T get() { - return gen.get(); + return gen->get(); } static float getUnder(T max) { - return gen.getUnder(max); + return gen->getUnder(max); } static uint64_t getUintUnder(uint64_t max) { - return static_cast(gen.getUnder(static_cast(max) + 1.0f)); + return static_cast(gen->getUnder(static_cast(max) + 1.0f)); } static float getRange(T min, T max) { - return gen.getRange(min, max); + return gen->getRange(min, max); } static float getRange(T width) { - return gen.getRange(width); + return gen->getRange(width); } static float getFullRange(T width) { - return gen.getRange(static_cast(2.0f) * width); + return gen->getRange(static_cast(2.0f) * width); } static bool proba(float threshold) @@ -100,7 +105,7 @@ class RNG using RNGf = RNG; template -RealNumberGenerator RNG::gen = RealNumberGenerator(); +RealNumberGenerator* RNG::gen = nullptr; template diff --git a/include/editor/editor_scene.hpp b/include/editor/editor_scene.hpp index 60e6b78d..33cbbbbc 100644 --- a/include/editor/editor_scene.hpp +++ b/include/editor/editor_scene.hpp @@ -118,7 +118,7 @@ struct EditorScene : public GUI::Scene void updateRenderOptions() const { - renderer->simulation.renderer.render_ants = display_controls->draw_ants; + renderer->simulation.renderer.render_ants = display_controls->draw_ants; renderer->simulation.world.renderer.draw_markers = display_controls->draw_markers; renderer->simulation.world.renderer.draw_density = display_controls->draw_density; } diff --git a/include/render/async_va_renderer.hpp b/include/render/async_va_renderer.hpp index f3d219a2..602dd8cd 100644 --- a/include/render/async_va_renderer.hpp +++ b/include/render/async_va_renderer.hpp @@ -13,6 +13,7 @@ struct AsyncRenderer bool run; bool swap_ok; + explicit AsyncRenderer(DoubleObject& target) : vertex_array(target) , run(true) diff --git a/include/render/world_renderer.hpp b/include/render/world_renderer.hpp index eeb48d9d..96e19825 100644 --- a/include/render/world_renderer.hpp +++ b/include/render/world_renderer.hpp @@ -43,7 +43,7 @@ struct WorldRenderer : public AsyncRenderer va[4 * i + 0].position = position; va[4 * i + 1].position = position + sf::Vector2f(cell_size_eps, 0.0f); va[4 * i + 2].position = position + sf::Vector2f(cell_size_eps, cell_size_eps); - va[4 * i + 3].position = position + sf::Vector2f(0.0f, cell_size_eps); + va[4 * i + 3].position = position + sf::Vector2f(0.0f , cell_size_eps); ++i; } } @@ -93,6 +93,7 @@ struct WorldRenderer : public AsyncRenderer va[4 * i + 1].color = color; va[4 * i + 2].color = color; va[4 * i + 3].color = color; + ++i; } } diff --git a/include/simulation/ant/ant.hpp b/include/simulation/ant/ant.hpp index d1ff06cc..9b693986 100644 --- a/include/simulation/ant/ant.hpp +++ b/include/simulation/ant/ant.hpp @@ -13,15 +13,15 @@ struct SamplingResult { - float max_intensity = 0.0f; + float max_intensity = 0.0f; // To objective stuff - sf::Vector2f max_direction; - WorldCell* max_cell = nullptr; - bool found_permanent = false; - bool found_fight = false; + sf::Vector2f max_direction = {0.0f, 0.0f}; + WorldCell* max_cell = nullptr; + bool found_permanent = false; + bool found_fight = false; // Repellent stuff - float max_repellent = 0.0f; - WorldCell* repellent_cell = nullptr; + float max_repellent = 0.0f; + WorldCell* repellent_cell = nullptr; }; diff --git a/include/simulation/ant/worker.hpp b/include/simulation/ant/worker.hpp index 4c70d126..7c27b2a1 100644 --- a/include/simulation/ant/worker.hpp +++ b/include/simulation/ant/worker.hpp @@ -19,8 +19,9 @@ struct WorkerUpdater // Sample the world for (uint32_t i(sample_count); i--;) { // Get random point in range - const float sample_angle = current_angle + RNGf::getRange(sample_angle_range); - const float distance = RNGf::getUnder(ant.marker_detection_max_dist); + const float delta_angle = RNGf::getRange(sample_angle_range); + const float sample_angle = current_angle + delta_angle; + const float distance = RNGf::getUnder(Ant::marker_detection_max_dist); const sf::Vector2f to_marker = { cos(sample_angle), sin(sample_angle) }; auto* cell = world.map.getSafe(ant.position + distance * to_marker); const HitPoint hit_result = world.map.getFirstHit(ant.position, to_marker, distance); @@ -51,7 +52,8 @@ struct WorkerUpdater result.repellent_cell = cell; } // Check for the most intense marker - const float marker_intensity = to(cell->getIntensity(marker_phase, ant.col_id) * std::pow(cell->wall_dist, 2.0)); + const float wall_rep = cell->wall_dist * cell->wall_dist; + const auto marker_intensity = to(cell->getIntensity(marker_phase, ant.col_id)) * wall_rep; if (marker_intensity > result.max_intensity) { result.max_intensity = marker_intensity; result.max_direction = to_marker; @@ -62,7 +64,8 @@ struct WorkerUpdater break; } } - if (result.found_fight) { + + if (result.found_fight) { ant.direction = getAngle(result.max_direction); ant.fight_mode = FightMode::ToFight; return; @@ -80,7 +83,8 @@ struct WorkerUpdater if (result.repellent_cell && ant.phase == Mode::ToHome) { result.repellent_cell->getRepellent(ant.col_id) *= 0.95f; } - // Update direction + + // Update direction if (result.max_intensity) { // Slowly degrade the track to accelerate its dissipation if (RNGf::proba(0.2f) && ant.phase == Mode::ToFood) { diff --git a/include/simulation/colony/colony.hpp b/include/simulation/colony/colony.hpp index 51f2ecca..bed26ca2 100644 --- a/include/simulation/colony/colony.hpp +++ b/include/simulation/colony/colony.hpp @@ -42,7 +42,7 @@ struct Colony { id = colony_id; base.food = 0.0f; - uint32_t ants_count = 2000; + uint32_t ants_count = 1000; for (uint32_t i(ants_count); i--;) { createWorker(); } @@ -117,7 +117,7 @@ struct Colony { // Update stats if (pop_diff_update.updateAutoReset(dt)) { - pop_diff.addValue(to(ants.size())); + pop_diff.addValue(to(ants.size())); } createNewAnts(dt); // Update ants and check if collision with colony diff --git a/include/simulation/config.hpp b/include/simulation/config.hpp index 25095ca5..015f211a 100644 --- a/include/simulation/config.hpp +++ b/include/simulation/config.hpp @@ -66,7 +66,7 @@ struct DefaultConf DefaultConf::USE_FULLSCREEN = std::atoi(line_c); break; case 3: - DefaultConf::GUI_SCALE = std::atof(line_c); + DefaultConf::GUI_SCALE = static_cast(std::atof(line_c)); break; case 4: DefaultConf::ANTS_COUNT = std::atoi(line_c); diff --git a/include/simulation/simulation.hpp b/include/simulation/simulation.hpp index 51125ae9..59c745b9 100644 --- a/include/simulation/simulation.hpp +++ b/include/simulation/simulation.hpp @@ -28,6 +28,7 @@ struct Simulation , renderer() , distance_field_builder(world.map) { + distance_field_builder.requestUpdate(); } void loadMap(const std::string& map_filename) diff --git a/include/simulation/world/world.hpp b/include/simulation/world/world.hpp index 50fa6972..a36cac43 100644 --- a/include/simulation/world/world.hpp +++ b/include/simulation/world/world.hpp @@ -21,6 +21,7 @@ struct World , size(to(width), to(height)) , renderer(map, va_map) { + // Create walls around the map for (int32_t x(0); x < map.width; x++) { for (int32_t y(0); y < map.height; y++) { if (x == 0 || x == map.width - 1 || y == 0 || y == map.height - 1) { diff --git a/include/simulation/world/world_grid.hpp b/include/simulation/world/world_grid.hpp index 301a3a62..f19cc5a0 100644 --- a/include/simulation/world/world_grid.hpp +++ b/include/simulation/world/world_grid.hpp @@ -13,8 +13,8 @@ constexpr float min_intensity = 0.1f; struct AntRef { bool active = false; - uint8_t col_id; - uint16_t ant_id; + uint8_t col_id = 0; + uint16_t ant_id = 0; }; @@ -24,14 +24,14 @@ struct ColonyCell float intensity[3]; // Is the marker permanent ? bool permanent = false; - // Repellent instensity + // Repellent intensity float repellent; // Current ant int16_t current_ant = -1; bool fighting; ColonyCell() - : intensity{ min_intensity, min_intensity, min_intensity } + : intensity{0.0f, 0.0f, 0.0f} , repellent(0.0f) , fighting(false) {} @@ -40,7 +40,7 @@ struct ColonyCell { // Reset current ant current_ant = -1; - fighting = false; + fighting = false; // Update toFood and toHome intensity[0] -= permanent ? 0.0f : dt; intensity[1] -= dt; @@ -130,11 +130,13 @@ struct WorldCell return markers[colony_id].repellent; } + [[nodiscard]] float getIntensity(Mode mode, uint8_t colony_id) const { return std::max(min_intensity, markers[colony_id].intensity[to(mode)]); } + [[nodiscard]] bool isPermanent(uint8_t colony_id) const { return markers[colony_id].permanent; @@ -213,7 +215,7 @@ struct WorldGrid : public Grid void remove(sf::Vector2f pos, Mode type, uint8_t colony_id) { WorldCell& cell = get(pos); - const uint32_t mode_index = to(type); + const auto mode_index = to(type); cell.markers[colony_id].intensity[mode_index] = 0.0f; } diff --git a/src/main.cpp b/src/main.cpp index 40f335b4..7fb31b0a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,6 +15,9 @@ int main() } else { std::cout << "Configuration file couldn't be found." << std::endl; } + + RNGf::initialize(); + sf::ContextSettings settings; settings.antialiasingLevel = 4; int32_t window_style = Conf::USE_FULLSCREEN ? sf::Style::Fullscreen : sf::Style::Default;