#include <VisualContext.h>
Public Member Functions | |
VisualContext (World *w, int i, int j) | |
Constructor. | |
virtual | ~VisualContext () |
Empty Destructor. | |
void | setWorshiper (Worshiper *w) |
Set the Worshiper. | |
bool | legal (int i, int j) const |
Determine if a pair of absolute coordinates is legal or note. | |
bool | legal (std::pair< int, int > p) const |
Determine if a pair of absolute coordinates is legal or note. | |
bool | legal (const MoveIterator &i) const |
Determine if a movement is legal or note. | |
std::vector< unsigned int > | getPheromones (move_command cmd) const |
Get the understandable pheromones in a visible cell. | |
std::vector< unsigned int > | getPheromones (const MoveIterator &i) const |
Get the understandable pheromones in a visible cell. | |
std::list< WorshiperInfo > | getWorshipers (move_command cmd) const |
Get the info about worshipers present in a cell. | |
std::list< WorshiperInfo > | getWorshipers (const MoveIterator &i) const |
Get the info about worshipers present in a cell. | |
bool | haveFood (move_command cmd) const |
Detect food in a cell. | |
bool | haveFood (const MoveIterator &i) const |
Detect food in a cell. | |
bool | reachable (move_command cmd) const |
Detect if a cell is reachable. | |
bool | reachable (const MoveIterator &i) const |
Detect if a cell is reachable. | |
move_command | opposite (move_command cmd) const |
Give the opposite movement. | |
move_command | opposite (const MoveIterator &i) const |
Give the opposite movement. | |
MoveIterator | beginMove () const |
Give the beginning iterator to get the handled movements. | |
MoveIterator | endMove () const |
Give the end iterator to get the handled movements. | |
unsigned int | nbMovements () const |
Give the number of handled movements. | |
Protected Member Functions | |
Cell * | getCell (std::pair< int, int > p) const |
Return the cell given its coordinates. | |
Cell * | getCell (move_command cmd) const |
Return the cell from the wanted move. | |
Cell * | getCell (const MoveIterator &i) const |
Return the cell from the wanted move. | |
std::vector< unsigned int > | getPheromones (Cell *c) const |
Get the understandable pheromones in a visible cell. | |
std::list< WorshiperInfo > | getWorshipers (Cell *c) const |
Get the info about worshipers present in a cell. | |
bool | haveFood (Cell *c) const |
Detect food in a cell. | |
Protected Attributes | |
World * | _world |
Worshiper * | _worshiper |
int | _i |
int | _j |
Friends | |
class | World |
Definition at line 42 of file VisualContext.h.
VisualContext::VisualContext | ( | World * | w, | |
int | i, | |||
int | j | |||
) |
Constructor.
w | the world | |
i | the vertical coordinate | |
j | the horizontal coordinate |
Definition at line 46 of file VisualContext.cpp.
00046 : 00047 _world(w), _i(i), _j(j), _worshiper(NULL) { 00048 00049 }
Cell * VisualContext::getCell | ( | std::pair< int, int > | p | ) | const [protected] |
Return the cell given its coordinates.
p | pair of coordinates |
Definition at line 72 of file VisualContext.cpp.
References _world, World::getCell(), and legal().
Referenced by getCell(), getPheromones(), getWorshipers(), haveFood(), and reachable().
00072 { 00073 Cell * res; 00074 00075 if(legal(p)){ 00076 res = _world->getCell(p.first, p.second); 00077 } else { 00078 throw "Illegal cell"; 00079 } 00080 00081 return res; 00082 }
Cell * VisualContext::getCell | ( | move_command | cmd | ) | const [protected] |
Return the cell from the wanted move.
cmd | wanted move |
Definition at line 84 of file VisualContext.cpp.
References _i, _j, _world, Move::getAbsCoords(), getCell(), and World::getMove().
Cell * VisualContext::getCell | ( | const MoveIterator & | i | ) | const [protected] |
Return the cell from the wanted move.
i | iterator pointing to the *Move handling the wanted movement |
Definition at line 88 of file VisualContext.cpp.
References _i, _j, and getCell().
vector< unsigned int > VisualContext::getPheromones | ( | Cell * | c | ) | const [protected] |
Get the understandable pheromones in a visible cell.
Return a vector of booleans. The case number i means that the pheromone number i is present in the case.
c | pointer to the cell |
Definition at line 92 of file VisualContext.cpp.
References _worshiper, Worshiper::getColony(), Cell::getPheromones(), and NUMBER_PHEROMONES.
Referenced by getPheromones(), Warrior::think(), and Explorer::think().
00092 { 00093 map<Pheromone *, unsigned int>* listPhero = c->getPheromones(); 00094 vector<unsigned int> vect = vector<unsigned int>(NUMBER_PHEROMONES); 00095 00096 Colony * colony = _worshiper->getColony(); 00097 00098 for(int i = 0; i < NUMBER_PHEROMONES; i++){ 00099 00100 map<Pheromone *, unsigned int>::iterator res = 00101 listPhero->find(colony->getPheromone(i)); 00102 00103 vect[i] = (res != listPhero->end() ? res->second : 0); 00104 00105 } 00106 00107 return vect; 00108 }
list< WorshiperInfo > VisualContext::getWorshipers | ( | Cell * | c | ) | const [protected] |
Get the info about worshipers present in a cell.
Return a vector of WorshiperInfo (one per worshiper)
c | pointer to the cell |
Definition at line 168 of file VisualContext.cpp.
References _worshiper, Cell::getPeople(), WorshiperInfo::setWorshiperInfo(), and WorshiperInfo::setWorshiperRequest().
Referenced by getWorshipers(), Warrior::think(), and GeneticBehaviour::think().
00168 { 00169 list<Worshiper*> * l = c->getPeople(); 00170 list<WorshiperInfo> li; 00171 00172 list<Worshiper*>::iterator end_l = l->end(); 00173 00174 for(list<Worshiper*>::iterator it_l = l->begin(); 00175 it_l != end_l; 00176 ++it_l){ 00177 00178 //We create a WorshiperInfo for this Worshiper and add it to the list 00179 WorshiperInfo w; 00180 w.setWorshiperInfo(*it_l); 00181 w.setWorshiperRequest(_worshiper); 00182 li.push_back(w); 00183 } 00184 00185 return li; 00186 }
bool VisualContext::haveFood | ( | Cell * | c | ) | const [protected] |
Detect food in a cell.
c | pointer to the cell |
Definition at line 132 of file VisualContext.cpp.
References Cell::getFood().
Referenced by haveFood(), GeneticBehaviour::think(), and Explorer::think().
00132 { 00133 return (c->getFood() > 0); 00134 }
void VisualContext::setWorshiper | ( | Worshiper * | w | ) |
Set the Worshiper.
w | worshiper |
Definition at line 55 of file VisualContext.cpp.
References _worshiper.
Referenced by Worshiper::setVisualContext().
00055 { 00056 _worshiper = w; 00057 }
bool VisualContext::legal | ( | int | i, | |
int | j | |||
) | const |
Determine if a pair of absolute coordinates is legal or note.
i | vertical coordinate | |
j | horizontal coordinate |
Definition at line 59 of file VisualContext.cpp.
Referenced by getCell(), legal(), Warrior::think(), GeneticBehaviour::think(), and Explorer::think().
00059 { 00060 return (i > 0 && i < _world->_height - 1 && 00061 j > 0 && j < _world->_width - 1); 00062 }
bool VisualContext::legal | ( | std::pair< int, int > | p | ) | const |
Determine if a pair of absolute coordinates is legal or note.
p | the pair of coordinates |
Definition at line 64 of file VisualContext.cpp.
References legal().
00064 { 00065 return legal(p.first, p.second); 00066 }
bool VisualContext::legal | ( | const MoveIterator & | i | ) | const |
Determine if a movement is legal or note.
i | iterator pointing to the *Move handling the wanted movement |
Definition at line 68 of file VisualContext.cpp.
References _i, _j, and legal().
vector< unsigned int > VisualContext::getPheromones | ( | move_command | cmd | ) | const |
Get the understandable pheromones in a visible cell.
Return a vector of booleans. The case number i means that the pheromone number i is present in the case.
cmd | wanted move |
Definition at line 110 of file VisualContext.cpp.
References _worshiper, getCell(), Worshiper::getColony(), Cell::getPheromones(), and NUMBER_PHEROMONES.
00110 { 00111 map<Pheromone *, unsigned int>* listPhero = getCell(cmd)->getPheromones(); 00112 vector<unsigned int> vect = vector<unsigned int>(NUMBER_PHEROMONES); 00113 00114 Colony * colony = _worshiper->getColony(); 00115 00116 for(int i = 0; i < NUMBER_PHEROMONES; i++){ 00117 00118 map<Pheromone *, unsigned int>::iterator res = 00119 listPhero->find(colony->getPheromone(i)); 00120 00121 vect[i] = (res != listPhero->end() ? res->second : 0); 00122 00123 } 00124 00125 return vect; 00126 }
vector< unsigned int > VisualContext::getPheromones | ( | const MoveIterator & | i | ) | const |
Get the understandable pheromones in a visible cell.
Return a vector of booleans. The case number i means that the pheromone number i is present in the case.
i | iterator pointing to the *Move handling the wanted movement |
Definition at line 128 of file VisualContext.cpp.
References getCell(), and getPheromones().
00128 { 00129 return getPheromones(getCell(i)); 00130 }
list< WorshiperInfo > VisualContext::getWorshipers | ( | move_command | cmd | ) | const |
Get the info about worshipers present in a cell.
Return a vector of WorshiperInfo (one per worshiper)
cmd | wanted move |
Definition at line 188 of file VisualContext.cpp.
References getCell(), and getWorshipers().
00188 { 00189 return getWorshipers(getCell(cmd)); 00190 }
list< WorshiperInfo > VisualContext::getWorshipers | ( | const MoveIterator & | i | ) | const |
Get the info about worshipers present in a cell.
Return a vector of WorshiperInfo (one per worshiper)
i | iterator pointing to the *Move handling the wanted movement |
Definition at line 192 of file VisualContext.cpp.
References getCell(), and getWorshipers().
00192 { 00193 return getWorshipers(getCell(i)); 00194 }
bool VisualContext::haveFood | ( | move_command | cmd | ) | const |
Detect food in a cell.
cmd | wanted move |
Definition at line 136 of file VisualContext.cpp.
References getCell(), and haveFood().
bool VisualContext::haveFood | ( | const MoveIterator & | i | ) | const |
Detect food in a cell.
i | iterator pointing to the *Move handling the wanted movement |
Definition at line 140 of file VisualContext.cpp.
References getCell(), and haveFood().
bool VisualContext::reachable | ( | move_command | cmd | ) | const |
Detect if a cell is reachable.
cmd | wanted move |
Definition at line 144 of file VisualContext.cpp.
References _worshiper, getCell(), and Cell::reachable().
Referenced by Warrior::think(), GeneticBehaviour::think(), and Explorer::think().
00144 { 00145 bool res = false; 00146 00147 try { 00148 res = getCell(cmd)->reachable(_worshiper); 00149 } catch(...) { 00150 res = false; 00151 } 00152 00153 return res; 00154 }
bool VisualContext::reachable | ( | const MoveIterator & | i | ) | const |
Detect if a cell is reachable.
i | iterator pointing to the *Move handling the wanted movement |
Definition at line 156 of file VisualContext.cpp.
References _worshiper, getCell(), and Cell::reachable().
00156 { 00157 bool res = false; 00158 00159 try { 00160 res = getCell(i)->reachable(_worshiper); 00161 } catch(...) { 00162 res = false; 00163 } 00164 00165 return res; 00166 }
move_command VisualContext::opposite | ( | move_command | cmd | ) | const |
Give the opposite movement.
cmd | the command |
Definition at line 196 of file VisualContext.cpp.
References _world, World::getMove(), and Move::opposite().
move_command VisualContext::opposite | ( | const MoveIterator & | i | ) | const |
Give the opposite movement.
i | iterator pointing to the *Move handling the wanted movement |
Definition at line 200 of file VisualContext.cpp.
MoveIterator VisualContext::beginMove | ( | ) | const |
Give the beginning iterator to get the handled movements.
Definition at line 204 of file VisualContext.cpp.
References _world, Move::begin(), and World::getMove().
Referenced by Warrior::think(), GeneticBehaviour::think(), and Explorer::think().
MoveIterator VisualContext::endMove | ( | ) | const |
Give the end iterator to get the handled movements.
Definition at line 208 of file VisualContext.cpp.
References _world, Move::end(), and World::getMove().
Referenced by Warrior::think(), GeneticBehaviour::think(), and Explorer::think().
unsigned int VisualContext::nbMovements | ( | ) | const |
Give the number of handled movements.
Definition at line 212 of file VisualContext.cpp.
References _world, World::getMove(), and Move::size().
Referenced by Behaviour::randomMove().
World* VisualContext::_world [protected] |
The world
Definition at line 49 of file VisualContext.h.
Referenced by beginMove(), endMove(), getCell(), nbMovements(), and opposite().
Worshiper* VisualContext::_worshiper [protected] |
The Worshiper
Definition at line 52 of file VisualContext.h.
Referenced by getPheromones(), getWorshipers(), reachable(), and setWorshiper().
int VisualContext::_i [protected] |
z position
Definition at line 55 of file VisualContext.h.
Referenced by getCell(), legal(), and World::makeCommand().
int VisualContext::_j [protected] |
x position
Definition at line 58 of file VisualContext.h.
Referenced by getCell(), legal(), and World::makeCommand().