## Sid Meier's Civilization 4
## Copyright Firaxis Games 2005
##
## Implementaion of miscellaneous game functions

import CvUtil
from CvPythonExtensions import *
import CvEventInterface
import CustomFunctions
import ScenarioFunctions

import PyHelpers
PyPlayer = PyHelpers.PyPlayer

# globals
cf = CustomFunctions.CustomFunctions()
gc = CyGlobalContext()
sf = ScenarioFunctions.ScenarioFunctions()



class CvGameUtils:
	"Miscellaneous game functions"
	def __init__(self):
		pass

	def isVictoryTest(self):
		if ( gc.getGame().getElapsedGameTurns() > 10 ):
			return True
		else:
			return False

	def isVictory(self, argsList):
		eVictory = argsList[0]
		return True

	def isPlayerResearch(self, argsList):
		ePlayer = argsList[0]
		return True

	def getExtraCost(self, argsList):
		ePlayer = argsList[0]
		return 0

	def createBarbarianCities(self):
		return False

	def createBarbarianUnits(self):
		return False

	def skipResearchPopup(self,argsList):
		ePlayer = argsList[0]
		return False

	def showTechChooserButton(self,argsList):
		ePlayer = argsList[0]
		return True

	def getFirstRecommendedTech(self,argsList):
		ePlayer = argsList[0]
		return TechTypes.NO_TECH

	def getSecondRecommendedTech(self,argsList):
		ePlayer = argsList[0]
		eFirstTech = argsList[1]
		return TechTypes.NO_TECH

	def canRazeCity(self,argsList):
		iRazingPlayer, pCity = argsList
		return True

	def canDeclareWar(self,argsList):
		iAttackingTeam, iDefendingTeam = argsList
		return True

	def skipProductionPopup(self,argsList):
		pCity = argsList[0]
		return False

	def showExamineCityButton(self,argsList):
		pCity = argsList[0]
		return True

	def getRecommendedUnit(self,argsList):
		pCity = argsList[0]
		return UnitTypes.NO_UNIT

	def getRecommendedBuilding(self,argsList):
		pCity = argsList[0]
		return BuildingTypes.NO_BUILDING

	def updateColoredPlots(self):
		return False

	def isActionRecommended(self,argsList):
		pUnit = argsList[0]
		iAction = argsList[1]
		return False

	def unitCannotMoveInto(self,argsList):
		ePlayer = argsList[0]
		iUnitId = argsList[1]
		iPlotX = argsList[2]
		iPlotY = argsList[3]
		return False

	def cannotHandleAction(self,argsList):
		pPlot = argsList[0]
		iAction = argsList[1]
		bTestVisible = argsList[2]
		return False

	def canBuild(self,argsList):
		iX, iY, iBuild, iPlayer = argsList
		pPlayer = gc.getPlayer(iPlayer)
		eTeam = gc.getTeam(pPlayer.getTeam())
		pPlot = CyMap().plot(iX, iY)

		return -1	# Returning -1 means ignore; 0 means Build cannot be performed; 1 or greater means it can

	def cannotFoundCity(self,argsList):
		iPlayer, iPlotX, iPlotY = argsList
		pPlot = CyMap().plot(iPlotX,iPlotY)
		return False

	def cannotSelectionListMove(self,argsList):
		pPlot = argsList[0]
		bAlt = argsList[1]
		bShift = argsList[2]
		bCtrl = argsList[3]
		return False

	def cannotSelectionListGameNetMessage(self,argsList):
		eMessage = argsList[0]
		iData2 = argsList[1]
		iData3 = argsList[2]
		iData4 = argsList[3]
		iFlags = argsList[4]
		bAlt = argsList[5]
		bShift = argsList[6]
		return False

	def cannotDoControl(self,argsList):
		eControl = argsList[0]
		return False

	def canResearch(self,argsList):
		ePlayer = argsList[0]
		eTech = argsList[1]
		bTrade = argsList[2]
		return False

	def cannotResearch(self,argsList):
		ePlayer = argsList[0]
		eTech = argsList[1]
		bTrade = argsList[2]
		pPlayer = gc.getPlayer(ePlayer)
		iCiv = pPlayer.getCivilizationType()
		eTeam = gc.getTeam(pPlayer.getTeam())

		if gc.getGame().isOption(GameOptionTypes.GAMEOPTION_NO_RELIGION_0):
			if eTech == gc.getInfoTypeForString('TECH_ORDERS_FROM_HEAVEN'):
				return True
		if gc.getGame().isOption(GameOptionTypes.GAMEOPTION_NO_RELIGION_1):
			if eTech == gc.getInfoTypeForString('TECH_HONOR'):
				return True
		if gc.getGame().isOption(GameOptionTypes.GAMEOPTION_NO_RELIGION_2):
			if eTech == gc.getInfoTypeForString('TECH_WAY_OF_THE_EARTHMOTHER'):
				return True
		if gc.getGame().isOption(GameOptionTypes.GAMEOPTION_NO_RELIGION_3):
			if eTech == gc.getInfoTypeForString('TECH_WAY_OF_THE_FORESTS'):
				return True
		if gc.getGame().isOption(GameOptionTypes.GAMEOPTION_NO_RELIGION_4):
			if eTech == gc.getInfoTypeForString('TECH_MESSAGE_FROM_THE_DEEP'):
				return True
		if gc.getGame().isOption(GameOptionTypes.GAMEOPTION_NO_RELIGION_5):
			if eTech == gc.getInfoTypeForString('TECH_DECEPTION'):
				return True
		if gc.getGame().isOption(GameOptionTypes.GAMEOPTION_NO_RELIGION_6):
			if eTech == gc.getInfoTypeForString('TECH_CORRUPTION_OF_SPIRIT'):
				return True


		if CyGame().getWBMapScript():
			bBlock = sf.cannotResearch(ePlayer, eTech, bTrade)
			if bBlock:
				return True

		return False

	def canDoCivic(self,argsList):
		ePlayer = argsList[0]
		eCivic = argsList[1]
		return False

	def cannotDoCivic(self,argsList):
		ePlayer = argsList[0]
		eCivic = argsList[1]
		pPlayer = gc.getPlayer(ePlayer)
		eTeam = gc.getTeam(pPlayer.getTeam())

		if eCivic == gc.getInfoTypeForString('CIVIC_NO_MEMBERSHIP'):
			if CyGame().getWBMapScript():
				bBlock = sf.cannotDoCivic(ePlayer, eCivic)
				if bBlock:
					return True
		if eCivic == gc.getInfoTypeForString('CIVIC_UNDERCOUNCIL'):
			if pPlayer.getStateReligion() == gc.getInfoTypeForString('RELIGION_THE_EMPYREAN'):
				return True
			if CyGame().getWBMapScript():
				bBlock = sf.cannotDoCivic(ePlayer, eCivic)
				if bBlock:
					return True
			elif not CyGame().isReligionFounded(gc.getInfoTypeForString('RELIGION_COUNCIL_OF_ESUS')):
				return True
		if eCivic == gc.getInfoTypeForString('CIVIC_OVERCOUNCIL'):
			if pPlayer.getStateReligion() == gc.getInfoTypeForString('RELIGION_COUNCIL_OF_ESUS'):
				return True
			if CyGame().getWBMapScript():
				bBlock = sf.cannotDoCivic(ePlayer, eCivic)
				if bBlock:
					return True
			elif not CyGame().isReligionFounded(gc.getInfoTypeForString('RELIGION_THE_EMPYREAN')):
				return True


		return False

	def canTrain(self,argsList):
		pCity = argsList[0]
		eUnit = argsList[1]
		bContinue = argsList[2]
		bTestVisible = argsList[3]
		bIgnoreCost = argsList[4]
		bIgnoreUpgrades = argsList[5]
		return False

	def cannotTrain(self,argsList):
		pCity = argsList[0]
		eUnit = argsList[1]
		bContinue = argsList[2]
		bTestVisible = argsList[3]
		bIgnoreCost = argsList[4]
		bIgnoreUpgrades = argsList[5]
		ePlayer = pCity.getOwner()
		pPlayer = gc.getPlayer(ePlayer)
		eUnitClass = gc.getUnitInfo(eUnit).getUnitClassType()
		eTeam = gc.getTeam(pPlayer.getTeam())


		if not pPlayer.isHuman():
			if gc.getUnitInfo(eUnit).getTier() == 1:
				if pPlayer.getUnitClassCount(eUnitClass) > 3 * pPlayer.getNumCities():
					return True

		if eUnitClass == gc.getInfoTypeForString('UNITCLASS_NIGHTWATCH'):
			if pPlayer.getStateReligion() == gc.getInfoTypeForString('RELIGION_COUNCIL_OF_ESUS'):
				return False
			if CyGame().isVotePassed(gc.getInfoTypeForString('VOTE_ENLIST_THE_NIGHTWATCH')):
				if pPlayer.isFullMember(gc.getInfoTypeForString('DIPLOVOTE_UNDERCOUNCIL')):
					return False
			return True

		if eUnitClass == gc.getInfoTypeForString('UNITCLASS_RADIANT_GUARD'):
			if pPlayer.getStateReligion() == gc.getInfoTypeForString('RELIGION_THE_EMPYREAN'):
				return False
			if CyGame().isVotePassed(gc.getInfoTypeForString('VOTE_ENLIST_THE_GUARD')):
				if pPlayer.isFullMember(gc.getInfoTypeForString('DIPLOVOTE_OVERCOUNCIL')):
					return False
			return True


		if eUnitClass == gc.getInfoTypeForString('UNITCLASS_HYBOREM') and not pPlayer.getLeaderType() == gc.getInfoTypeForString('LEADER_HYBOREM'):
			return True
		if eUnitClass == gc.getInfoTypeForString('UNITCLASS_LETHE') and not pPlayer.getLeaderType() == gc.getInfoTypeForString('LEADER_LETHE'):
			return True
		if eUnitClass == gc.getInfoTypeForString('UNITCLASS_JUDECCA') and not pPlayer.getLeaderType() == gc.getInfoTypeForString('LEADER_JUDECCA'):
			return True
		if eUnitClass == gc.getInfoTypeForString('UNITCLASS_OUZZA') and not pPlayer.getLeaderType() == gc.getInfoTypeForString('LEADER_OUZZA'):
			return True
		if eUnitClass == gc.getInfoTypeForString('UNITCLASS_MERESIN') and not pPlayer.getLeaderType() == gc.getInfoTypeForString('LEADER_MERESIN'):
			return True
		if eUnitClass == gc.getInfoTypeForString('UNITCLASS_SALLOS') and not pPlayer.getLeaderType() == gc.getInfoTypeForString('LEADER_SALLOS'):
			return True
		if eUnitClass == gc.getInfoTypeForString('UNITCLASS_STATIUS') and not pPlayer.getLeaderType() == gc.getInfoTypeForString('LEADER_STATIUS'):
			return True
		if eUnit == gc.getInfoTypeForString('UNIT_BEAST_OF_AGARES'):
			if pCity.getPopulation() <= 5:
				return True

		if pPlayer.getCivics(gc.getInfoTypeForString('CIVICOPTION_MEMBERSHIP')) == gc.getInfoTypeForString('CIVIC_CRUSADE'):
			if eUnit == gc.getInfoTypeForString('UNIT_WORKER'):
				return True
			if eUnit == gc.getInfoTypeForString('UNIT_SETTLER'):
				return True
			if eUnit == gc.getInfoTypeForString('UNIT_WORKBOAT'):
				return True

		if eUnit == gc.getInfoTypeForString('UNIT_ACHERON'):
			if gc.getGame().isOption(GameOptionTypes.GAMEOPTION_NO_ACHERON):
				return True

		if eUnit == gc.getInfoTypeForString('UNIT_DISCIPLE_OF_ACHERON') or eUnit == gc.getInfoTypeForString('UNIT_SON_OF_THE_INFERNO') or eUnit == gc.getInfoTypeForString('UNIT_DRAGON_FANATIC'):
			if gc.getGame().isOption(GameOptionTypes.GAMEOPTION_NO_ACHERON):
				return True

		if eUnit == gc.getInfoTypeForString('UNIT_DUIN'):
			if gc.getGame().isOption(GameOptionTypes.GAMEOPTION_NO_DUIN):
				return True

		if eUnit == gc.getInfoTypeForString('UNIT_LUONNOTAR'):
			if gc.getGame().isOption(GameOptionTypes.GAMEOPTION_WB_EXTRA):
				return True

		if CyGame().getWBMapScript():
			bBlock = sf.cannotTrain(pCity, eUnit, bContinue, bTestVisible, bIgnoreCost, bIgnoreUpgrades)
			if bBlock:
				return True


		return False

	def canConstruct(self,argsList):
		pCity = argsList[0]
		eBuilding = argsList[1]
		bContinue = argsList[2]
		bTestVisible = argsList[3]
		bIgnoreCost = argsList[4]
		return False

	def cannotConstruct(self,argsList):
		pCity = argsList[0]
		eBuilding = argsList[1]
		bContinue = argsList[2]
		bTestVisible = argsList[3]
		bIgnoreCost = argsList[4]
		pPlayer = gc.getPlayer(pCity.getOwner())
		iBuildingClass = gc.getBuildingInfo(eBuilding).getBuildingClassType()
		eTeam = gc.getTeam(pPlayer.getTeam())


		if eBuilding == gc.getInfoTypeForString('BUILDING_GRAND_MENAGERIE'):
			if pCity.getNumRealBuilding(gc.getInfoTypeForString('BUILDING_GIANT_CAGE')) + pCity.getNumRealBuilding(gc.getInfoTypeForString('BUILDING_AVIARY')) + pCity.getNumRealBuilding(gc.getInfoTypeForString('BUILDING_LION_CAGE')) + pCity.getNumRealBuilding(gc.getInfoTypeForString('BUILDING_WOLF_PEN')) + pCity.getNumRealBuilding(gc.getInfoTypeForString('BUILDING_TIGER_CAGE')) + pCity.getNumRealBuilding(gc.getInfoTypeForString('BUILDING_PANTHER_CAGE')) + pCity.getNumRealBuilding(gc.getInfoTypeForString('BUILDING_DANCING_BEAR')) + pCity.getNumRealBuilding(gc.getInfoTypeForString('BUILDING_GORILLA_CAGE')) + pCity.getNumRealBuilding(gc.getInfoTypeForString('BUILDING_BABOON_CAGE')) + pCity.getNumRealBuilding(gc.getInfoTypeForString('BUILDING_SPIDER_PEN')) + pCity.getNumRealBuilding(gc.getInfoTypeForString('BUILDING_SCORPION_PEN')) + pCity.getNumRealBuilding(gc.getInfoTypeForString('BUILDING_ELEPHANT_PEN')) + pCity.getNumRealBuilding(gc.getInfoTypeForString('BUILDING_GRIFFON_CAGE')) + pCity.getNumRealBuilding(gc.getInfoTypeForString('BUILDING_WEREWOLF_CAGE')) + pCity.getNumRealBuilding(gc.getInfoTypeForString('BUILDING_HUMAN_CAGE')) + pCity.getNumRealBuilding(gc.getInfoTypeForString('BUILDING_ELF_CAGE')) + pCity.getNumRealBuilding(gc.getInfoTypeForString('BUILDING_ORC_CAGE')) + pCity.getNumRealBuilding(gc.getInfoTypeForString('BUILDING_DWARF_CAGE')) + pCity.getNumRealBuilding(gc.getInfoTypeForString('BUILDING_LAMIA_CAGE')) + pCity.getNumRealBuilding(gc.getInfoTypeForString('BUILDING_CENTAUR_CAGE')) + pCity.getNumRealBuilding(gc.getInfoTypeForString('BUILDING_MUSTEVAL_CAGE')) + pCity.getNumRealBuilding(gc.getInfoTypeForString('BUILDING_FROSTLING_CAGE')) + pCity.getNumRealBuilding(gc.getInfoTypeForString('BUILDING_SATYR_CAGE')) + pCity.getNumRealBuilding(gc.getInfoTypeForString('BUILDING_LIZARDMAN_CAGE')) + pCity.getNumRealBuilding(gc.getInfoTypeForString('BUILDING_TORTOISE_TANK')) + pCity.getNumRealBuilding(gc.getInfoTypeForString('BUILDING_SEA_SERPENT_TANK')) < 10:
				return True

		if eBuilding == gc.getInfoTypeForString('BUILDING_EYES_AND_EARS_NETWORK'):
			if not pPlayer.isFullMember(gc.getInfoTypeForString('DIPLOVOTE_UNDERCOUNCIL')):
				return True


		if pPlayer.getCivics(gc.getInfoTypeForString('CIVICOPTION_MEMBERSHIP')) == gc.getInfoTypeForString('CIVIC_CRUSADE'):
			if eBuilding == gc.getInfoTypeForString('BUILDING_ELDER_COUNCIL'):
				return True
			if eBuilding == gc.getInfoTypeForString('BUILDING_MARKET'):
				return True
			if eBuilding == gc.getInfoTypeForString('BUILDING_MONUMENT'):
				return True
			if eBuilding == gc.getInfoTypeForString('BUILDING_MONEYCHANGER'):
				return True
			if eBuilding == gc.getInfoTypeForString('BUILDING_THEATRE'):
				return True
			if eBuilding == gc.getInfoTypeForString('BUILDING_AQUEDUCT'):
				return True
			if eBuilding == gc.getInfoTypeForString('BUILDING_PUBLIC_BATHS'):
				return True
			if eBuilding == gc.getInfoTypeForString('BUILDING_HERBALIST'):
				return True
			if eBuilding == gc.getInfoTypeForString('BUILDING_CARNIVAL'):
				return True
			if eBuilding == gc.getInfoTypeForString('BUILDING_COURTHOUSE'):
				return True
			if eBuilding == gc.getInfoTypeForString('BUILDING_GAMBLING_HOUSE'):
				return True
			if eBuilding == gc.getInfoTypeForString('BUILDING_GRANARY'):
				return True
			if eBuilding == gc.getInfoTypeForString('BUILDING_SMOKEHOUSE'):
				return True
			if eBuilding == gc.getInfoTypeForString('BUILDING_LIBRARY'):
				return True
			if eBuilding == gc.getInfoTypeForString('BUILDING_HARBOR'):
				return True
			if eBuilding == gc.getInfoTypeForString('BUILDING_ALCHEMY_LAB'):
				return True
			if eBuilding == gc.getInfoTypeForString('BUILDING_BREWERY'):
				return True

		if eBuilding == gc.getInfoTypeForString('BUILDING_SHRINE_OF_THE_CHAMPION'):
			iHero = cf.getHero(pPlayer)
			if iHero == -1:
				return True
			if CyGame().isUnitClassMaxedOut(iHero, 0) == False:
				return True
			for iPlayer2 in range(gc.getMAX_PLAYERS()):
				pPlayer2 = gc.getPlayer(iPlayer2)
				if not pPlayer2.isBarbarian():
					if pPlayer2.getUnitClassCount(iHero) > 0:
						return True



		if eBuilding == gc.getInfoTypeForString('BUILDING_WYRMHOLD_ACHERON'):
			if not (pPlayer.getCivilizationType() == gc.getInfoTypeForString('CIVILIZATION_BARBARIAN') or pPlayer.getUnitClassCount(gc.getInfoTypeForString('UNITCLASS_ACHERON')) >0):
				return True
		if eBuilding == gc.getInfoTypeForString('BUILDING_WYRMHOLD_EURABATRES'):
			if not (pPlayer.getCivilizationType() == gc.getInfoTypeForString('CIVILIZATION_KURIOTATES') or pPlayer.getUnitClassCount(gc.getInfoTypeForString('UNITCLASS_EURABATRES')) >0):
				return True
		if eBuilding == gc.getInfoTypeForString('BUILDING_WYRMHOLD_ABASHI'):
			if not (pPlayer.getCivilizationType() == gc.getInfoTypeForString('CIVILIZATION_SHEAIM') or pPlayer.getUnitClassCount(gc.getInfoTypeForString('UNITCLASS_ABASHI')) >0):
				return True
		if eBuilding == gc.getInfoTypeForString('BUILDING_WYRMHOLD_DRIFA'):
			if not (pPlayer.getCivilizationType() == gc.getInfoTypeForString('CIVILIZATION_ILLIANS') or pPlayer.getUnitClassCount(gc.getInfoTypeForString('UNITCLASS_DRIFA')) >0):
				return True
		if eBuilding == gc.getInfoTypeForString('BUILDING_WYRMHOLD_THALATTH'):
			if not (pPlayer.getStateReligion() == gc.getInfoTypeForString('RELIGION_OCTOPUS_OVERLORDS') or pPlayer.getUnitClassCount(gc.getInfoTypeForString('UNITCLASS_THALATTH')) >0):
				return True




		if eBuilding == gc.getInfoTypeForString('BUILDING_MERCURIAN_GATE'):
			if gc.getGame().isOption(GameOptionTypes.GAMEOPTION_NO_HYBOREM_OR_BASIUM):
				return True
			if pPlayer.getStateReligion() == gc.getInfoTypeForString('RELIGION_THE_ASHEN_VEIL'):
				return True
			if not pPlayer.isHuman():
				if pPlayer.getAlignment() == gc.getInfoTypeForString('ALIGNMENT_EVIL'):
					return True
			iBasium = cf.getLeader(gc.getInfoTypeForString('LEADER_BASIUM'))
			if iBasium != -1:
				pBasium = gc.getPlayer(iBasium)
				if pBasium.getUnitClassCount(gc.getInfoTypeForString('UNITCLASS_BASIUM')) > 0:
					return True


		iAltar1 = gc.getInfoTypeForString('BUILDING_ALTAR_OF_THE_LUONNOTAR')
		iAltar2 = gc.getInfoTypeForString('BUILDING_ALTAR_OF_THE_LUONNOTAR_ANOINTED')
		iAltar3 = gc.getInfoTypeForString('BUILDING_ALTAR_OF_THE_LUONNOTAR_BLESSED')
		iAltar4 = gc.getInfoTypeForString('BUILDING_ALTAR_OF_THE_LUONNOTAR_CONSECRATED')
		iAltar5 = gc.getInfoTypeForString('BUILDING_ALTAR_OF_THE_LUONNOTAR_DIVINE')
		iAltar6 = gc.getInfoTypeForString('BUILDING_ALTAR_OF_THE_LUONNOTAR_EXALTED')
		iAltar7 = gc.getInfoTypeForString('BUILDING_ALTAR_OF_THE_LUONNOTAR_FINAL')
		if (eBuilding == iAltar1 or eBuilding == iAltar2 or eBuilding == iAltar3 or eBuilding == iAltar4 or eBuilding == iAltar5 or eBuilding == iAltar6 or eBuilding == iAltar7):
			if pPlayer.getAlignment() == gc.getInfoTypeForString('ALIGNMENT_EVIL'):
				return True
			if eBuilding == iAltar1:
				if (pPlayer.countNumBuildings(iAltar2) > 0 or pPlayer.countNumBuildings(iAltar3) > 0 or pPlayer.countNumBuildings(iAltar4) > 0 or pPlayer.countNumBuildings(iAltar5) > 0 or pPlayer.countNumBuildings(iAltar6) > 0 or pPlayer.countNumBuildings(iAltar7) > 0):
					return True
			if eBuilding == iAltar2:
				if (pPlayer.countNumBuildings(iAltar3) > 0 or pPlayer.countNumBuildings(iAltar4) > 0 or pPlayer.countNumBuildings(iAltar5) > 0 or pPlayer.countNumBuildings(iAltar6) > 0 or pPlayer.countNumBuildings(iAltar7) > 0):
					return True
			if eBuilding == iAltar3:
				if (pPlayer.countNumBuildings(iAltar4) > 0 or pPlayer.countNumBuildings(iAltar5) > 0 or pPlayer.countNumBuildings(iAltar6) > 0 or pPlayer.countNumBuildings(iAltar7) > 0):
					return True
			if eBuilding == iAltar4:
				if (pPlayer.countNumBuildings(iAltar5) > 0 or pPlayer.countNumBuildings(iAltar6) > 0 or pPlayer.countNumBuildings(iAltar7) > 0):
					return True
			if eBuilding == iAltar5:
				if (pPlayer.countNumBuildings(iAltar6) > 0 or pPlayer.countNumBuildings(iAltar7) > 0):
					return True
			if eBuilding == iAltar6:
				if pPlayer.countNumBuildings(iAltar7) > 0:
					return True

