started to convert handleAction
This commit is contained in:
parent
84a9aad3e0
commit
c07bec7796
@ -2,9 +2,10 @@
|
|||||||
#include "map.h"
|
#include "map.h"
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
|
||||||
Dude::Dude(const Coord &_pos, Map &map) :
|
Dude::Dude(const Coord &_pos, Map &_map, int &_team) :
|
||||||
m_pos(_pos),
|
m_pos(_pos),
|
||||||
m_map(map),
|
m_map(_map),
|
||||||
|
m_team(_team),
|
||||||
m_dead(false),
|
m_dead(false),
|
||||||
m_success(true),
|
m_success(true),
|
||||||
m_inventory(-1)
|
m_inventory(-1)
|
||||||
|
@ -8,6 +8,7 @@ class Dude : public Info
|
|||||||
private:
|
private:
|
||||||
Coord m_pos;
|
Coord m_pos;
|
||||||
Map &m_map;
|
Map &m_map;
|
||||||
|
int m_team;
|
||||||
bool m_dead;
|
bool m_dead;
|
||||||
bool m_success;
|
bool m_success;
|
||||||
int m_inventory;
|
int m_inventory;
|
||||||
@ -15,8 +16,9 @@ private:
|
|||||||
Com m_comData;
|
Com m_comData;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Dude(const Coord &_pos, Map &map);
|
Dude(const Coord &_pos, Map &map, int &team);
|
||||||
char* getMemory();
|
char* getMemory();
|
||||||
|
int getTeam(){ return m_team; }
|
||||||
void setSuccess(bool success) { m_success = success; }
|
void setSuccess(bool success) { m_success = success; }
|
||||||
void setInventory(int item) { m_inventory = item; }
|
void setInventory(int item) { m_inventory = item; }
|
||||||
void trespass() { m_dead = true; }
|
void trespass() { m_dead = true; }
|
||||||
|
@ -1,9 +1,11 @@
|
|||||||
#include "simulation.h"
|
#include "simulation.h"
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include "map.h"
|
#include "map.h"
|
||||||
|
#include "dude.h"
|
||||||
|
|
||||||
Simulation::Simulation(Map *_map, std::vector<BehaviorFunction> &_behaviors):
|
Simulation::Simulation(Map &_map, std::vector<BehaviorFunction> &_behaviors):
|
||||||
p_map(_map)
|
m_map(_map)
|
||||||
{
|
{
|
||||||
int i=0;
|
int i=0;
|
||||||
for(BehaviorFunction &behavior : _behaviors){
|
for(BehaviorFunction &behavior : _behaviors){
|
||||||
@ -17,14 +19,200 @@ Simulation::Simulation(Map *_map, std::vector<BehaviorFunction> &_behaviors):
|
|||||||
|
|
||||||
void Simulation::update()
|
void Simulation::update()
|
||||||
{
|
{
|
||||||
|
std::random_shuffle(m_dudes.begin(),m_dudes.end());
|
||||||
|
for (auto dude : m_dudes){
|
||||||
|
auto action = m_teams[dude->getTeam()].update(*dude); //get action for this dude from behavior function in team
|
||||||
|
handleAction(action,dude);
|
||||||
|
// TODO perform action (cf old_code/main.c -> void handleAction(t_dude* dude) )
|
||||||
|
}
|
||||||
|
|
||||||
|
// for each team, spawn dude if condition met
|
||||||
for(int i=0; i<m_teams.size(); ++i){
|
for(int i=0; i<m_teams.size(); ++i){
|
||||||
Team &t = m_teams[i];
|
Team &t = m_teams[i];
|
||||||
if (t.updateSpawn())
|
if (t.updateSpawn())
|
||||||
{
|
{
|
||||||
Dir randDir = Dir(rand()%4);
|
Dir randDir = Dir(rand()%4);
|
||||||
Coord spawnPos = p_map->team(i) + Coord(randDir);
|
Coord spawnPos = m_map.team(i) + Coord(randDir);
|
||||||
t.spawn(spawnPos, *p_map);
|
m_dudes.push_back(new Dude(spawnPos,m_map,i));
|
||||||
}
|
}
|
||||||
t.updateDudes();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Simulation::handleAction(Action *action, Dude *dude){
|
||||||
|
Coord targetPos(dude->getPos() + Coord(action->dir));
|
||||||
|
Pixel target = m_map[targetPos];
|
||||||
|
Dude* targetDude;
|
||||||
|
dude->setSuccess(false);
|
||||||
|
switch(action->type){
|
||||||
|
case Action::MOVE:
|
||||||
|
if (target.type != Pixel::WALL && target.type != Pixel::ROCK
|
||||||
|
&& target.type != Pixel::BEDROCK && target.type != Pixel::IRON_ORE
|
||||||
|
&& target.type != Pixel::TREE && target.type != Pixel::LIBRARY)
|
||||||
|
NULL; // TODO: move action
|
||||||
|
break;
|
||||||
|
case Action::ATTACK:
|
||||||
|
if (target.type == Pixel::DUDE
|
||||||
|
|| target.type == Pixel::SPAWN
|
||||||
|
|| target.type == Pixel::WALL
|
||||||
|
|| target.type == Pixel::LIBRARY
|
||||||
|
|| target.type == Pixel::ROAD)
|
||||||
|
{
|
||||||
|
dude->setSuccess(true);
|
||||||
|
if (target.type == Pixel::DUDE)
|
||||||
|
NULL; // TODO: add fight between dude and targetDude
|
||||||
|
else{
|
||||||
|
if(target.type == Pixel::SPAWN)
|
||||||
|
m_teams[target.data.nbRes].destroySpawn();
|
||||||
|
m_map[targetPos].type = Pixel::GRASS;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case Action::PICK:
|
||||||
|
if(target.type >= Pixel::FOOD && target.type <= Pixel::SWORD && dude->getInventory() == -1){
|
||||||
|
dude->setInventory(target.type);
|
||||||
|
target.data.nbRes--;
|
||||||
|
if (target.data.nbRes < 1){
|
||||||
|
m_map[targetPos].type = Pixel::GRASS;
|
||||||
|
// TODO: change color of target
|
||||||
|
}
|
||||||
|
dude->setSuccess(true);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case Action::PUT:
|
||||||
|
if(dude->getInventory() != -1 && (target.type == Pixel::GRASS || target.type == Pixel::MARK || target.type == dude->getInventory())){
|
||||||
|
if(target.type == Pixel::GRASS || target.type == Pixel::MARK){
|
||||||
|
m_map[targetPos].type = dude->getInventory();
|
||||||
|
target.data.nbRes = 1;
|
||||||
|
}else
|
||||||
|
target.data.nbRes++;
|
||||||
|
dude->setInventory(-1);
|
||||||
|
// TODO: change pixel to new type
|
||||||
|
}else if(target.type == Pixel::SPAWN && dude->getInventory() == Pixel::FOOD){
|
||||||
|
dude->setInventory(-1);
|
||||||
|
m_teams[target.data.nbRes].addFood();
|
||||||
|
}//else{
|
||||||
|
// printf("put failed : trying to put %d in %d\n", dude->inventory, target.type);
|
||||||
|
// dude->success = 0;
|
||||||
|
//}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
case WORK :
|
||||||
|
dude->success = 1;
|
||||||
|
switch(target.type){
|
||||||
|
case ROCK :
|
||||||
|
map[target_pos.x][target_pos.y].type = STONE;
|
||||||
|
nb_res = malloc(sizeof(int));
|
||||||
|
*nb_res = 1;
|
||||||
|
map[target_pos.x][target_pos.y].data = nb_res;
|
||||||
|
break;
|
||||||
|
case BERRIES :
|
||||||
|
map[target_pos.x][target_pos.y].type = FOOD;
|
||||||
|
nb_res = malloc(sizeof(int));
|
||||||
|
*nb_res = 1;
|
||||||
|
map[target_pos.x][target_pos.y].data = nb_res;
|
||||||
|
break;
|
||||||
|
case TREE :
|
||||||
|
map[target_pos.x][target_pos.y].type = WOOD;
|
||||||
|
nb_res = malloc(sizeof(int));
|
||||||
|
*nb_res = 1;
|
||||||
|
map[target_pos.x][target_pos.y].data = nb_res;
|
||||||
|
break;
|
||||||
|
case IRON_ORE :
|
||||||
|
map[target_pos.x][target_pos.y].type = IRON;
|
||||||
|
nb_res = malloc(sizeof(int));
|
||||||
|
*nb_res = 1;
|
||||||
|
map[target_pos.x][target_pos.y].data = nb_res;
|
||||||
|
break;
|
||||||
|
case GRASS :
|
||||||
|
map[target_pos.x][target_pos.y].type = MARK;
|
||||||
|
map[target_pos.x][target_pos.y].data = NULL;
|
||||||
|
break;
|
||||||
|
case MARK :
|
||||||
|
map[target_pos.x][target_pos.y].type = GRASS;
|
||||||
|
map[target_pos.x][target_pos.y].data = NULL;
|
||||||
|
break;
|
||||||
|
case WOOD :
|
||||||
|
nb_res = target.data;
|
||||||
|
switch(*nb_res){
|
||||||
|
case 1 :
|
||||||
|
free(target.data);
|
||||||
|
map[target_pos.x][target_pos.y].type = WALL;
|
||||||
|
map[target_pos.x][target_pos.y].data = NULL;
|
||||||
|
break;
|
||||||
|
case 2 :
|
||||||
|
free(target.data);
|
||||||
|
map[target_pos.x][target_pos.y].type = LIBRARY;
|
||||||
|
map[target_pos.x][target_pos.y].data = malloc(128);
|
||||||
|
memset(map[target_pos.x][target_pos.y].data, 0, 128);
|
||||||
|
default :
|
||||||
|
dude->success = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case STONE :
|
||||||
|
nb_res = target.data;
|
||||||
|
if(*nb_res != 1)
|
||||||
|
dude->success = 0;
|
||||||
|
else{
|
||||||
|
free(target.data);
|
||||||
|
map[target_pos.x][target_pos.y].type = ROAD;
|
||||||
|
map[target_pos.x][target_pos.y].data = NULL;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case IRON :
|
||||||
|
nb_res = target.data;
|
||||||
|
if(*nb_res != 1)
|
||||||
|
dude->success = 0;
|
||||||
|
else
|
||||||
|
map[target_pos.x][target_pos.y].type = SWORD;
|
||||||
|
break;
|
||||||
|
default :
|
||||||
|
dude->success = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if(dude->success)
|
||||||
|
putpixel(img, target_pos.x, target_pos.y, getColor(map[target_pos.x][target_pos.y]));
|
||||||
|
break;
|
||||||
|
case WAIT :
|
||||||
|
dude->success = 1;
|
||||||
|
break;
|
||||||
|
case COMMUNICATE :
|
||||||
|
switch(target.type){
|
||||||
|
// TODO : conflicts are not handled in a fair way
|
||||||
|
case DUDE :
|
||||||
|
printf("message to dude\n");
|
||||||
|
action.com_data.flag = (action.dir+2)%4;
|
||||||
|
target_dude = target.data;
|
||||||
|
if(target_dude->com_data == NULL){
|
||||||
|
target_dude->com_data = malloc(sizeof(t_com));
|
||||||
|
*(target_dude->com_data) = action.com_data;
|
||||||
|
dude->success = 1;
|
||||||
|
}else
|
||||||
|
dude->success = 0;
|
||||||
|
break;
|
||||||
|
case LIBRARY :
|
||||||
|
if(action.com_data.flag & READ){
|
||||||
|
if(dude->com_data == NULL){
|
||||||
|
action.com_data.flag = action.dir;
|
||||||
|
dude->com_data = malloc(sizeof(t_com));
|
||||||
|
*(dude->com_data) = action.com_data;
|
||||||
|
memcpy(dude->com_data + sizeof(int), target.data + (action.com_data.flag | 3), 32);
|
||||||
|
}else{
|
||||||
|
dude->success = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
memcpy(target.data + action.com_data.flag, action.com_data.data, 32);
|
||||||
|
}
|
||||||
|
dude->success = 1;
|
||||||
|
break;
|
||||||
|
default :
|
||||||
|
dude->success = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}*/
|
||||||
|
@ -12,15 +12,16 @@ class Map;
|
|||||||
class Simulation
|
class Simulation
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
Map *p_map;
|
Map &m_map;
|
||||||
|
std::vector<Dude*> m_dudes;
|
||||||
std::vector<Team> m_teams;
|
std::vector<Team> m_teams;
|
||||||
public:
|
public:
|
||||||
Simulation(Map *_map, std::vector<BehaviorFunction> &_behaviors);
|
Simulation(Map &_map, std::vector<BehaviorFunction> &_behaviors);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief update runs one step of simulation
|
* @brief update runs one step of simulation
|
||||||
*/
|
*/
|
||||||
void update();
|
void update();
|
||||||
|
void handleAction(Action*, Dude* dude);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // SIMULATION_H
|
#endif // SIMULATION_H
|
||||||
|
18
src/team.cpp
18
src/team.cpp
@ -23,17 +23,15 @@ bool Team::updateSpawn()
|
|||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
void Team::destroySpawn(){
|
||||||
void Team::spawn(Coord &d, Map &map)
|
m_spawnCooldown = 0;
|
||||||
{
|
m_foodQuantity = 0;
|
||||||
m_dudes.push_back(Dude(d, map));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Team::updateDudes()
|
|
||||||
|
Action* Team::update(Dude dude)
|
||||||
{
|
{
|
||||||
for (Dude &dude : m_dudes){
|
Action* action;
|
||||||
Action* action;
|
m_behavior(action, dude.getMemory(), &dude);
|
||||||
m_behavior(action, dude.getMemory(), &dude);
|
return action;
|
||||||
// TODO perform action (cf old_code/main.c -> void handleAction(t_dude* dude) )
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -18,13 +18,13 @@ class Team
|
|||||||
glm::vec3 m_dudeColor;
|
glm::vec3 m_dudeColor;
|
||||||
int m_spawnCooldown;
|
int m_spawnCooldown;
|
||||||
int m_foodQuantity;
|
int m_foodQuantity;
|
||||||
std::vector<Dude> m_dudes;
|
|
||||||
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 spawn(Coord &d, Map &map);
|
void addFood() {m_foodQuantity++; }
|
||||||
void updateDudes();
|
void destroySpawn();
|
||||||
|
Action* update(Dude);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // TEAM_H
|
#endif // TEAM_H
|
||||||
|
Loading…
x
Reference in New Issue
Block a user