define script GateHouseControl
define script TakeOverStatus
define script Fireworks(nTown)

begin script GateHouseControl

	Gatehouse[NUMBER_OF_GATEHOUSES]
	GateTown[NUMBER_OF_GATEHOUSES]
	NumberOfGates = 0
	TestPlatoon = 0
	TestSiege = 0
	TestDistance = 0
	MyCreature = 0
	Cnt = 0
	Check[NUMBER_OF_GATEHOUSES] 
	Wall1 = 0
	Wall2 = 0
	MyPlatoon = 0
	NumberOnWall1 = 0
	NumberOnWall2 = 0
	PlatoonSize = 0
	
start
	wait 5 seconds
	begin loop
		// Identify the gates
		Check[Cnt] = get building GATEHOUSE in Town[Cnt] min built 1.0
		if Check[Cnt] not exists
			Check[Cnt] = get building GATEHOUSEF in Town[Cnt] min built 1.0
		end if

		if Check[Cnt] exists
			GateTown[NumberOfGates] = Town[Cnt]
			Gatehouse[NumberOfGates] = Check[Cnt]

			wait 1 second
			// Find the walls and place troops
			Wall1 = get wall segment nearest {Gatehouse[NumberOfGates]} radius 40			
			Wall2 = get wall segment nearest {Gatehouse[NumberOfGates]}+{35,0,0} radius 40

			if get distance from {Wall2} to {Wall1} < 35
				Wall2 = get wall segment nearest {Gatehouse[NumberOfGates]}+{-35,0,0} radius 40
			end if
			if get distance from {Wall2} to {Wall1} < 35
				Wall2 = get wall segment nearest {Gatehouse[NumberOfGates]}+{0,0,35} radius 40
			end if
			if get distance from {Wall2} to {Wall1} < 35
				Wall2 = get wall segment nearest {Gatehouse[NumberOfGates]}+{0,0,-35} radius 40
			end if

			NumberOnWall1 = how many soldiers can get on wall Wall1
			NumberOnWall2 = how many soldiers can get on wall Wall2
			
			if adult capacity of Town[Cnt] - size of Town[Cnt] > 80 and adult capacity of Town[Cnt] > 150
				PlatoonSize = 20
			elsif adult capacity of Town[Cnt] - size of Town[Cnt] > 40
				PlatoonSize = 10
			else
				PlatoonSize = (adult capacity of Town[Cnt] - size of Town[Cnt])/4
			end if
			
			if NumberOnWall1 > PlatoonSize
				NumberOnWall1 = PlatoonSize
			end if
			if NumberOnWall2 > PlatoonSize
				NumberOnWall2 = PlatoonSize
			end if
			
			MyPlatoon = recruit PLATOON_INFO_AZTEC_RANGED_3 town Town[Cnt] platoon of size NumberOnWall1
			wait until MyPlatoon exists
			add action PLATOON_AGENDA_ACTION_GET_ON_WALL using Wall1 to MyPlatoon action queue
			
			MyPlatoon = recruit PLATOON_INFO_AZTEC_RANGED_1 town Town[Cnt] platoon of size NumberOnWall2
			wait until MyPlatoon exists
			add action PLATOON_AGENDA_ACTION_GET_ON_WALL using Wall2 to MyPlatoon action queue
			
			NumberOfGates++ 			
		end if
		Cnt++
		until Cnt > townCount
	end loop


	// The check loop. If there are any gates to check that is
	if NumberOfGates != 0
		Cnt = 0
		wait until get player 0 creature exists
		MyCreature = get player 0 creature
		begin loop

			if get GateTown[Cnt] player != 0
				TestPlatoon = get platoon of player 0 nearest {Gatehouse[Cnt]} radius 50
				TestSiege = get siege weapon of player 0 nearest {Gatehouse[Cnt]} radius 100
				TestDistance = get distance from {Gatehouse[Cnt]} to {MyCreature}

				if TestPlatoon exists or TestSiege exists or TestDistance < 65
					set gate Gatehouse[Cnt] close
					set GateTown[Cnt] emergency for 30 seconds
				else
					set gate Gatehouse[Cnt] open
					stop GateTown[Cnt] emergency
				end if
			end if

			Cnt++		
			if Cnt == NumberOfGates
				Cnt = 0
			end if

		end loop
	end if

