コード例 #1
0
ファイル: Game.cpp プロジェクト: DrivenByHim/TextBasedGames
void Game::Play () {
    m_current = FIRST;
    m_board.Reset();

    while (IsPlaying()) {
        m_board.Display();
        m_players[m_current].MakeMove(m_board);
        SetNextPlayer();
    }

    m_board.Display();
    AnnounceWinner();
}
コード例 #2
0
void CGameManager::LoadSave(int nSlot)
{
	Reset();

	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());

	TiXmlDocument doc;
	if (doc.LoadFile(stringpath.c_str()))
	{
		TiXmlElement* pRoot = doc.RootElement();


		if (pRoot == nullptr)
			return;

		int nMapID, nCurrPlayer, nPhaseCount, nCurrPhase;
		pRoot->QueryIntAttribute("mapID", &nMapID);
		pRoot->QueryIntAttribute("currPlayer", &nCurrPlayer);
		pRoot->QueryIntAttribute("phaseNumber", &nPhaseCount);
		pRoot->QueryIntAttribute("phase", &nCurrPhase);


		LoadMap(nMapID);

		// Map modifcations! Weeeeeeeee
		int nNumMapModifications;
		TiXmlElement* pMapMods = pRoot->FirstChildElement("MapModifications");
		pMapMods->QueryIntAttribute("numModifications", &nNumMapModifications);
		TiXmlElement* pMapModification = pMapMods->FirstChildElement("MapModification");
		for (int i = 0; i < nNumMapModifications; ++i)
		{
			MapModification mod;
			int nType;
			pMapModification->QueryIntAttribute("modType", &nType);
			mod.modType = (SPELL_TYPE)nType;
			pMapModification->QueryIntAttribute("modPosX", &mod.posX);
			pMapModification->QueryIntAttribute("modPosY", &mod.posY);
			pMapModification->QueryIntAttribute("modOtherData", &mod.otherData);
			switch (mod.modType)
			{
			case SP_FAKEMAPMOD:
				{
					CTile* selectedTile = CTileManager::GetInstance()->GetTile(mod.posX, mod.posY);
					if (selectedTile != nullptr)
					{
						selectedTile->SetPlayerID(mod.otherData);
						selectedTile->SetIfCaptured(true);
					}
				}
				break;
			case SP_RAISEMOUNTAIN:
				{
					CTile* selectedTile = CTileManager::GetInstance()->GetTile(mod.posX, mod.posY);
					if( selectedTile != nullptr )
					{	
						if (selectedTile->GetTileType() != TT_FARM && selectedTile->GetTileType() != TT_MILL &&
							selectedTile->GetTileType() != TT_MINE)
						{
							selectedTile->SetTileType(TT_MOUNTAINS);
							selectedTile->SetIfImpassable(false);
							CGameManager::GetInstance()->AddModification(mod);
						}
					}
				}
				break;
			case SP_DESTROYFOREST:
				{
					CTile* selectedTile = CTileManager::GetInstance()->GetTile(mod.posX, mod.posY);
					if( selectedTile != nullptr )
					{	
						if (selectedTile->GetTileType() == TT_FOREST)
						{
							selectedTile->SetTileType(TT_PLAINS);
							CGameManager::GetInstance()->AddModification(mod);
						}
					}
				}
				break;

			case SP_ICEAGE:
				{
					CTile* selectedTile = CTileManager::GetInstance()->GetTile(mod.posX, mod.posY);
					if( selectedTile != nullptr )
					{	
						selectedTile->SetIfFrozen(true);
						CGameManager::GetInstance()->AddModification(mod);
					}
				}
				break;
			}

			pMapModification = pMapModification->NextSiblingElement("MapModification");

		}


		TiXmlElement* pPlayers = pRoot->FirstChildElement("Players");
		TiXmlElement* pPlayer = pPlayers->FirstChildElement("Player");

		for (decltype(m_vPlayers.size()) i = 0; i < m_vPlayers.size(); ++i)
		{
	
			delete m_vPlayers[i];
		}
		m_vPlayers.clear();
		m_nNewPlayerID = 0;
		for (int np = 0; np < 2; ++np)
		{
			int nAIControlled;
			int nPlayerID;
			int nAP, nWood, nMetal;
			pPlayer->QueryIntAttribute("id", &nPlayerID);
			pPlayer->QueryIntAttribute("ai", &nAIControlled);
			pPlayer->QueryIntAttribute("wood", &nWood);
			pPlayer->QueryIntAttribute("AP", &nAP);
			pPlayer->QueryIntAttribute("metal", &nMetal);
			CPlayer* pplay = CreatePlayer(IntToBool(nAIControlled));

			pplay->SetWood(nWood);
			pplay->SetAP(nAP);
			pplay->SetMetal(nMetal);


			// Load stats!
			TiXmlElement* pStats = pPlayer->FirstChildElement("Stats");

			TiXmlElement* pStat = pStats->FirstChildElement("ChampionDamageDone");
			pStat->QueryIntAttribute("value", &pplay->GetStats()->nChampionDamageDone);
			pStat = pStats->FirstChildElement("ChampionHealingDone");
			pStat->QueryIntAttribute("value", &pplay->GetStats()->nChampionHealingDone);
			pStat = pStats->FirstChildElement("SwordsmanCreated");
			pStat->QueryIntAttribute("value", &pplay->GetStats()->nSwordsmanCreated);
			pStat = pStats->FirstChildElement("ArcherCreated");
			pStat->QueryIntAttribute("value", &pplay->GetStats()->nArcherCreated);
			pStat = pStats->FirstChildElement("CalvaryCreated");
			pStat->QueryIntAttribute("value", &pplay->GetStats()->nCalvaryCreated);
			pStat = pStats->FirstChildElement("SwordsmanDamageDone");
			pStat->QueryIntAttribute("value", &pplay->GetStats()->nSwordsmanDamageDone);
			pStat = pStats->FirstChildElement("ArcherDamageDone");
			pStat->QueryIntAttribute("value", &pplay->GetStats()->nArcherDamageDone);
			pStat = pStats->FirstChildElement("CalvaryDamageDone");
			pStat->QueryIntAttribute("value", &pplay->GetStats()->nCalvaryDamageDone);
			pStat = pStats->FirstChildElement("SwordsmanKilled");
			pStat->QueryIntAttribute("value", &pplay->GetStats()->nSwordsmanKilled);
			pStat = pStats->FirstChildElement("ArcherKilled");
			pStat->QueryIntAttribute("value", &pplay->GetStats()->nArcherKilled);
			pStat = pStats->FirstChildElement("CalvaryKilled");
			pStat->QueryIntAttribute("value", &pplay->GetStats()->nCavalryKilled);
			pStat = pStats->FirstChildElement("WoodEarned");
			pStat->QueryIntAttribute("value", &pplay->GetStats()->nPlayerWoodEarned);
			pStat = pStats->FirstChildElement("WoodSpent");
			pStat->QueryIntAttribute("value", &pplay->GetStats()->nPlayerWoodSpent);
			pStat = pStats->FirstChildElement("MetalEarned");
			pStat->QueryIntAttribute("value", &pplay->GetStats()->nPlayerMetalEarned);
			pStat = pStats->FirstChildElement("MetalSpent");
			pStat->QueryIntAttribute("value", &pplay->GetStats()->nPlayerMetalSpent);
			pStat = pStats->FirstChildElement("APSpent");
			pStat->QueryIntAttribute("value", &pplay->GetStats()->nPlayerAPSpent);



			TiXmlElement* pChampion = pPlayer->FirstChildElement("Champion");

			int nPosX, nPosY, nHealth, nXP, nFacing, nTilesMoved, nHasAttacked;
			// TODO: load in spells here from ability manager
			pChampion->QueryIntAttribute("posX", &nPosX);
			pChampion->QueryIntAttribute("posY", &nPosY);
			pChampion->QueryIntAttribute("health", &nHealth);
			pChampion->QueryIntAttribute("xp", &nXP);
			pChampion->QueryIntAttribute("facing", &nFacing);
			pChampion->QueryIntAttribute("tilesMoved", &nTilesMoved);
			pChampion->QueryIntAttribute("hasAttacked", &nHasAttacked);

			pplay->SetExp(nXP);

			int nNumSpells;
			TiXmlElement* pSpells = pChampion->FirstChildElement("Spells");
			pSpells->QueryIntAttribute("numSpells", &nNumSpells);
			std::vector< std::pair<SPELL_TYPE, int> > spells;
			TiXmlElement* pSpell = pSpells->FirstChildElement("Spell");

			for (int i = 0; i < nNumSpells; ++i)
			{
				int nType;
				int cooldown;
				pSpell->QueryIntAttribute("sType", &nType);
				pSpell->QueryIntAttribute("Cooldown", &cooldown);
				std::pair<SPELL_TYPE, int> tmp((SPELL_TYPE)nType, cooldown);
				spells.push_back(tmp);
				pSpell = pSpell->NextSiblingElement("Spell");
			}

			int nNumBought;
			TiXmlElement* pBoughtSpells = pChampion->FirstChildElement("BoughtSpells");
			pBoughtSpells->QueryIntAttribute("numBought", &nNumBought);
			std::vector<SPELL_TYPE> bought;
			TiXmlElement* pBought = pBoughtSpells->FirstChildElement("Bought");
			
			for( int i = 0; i < nNumBought; i++)
			{
				int type;
				pBought->QueryIntAttribute("Type", &type);
				bought.push_back((SPELL_TYPE)type);
				pBought = pBought->NextSiblingElement("Bought");
			}

			int nNumEffects;
			TiXmlElement* pEffects = pChampion->FirstChildElement("Effects");
			pEffects->QueryIntAttribute("numEffects", &nNumEffects);
			std::vector<SPELL_TYPE> effects;
			TiXmlElement* pEffect = pEffects->FirstChildElement("Effect");
			for (int i = 0; i < nNumEffects; ++i)
			{
				int nType;
				pEffect->QueryIntAttribute("ability", &nType);
				effects.push_back((SPELL_TYPE)nType);
				pEffect = pEffect->NextSiblingElement("Effect");
			}

			CSpawnUnitMessage* pMsg = new CSpawnUnitMessage(spells, effects, bought, Vec2D(nPosX, nPosY), nPlayerID, UT_HERO, nFacing, true, 
				nHealth, nTilesMoved, IntToBool(nHasAttacked));
			CMessageSystem::GetInstance()->SendMessageW(pMsg);


			int nNumUnits;
			TiXmlElement* pUnits = pPlayer->FirstChildElement("Units");
			pUnits->QueryIntAttribute("numUnits", &nNumUnits);

			TiXmlElement* pUnit = pUnits->FirstChildElement("Unit");
			for (int i = 0; i < nNumUnits; ++i)
			{
				int nUnitPosX, nUnitPosY, nUnitType, nUnitHealth, nUnitFacing, nUnitTilesMoved, nUnitHasAttacked;
				pUnit->QueryIntAttribute("posX", &nUnitPosX);
				pUnit->QueryIntAttribute("posY", &nUnitPosY);
				pUnit->QueryIntAttribute("unitType", &nUnitType);
				pUnit->QueryIntAttribute("health", &nUnitHealth);
				pUnit->QueryIntAttribute("facing", &nUnitFacing);
				pUnit->QueryIntAttribute("tilesMoved", &nUnitTilesMoved);
				pUnit->QueryIntAttribute("hasAttacked", &nUnitHasAttacked);
				std::vector<std::pair<SPELL_TYPE, int>> spells;
				int nNumEffects;
				TiXmlElement* pEffects = pUnit->FirstChildElement("Effects");
				pEffects->QueryIntAttribute("numEffects", &nNumEffects);
				std::vector<SPELL_TYPE> effects;
				TiXmlElement* pEffect = pEffects->FirstChildElement("Effect");
				for (int i = 0; i < nNumEffects; ++i)
				{
					int nType;
					pEffect->QueryIntAttribute("ability", &nType);
					effects.push_back((SPELL_TYPE)nType);
					pEffect = pEffect->NextSiblingElement("Effect");
				}
				CSpawnUnitMessage* pUnitMsg = 
					new CSpawnUnitMessage(spells, effects, Vec2D(nUnitPosX, nUnitPosY), nPlayerID, (UNIT_TYPE)nUnitType, nUnitFacing, true, nUnitHealth,
						nUnitTilesMoved,IntToBool(nUnitHasAttacked) );
				CMessageSystem::GetInstance()->SendMessageW(pUnitMsg);

				pUnit = pUnit->NextSiblingElement("Unit");
			}
			pPlayer = pPlayer->NextSiblingElement("Player");
		}


		SetCurrentPlayer(nCurrPlayer);
		if (nCurrPlayer == 0)
			SetNextPlayer(1);
		else
			SetNextPlayer(0);
		SetPhaseCount(nPhaseCount);
		SetCurrentPhase((GAME_PHASE)nCurrPhase);
		CMessageSystem::GetInstance()->ProcessMessages();

		m_bLoadingFromSave = true;
	}
}