<?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-ultra" faction="norsemen" team="2"/>
		<player control="cpu" faction="tech" team="2"/>	
		<player control="cpu" faction="tech" team="1"/>	
		<player control="cpu" faction="tech" team="1"/>			
	</players>
	<map value="map_2"/>
	<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)
			disableAi(5)
			disableAi(6)
			disableAi(7)
			disableConsume(0)
			disableConsume(1)
			disableConsume(2)
			disableConsume(3)
			disableConsume(4)
			disableConsume(5)
			disableConsume(6)
			disableConsume(7)

			--Start MSG
			showMessage('start', 'game')
			showMessage('start2', 'game')

			clearDisplayText()
			setDisplayText('geta')

			--your units

			createUnit('lord', 0, startLocation(0))
			player_unit= lastCreatedUnit()
			setCameraPosition(unitPosition(player_unit))
			for i=1, 2 do
				createUnit('horseman', 0, startLocation(0))
			end	
			for i=1, 4 do
				createUnit('swordman', 0, startLocation(0))
			end	
			for i=1, 4 do
				createUnit('archer', 0, startLocation(0))
			end	
			for i=1, 2 do
				createUnit('guard', 0, startLocation(0))
			end	


			--small town units
			createUnit('town', 1, startLocation(1))
			createUnit('castle', 1, {startLocation(1)[1] - 1, startLocation(1)[2] - 6})
			castle_unit= lastCreatedUnit()
			createUnit('hut', 1, {startLocation(1)[1] + 12, startLocation(1)[2] - 4})
			hut1= lastCreatedUnit()
			createUnit('hut', 1, {startLocation(1)[1] - 4, startLocation(1)[2] + 5})
			hut2= lastCreatedUnit()
			createUnit('farm', 1, {startLocation(1)[1] + 9, startLocation(1)[2] + 6})
			farm= lastCreatedUnit()
			createUnit('church', 1, {startLocation(1)[1] - 15, startLocation(1)[2] - 4})
			church= lastCreatedUnit()
			createUnit('hut', 1, {startLocation(1)[1] - 36, startLocation(1)[2] + 2})
			hut3= lastCreatedUnit()
			createUnit('worker', 1, {startLocation(1)[1] - 33, startLocation(1)[2] + 7})
			hermit= lastCreatedUnit()
			createUnit('technician', 1, {startLocation(1)[1] + 3, startLocation(1)[2] + 1})
			lord1_unit= lastCreatedUnit()
			createUnit('cow', 1, {startLocation(1)[1] + 10, startLocation(1)[2] + 8})
			farm= lastCreatedUnit()
			createUnit('swordman', 1, {startLocation(1)[1] + 10, startLocation(1)[2] - 0})
			sman= lastCreatedUnit()
			createUnit('archer', 1, {startLocation(1)[1] - 2, startLocation(1)[2] + 8})
			aman= lastCreatedUnit()
			createUnit('churchman', 1, {startLocation(1)[1] - 13, startLocation(1)[2] + 0})
			churchman= lastCreatedUnit()
			createUnit('worker', 1, {startLocation(1)[1] + 7, startLocation(1)[2] + 10})
			wman= lastCreatedUnit()
			giveResource('gold', 1, 1000); 
			giveResource('wood', 1, 1000); 


			createUnit('thor', 2, startLocation(2))
			thor= lastCreatedUnit()


    			cell_event0 = registerCellTriggerEventForUnitToUnit(player_unit,lord1_unit)

 
		</startup>

		<cellTriggerEvent>   

		       if triggeredCellEventId() == cell_event0 then 

			clearDisplayText()
			setDisplayText('def')
			togglePauseGame(1);
			showMessage('lord1', 'tm')
			showMessage('lord2', 'tm')
			showMessage('lord3', 'tm')
			showMessage('you1', 'you')

			--create enemy attackers
			for i=1, 5 do
				createUnit('spearman', 2, startLocation(5))
				givePositionCommand(lastCreatedUnit(), 'attack', startLocation(1))
			end
			for i=1, 3 do
				createUnit('archer', 2, startLocation(5))
				givePositionCommand(lastCreatedUnit(), 'attack', startLocation(1))
			end

			createUnit('crossbow', 2, startLocation(5))	
			rober1Unit= lastCreatedUnit()
			givePositionCommand(rober1Unit, 'attack', startLocation(1))

			--create barracks
			createUnit('barracks_', 1, {startLocation(1)[1] - 15, startLocation(1)[2] + 4})
			build= lastCreatedUnit()
			givePositionCommand(wman, 'move', {startLocation(1)[1] - 13, startLocation(1)[2] + 2})
 		        unregisterCellTriggerEvent(cell_event0)

		       end

		       if triggeredCellEventId() == cell_event1 then 

			clearDisplayText()
			setDisplayText('geta')
			showMessage('lord5', 'tm')

			--create your army
			for i=1, 5 do
			createUnit('swordman', 0, {startLocation(1)[1] - 15, startLocation(1)[2] + 4})
			end
			for i=1, 5 do
			createUnit('archer', 0, {startLocation(1)[1] - 15, startLocation(1)[2] + 4})
			end

			--create enemy siegers
			for i=1, 8 do
				createUnit('spearman', 2, startLocation(2))
				givePositionCommand(lastCreatedUnit(), 'attack', startLocation(1))
			end
			for i=1, 15 do
				createUnit('spearman', 2, startLocation(2))
				givePositionCommand(lastCreatedUnit(), 'attack', startLocation(2))
			end
			for i=1, 10 do
				createUnit('archer', 2, startLocation(2))
				givePositionCommand(lastCreatedUnit(), 'attack', startLocation(2))
			end
			for i=1, 4 do
				createUnit('axe_thrower', 2, startLocation(2))
				givePositionCommand(lastCreatedUnit(), 'attack', startLocation(2))
			end
			createUnit('wild_sow', 2, startLocation(2))
			sow_unit= lastCreatedUnit()
			givePositionCommand(sow_unit, 'attack', {startLocation(2)[1] + 4, startLocation(2)[2] - 5})
			for i=1, 3 do
				createUnit('battleaxe', 2, startLocation(2))
				givePositionCommand(lastCreatedUnit(), 'attack', startLocation(2))
			end
			for i=1, 2 do
				createUnit('crossbow', 2, startLocation(2))
				givePositionCommand(lastCreatedUnit(), 'attack', startLocation(2))
			end

 		        unregisterCellTriggerEvent(cell_event1)

		       end

		       if triggeredCellEventId() == cell_event2 then 

			clearDisplayText()
			setDisplayText('getv')
			showMessage('hermit1', 'hermit')
			showMessage('hermit2', 'hermit')
			showMessage('hermit3', 'hermit')

    			cell_s_event1 = registerCellTriggerEventForUnitToUnit(player_unit,student1)

 		        unregisterCellTriggerEvent(cell_event2)

		       end

		       if triggeredCellEventId() == cell_event3 then 

			clearDisplayText()
			setDisplayText('ba')
			showMessage('hermit4', 'hermit')
			objective='b_a'

 		        unregisterCellTriggerEvent(cell_event3)

		       end

		       if triggeredCellEventId() == cell_event4 then 

			showMessage('lord8', 'lord')
			showMessage('lord9', 'lord')
			showMessage('lord10', 'lord')
			togglePauseGame(1);
			objective='proov'

			for i=1, 10 do
				createUnit('swordman', 0, {startLocation(3)[1] - 9, startLocation(3)[2] - 4})
			end

			for i=1, 10 do
				createUnit('archer', 0, {startLocation(3)[1] - 8, startLocation(3)[2] + 4})
			end

			givePositionCommand(ar1_unit, 'attack', startLocation(4))
			givePositionCommand(ar2_unit, 'attack', startLocation(4))
			givePositionCommand(sw1_unit, 'attack', startLocation(4))
			givePositionCommand(sw2_unit, 'attack', startLocation(4))

			--last enemy camp
			createUnit('castle', 4, startLocation(4))	
			last_unit= lastCreatedUnit()
			giveResource('gold', 4, 500); 
			giveResource('wood', 4, 500); 
			createUnit('bone_tent', 4, startLocation(4))	
			createUnit('house', 4, startLocation(4))	
			for i=1, 18 do
				createUnit('spearman', 4, startLocation(4))
			end
			for i=1, 14 do
				createUnit('archer', 4, startLocation(4))
			end
			for i=1, 10 do
				createUnit('axe_thrower', 4, startLocation(4))
			end
			for i=1, 5 do
				createUnit('battleaxe', 4, startLocation(4))
			end
			for i=1, 3 do
				createUnit('crossbow', 4, startLocation(4))
			end
			for i=1, 1 do
				createUnit('thor', 4, startLocation(4))
			end
			for i=1, 1 do
				createUnit('swordman', 4, {startLocation(4)[1] - 8, startLocation(4)[2] - 12})
			end
			swordman_unit= lastCreatedUnit()

			enableAi(5)

 		        unregisterCellTriggerEvent(cell_event4)
			end

		       if triggeredCellEventId() == cell_event5 then 

			clearDisplayText()
			setDisplayText('win')
			showMessage('lord11', 'lord')

			togglePauseGame(1);

			destroyUnit(castle_unit);
			destroyUnit(d1_unit);
			destroyUnit(d2_unit);
			destroyUnit(b1_unit);
			destroyUnit(b2_unit);
			destroyUnit(smith_unit);
			destroyUnit(w1_unit);
			destroyUnit(w2_unit);

			createUnit('castle', 0, {startLocation(3)[1] - 1, startLocation(3)[2] - 6})
			createUnit('defense_tower', 0, {startLocation(3)[1] + 14, startLocation(3)[2] - 8})
			createUnit('defense_tower', 0, {startLocation(3)[1] + 22, startLocation(3)[2] + 1})
			createUnit('farm', 0, {startLocation(3)[1] - 9, startLocation(3)[2] - 4})
			createUnit('barracks', 0, {startLocation(3)[1] - 8, startLocation(3)[2] + 4})
			createUnit('blacksmith', 0, {startLocation(3)[1] + 9, startLocation(3)[2] + 4})
			createUnit('worker', 0, {startLocation(3)[1] + 6, startLocation(3)[2] + 4})
			createUnit('worker', 0, {startLocation(3)[1] + 7, startLocation(3)[2] + 6})

			enableAi(4)
			giveResource('food', 0, 120); 
			giveResource('food', 4, 120); 
			enableConsume(0)
			enableConsume(4)

 		        unregisterCellTriggerEvent(cell_event5)

		       end

		       if triggeredCellEventId() == cell_s_event1 then 

			showMessage('s1', 'student')

    			cell_s_event2 = registerCellTriggerEventForUnitToUnit(player_unit,student2)

			givePositionCommand(hermit, 'move', {startLocation(1)[1] - 30, startLocation(1)[2] + 10})
 		        unregisterCellTriggerEvent(cell_s_event1)

		       end

		       if triggeredCellEventId() == cell_s_event2 then 

			showMessage('s2', 'student')

			givePositionCommand(student2, 'move', {startLocation(1)[1] - 33, startLocation(1)[2] + 7})

    			cell_s_event3 = registerCellTriggerEventForUnitToUnit(player_unit,student3)

 		        unregisterCellTriggerEvent(cell_s_event2)

		       end

		       if triggeredCellEventId() == cell_s_event3 then 

			showMessage('s3', 'student')
			objective='getstone'

 		        unregisterCellTriggerEvent(cell_s_event3)

		       end
		       if triggeredCellEventId() == cell_s_event3_2 then 
			showMessage('s32', 'student')
    			cell_s_event4 = registerCellTriggerEventForUnitToUnit(player_unit,student4)
			objective='none'
			giveResource('stone', 0, -30); 
			givePositionCommand(student3, 'move', {startLocation(1)[1] - 33, startLocation(1)[2] + 7})
 		        unregisterCellTriggerEvent(cell_s_event3_2)
		       end

		       if triggeredCellEventId() == cell_s_event4 then 

			showMessage('s4', 'student')

    			cell_s_event6 = registerCellTriggerEventForUnitToUnit(player_unit,student6)
			givePositionCommand(student4, 'move', {startLocation(1)[1] - 33, startLocation(1)[2] + 7})
 		        unregisterCellTriggerEvent(cell_s_event4)

		       end

		       if triggeredCellEventId() == cell_s_event5 then 
