Skip to content

Commit 9bdc7ff

Browse files
committed
Fix wall distance
1 parent 39584ac commit 9bdc7ff

File tree

12 files changed

+49
-31
lines changed

12 files changed

+49
-31
lines changed

include/common/number_generator.hpp

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,37 +58,42 @@ template<typename T>
5858
class RNG
5959
{
6060
private:
61-
static RealNumberGenerator<T> gen;
61+
static RealNumberGenerator<T>* gen;
6262

6363
public:
64+
static void initialize()
65+
{
66+
gen = new RealNumberGenerator<T>();
67+
}
68+
6469
static T get()
6570
{
66-
return gen.get();
71+
return gen->get();
6772
}
6873

6974
static float getUnder(T max)
7075
{
71-
return gen.getUnder(max);
76+
return gen->getUnder(max);
7277
}
7378

7479
static uint64_t getUintUnder(uint64_t max)
7580
{
76-
return static_cast<uint64_t>(gen.getUnder(static_cast<float>(max) + 1.0f));
81+
return static_cast<uint64_t>(gen->getUnder(static_cast<float>(max) + 1.0f));
7782
}
7883

7984
static float getRange(T min, T max)
8085
{
81-
return gen.getRange(min, max);
86+
return gen->getRange(min, max);
8287
}
8388

8489
static float getRange(T width)
8590
{
86-
return gen.getRange(width);
91+
return gen->getRange(width);
8792
}
8893

8994
static float getFullRange(T width)
9095
{
91-
return gen.getRange(static_cast<T>(2.0f) * width);
96+
return gen->getRange(static_cast<T>(2.0f) * width);
9297
}
9398

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

102107
template<typename T>
103-
RealNumberGenerator<T> RNG<T>::gen = RealNumberGenerator<T>();
108+
RealNumberGenerator<T>* RNG<T>::gen = nullptr;
104109

105110

106111
template<typename T>

include/editor/editor_scene.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ struct EditorScene : public GUI::Scene
118118

119119
void updateRenderOptions() const
120120
{
121-
renderer->simulation.renderer.render_ants = display_controls->draw_ants;
121+
renderer->simulation.renderer.render_ants = display_controls->draw_ants;
122122
renderer->simulation.world.renderer.draw_markers = display_controls->draw_markers;
123123
renderer->simulation.world.renderer.draw_density = display_controls->draw_density;
124124
}

include/render/async_va_renderer.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ struct AsyncRenderer
1313
bool run;
1414
bool swap_ok;
1515

16+
explicit
1617
AsyncRenderer(DoubleObject<sf::VertexArray>& target)
1718
: vertex_array(target)
1819
, run(true)

include/render/world_renderer.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ struct WorldRenderer : public AsyncRenderer
4343
va[4 * i + 0].position = position;
4444
va[4 * i + 1].position = position + sf::Vector2f(cell_size_eps, 0.0f);
4545
va[4 * i + 2].position = position + sf::Vector2f(cell_size_eps, cell_size_eps);
46-
va[4 * i + 3].position = position + sf::Vector2f(0.0f, cell_size_eps);
46+
va[4 * i + 3].position = position + sf::Vector2f(0.0f , cell_size_eps);
4747
++i;
4848
}
4949
}
@@ -93,6 +93,7 @@ struct WorldRenderer : public AsyncRenderer
9393
va[4 * i + 1].color = color;
9494
va[4 * i + 2].color = color;
9595
va[4 * i + 3].color = color;
96+
9697
++i;
9798
}
9899
}

include/simulation/ant/ant.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@
1313

1414
struct SamplingResult
1515
{
16-
float max_intensity = 0.0f;
16+
float max_intensity = 0.0f;
1717
// To objective stuff
18-
sf::Vector2f max_direction;
19-
WorldCell* max_cell = nullptr;
20-
bool found_permanent = false;
21-
bool found_fight = false;
18+
sf::Vector2f max_direction = {0.0f, 0.0f};
19+
WorldCell* max_cell = nullptr;
20+
bool found_permanent = false;
21+
bool found_fight = false;
2222
// Repellent stuff
23-
float max_repellent = 0.0f;
24-
WorldCell* repellent_cell = nullptr;
23+
float max_repellent = 0.0f;
24+
WorldCell* repellent_cell = nullptr;
2525
};
2626

2727

