src/Warrior.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 "Warrior.h"
00026 
00027 #include <list>
00028 #include <vector>
00029 #include <map>
00030 
00031 #include "Random.h"
00032 #include "MoveCommand.h"
00033 #include "WorshiperInfo.h"
00034 #include "VisualContext.h"
00035 #include "MoveIterator.h"
00036 
00038 #define WARRIOR_DANGER    2
00039 
00040 using namespace std;
00041 
00042 Warrior::Warrior() : HeuristicBehaviour() {
00043 
00044 }
00045 
00046 Warrior::~Warrior() {
00047 
00048 }
00049 
00050 void Warrior::think() {
00051    //Clear the pheromone list
00052   _pheros->clear();
00053 
00054   _action = MOVECMD_NOMOVE; //By default
00055 
00056   VisualContext * visu = getVisualContext();
00057   MoveIterator it_move, it2_move, end_move;
00058   list<WorshiperInfo> winfos;
00059   list<WorshiperInfo>::iterator it_winfos, end_winfos;
00060   vector<unsigned int> pheros;
00061   bool found;
00062 
00063   //We try to locate a cell with people
00064   found = false;
00065   end_move = visu->endMove();
00066   for(it_move = visu->beginMove(); !found && it_move != end_move; ++it_move){
00067     if(*it_move != MOVECMD_NOMOVE && visu->legal(it_move) && visu->reachable(it_move)){
00068       winfos = visu->getWorshipers(it_move);
00069 
00070       if(!winfos.empty()){
00071 
00072         end_winfos = winfos.end();
00073 
00074         //Is there an ennemy ?
00075         for(it_winfos = winfos.begin(); !found && it_winfos != end_winfos; ++it_winfos){
00076           found = it_winfos->isEnnemy();
00077         }
00078         --it_winfos;
00079 
00080         //There is an ennemy ! Let's fight !!!
00081         //NB : I am so aggressive I don't even check his size...
00082         if(found){
00083           _action = *it_move;
00084           putPheromone(WARRIOR_DANGER);
00085         }
00086       }
00087     }
00088   }
00089 
00090   //No ennemies, too bad... Let's see if there are some WARRIOR_DANGER !
00091   if(!found){
00092     found = false;
00093     for(it_move = visu->beginMove(); !found && it_move != end_move; ++it_move){
00094       if(*it_move != MOVECMD_NOMOVE && visu->legal(it_move) && visu->reachable(it_move)){
00095         it2_move = it_move;
00096         pheros = visu->getPheromones(it_move);
00097         found = (pheros[WARRIOR_DANGER] > 0);
00098       }
00099     }
00100 
00101     //Ok, let's hope there is an ennemy...
00102     if(found){
00103       _action = *it2_move;
00104     } else {
00105       _action = randomMove();
00106     }
00107 
00108   }
00109 }

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