#include "Den.h"
#include "../Interface/Actions.h"


namespace mechanics
{

	void Den::killWilding(Wildling *w) {
		for(auto it=spawners.begin();it!=spawners.end();++it) {
			for(auto wit=(*it)->wildlings.begin();wit!=(*it)->wildlings.end();++wit) {
				if(*wit==w) {
					(*it)->wildlings.erase(wit);
					break;
				}
			}
		}

		PlayerGame::current().destroyWildling(*w);
	}

	void Den::beginAttack() {
		std::cout<<"Attacking to town"<<std::endl;
		for(auto it=spawners.begin();it!=spawners.end();++it) {
			for(auto wit=(*it)->wildlings.begin();wit!=(*it)->wildlings.end();++wit) {
				int limit=20;
				auto pnt=gge::utils::Random(PlayerGame::current().getTownBounds());
				while(limit-- && !Map::current().getPointInfoMap()(pnt).buildable)
					pnt=gge::utils::Random(PlayerGame::current().getTownBounds());

				(*wit)->moveTo(pnt);
			}
		}
		mNextAttack=(unsigned)currentGameTime+mProvider.regularAttackCD*MINUTE/60;
	}

	void Den::Killed(Attacker *attacker, BaseWeapon *weapon) {
		Attackable::Killed(attacker, weapon);
		PlayerGame::current().destroyDen(*this);

		for(auto it=Map::current().destroyedEvents.begin();it!=Map::current().destroyedEvents.end();++it) {
			if(it->object==this && !it->isTriggered && interface::showEvent(*it)) {
				it->isTriggered=true;
			}
		}

		removeFromMap();
	}

}