### Start AI restrictions ###
		if pPlayer.isHuman() == False:
			if eBuilding == gc.getInfoTypeForString('BUILDING_PROPHECY_OF_RAGNAROK'):
				if pPlayer.getAlignment() != gc.getInfoTypeForString('ALIGNMENT_EVIL'):
					return True


### End AI restrictions ###

		if eBuilding == gc.getInfoTypeForString('BUILDING_SMUGGLERS_PORT'):
			if pPlayer.isSmugglingRing() == False:
				return True

		return False

	def canCreate(self,argsList):
		pCity = argsList[0]
		eProject = argsList[1]
		bContinue = argsList[2]
		bTestVisible = argsList[3]
		return False

	def cannotCreate(self,argsList):
		pCity = argsList[0]
		eProject = argsList[1]
		bContinue = argsList[2]
		bTestVisible = argsList[3]
		pPlayer = gc.getPlayer(pCity.getOwner())
		eTeam = gc.getTeam(pPlayer.getTeam())
		iCiv=pPlayer.getCivilizationType()

		iTeam = gc.getTeam(pPlayer.getTeam())

		if eProject == gc.getInfoTypeForString('PROJECT_STIR_FROM_SLUMBER'):
			if not pCity.isHasReligion(gc.getInfoTypeForString('RELIGION_CULT_OF_THE_DRAGON')):
				return True


		if eProject == gc.getInfoTypeForString('PROJECT_INFERNAL_PACT'):
			if iCiv == gc.getInfoTypeForString('CIVILIZATION_INFERNAL'):
				return True
			if gc.getGame().isOption(GameOptionTypes.GAMEOPTION_NO_HYBOREM_OR_BASIUM):
				return True
			if not pCity.isHasReligion(gc.getInfoTypeForString('RELIGION_THE_ASHEN_VEIL')):
				return True
			if not pPlayer.isHuman():
				if pPlayer.getStateReligion() != gc.getInfoTypeForString('RELIGION_THE_ASHEN_VEIL'):
					return True

			if not CyGame().isLeaderEverActive(gc.getInfoTypeForString('LEADER_HYBOREM')):
				return False
			if not CyGame().isLeaderEverActive(gc.getInfoTypeForString('LEADER_JUDECCA')):
				return False
			if not CyGame().isLeaderEverActive(gc.getInfoTypeForString('LEADER_SALLOS')):
				return False
			if not CyGame().isLeaderEverActive(gc.getInfoTypeForString('LEADER_OUZZA')):
				return False
			if not CyGame().isLeaderEverActive(gc.getInfoTypeForString('LEADER_MERESIN')):
				return False
			if not CyGame().isLeaderEverActive(gc.getInfoTypeForString('LEADER_STATIUS')):
				return False
			if not CyGame().isLeaderEverActive(gc.getInfoTypeForString('LEADER_LETHE')):
				return False
			return True




		if eProject == gc.getInfoTypeForString('PROJECT_PURGE_THE_UNFAITHFUL'):
			if pPlayer.isHuman() == False:
				return True
			if pPlayer.getStateReligion() == -1:
				return True

		if eProject == gc.getInfoTypeForString('PROJECT_BIRTHRIGHT_REGAINED'):
			if not pPlayer.isFeatAccomplished(FeatTypes.FEAT_GLOBAL_SPELL):
				return True



		if eProject == gc.getInfoTypeForString('PROJECT_THE_WHITE_HAND'):
			if iCiv == gc.getInfoTypeForString('CIVILIZATION_ILLIANS') or iCiv == gc.getInfoTypeForString('CIVILIZATION_DOVIELLO'):
				iAuricPlayer = cf.getLeader(gc.getInfoTypeForString('LEADER_AURIC'))
				if iAuricPlayer != -1:
					return False
			return True
		if eProject == gc.getInfoTypeForString('PROJECT_THE_DEEPENING'):
			if not pPlayer.getStateReligion() == gc.getInfoTypeForString('RELIGION_WHITE_HAND'):
				return True
		if eProject == gc.getInfoTypeForString('PROJECT_ASCENSION') or eProject == gc.getInfoTypeForString('PROJECT_THE_DRAW'):
			if not pPlayer.getStateReligion() == gc.getInfoTypeForString('RELIGION_WHITE_HAND'):
				return True
			if not CyGame().isUnitClassMaxedOut(gc.getInfoTypeForString('UNITCLASS_AURIC'), 0):
				return True
			iAuricPlayer = cf.getLeader(gc.getInfoTypeForString('LEADER_AURIC'))
			if iAuricPlayer != -1:
				pAuricPlayer = gc.getPlayer(iAuricPlayer)
				if pAuricPlayer.getUnitClassCount(gc.getInfoTypeForString('UNITCLASS_AURIC')) >0:
					return False
			return True




		if eProject == gc.getInfoTypeForString('PROJECT_SAMHAIN'):
			if pPlayer.isHuman() == False:
				if pPlayer.getNumCities() <= 3:
					return True

		return False

	def canMaintain(self,argsList):
		pCity = argsList[0]
		eProcess = argsList[1]
		bContinue = argsList[2]
		return False

	def cannotMaintain(self,argsList):
		pCity = argsList[0]
		eProcess = argsList[1]
		bContinue = argsList[2]
		return False

	def AI_chooseTech(self,argsList):
		ePlayer = argsList[0]
		bFree = argsList[1]
		pPlayer = gc.getPlayer(ePlayer)

		return TechTypes.NO_TECH

	def AI_chooseProduction(self,argsList):
		return False

	def AI_unitUpdate(self,argsList):
		pUnit = argsList[0]
		pPlot = pUnit.plot()

		if pUnit.getUnitClassType() == gc.getInfoTypeForString('UNITCLASS_GIANT_SPIDER'):
			iX = pUnit.getX()
			iY = pUnit.getY()
			for iiX in range(iX-1, iX+2, 1):
				for iiY in range(iY-1, iY+2, 1):
					pLoopPlot = CyMap().plot(iiX,iiY)
					for i in range(pLoopPlot.getNumUnits()):
						if pLoopPlot.getUnit(i).getOwner() != pUnit.getOwner():
							return 0
			pUnit.getGroup().pushMission(MissionTypes.MISSION_SKIP, 0, 0, 0, False, False, MissionAITypes.NO_MISSIONAI, pUnit.plot(), pUnit)
			return 1

		if pUnit.getUnitType() == gc.getInfoTypeForString('UNIT_ACHERON'):
			if pPlot.isVisibleEnemyUnit(pUnit.getOwner()):
				pUnit.cast(gc.getInfoTypeForString('SPELL_BREATH_FIRE'))

		# iImprovement = pPlot.getImprovementType()
		# if iImprovement != -1:
			# if (iImprovement == gc.getInfoTypeForString('IMPROVEMENT_BARROW') or iImprovement == gc.getInfoTypeForString('IMPROVEMENT_RUINS') or iImprovement == gc.getInfoTypeForString('IMPROVEMENT_HELLFIRE')):
				# if not pUnit.isAnimal():
					# if pPlot.getNumUnits() - pPlot.getNumAnimalUnits() == 1:
						# pUnit.getGroup().pushMission(MissionTypes.MISSION_SKIP, 0, 0, 0, False, False, MissionAITypes.NO_MISSIONAI, pUnit.plot(), pUnit)
						# return 1
			# if (iImprovement == gc.getInfoTypeForString('IMPROVEMENT_BEAR_DEN') or iImprovement == gc.getInfoTypeForString('IMPROVEMENT_LION_DEN')):
				# if pUnit.isAnimal():
					# if pPlot.getNumAnimalUnits() == 1:
						# pUnit.getGroup().pushMission(MissionTypes.MISSION_SKIP, 0, 0, 0, False, False, MissionAITypes.NO_MISSIONAI, pUnit.plot(), pUnit)
						# return 1
			# if iImprovement == gc.getInfoTypeForString('IMPROVEMENT_GOBLIN_FORT'):
				# if pUnit.getUnitCombatType() == gc.getInfoTypeForString('UNITCOMBAT_ARCHER'):
					# pUnit.getGroup().pushMission(MissionTypes.MISSION_SKIP, 0, 0, 0, False, False, MissionAITypes.NO_MISSIONAI, pUnit.plot(), pUnit)
					# return 1
				# if not pUnit.isAnimal():
					# if pPlot.getNumUnits() - pPlot.getNumAnimalUnits() <= 2:
						# pUnit.getGroup().pushMission(MissionTypes.MISSION_SKIP, 0, 0, 0, False, False, MissionAITypes.NO_MISSIONAI, pUnit.plot(), pUnit)
						# return 1

		return False

	def AI_doWar(self,argsList):
		eTeam = argsList[0]
		return False

	def AI_doDiplo(self,argsList):
		ePlayer = argsList[0]
		return False

	def calculateScore(self,argsList):
		ePlayer = argsList[0]
		bFinal = argsList[1]
		bVictory = argsList[2]

		iPopulationScore = CvUtil.getScoreComponent(gc.getPlayer(ePlayer).getPopScore(), gc.getGame().getInitPopulation(), gc.getGame().getMaxPopulation(), gc.getDefineINT("SCORE_POPULATION_FACTOR"), True, bFinal, bVictory)
		iLandScore = CvUtil.getScoreComponent(gc.getPlayer(ePlayer).getLandScore(), gc.getGame().getInitLand(), gc.getGame().getMaxLand(), gc.getDefineINT("SCORE_LAND_FACTOR"), True, bFinal, bVictory)
		iTechScore = CvUtil.getScoreComponent(gc.getPlayer(ePlayer).getTechScore(), gc.getGame().getInitTech(), gc.getGame().getMaxTech(), gc.getDefineINT("SCORE_TECH_FACTOR"), True, bFinal, bVictory)
		iWondersScore = CvUtil.getScoreComponent(gc.getPlayer(ePlayer).getWondersScore(), gc.getGame().getInitWonders(), gc.getGame().getMaxWonders(), gc.getDefineINT("SCORE_WONDER_FACTOR"), False, bFinal, bVictory)
		return int(iPopulationScore + iLandScore + iWondersScore + iTechScore)

	def doHolyCity(self):
		return False

	def doHolyCityTech(self,argsList):
		eTeam = argsList[0]
		ePlayer = argsList[1]
		eTech = argsList[2]
		bFirst = argsList[3]
		return False

	def doGold(self,argsList):
		ePlayer = argsList[0]
		return False

	def doResearch(self,argsList):
		ePlayer = argsList[0]
		return False

	def doGoody(self,argsList):
		ePlayer = argsList[0]
		pPlot = argsList[1]
		pUnit = argsList[2]
		return False

	def doGrowth(self,argsList):
		pCity = argsList[0]
		return False

	def doProduction(self,argsList):
		pCity = argsList[0]
		return False

	def doCulture(self,argsList):
		pCity = argsList[0]

		pPlayer = gc.getPlayer(pCity.getOwner())
		if pPlayer.isBarbarian():
			if pCity.getNumRealBuilding(gc.getInfoTypeForString('BUILDING_THE_DRAGONS_HORDE')) == 0:
				return 1
		return False

	def doPlotCulture(self,argsList):
		pCity = argsList[0]
		bUpdate = argsList[1]
		ePlayer = argsList[2]
		iCultureRate = argsList[3]
		return False

	def doReligion(self,argsList):
		pCity = argsList[0]
		return False

	def cannotSpreadReligion(self,argsList):
		iOwner, iUnitID, iReligion, iX, iY = argsList[0]
		return False

	def doGreatPeople(self,argsList):
		pCity = argsList[0]
		return False

	def doMeltdown(self,argsList):
		pCity = argsList[0]
		return False

	def doReviveActivePlayer(self,argsList):
		"allows you to perform an action after an AIAutoPlay"
		iPlayer = argsList[0]
		return False

	def doPillageGold(self, argsList):
		"controls the gold result of pillaging"
		pPlot = argsList[0]
		pUnit = argsList[1]

		iPillageGold = 0
		iPillageGold = CyGame().getSorenRandNum(gc.getImprovementInfo(pPlot.getImprovementType()).getPillageGold(), "Pillage Gold 1")
		iPillageGold += CyGame().getSorenRandNum(gc.getImprovementInfo(pPlot.getImprovementType()).getPillageGold(), "Pillage Gold 2")

		iPillageGold += (pUnit.getPillageChange() * iPillageGold) / 100

		return iPillageGold

	def doCityCaptureGold(self, argsList):
		"controls the gold result of capturing a city"

		pOldCity = argsList[0]

		iCaptureGold = 0

		iCaptureGold += gc.getDefineINT("BASE_CAPTURE_GOLD")
		iCaptureGold += (pOldCity.getPopulation() * gc.getDefineINT("CAPTURE_GOLD_PER_POPULATION"))
		iCaptureGold += CyGame().getSorenRandNum(gc.getDefineINT("CAPTURE_GOLD_RAND1"), "Capture Gold 1")
		iCaptureGold += CyGame().getSorenRandNum(gc.getDefineINT("CAPTURE_GOLD_RAND2"), "Capture Gold 2")

		if (gc.getDefineINT("CAPTURE_GOLD_MAX_TURNS") > 0):
			iCaptureGold *= cyIntRange((CyGame().getGameTurn() - pOldCity.getGameTurnAcquired()), 0, gc.getDefineINT("CAPTURE_GOLD_MAX_TURNS"))
			iCaptureGold /= gc.getDefineINT("CAPTURE_GOLD_MAX_TURNS")

		return iCaptureGold

	def citiesDestroyFeatures(self,argsList):
		iX, iY= argsList
		return True

	def canFoundCitiesOnWater(self,argsList):
		iX, iY= argsList
		return False

	def doCombat(self,argsList):
		pSelectionGroup, pDestPlot = argsList
		return False

	def getConscriptUnitType(self, argsList):
		iPlayer = argsList[0]
		iConscriptUnitType = -1 #return this with the value of the UNIT TYPE you want to be conscripted, -1 uses default system

		return iConscriptUnitType

	def getCityFoundValue(self, argsList):
		iPlayer, iPlotX, iPlotY = argsList
		iFoundValue = -1 # Any value besides -1 will be used

		return iFoundValue

	def canPickPlot(self, argsList):
		pPlot = argsList[0]
		return true

	def getUnitCostMod(self, argsList):
		iPlayer, iUnit = argsList
		iCostMod = -1 # Any value > 0 will be used

		return iCostMod

	def getBuildingCostMod(self, argsList):
		iPlayer, iCityID, iBuilding = argsList
		pPlayer = gc.getPlayer(iPlayer)
		pCity = pPlayer.getCity(iCityID)

		iCostMod = -1 # Any value > 0 will be used

		if iBuilding == gc.getInfoTypeForString('BUILDING_GAMBLING_HOUSE'):
			if pPlayer.isGamblingRing():
				iCostMod = gc.getBuildingInfo(iBuilding).getProductionCost() / 4

		return iCostMod

	def canUpgradeAnywhere(self, argsList):
		pUnit = argsList

		bCanUpgradeAnywhere = 0

		return bCanUpgradeAnywhere

	def getWidgetHelp(self, argsList):
		eWidgetType, iData1, iData2, bOption = argsList

		return u""

	def getUpgradePriceOverride(self, argsList):
		iPlayer, iUnitID, iUnitTypeUpgrade = argsList

		return -1	# Any value 0 or above will be used

	def getExperienceNeeded(self, argsList):
		# use this function to set how much experience a unit needs
		iLevel, iOwner = argsList

		iExperienceNeeded = 0

		# regular epic game experience
		iExperienceNeeded = iLevel * iLevel + 1

		iModifier = gc.getPlayer(iOwner).getLevelExperienceModifier()
		if (0 != iModifier):
			iExperienceNeeded += (iExperienceNeeded * iModifier + 99) / 100   # ROUND UP

		return iExperienceNeeded

