World Class Reference

#include <World.h>

Inheritance diagram for World:

Inheritance graph
[legend]
Collaboration diagram for World:

Collaboration graph
[legend]

Public Member Functions

 World (unsigned int width, unsigned int height, unsigned int nb_colonies)
 Constructor.
virtual ~World ()
 Empty Destructor.
unsigned int getWidth ()
 Get the World width.
unsigned int getHeight ()
 Get the World height.
Move * getMove () const
 Get the move responsability chain.
void addGodsGift (unsigned int i, unsigned int j)
 Add a food gift from Gods.
void iteration ()
 Make the main iteration.
virtual void draw ()
 Draw the World content with OpenGL.

Protected Member Functions

Cell *& getCell (unsigned int i, unsigned int j)
 Get one Cell of the World.
void addNewWorshipers ()
 Add the new Worshipers created by the Colonies to the World.
void addPheromone (Cell *cell, Worshiper *w)
 Add some Pheromones to a Cell.
void addCommand (Worshiper *w, unsigned int i, unsigned int j)
 Add a move command.
void makeCommand ()
 Execute the move commands.
Cell * fight (Cell *cell)
 Do all the fights on a Cell.
void feed (Cell *cell)
 Feed the Worshipers of a Cell.

Protected Attributes

unsigned int _width
unsigned int _height
Fight * _fightStrategy
std::vector< Colony * > _colonies
std::map< Worshiper *, coord_t > _commandes
Cell ** _map
Move * _move

Friends

class VisualContext

Detailed Description

< Our world

Definition at line 43 of file World.h.


Constructor & Destructor Documentation

World::World ( unsigned int  width,
unsigned int  height,
unsigned int  nb_colonies 
)

Constructor.

Parameters:
width the width of the World to create
height the height of the World to create
nb_colonies the number of Colonies to create

Definition at line 238 of file World.cpp.

References _colonies, _fightStrategy, _height, _map, _move, _width, CORRIDOR_SIZE_MAX, getCell(), j, MAX_FOOD_CAPACITY, and Random::value().

00238                                                                              :
00239     _width(width), _height(height), _colonies(nb_colonies) {
00240   unsigned int max;
00241   int i, j, n = 0;
00242 
00243   // TODO Initialisation + propre des colonies
00244   for(int i = 0; i < nb_colonies; i++){
00245     _colonies[i] = new Colony(MAX_FOOD_CAPACITY * 30, new GeneticReplication);
00246   }
00247 
00248   _map = new Cell*[_width * _height];
00249   for(i = 0; i < _height; i++) {
00250     for(j = 0; j < _width; j++) {
00251       if(i == 0 || i == _height - 1 || j == 0 || j == _width - 1) {
00252         // on s'assure que tous les bords sont bien des murs
00253         getCell(i, j) = new WallCell;
00254       } else {
00255         /*if((i + j) % 8 == 0) {
00256           getCell(i, j) = new WallCell;
00257         } else {
00258           max = MAX_FOOD_CAPACITY + 1;
00259           getCell(i, j) = new EmptyCell;
00260         }
00261         */
00262         // une case sur deux sera vide par défaut en moyenne
00263         switch(Random::value() % 10) {
00264           case 0:
00265                getCell(i, j) = new ColonyCell(_colonies[n++ % _colonies.size()], i, j);
00266             break;
00267           case 1:
00268             // il doit toujours y avoir au moins un peu de nouriture
00269             // et le taux de régénération doit être inférieur à la capacité
00270             max = (Random::value() % MAX_FOOD_CAPACITY) + 1;
00271             getCell(i, j) = new RenewableFood(max, (Random::value() % max) + 1);
00272             break;
00273           case 2:
00274             // il doit toujours y avoir au moins un peu de nouriture
00275             getCell(i, j) = new InfiniteFood((Random::value() % MAX_FOOD_CAPACITY) + 1);
00276             break;
00277           case 3:
00278             // attention, un couloir doit toujours rester traversable...
00279             getCell(i, j) = new CorridorCell((Random::value() % CORRIDOR_SIZE_MAX) + 1);
00280             break;
00281           case 4:
00282             getCell(i, j) = new WallCell;
00283             break;
00284           default:
00285             getCell(i, j) = new EmptyCell;
00286         }
00287       }
00288     }
00289   }
00290   delete getCell(7, 7);
00291   getCell(7, 7) = new ColonyCell(_colonies[0], 7, 7);
00292   delete getCell(8, 8);
00293   getCell(8, 8) = new ColonyCell(_colonies[1], 8, 8);
00294 
00295   _fightStrategy = new UntilVictoryFight;
00296 
00297   //Chaîne de responsabilité pour les actions de déplacement
00298   _move = new NoMove(
00299             new NorthMove(
00300               new SouthMove(
00301                 new WestMove(
00302                   new EastMove(
00303                     new NorthWestMove(
00304                       new NorthEastMove(
00305                         new SouthEastMove(
00306                           new SouthWestMove(NULL)
00307                         )
00308                       )
00309                     )
00310                   )
00311                 )
00312               )
00313             )
00314           );
00315 }

