#pragma once

// teamAI.h

#ifndef CIV4_TEAM_AI_H
#define CIV4_TEAM_AI_H

#include "CvTeam.h"

class CvTeamAI : public CvTeam
{

public:

	CvTeamAI();
	virtual ~CvTeamAI();

#ifdef _USRDLL
	// inlined for performance reasons, only in the dll
	static CvTeamAI& getTeam(TeamTypes eTeam)
	{
		FAssertMsg(eTeam != NO_TEAM, "eTeam is not assigned a valid value");
		FAssertMsg(eTeam < MAX_TEAMS, "eTeam is not assigned a valid value");
		return m_aTeams[eTeam]; 
	}
#endif
	DllExport static CvTeamAI& getTeamNonInl(TeamTypes eTeam);

	static void initStatics();
	static void freeStatics();

	void AI_init();
	void AI_uninit();
	void AI_reset(bool bConstructor);

	void AI_doTurnPre();
	void AI_doTurnPost();

	void AI_makeAssignWorkDirty();

/********************************************************************************/
/* 	BETTER_BTS_AI_MOD						10/6/08				jdog5000	    */
/* 																			    */
/* 																			    */
/********************************************************************************/
	int AI_getOurPlotStrength(CvPlot* pPlot, int iRange, bool bDefensiveBonuses, bool bTestMoves, bool bIncludeVassals = false);
/********************************************************************************/
/* 	BETTER_BTS_AI_MOD						END								    */
/********************************************************************************/

	void AI_updateAreaStragies(bool bTargets = true);
	void AI_updateAreaTargets();

	int AI_countFinancialTrouble() const;
	int AI_countMilitaryWeight(CvArea* pArea) const;

	bool AI_deduceCitySite(const CvCity* pCity) const; // K-Mod

	bool AI_isAnyCapitalAreaAlone() const;
	bool AI_isPrimaryArea(CvArea* pArea) const;
	bool AI_hasCitiesInPrimaryArea(TeamTypes eTeam) const;
	bool AI_hasSharedPrimaryArea(TeamTypes eTeam) const; // K-Mod
	AreaAITypes AI_calculateAreaAIType(CvArea* pArea, bool bPreparingTotal = false) const;

	int AI_calculateAdjacentLandPlots(TeamTypes eTeam) const;
	int AI_calculateCapitalProximity(TeamTypes eTeam) const;
	int AI_calculatePlotWarValue(TeamTypes eTeam) const;
/************************************************************************************************/
/* BETTER_BTS_AI_MOD                      07/10/08                                jdog5000      */
/*                                                                                              */
/* General AI                                                                                   */
/************************************************************************************************/
	int AI_calculateBonusWarValue(TeamTypes eTeam) const;
/************************************************************************************************/
/* BETTER_BTS_AI_MOD                       END                                                  */
/************************************************************************************************/

	bool AI_haveSeenCities(TeamTypes eTeam, bool bPrimaryAreaOnly = false, int iMinimum = 1) const; // K-Mod
	bool AI_isWarPossible() const;
/************************************************************************************************/
/* BETTER_BTS_AI_MOD                      06/12/10                         Fuyu & jdog5000      */
/*                                                                                              */
/* War Strategy AI                                                                              */
/************************************************************************************************/
	bool AI_isLandTarget(TeamTypes eTeam, bool bNeighborsOnly = false) const;
/************************************************************************************************/
/* BETTER_BTS_AI_MOD                       END                                                  */
/************************************************************************************************/
	bool AI_isAllyLandTarget(TeamTypes eTeam) const;
	bool AI_shareWar(TeamTypes eTeam) const;

	AttitudeTypes AI_getAttitude(TeamTypes eTeam, bool bForced = true) const;
	int AI_getAttitudeVal(TeamTypes eTeam, bool bForced = true) const;
	int AI_getMemoryCount(TeamTypes eTeam, MemoryTypes eMemory) const;

	int AI_chooseElection(const VoteSelectionData& kVoteSelectionData) const;

	int AI_startWarVal(TeamTypes eTeam) const;
	int AI_endWarVal(TeamTypes eTeam) const;
/********************************************************************************/
/**		REVOLUTION_MOD							6/9/08				jdog5000	*/
/**																				*/
/**		Revolution AI															*/
/********************************************************************************/
	int AI_minorKeepWarVal(TeamTypes eTeam) const;
	int AI_getBarbarianCivWarVal(TeamTypes eTeam, int iMaxDistance = 10) const;
/********************************************************************************/
/**		REVOLUTION_MOD							END								*/
/********************************************************************************/

