float ClosetoBorderFactor::calcDecision(Crood &crood)
{
	int close = 5;
	MapDataManager* mapdata = MapDataManager::getSingletonPtr();
	int mapsize = mapdata->getMapSize();
	for(int x = crood.mX - 2; x <= crood.mX + 2; x++)
	{
		for(int y = crood.mY- 2; y <= crood.mY + 2; y++)
		{
			bool out = false;
			if(x < -1 || y < -1 || x > mapsize || y > mapsize)
			{
				out = true;
			}
			else
			{
				int id = mapdata->getGridId(x, y);
				std::set<int>::iterator ite = mAreaSet.find(id);
				if(mAreaSet.end() == ite)
				{
					out = true;
				}
			}
			if(out)
			{
				int dist = GetDistance(x, y, crood.mX, crood.mY);
				if(dist < close )
					close = dist;
			}
		}
	}
	return 100.0f - 25.0f * close;
}
float HighTerrainAttrAreaFactor::calcDecision(Crood &crood)
{
	float possibility = 0.0f;
	MapDataManager* mapdata = MapDataManager::getSingletonPtr();
	for(int x = crood.mX - 2; x <= crood.mX + 2; x++)
	{
		for(int y = crood.mY- 2; y <= crood.mY + 2; y++)
		{
			int dist = 4 - GetDistance(x, y, crood.mX, crood.mY);
			switch(mAttrType)
			{
			case 0:
				possibility += 0.83f * dist * mapdata->getDefModify(x, y, -1);
				break;
			case 1:
				possibility += 0.83f * dist * mapdata->getCovert(x, y, -1);
				break;
			case 2:
				possibility += 0.83f * dist * (mapdata->getInfApCost(x, y, -1) - 2.0f);
				break;
			case 3:
				possibility += 0.83f * dist * (mapdata->getCavApCost(x, y, -1) - 2.0f);
				break;
			}
		}
	}
	return possibility;
}
bool BattleSquad::move(int tgx, int tgy, unsigned int &eventflag, bool costap)
{
	MapDataManager* mapdatamanager = MapDataManager::getSingletonPtr();
	if(mapdatamanager->getPassable(tgx, tgy, getFaction()))
	{
		float mapapcost = 0.0f;
		if(getHorseId() != "none")
			mapapcost = mapdatamanager->getCavApCost(tgx, tgy, getFaction());
		else
			mapapcost = mapdatamanager->getInfApCost(tgx, tgy, getFaction());

		LuaTempContext* luatempcontext = new LuaTempContext();
		luatempcontext->strMap["squadid"] = getSquadId();
		luatempcontext->intMap["srcx"] = getGridX();
		luatempcontext->intMap["srcy"] = getGridY();
		luatempcontext->intMap["tgtx"] = tgx;
		luatempcontext->intMap["tgty"] = tgy;
		luatempcontext->floatMap["apcost"] = mapapcost;
		Trigger("MoveTo", luatempcontext);
		mapdatamanager->Trigger("MoveTo", luatempcontext);
		mapapcost = luatempcontext->floatMap["apcost"];
		delete luatempcontext;

		if(costap)
		{
			float ap = getActionPoint();
			if(mapapcost <= ap)
			{
				ap -= mapapcost;
				setActionPoint(ap);
				if(mapapcost <= 2)
				{
					eventflag |= MOVEEVENT_CHARGE;
					int curdir = GetDirection(getGridX(), getGridY(), tgx, tgy);
					eventflag &= 0x03f;
					eventflag |= SetChargeDir(curdir);
				}
				else
				{
					eventflag &= ~MOVEEVENT_CHARGE;
				}
				return true;
			}
		}
		else
		{
			return true;
		}
	}
	eventflag |= MOVEEVENT_WRONG;
	return false;
}
//ClosetoBorderFactor
ClosetoBorderFactor::ClosetoBorderFactor(Area &area)
{
	MapDataManager* mapdata = MapDataManager::getSingletonPtr();
	int mapsize = mapdata->getMapSize();
	std::vector<Crood> croodvec = area.getCroodVec();
	std::vector<Crood>::iterator ite = croodvec.begin();
	for( ; ite != croodvec.end(); ite++ )
	{
		if((*ite).mX < -1 || (*ite).mY < -1 || (*ite).mX > mapsize || (*ite).mY > mapsize)
		{
			continue;
		}
		mAreaSet.insert(mapdata->getGridId((*ite).mX, (*ite).mY));
	}
}
bool BattleDeployState::mousePressed(const OIS::MouseEvent &arg, OIS::MouseButtonID id)
{
	int GX,GY;
	Terrain::getSingletonPtr()->coordinateToGrid(arg.state.X.abs,arg.state.Y.abs,GX,GY);
	if(mSelectSquad)
	{
		if(id == OIS::MB_Left)
		{
			MapDataManager* datamanager = MapDataManager::getSingletonPtr();
			if(mAreaGrap->inArea(GX,GY) && datamanager->getPassable(GX,GY,0) && BattleSquadManager::getSingleton().getBattleSquadAt(GX,GY,0,false) == NULL)
			{
				std::string id = mSelectSquad->getSquadId();
				SquadGraphics* squadgrap = SquadGrapManager::getSingleton().getSquad(id);
				squadgrap->setGrid(GX,GY);
				mSelectSquad->setGridX(GX);
				mSelectSquad->setGridY(GY);
				mDeployWindow->setDeployInfo(mSelectIndex,StringTable::getSingleton().getString("DeployConfirm"));
				mMapWindow->updatePoint();
				if(BattleSquadManager::getSingleton().allDeployed())
					mDeployWindow->setAllowConfirm(true);
			}
		}
		else if(id == OIS::MB_Right)
		{
			int x,y;
			x = mSelectSquad->getGridX();
			y = mSelectSquad->getGridY();
			Direction d = GetDirection(x,y, GX, GY);
			mSelectSquad->setDirection(d);
			std::string grapid = mSelectSquad->getSquadId();
			SquadGraphics* squadgrap = SquadGrapManager::getSingleton().getSquad(grapid);
			squadgrap->setDirection(d,false);
			mSquadWindow->setSquad(mSelectSquad);
		}

	}
	return true;
}