<!--
			togglePauseGame(1);
			showMessage('s5', 'student')
			showMessage('s52', 'student')
			showMessage('s53', 'student')

			timer_event1 = startTimerEvent()
-->
					givePositionCommand(student5, 'move', {startLocation(1)[1] - 33, startLocation(1)[2] + 7})

			showMessage('s54', 'student')
    			cell_event3 = registerCellTriggerEventForUnitToUnit(player_unit,hermit)
 		        unregisterCellTriggerEvent(cell_s_event5)

		       end

		       if triggeredCellEventId() == cell_s_event6 then 

			showMessage('s6', 'student')
			objective='getgold'
 		        unregisterCellTriggerEvent(cell_s_event6)

		       end
		       if triggeredCellEventId() == cell_s_event6_2 then 

			showMessage('s32', 'student')
			objective='none'
			giveResource('gold', 0, -25); 
			givePositionCommand(student6, 'move', {startLocation(1)[1] - 33, startLocation(1)[2] + 7})
    			cell_s_event5 = registerCellTriggerEventForUnitToUnit(player_unit,student5)
 		        unregisterCellTriggerEvent(cell_s_event6_2)

		       end

		</cellTriggerEvent>

		<timerTriggerEvent>

			if triggeredTimerEventId() == timer_event1 then
				if timerEventSecondsElapsed(triggeredTimerEventId()) >= 15 then
					morphToUnit(player_unit,'promote_to_horseman',1)
					stopTimerEvent(timer_event1)
				end
			end

		</timerTriggerEvent>

		<resourceHarvested>
			if objective=='getstone' and resourceAmount('stone', 0)>=30 then
    		  	cell_s_event3_2 = registerCellTriggerEventForUnitToUnit(player_unit,student3)
			objective='none'
			end		

			if objective=='getgold' and resourceAmount('gold', 0)>=25 then
    		  	cell_s_event6_2 = registerCellTriggerEventForUnitToUnit(player_unit,student6)
			objective='none'
			end		
		</resourceHarvested>

		<unitCreatedOfType type="aerodrome_">
			if objective=='b_a' then
				morphToUnit(student2,'none',1)		
				morphToUnit(student3,'none',1)
				morphToUnit(student4,'none',1)
				morphToUnit(student5,'none',1)
				morphToUnit(student6,'none',1)
				createUnit('battle_machine', 0, {startLocation(1)[1] - 33, startLocation(1)[2] + 7})
				createUnit('battle_machine', 0, {startLocation(1)[1] - 33, startLocation(1)[2] + 7})
				createUnit('battle_machine', 0, {startLocation(1)[1] - 33, startLocation(1)[2] + 7})
				createUnit('airship', 0, {startLocation(1)[1] - 33, startLocation(1)[2] + 7})
				createUnit('airship', 0, {startLocation(1)[1] - 33, startLocation(1)[2] + 7})
			objective='none'
			showMessage('hermit5', 'hermit')

			clearDisplayText()
			setDisplayText('geta')

			createUnit('thor', 2, startLocation(2))
			thor= lastCreatedUnit()
			togglePauseGame(1);
			showMessage('s52', 'student')
			showMessage('s53', 'student')
			timer_event1 = startTimerEvent()

			end	
		</unitCreatedOfType>
		
		<unitDied>

			--check for you dying
			if lastDeadUnit()==player_unit then
				clearDisplayText()
				setPlayerAsWinner(2)
				setPlayerAsWinner(3)				
				endGame()
				player_unit=nil
			end

			--check for you wining
			if lastDeadUnit()==last_unit then
				clearDisplayText()
				setPlayerAsWinner(0)
				setPlayerAsWinner(1)				
				endGame()
			end

			--check hermit dying
			if lastDeadUnit()==hermit then
				hermit=nil
			end

			--check for swordman dying
			if lastDeadUnit()==swordman_unit then
			swordman_unit=nil
    			cell_event5 = registerCellTriggerEventForUnitToUnit(player_unit,alord_unit)
			end

			--check for thor dying
			if lastDeadUnit()==thor then
			thor=nil

			--next town units
			createUnit('town', 6, startLocation(3))
			createUnit('castle', 6, {startLocation(3)[1] - 1, startLocation(3)[2] - 6})
			castle_unit= lastCreatedUnit()
			createUnit('defense_tower', 6, {startLocation(3)[1] + 14, startLocation(3)[2] - 8})
			d1_unit= lastCreatedUnit()
			createUnit('defense_tower', 6, {startLocation(3)[1] + 22, startLocation(3)[2] + 1})
			d2_unit= lastCreatedUnit()
			createUnit('barracks', 6, {startLocation(3)[1] - 9, startLocation(3)[2] - 4})
			b1_unit= lastCreatedUnit()
			createUnit('barracks', 6, {startLocation(3)[1] - 8, startLocation(3)[2] + 4})
			b2_unit= lastCreatedUnit()
			createUnit('blacksmith', 6, {startLocation(3)[1] + 9, startLocation(3)[2] + 4})
			smith_unit= lastCreatedUnit()
			createUnit('lord', 6, {startLocation(3)[1] + 2, startLocation(3)[2] + 2})
			alord_unit= lastCreatedUnit()
			createUnit('worker', 6, {startLocation(3)[1] + 6, startLocation(3)[2] + 4})
			w1_unit= lastCreatedUnit()
			createUnit('worker', 6, {startLocation(3)[1] + 7, startLocation(3)[2] + 6})
			w2_unit= lastCreatedUnit()

			createUnit('archer', 6, {startLocation(3)[1] + 14, startLocation(3)[2] - 1})
			ar1_unit= lastCreatedUnit()
			createUnit('archer', 6, {startLocation(3)[1] + 14, startLocation(3)[2] - 1})
			ar2_unit= lastCreatedUnit()

			createUnit('swordman', 6, {startLocation(3)[1] + 14, startLocation(3)[2] - 1})
			sw1_unit= lastCreatedUnit()
			createUnit('swordman', 6, {startLocation(3)[1] + 14, startLocation(3)[2] - 1})
			sw2_unit= lastCreatedUnit()

			givePositionCommand(ar1_unit, 'attack', {startLocation(3)[1] - 8, startLocation(3)[2] + 4})
			givePositionCommand(ar2_unit, 'attack', {startLocation(3)[1] - 8, startLocation(3)[2] + 4})
			givePositionCommand(sw1_unit, 'attack', {startLocation(3)[1] - 8, startLocation(3)[2] + 4})
			givePositionCommand(sw2_unit, 'attack', {startLocation(3)[1] - 8, startLocation(3)[2] + 4})
    			cell_event4 = registerCellTriggerEventForUnitToUnit(player_unit,alord_unit)

			end

			--check for first battle over
			if lastDeadUnit()==rober1Unit then

				givePositionCommand(wman, 'move', startLocation(1))
				destroyUnit(build);
				createUnit('barracks', 1, {startLocation(1)[1] - 15, startLocation(1)[2] + 4})
				barracks= lastCreatedUnit()
    				cell_event1 = registerCellTriggerEventForUnitToUnit(player_unit,barracks)
				showMessage('lord4', 'tm')
				if hermit ~= nil then
				givePositionCommand(hermit, 'move', {startLocation(1)[1] - 33, startLocation(1)[2] + 7})
				end
			end

			--check for sow dying
			if lastDeadUnit()==sow_unit then
				showMessage('lord6', 'tm')
				showMessage('lord7', 'tm')
				if hermit ~= nil then
    				cell_event2 = registerCellTriggerEventForUnitToUnit(player_unit,hermit)
				end

			createUnit('student', 7, {startLocation(1)[1] + 33, startLocation(1)[2] + 7})
			student1= lastCreatedUnit()
			createUnit('student', 7, {startLocation(1)[1] - 8, startLocation(1)[2] - 9})
			student2= lastCreatedUnit()
			createUnit('student', 7, {startLocation(1)[1] - 20, startLocation(1)[2] - 3})
			student3= lastCreatedUnit()
			createUnit('student', 7, {startLocation(1)[1] - 15, startLocation(1)[2] + 0})
			student4= lastCreatedUnit()
			createUnit('student', 7, {startLocation(1)[1] - 5, startLocation(1)[2] + 7})
			student5= lastCreatedUnit()
			createUnit('student', 7, {startLocation(1)[1] + 10, startLocation(1)[2] - 7})
			student6= lastCreatedUnit()

			end
			
		</unitDied>		
	</scripts>		
</scenario>