Here is the call graph for this function:


Member Function Documentation

Cell*& World::getCell ( unsigned int  i,
unsigned int  j 
) [inline, protected]

Get one Cell of the World.

Parameters:
i the z coordinate of the Cell
j the x coordinate of the Cell
Returns:
a ref to the pointer of the Cell

Definition at line 78 of file World.h.

References _map, and _width.

Referenced by addCommand(), addGodsGift(), draw(), VisualContext::getCell(), iteration(), makeCommand(), and World().

00078                                                         {
00079     return _map[i * _width + j];
00080   }

Here is the caller graph for this function:

void World::addPheromone ( Cell *  cell,
Worshiper *  w 
) [protected]

Add some Pheromones to a Cell.

Parameters:
cell the Cell where to drop the Pheromone
w the Worshiper that drop the Pheromone

Definition at line 91 of file World.cpp.

References Cell::addPheromone(), and Worshiper::getPheromones().

Referenced by iteration().

00091                                                  {
00092   std::list<Pheromone*>::iterator it, end;
00093   std::list<Pheromone*> *pheromones = NULL;
00094 
00095   // on récupère les phéromones a créer par l'individu
00096   pheromones = w->getPheromones();
00097   if(pheromones != NULL) {
00098     end = pheromones->end();
00099     // et on ajoute chacune des phéromones à la case (mise à jour si nécessaire)
00100     for(it = pheromones->begin(); it != end; ++it) {
00101       cell->addPheromone(*it);
00102     }
00103   }
00104 }

Here is the call graph for this function:

Here is the caller graph for this function:

void World::addCommand ( Worshiper *  w,
unsigned int  i,
unsigned int  j 
) [protected]

Add a move command.

Parameters:
w the Worshiper that want to move
i the current z position of the Worshiper
j the current x position of the Worshiper

Definition at line 106 of file World.cpp.

References _commandes, _height, _move, _width, Move::getAbsCoords(), Worshiper::getAction(), getCell(), and Cell::reachable().

Referenced by iteration().

00106                                                                    {
00107   std::pair<unsigned int, unsigned int> coord;
00108 
00109   // ou doit on aller ?
00110   coord = _move->getAbsCoords(w->getAction(), i, j);
00111   // est-ce que l'on se déplace bien, que les coordonées sont valides
00112   // et que la case atteignable ?
00113   if((coord.first != i || coord.second != j) &&
00114       coord.first > 0 && coord.first < _height - 1 &&
00115       coord.second > 0 && coord.second < _width - 1 &&
00116       getCell(coord.first, coord.second)->reachable(w)) {
00117     // si c'est le cas, alors on met en attente une commande de déplacement
00118     coord_t c =  {coord.first, coord.second, i, j};
00119     _commandes[w] = c;
00120   }
00121 }

Here is the call graph for this function:

Here is the caller graph for this function:

Cell * World::fight ( Cell *  cell  )  [protected]

Do all the fights on a Cell.