	int AI_techTradeVal(TechTypes eTech, TeamTypes eTeam) const;
	DenialTypes AI_techTrade(TechTypes eTech, TeamTypes eTeam) const;

	int AI_mapTradeVal(TeamTypes eTeam) const;
	DenialTypes AI_mapTrade(TeamTypes eTeam) const;

	int AI_vassalTradeVal(TeamTypes eTeam) const;
	DenialTypes AI_vassalTrade(TeamTypes eTeam) const;

	int AI_surrenderTradeVal(TeamTypes eTeam) const;
	DenialTypes AI_surrenderTrade(TeamTypes eTeam, int iPowerMultiplier = 100) const;

/************************************************************************************************/
/* BETTER_BTS_AI_MOD                      03/20/10                                jdog5000      */
/*                                                                                              */
/* Victory Strategy AI                                                                          */
/************************************************************************************************/
	bool AI_isAnyMemberDoVictoryStrategy( int iVictoryStrategy ) const;
	bool AI_isAnyMemberDoVictoryStrategyLevel4() const;
	bool AI_isAnyMemberDoVictoryStrategyLevel3() const;
	int AI_countMembersWithStrategy(int iStrategy) const; // K-Mod
/************************************************************************************************/
/* BETTER_BTS_AI_MOD                       END                                                  */
/************************************************************************************************/

	int AI_getWarSuccessRating() const; // K-Mod

/************************************************************************************************/
/* BETTER_BTS_AI_MOD - War Strategy AI    05/14/10                                jdog5000      */
/************************************************************************************************/
	int AI_getEnemyPowerPercent( bool bConsiderOthers = false ) const;
	int AI_getRivalAirPower( ) const;
	bool AI_acceptSurrender( TeamTypes eSurrenderTeam );
	bool AI_isOkayVassalTarget( TeamTypes eTeam );

	void AI_getWarRands( int &iMaxWarRand, int &iLimitedWarRand, int &iDogpileWarRand ) const;
	void AI_getWarThresholds( int &iMaxWarThreshold, int &iLimitedWarThreshold, int &iDogpileWarThreshold ) const;
	int AI_getTotalWarOddsTimes100( ) const;
/************************************************************************************************/
/* BETTER_BTS_AI_MOD                       END                                                  */
/************************************************************************************************/

	int AI_makePeaceTradeVal(TeamTypes ePeaceTeam, TeamTypes eTeam) const;
	DenialTypes AI_makePeaceTrade(TeamTypes ePeaceTeam, TeamTypes eTeam) const;

	int AI_declareWarTradeVal(TeamTypes eWarTeam, TeamTypes eTeam) const;
	DenialTypes AI_declareWarTrade(TeamTypes eWarTeam, TeamTypes eTeam, bool bConsiderPower = true) const;

	int AI_openBordersTradeVal(TeamTypes eTeam) const;
	DenialTypes AI_openBordersTrade(TeamTypes eTeam) const;

	int AI_defensivePactTradeVal(TeamTypes eTeam) const;
	DenialTypes AI_defensivePactTrade(TeamTypes eTeam) const;

	DenialTypes AI_permanentAllianceTrade(TeamTypes eTeam) const;

	TeamTypes AI_getWorstEnemy() const;
	void AI_updateWorstEnemy();

/************************************************************************************************/
/* Advanced Diplomacy                   START                                                   */
/************************************************************************************************/
	TeamTypes AI_getEnemy() const;
	void AI_updateEnemy();
	
	TeamTypes AI_getClosestAlly() const;
	void AI_updateClosestAlly();
	
	TeamTypes AI_getFriend() const;
	void AI_updateFriend();
/************************************************************************************************/
/* Advanced Diplomacy                   END                                                   */
/************************************************************************************************/
	
	int AI_getWarPlanStateCounter(TeamTypes eIndex) const;
	void AI_setWarPlanStateCounter(TeamTypes eIndex, int iNewValue);
	void AI_changeWarPlanStateCounter(TeamTypes eIndex, int iChange);

	int AI_getAtWarCounter(TeamTypes eIndex) const;
	void AI_setAtWarCounter(TeamTypes eIndex, int iNewValue);
	void AI_changeAtWarCounter(TeamTypes eIndex, int iChange);

