<?xml version="1.0" standalone="yes" ?> 
<scenario>
	<difficulty value="3"/>
	<players>
		<player control="human" faction="tech" team="1"/>
		<player control="cpu-easy" faction="tech" team="1"/>
		<player control="cpu" faction="norsemen" team="2"/>
		<player control="cpu" faction="tech" team="2"/>	
		<player control="cpu" faction="persian" team="2"/>	
		<player control="cpu" faction="tech" team="2"/>	
		<player control="cpu" faction="tech" team="2"/>	
		<player control="cpu" faction="tech" team="2"/>			
	</players>
	<map value="map_1"/>
	<tileset value="forest"/>
	<tech-tree value="megapack_"/>
	<default-resources value="false"/>
	<default-units value="false"/>
	<fog-of-war value="true"/>
	<default-victory-conditions value="false"/>
	<scripts>
		<startup>
			--disable AI
			disableAi(1)
			disableAi(2)
			disableAi(3)
			disableAi(4)
			disableConsume(0)
			disableConsume(1)
			disableConsume(2)
			disableConsume(3)
			disableConsume(4)
			youhelped=false

			--Start MSG
			showMessage('start_go1', 'tba')
			showMessage('start_go2', 'tba')

			--your units

			createUnit('worker_', 0, {startLocation(0)[1] - 0, startLocation(0)[2] + 5})
			workerUnit= lastCreatedUnit()
			setCameraPosition(unitPosition(workerUnit))
			createUnit('hut', 0, startLocation(0))
			houseUnit= lastCreatedUnit()

			--lords units
			createUnit('castle', 1, startLocation(6))
			castleUnit= lastCreatedUnit()
			giveResource('gold', 1, 1000); 
			giveResource('wood', 1, 1000); 
			createUnit('horseman', 1, {startLocation(6)[1] - 0, startLocation(6)[2] + 8})	
			horseUnit= lastCreatedUnit()
			createUnit('guard', 1, {startLocation(6)[1] - 30, startLocation(6)[2] + 20})	
			guard1Unit= lastCreatedUnit()
			createUnit('guard', 1, {startLocation(6)[1] - 30, startLocation(6)[2] + 14})	
			guard2Unit= lastCreatedUnit()
			createUnit('trigger', 1, {startLocation(6)[1] - 24, startLocation(6)[2] + 4})	
			trigger1Unit= lastCreatedUnit()
    			wc_event = registerCellTriggerEventForUnitToUnit(workerUnit,guard1Unit)
    			t1_event = registerCellTriggerEventForUnitToUnit(workerUnit,trigger1Unit)

			--towns units
			createUnit('town', 1, startLocation(1))
			twnUnit= lastCreatedUnit()
			createUnit('hut', 1, {startLocation(1)[1] + 16, startLocation(1)[2] - 6})
			hut1= lastCreatedUnit()
			createUnit('hut', 1, {startLocation(1)[1] - 14, startLocation(1)[2] - 4})
			hut2= lastCreatedUnit()
			createUnit('hut', 1, {startLocation(1)[1] + 10, startLocation(1)[2] + 12})
			hut3= lastCreatedUnit()
			createUnit('farm', 1, {startLocation(1)[1] + 28, startLocation(1)[2] - 4})
			fUnit= lastCreatedUnit()
			createUnit('blacksmith', 1, {startLocation(1)[1] - 0, startLocation(1)[2] - 4})
			bUnit= lastCreatedUnit()
			giveUpgradeCommand(lastCreatedUnit(), 'stables')
			createUnit('barracks', 1, {startLocation(1)[1] + 20, startLocation(1)[2] + 6})	
			barUnit= lastCreatedUnit()
			createUnit('church', 1, {startLocation(1)[1] - 10, startLocation(1)[2] + 8})
			churchUnit= lastCreatedUnit()

			createUnit('worker', 1, {startLocation(1)[1] - 6, startLocation(1)[2] - 0})
			wokUnit= lastCreatedUnit()
			createUnit('archer', 1, {startLocation(1)[1] + 25, startLocation(1)[2] + 6})	
			achUnit= lastCreatedUnit()
			createUnit('swordman', 1, {startLocation(1)[1] + 18, startLocation(1)[2] + 5})	
			sUnit= lastCreatedUnit()

			--enemy land
			createUnit('bone_tent', 2, startLocation(2))	
			createUnit('blacksmith', 2, startLocation(2))	
			createUnit('castle', 2, startLocation(2))
			ECUnit= lastCreatedUnit()	
			createUnit('farm', 2, startLocation(2))	
			createUnit('house', 2, startLocation(2))	
			createUnit('med_bar', 2, startLocation(2))
			createUnit('thor', 2, startLocation(2))
			createUnit('thor', 2, startLocation(2))
			for i=1, 6 do
				createUnit('spearman', 2, startLocation(2))
			end	
			for i=1, 8 do
				createUnit('spearman', 2, startLocation(7))
			end	
			for i=1, 8 do
				createUnit('archer', 2, startLocation(2))
			end	
			for i=1, 4 do
				createUnit('axe_thrower', 2, startLocation(2))
			end	
			for i=1, 4 do
				createUnit('swordman', 2, startLocation(2))
			end	
			for i=1, 3 do
				createUnit('thrull', 2, startLocation(2))
			end	
			for i=1, 2 do
				createUnit('cow', 2, startLocation(2))
			end	


			--enemy camp 2
			createUnit('valhalla', 2, startLocation(3))	
			boneUnit= lastCreatedUnit()
			createUnit('bone_tent', 2, startLocation(3))	
			valaUnit= lastCreatedUnit()
			createUnit('spearman', 2, startLocation(3))
			createUnit('battleaxe', 2, startLocation(3))
			baxUnit= lastCreatedUnit()
			createUnit('archer', 2, startLocation(3))
			createUnit('archer', 2, startLocation(3))
			createUnit('spearman', 2, startLocation(3))
			
			--enemy camp
			createUnit('bone_tent', 2, startLocation(5))	
			bandhUnit= lastCreatedUnit()
			createUnit('spearman', 2, startLocation(5))