##--------	Unofficial Bug Fix: Added by Denev 2009/12/31
	def applyBuildEffects(self, argsList):
		pUnit, pCity = argsList
		if pCity.getNumBuilding(gc.getInfoTypeForString('BUILDING_CHANCEL_OF_GUARDIANS')) > 0:
			if not pUnit.noDefensiveBonus() and pUnit.baseCombatStrDefense() > 0:
				if CyGame().getSorenRandNum(100, "Chancel of Guardians") < 20:
					pUnit.setHasPromotion(gc.getInfoTypeForString('PROMOTION_DEFENSIVE'), True)

		if pCity.getNumBuilding(gc.getInfoTypeForString('BUILDING_CAVE_OF_ANCESTORS')) > 0:
			if pUnit.getUnitCombatType() == gc.getInfoTypeForString('UNITCOMBAT_ADEPT'):
				iNumManaKinds = 0
				for iBonus in range(gc.getNumBonusInfos()):
					if pCity.hasBonus(iBonus):
						if gc.getBonusInfo(iBonus).getBonusClassType() == gc.getInfoTypeForString('BONUSCLASS_MANA'):
							iNumManaKinds += 1
				if iNumManaKinds > 0:
					pUnit.changeExperience(iNumManaKinds, -1, False, False, False)

		if pCity.getNumBuilding(gc.getInfoTypeForString('BUILDING_ASYLUM')) > 0:
			if pUnit.isAlive():
				if not isWorldUnitClass(pUnit.getUnitClassType()):
					if CyGame().getSorenRandNum(100, "Asylum") < 10:
						pUnit.setHasPromotion(gc.getInfoTypeForString('PROMOTION_CRAZED'), True)
						pUnit.setHasPromotion(gc.getInfoTypeForString('PROMOTION_ENRAGED'), False)

		if pUnit.getRace() == gc.getInfoTypeForString('PROMOTION_GOLEM'):
			if pCity.getNumRealBuilding(gc.getInfoTypeForString('BUILDING_BLASTING_WORKSHOP')) > 0:
				pUnit.setHasPromotion(gc.getInfoTypeForString('PROMOTION_FIRE2'), True)
			if pCity.getNumRealBuilding(gc.getInfoTypeForString('BUILDING_PALLENS_ENGINE')) > 0:
				pUnit.setHasPromotion(gc.getInfoTypeForString('PROMOTION_PERFECT_SIGHT'), True)
			if pCity.getNumRealBuilding(gc.getInfoTypeForString('BUILDING_ADULARIA_CHAMBER')) > 0:
				pUnit.setHasPromotion(gc.getInfoTypeForString('PROMOTION_STEALTH'), True)
				pUnit.setHasPromotion(gc.getInfoTypeForString('PROMOTION_HIDDEN'), True)
			if pCity.getNumRealBuilding(gc.getInfoTypeForString('BUILDING_GOLEM_TRACKS')) > 0:
				pUnit.setHasPromotion(gc.getInfoTypeForString('PROMOTION_MOBILITY2'), True)
			if pCity.getNumRealBuilding(gc.getInfoTypeForString('BUILDING_DWARVEN_SMITHY')) > 0:
				pUnit.setHasPromotion(gc.getInfoTypeForString('PROMOTION_STRONG'), True)

		elif pUnit.getRace() == gc.getInfoTypeForString('PROMOTION_DWARF'):
			if pCity.getNumBuilding(gc.getInfoTypeForString('BUILDING_BREWERY')) > 0:
				pUnit.changeExperience(2, -1, False, False, False)

		elif pUnit.getRace() == gc.getInfoTypeForString('PROMOTION_DEMON'):
			if pCity.getNumBuilding(gc.getInfoTypeForString('BUILDING_DEMONS_ALTAR')) > 0:
				pUnit.changeExperience(2, -1, False, False, False)

