Skip to content

Commit

Permalink
Make wolf random journeys more distant
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrek-szczygiel committed Jun 7, 2021
1 parent c1ae662 commit cc537a0
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 10 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.11)
project(sim VERSION "1.1.2")
project(sim VERSION "1.1.3")

configure_file("src/version.h.in" "version.h" @ONLY)
include_directories(${PROJECT_BINARY_DIR})
Expand Down
18 changes: 11 additions & 7 deletions src/simulation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,9 @@ AgentGenes Simulation::mutate_genes(AgentGenes mom, AgentGenes dad) {
std::clamp(new_sensor_range, 1, m_config.genes_max_sensor_range)};
}

Vec2 Simulation::random_position_around(Vec2 pos, int distance) {
Vec2 min = {std::max(0, pos.x - distance), std::max(0, pos.y - distance)};
Vec2 max = {std::min(m_size.x - 1, pos.x + distance), std::min(m_size.y - 1, pos.y + distance)};
Vec2 Simulation::random_position_around(Vec2 pos, int dist) {
Vec2 min = {std::max(0, pos.x - dist), std::max(0, pos.y - dist)};
Vec2 max = {std::min(m_size.x - 1, pos.x + dist), std::min(m_size.y - 1, pos.y + dist)};
return {random(min.x, max.x), random(min.y, max.y)};
}

Expand Down Expand Up @@ -114,9 +114,13 @@ bool Simulation::move_agent_if_empty(Agent* agent, Vec2 pos) {
return false;
}

void Simulation::move_agent_random(Agent* agent) {
void Simulation::move_agent_random(Agent* agent, int dist) {
if (agent->random_direction == Vec2{0, 0}) {
agent->random_direction = random_position_around(agent->pos, agent->genes.sensor_range * 2);
if (dist == -1) {
agent->random_direction = random_position();
} else {
agent->random_direction = random_position_around(agent->pos, dist);
}
}

auto path = m_pathfinder.get_next_step(agent->pos, agent->random_direction, m_grid);
Expand Down Expand Up @@ -215,7 +219,7 @@ void Simulation::update_chicken(Agent* chicken) {
}
}

move_agent_random(chicken);
move_agent_random(chicken, chicken->genes.sensor_range);
}

void Simulation::update_wolf(Agent* wolf) {
Expand Down Expand Up @@ -254,7 +258,7 @@ void Simulation::update_wolf(Agent* wolf) {
}
}

move_agent_random(wolf);
move_agent_random(wolf, -1);
}

Path Simulation::get_path_to_nearest(Agent* from, AgentType to, bool fed) {
Expand Down
4 changes: 2 additions & 2 deletions src/simulation.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,14 +87,14 @@ class Simulation {

int random(int min, int max) { return std::uniform_int_distribution(min, max)(m_mt19937); }
Vec2 random_position() { return {random(0, m_size.x - 1), random(0, m_size.y - 1)}; }
Vec2 random_position_around(Vec2 pos, int distance);
Vec2 random_position_around(Vec2 pos, int dist);
AgentGenes mutate_genes(AgentGenes mom, AgentGenes dad);

void add_agent(AgentType type, AgentGenes genes, Vec2 pos);
void move_agent(Agent* agent, Vec2 pos);
bool move_agent_if_empty(Agent* agent, Vec2 pos);
void move_agent_around(Agent* agent, Vec2 pos);
void move_agent_random(Agent* agent);
void move_agent_random(Agent* agent, int dist);

inline bool empty(Vec2 pos) const { return !out_of_map(pos) && (!m_grid.at(pos) || m_grid.at(pos)->is_dead()); }

Expand Down

0 comments on commit cc537a0

Please sign in to comment.