00001
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
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
00060 _tex_name = stripe.genTexture1D();
00061
00062
00063 _replicationStrategy->setColony(this);
00064
00065
00066 _pheromones = new Pheromone[NUMBER_PHEROMONES];
00067 }
00068
00069 Colony::~Colony() {
00070
00071 delete _replicationStrategy;
00072
00073
00074 delete[] _pheromones;
00075
00076
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) {
00095 _food = reste;
00096 worshiper = new Worshiper(b, this, size, food);
00097 _nb_worshipers++;
00098 } else if(_food > 0) {
00099 worshiper = new Worshiper(b, this, size, _food);
00100 _food = 0;
00101 _nb_worshipers++;
00102 }
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
00113 _food += w->getFood();
00114
00115
00116 w->destroyVisualContext();
00117
00118
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 }