fixed freeze in simple behavior

This commit is contained in:
Anselme FRANÇOIS 2016-05-28 13:30:06 +02:00
parent ff64c7b780
commit fed5b48afe
4 changed files with 18 additions and 6 deletions

View File

@ -90,10 +90,17 @@ extern "C" void think(Action *action, char *memory, const Info *info)
} }
} }
int blockedCount = 0;
action->type = Action::MOVE; action->type = Action::MOVE;
do{ do{
action->dir = Dir((data->last_dir + rand()%3)%4); action->dir = Dir((data->last_dir + rand()%3)%4);
type = info->getNear(action->dir); type = info->getNear(action->dir);
++blockedCount;
if(blockedCount > 3)
{
action->type = Action::WAIT;
break;
}
}while(!PixelProperty::isWalkable(type)); }while(!PixelProperty::isWalkable(type));
data->last_dir = action->dir; data->last_dir = action->dir;

View File

@ -37,17 +37,14 @@ void Simulation::update()
if(t.updateSpawn()) if(t.updateSpawn())
{ {
Coord spawnPos = p_map->team(i) + Coord(Dir(rand()%4)); 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)) if(PixelProperty::isWalkable(p_map->getPixel(spawnPos).type))
{ {
t.popDude();
Dude *dude = new Dude(spawnPos,p_map,i); Dude *dude = new Dude(spawnPos,p_map,i);
(*p_map)[spawnPos].data.dudePtr = dude; (*p_map)[spawnPos].data.dudePtr = dude;
p_map->updatePixel(spawnPos); p_map->updatePixel(spawnPos);
m_dudes.push_back(dude); m_dudes.push_back(dude);
} }
else
std::cout << "Unpopable" << std::endl;
// TODO check other case or delay the spawning
} }
} }
} }

View File

@ -6,6 +6,7 @@ Team::Team(const glm::vec3 &spawnColor, const glm::vec3 &dudeColor, BehaviorFunc
m_dudeColor(dudeColor), m_dudeColor(dudeColor),
m_spawnCooldown(1), m_spawnCooldown(1),
m_foodQuantity(NB_STARTING_FOOD), m_foodQuantity(NB_STARTING_FOOD),
m_dudesReady(0),
m_behavior(_behavior) m_behavior(_behavior)
{ {
} }
@ -19,10 +20,15 @@ bool Team::updateSpawn()
{ {
m_spawnCooldown = SPAWN_COOLDOWN; m_spawnCooldown = SPAWN_COOLDOWN;
--m_foodQuantity; --m_foodQuantity;
return true; ++m_dudesReady;
} }
} }
return false; return m_dudesReady > 0;
}
void Team::popDude()
{
--m_dudesReady;
} }
void Team::destroySpawn(){ void Team::destroySpawn(){

View File

@ -19,10 +19,12 @@ class Team
glm::vec3 m_dudeColor; glm::vec3 m_dudeColor;
int m_spawnCooldown; int m_spawnCooldown;
int m_foodQuantity; int m_foodQuantity;
int m_dudesReady;
BehaviorFunction m_behavior; BehaviorFunction m_behavior;
public: public:
Team(const glm::vec3 &spawnColor, const glm::vec3 &dudeColor, BehaviorFunction _behavior); Team(const glm::vec3 &spawnColor, const glm::vec3 &dudeColor, BehaviorFunction _behavior);
bool updateSpawn(); bool updateSpawn();
void popDude();
void addFood() {m_foodQuantity++; } void addFood() {m_foodQuantity++; }
void destroySpawn(); void destroySpawn();
Action* update(Dude*); Action* update(Dude*);