src/Explorer.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 "Explorer.h"
00026 
00027 #include <list>
00028 #include <vector>
00029 #include <map>
00030 
00031 #include "Random.h"
00032 #include "MoveCommand.h"
00033 #include "MoveIterator.h"
00034 #include "WorshiperInfo.h"
00035 #include "VisualContext.h"
00036 
00037 using namespace std;
00038 
00039 Explorer::Explorer() : HeuristicBehaviour() {
00040 
00041 }
00042 
00043 Explorer::~Explorer() {
00044 
00045 }
00046 
00047 void Explorer::think() {
00048 
00049   //Clear the pheromone list
00050   _pheros->clear();
00051 
00052   _action = MOVECMD_NOMOVE; //By default
00053 
00054   VisualContext * visu = getVisualContext();
00055   map<MoveIterator, vector<unsigned int> > pheros;
00056   map<MoveIterator, vector<unsigned int> >::iterator it_map, end_map;
00057   bool found;
00058 
00059   //For each legal and reachable cell we get all pheromones
00060   MoveIterator it_move, here_move, end_move = visu->endMove();
00061   for(it_move = visu->beginMove(); it_move != end_move; ++it_move){
00062     if(visu->legal(it_move) && visu->reachable(it_move)){
00063       pheros[it_move] = visu->getPheromones(it_move);
00064     }
00065     if(*it_move == MOVECMD_NOMOVE){
00066       here_move = it_move;
00067     }
00068   }
00069   end_map = pheros.end();
00070 
00071   //Am I on a cell where there are both EXPLORER_SEARCH and EXPLORER_GOBACK ?
00072   if(pheros[here_move][EXPLORER_SEARCH] != 0 &&
00073       pheros[here_move][EXPLORER_GOBACK] != 0 ){
00074 
00075     //I have to find a EXPLORER_SEARCH older than the one in my cell
00076     found = false;
00077     for(it_map = pheros.begin(); ! found && it_map != end_map; ++it_map){
00078       if(*(it_map->first) != MOVECMD_NOMOVE){
00079         found = (it_map->second[EXPLORER_SEARCH] ==
00080           (pheros[here_move][EXPLORER_SEARCH] - 1));
00081       }
00082     }
00083     --it_map;
00084 
00085     if(found){ //Good, I've found such a cell, let's go home !
00086 
00087       _action = *(it_map->first);
00088       //No pheromone posting ; they are already here
00089 
00090     } else { //It looks like I'm lost :'(. My last hope is to go random !
00091       _action = randomMove();
00092       putPheromone(EXPLORER_SEARCH);
00093     }
00094 
00095   } else {
00096 
00097     //Are these pheromones in the neighboorhood ?
00098     found = false;
00099     for(it_map = pheros.begin(); ! found && it_map != end_map; ++it_map){
00100       if(*(it_map->first) != MOVECMD_NOMOVE){
00101         found = (it_map->second[EXPLORER_SEARCH] != 0 &&
00102           it_map->second[EXPLORER_GOBACK] != 0);
00103       }
00104     }
00105     --it_map;
00106 
00107     if(found){
00108     //Good, now let's see if we are on a cell where there is
00109     //an EXPLORER_SEARCH older
00110 
00111       if(pheros[here_move][EXPLORER_SEARCH] ==
00112           it_map->second[EXPLORER_SEARCH] - 1){
00113         //It means the first time I went here, it was going to the other cell,
00114         //so the colony is in the other direction.
00115         //Let's try to find a EXPLORER_SEARCH older in order to follow the track
00116         int life = pheros[here_move][EXPLORER_SEARCH];
00117 
00118         found = false;
00119         for(it_map = pheros.begin(); ! found && it_map != end_map; ++it_map){
00120           if(*(it_map->first) != MOVECMD_NOMOVE){
00121             found = (it_map->second[EXPLORER_SEARCH] == life - 1);
00122           }
00123         }
00124         --it_map;
00125 
00126         if(found){ //Great, If I follow it, I will find the colony
00127           _action = *(it_map->first);
00128           putPheromone(EXPLORER_GOBACK); //To know that I am going back
00129 
00130         } else { //I must be lost... let's go random !
00131           _action = randomMove();
00132           putPheromone(EXPLORER_SEARCH); //To keep the track
00133         }
00134 
00135       } else {
00136         //It means I probably am on a food cell. So let's go back to the colony!
00137         _action = *(it_map->first); //Don't put pheromones on food cells
00138       }
00139 
00140     } else { //No, they aren't. I'm gonna have to find food by myself !
00141 
00142       //Is there any food around ?
00143       found = false;
00144       for(it_map = pheros.begin(); ! found && it_map != end_map; ++it_map){
00145         found = visu->haveFood(it_map->first);
00146       }
00147       --it_map;
00148 
00149       if(found) { //Yes, let's eat :)
00150         _action = *(it_map->first);
00151         putPheromone(EXPLORER_SEARCH); //To keep the track
00152         putPheromone(EXPLORER_GOBACK); //So that I know that I have to go back
00153 
00154       } else { //No, let's search :(
00155         _action = randomMove();
00156         putPheromone(EXPLORER_SEARCH); //To keep the track
00157       }
00158     }
00159   }
00160 }

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