From dfb5d471a61cb152d46b5b976b7af9d4eaa875c1 Mon Sep 17 00:00:00 2001 From: Anselme Date: Wed, 18 May 2016 17:31:50 +0200 Subject: [PATCH] Info is now an interface, implemented by Dude --- src/behavior.cpp | 21 --------------------- src/behavior.h | 20 ++++++-------------- src/dude.cpp | 38 ++++++++++++++++++++++++++++---------- src/dude.h | 24 +++++++++++++++++++----- src/simulation.cpp | 17 +++++++++++------ src/simulation.h | 4 ++-- src/team.cpp | 28 +++++++++++++++------------- src/team.h | 8 ++++---- 8 files changed, 85 insertions(+), 75 deletions(-) delete mode 100644 src/behavior.cpp diff --git a/src/behavior.cpp b/src/behavior.cpp deleted file mode 100644 index f9dcbea..0000000 --- a/src/behavior.cpp +++ /dev/null @@ -1,21 +0,0 @@ -#include "behavior.h" -#include "map.h" -// TODO Info functions - -Info::Info(){ - -} - -Info::Info(Map *_map):p_map(_map){ - -} - -int Info::getNear(Dir d) -{ - return (*p_map)[Coord(d)].type; -} - -int Info::getInfo(Dir d) -{ - return (*p_map)[Coord(d)].data.nbRes; -} diff --git a/src/behavior.h b/src/behavior.h index 80064f4..8058aea 100644 --- a/src/behavior.h +++ b/src/behavior.h @@ -34,7 +34,6 @@ struct Action PUT, WORK, WAIT, - COLOR, COMMUNICATE }; @@ -43,20 +42,13 @@ struct Action Com com_data; }; -class Info +struct Info { -private: - Map *p_map; - Coord m_pos; -// TODO -public: - bool m_success; - int m_inventory; - Info(); - Info(Map*); - const Com& getCom(); - int getNear(Dir d); - int getInfo(Dir d); + virtual bool getSuccess() = 0; + virtual bool getInventory() = 0; + virtual const Com& getCom() = 0; + virtual int getNear(Dir d) = 0; + virtual int getInfo(Dir d) = 0; }; typedef void (*BehaviorFunction)(Action *action, char *memory, const Info *info); diff --git a/src/dude.cpp b/src/dude.cpp index 1d6ac1d..2ab9ee5 100644 --- a/src/dude.cpp +++ b/src/dude.cpp @@ -1,14 +1,15 @@ #include "dude.h" -#include "string.h" +#include "map.h" +#include -Dude::Dude() +Dude::Dude(const Coord &_pos, Map &map) : + m_pos(_pos), + m_map(map), + m_dead(false), + m_success(true), + m_inventory(-1) { - -} - -Dude::Dude(Coord _pos, Info _info):m_pos(_pos),m_info(_info),m_dead(false) -{ - memset(&m_memory,0,DUDE_MEMORY_SIZE*sizeof(char)); + std::memset(&m_memory, 0, DUDE_MEMORY_SIZE); } char* Dude::getMemory() @@ -16,7 +17,24 @@ char* Dude::getMemory() return m_memory; } -Info* Dude::getInfo() +void Dude::move(Dir d) { - return &m_info; + m_pos += Coord(d); + m_pos.x %= m_map.getWidth(); + m_pos.y %= m_map.getHeight(); +} + +void Dude::receiveComData(const Com &comData) +{ + std::memcpy(&m_comData, &comData, sizeof(Com)); +} + +int Dude::getNear(Dir d) +{ + return m_map[Coord(d) + m_pos].type; +} + +int Dude::getInfo(Dir d) +{ + return m_map[Coord(d) + m_pos].data.nbRes; } diff --git a/src/dude.h b/src/dude.h index 50109b3..6fd1eb4 100644 --- a/src/dude.h +++ b/src/dude.h @@ -3,18 +3,32 @@ #include "behavior.h" -class Dude +class Dude : public Info { private: Coord m_pos; - Info m_info; + Map &m_map; bool m_dead; + bool m_success; + int m_inventory; char m_memory[DUDE_MEMORY_SIZE]; + Com m_comData; + public: - Dude(); - Dude(Coord,Info); + Dude(const Coord &_pos, Map &map); char* getMemory(); - Info* getInfo(); + void setSuccess(bool success) { m_success = success; } + void setInventory(int item) { m_inventory = item; } + void trespass() { m_dead = true; } + const Coord& getPos() { return m_pos; } + void move(Dir d); + void receiveComData(const Com &comData); + + virtual bool getSuccess() { return m_success; } + virtual bool getInventory() { return m_inventory; } + virtual const Com& getCom() { return m_comData; } + virtual int getNear(Dir d); + virtual int getInfo(Dir d); }; #endif // DUDE_H diff --git a/src/simulation.cpp b/src/simulation.cpp index b45bc7b..03face5 100644 --- a/src/simulation.cpp +++ b/src/simulation.cpp @@ -8,18 +8,23 @@ Simulation::Simulation(Map *_map,std::vector _behaviors): int i=0; for(auto behavior : _behaviors){ glm::vec3 color; - color[i%3]=i*50; - m_teams[i] = Team(color,behavior); + color[i%3]=0.5f*(1 + i/3); + m_teams.push_back(Team(color, color, behavior)); i++; } } void Simulation::run() { - for(auto t : m_teams){ - if (t.second.updateSpawn()) - t.second.spawn(p_map->team(t.first)); - t.second.updateDudes(); + for(int i=0; iteam(i) + Coord(randDir); + t.spawn(spawnPos, *p_map); + } + t.updateDudes(); } } diff --git a/src/simulation.h b/src/simulation.h index 9a9daed..72907ef 100644 --- a/src/simulation.h +++ b/src/simulation.h @@ -13,9 +13,9 @@ class Simulation { private: Map *p_map; - std::map m_teams; + std::vector m_teams; public: - Simulation(Map *_map,std::vector _behaviors); + Simulation(Map *_map, std::vector _behaviors); void run(); }; diff --git a/src/team.cpp b/src/team.cpp index 2788857..d4ed590 100644 --- a/src/team.cpp +++ b/src/team.cpp @@ -1,30 +1,32 @@ #include "team.h" -Team::Team() -{ - -} - -Team::Team(glm::vec3 _color,BehaviorFunction _behavior): - m_color(_color),m_spawnCooldown(SPAWN_COOLDOWN), - m_foodQuantity(NB_STARTING_FOOD), m_behavior(_behavior) +Team::Team(const glm::vec3 &teamColor, const glm::vec3 &dudeColor, BehaviorFunction _behavior): + m_teamColor(teamColor), + m_dudeColor(dudeColor), + m_spawnCooldown(1), + m_foodQuantity(NB_STARTING_FOOD), + m_behavior(_behavior) { } bool Team::updateSpawn() { - return !(--m_spawnCooldown); + if(m_foodQuantity > 0) + return !(--m_spawnCooldown); } -void Team::spawn(Coord &d) +void Team::spawn(Coord &d, Map &map) { - m_dudes.push_back(Dude(d,Info())); + m_spawnCooldown = SPAWN_COOLDOWN; + --m_foodQuantity; + m_dudes.push_back(Dude(d, map)); } void Team::updateDudes() { - for (auto dude : m_dudes){ + for (Dude &dude : m_dudes){ Action* action; - m_behavior(action,dude.getMemory(),dude.getInfo()); + m_behavior(action, dude.getMemory(), &dude); + // TODO perform action } } diff --git a/src/team.h b/src/team.h index 2cca372..5e579de 100644 --- a/src/team.h +++ b/src/team.h @@ -14,16 +14,16 @@ class Team { - glm::vec3 m_color; + glm::vec3 m_teamColor; + glm::vec3 m_dudeColor; int m_spawnCooldown; int m_foodQuantity; std::vector m_dudes; BehaviorFunction m_behavior; public: - Team(); - Team(glm::vec3 _color,BehaviorFunction _behavior); + Team(const glm::vec3 &spawnColor, const glm::vec3 &dudeColor, BehaviorFunction _behavior); bool updateSpawn(); - void spawn(Coord &d); + void spawn(Coord &d, Map &map); void updateDudes(); };