src/ReachableCell.cpp

Go to the documentation of this file.
00001 
00008 /*
00009 This file is part of Teapot Colony Wars.
00010 
00011 Teapot Colony Wars is free software: you can redistribute it and/or modify
00012 it under the terms of the GNU General Public License as published by
00013 the Free Software Foundation, either version 2 of the License, or
00014 (at your option) any later version.
00015 
00016 Teapot Colony Wars is distributed in the hope that it will be useful,
00017 but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 GNU General Public License for more details.
00020 
00021 You should have received a copy of the GNU General Public License
00022 along with Teapot Colony Wars.  If not, see <http://www.gnu.org/licenses/>.
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   // affichage des Worshipers sous forme de cercle : calcul de l'angle entre eux
00045   GLfloat angle = 360.0 / (GLfloat) _people->size();
00046   // translatation à effectuer pour dessiner un Worshiper
00047   GLfloat translate = CELL_SIZE / -3.0;
00048 
00049   // on passe en mode dessin
00050   glMatrixMode(GL_MODELVIEW);
00051 
00052   // on dessine la cellule
00053   Cell::draw();
00054 
00055   glPushMatrix();
00056   // pour chaque Worshiper on se place à l'endroit ou on veut le dessiner
00057   // et on appelle sa méthode de dessin
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     // petite astuce : pour éviter que le destructeur de cette Cell détruise
00119     // la Cell en dessous ainsi que les pointeurs qu'elle partage avec elle,
00120     // on les positionnent à NULL
00121     _people = NULL;
00122     _pheromones = NULL;
00123     next = _next;
00124     _next = NULL;
00125   }
00126   // puis on s'auto-détruit
00127   delete this;
00128 
00129   // et on retourne la Cell qui était précédement en dessous
00130   return next;
00131 }
00132 
00133 Cell* ReachableCell::makeActions() {
00134   std::map<Pheromone*, unsigned int>::iterator it, end;
00135 
00136   // on met à jour les phéromones en fonction de leur durée de vie
00137   end = _pheromones->end();
00138   for(it = _pheromones->begin(); it != end; ++it) {
00139     if(it->second <= 1) {
00140       // on supprime la phéromone
00141       _pheromones->erase(it);
00142     } else {
00143       // sinon on la décrémente
00144       it->second--;
00145     }
00146   }
00147 
00148   return this;
00149 }

Generated on Sat Feb 2 22:22:54 2008 for Teapot Colony Wars by  doxygen 1.5.4