##--------	Unofficial Bug Fix: End Add


# Return 1 if a Mission was pushed
	def AI_MageTurn(self, argsList):
		pUnit = argsList[0]
		pPlot = pUnit.plot()
		pPlayer = gc.getPlayer(pUnit.getOwner())
		eTeam = gc.getTeam(pPlayer.getTeam())
		iCiv = pPlayer.getCivilizationType()
		iX = pUnit.getX()
		iY = pUnit.getY()

		if (pUnit.getUnitAIType() == gc.getInfoTypeForString('UNITAI_TERRAFORMER')):

#-----------------------------------
#TERRAFORMING
#
#SETTING FLAGS
#
#-----------------------------------

			searchdistance=3

#-----------------------------------
#SETTING FLAGS
#
#INIT
#CIV SPECIFIC
#UNIT SPECIFIC
#-----------------------------------

#INIT
			smokeb = true #terraformer tries to put out smoke
			desertb = true #terraformer tries to spring deserts
			snowb = true #terraformer tries to scorch snow to tundra
			tundrab = false #terraformer tries to scorch tundra to plains
			marshb = true #terraformer tries to scorch marsh to grassland
			grassb = false #terraformer tries to scorch grassland to plains
			hellterrb = true #terraformer tries to remove hell terrain
			treesb = false #terraformer tries to Create Trees

			desert = 0
			snow = 0
			tundra = 0
			marsh = 0
			grass = 0
			hellterr = 0
			floodplain = 0
			trees = 0

