//------------------------------------------------------------------------------
//string GetName();
int CvLuaLeague::lGetName(lua_State* L)
{
	CvLeague* pLeague = GetInstance(L);

	Localization::String strName = pLeague->GetName();
	const CvString sResult = strName.toUTF8();
	lua_pushstring(L, sResult);
	return 1;
}
/// Should the AI look at switching ideology branches?
void CvPolicyAI::DoConsiderIdeologySwitch(CvPlayer* pPlayer)
{
	// Gather basic Ideology info
	int iCurrentHappiness = pPlayer->GetExcessHappiness();
	int iPublicOpinionUnhappiness = pPlayer->GetCulture()->GetPublicOpinionUnhappiness();
	PolicyBranchTypes ePreferredIdeology = pPlayer->GetCulture()->GetPublicOpinionPreferredIdeology();
	PolicyBranchTypes eCurrentIdeology = pPlayer->GetPlayerPolicies()->GetLateGamePolicyTree();
#if !defined(NO_ACHIEVEMENTS)
	PlayerTypes eMostPressure = pPlayer->GetCulture()->GetPublicOpinionBiggestInfluence();
#endif
#if defined(MOD_DIPLOMACY_CIV4_FEATURES)
	if(MOD_DIPLOMACY_CIV4_FEATURES)
	{
		if(GET_TEAM(pPlayer->getTeam()).IsVassalOfSomeone() && pPlayer->GetPlayerPolicies()->GetLateGamePolicyTree() != NO_POLICY_BRANCH_TYPE)
		{
			TeamTypes eMasterTeam = GET_TEAM(pPlayer->getTeam()).GetMaster();
			if(eMasterTeam != NO_TEAM)
			{
				// Loop through all players to see if they're on our team
				for(int iPlayerLoop = 0; iPlayerLoop < MAX_MAJOR_CIVS; iPlayerLoop++)
				{
					PlayerTypes eMaster = (PlayerTypes) iPlayerLoop;

					// Assumes one player per team for master
					if(GET_PLAYER(eMaster).getTeam() == GET_TEAM(eMasterTeam).GetID())
					{
						if(GET_PLAYER(eMaster).GetPlayerPolicies()->GetLateGamePolicyTree() != NO_POLICY_BRANCH_TYPE && GET_PLAYER(eMaster).GetPlayerPolicies()->GetLateGamePolicyTree() != pPlayer->GetPlayerPolicies()->GetLateGamePolicyTree())
						{
							// Cleared all obstacles -- REVOLUTION!
							pPlayer->SetAnarchyNumTurns(GC.getSWITCH_POLICY_BRANCHES_ANARCHY_TURNS());
							pPlayer->GetPlayerPolicies()->DoSwitchIdeologies(GET_PLAYER(eMaster).GetPlayerPolicies()->GetLateGamePolicyTree());	
							Localization::String strSummary = Localization::Lookup("TXT_KEY_ANARCHY_BEGINS_SUMMARY");
							Localization::String strMessage = Localization::Lookup("TXT_KEY_ANARCHY_BEGINS");
							pPlayer->GetNotifications()->Add(NOTIFICATION_GENERIC, strMessage.toUTF8(), strSummary.toUTF8(), pPlayer->GetID(), GC.getSWITCH_POLICY_BRANCHES_ANARCHY_TURNS(), -1);
							return;
						}
					}
				}
			}
		}
	}
#endif	
	// Possible enough that we need to look at this in detail?
	if (iCurrentHappiness <= GC.getSUPER_UNHAPPY_THRESHOLD() && iPublicOpinionUnhappiness >= 10)
	{
		// How much Happiness could we gain from a switch?
		int iHappinessCurrentIdeology = GetBranchBuildingHappiness(pPlayer, eCurrentIdeology);
		int iHappinessPreferredIdeology = GetBranchBuildingHappiness(pPlayer, ePreferredIdeology);

		// Does the switch fight against our clearly preferred victory path?
		bool bDontSwitchFreedom = false;
		bool bDontSwitchOrder = false;
		bool bDontSwitchAutocracy = false;
		int iConquestPriority = pPlayer->GetGrandStrategyAI()->GetConquestPriority();
		int iDiploPriority = pPlayer->GetGrandStrategyAI()->GetUnitedNationsPriority();
		int iTechPriority = pPlayer->GetGrandStrategyAI()->GetSpaceshipPriority();
		int iCulturePriority = pPlayer->GetGrandStrategyAI()->GetCulturePriority();
		int iClearPrefPercent = GC.getIDEOLOGY_PERCENT_CLEAR_VICTORY_PREF();
		if (iConquestPriority > (iDiploPriority   * (100 + iClearPrefPercent) / 100) &&
			iConquestPriority > (iTechPriority    * (100 + iClearPrefPercent) / 100) &&
			iConquestPriority > (iCulturePriority * (100 + iClearPrefPercent) / 100))
		{
			bDontSwitchFreedom = true;
		}
		else if (iDiploPriority > (iConquestPriority * (100 + iClearPrefPercent) / 100) &&
			iDiploPriority > (iTechPriority     * (100 + iClearPrefPercent) / 100) &&
			iDiploPriority > (iCulturePriority  * (100 + iClearPrefPercent) / 100))
		{
			bDontSwitchOrder = true;
		}
		else if (iTechPriority > (iConquestPriority * (100 + iClearPrefPercent) / 100) &&
			iTechPriority > (iDiploPriority    * (100 + iClearPrefPercent) / 100) &&
			iTechPriority > (iCulturePriority  * (100 + iClearPrefPercent) / 100))
		{
			bDontSwitchAutocracy = true;
		}
#if defined(MOD_BALANCE_CORE)
		//Sanity check - would a change to this branch simply make us unhappy in another way? If so, don't do it.
		if(ePreferredIdeology != NO_POLICY_BRANCH_TYPE)
		{
			int iUnhappiness = pPlayer->GetCulture()->ComputeHypotheticalPublicOpinionUnhappiness(ePreferredIdeology);
			if(iUnhappiness >= iPublicOpinionUnhappiness)
			{
				return;
			}
		}
#endif
		int iTotalHappinessImprovement = iPublicOpinionUnhappiness + iHappinessPreferredIdeology - iHappinessCurrentIdeology;
		if (iTotalHappinessImprovement >= 10)
		{
			if (bDontSwitchFreedom && ePreferredIdeology == GC.getPOLICY_BRANCH_FREEDOM())
			{
				return;
			}
			if (bDontSwitchAutocracy && ePreferredIdeology == GC.getPOLICY_BRANCH_AUTOCRACY())
			{
				return;
			}
			if (bDontSwitchOrder && ePreferredIdeology == GC.getPOLICY_BRANCH_ORDER())
			{
				return;
			}
#if defined(MOD_BALANCE_CORE)
			//Final sanity check - are we flip-flopping?
			if(GC.getGame().getGameTurn() - pPlayer->GetCulture()->GetTurnIdeologySwitch() <= 30)
			{
				return;
			}
#endif

			// Cleared all obstacles -- REVOLUTION!
			pPlayer->SetAnarchyNumTurns(GC.getSWITCH_POLICY_BRANCHES_ANARCHY_TURNS());
			pPlayer->GetPlayerPolicies()->DoSwitchIdeologies(ePreferredIdeology);	
#if defined(MOD_BALANCE_CORE)
			Localization::String strSummary = Localization::Lookup("TXT_KEY_ANARCHY_BEGINS_SUMMARY");
			Localization::String strMessage = Localization::Lookup("TXT_KEY_ANARCHY_BEGINS");
			pPlayer->GetNotifications()->Add(NOTIFICATION_GENERIC, strMessage.toUTF8(), strSummary.toUTF8(), pPlayer->GetID(), GC.getSWITCH_POLICY_BRANCHES_ANARCHY_TURNS(), -1);
#endif
#if !defined(NO_ACHIEVEMENTS)
			if (ePreferredIdeology == GC.getPOLICY_BRANCH_FREEDOM() && eCurrentIdeology == GC.getPOLICY_BRANCH_ORDER())
			{
				if (GET_PLAYER(eMostPressure).GetID() == GC.getGame().getActivePlayer())
				{
					gDLL->UnlockAchievement(ACHIEVEMENT_XP2_39);
				}
			}
#endif
		}
	}
}
bool QuietDiplomacy::LeaderDiscussion(CvPlayer* human, CvPlayer* computer, const char* text)
{
    CvAssertMsg(human && computer && text, "Quiet Diplomacy: Assertion error!");
    CvAssertMsg(human->isHuman(), "Quiet Diplomacy: Not a human!");

    // Send a notification.
    CvNotifications* notifications = human->GetNotifications();
    
    if(notifications)
    {
        // Create localized strings.
        // Hardcode some translation strings so DLL can be used alone without XML texts.
        std::string language = Localization::GetCurrentLanguage().GetType();

        std::string message;
        std::string summary;

        if(DoesTextKeyExist("TXT_KEY_QUIETDIPLOMACY_LEADERDISCUSSION_SUMMARY") && 
            DoesTextKeyExist("TXT_KEY_QUIETDIPLOMACY_LEADERDISCUSSION_MESSAGE"))
        {
            // Fetch from the database.
            Localization::String localeSummary = Localization::Lookup("TXT_KEY_QUIETDIPLOMACY_LEADERDISCUSSION_SUMMARY");
            localeSummary << Localization::Lookup(computer->getNameKey());

            Localization::String localeMessage = Localization::Lookup("TXT_KEY_QUIETDIPLOMACY_LEADERDISCUSSION_MESSAGE");
            localeMessage << Localization::Lookup(computer->getNameKey());
            localeMessage << text;

            summary = localeSummary.toUTF8();
            message = localeMessage.toUTF8();
        }
        else
        {
            if(language == "pl_PL")
            {
                // Polish
                Localization::String localeLeader = Localization::Lookup(computer->getNameKey());

                size_t localeLeaderBytes = 0;
                const char* localeLeaderString = localeLeader.toUTF8(localeLeaderBytes, 2);

                summary += "Wiadomo\xc5\x9b\xc4\x87 od ";
                summary.append(localeLeaderString, localeLeaderBytes);

                message += Localization::Lookup(computer->getNameKey()).toUTF8();
                message += ": ";
                message += text;
            }
            else
            {
                // English
                summary += "Message from ";
                summary += Localization::Lookup(computer->getNameKey()).toUTF8();

                message += Localization::Lookup(computer->getNameKey()).toUTF8();
                message += ": ";
                message += text;
            }
        }

        // Get computer's capital.
        int x = -1;
        int y = -1;

        CvCity* computerCapital = computer->getCapitalCity();
        if(computerCapital && computerCapital->isRevealed(human->getTeam(), false))
        {
            x = computerCapital->getX();
            y = computerCapital->getY();
        }

        // Add a notification.
        notifications->Add(NOTIFICATION_PEACE_ACTIVE_PLAYER, message.c_str(), summary.c_str(), x, y, computer->GetID());
    }

    // Inform that we took care of it.
    return true;
}
//------------------------------------------------------------------------------
void CvDllNetMessageHandler::ResponsePlayerDealFinalized(PlayerTypes eFromPlayer, PlayerTypes eToPlayer, PlayerTypes eActBy, bool bAccepted)
{
	CvGame& game = GC.getGame();
	PlayerTypes eActivePlayer = game.getActivePlayer();

	// is the deal valid?
	if(!game.GetGameDeals()->FinalizeDeal(eFromPlayer, eToPlayer, bAccepted))
	{
		Localization::String strMessage;
		Localization::String strSummary = Localization::Lookup("TXT_KEY_DEAL_EXPIRED");

		CvPlayerAI& kToPlayer = GET_PLAYER(eToPlayer);
		CvPlayerAI& kFromPlayer = GET_PLAYER(eFromPlayer);
		CvPlayerAI& kActivePlayer = GET_PLAYER(eActivePlayer);

		strMessage = Localization::Lookup("TXT_KEY_DEAL_EXPIRED_FROM_YOU");
		strMessage << kToPlayer.getNickName();
		kFromPlayer.GetNotifications()->Add(NOTIFICATION_PLAYER_DEAL_RESOLVED, strMessage.toUTF8(), strSummary.toUTF8(), eToPlayer, -1, -1);

		strMessage = Localization::Lookup("TXT_KEY_DEAL_EXPIRED_FROM_THEM");
		strMessage << kFromPlayer.getNickName();
		kToPlayer.GetNotifications()->Add(NOTIFICATION_PLAYER_DEAL_RESOLVED, strMessage.toUTF8(), strSummary.toUTF8(), eFromPlayer, -1, -1);
	}
	else
	{
		CvPlayerAI& kToPlayer = GET_PLAYER(eToPlayer);
		CvPlayerAI& kFromPlayer = GET_PLAYER(eFromPlayer);
		if(bAccepted)
		{
			Localization::String strSummary = Localization::Lookup("TXT_KEY_DEAL_ACCEPTED");
			Localization::String strMessage = Localization::Lookup("TXT_KEY_DEAL_ACCEPTED_BY_THEM");
			strMessage << kToPlayer.getNickName();
			kFromPlayer.GetNotifications()->Add(NOTIFICATION_PLAYER_DEAL_RESOLVED, strMessage.toUTF8(), strSummary.toUTF8(), eToPlayer, -1, -1);

			strSummary = Localization::Lookup("TXT_KEY_DEAL_ACCEPTED");
			strMessage = Localization::Lookup("TXT_KEY_DEAL_ACCEPTED_BY_YOU");
			strMessage << kFromPlayer.getNickName();
			kToPlayer.GetNotifications()->Add(NOTIFICATION_PLAYER_DEAL_RESOLVED, strMessage.toUTF8(), strSummary.toUTF8(), eFromPlayer, -1, -1);
		}
		else
		{
			if(eActBy == eFromPlayer)
			{
				Localization::String strSummary = Localization::Lookup("TXT_KEY_DEAL_WITHDRAWN");
				Localization::String strMessage = Localization::Lookup("TXT_KEY_DEAL_WITHDRAWN_BY_YOU");
				strMessage << kToPlayer.getNickName();
				kFromPlayer.GetNotifications()->Add(NOTIFICATION_PLAYER_DEAL_RESOLVED, strMessage.toUTF8(), strSummary.toUTF8(), eToPlayer, -1, -1);

				strSummary = Localization::Lookup("TXT_KEY_DEAL_WITHDRAWN");
				strMessage = Localization::Lookup("TXT_KEY_DEAL_WITHDRAWN_BY_THEM");
				strMessage << kFromPlayer.getNickName();
				kToPlayer.GetNotifications()->Add(NOTIFICATION_PLAYER_DEAL_RESOLVED, strMessage.toUTF8(), strSummary.toUTF8(), eFromPlayer, -1, -1);
			}
			else
			{
				Localization::String strSummary = Localization::Lookup("TXT_KEY_DEAL_REJECTED");
				Localization::String strMessage = Localization::Lookup("TXT_KEY_DEAL_REJECTED_BY_THEM");
				strMessage << kToPlayer.getNickName();
				kFromPlayer.GetNotifications()->Add(NOTIFICATION_PLAYER_DEAL_RESOLVED, strMessage.toUTF8(), strSummary.toUTF8(), eToPlayer, -1, -1);

				strSummary = Localization::Lookup("TXT_KEY_DEAL_REJECTED");
				strMessage = Localization::Lookup("TXT_KEY_DEAL_REJECTED_BY_YOU");
				strMessage << kFromPlayer.getNickName();
				kToPlayer.GetNotifications()->Add(NOTIFICATION_PLAYER_DEAL_RESOLVED, strMessage.toUTF8(), strSummary.toUTF8(), eFromPlayer, -1, -1);
			}
		}
	}
}
void CvGameTextMgr::setDateStr(CvString& strString, int iGameTurn, bool bSave, CalendarTypes eCalendar, int iStartYear, GameSpeedTypes eSpeed)
{
	CvString strYearBuffer;
	CvString strWeekBuffer;

	setYearStr(strYearBuffer, iGameTurn, bSave, eCalendar, iStartYear, eSpeed);

	const int iNumMonths = DB.Count("Months");
	const int iNumSeasons = DB.Count("Seasons");

	switch (eCalendar)
	{
	case CALENDAR_DEFAULT:
		{
			int iTurnMonth = getTurnMonthForGame(iGameTurn, iStartYear, eCalendar, eSpeed);
			if (0 == (getTurnMonthForGame(iGameTurn + 1, iStartYear, eCalendar, eSpeed) - iTurnMonth) % iNumMonths)
			{
				strString = strYearBuffer;
			}
			else
			{
				int iMonth = iTurnMonth % iNumMonths;

				Database::SingleResult kResult;
				CvBaseInfo kInfo;
				DB.SelectAt(kResult, "Months", iMonth);
				kInfo.CacheResult(kResult);

				Localization::String lstrMonth = Localization::Lookup(kInfo.GetDescription());
				CvString strMonth = lstrMonth.toUTF8();

				if (bSave)
				{
					strString = (strYearBuffer + "-" + strMonth);
				}
				else
				{
					strString = (strMonth + CvString(", ") + strYearBuffer);
				}
			}
		}
		break;
	case CALENDAR_YEARS:
	case CALENDAR_BI_YEARLY:
		strString = strYearBuffer;
		break;

	case CALENDAR_TURNS:
		strString = GetLocalizedText("TXT_KEY_TIME_TURN", (iGameTurn + 1));
		break;

	case CALENDAR_SEASONS:
		{
			CvBaseInfo kSeasonInfo;
			Database::SingleResult kResult;
			DB.SelectAt(kResult, "Seasons", iGameTurn % iNumSeasons);
			kSeasonInfo.CacheResult(kResult);
			const char* Desc = kSeasonInfo.GetDescription();
			if (bSave)
			{
				strString = (strYearBuffer + "-" + CvString(Desc));
			}
			else
			{
				strString = (CvString(Desc) + CvString(", ") + strYearBuffer);
			}
		}

		break;

	case CALENDAR_MONTHS:
		{
			CvBaseInfo kMonthInfo;
			Database::SingleResult kResult;
			DB.SelectAt(kResult, "Months", iGameTurn % iNumMonths);
			kMonthInfo.CacheResult(kResult);

			const char* Descr = kMonthInfo.GetDescription();

			if (bSave)
			{
				strString = (strYearBuffer + "-" + CvString(Descr));
			}
			else
			{
				strString = (CvString(Descr) + CvString(", ") + strYearBuffer);
			}
		}


		break;

	case CALENDAR_WEEKS:
		{
			strWeekBuffer = GetLocalizedText("TXT_KEY_TIME_WEEK", ((iGameTurn % GC.getWEEKS_PER_MONTHS()) + 1));

			const int idx = (iGameTurn / GC.getWEEKS_PER_MONTHS()) % iNumMonths;

			CvBaseInfo kCalendarInfo;
			Database::SingleResult kResult;

			DB.SelectAt(kResult, "Months", idx);
			kCalendarInfo.CacheResult(kResult);

			const char* Descr = kCalendarInfo.GetDescription();


			if (bSave)
			{
				strString = (strYearBuffer + "-" + CvString(Descr) + "-" + strWeekBuffer);
			}
			else
			{
				strString = (strWeekBuffer + ", " + CvString(Descr) + ", " + strYearBuffer);
			}
		}
		break;

	default:
		CvAssert(false);
	}
}