src/Worshiper.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 "Worshiper.h"
00026 
00027 #include <cmath>
00028 
00029 #include "Behaviour.h"
00030 #include "VisualContext.h"
00031 #include "Pheromone.h"
00032 #include "Colony.h"
00033 
00034 void Worshiper::compute_tex_vector() {
00035   GLfloat ratio = (GLfloat) _food / (GLfloat) getCapacity();
00036 
00037   m_tex_vector[0] = 0.0;
00038   m_tex_vector[1] = 0.0;
00039   m_tex_vector[3] = 0.0;
00040 
00041   if(ratio >= 0.4) {
00042     m_tex_vector[2] = 0.15 / ratio;
00043   } else {
00044     m_tex_vector[2] = -0.4 * log(ratio);
00045   }
00046 }
00047 
00048 Worshiper::Worshiper(Behaviour * behaviourStrategy, Colony * colony,
00049     unsigned int size, unsigned int food) :
00050     _behaviourStrategy(behaviourStrategy), _colony(colony), _size(size),
00051     _food(food), _last_command(MOVECMD_NOMOVE), _visualContext(NULL) {
00052 
00053   if(_food > getCapacity()) food = getCapacity();
00054 
00055   compute_tex_vector();
00056 
00057   _behaviourStrategy->setWorshiper(this);
00058 
00059 }
00060 
00061 Worshiper::~Worshiper() {
00062   //Delete the visual context if it is necessary
00063   destroyVisualContext();
00064 
00065   //Delete the behaviour
00066   delete _behaviourStrategy;
00067 }
00068 
00069 void Worshiper::destroyVisualContext() {
00070   if(_visualContext){
00071     delete _visualContext;
00072     _visualContext = NULL;
00073   }
00074 }
00075 
00076 void Worshiper::think() {
00077   _behaviourStrategy->think();
00078 }
00079 
00080 move_command Worshiper::getAction() {
00081   _last_command = _behaviourStrategy->getAction();
00082   return _last_command;
00083 }
00084 
00085 std::list<Pheromone*>* Worshiper::getPheromones() const {
00086   return _behaviourStrategy->getPheromones();
00087 }
00088 
00089 void Worshiper::draw() {
00090   GLfloat size;
00091 
00092   // on affiche le Worshiper avec une taille égale au log en base 2 de sa vrai
00093   // taille (de manière limiter la taille à l'affichage car un facteur 16 entre
00094   // les individus est plus dur à gérer qu'un facteur 4)
00095   size = (GLint) (log(_size) / log(2.0)) + 1;
00096 
00097   // activation de la génération automatique de coordonnées de texture
00098   glEnable(GL_TEXTURE_GEN_S);
00099   glEnable(GL_TEXTURE_1D);
00100 
00101   // changement de texture courante
00102   glBindTexture(GL_TEXTURE_1D, _colony->getTexName());
00103   // application de la texture en fonction du vecteur (permet avec une seule
00104   // texture pour tous les worshipers d'une colonie d'afficher pour chacun
00105   // son niveau de remplissage)
00106   glTexGenfv(GL_S, GL_OBJECT_PLANE, m_tex_vector);
00107 
00108   glMatrixMode(GL_MODELVIEW);
00109   glColor3f(1.0, 1.0, 1.0);
00110 
00111   // affichage (on s'arrange pour que le Worshiper repose bien sur sa Cell)
00112   glPushMatrix();
00113   glTranslatef(0.0, 0.7 * size, 0.0);
00114   glutSolidTeapot(size);
00115   glPopMatrix();
00116 
00117   // désactivation de la génération de textures
00118   glDisable(GL_TEXTURE_1D);
00119   glDisable(GL_TEXTURE_GEN_S);
00120 }
00121 
00122 void Worshiper::setFood(unsigned int new_var) {
00123   _food = new_var;
00124   // on recalcule la déformation de texture
00125   compute_tex_vector();
00126 }
00127 
00128 void Worshiper::setVisualContext(VisualContext * v) {
00129   _visualContext = v;
00130   _visualContext->setWorshiper(this);
00131 }
00132 
00133 bool Worshiper::subFood() {
00134   int res = _food - (FOOD_CONSUMPTION * _size);
00135 
00136   if(res > 0){
00137     _food = res;
00138   } else {
00139     _food = 0;
00140   }
00141 
00142   // on recalcule la déformation de texture
00143   compute_tex_vector();
00144 
00145   return (_food != 0);
00146 }

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