#CIV SPECIFICS
			if iCiv == gc.getInfoTypeForString('CIVILIZATION_INFERNAL'):
				smokeb = false
			if iCiv == gc.getInfoTypeForString('CIVILIZATION_ILLIANS'):
				snowb = false
			if (iCiv == gc.getInfoTypeForString('CIVILIZATION_DOVIELLO') or iCiv == gc.getInfoTypeForString('CIVILIZATION_ILLIANS')):
				tundrab = false
			if iCiv == gc.getInfoTypeForString('CIVILIZATION_INFERNAL'):
				hellterrb = false

#UNIT SPECIFIC
##			if pUnit.getUnitType() == gc.getInfoTypeForString('UNIT_DEVOUT'):
##				desertb = false #terraformer tries to spring deserts
##				snowb = false #terraformer tries to scorch snow to tundra
##				tundrab = false #terraformer tries to scorch tundra to plains
##				marshb = false #terraformer tries to scorch marsh to grassland
##				grassb = false #terraformer tries to scorch grassland to plains
##				hellterrb = true #terraformer tries to remove hell terrain
##				treesb = false #terraformer tries to Create Trees
##				treesimpb= false #terraformer can Create Trees in Improvements
##
##			if pUnit.getUnitType() == gc.getInfoTypeForString('UNIT_PRIEST_OF_LEAVES'):
##				desertb = false #terraformer tries to spring deserts
##				snowb = false #terraformer tries to scorch snow to tundra
##				tundrab = false #terraformer tries to scorch tundra to plains
##				marshb = false #terraformer tries to scorch marsh to grassland
##				grassb = false #terraformer tries to scorch grassland to plains
##				hellterrb = false #terraformer tries to remove hell terrain
##				treesb = true #terraformer tries to Create Trees
##				treesimpb = false
##				if (iCiv == gc.getInfoTypeForString('CIVILIZATION_LJOSALFAR') or iCiv == gc.getInfoTypeForString('CIVILIZATION_SVARTALFAR')):
##					treesimpb = true
##				if ((treesimpb == False) and (pPlayer.getStateReligion() != gc.getInfoTypeForString('RELIGION_FELLOWSHIP_OF_LEAVES'))):
##					if not pPlayer.isHuman():
##						pUnit.setUnitAIType(gc.getInfoTypeForString('UNITAI_MEDIC'))
##						return 0


#TERRAFORMING
			if pPlot.getOwner()==pUnit.getOwner():
				if (desertb or pPlot.isRiver()):
					if pPlot.getTerrainType()==gc.getInfoTypeForString('TERRAIN_DESERT'):
						if pUnit.canCast(gc.getInfoTypeForString('SPELL_SPRING'),false):
							pUnit.cast(gc.getInfoTypeForString('SPELL_SPRING'))
							return 1
				elif smokeb:
					if pPlot.getImprovementType() == gc.getInfoTypeForString('IMPROVEMENT_SMOKE'):
						if pUnit.canCast(gc.getInfoTypeForString('SPELL_SPRING'),false):
							pUnit.cast(gc.getInfoTypeForString('SPELL_SPRING'))
							return 1

				if (snowb and pPlot.getTerrainType()==gc.getInfoTypeForString('TERRAIN_SNOW')):
					if pUnit.canCast(gc.getInfoTypeForString('SPELL_SCORCH'),false):
						pUnit.cast(gc.getInfoTypeForString('SPELL_SCORCH'))
						return 1

				if (tundrab and pPlot.getTerrainType()==gc.getInfoTypeForString('TERRAIN_TUNDRA')):
					if pUnit.canCast(gc.getInfoTypeForString('SPELL_SCORCH'),false):
						pUnit.cast(gc.getInfoTypeForString('SPELL_SCORCH'))
						return 1

				if (marshb and pPlot.getTerrainType()==gc.getInfoTypeForString('TERRAIN_MARSH')):
					if pUnit.canCast(gc.getInfoTypeForString('SPELL_SCORCH'),false):
						pUnit.cast(gc.getInfoTypeForString('SPELL_SCORCH'))
						return 1

				if (grassb and pPlot.getTerrainType()==gc.getInfoTypeForString('TERRAIN_GRASS')):
					if pUnit.canCast(gc.getInfoTypeForString('SPELL_SCORCH'),false):
						pUnit.cast(gc.getInfoTypeForString('SPELL_SCORCH'))
						return 1

				if hellterrb:
					if pUnit.canCast(gc.getInfoTypeForString('SPELL_SANCTIFY'),false):
						pUnit.cast(gc.getInfoTypeForString('SPELL_SANCTIFY'))
						return 1

				if treesb:
					if pPlot.getFeatureType()==-1:
						if pUnit.canCast(gc.getInfoTypeForString('SPELL_BLOOM'),false):
							if treesimpb or pPlot.getBonusType(-1) == -1:
								pUnit.cast(gc.getInfoTypeForString('SPELL_BLOOM'))
								return 1

