/* 
 * Tactic is the legal property of its developers, whose names are too numerous 
 * to list here.  Please refer to the COPYRIGHT file distributed with this 
 * source distribution. 
 * 
 * This library is free software; you can redistribute it and/or 
 * modify it under the terms of the GNU Lesser General Public 
 * License as published by the Free Software Foundation; either 
 * version 2.1 of the License, or (at your option) any later version. 
 * 
 * This library is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
 * Lesser General Public License for more details. 
 * 
 * You should have received a copy of the GNU Lesser General Public 
 * License along with this library; if not, write to the Free Software 
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
 *
 * See README for more informations
 *
 * OrderReconnaissance.cpp
 *
 * ...
 * Date 22/03/06
 *
 */
#include "OrderReconnaissance.h"
#include "GroupPathFinder.h"
#include "GroupPathFinderManager.h"
#include "GroundArmyInfo.h"
#include "GroundMap.h"
#include "Point2D.h"
#include "GroupAI.h"

OrderReconnaissance::OrderReconnaissance()
{
	lastPathFinder = NULL;
	count = 0;
}

OrderReconnaissance::OrderReconnaissance(GroupAI* g) : Order(g)
{
	lastPathFinder = NULL;
	count = 0;
}

void OrderReconnaissance::execute()
{
	DebugAI::trace('o', "Executing OrderReconnaissance");


	/* we did not reach the goal between the two AI "ticks" */
	//if (ground::GroupPathFinderManager::getInstance().containGroup(lastPathFinder) && !lastPathFinder->moveFinished())
	if(!isFinished() && count < 6)
	{
		count ++;
		DebugAI::trace('o', "Next order not finished, OrderReconnaissance delayed: "+Converter::floatToString((float)count));
		// nothing to do, the path is already calculated.
	} else {
		Order::execute();
		count = 0;
		ground::GroundArmyInfo* toto = myGroup->getGroundArmyInfo();
		ground::GroundMap &map = ground::GroundMap::getInstance(); // used to move the group to specific place
		Point2D placeToVisit; // location of the place to visit
		ground::GroupPathFinder* pathFinder;  // used to manage the group of unit with the pathfinder

		myGroup->resetActions();

		pathFinder = new ground::GroupPathFinder();
		placeToVisit = (myGroup->getGroundArmyInfo())->getOldestCase(myGroup->getPosition(), 32);
		lastPathFinder = pathFinder;

		for (std::vector<Unit*>::iterator it = myGroup->getUnits()->begin(); it != myGroup->getUnits()->end();++it) {
			(*it)->notifyEndOrder("OrderReconnaissance");
			pathFinder->addUnit(*it);
		}

		map.setPathFinderMode(ground::pathFinding::HIDDEN_EXPLORATION);
		map.moveGroupTo(*pathFinder, placeToVisit);
	}

	DebugAI::trace('o', "OrderReconnaissance Executed");
}

OrderReconnaissance::~OrderReconnaissance() {}

FuzzyValueName OrderReconnaissance::getType()
{
	return ORDER_RECONNAISSANCE;
}