Parameters:
cell the cell where the fights take place
Returns:
the new Cell after the battle has ended

Definition at line 146 of file World.cpp.

References _fightStrategy, Cell::addFood(), ColonyCell::getColony(), Colony::incrementFood(), ReachableCell::push(), and Fight::resolve().

Referenced by iteration().

00146                              {
00147   ReachableCell *next = NULL;
00148   ColonyCell *colony = NULL;
00149   CorpseFood *dead = NULL;
00150   unsigned int food;
00151 
00152   // on effectue le combat conformément à la stratégie établie
00153   food = _fightStrategy->resolve(cell);
00154   // que leurs cadavres nourrissent la terre (et leurs congénères)
00155   if(food > 0) {
00156     if(dynamic_cast<CorpseFood*>(cell)) {
00157       // on est déjà sur une CorpseFood, on ajoute juste de la nouriture.
00158       cell->addFood(food);
00159     } else {
00160       // sinon on en crée une.
00161       // la case à recouvrir doit être atteignable
00162       next = dynamic_cast<ReachableCell*>(cell);
00163       //Une CorpseFood ne doit pas recouvrir une colonie
00164       if(colony = dynamic_cast<ColonyCell*>(cell)) {
00165         // on ajoute plutôt la nouriture produite à la colonie
00166         colony->getColony()->incrementFood(food);
00167       } else if(next) {
00168         // création de la CorpseFood
00169         dead = new CorpseFood(food);
00170         if(dead != NULL) {
00171           // on l'empile par dessus l'ancienne case
00172           dead->push(next);
00173           // et on s'assure que la case retournée sera bien la nouvelle.
00174           cell = dead;
00175         }
00176       }
00177     }
00178   }
00179 
00180   return cell;
00181 }

Here is the call graph for this function:

Here is the caller graph for this function:

void World::feed ( Cell *  cell  )  [protected]

Feed the Worshipers of a Cell.

Parameters:
cell the Cell where we want to feed the Worshipers in

Definition at line 183 of file World.cpp.

References Cell::getFood(), Cell::getPeople(), Cell::removeWorshiper(), and Cell::subFood().

Referenced by iteration().

00183                            {
00184   std::list<Worshiper*> *worshipers = NULL;
00185   std::list<Worshiper*>::iterator it, end;
00186   unsigned int nb_people, ratio;
00187   unsigned int needed, consumed;
00188   unsigned int food;
00189   bool full;
00190 
00191   // qui est sur la case ?
00192   worshipers = cell->getPeople();
00193   // combien de nouriture est diponible ?
00194   food = cell->getFood();
00195   // si il y a de la nouriture et des worshipers sur la case
00196   if(food > 0 && worshipers != NULL && (nb_people = worshipers->size()) > 0) {
00197     // tout le monde n'a pas mangé à sa faim (et pour cause : personne
00198     // n'a encore mangé quoi que ce soit)
00199     full = false;
00200     // tant qu'il reste de la nouriture et que tout le monde n'est pas gavé
00201     while(food > 0 && ! full) {
00202       // on part du principe que tu le monde aura eu sa dose, les insatisfaits
00203       // n'ont qu'a faire une réclamation
00204       full = true;
00205       // on partage la nouriture en portion égales
00206       ratio = (unsigned int) (food / nb_people) + 1;
00207       // et chaque worshipers reçoit sa part
00208       end = worshipers->end();
00209       for(it = worshipers->begin(); food > 0 && it != end; ++it) {
00210         // on calcule dabord ce dont il a besoin
00211         needed = (*it)->getCapacity() - (*it)->getFood();
00212         // on vérifie ensuite si il pourra manger à sa faim
00213         full = !(full && needed > ratio);
00214         // on lui attribue sa part
00215         needed = (needed > ratio ? ratio : needed);
00216         // on la soustrait de la quantité totale
00217         consumed = cell->subFood(needed);
00218         // on met la quantité de nouriture disponible à jour
00219         food = cell->getFood();
00220         // et on lui donne ce qui a été réellement soustrait
00221         (*it)->setFood((*it)->getFood() + consumed);
00222       }
00223     }
00224   }
00225   // maintenant on décrémente d'une unité la nouriture de chaque individu
00226   end = worshipers->end();
00227   for(it = worshipers->begin(); it != end; ++it) {
00228     if(! (*it)->subFood()) {
00229       // oups... je crois qu'on est mort de faim. que c'est bête...
00230       // adieux !
00231       delete (*it);
00232       cell->removeWorshiper(*it);
00233       --it;
00234     }
00235   }
00236 }