#-----------------------------------
#LOOK FOR WORK
#
#MALAKIM EXCEPTION
#LOOK FOR WORK
#-----------------------------------


			for isearch in range(1,searchdistance,1):

	#LOOK FOR WORK
				if 1==1:
					for iiX in range(iX-isearch, iX+isearch+1, 1):
						for iiY in range(iY-isearch, iY+isearch+1, 1):
							pPlot2 = CyMap().plot(iiX,iiY)
							if not (pPlot2.isNone() or pPlot2.isImpassable() or pPlot2.isVisibleEnemyUnit(pUnit.getOwner())):
								if pPlot2.getOwner()==pUnit.getOwner():
									if not (pPlot2.getImprovementType() != -1 and (gc.getImprovementInfo(pPlot2.getImprovementType()).isUnique() == true)):
										if pUnit.isHasPromotion(gc.getInfoTypeForString('PROMOTION_WATER1')):
											if smokeb:
												if (pPlot2.getImprovementType() == gc.getInfoTypeForString('IMPROVEMENT_SMOKE')):
													desert=desert+1
											if (desertb or pPlot.isRiver()):
												if (pPlot2.getTerrainType()==gc.getInfoTypeForString('TERRAIN_DESERT') and not pPlot2.getFeatureType() == gc.getInfoTypeForString('FEATURE_FLOOD_PLAINS')):
													desert=desert+1
										if pUnit.isHasPromotion(gc.getInfoTypeForString('PROMOTION_SUN1')):
											if snowb:
												if pPlot2.getTerrainType()==gc.getInfoTypeForString('TERRAIN_SNOW'):
													snow=snow+1
											if tundrab:
												if pPlot2.getTerrainType()==gc.getInfoTypeForString('TERRAIN_TUNDRA'):
													tundra=tundra+1
											if marshb:
												if pPlot2.getTerrainType()==gc.getInfoTypeForString('TERRAIN_MARSH'):
													marsh=marsh+1
											if grassb:
												if pPlot2.getTerrainType()==gc.getInfoTypeForString('TERRAIN_GRASS'):
													grass=grass+1
										if hellterrb:
											if pUnit.isHasPromotion(gc.getInfoTypeForString('PROMOTION_LIFE1')):
												if (pPlot2.getTerrainType() == gc.getInfoTypeForString('TERRAIN_BROKEN_LANDS') or pPlot2.getTerrainType() == gc.getInfoTypeForString('TERRAIN_BURNING_SANDS') or pPlot2.getTerrainType() == gc.getInfoTypeForString('TERRAIN_FIELDS_OF_PERDITION') or pPlot2.getTerrainType() == gc.getInfoTypeForString('TERRAIN_SHALLOWS')):
													hellterr=hellterr+1
										if treesb:
											if (pPlot2.getFeatureType() == -1):
												if (pPlot2.getTerrainType() == gc.getInfoTypeForString('TERRAIN_GRASS') or pPlot2.getTerrainType() == gc.getInfoTypeForString('TERRAIN_PLAINS') or pPlot2.getTerrainType() == gc.getInfoTypeForString('TERRAIN_TUNDRA')):
													if not pPlot2.isCity():
														if (pPlot2.getImprovementType()==-1 or treesimpb):
															trees=trees+1

	#Remove some deserts/smoke etc.?

				if desert>0:
					for iiX in range(iX-isearch, iX+isearch+1, 1):
						for iiY in range(iY-isearch, iY+isearch+1, 1):
							pPlot2 = CyMap().plot(iiX,iiY)
							if not (pPlot2.isNone() or pPlot2.isImpassable() or pPlot2.isVisibleEnemyUnit(pUnit.getOwner())):
								if pPlot2.getOwner()==pUnit.getOwner():
									if not (pPlot2.getImprovementType() != -1 and (gc.getImprovementInfo(pPlot2.getImprovementType()).isUnique() == true)):
										if smokeb:
											if (pPlot2.getImprovementType() == gc.getInfoTypeForString('IMPROVEMENT_SMOKE')):
												pUnit.getGroup().pushMission(MissionTypes.MISSION_MOVE_TO, iiX, iiY, 0, False, False, MissionAITypes.NO_MISSIONAI, pUnit.plot(), pUnit)
												return 1
										if (desertb or pPlot.isRiver()):
											if (pPlot2.getTerrainType()==gc.getInfoTypeForString('TERRAIN_DESERT') and not pPlot2.getFeatureType() == gc.getInfoTypeForString('FEATURE_FLOOD_PLAINS')):
												pUnit.getGroup().pushMission(MissionTypes.MISSION_MOVE_TO, iiX, iiY, 0, False, False, MissionAITypes.NO_MISSIONAI, pUnit.plot(), pUnit)
												return 1


				if snow>0:
					for iiX in range(iX-isearch, iX+isearch+1, 1):
						for iiY in range(iY-isearch, iY+isearch+1, 1):
							pPlot2 = CyMap().plot(iiX,iiY)
							if not (pPlot2.isNone() or pPlot2.isImpassable() or pPlot2.isVisibleEnemyUnit(pUnit.getOwner())):
								if pPlot2.getOwner()==pUnit.getOwner():
									if not (pPlot2.getImprovementType() != -1 and (gc.getImprovementInfo(pPlot2.getImprovementType()).isUnique() == true)):
										if pPlot2.getTerrainType()==gc.getInfoTypeForString('TERRAIN_SNOW'):
											pUnit.getGroup().pushMission(MissionTypes.MISSION_MOVE_TO, iiX, iiY, 0, False, False, MissionAITypes.NO_MISSIONAI, pUnit.plot(), pUnit)
											return 1

				if tundra>0:
					for iiX in range(iX-isearch, iX+isearch+1, 1):
						for iiY in range(iY-isearch, iY+isearch+1, 1):
							pPlot2 = CyMap().plot(iiX,iiY)
							if not (pPlot2.isNone() or pPlot2.isImpassable() or pPlot2.isVisibleEnemyUnit(pUnit.getOwner())):
								if pPlot2.getOwner()==pUnit.getOwner():
									if not (pPlot2.getImprovementType() != -1 and (gc.getImprovementInfo(pPlot2.getImprovementType()).isUnique() == true)):
										if pPlot2.getTerrainType()==gc.getInfoTypeForString('TERRAIN_TUNDRA'):
											pUnit.getGroup().pushMission(MissionTypes.MISSION_MOVE_TO, iiX, iiY, 0, False, False, MissionAITypes.NO_MISSIONAI, pUnit.plot(), pUnit)
											return 1
				if marsh>0:
					for iiX in range(iX-isearch, iX+isearch+1, 1):
						for iiY in range(iY-isearch, iY+isearch+1, 1):
							pPlot2 = CyMap().plot(iiX,iiY)
							if not (pPlot2.isNone() or pPlot2.isImpassable() or pPlot2.isVisibleEnemyUnit(pUnit.getOwner())):
								if pPlot2.getOwner()==pUnit.getOwner():
									if not (pPlot2.getImprovementType() != -1 and (gc.getImprovementInfo(pPlot2.getImprovementType()).isUnique() == true)):
										if pPlot2.getTerrainType()==gc.getInfoTypeForString('TERRAIN_MARSH'):
											pUnit.getGroup().pushMission(MissionTypes.MISSION_MOVE_TO, iiX, iiY, 0, False, False, MissionAITypes.NO_MISSIONAI, pUnit.plot(), pUnit)
											return 1

				if grass>0:
					for iiX in range(iX-isearch, iX+isearch+1, 1):
						for iiY in range(iY-isearch, iY+isearch+1, 1):
							pPlot2 = CyMap().plot(iiX,iiY)
							if not (pPlot2.isNone() or pPlot2.isImpassable() or pPlot2.isVisibleEnemyUnit(pUnit.getOwner())):
								if pPlot2.getOwner()==pUnit.getOwner():
									if not (pPlot2.getImprovementType() != -1 and (gc.getImprovementInfo(pPlot2.getImprovementType()).isUnique() == true)):
										if pPlot2.getTerrainType()==gc.getInfoTypeForString('TERRAIN_GRASS'):
											pUnit.getGroup().pushMission(MissionTypes.MISSION_MOVE_TO, iiX, iiY, 0, False, False, MissionAITypes.NO_MISSIONAI, pUnit.plot(), pUnit)
											return 1


	#Hell terrain to sanctify?
				if (hellterr>0 and pUnit.isHasPromotion(gc.getInfoTypeForString('PROMOTION_LIFE1'))):
					for iiX in range(iX-isearch, iX+isearch+1, 1):
						for iiY in range(iY-isearch, iY+isearch+1, 1):
							pPlot2 = CyMap().plot(iiX,iiY)
							if not (pPlot2.isNone() or pPlot2.isImpassable() or pPlot2.isVisibleEnemyUnit(pUnit.getOwner()) or pPlot2.isWater()):
								if pPlot2.getOwner()==pUnit.getOwner():
									if gc.getGame().isOption(GameOptionTypes.GAMEOPTION_NO_PLOT_COUNTER):
										if (pPlot2.getTerrainType() == gc.getInfoTypeForString('TERRAIN_BROKEN_LANDS') or pPlot2.getTerrainType() == gc.getInfoTypeForString('TERRAIN_BURNING_SANDS') or pPlot2.getTerrainType() == gc.getInfoTypeForString('TERRAIN_FIELDS_OF_PERDITION') or pPlot2.getTerrainType() == gc.getInfoTypeForString('TERRAIN_SHALLOWS')):
											pUnit.getGroup().pushMission(MissionTypes.MISSION_MOVE_TO, iiX, iiY, 0, False, False, MissionAITypes.NO_MISSIONAI, pUnit.plot(), pUnit)
									elif pPlot2.getPlotCounter()>7:
										pUnit.getGroup().pushMission(MissionTypes.MISSION_MOVE_TO, iiX, iiY, 0, False, False, MissionAITypes.NO_MISSIONAI, pUnit.plot(), pUnit)

				if floodplain>0:
					for iiX in range(iX-isearch, iX+isearch+1, 1):
						for iiY in range(iY-isearch, iY+isearch+1, 1):
							pPlot2 = CyMap().plot(iiX,iiY)
							if not (pPlot2.isNone() or pPlot2.isImpassable() or pPlot2.isVisibleEnemyUnit(pUnit.getOwner())):
								if pPlot2.getOwner()==pUnit.getOwner():
									if not (pPlot2.getImprovementType() != -1 and (gc.getImprovementInfo(pPlot2.getImprovementType()).isUnique() == true)):
										if (pPlot2.isRiver() and pPlot2.getFeatureType()==-1):
											pUnit.getGroup().pushMission(MissionTypes.MISSION_MOVE_TO, iiX, iiY, 0, False, False, MissionAITypes.NO_MISSIONAI, pUnit.plot(), pUnit)
											return 1

				if trees>0 and pUnit.isHasPromotion(gc.getInfoTypeForString('PROMOTION_NATURE2')):
					for iiX in range(iX-isearch, iX+isearch+1, 1):
						for iiY in range(iY-isearch, iY+isearch+1, 1):
							pPlot2 = CyMap().plot(iiX,iiY)
							if not (pPlot2.isNone() or pPlot2.isImpassable() or pPlot2.isVisibleEnemyUnit(pUnit.getOwner())):
								if pPlot2.getOwner()==pUnit.getOwner():
									if not (pPlot2.getImprovementType() != -1 and (gc.getImprovementInfo(pPlot2.getImprovementType()).isUnique() == true)):
										if (pPlot2.getFeatureType() == -1):
											if (pPlot2.getTerrainType() == gc.getInfoTypeForString('TERRAIN_GRASS') or pPlot2.getTerrainType() == gc.getInfoTypeForString('TERRAIN_PLAINS') or pPlot2.getTerrainType() == gc.getInfoTypeForString('TERRAIN_TUNDRA')):
												if not pPlot2.isCity():
													if ((pPlot2.getImprovementType()==-1 and pPlot2.getBonusType(-1)==-1)or treesimpb):
														pUnit.getGroup().pushMission(MissionTypes.MISSION_MOVE_TO, iiX, iiY, 0, False, False, MissionAITypes.NO_MISSIONAI, pUnit.plot(), pUnit)
														if pUnit.plot() == pPlot2:
															pUnit.cast(gc.getInfoTypeForString('SPELL_BLOOM'))
														return 1

