src/Colony.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 "Colony.h"
00026 
00027 #include "Constants.h"
00028 #include "GenColor.h"
00029 #include "Image.h"
00030 #include "Pheromone.h"
00031 #include "Replication.h"
00032 #include "Worshiper.h"
00033 
00034 Colony::Colony(unsigned int food, Replication * replication) :
00035     _replicationStrategy(replication),
00036     _nb_worshipers(0),
00037     _food(food) {
00038   Image stripe(WORSHIPER_TEXTURE_SIZE, 1);
00039   GLubyte *pixels;
00040   int i;
00041 
00042   _colors = GenColor::instance()->getNewColor();
00043 
00044   // creation of the texture image for displaying worshipers
00045   pixels = stripe.getPixels();
00046   for(i = 0; i < WORSHIPER_TEXTURE_SIZE; i++) {
00047     if(i < WORSHIPER_TEXTURE_SIZE / 2) {
00048       pixels[4 * i]     = _colors[0];
00049       pixels[4 * i + 1] = _colors[1];
00050       pixels[4 * i + 2] = _colors[2];
00051       pixels[4 * i + 3] = (GLubyte) COLOR_MAX;
00052     } else {
00053       pixels[4 * i]     = _colors[0] + COLOR_LIMIT;
00054       pixels[4 * i + 1] = _colors[1] + COLOR_LIMIT;
00055       pixels[4 * i + 2] = _colors[2] + COLOR_LIMIT;
00056       pixels[4 * i + 3] = (GLubyte) COLOR_MAX;
00057     }
00058   }
00059   // generation of the texture's id
00060   _tex_name = stripe.genTexture1D();
00061 
00062   //Reciproc link with the Replication
00063   _replicationStrategy->setColony(this);
00064 
00065   //Pheromones generation
00066   _pheromones = new Pheromone[NUMBER_PHEROMONES];
00067 }
00068 
00069 Colony::~Colony() {
00070   //Replication
00071   delete _replicationStrategy;
00072 
00073   //Phéromones
00074   delete[] _pheromones;
00075 
00076   //Couleur
00077   delete[] _colors;
00078 }
00079 
00080 unsigned int Colony::getNbCells() {
00081   return _cells.size();
00082 }
00083 
00084 void Colony::setReplication(Replication * r) {
00085   _replicationStrategy = r;
00086 }
00087 
00088 Worshiper* Colony::createWorshiper(unsigned int size, unsigned int food,
00089     Behaviour * b) {
00090 
00091   Worshiper * worshiper = NULL;
00092   int reste = _food - food;
00093 
00094   if(reste >= 0) { //There is enough food
00095     _food = reste;
00096     worshiper = new  Worshiper(b, this, size, food);
00097     _nb_worshipers++;
00098   } else if(_food > 0) { //There is not enough food, just a little we can spend
00099     worshiper = new  Worshiper(b, this, size, _food);
00100     _food = 0;
00101     _nb_worshipers++;
00102   } //else we have no food at all
00103 
00104   return worshiper;
00105 }
00106 
00107 Pheromone* Colony::getPheromone(unsigned int i) {
00108   return (i < NUMBER_PHEROMONES ? &_pheromones[i] : NULL);
00109 }
00110 
00111 void Colony::addSurvivor(Worshiper* w) {
00112   //Add the food to the colony
00113   _food += w->getFood();
00114 
00115   //Destroy the worshiper's visual context
00116   w->destroyVisualContext();
00117 
00118   //The replication handles it
00119   _replicationStrategy->addSurvivor(w);
00120 }
00121 
00122 unsigned int Colony::getNbWorshipers() {
00123   return _nb_worshipers;
00124 }
00125 
00126 void Colony::addCell(ColonyCell* cell) {
00127   _cells.push_front(cell);
00128 }
00129 
00130 void Colony::getCellIterators(std::list<ColonyCell*>::iterator & begin,
00131                               std::list<ColonyCell*>::iterator & end) {
00132   begin = _cells.begin();
00133   end = _cells.end();
00134 }
00135 
00136 std::list<Worshiper*>* Colony::getNewWorshipers() {
00137   return _replicationStrategy->getNewWorshipers();
00138 }
00139 
00140 unsigned int Colony::getFood() const {
00141     return _food;
00142   }
00143 
00144 void Colony::incrementFood(unsigned int inc) {
00145   _food += inc;
00146 }
00147 
00148 bool Colony::decrementFood(unsigned int dec) {
00149   bool res;
00150   if(_food >= dec) {
00151     _food -= dec;
00152     res = true;
00153   } else {
00154     res = false;
00155   }
00156   return res;
00157 }
00158 
00159 void Colony::setColor(GLubyte red, GLubyte green, GLubyte blue) {
00160   _colors[0] = red;
00161   _colors[1] = green;
00162   _colors[2] = blue;
00163 }
00164 
00165 GLubyte* Colony::getColor() {
00166   return _colors;
00167 }
00168 
00169 
00170 GLuint Colony::getTexName() {
00171   return _tex_name;
00172 }

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