src/UntilVictoryFight.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 "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   // on récupère les worshipers présents sur la case
00049   worshipers = c->getPeople();
00050   if(worshipers != NULL) {
00051     end = worshipers->end();
00052     // on recherche le plus gros d'entre eux
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     // et maintenant il élimine tous les worshipers des autres colonies
00060     if(bigger != NULL) {
00061       end = worshipers->end();
00062       for(it = worshipers->begin(); it != end;) {
00063         // s'agit-il d'un ennemis ?
00064         if(*it != bigger && (*it)->getColony() != bigger->getColony()) {
00065           // Armes égales ? Que Dieu décide de l'issu de ce combat !...
00066           if(((*it)->getSize() == size_max) && (Random::value() % 2)) {
00067             // oups, on vient de se faire déssouder,
00068             food += bigger->getFood();
00069             c->removeWorshiper(bigger);
00070             delete(bigger);
00071             // un nouveau champion vient de naître
00072             bigger = *it;
00073             // et on n'a plus qu'a tout recommencer dans
00074             // la joie et la bonne humeur
00075             restart = true;
00076           } else {
00077             // ...ou pas (désolé mon petit, mais je crois que tu va souffrir)
00078             food += (*it)->getFood();
00079             c->removeWorshiper(*it);
00080             delete (*it);
00081             --it;
00082           }
00083         }
00084         // si on recommence, il ne faut pas incrémenter l'itérateur,
00085         // on se replace plutôt en début de liste.
00086         if(! restart) {
00087           ++it;
00088         } else {
00089           restart = false;
00090           it = worshipers->begin();
00091         }
00092       }
00093     }
00094   }
00095 
00096   return food;
00097 }

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