#Nothing to do, lets move on to another City!
			iBestCount=0
			pBestCity=0
			for icity in range(pPlayer.getNumCities()):
				pCity = pPlayer.getCity(icity)
				if not pCity.isNone():
					iCount=0
					for iI in range(1, 21):
						pPlot2 = pCity.getCityIndexPlot(iI)
						if not (pPlot2.isNone() or pPlot2.isImpassable() or pPlot2.isVisibleEnemyUnit(pUnit.getOwner())):
							if pPlot2.getOwner()==pUnit.getOwner():
								if not (pPlot2.getImprovementType() != -1 and (gc.getImprovementInfo(pPlot2.getImprovementType()).isUnique() == true)):
									if pUnit.isHasPromotion(gc.getInfoTypeForString('PROMOTION_WATER1')):
										if smokeb:
											if (pPlot2.getImprovementType() == gc.getInfoTypeForString('IMPROVEMENT_SMOKE')):
												iCount=iCount+1
										if (desertb or pPlot.isRiver()):
											if (pPlot2.getTerrainType()==gc.getInfoTypeForString('TERRAIN_DESERT') and not pPlot2.getFeatureType() == gc.getInfoTypeForString('FEATURE_FLOOD_PLAINS')):
												iCount=iCount+1
									if pUnit.isHasPromotion(gc.getInfoTypeForString('PROMOTION_SUN1')):
										if snowb:
											if pPlot2.getTerrainType()==gc.getInfoTypeForString('TERRAIN_SNOW'):
												iCount=iCount+1
										if tundrab:
											if pPlot2.getTerrainType()==gc.getInfoTypeForString('TERRAIN_TUNDRA'):
												iCount=iCount+1
										if marshb:
											if pPlot2.getTerrainType()==gc.getInfoTypeForString('TERRAIN_MARSH'):
												iCount=iCount+1
										if grassb:
											if pPlot2.getTerrainType()==gc.getInfoTypeForString('TERRAIN_GRASS'):
												iCount=iCount+1
									if hellterrb:
										if pUnit.isHasPromotion(gc.getInfoTypeForString('PROMOTION_LIFE1')):
											if (pPlot2.getTerrainType() == gc.getInfoTypeForString('TERRAIN_BROKEN_LANDS') or pPlot2.getTerrainType() == gc.getInfoTypeForString('TERRAIN_BURNING_SANDS') or pPlot2.getTerrainType() == gc.getInfoTypeForString('TERRAIN_FIELDS_OF_PERDITION') or pPlot2.getTerrainType() == gc.getInfoTypeForString('TERRAIN_SHALLOWS')):
												iCount=iCount+1
									if treesb:
										if (pPlot2.getFeatureType() == -1):
											if (pPlot2.getTerrainType() == gc.getInfoTypeForString('TERRAIN_GRASS') or pPlot2.getTerrainType() == gc.getInfoTypeForString('TERRAIN_PLAINS') or pPlot2.getTerrainType() == gc.getInfoTypeForString('TERRAIN_TUNDRA')):
												if not pPlot2.isCity():
													if (pPlot2.getImprovementType()==-1 or treesimpb):
														iCount=iCount+1

					if (iCount>iBestCount):
						pBestCity=pCity
						iBestCount=iCount
			if (pBestCity!=0):
				pCPlot = pBestCity.plot()
				CX = pCPlot.getX()
				CY = pCPlot.getY()
				pUnit.getGroup().pushMission(MissionTypes.MISSION_MOVE_TO, CX, CY, 0, False, False, MissionAITypes.NO_MISSIONAI, pUnit.plot(), pUnit)
				return 1
			return 0


	def AI_Mage_UPGRADE_MANA(self, argsList):
		pUnit = argsList[0]

#-----------------------------------
#UNITAI_MANA_UPGRADE
#Terraformer looks around for mana, changes UNITAI if he doesn't find some
#
# 1) Look for non raw mana and upgrade
# 2) Look for raw mana, decide how to upgrade, and do it!
# 3) Look for mana to dispel, and do it!
#-----------------------------------

		pPlot = pUnit.plot()
		pPlayer = gc.getPlayer(pUnit.getOwner())
		eTeam = gc.getTeam(pPlayer.getTeam())
		iX = pUnit.getX()
		iY = pUnit.getY()

		smokeb = true #Civ likes to put out smoke
		desertb = true #Civ likes to spring deserts
		snowb = true #Civ likes to scorch snow to tundra
		tundrab = false #Civ likes to scorch tundra to plains
		marshb = true #Civ likes to scorch marsh to grassland
		grassb = false #Civ likes to scorch grassland to plains
		hellterrb = true #Civ likes to remove hell terrain

		if (pPlayer.getCivilizationType() == gc.getInfoTypeForString('CIVILIZATION_INFERNAL')):
			smokeb = false
			hellterrb = false

#		if (pPlayer.getCivilizationType() == gc.getInfoTypeForString('CIVILIZATION_INFERNAL')):
#			desertb = false

		if (pPlayer.getCivilizationType() == gc.getInfoTypeForString('CIVILIZATION_ILLIANS')):
			snowb = false

		if pPlayer.getCivilizationType()  == gc.getInfoTypeForString('CIVILIZATION_SHEAIM'):
			hellterrb = false