<!--			createUnit('battleaxe', 2, startLocation(5))-->
			createUnit('spearman', 2, startLocation(5))
			
			--create enemy attackers
			for i=1, 2 do
				createUnit('spearman', 2, startLocation(5))
				givePositionCommand(lastCreatedUnit(), 'attack', startLocation(1))	
			end

				createUnit('spearman', 2, startLocation(5))	
				rober1Unit= lastCreatedUnit()
				givePositionCommand(rober1Unit, 'attack', {startLocation(1)[1] + 60, startLocation(1)[2] + 24})

			--sheperds units
			createUnit('farm', 4, startLocation(4))
			farmUnit= lastCreatedUnit()
			for i=1, 3 do
				createUnit('worker', 4, startLocation(4))	
			end
			for i=1, 3 do
				createUnit('sheep', 4, startLocation(4))	
			end


			
<!--			objectives
			objective='destroy_west_village'
			showMessage('Brief1', 'tba')
			setDisplayText('ObjectiveWest')-->

    			 cell_event0 = registerCellTriggerEventForUnitToUnit(workerUnit,barUnit)
			
		</startup>

		<cellTriggerEvent>   
		       if triggeredCellEventId() == cell_event1 then 
			clearDisplayText()
			setDisplayText('getwood2')
			objective='getwood'
			showMessage('getwood', 'smith')
 		         unregisterCellTriggerEvent(cell_event1)

		       end

		       if triggeredCellEventId() == cell_event0 then 

    			cell_event1 = registerCellTriggerEventForUnitToUnit(workerUnit,bUnit)
			clearDisplayText()
			setDisplayText('getinlist')
			showMessage('getinlist2', 'bo')
 		         unregisterCellTriggerEvent(cell_event0)

		       end

		       if triggeredCellEventId() == cell_event2 then 
				morphToUnit(workerUnit,'promote_to_swordman',1)
				giveResource('gold', 0, -50); 	
				clearDisplayText()
				showMessage('gotsword', 'bo')	
 		    	        unregisterCellTriggerEvent(cell_event2)
 		   	        unregisterCellTriggerEvent(wc_event)
				cell_event3 = registerCellTriggerEventForUnitToUnit(workerUnit,guard1Unit)
		       end

		       if triggeredCellEventId() == wc_event then 
			showMessage('sp', 'gard')
 		        unregisterCellTriggerEvent(wc_event)
		       end

		       if triggeredCellEventId() == cell_event3 then 
			showMessage('gb', 'gard')
			setDisplayText('getb')
			objective= 'getbrave'	
 		        unregisterCellTriggerEvent(cell_event3)
		       end

		       if triggeredCellEventId() == t1_event then 
				giveAttackCommand(horseUnit, workerUnit)
				unregisterCellTriggerEvent(t1_event)
		       end

		       if triggeredCellEventId() == event_goin then 
			showMessage('hf', 'gard')
				unregisterCellTriggerEvent(event_goin)
		       end

		       if triggeredCellEventId() == event_castle then 
			showMessage('lord1', 'lord')
			showMessage('lord2', 'lord')
			clearDisplayText()
			setDisplayText('bak')
			objective= 'getknight'	
    			event_church = registerCellTriggerEventForUnitToUnit(workerUnit,churchUnit)
			createUnit('technician', 1, {startLocation(6)[1] + 2, startLocation(6)[2] + 2})	
			lordUnit= lastCreatedUnit()
			givePositionCommand(lordUnit, 'attack', {startLocation(6)[1] - 0, startLocation(6)[2] + 6})
			createUnit('churchman', 1, {startLocation(1)[1] - 10, startLocation(1)[2] + 8})
			cmUnit= lastCreatedUnit()
			givePositionCommand(cmUnit, 'attack', {startLocation(1)[1] - 10, startLocation(1)[2] + 6})
				unregisterCellTriggerEvent(event_castle)
