00001
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
00063 destroyVisualContext();
00064
00065
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
00093
00094
00095 size = (GLint) (log(_size) / log(2.0)) + 1;
00096
00097
00098 glEnable(GL_TEXTURE_GEN_S);
00099 glEnable(GL_TEXTURE_1D);
00100
00101
00102 glBindTexture(GL_TEXTURE_1D, _colony->getTexName());
00103
00104
00105
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
00112 glPushMatrix();
00113 glTranslatef(0.0, 0.7 * size, 0.0);
00114 glutSolidTeapot(size);
00115 glPopMatrix();
00116
00117
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
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
00143 compute_tex_vector();
00144
00145 return (_food != 0);
00146 }