include/simulation/ant/worker.hpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ struct WorkerUpdater
1919
// Sample the world
2020
for (uint32_t i(sample_count); i--;) {
2121
// Get random point in range
22-
const float sample_angle = current_angle + RNGf::getRange(sample_angle_range);
23-
const float distance = RNGf::getUnder(ant.marker_detection_max_dist);
22+
const float delta_angle = RNGf::getRange(sample_angle_range);
23+
const float sample_angle = current_angle + delta_angle;
24+
const float distance = RNGf::getUnder(Ant::marker_detection_max_dist);
2425
const sf::Vector2f to_marker = { cos(sample_angle), sin(sample_angle) };
2526
auto* cell = world.map.getSafe(ant.position + distance * to_marker);
2627
const HitPoint hit_result = world.map.getFirstHit(ant.position, to_marker, distance);
@@ -51,7 +52,8 @@ struct WorkerUpdater
5152
result.repellent_cell = cell;
5253
}
5354
// Check for the most intense marker
54-
const float marker_intensity = to<float>(cell->getIntensity(marker_phase, ant.col_id) * std::pow(cell->wall_dist, 2.0));
55+
const float wall_rep = cell->wall_dist * cell->wall_dist;
56+
const auto marker_intensity = to<float>(cell->getIntensity(marker_phase, ant.col_id)) * wall_rep;
5557
if (marker_intensity > result.max_intensity) {
5658
result.max_intensity = marker_intensity;
5759
result.max_direction = to_marker;
@@ -62,7 +64,8 @@ struct WorkerUpdater
6264
break;
6365
}
6466
}
65-
if (result.found_fight) {
67+
68+
if (result.found_fight) {
6669
ant.direction = getAngle(result.max_direction);
6770
ant.fight_mode = FightMode::ToFight;
6871
return;
@@ -80,7 +83,8 @@ struct WorkerUpdater
8083
if (result.repellent_cell && ant.phase == Mode::ToHome) {
8184
result.repellent_cell->getRepellent(ant.col_id) *= 0.95f;
8285
}
83-
// Update direction
86+
87+
// Update direction
8488
if (result.max_intensity) {
8589
// Slowly degrade the track to accelerate its dissipation
8690
if (RNGf::proba(0.2f) && ant.phase == Mode::ToFood) {

include/simulation/colony/colony.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ struct Colony
4242
{
4343
id = colony_id;
4444
base.food = 0.0f;
45-
uint32_t ants_count = 2000;
45+
uint32_t ants_count = 1000;
4646
for (uint32_t i(ants_count); i--;) {
4747
createWorker();
4848
}
@@ -117,7 +117,7 @@ struct Colony
117117
{
118118
// Update stats
119119
if (pop_diff_update.updateAutoReset(dt)) {
120-
pop_diff.addValue(to<float>(ants.size()));
120+
pop_diff.addValue(to<int64_t>(ants.size()));
121121
}
122122
createNewAnts(dt);
123123
// Update ants and check if collision with colony

include/simulation/config.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ struct DefaultConf
6666
DefaultConf<T>::USE_FULLSCREEN = std::atoi(line_c);
6767
break;
6868
case 3:
69-
DefaultConf<T>::GUI_SCALE = std::atof(line_c);
69+
DefaultConf<T>::GUI_SCALE = static_cast<float>(std::atof(line_c));
7070
break;
7171
case 4:
7272
DefaultConf<T>::ANTS_COUNT = std::atoi(line_c);

include/simulation/simulation.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ struct Simulation
2828
, renderer()
2929
, distance_field_builder(world.map)
3030
{
31+
distance_field_builder.requestUpdate();
3132
}
3233

3334
void loadMap(const std::string& map_filename)

include/simulation/world/world.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ struct World
2121
, size(to<float>(width), to<float>(height))
2222
, renderer(map, va_map)
2323
{
24+
// Create walls around the map
2425
for (int32_t x(0); x < map.width; x++) {
2526
for (int32_t y(0); y < map.height; y++) {
2627
if (x == 0 || x == map.width - 1 || y == 0 || y == map.height - 1) {

include/simulation/world/world_grid.hpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ constexpr float min_intensity = 0.1f;
1313
struct AntRef
1414
{
1515
bool active = false;
16-
uint8_t col_id;
17-
uint16_t ant_id;
16+
uint8_t col_id = 0;
17+
uint16_t ant_id = 0;
1818
};
1919

2020

@@ -24,14 +24,14 @@ struct ColonyCell
2424
float intensity[3];
2525
// Is the marker permanent ?
2626
bool permanent = false;
27-
// Repellent instensity
27+
// Repellent intensity
2828
float repellent;
2929
// Current ant
3030
int16_t current_ant = -1;
3131
bool fighting;
3232

3333
ColonyCell()
34-
: intensity{ min_intensity, min_intensity, min_intensity }
34+
: intensity{0.0f, 0.0f, 0.0f}
3535
, repellent(0.0f)
3636
, fighting(false)
3737
{}
@@ -40,7 +40,7 @@ struct ColonyCell
4040
{
4141
// Reset current ant
4242
current_ant = -1;
43-
fighting = false;
43+
fighting = false;
4444
// Update toFood and toHome
4545
intensity[0] -= permanent ? 0.0f : dt;
4646
intensity[1] -= dt;
@@ -130,11 +130,13 @@ struct WorldCell
130130
return markers[colony_id].repellent;
131131
}
132132

133+
[[nodiscard]]
133134
float getIntensity(Mode mode, uint8_t colony_id) const
134135
{
135136
return std::max(min_intensity, markers[colony_id].intensity[to<uint32_t>(mode)]);
136137
}
137138

139+
[[nodiscard]]
138140
bool isPermanent(uint8_t colony_id) const
139141
{
140142
return markers[colony_id].permanent;
@@ -213,7 +215,7 @@ struct WorldGrid : public Grid<WorldCell>
213215
void remove(sf::Vector2f pos, Mode type, uint8_t colony_id)
214216
{
215217
WorldCell& cell = get(pos);
216-
const uint32_t mode_index = to<uint32_t>(type);
218+
const auto mode_index = to<uint32_t>(type);
217219
cell.markers[colony_id].intensity[mode_index] = 0.0f;
218220
}
219221

src/main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ int main()
1515
} else {
1616
std::cout << "Configuration file couldn't be found." << std::endl;
1717
}
18+
19+
RNGf::initialize();
20+
1821
sf::ContextSettings settings;
1922
settings.antialiasingLevel = 4;
2023
int32_t window_style = Conf::USE_FULLSCREEN ? sf::Style::Fullscreen : sf::Style::Default;

0 commit comments

Comments
 (0)