<!--			temp for testing
			event_2ndbc = registerCellTriggerEventForUnitToUnit(workerUnit,lordUnit)
			morphToUnit(workerUnit,'promote_to_guard',1)
			temp for testing-->
		       end

		       if triggeredCellEventId() == event_church then 
				if cmUnit ~= nil then
					showMessage('chmn1', 'chmn')
					showMessage('chmn2', 'chmn')
					givePositionCommand(cmUnit, 'attack',  {startLocation(1)[1] + 10, startLocation(1)[2] + 0})
					timer_event1 = startTimerEvent()
				end
				unregisterCellTriggerEvent(event_church)
		       end

		       if triggeredCellEventId() == event_churchman then 

				if cmUnit ~= nil then
				showMessage('chmn3', 'chmn')
				givePositionCommand(cmUnit, 'attack', {startLocation(1)[1] + 60, startLocation(1)[2] + 24})
				createUnit('trigger', 3, {startLocation(1)[1] + 60, startLocation(1)[2] + 24})
				trigger2Unit= lastCreatedUnit()
    				event_churchman2 = registerCellTriggerEventForUnitToUnit(workerUnit,trigger2Unit)
				end
				unregisterCellTriggerEvent(event_churchman)
		       end

		       if triggeredCellEventId() == event_churchman2 then 
				showMessage('chmn4', 'chmn')
				objective= 'killband2'
				clearDisplayText()
				setDisplayText('killrc')
    				event_gethelp = registerCellTriggerEventForUnitToUnit(workerUnit,sUnit)
				unregisterCellTriggerEvent(event_churchman2)
		       end

		       if triggeredCellEventId() == event_churchman3 then 
				if cmUnit ~= nil then
				showMessage('chmn5', 'chmn')
				clearDisplayText()
				setDisplayText('gmm')
				givePositionCommand(cmUnit, 'attack', {startLocation(1)[1] - 10, startLocation(1)[2] + 6})
    				event_ttp = registerCellTriggerEventForUnitToUnit(workerUnit,wokUnit)
				end
				unregisterCellTriggerEvent(event_churchman3)
		       end

		       if triggeredCellEventId() == event_swdman then 
				showMessage('sman1', 'smn')
				unregisterCellTriggerEvent(event_swdman)
		       end

		       if triggeredCellEventId() == event_gethelp then
				if sUnit ~= nil then
					showMessage('sman2', 'smn')
					givePositionCommand(sUnit, 'attack', startLocation(5))
				end
				unregisterCellTriggerEvent(event_gethelp)
		       end

		       if triggeredCellEventId() == event_ttp then 
				showMessage('wok1', 'wokr')
				showMessage('wok2', 'wokr')
				clearDisplayText()
				setDisplayText('kf')
				objective= 'killfarm'	
				givePositionCommand(wokUnit, 'move', {startLocation(1)[1] + 28, startLocation(1)[2] - 4})
				createUnit('cow', 1, {startLocation(1)[1] + 28, startLocation(1)[2] - 4})
				cow1Unit= lastCreatedUnit()
				createUnit('cow', 1, {startLocation(1)[1] + 28, startLocation(1)[2] - 4})
				cow2Unit= lastCreatedUnit()
				createUnit('cow', 1, {startLocation(1)[1] + 28, startLocation(1)[2] - 4})
				cow3Unit= lastCreatedUnit()
				unregisterCellTriggerEvent(event_ttp)
		       end

		       if triggeredCellEventId() == event_gy50g then 
				showMessage('wok3', 'wokr')
				giveResource('gold', 0, 50); 	
    				event_churchk = registerCellTriggerEventForUnitToUnit(workerUnit,cmUnit)
				clearDisplayText()
				setDisplayText('fgykh')
				unregisterCellTriggerEvent(event_gy50g)
		       end

		       if triggeredCellEventId() == event_churchk then 
				morphToUnit(workerUnit,'promote_to_guard',1)
				giveResource('gold', 0, -50); 	
				clearDisplayText()
				setDisplayText('grttl')
				showMessage('okokok', 'chmn')
				showMessage('okokok2', 'chmn')
				event_2ndbc = registerCellTriggerEventForUnitToUnit(workerUnit,lordUnit)
 		   	        unregisterCellTriggerEvent(event_churchk)
		       end

		       if triggeredCellEventId() == event_2ndbc then 
				clearDisplayText()
				setDisplayText('gyaakbc')
				showMessage('lord3', 'lord')
				showMessage('lord4', 'lord')
				event_barracks2 = registerCellTriggerEventForUnitToUnit(workerUnit,barUnit)
 		   	        unregisterCellTriggerEvent(event_2ndbc)
		       end

		       if triggeredCellEventId() == event_barracks2 then 
				showMessage('inhmm', 'you')
				objective= 'killcamp'
				createUnit('archer', 0, {startLocation(1)[1] + 20, startLocation(1)[2] + 6})
				archer1Unit= lastCreatedUnit()
				createUnit('archer', 0, {startLocation(1)[1] + 20, startLocation(1)[2] + 6})
				archer2Unit= lastCreatedUnit()
				createUnit('archer', 0, {startLocation(1)[1] + 20, startLocation(1)[2] + 6})
				archer3Unit= lastCreatedUnit()
				createUnit('swordman', 0, {startLocation(1)[1] + 20, startLocation(1)[2] + 6})
				swordman1Unit= lastCreatedUnit()
				createUnit('swordman', 0, {startLocation(1)[1] + 20, startLocation(1)[2] + 6})
				swordman2Unit= lastCreatedUnit()
				createUnit('swordman', 0, {startLocation(1)[1] + 20, startLocation(1)[2] + 6})
				swordman3Unit= lastCreatedUnit()
				givePositionCommand(archer1Unit, 'attack', startLocation(3))
				givePositionCommand(archer2Unit, 'attack', startLocation(3))
				givePositionCommand(archer3Unit, 'attack', startLocation(3))
				givePositionCommand(swordman1Unit, 'attack', startLocation(3))
				givePositionCommand(swordman2Unit, 'attack', startLocation(3))
				givePositionCommand(swordman3Unit, 'attack', startLocation(3))
				givePositionCommand(horseUnit, 'attack', {startLocation(6)[1] - 0, startLocation(6)[2] + 12})