Here is the call graph for this function:

Here is the caller graph for this function:

unsigned int World::getWidth (  ) 

Get the World width.

Returns:
the value of _width

Definition at line 328 of file World.cpp.

References _width.

Referenced by Display::Display(), Display::init(), and Display::translate_camera().

00328                              {
00329   return _width;
00330 }

Here is the caller graph for this function:

unsigned int World::getHeight (  ) 

Get the World height.

Returns:
the value of _height

Definition at line 332 of file World.cpp.

References _height.

Referenced by Display::Display(), Display::init(), and Display::translate_camera().

00332                               {
00333   return _height;
00334 }

Here is the caller graph for this function:

Move * World::getMove (  )  const

Get the move responsability chain.

Returns:
the move repsonsability chain

Definition at line 336 of file World.cpp.

References _move.

Referenced by VisualContext::beginMove(), VisualContext::endMove(), VisualContext::getCell(), VisualContext::nbMovements(), and VisualContext::opposite().

00336                            {
00337   return _move;
00338 }

Here is the caller graph for this function:

void World::addGodsGift ( unsigned int  i,
unsigned int  j 
)

Add a food gift from Gods.

Parameters:
i the z position of the Cell to create
j the x position of the Cell to create

Definition at line 340 of file World.cpp.

References getCell(), GODS_GIFT_FOOD_VALUE, and ReachableCell::push().

Referenced by Display::inputKey().

00340                                                       {
00341   ReachableCell *next = NULL;
00342   GodsGiftFood *gift = NULL;
00343   Cell *cell = NULL;
00344 
00345   cell = getCell(i, j);
00346   next = dynamic_cast<ReachableCell*>(cell);
00347   //Une GodsGiftFood ne doit pas recouvrir une colonie
00348   if(next && ! dynamic_cast<ColonyCell*>(cell)) {
00349     gift = new GodsGiftFood(GODS_GIFT_FOOD_VALUE);
00350     if(gift != NULL) {
00351       // on l'empile par dessus l'ancienne case
00352       gift->push(next);
00353       // et on s'assure que la case retournée sera bien la nouvelle.
00354       getCell(i, j) = gift;
00355     }
00356   }
00357 }

Here is the call graph for this function:

Here is the caller graph for this function:


Field Documentation

unsigned int World::_width [protected]

The world width

Definition at line 50 of file World.h.

Referenced by addCommand(), draw(), getCell(), getWidth(), iteration(), World(), and ~World().

unsigned int World::_height [protected]

The world height

Definition at line 53 of file World.h.

Referenced by addCommand(), draw(), getHeight(), iteration(), World(), and ~World().

Fight* World::_fightStrategy [protected]

The Fight strategy

Definition at line 56 of file World.h.

Referenced by fight(), World(), and ~World().

std::vector<Colony*> World::_colonies [protected]

The list of the Colonies in the World

Definition at line 59 of file World.h.

Referenced by addNewWorshipers(), and World().

std::map<Worshiper*, coord_t> World::_commandes [protected]

The map of the move commands to do

Definition at line 62 of file World.h.

Referenced by addCommand(), and makeCommand().

Cell** World::_map [protected]

The Cell array

Definition at line 65 of file World.h.

Referenced by getCell(), World(), and ~World().

Move* World::_move [protected]

Responsability chain : all the handled moves

Definition at line 68 of file World.h.

Referenced by addCommand(), getMove(), World(), and ~World().


The documentation for this class was generated from the following files:
Generated on Sat Feb 2 22:23:31 2008 for Teapot Colony Wars by  doxygen 1.5.4