Skip to content

Commit

Permalink
Fix many bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
johnBuffer committed Apr 6, 2021
1 parent ce9bf4c commit 40d3d97
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 48 deletions.
65 changes: 40 additions & 25 deletions include/ant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ struct Ant
, reserve(max_reserve)
, id(id_)
, liberty_coef(RNGf::getRange(0.0001f, 0.001f))
, hits(0)
{
}

Expand All @@ -36,7 +37,7 @@ struct Ant
last_direction_update += dt;
if (last_direction_update > direction_update_period) {
findMarker(world);
direction += getRandRange(direction_noise_range);
direction += RNGf::getFullRange(direction_noise_range);
last_direction_update = 0.0f;
}

Expand All @@ -48,24 +49,33 @@ struct Ant
direction.update(dt);
}

void updatePosition(World& world, const float dt)
void updatePosition(World& world, float dt)
{
sf::Vector2f v = direction.getVec();
const sf::Vector2f next_position = position + (dt * move_speed) * direction.getVec();
const sf::Vector2f next_position = position + (dt * move_speed) * v;
if (!world.grid_walls.isEmpty(next_position)) {
++hits;
const sf::Vector2f normal = getWallNormal(world, v);
const float dot = normal.x * v.x + normal.y * v.y;
v = v - 2 * dot * normal;
direction.addNow(v, normal.x == -1.0f);
positions.push_back(position);
velocities.push_back(v);
normals.push_back(normal);
v.x *= (normal.x ? -1.0f : 1.0f);
v.y *= (normal.y ? -1.0f : 1.0f);
direction.setDirectionNow(v);
}
else {
hits = 0;
}

if (hits > 1) {
// Bad but nothing better for now
position = Conf::COLONY_POSITION;
}

position += (dt * move_speed) * v;

position.x = position.x < 0.0f ? Conf::WIN_WIDTH : position.x;
position.y = position.y < 0.0f ? Conf::WIN_HEIGHT : position.y;

position.x = position.x > Conf::WIN_WIDTH ? 0.0f : position.x;
position.y = position.y > Conf::WIN_HEIGHT ? 0.0f : position.y;
position.x = (position.x < 0.0f || position.x > Conf::WIN_WIDTH) ? Conf::COLONY_POSITION.x : position.x;
position.y = (position.y < 0.0f || position.y > Conf::WIN_HEIGHT) ? Conf::COLONY_POSITION.y : position.y;
}

void checkFood(World& world)
Expand Down Expand Up @@ -98,7 +108,7 @@ struct Ant
// Init
const sf::Vector2f dir_vec = direction.getVec();
const uint32_t grid_cell_size = world.markers.cell_size;
const float radius = 40.0f;
const float radius = 20.0f;
const int32_t radius_cell = radius / grid_cell_size;
const int32_t cell_x = position.x / grid_cell_size;
const int32_t cell_y = position.y / grid_cell_size;
Expand All @@ -110,7 +120,7 @@ struct Ant
float max_intensity = 0.0f;
sf::Vector2f max_direction;
// Sample the markers
const uint32_t sample_count = 100;
const uint32_t sample_count = 32;
for (uint32_t i(0); i < sample_count; ++i) {
const uint32_t sample_x = RNGf::getRange(min_range_x, max_range_x + 1.0f);
const uint32_t sample_y = RNGf::getRange(min_range_y, max_range_y + 1.0f);
Expand Down Expand Up @@ -199,26 +209,31 @@ struct Ant
va[index + 3].position = position - width * nrm_vec - length * dir_vec;
}

sf::Vector2f position;

Direction direction;

float last_direction_update;
float last_marker;
Marker::Type phase;
float reserve;
const uint32_t id;
float liberty_coef;

// Parameters
const float width = 3.0f;
const float length = 4.7f;
const float move_speed = 50.0f;
const float marker_detection_max_dist = 40.0f;
const float direction_update_period = 0.125f;
const float marker_period = 0.25f;
const float max_reserve = 6000.0f;
const float max_reserve = 200000.0f;
const float direction_noise_range = PI * 0.1f;
const float marker_reserve_consumption = 0.02f;
const float colony_size = 20.0f;

uint32_t hits;
std::vector<sf::Vector2f> normals;
std::vector<sf::Vector2f> positions;
std::vector<sf::Vector2f> velocities;

sf::Vector2f position;

Direction direction;

