00001
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "ReachableCell.h"
00026
00027 #include <GL/glut.h>
00028
00029 #include "Constants.h"
00030 #include "Worshiper.h"
00031
00032 ReachableCell::ReachableCell() : _next(NULL) {
00033 _people = new std::list<Worshiper *>;
00034 _pheromones = new std::map<Pheromone*, unsigned int>;
00035 }
00036
00037 ReachableCell::~ReachableCell() {
00038 delete _people;
00039 delete _pheromones;
00040 }
00041
00042 void ReachableCell::draw() {
00043 std::list<Worshiper *>::iterator i, end = _people->end();
00044
00045 GLfloat angle = 360.0 / (GLfloat) _people->size();
00046
00047 GLfloat translate = CELL_SIZE / -3.0;
00048
00049
00050 glMatrixMode(GL_MODELVIEW);
00051
00052
00053 Cell::draw();
00054
00055 glPushMatrix();
00056
00057
00058 for(i = _people->begin(); i != end; ++i) {
00059 glPushMatrix();
00060 glTranslatef(translate, 0.0, 0.0);
00061 (*i)->draw();
00062 glPopMatrix();
00063 glRotatef(angle, 0.0, 1.0, 0.0);
00064 }
00065 glPopMatrix();
00066 }
00067
00068 bool ReachableCell::reachable(Worshiper *w) {
00069 return (_people->size() < MAX_WORSHIPER_PER_CELL) &&
00070 (_next != NULL ? _next->reachable(w) : true);
00071 }
00072
00073 std::list<Worshiper*>* ReachableCell::getPeople() {
00074 return _people;
00075 }
00076
00077 std::map<Pheromone*, unsigned int>* ReachableCell::getPheromones() {
00078 return _pheromones;
00079 }
00080
00081 bool ReachableCell::addWorshiper(Worshiper *w) {
00082 bool ok = reachable(w);
00083
00084 if(ok) {
00085 _people->push_back(w);
00086 }
00087
00088 return ok;
00089 }
00090
00091 bool ReachableCell::removeWorshiper(const Worshiper *w) {
00092 std::list<Worshiper *>::iterator it, end = _people->end();
00093 bool find = false;
00094
00095 for(it = _people->begin(); !find && it != end; ++it) {
00096 if(find = ((*it) == w)) {
00097 _people->erase(it);
00098 }
00099 }
00100
00101 return find;
00102 }
00103
00104 void ReachableCell::addPheromone(Pheromone *p) {
00105 (*_pheromones)[p] = PHEROMONE_LIFE_TIME;
00106 }
00107
00108 void ReachableCell::push(ReachableCell *next) {
00109 _next = next;
00110 _people = _next->_people;
00111 _pheromones = _next->_pheromones;
00112 }
00113
00114 ReachableCell* ReachableCell::pop() {
00115 ReachableCell *next = NULL;
00116
00117 if(_next != NULL) {
00118
00119
00120
00121 _people = NULL;
00122 _pheromones = NULL;
00123 next = _next;
00124 _next = NULL;
00125 }
00126
00127 delete this;
00128
00129
00130 return next;
00131 }
00132
00133 Cell* ReachableCell::makeActions() {
00134 std::map<Pheromone*, unsigned int>::iterator it, end;
00135
00136
00137 end = _pheromones->end();
00138 for(it = _pheromones->begin(); it != end; ++it) {
00139 if(it->second <= 1) {
00140
00141 _pheromones->erase(it);
00142 } else {
00143
00144 it->second--;
00145 }
00146 }
00147
00148 return this;
00149 }