00001
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "VisualContext.h"
00026
00027 #include <utility>
00028 #include <vector>
00029 #include <list>
00030 #include <map>
00031 #include <algorithm>
00032
00033 #include "Constants.h"
00034 #include "MoveCommand.h"
00035 #include "World.h"
00036 #include "Cell.h"
00037 #include "Colony.h"
00038 #include "Worshiper.h"
00039 #include "WorshiperInfo.h"
00040 #include "MoveIterator.h"
00041 #include "Pheromone.h"
00042 #include "Move.h"
00043
00044 using namespace std;
00045
00046 VisualContext::VisualContext(World * w, int i, int j) :
00047 _world(w), _i(i), _j(j), _worshiper(NULL) {
00048
00049 }
00050
00051 VisualContext::~VisualContext() {
00052
00053 }
00054
00055 void VisualContext::setWorshiper(Worshiper * w) {
00056 _worshiper = w;
00057 }
00058
00059 bool VisualContext::legal(int i, int j) const {
00060 return (i > 0 && i < _world->_height - 1 &&
00061 j > 0 && j < _world->_width - 1);
00062 }
00063
00064 bool VisualContext::legal(std::pair<int,int> p) const {
00065 return legal(p.first, p.second);
00066 }
00067
00068 bool VisualContext::legal(const MoveIterator& i) const {
00069 return legal(i->getAbsCoords(_i, _j));
00070 }
00071
00072 Cell* VisualContext::getCell(std::pair<int,int> p) const {
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 }
00083
00084 Cell* VisualContext::getCell(move_command cmd) const {
00085 return getCell(_world->getMove()->getAbsCoords(cmd, _i, _j));
00086 }
00087
00088 Cell* VisualContext::getCell(const MoveIterator& i) const {
00089 return getCell(i->getAbsCoords(_i, _j));
00090 }
00091
00092 vector<unsigned int> VisualContext::getPheromones(Cell * c) const {
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 }
00109
00110 vector<unsigned int> VisualContext::getPheromones(move_command cmd) const {
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 }
00127
00128 vector<unsigned int> VisualContext::getPheromones(const MoveIterator& i) const {
00129 return getPheromones(getCell(i));
00130 }
00131
00132 bool VisualContext::haveFood(Cell * c) const {
00133 return (c->getFood() > 0);
00134 }
00135
00136 bool VisualContext::haveFood(move_command cmd) const {
00137 return haveFood(getCell(cmd));
00138 }
00139
00140 bool VisualContext::haveFood(const MoveIterator& i) const {
00141 return haveFood(getCell(i));
00142 }
00143
00144 bool VisualContext::reachable(move_command cmd) const {
00145 bool res = false;
00146
00147 try {
00148 res = getCell(cmd)->reachable(_worshiper);
00149 } catch(...) {
00150 res = false;
00151 }
00152
00153 return res;
00154 }
00155
00156 bool VisualContext::reachable(const MoveIterator& i) const {
00157 bool res = false;
00158
00159 try {
00160 res = getCell(i)->reachable(_worshiper);
00161 } catch(...) {
00162 res = false;
00163 }
00164
00165 return res;
00166 }
00167
00168 list<WorshiperInfo> VisualContext::getWorshipers(Cell * c) const {
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
00179 WorshiperInfo w;
00180 w.setWorshiperInfo(*it_l);
00181 w.setWorshiperRequest(_worshiper);
00182 li.push_back(w);
00183 }
00184
00185 return li;
00186 }
00187
00188 list<WorshiperInfo> VisualContext::getWorshipers(move_command cmd) const {
00189 return getWorshipers(getCell(cmd));
00190 }
00191
00192 list<WorshiperInfo> VisualContext::getWorshipers(const MoveIterator& i) const {
00193 return getWorshipers(getCell(i));
00194 }
00195
00196 move_command VisualContext::opposite(move_command cmd) const {
00197 return _world->getMove()->opposite(cmd);
00198 }
00199
00200 move_command VisualContext::opposite(const MoveIterator& i) const {
00201 return i->getHandledOpposite();
00202 }
00203
00204 MoveIterator VisualContext::beginMove() const {
00205 return _world->getMove()->begin();
00206 }
00207
00208 MoveIterator VisualContext::endMove() const {
00209 return _world->getMove()->end();
00210 }
00211
00212 unsigned int VisualContext::nbMovements() const {
00213 return _world->getMove()->size();
00214 }