	int AI_getAtPeaceCounter(TeamTypes eIndex) const;
	void AI_setAtPeaceCounter(TeamTypes eIndex, int iNewValue);
	void AI_changeAtPeaceCounter(TeamTypes eIndex, int iChange);

	int AI_getHasMetCounter(TeamTypes eIndex) const;
	void AI_setHasMetCounter(TeamTypes eIndex, int iNewValue);
	void AI_changeHasMetCounter(TeamTypes eIndex, int iChange);

	int AI_getOpenBordersCounter(TeamTypes eIndex) const;
	void AI_setOpenBordersCounter(TeamTypes eIndex, int iNewValue);
	void AI_changeOpenBordersCounter(TeamTypes eIndex, int iChange);

	int AI_getDefensivePactCounter(TeamTypes eIndex) const;
	void AI_setDefensivePactCounter(TeamTypes eIndex, int iNewValue);
	void AI_changeDefensivePactCounter(TeamTypes eIndex, int iChange);

	int AI_getShareWarCounter(TeamTypes eIndex) const;
	void AI_setShareWarCounter(TeamTypes eIndex, int iNewValue);
	void AI_changeShareWarCounter(TeamTypes eIndex, int iChange);

	int AI_getWarSuccess(TeamTypes eIndex) const;
	void AI_setWarSuccess(TeamTypes eIndex, int iNewValue);
	void AI_changeWarSuccess(TeamTypes eIndex, int iChange);

	int AI_getEnemyPeacetimeTradeValue(TeamTypes eIndex) const;
	void AI_setEnemyPeacetimeTradeValue(TeamTypes eIndex, int iNewValue);
	void AI_changeEnemyPeacetimeTradeValue(TeamTypes eIndex, int iChange);

	int AI_getEnemyPeacetimeGrantValue(TeamTypes eIndex) const;
	void AI_setEnemyPeacetimeGrantValue(TeamTypes eIndex, int iNewValue);
	void AI_changeEnemyPeacetimeGrantValue(TeamTypes eIndex, int iChange);

	WarPlanTypes AI_getWarPlan(TeamTypes eIndex) const;
	bool AI_isChosenWar(TeamTypes eIndex) const;
	bool AI_isSneakAttackPreparing(TeamTypes eIndex) const;
	bool AI_isSneakAttackReady(TeamTypes eIndex) const;
	void AI_setWarPlan(TeamTypes eIndex, WarPlanTypes eNewValue, bool bWar = true);

/************************************************************************************************/
/* Advanced Diplomacy                   START                                                   */
/************************************************************************************************/
	int AI_getLimitedBordersCounter(TeamTypes eIndex) const;
	void AI_setLimitedBordersCounter(TeamTypes eIndex, int iNewValue);
	void AI_changeLimitedBordersCounter(TeamTypes eIndex, int iChange);

	int AI_getEmbassyCounter(TeamTypes eIndex) const;
	void AI_setEmbassyCounter(TeamTypes eIndex, int iNewValue);
	void AI_changeEmbassyCounter(TeamTypes eIndex, int iChange);

	int AI_getFreeTradeAgreementCounter(TeamTypes eIndex) const;
	void AI_setFreeTradeAgreementCounter(TeamTypes eIndex, int iNewValue);
	void AI_changeFreeTradeAgreementCounter(TeamTypes eIndex, int iChange);

	int AI_getNonAggressionCounter(TeamTypes eIndex) const;
	void AI_setNonAggressionCounter(TeamTypes eIndex, int iNewValue);
	void AI_changeNonAggressionCounter(TeamTypes eIndex, int iChange);

	int AI_getPOWCounter(TeamTypes eIndex) const;
	void AI_setPOWCounter(TeamTypes eIndex, int iNewValue);
	void AI_changePOWCounter(TeamTypes eIndex, int iChange);
/************************************************************************************************/
/* Advanced Diplomacy                   END                                                   */
/************************************************************************************************/

	int AI_teamCloseness(TeamTypes eIndex, int iMaxDistance = -1) const;
	
	bool AI_performNoWarRolls(TeamTypes eTeam);
	
	int AI_getAttitudeWeight(TeamTypes eTeam) const;
	
	int AI_getLowestVictoryCountdown() const;

	int AI_getTechMonopolyValue(TechTypes eTech, TeamTypes eTeam) const;
	