<!--			temp for testing
			giveAttackCommand(baxUnit, valaUnit)
			temp for testing-->
				youhelped=true
				if sUnit ~= nil then
					givePositionCommand(sUnit, 'attack', startLocation(3))
				end
 		   	        unregisterCellTriggerEvent(event_barracks2)
		       end

		       if triggeredCellEventId() == event_barracks3 then 
				showMessage('igota', 'you')
				objective= 'main_battle'
				--create enemy attackers
				for i=1, 10 do
					createUnit('swordman', 1, {startLocation(1)[1] + 20, startLocation(1)[2] + 6})
					givePositionCommand(lastCreatedUnit(), 'attack', startLocation(2))
				end
				for i=1, 5 do
					createUnit('archer', 1, {startLocation(1)[1] + 20, startLocation(1)[2] + 6})
					givePositionCommand(lastCreatedUnit(), 'attack', startLocation(2))
				end
				if sUnit ~= nil then
					morphToUnit(sUnit,'promote_to_guard',1)
				end
 		   	        unregisterCellTriggerEvent(event_barracks3)
		       end

		       if triggeredCellEventId() == event_ttl2 then 
					showMessage('gswtm', 'lord')
					timer_event2 = startTimerEvent()
					togglePauseGame(1);
 		   	        unregisterCellTriggerEvent(event_ttl2)
		       end


		</cellTriggerEvent>

		<timerTriggerEvent>

			if triggeredTimerEventId() == timer_event1 then
				if timerEventSecondsElapsed(triggeredTimerEventId()) >= 20 then
    					event_churchman = registerCellTriggerEventForUnitToUnit(workerUnit,cmUnit)
					stopTimerEvent(timer_event1)
				end
			end

			if triggeredTimerEventId() == timer_event2 then
				if timerEventSecondsElapsed(triggeredTimerEventId()) >= 15 then
					clearDisplayText()
					morphToUnit(workerUnit,'promote_to_horseman',1)
					setDisplayText('gyaakbc')
					showMessage('lord5', 'lord')
					showMessage('lord6', 'lord')
					showMessage('me', 'you')
					givePositionCommand(horseUnit, 'attack', startLocation(2))
					givePositionCommand(lordUnit, 'attack', startLocation(2))
					event_barracks3 = registerCellTriggerEventForUnitToUnit(workerUnit,barUnit)
					stopTimerEvent(timer_event2)
				end
			end
		</timerTriggerEvent>

		<resourceHarvested>
			if objective=='getwood' and resourceAmount('wood', 0)>=20 then
				showMessage('woodone', 'smith')
				giveResource('gold', 0, 50); 	
				giveResource('wood', 0, -20); 	
    		  		cell_event2 = registerCellTriggerEventForUnitToUnit(workerUnit,barUnit)
				clearDisplayText()
		
			end			
		</resourceHarvested>

		<unitCreatedOfType type="worker">
			if objective=='try_to_win' and unitCountOfType(0, 'worker')>=3 then
				if cmUnit ~= nil then
					giveAttackCommand(cmUnit, castleUnit)
					objective= 'dowin'
				end
			end		
		</unitCreatedOfType>
		
		<unitDied>
			--check for you dying
			if lastDeadUnit()==workerUnit then
				clearDisplayText()
				setPlayerAsWinner(2)
				setPlayerAsWinner(3)				
				endGame()
				workerUnit=nil
			end

			--check for you proving your self
			if lastDeadUnit()==rober1Unit then
				showMessage('ipm', 'tba')
 		       		unregisterCellTriggerEvent(cell_event3)
				unregisterCellTriggerEvent(t1_event)
				clearDisplayText()
				event_castle = registerCellTriggerEventForUnitToUnit(workerUnit,castleUnit)
    		  		event_goin = registerCellTriggerEventForUnitToUnit(workerUnit,guard1Unit)
			end

			--check for archer dying
			if lastDeadUnit()==achUnit then
    		  		event_swdman = registerCellTriggerEventForUnitToUnit(workerUnit,sUnit)
			end

			--check for swordman dying
			if lastDeadUnit()==sUnit then
				sUnit=nil
			end

			--check for churchman dying
			if lastDeadUnit()==cmUnit then
				cmUnit=nil
			end

			--check for bandits house dying
			if lastDeadUnit()==bandhUnit and objective=='killband2' then
				unregisterCellTriggerEvent(event_gethelp)
    				event_churchman3 = registerCellTriggerEventForUnitToUnit(workerUnit,cmUnit)
				showMessage('yes', 'you')
				if sUnit ~= nil then
					givePositionCommand(sUnit, 'attack', {startLocation(1)[1] + 18, startLocation(1)[2] + 5})
				end
			end

			--check for farm dying
			if lastDeadUnit()==farmUnit and objective=='killfarm' then
    				event_gy50g = registerCellTriggerEventForUnitToUnit(workerUnit,wokUnit)
				givePositionCommand(wokUnit, 'move', startLocation(4))
				givePositionCommand(cow1Unit, 'move', startLocation(4))
				givePositionCommand(cow2Unit, 'move', startLocation(4))
				givePositionCommand(cow3Unit, 'move', startLocation(4))


			end

			--check for vala dying
			if lastDeadUnit()==valaUnit and objective=='killcamp' then
				morphToUnit(lordUnit,'build_battle_machine',1)
				clearDisplayText()
    				event_ttl2 = registerCellTriggerEventForUnitToUnit(workerUnit,lordUnit)
				showMessage('bobcw', 'you')
				showMessage('bobcw2', 'you')
			end

			--check for lord losing battle
			if lastDeadUnit()==lordUnit and objective=='main_battle' then
				if workerUnit ~= nil and youhelped == true then
				showMessage('youareking', 'peo')
				destroyUnit(castleUnit);
				createUnit('castle', 0, startLocation(6))
				castleUnit= lastCreatedUnit()
				giveResource('gold', 0, 500); 
				giveResource('wood', 0, 500); 
				giveResource('stone', 0, 50); 
				giveResource('food', 0, 50); 
				destroyUnit(guard1Unit);
				createUnit('guard', 0, {startLocation(6)[1] - 30, startLocation(6)[2] + 20})
				guard1Unit= lastCreatedUnit()
				destroyUnit(guard2Unit);
				createUnit('guard', 0, {startLocation(6)[1] - 30, startLocation(6)[2] + 14})
				guard2Unit= lastCreatedUnit()
			--new town units
			destroyUnit(hut1);
			destroyUnit(hut2);
			destroyUnit(hut3);
			destroyUnit(fUnit);
			destroyUnit(bUnit);
			destroyUnit(barUnit);
			destroyUnit(churchUnit);
			createUnit('hut', 0, {startLocation(1)[1] + 16, startLocation(1)[2] - 6})
			createUnit('hut', 0, {startLocation(1)[1] - 14, startLocation(1)[2] - 4})
			createUnit('hut', 0, {startLocation(1)[1] + 10, startLocation(1)[2] + 12})
			createUnit('farm', 0, {startLocation(1)[1] + 28, startLocation(1)[2] - 4})
			createUnit('cow', 0, {startLocation(1)[1] + 28, startLocation(1)[2] - 4})
			createUnit('cow', 0, {startLocation(1)[1] + 28, startLocation(1)[2] - 4})
			createUnit('blacksmith', 0, {startLocation(1)[1] - 0, startLocation(1)[2] - 4})
			createUnit('barracks', 0, {startLocation(1)[1] + 20, startLocation(1)[2] + 6})	
			createUnit('technodrome', 0, {startLocation(1)[1] - 10, startLocation(1)[2] + 8})

				enableAi(2)
				enableConsume(0)
				enableConsume(2)

				--objectives
				objective='try_to_win'
				setDisplayText('lst')
				end
			end

	

			--check for last village destroyed
			if lastDeadUnit()==ECUnit then
				clearDisplayText()
				setPlayerAsWinner(0)
				setPlayerAsWinner(1)				
				endGame()
			end

			--check for last village destroyed
			if objective=='do_win' and unitCount(2)==0 then
				clearDisplayText()
				setPlayerAsWinner(0)
				setPlayerAsWinner(1)				
				endGame()
			end
			
		</unitDied>		
	</scripts>		
</scenario>