end script GateHouseControl

begin script TakeOverStatus

	techLevel = 0
	Cnt = 0
	Win = 0
	Vis = 0
	//oTown = 0
start
	begin loop
		if Town[Cnt] exists
			TownStatus[Cnt] = 0
		else
			TownStatus[Cnt] = -1
		end if
		if get Town[Cnt] player == 0
			TownStatus[Cnt] = 1
		end if

		Cnt++
		until Cnt > NUMBER_OF_TOWNS
	end loop

	Cnt = 0
	begin loop
		wait 1 seconds
		Cnt = 0
		Win = 1
		force while Cnt <= NUMBER_OF_TOWNS
			if TownStatus[Cnt] == 1 and get Town[Cnt] player != 0
				TownStatus[Cnt] = 0
			end if
			
			if TownStatus[Cnt] == 0 
				if get Town[Cnt] player == 0
					techLevel = get tech level of town Town[Cnt]
					if techLevel == 0
						increment tribute by 25000
					elsif techLevel == 1
						increment tribute by 50000
					elsif techLevel == 2
						increment tribute by 100000
					end if
					TownStatus[Cnt] = 1
				else
					Win = 0
				end if
				
				if town Town[Cnt] is under takeover from player 0
					RunAI++
				end if
			end if
			Cnt++
		end while
		
	
		if key KB_U down and key KB_I down
			Cnt = 0
			begin cinema
			start music "big_battle_01" loop forever
			move camera focus to {Town[0]}+{0,200,0} time 2
			move camera position to hand position +{0,200,0} time 5
			//wait until camera ready
			begin loop
				if Town[Cnt] exists				
					if get distance from hand position to {Town[Cnt]} > 200
						run background script Fireworks(Town[Cnt])
						move camera focus to {Town[Cnt]}+{0,200,0} time 5
						wait 7 seconds
					else
						run background script Fireworks(Town[Cnt])
					end if					
				end if
				Cnt++
				until Cnt == NUMBER_OF_TOWNS
			end loop
			stop music with fadetime 6
			end cinema
		
		end if
		
		until Win == 1
	end loop
	
	wait 2 seconds
	increment tribute by 250000
	wait 2 seconds
	Cnt = 0
	start music "big_battle_01" loop forever
	begin cinema
	move camera focus to {Town[0]}+{0,200,0} time 2
	move camera position to hand position +{0,200,0} time 5
	begin loop
		if Town[Cnt] exists			
			if get distance from hand position to {Town[Cnt]} > 300
				run background script Fireworks(Town[Cnt])
				move camera focus to {Town[Cnt]}+{0,200,0} time 7
				wait 7 seconds
			else
				run background script Fireworks(Town[Cnt])
			end if					
		end if
		Cnt++
		until Cnt == NUMBER_OF_TOWNS
	end loop
	stop music with fadetime 6
	end cinema	
	enable global influence
	
end script TakeOverStatus

begin script Fireworks(nTown)
	Cnt = 0
	Vis = 0
	Num = 0
start
	Cnt = 1
	begin loop
		Num = number from 1 to 3
		if Num == 1
			Vis = create visual effect VISUAL_EFFECT_GOLD_SCROLL at {nTown} time 1
		elsif Num == 2
			Vis = create visual effect VISUAL_EFFECT_SILVER_SCROLL at {nTown} time 1
		else
			Vis = create visual effect VISUAL_EFFECT_BRONZE_SCROLL at {nTown} time 1
		end if
		
		Vis = create visual effect VISUAL_EFFECT_FIREWORK_1 at {nTown} time 10
		wait 0.3 seconds
		Vis = create visual effect VISUAL_EFFECT_FIREWORK_2 at {nTown} time 10
		wait 0.3 seconds
		Vis = create visual effect VISUAL_EFFECT_FIREWORK_3 at {nTown} time 10
		wait 0.3 seconds
		Cnt++
		until Cnt == 60
	end loop

end script Fireworks