	bool AI_isWaterAreaRelevant(CvArea* pArea);
	
	virtual void read(FDataStreamBase* pStream);
	virtual void write(FDataStreamBase* pStream);
/************************************************************************************************/
/* Afforess	                  Start		 07/29/10                                               */
/* Advanced Diplomacy                                                                           */
/************************************************************************************************/
	int AI_LimitedBordersTradeVal(TeamTypes eTeam) const;
	DenialTypes AI_LimitedBordersTrade(TeamTypes eTeam) const;
	
	int AI_embassyTradeVal(TeamTypes eTeam) const;
	DenialTypes AI_embassyTrade(TeamTypes eTeam) const;

    int AI_contactTradeVal(TeamTypes eContactTeam, TeamTypes eTeam) const;
	DenialTypes AI_contactTrade(TeamTypes eContactTeam, TeamTypes eTeam) const;

	int AI_FreeTradeAgreementVal(TeamTypes eTeam) const;
	DenialTypes AI_FreeTradeAgreement(TeamTypes eTeam) const;

	int AI_NonAggressionTradeVal(TeamTypes eTeam) const;
	DenialTypes AI_NonAggressionTrade(TeamTypes eTeam) const;
	
	int AI_POWTradeVal(TeamTypes eTeam) const;
	DenialTypes AI_POWTrade(TeamTypes eTeam) const;
/************************************************************************************************/
/* Advanced Diplomacy         END                                                               */
/************************************************************************************************/
protected:

	static CvTeamAI* m_aTeams;

	TeamTypes m_eWorstEnemy;
/************************************************************************************************/
/* Advanced Diplomacy         START                                                               */
/************************************************************************************************/
	TeamTypes m_eEnemy;
	TeamTypes m_eClosestAlly;
	TeamTypes m_eFriend;
/************************************************************************************************/
/* Advanced Diplomacy         END                                                               */
/************************************************************************************************/	

	int* m_aiWarPlanStateCounter;
	int* m_aiAtWarCounter;
	int* m_aiAtPeaceCounter;
	int* m_aiHasMetCounter;
	int* m_aiOpenBordersCounter;
	int* m_aiDefensivePactCounter;
	int* m_aiShareWarCounter;
	int* m_aiWarSuccess;
	int* m_aiEnemyPeacetimeTradeValue;
	int* m_aiEnemyPeacetimeGrantValue;
/************************************************************************************************/
/* Advanced Diplomacy         START                                                               */
/************************************************************************************************/
	int* m_aiLimitedBordersCounter;
	int* m_aiEmbassyCounter;
	int* m_aiFreeTradeAgreementCounter;
	int* m_aiNonAggressionCounter;
	int* m_aiPOWCounter;
/* Afforess	                  Start		 		                                                */
	int m_iNoTechTradeThreshold;
	int m_iTechTradeKnownPercent;
	int m_iMaxWarRand;
	int m_iMaxWarNearbyPowerRatio;
	int m_iMaxWarDistantPowerRatio;
	int m_iMaxWarMinAdjacentLandPercent;
	int m_iLimitedWarRand;
	int m_iLimitedWarPowerRatio;
	int m_iDogpileWarRand;
	int m_iMakePeaceRand;
/************************************************************************************************/
/* Advanced Diplomacy         END                                                               */
/************************************************************************************************/	
	//edited functions
	WarPlanTypes* m_aeWarPlan;

	int AI_noTechTradeThreshold() const;
	int AI_techTradeKnownPercent() const;
	int AI_maxWarRand() const;
	int AI_maxWarNearbyPowerRatio() const;
	int AI_maxWarDistantPowerRatio() const;
	int AI_maxWarMinAdjacentLandPercent() const;
	int AI_limitedWarRand() const;
	int AI_limitedWarPowerRatio() const;
	int AI_dogpileWarRand() const;
	int AI_makePeaceRand() const;
	int AI_noWarAttitudeProb(AttitudeTypes eAttitude) const;

	void AI_doCounter();
	void AI_doWar();


	// added so under cheat mode we can call protected functions for testing
	friend class CvGameTextMgr;
	friend class CvDLLWidgetData;
};

// helper for accessing static functions
#ifdef _USRDLL
#define GET_TEAM CvTeamAI::getTeam
#else
#define GET_TEAM CvTeamAI::getTeamNonInl
#endif

#endif
