00001 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 #include "UntilVictoryFight.h"
00026 
00027 #include <list>
00028 
00029 #include "Cell.h"
00030 #include "Worshiper.h"
00031 #include "Random.h"
00032 
00033 UntilVictoryFight::UntilVictoryFight() {
00034 
00035 }
00036 
00037 UntilVictoryFight::~UntilVictoryFight() {
00038 
00039 }
00040 
00041 unsigned int UntilVictoryFight::resolve(Cell *c) {
00042   std::list<Worshiper*>* worshipers = NULL;
00043   std::list<Worshiper*>::iterator it, end;
00044   Worshiper *bigger = NULL;
00045   unsigned int size_max = 0, food = 0;
00046   bool restart = false;
00047 
00048   
00049   worshipers = c->getPeople();
00050   if(worshipers != NULL) {
00051     end = worshipers->end();
00052     
00053     for(it = worshipers->begin(); it != end; ++it) {
00054       if((*it)->getSize() > size_max) {
00055         bigger = *it;
00056         size_max = bigger->getSize();
00057       }
00058     }
00059     
00060     if(bigger != NULL) {
00061       end = worshipers->end();
00062       for(it = worshipers->begin(); it != end;) {
00063         
00064         if(*it != bigger && (*it)->getColony() != bigger->getColony()) {
00065           
00066           if(((*it)->getSize() == size_max) && (Random::value() % 2)) {
00067             
00068             food += bigger->getFood();
00069             c->removeWorshiper(bigger);
00070             delete(bigger);
00071             
00072             bigger = *it;
00073             
00074             
00075             restart = true;
00076           } else {
00077             
00078             food += (*it)->getFood();
00079             c->removeWorshiper(*it);
00080             delete (*it);
00081             --it;
00082           }
00083         }
00084         
00085         
00086         if(! restart) {
00087           ++it;
00088         } else {
00089           restart = false;
00090           it = worshipers->begin();
00091         }
00092       }
00093     }
00094   }
00095 
00096   return food;
00097 }