## Copyright ClassicThunder 2006-2007

from CvPythonExtensions import *
import CvUtil
import CvScreensInterface
import CvDebugTools
import CvWBPopups
import PyHelpers
import Popup as PyPopup
import CvCameraControls
import CvTopCivs
import sys
import CvWorldBuilderScreen
import CvAdvisorUtils
import CvTechChooser
import pickle
import math
import string
from CvPythonExtensions import *
import CvMapGeneratorUtil

gc = CyGlobalContext()
localText = CyTranslator()
PyPlayer = PyHelpers.PyPlayer
PyInfo = PyHelpers.PyInfo

def isSpreadAble(pPlot):
	if (pPlot.getFeatureType() == 1):
		return true
	elif (pPlot.getFeatureType() == 4):
		return true
	elif (pPlot.isCity() == true):
		return true
	
	return false
			
class FireUtils:
	def __init__(self):
		self.IndexsWithFire = []

	def StartNewGame(self):
		for Index in range(CyMap().numPlots()):
			Plot = CyMap().plotByIndex(Index)
			Plot.setScriptData(pickle.dumps('-1'))
	
	def DoTurn(self):
		Possplots = []
		#Burn out and get spreadable plots
		if (len(self.IndexsWithFire) != 0):
			for Index in self.IndexsWithFire:
				Plot = CyMap().plotByIndex(Index)
				Turns = int(pickle.loads(Plot.getScriptData()))

				if (Plot.isCity()):
					City = Plot.getPlotCity()
					City.setPopulation( City.getPopulation() - int(math.ceil( City.getPopulation() / 5 )))
					if (City.getPopulation() == 0):
						Plot.setScriptData(pickle.dumps(str(-1)))
						Plot.erase()
						Plot.setBonusType( gc.getInfoTypeForString('IMPROVEMENT_CITY_RUINS') )
						Turns = 0		
								
				if (Turns == 0):
					Plot.setScriptData(pickle.dumps(str(-1)))
					Plot.setBonusType(-1)
					ListIndex = self.IndexsWithFire.index(Index)
					del self.IndexsWithFire[ListIndex]
											
				elif (Turns >= 1):
					Plot.setScriptData(pickle.dumps(str(Turns - 1)))
					X = Plot.getX()
					Y = Plot.getY()
					#Up plot
					Plot = CyMap().plot(X, Y+1)
					if isSpreadAble(Plot):
						if (Plot not in Possplots):
							Possplots.append(Plot)				
					#Right Plot
					Plot = CyMap().plot(X+1, Y)
					if isSpreadAble(Plot):
						if (Plot not in Possplots):
							Possplots.append(Plot)					
					#Bottom Plot
					Plot = CyMap().plot(X, Y-1)	
					if isSpreadAble(Plot):
						if (Plot not in Possplots):
							Possplots.append(Plot)						
					#Left Plot
					Plot = CyMap().plot(X-1, Y)
					if isSpreadAble(Plot):
						if (Plot not in Possplots):
							Possplots.append(Plot)
		
		# Spreading
		for ePlot in Possplots:
			Start = 6 + (len(self.IndexsWithFire) / 2)
			if (Start < gc.getGame().getSorenRandNum(11, "Spread Fire")):
				ePlot.setBonusType(-1)
				Fire = gc.getInfoTypeForString('BONUS_FIRE')
				ePlot.setBonusType(Fire)
				if (ePlot.isCity() == false):
					ePlot.setTerrainType(1, true, true)
					ePlot.setFeatureType(-1, -1)
				X = ePlot.getX()
				Y = ePlot.getY()
				self.IndexsWithFire.append(CyMap().plotNum(X,Y))
				Turns = (1 + gc.getGame().getSorenRandNum(3, "Fire Turns"))
				ePlot.setScriptData(pickle.dumps(str(Turns)))
	
	def StartNewFire(self, Winner, Loser):
		self.GoodFire = [gc.getInfoTypeForString('UNIT_FIRECATAPULT'), gc.getInfoTypeForString('UNIT_ATTACKDRAGON')]
		self.MediumFire = [gc.getInfoTypeForString('UNIT_FIREMAGE'), gc.getInfoTypeForString('UNIT_LAIRDRAGON')]
		self.BadFire = [gc.getInfoTypeForString('UNIT_FIREARCHER'), gc.getInfoTypeForString('UNIT_FIRELONGBOWMAN')] 		
		lplot = Loser.plot()
		burn = false
		Possplots = []
		
		if (Winner.getUnitType() in self.GoodFire):
			burn = true
			rand = 6
		elif (Winner.getUnitType() in self.MediumFire):
			burn = true
			rand = 4
		elif (Winner.getUnitType() in self.BadFire):
			burn = true
			rand = 2
		else:
			return
			
		Rand = gc.getGame().getSorenRandNum( 11, "Fire Start" )
		if (rand > Rand):
			if (burn == true):
				X = lplot.getX()
				Y = lplot.getY()
			
				#Up plot
				Plot = CyMap().plot(X, Y+1)
				if isSpreadAble(Plot):
					if (Plot not in Possplots):
						Possplots.append(Plot)
			
				#Right Plot
				Plot = CyMap().plot(X+1, Y)
				if isSpreadAble(Plot):
					if (Plot not in Possplots):
						Possplots.append(Plot)
				
				#Bottom Plot
				Plot = CyMap().plot(X, Y-1)	
				if isSpreadAble(Plot):
					if (Plot not in Possplots):
						Possplots.append(Plot)
					
				#Left Plot
				Plot = CyMap().plot(X-1, Y)
				if isSpreadAble(Plot):
					if (Plot not in Possplots):
						Possplots.append(Plot)
						
			if ( len(Possplots) != 0 ):
				Rand = gc.getGame().getSorenRandNum( len(Possplots), "Fire Turns" )
				Plot = Possplots[Rand]
				Plot.setBonusType(-1)
				Fire = gc.getInfoTypeForString('BONUS_FIRE')
				Plot.setBonusType(Fire)
				if (Plot.isCity() == false):
					Plot.setTerrainType(1, true, true)
					Plot.setFeatureType(-1, -1)
				X = Plot.getX()
				Y = Plot.getY()
				self.IndexsWithFire.append(CyMap().plotNum(X,Y))
				Turns = (2 + gc.getGame().getSorenRandNum(3, "Fire Turns"))
				Plot.setScriptData(pickle.dumps(str(Turns)))
			
	def Load(self):	
		for Index in (CyMap().numPlots()):
			Plot = CyMap().plotByIndex(Index)
			if ( int(pickle.loads(Plot.getScriptData())) != -1 ):
				self.IndexsWithFire.append(Index)