#Look for Mana to Dispel
		searchdistance=15

		if pUnit.isHasPromotion(gc.getInfoTypeForString('PROMOTION_METAMAGIC2')):
			for isearch in range(1,searchdistance+1,1):
				for iiY in range(iY-isearch, iY+isearch, 1):
					for iiX in range(iX-isearch, iX+isearch, 1):
						pPlot2 = CyMap().plot(iiX,iiY)
						if not (pPlot2.isNone() or pPlot2.isImpassable() or pPlot2.isVisibleEnemyUnit(pUnit.getOwner())):
							if pPlot2.getOwner()==pUnit.getOwner():

								if pPlot2.getBonusType(-1) != -1:
									iBonus = pPlot2.getBonusType(TeamTypes.NO_TEAM)
									if gc.getBonusInfo(iBonus).getBonusClassType() == gc.getInfoTypeForString('BONUSCLASS_MANA'):
										bDispel = true

										if (pPlayer.getArcaneTowerVictoryFlag()==0):
											if CyGame().getSorenRandNum(50, "Don't have to Dispel all the Time"):
												bDispel = false
										if (pPlayer.getArcaneTowerVictoryFlag()==1):
											if iBonus == gc.getInfoTypeForString('BONUS_MANA_BODY'):
												if pPlayer.getNumAvailableBonuses(gc.getInfoTypeForString('BONUS_MANA_BODY'))==1:
													bDispel = false
											if iBonus == gc.getInfoTypeForString('BONUS_MANA_LIFE'):
												if pPlayer.getNumAvailableBonuses(gc.getInfoTypeForString('BONUS_MANA_LIFE'))==1:
													bDispel = false
											if iBonus == gc.getInfoTypeForString('BONUS_MANA_ENCHANTMENT'):
												if pPlayer.getNumAvailableBonuses(gc.getInfoTypeForString('BONUS_MANA_ENCHANTMENT'))==1:
													bDispel = false
											if iBonus == gc.getInfoTypeForString('BONUS_MANA_NATURE'):
												if pPlayer.getNumAvailableBonuses(gc.getInfoTypeForString('BONUS_MANA_NATURE'))==1:
													bDispel = false
											if iBonus == gc.getInfoTypeForString('BONUS_MANA_FORCE'):
												if pPlayer.getNumAvailableBonuses(gc.getInfoTypeForString('BONUS_MANA_FORCE'))==1:
													bDispel = false

										if (pPlayer.getArcaneTowerVictoryFlag()==2):
											if iBonus == gc.getInfoTypeForString('BONUS_MANA_LAW'):
												if pPlayer.getNumAvailableBonuses(gc.getInfoTypeForString('BONUS_MANA_LAW'))==1:
													bDispel = false
											if iBonus == gc.getInfoTypeForString('BONUS_MANA_SUN'):
												if pPlayer.getNumAvailableBonuses(gc.getInfoTypeForString('BONUS_MANA_SUN'))==1:
													bDispel = false
											if iBonus == gc.getInfoTypeForString('BONUS_MANA_SPIRIT'):
												if pPlayer.getNumAvailableBonuses(gc.getInfoTypeForString('BONUS_MANA_SPIRIT'))==1:
													bDispel = false
											if iBonus == gc.getInfoTypeForString('BONUS_MANA_MIND'):
												if pPlayer.getNumAvailableBonuses(gc.getInfoTypeForString('BONUS_MANA_MIND'))==1:
													bDispel = false
											if iBonus == gc.getInfoTypeForString('BONUS_MANA_CREATION'):
												if pPlayer.getNumAvailableBonuses(gc.getInfoTypeForString('BONUS_MANA_CREATION'))==1:
													bDispel = false

										if (pPlayer.getArcaneTowerVictoryFlag()==3):
											if iBonus == gc.getInfoTypeForString('BONUS_MANA_CHAOS'):
												if pPlayer.getNumAvailableBonuses(gc.getInfoTypeForString('BONUS_MANA_CHAOS'))==1:
													bDispel = false
											if iBonus == gc.getInfoTypeForString('BONUS_MANA_DEATH'):
												if pPlayer.getNumAvailableBonuses(gc.getInfoTypeForString('BONUS_MANA_DEATH'))==1:
													bDispel = false
											if iBonus == gc.getInfoTypeForString('BONUS_MANA_ENTROPY'):
												if pPlayer.getNumAvailableBonuses(gc.getInfoTypeForString('BONUS_MANA_ENTROPY'))==1:
													bDispel = false
											if iBonus == gc.getInfoTypeForString('BONUS_MANA_SHADOW'):
												if pPlayer.getNumAvailableBonuses(gc.getInfoTypeForString('BONUS_MANA_SHADOW'))==1:
													bDispel = false
											if iBonus == gc.getInfoTypeForString('BONUS_MANA_DIMENSIONAL'):
												if pPlayer.getNumAvailableBonuses(gc.getInfoTypeForString('BONUS_MANA_DIMENSIONAL'))==1:
													bDispel = false

										if (pPlayer.getArcaneTowerVictoryFlag()==4):
											if iBonus == gc.getInfoTypeForString('BONUS_MANA_EARTH'):
												if pPlayer.getNumAvailableBonuses(gc.getInfoTypeForString('BONUS_MANA_EARTH'))==1:
													bDispel = false
											if iBonus == gc.getInfoTypeForString('BONUS_MANA_FIRE'):
												if pPlayer.getNumAvailableBonuses(gc.getInfoTypeForString('BONUS_MANA_FIRE'))==1:
													bDispel = false
											if iBonus == gc.getInfoTypeForString('BONUS_MANA_AIR'):
												if pPlayer.getNumAvailableBonuses(gc.getInfoTypeForString('BONUS_MANA_AIR'))==1:
													bDispel = false
											if iBonus == gc.getInfoTypeForString('BONUS_MANA_WATER'):
												if pPlayer.getNumAvailableBonuses(gc.getInfoTypeForString('BONUS_MANA_WATER'))==1:
													bDispel = false
											if iBonus == gc.getInfoTypeForString('BONUS_MANA_ICE'):
												if pPlayer.getNumAvailableBonuses(gc.getInfoTypeForString('BONUS_MANA_ICE'))==1:
													bDispel = false

										if bDispel:
											if not (iiX==iX and iiY==iY):
#												CyInterface().addImmediateMessage('Searching for stuff to Dispel', "AS2D_NEW_ERA")
												pUnit.getGroup().pushMission(MissionTypes.MISSION_MOVE_TO, iiX, iiY, 0, False, False, MissionAITypes.NO_MISSIONAI, pUnit.plot(), pUnit)
												return 1
											if pUnit.canCast(gc.getInfoTypeForString('SPELL_DISPEL_MAGIC'),false):
												pUnit.cast(gc.getInfoTypeForString('SPELL_DISPEL_MAGIC'))
												return 1

#Dispel more if we seek Tower Victory Condition
			if (pPlayer.getArcaneTowerVictoryFlag()>0):
				iBestCount=0
				pBestCity=0
				for icity in range(pPlayer.getNumCities()):
					pCity = pPlayer.getCity(icity)
					if not pCity.isNone():
						iCount=0
						for iI in range(1, 21):
							pPlot2 = pCity.getCityIndexPlot(iI)
							if not (pPlot2.isNone() or pPlot2.isImpassable() or pPlot2.isVisibleEnemyUnit(pUnit.getOwner())):
								if pPlot2.getOwner()==pUnit.getOwner():
									if pPlot2.getBonusType(-1) != -1:
										iBonus = pPlot2.getBonusType(TeamTypes.NO_TEAM)
										if gc.getBonusInfo(iBonus).getBonusClassType() == gc.getInfoTypeForString('BONUSCLASS_MANA'):
											iCount=iCount+1
										if gc.getBonusInfo(iBonus).getBonusClassType() == gc.getInfoTypeForString('BONUSCLASS_RAWMANA'):
											iCount=iCount+1

						if (iCount>iBestCount):
							pBestCity=pCity
							iBestCount=iCount
				if (pBestCity!=0):
					pCPlot = pBestCity.plot()
					CX = pCPlot.getX()
					CY = pCPlot.getY()
					pUnit.getGroup().pushMission(MissionTypes.MISSION_MOVE_TO, CX, CY, 0, False, False, MissionAITypes.NO_MISSIONAI, pUnit.plot(), pUnit)
					return 1


#found no mana, return 2 so UNITAI is reset

		return 2

#returns the current flag for Tower Victory
	def AI_TowerMastery(self, argsList):
		ePlayer = argsList[0]
		flag = argsList[1]

		pPlayer = gc.getPlayer(ePlayer)
		eTeam = gc.getTeam(pPlayer.getTeam())

#		CyInterface().addImmediateMessage('This is AI_TowerMastery ', "AS2D_NEW_ERA")
#		CyInterface().addImmediateMessage('Flag is '+str(pPlayer.getArcaneTowerVictoryFlag()), "AS2D_NEW_ERA")

		if flag == 0:
#			if eTeam.isHasTech(gc.getInfoTypeForString('TECH_SORCERY')) == False :
#				return 0
#			if pPlayer.getNumAvailableBonuses(gc.getInfoTypeForString('BONUS_MANA_METAMAGIC'))==0:
#				return 0

			possiblemana=0
			for i in range (CyMap().numPlots()):
				pPlot = CyMap().plotByIndex(i)
				if (pPlot.getOwner()==ePlayer):
					if pPlot.getBonusType(-1) != -1:
						iBonus = pPlot.getBonusType(TeamTypes.NO_TEAM)
						if gc.getBonusInfo(iBonus).getBonusClassType() == gc.getInfoTypeForString('BONUSCLASS_MANA'):
							possiblemana=possiblemana+1
						if gc.getBonusInfo(iBonus).getBonusClassType() == gc.getInfoTypeForString('BONUSCLASS_RAWMANA'):
							possiblemana=possiblemana+1

			if possiblemana<4:
				return 0

			if pPlayer.isHasTech(gc.getInfoTypeForString('TECH_ALTERATION')):
				if pPlayer.getBuildingClassCount(gc.getInfoTypeForString('BUILDINGCLASS_TOWER_OF_ALTERATION'))==0:
					return 1

			if pPlayer.isHasTech(gc.getInfoTypeForString('TECH_DIVINATION')):
				if pPlayer.getBuildingClassCount(gc.getInfoTypeForString('BUILDINGCLASS_TOWER_OF_DIVINATION'))==0:
					return 2

			if pPlayer.isHasTech(gc.getInfoTypeForString('TECH_NECROMANCY')):
				if pPlayer.getBuildingClassCount(gc.getInfoTypeForString('BUILDINGCLASS_TOWER_OF_NECROMANCY'))==0:
					if not pPlayer.isCivic(CvUtil.findInfoTypeNum(gc.getCivicInfo,gc.getNumCivicInfos(),'CIVIC_OVERCOUNCIL')):
						return 3

			if pPlayer.isHasTech(gc.getInfoTypeForString('TECH_ELEMENTALISM')):
				if pPlayer.getBuildingClassCount(gc.getInfoTypeForString('BUILDINGCLASS_TOWER_OF_THE_ELEMENTS'))==0:
					return 4

		if flag==1:
			if pPlayer.getBuildingClassCount(gc.getInfoTypeForString('BUILDINGCLASS_TOWER_OF_ALTERATION'))>0:
				return 0
			else:
				return 1

		if flag==2:
			if pPlayer.getBuildingClassCount(gc.getInfoTypeForString('BUILDINGCLASS_TOWER_OF_DIVINATION'))>0:
				return 0
			else:
				return 2

		if flag==3:
			if pPlayer.getBuildingClassCount(gc.getInfoTypeForString('BUILDINGCLASS_TOWER_OF_NECROMANCY'))>0:
				return 0
			else:
				return 3

		if flag==4:
			if pPlayer.getBuildingClassCount(gc.getInfoTypeForString('BUILDINGCLASS_TOWER_OF_THE_ELEMENTS'))>0:
				return 0
			else:
				return 4

		return 0
