void CScriptManager::Execute( CAbility* pAbility, CTile* pTile, CUnit* pCaster, CTile* TileCharged )
{
		// Finds the facing for the specified unit
		int face = pCaster->GetFacing();
		std::vector< Vec2D > TilePos;
		
		std::vector< Vec2D > pat;
		if( pAbility->GetIfFacing() )
			pat = CAbilityManager::GetInstance()->GetProperFacing(pCaster->GetFacing(), pAbility, pTile);
		else
			pat = pAbility->GetPattern();
		
		for( unsigned int i = 0; i < pAbility->GetPattern().size(); i++ )
		{
			pat[i].nPosX += pTile->GetPosition().nPosX;
			pat[i].nPosY += pTile->GetPosition().nPosY;

			TilePos.push_back(pat[i]);
		}

		lua_getglobal(L, "OnUse");

		lua_newtable(L);
		CGameManager* pGM = CGameManager::GetInstance();
		vector< CUnit* > affected;
		int nCount = 0;
		int z = (int)TilePos.size()-1;
		for( int i = 0; i <= z; i++ )
		{
			CUnit* tmp = pGM->FindUnit(TilePos[i].nPosX, TilePos[i].nPosY);
		
			if( pAbility->GetType() == SP_CHARGE || pAbility->GetType() == SP_RUSH )
			{
				if( TilePos[i] == TileCharged->GetPosition() )
					break;
			}

			if( tmp == nullptr )
				continue;

			affected.push_back( tmp );

			lua_newtable(L);
			lua_pushstring(L, "posX");
			lua_pushnumber(L, tmp->GetPos().nPosX);
			lua_settable(L, -3);
			lua_pushstring(L, "posY");
			lua_pushnumber(L, tmp->GetPos().nPosY);
			lua_settable(L, -3);
			lua_pushstring(L, "health");
			lua_pushnumber(L, tmp->GetHP());
			lua_settable(L, -3);
			lua_pushstring(L, "speed");
			lua_pushnumber(L, tmp->GetSpeed());
			lua_settable(L, -3);
			lua_pushstring(L, "shielded");
			lua_pushnumber(L, tmp->GetShielded());
			lua_settable(L, -3);
			lua_pushstring(L, "uniqueID");
			lua_pushnumber(L, tmp->GetUniqueID());
			lua_settable(L, -3);
			lua_pushnumber(L, nCount+1);
			nCount++;
			lua_insert(L, -2);
			lua_settable(L, -3);
		}

		lua_setglobal(L, "tUnitData");

		std::string path = "Assets/Ability/" + pAbility->GetLua();
		luaL_dofile(L, path.c_str());
		lua_getglobal(L, "OnUse");
		
		lua_pcall(L, 0, 0, 0);

		lua_getglobal(L, "tUnitData");
		lua_pushnil(L);
	
		vector<std::pair<std::string, int>> tData;
	//	std::pair<std::string, int> tmp;

		tData.clear();

		while(lua_next(L, -2) != 0) 
		{
			if( lua_istable(L, -1) )
			{
				lua_pushnil(L);
				while( lua_next(L, -2) )
				{
					if(lua_isnumber(L, -1))
					{
						std::pair<std::string, int> tmp;
						tmp.first = lua_tostring(L, -2);
						tmp.second = (int)lua_tonumber(L, -1);
						tData.push_back(tmp);
					}
					lua_pop(L, 1);
				}
			}
			else
			{
				if(lua_isnumber(L, -1))
				{
					std::pair<std::string, int> tmp;
					tmp.first = lua_tostring(L, -2);
					tmp.second = (int)lua_tonumber(L, -1);
					tData.push_back(tmp);
				}	
			}
			lua_pop(L, 1);
		}

		/*try
		{
			lua_close(L);
		}
		catch (int e)
		{
			int x = 9;
		}*/

		//lua_close(L);

		int x = 0;
		int y = 0;

		//for( unsigned int i = 0; i < affected.size(); i++ )
		//{
		//	x = 0;
		//	y = 0;
		//	for( unsigned int l = 0 + i*5; l < 5 + 5*i; l++ )
		//	{
		//		if( tData[l].first == "health" )
		//			affected[i]->SetHP(tData[l].second);

		//		if( tData[l].first == "posX" )
		//			x = tData[l].second;

		//		if( tData[l].first == "posY" )
		//			y = tData[l].second;

		//		if( tData[l].first == "speed" )
		//			affected[i]->SetSpeed(tData[l].second);

		//		if( tData[l].first == "shielded" )
		//		{
		//			if( tData[l].second == 1 )
		//				affected[i]->SetShielded(tData[l].second);
		//		}
		//	}
		//	affected[i]->SetPos(x,y);
		//}

		CSoundManager* pSM = CSoundManager::GetInstance();
		pSM->Play(pAbility->GetSound(), false, false);
}
void CGameManager::SaveGame(int nSlot)
{
	TiXmlDocument doc;
	TiXmlDeclaration* pDec = new TiXmlDeclaration("1.0", "utf-8", "");

	doc.LinkEndChild(pDec);

	TiXmlElement* pRoot = new TiXmlElement("Map");
	doc.LinkEndChild(pRoot);

	pRoot->SetAttribute("mapID", m_nCurrentLevel);
	pRoot->SetAttribute("phaseNumber", m_nPhaseCount);
	pRoot->SetAttribute("phase", m_nCurrentPhase);
	pRoot->SetAttribute("currPlayer", m_pCurrentPlayer->GetPlayerID());

	time_t now = time(0);
	struct tm tstruct;
	char buf[80];
	localtime_s(&tstruct, &now);

	strftime(buf, sizeof(buf), "%Y-%m-%d.%X", &tstruct);

	std::string time = buf;

	pRoot->SetAttribute("date", time.c_str());

	/////////////////////////////////////////////////////////////////
	// BUG FIX
	// Reference Bug # BB-069
	// BUG FIX START
	/////////////////////////////////////////////////////////////////


	// Added tile resource data to save system
	for (int i = 0; i < CTileManager::GetInstance()->GetNumRows(); ++i)
	{
		for (int j = 0; j < CTileManager::GetInstance()->GetNumColumns(); ++j)
		{
			CTile* pTile = CTileManager::GetInstance()->GetTile(i, j);
			if (!pTile->GetIfResourceTile())
				continue;

			if (pTile->GetIfCaptured())
			{
				MapModification mapMod;
				mapMod.modType = SP_FAKEMAPMOD;
				mapMod.posX = i;
				mapMod.posY = j;
				mapMod.otherData = pTile->GetPlayerID();
				m_vMapMods.push_back(mapMod);
			}
		}
	}

	/////////////////////////////////////////////////////////////////
	// BUG FIX END  Reference # BB-069
	/////////////////////////////////////////////////////////////////

	TiXmlElement* pMapMods = new TiXmlElement("MapModifications");
	pMapMods->SetAttribute("numModifications", m_vMapMods.size());
	pRoot->LinkEndChild(pMapMods);



	for (unsigned int i = 0; i < m_vMapMods.size(); ++i)
	{
		TiXmlElement* pMapMod = new TiXmlElement("MapModification");
		pMapMod->SetAttribute("modType", m_vMapMods[i].modType);
		pMapMod->SetAttribute("modPosX", m_vMapMods[i].posX);
		pMapMod->SetAttribute("modPosY", m_vMapMods[i].posY);
		pMapMod->SetAttribute("modOtherData", m_vMapMods[i].otherData);
		pMapMods->LinkEndChild(pMapMod);
	}

	TiXmlElement* pPlayers = new TiXmlElement("Players");
	pRoot->LinkEndChild(pPlayers);

	for (int i = 0; i < 2; ++i)
	{
		TiXmlElement* pPlayer = new TiXmlElement("Player");
		pPlayers->LinkEndChild(pPlayer);
		pPlayer->SetAttribute("id", m_vPlayers[i]->GetPlayerID());
		pPlayer->SetAttribute("ai", (int)m_vPlayers[i]->GetAI());
		pPlayer->SetAttribute("AP", m_vPlayers[i]->GetAP());
		pPlayer->SetAttribute("wood", m_vPlayers[i]->GetWood());
		pPlayer->SetAttribute("metal", m_vPlayers[i]->GetMetal());
	
		// Game Stats!

		TiXmlElement* pStats = new TiXmlElement("Stats");
		pStats->SetAttribute("numStats", 17);

		TiXmlElement* pChampionDmgDone = new TiXmlElement("ChampionDamageDone");
		pChampionDmgDone->SetAttribute("value", m_vPlayers[i]->GetStats()->nChampionDamageDone);
		pStats->LinkEndChild(pChampionDmgDone);

		TiXmlElement* pChampionHealingDone = new TiXmlElement("ChampionHealingDone");
		pChampionHealingDone->SetAttribute("value", m_vPlayers[i]->GetStats()->nChampionHealingDone);
		pStats->LinkEndChild(pChampionHealingDone);

		TiXmlElement* pSwordsmanCreated = new TiXmlElement("SwordsmanCreated");
		pSwordsmanCreated->SetAttribute("value", m_vPlayers[i]->GetStats()->nSwordsmanCreated);
		pStats->LinkEndChild(pSwordsmanCreated);

		TiXmlElement* pArcherCreated = new TiXmlElement("ArcherCreated");
		pArcherCreated->SetAttribute("value", m_vPlayers[i]->GetStats()->nArcherCreated);
		pStats->LinkEndChild(pArcherCreated);

		TiXmlElement* pCalvaryCreated = new TiXmlElement("CalvaryCreated");
		pCalvaryCreated->SetAttribute("value", m_vPlayers[i]->GetStats()->nCalvaryCreated);
		pStats->LinkEndChild(pCalvaryCreated);

		TiXmlElement* pSwordsmanDmgDone = new TiXmlElement("SwordsmanDamageDone");
		pSwordsmanDmgDone->SetAttribute("value", m_vPlayers[i]->GetStats()->nSwordsmanDamageDone);
		pStats->LinkEndChild(pSwordsmanDmgDone);

		TiXmlElement* pArcherDmgDone = new TiXmlElement("ArcherDamageDone");
		pArcherDmgDone->SetAttribute("value", m_vPlayers[i]->GetStats()->nArcherDamageDone);
		pStats->LinkEndChild(pArcherDmgDone);

		TiXmlElement* pCalvaryDmgDone = new TiXmlElement("CalvaryDamageDone");
		pCalvaryDmgDone->SetAttribute("value", m_vPlayers[i]->GetStats()->nCalvaryDamageDone);
		pStats->LinkEndChild(pCalvaryDmgDone);

		TiXmlElement* pSwordsmanKilled = new TiXmlElement("SwordsmanKilled");
		pSwordsmanKilled->SetAttribute("value", m_vPlayers[i]->GetStats()->nSwordsmanKilled);
		pStats->LinkEndChild(pSwordsmanKilled);

		TiXmlElement* pArcherKilled = new TiXmlElement("ArcherKilled");
		pArcherKilled->SetAttribute("value", m_vPlayers[i]->GetStats()->nArcherKilled);
		pStats->LinkEndChild(pArcherKilled);

		TiXmlElement* pCalvaryKilled = new TiXmlElement("CalvaryKilled");
		pCalvaryKilled->SetAttribute("value", m_vPlayers[i]->GetStats()->nCavalryKilled);
		pStats->LinkEndChild(pCalvaryKilled);

		TiXmlElement* pWoodEarned = new TiXmlElement("WoodEarned");
		pWoodEarned->SetAttribute("value", m_vPlayers[i]->GetStats()->nPlayerWoodEarned);
		pStats->LinkEndChild(pWoodEarned);

		TiXmlElement* pMetalEarned = new TiXmlElement("MetalEarned");
		pMetalEarned->SetAttribute("value", m_vPlayers[i]->GetStats()->nPlayerMetalEarned);
		pStats->LinkEndChild(pMetalEarned);

		TiXmlElement* pWoodSpent = new TiXmlElement("WoodSpent");
		pWoodSpent->SetAttribute("value", m_vPlayers[i]->GetStats()->nPlayerWoodSpent);
		pStats->LinkEndChild(pWoodSpent);

		TiXmlElement* pMetalSpent = new TiXmlElement("MetalSpent");
		pMetalSpent->SetAttribute("value", m_vPlayers[i]->GetStats()->nPlayerMetalSpent);
		pStats->LinkEndChild(pMetalSpent);

		TiXmlElement* pAPSpent = new TiXmlElement("APSpent");
		pAPSpent->SetAttribute("value", m_vPlayers[i]->GetStats()->nPlayerAPSpent);
		pStats->LinkEndChild(pAPSpent);

		pPlayer->LinkEndChild(pStats);


		TiXmlElement* pChampion = new TiXmlElement("Champion");
		CHero* pHero = dynamic_cast<CHero*>(GetChampion(m_vPlayers[i]->GetPlayerID()));
		if (pHero == nullptr)
		{
			// Why are you here?
			MessageBoxA(0, "How did you get here? Why are you saving when hero is dead? Go away. Don't save after you won",
				"ERROR:", MB_OK);
			continue;
		}
		if (pHero->GetNumWaypoints() > 0)
		{
			pChampion->SetAttribute("posX", pHero->GetLastWaypoint().nPosX);
			pChampion->SetAttribute("posY", pHero->GetLastWaypoint().nPosY);
		}
		else
		{
			pChampion->SetAttribute("posX", pHero->GetPos().nPosX);
			pChampion->SetAttribute("posY", pHero->GetPos().nPosY);
		}

		pChampion->SetAttribute("health", pHero->GetHP());
		pChampion->SetAttribute("xp", m_vPlayers[i]->GetExp());
		pChampion->SetAttribute("facing", pHero->GetFacing());
		pChampion->SetAttribute("tilesMoved", pHero->GetTilesMoved());
		pChampion->SetAttribute("hasAttacked", (int)pHero->GetHasAttacked());
		// TODO: setup spell saving here

		TiXmlElement* pSpells = new TiXmlElement("Spells");
		pSpells->SetAttribute("numSpells", pHero->GetNumSpells());
		pChampion->LinkEndChild(pSpells);

		for (unsigned int n = 0; n < pHero->GetNumSpells(); ++n)
		{
			TiXmlElement* pSpell = new TiXmlElement("Spell");
			pSpell->SetAttribute("sType", pHero->GetSpell(n)->GetType());
			pSpell->SetAttribute("Cooldown", pHero->GetCooldown(n));
			pSpells->LinkEndChild(pSpell);
		}

		TiXmlElement* pBoughtSpells = new TiXmlElement("BoughtSpells");
		pBoughtSpells->SetAttribute("numBought", pHero->GetNumBought());
		pChampion->LinkEndChild(pBoughtSpells);

		for( int n = 0; n < pHero->GetNumBought(); n++ )
		{
			TiXmlElement* pBought = new TiXmlElement("Bought");
			pBought->SetAttribute("Type", pHero->GetBought(n)->GetType());
			pBoughtSpells->LinkEndChild(pBought);
		}

		TiXmlElement* pEffects = new TiXmlElement("Effects");
		pChampion->LinkEndChild(pEffects);
		pEffects->SetAttribute("numEffects", pHero->GetNumEffects());
		for (int x = 0; x < pHero->GetNumEffects(); ++x)
		{
			TiXmlElement* pEffect = new TiXmlElement("Effect");
			pEffect->SetAttribute("ability", pHero->GetEffect(x)->GetType());
			pEffects->LinkEndChild(pEffect);
		}

		pPlayer->LinkEndChild(pChampion);

		// Lets save the units!
		TiXmlElement* pUnits = new TiXmlElement("Units");
		pPlayer->LinkEndChild(pUnits);
		int nNumUnits = 0;

		for (decltype(m_vUnits.size()) j = 0; j < m_vUnits.size(); ++j)
		{
			// this is our unit, lets save it!
			if (m_vUnits[j]->GetPlayerID() == m_vPlayers[i]->GetPlayerID() && m_vUnits[j]->GetType() != UT_HERO)
			{
				TiXmlElement* pUnit = new TiXmlElement("Unit");
				CUnit* puni = m_vUnits[j];
				nNumUnits++;
				if (puni->GetNumWaypoints() > 0)
				{
					pUnit->SetAttribute("posX", puni->GetLastWaypoint().nPosX);
					pUnit->SetAttribute("posY", puni->GetLastWaypoint().nPosY);
				}
				else
				{
					pUnit->SetAttribute("posX", puni->GetPos().nPosX);
					pUnit->SetAttribute("posY", puni->GetPos().nPosY);
				}
				pUnit->SetAttribute("unitType", (int)puni->GetType());
				pUnit->SetAttribute("health", puni->GetHP());
				pUnit->SetAttribute("facing", puni->GetFacing());
				pUnit->SetAttribute("tilesMoved", puni->GetTilesMoved());
				pUnit->SetAttribute("hasAttacked", (int)puni->GetHasAttacked());

				// Effects/debuffs on the unit
				TiXmlElement* pEffects = new TiXmlElement("Effects");
				pUnit->LinkEndChild(pEffects);
				pEffects->SetAttribute("numEffects", puni->GetNumEffects());
				for (int n = 0; n < puni->GetNumEffects(); ++n)
				{
					TiXmlElement* pEffect = new TiXmlElement("Effect");
					pEffect->SetAttribute("ability", puni->GetEffect(n)->GetType());
					pEffects->LinkEndChild(pEffect);
				}

				pUnits->LinkEndChild(pUnit);
			}
		}

		pUnits->SetAttribute("numUnits", nNumUnits);
	}

	/////////////////////////////////////////////////////////////////
	// BUG FIX
	// Reference Bug # BB-018
	// BUG FIX START
	/////////////////////////////////////////////////////////////////

	// Updated to use appdata instead of relative pathing to avoid administrative rights issues
	wchar_t path[MAX_PATH];
	HRESULT hr = SHGetFolderPathW(0, CSIDL_APPDATA, 0, SHGFP_TYPE_CURRENT, path);

	std::wstring pathtowrite(path, path+ wcslen(path));
	
	pathtowrite += L"\\LeagueOfChampionCraft";
	CreateDirectory(pathtowrite.c_str(), 0);

	std::wostringstream woss;
	woss << "\\saveslot" << nSlot << ".xml";


	pathtowrite += woss.str();
	std::string stringpath(pathtowrite.begin(), pathtowrite.end());

	doc.SaveFile(stringpath.c_str());


	/////////////////////////////////////////////////////////////////
	// BUG FIX END  Reference # BB-018
	/////////////////////////////////////////////////////////////////

}