Skip to content

Commit

Permalink
Fix wall distance
Browse files Browse the repository at this point in the history
  • Loading branch information
johnBuffer committed Mar 3, 2022
1 parent 39584ac commit 9bdc7ff
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 31 deletions.
21 changes: 13 additions & 8 deletions include/common/number_generator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,37 +58,42 @@ template<typename T>
class RNG
{
private:
static RealNumberGenerator<T> gen;
static RealNumberGenerator<T>* gen;

public:
static void initialize()
{
gen = new RealNumberGenerator<T>();
}

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<uint64_t>(gen.getUnder(static_cast<float>(max) + 1.0f));
return static_cast<uint64_t>(gen->getUnder(static_cast<float>(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<T>(2.0f) * width);
return gen->getRange(static_cast<T>(2.0f) * width);
}

static bool proba(float threshold)
Expand All @@ -100,7 +105,7 @@ class RNG
using RNGf = RNG<float>;

template<typename T>
RealNumberGenerator<T> RNG<T>::gen = RealNumberGenerator<T>();
RealNumberGenerator<T>* RNG<T>::gen = nullptr;


template<typename T>
Expand Down
2 changes: 1 addition & 1 deletion include/editor/editor_scene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions include/render/async_va_renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ struct AsyncRenderer
bool run;
bool swap_ok;

explicit
AsyncRenderer(DoubleObject<sf::VertexArray>& target)
: vertex_array(target)
, run(true)
Expand Down
3 changes: 2 additions & 1 deletion include/render/world_renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
}
Expand Down
14 changes: 7 additions & 7 deletions include/simulation/ant/ant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};


Expand Down
14 changes: 9 additions & 5 deletions include/simulation/ant/worker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -51,7 +52,8 @@ struct WorkerUpdater
result.repellent_cell = cell;
}
// Check for the most intense marker
const float marker_intensity = to<float>(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<float>(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;
Expand All @@ -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;
Expand All @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions include/simulation/colony/colony.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -117,7 +117,7 @@ struct Colony
{
// Update stats
if (pop_diff_update.updateAutoReset(dt)) {
pop_diff.addValue(to<float>(ants.size()));
pop_diff.addValue(to<int64_t>(ants.size()));
}
createNewAnts(dt);
// Update ants and check if collision with colony
Expand Down
2 changes: 1 addition & 1 deletion include/simulation/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ struct DefaultConf
DefaultConf<T>::USE_FULLSCREEN = std::atoi(line_c);
break;
case 3:
DefaultConf<T>::GUI_SCALE = std::atof(line_c);
DefaultConf<T>::GUI_SCALE = static_cast<float>(std::atof(line_c));
break;
case 4:
DefaultConf<T>::ANTS_COUNT = std::atoi(line_c);
Expand Down
1 change: 1 addition & 0 deletions include/simulation/simulation.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ struct Simulation
, renderer()
, distance_field_builder(world.map)
{
distance_field_builder.requestUpdate();
}

void loadMap(const std::string& map_filename)
Expand Down
1 change: 1 addition & 0 deletions include/simulation/world/world.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ struct World
, size(to<float>(width), to<float>(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) {
Expand Down
14 changes: 8 additions & 6 deletions include/simulation/world/world_grid.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};


Expand All @@ -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)
{}
Expand All @@ -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;
Expand Down Expand Up @@ -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<uint32_t>(mode)]);
}

[[nodiscard]]
bool isPermanent(uint8_t colony_id) const
{
return markers[colony_id].permanent;
Expand Down Expand Up @@ -213,7 +215,7 @@ struct WorldGrid : public Grid<WorldCell>
void remove(sf::Vector2f pos, Mode type, uint8_t colony_id)
{
WorldCell& cell = get(pos);
const uint32_t mode_index = to<uint32_t>(type);
const auto mode_index = to<uint32_t>(type);
cell.markers[colony_id].intensity[mode_index] = 0.0f;
}

Expand Down
3 changes: 3 additions & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 9bdc7ff

Please sign in to comment.