From fed5b48afe837bbe20476e4beb96b5712cba60fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anselme=20FRAN=C3=87OIS?= Date: Sat, 28 May 2016 13:30:06 +0200 Subject: [PATCH] fixed freeze in simple behavior --- behaviors/simple.cpp | 7 +++++++ src/simulation.cpp | 5 +---- src/team.cpp | 10 ++++++++-- src/team.h | 2 ++ 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/behaviors/simple.cpp b/behaviors/simple.cpp index d34d8a1..0f09075 100644 --- a/behaviors/simple.cpp +++ b/behaviors/simple.cpp @@ -90,10 +90,17 @@ extern "C" void think(Action *action, char *memory, const Info *info) } } + int blockedCount = 0; action->type = Action::MOVE; do{ action->dir = Dir((data->last_dir + rand()%3)%4); type = info->getNear(action->dir); + ++blockedCount; + if(blockedCount > 3) + { + action->type = Action::WAIT; + break; + } }while(!PixelProperty::isWalkable(type)); data->last_dir = action->dir; diff --git a/src/simulation.cpp b/src/simulation.cpp index ac948ca..b28d1b0 100644 --- a/src/simulation.cpp +++ b/src/simulation.cpp @@ -37,17 +37,14 @@ void Simulation::update() if(t.updateSpawn()) { Coord spawnPos = p_map->team(i) + Coord(Dir(rand()%4)); - std::cout << p_map->getPixel(spawnPos).type << std::endl; if(PixelProperty::isWalkable(p_map->getPixel(spawnPos).type)) { + t.popDude(); Dude *dude = new Dude(spawnPos,p_map,i); (*p_map)[spawnPos].data.dudePtr = dude; p_map->updatePixel(spawnPos); m_dudes.push_back(dude); } - else - std::cout << "Unpopable" << std::endl; - // TODO check other case or delay the spawning } } } diff --git a/src/team.cpp b/src/team.cpp index 995ee4b..0302f48 100644 --- a/src/team.cpp +++ b/src/team.cpp @@ -6,6 +6,7 @@ Team::Team(const glm::vec3 &spawnColor, const glm::vec3 &dudeColor, BehaviorFunc m_dudeColor(dudeColor), m_spawnCooldown(1), m_foodQuantity(NB_STARTING_FOOD), + m_dudesReady(0), m_behavior(_behavior) { } @@ -19,10 +20,15 @@ bool Team::updateSpawn() { m_spawnCooldown = SPAWN_COOLDOWN; --m_foodQuantity; - return true; + ++m_dudesReady; } } - return false; + return m_dudesReady > 0; +} + +void Team::popDude() +{ + --m_dudesReady; } void Team::destroySpawn(){ diff --git a/src/team.h b/src/team.h index d6f3ffb..0b94f01 100644 --- a/src/team.h +++ b/src/team.h @@ -19,10 +19,12 @@ class Team glm::vec3 m_dudeColor; int m_spawnCooldown; int m_foodQuantity; + int m_dudesReady; BehaviorFunction m_behavior; public: Team(const glm::vec3 &spawnColor, const glm::vec3 &dudeColor, BehaviorFunction _behavior); bool updateSpawn(); + void popDude(); void addFood() {m_foodQuantity++; } void destroySpawn(); Action* update(Dude*);