float last_direction_update;
float last_marker;
Marker::Type phase;
float reserve;
const uint32_t id;
float liberty_coef;
};
9 changes: 8 additions & 1 deletion include/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ struct DefaultConf
const static sf::Color TO_FOOD_COLOR;
const static sf::Color TO_HOME_COLOR;
const static sf::Color COLONY_COLOR;
const static uint32_t MAX_MARKERS_PER_CELL;
static float COLONY_SIZE;
static sf::Vector2f COLONY_POSITION;
static uint32_t WIN_WIDTH;
static uint32_t WIN_HEIGHT;
static uint32_t ANTS_COUNT;
Expand Down Expand Up @@ -44,6 +45,12 @@ const sf::Color Conf::FOOD_COLOR = sf::Color(66, 153, 66);
const sf::Color Conf::TO_FOOD_COLOR = sf::Color(0, 255, 0);
const sf::Color Conf::TO_HOME_COLOR = sf::Color(255, 0, 0);
const sf::Color Conf::COLONY_COLOR = Conf::ANT_COLOR;
uint32_t Conf::WIN_WIDTH = 1920;
uint32_t Conf::WIN_HEIGHT = 1080;
uint32_t Conf::ANTS_COUNT = 1024;
float Conf::COLONY_SIZE = 20.0f;
//sf::Vector2f Conf::COLONY_POSITION = sf::Vector2f(Conf::WIN_WIDTH * 0.5f, Conf::WIN_HEIGHT * 0.5f);
sf::Vector2f Conf::COLONY_POSITION = sf::Vector2f(100.0f, 100.0f);

std::shared_ptr<sf::Texture> Conf::ANT_TEXTURE;
std::shared_ptr<sf::Texture> Conf::MARKER_TEXTURE;
Expand Down
9 changes: 9 additions & 0 deletions include/direction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ struct Direction
updateVec();
}

void setDirectionNow(sf::Vector2f d)
{
m_target_vec = d;
m_vec = d;
m_angle = getAngle(d);
m_target_angle = m_angle;
}


private:
sf::Vector2f m_vec;
sf::Vector2f m_target_vec;
Expand Down
12 changes: 8 additions & 4 deletions include/world.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ struct Grid
return add(getCellCoords(obj.position), obj);
}

sf::Vector2f getCellCenter(sf::Vector2f position)
{
const sf::Vector2i cell_coords = getCellCoords(position);
return float(cell_size) * sf::Vector2f(cell_coords.x + 0.5f, cell_coords.y + 0.5f);
}

void clearAt(sf::Vector2f position)
{
auto cell_coords = getCellCoords(position);
Expand Down Expand Up @@ -77,10 +83,8 @@ struct Grid
{
if (checkCell(cell_coords)) {
std::list<T>& l = cells[getIndexFromCoords(cell_coords)];
if (Conf::MAX_MARKERS_PER_CELL > l.size()) {
l.emplace_back(obj);
return &l.back();
}
l.emplace_back(obj);
return &l.back();
}
return nullptr;
}
Expand Down
14 changes: 0 additions & 14 deletions src/config.cpp

This file was deleted.

2 changes: 1 addition & 1 deletion src/display_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ void DisplayManager::draw()
m_world.renderMarkers(m_target, rs_ground);
}
m_world.renderFood(m_target, rs_ground);
m_world.renderWalls(m_target, rs_ground);
// Render ants
if (render_ants) {
m_colony.render(m_target, rs);
}
m_world.renderWalls(m_target, rs_ground);

const float size = m_colony.size;
sf::CircleShape circle(size);
Expand Down
6 changes: 3 additions & 3 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ int main()
Conf::loadTextures();

World world(Conf::WIN_WIDTH, Conf::WIN_HEIGHT);
Colony colony(Conf::WIN_WIDTH / 2, Conf::WIN_HEIGHT / 2, 1024);
Colony colony(Conf::COLONY_POSITION.x, Conf::COLONY_POSITION.y, 1024);
//Colony colony(250, 250, 512);
for (uint32_t i(0); i < 64; ++i) {
float angle = float(i) / 64.0f * (2.0f * PI);
Expand All @@ -51,9 +51,9 @@ int main()
for (uint32_t x(0); x < wall_map.getSize().x; ++x) {
for (uint32_t y(0); y < wall_map.getSize().y; ++y) {
const sf::Vector2f position = float(world.grid_walls.cell_size) * sf::Vector2f(x, y);
if (wall_map.getPixel(x, y).r == 255) {
if (wall_map.getPixel(x, y).r > 50) {
world.addWall(position);
} else if (wall_map.getPixel(x, y).g > 0) {
} else if (wall_map.getPixel(x, y).g > 100) {
world.addFoodAt(position.x, position.y, 5.0f);
}
}
Expand Down

0 comments on commit 40d3d97

Please sign in to comment.