<?xml version="1.0" standalone="yes" ?> 
<scenario>
	<difficulty value="3"/>
	<players>
		<player control="human" faction="tech" team="1"/>
		<player control="network" faction="tech" team="1"/>
		<player control="cpu" faction="tech" team="1"/>
		<player control="cpu" faction="norsemen" team="1"/>
		<player control="cpu" faction="tech" team="2"/>
		<player control="cpu" faction="tech" team="2"/>
	</players>
	<map value="rmap"/>
	<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)
			disableConsume(0)
			disableConsume(1)
			disableConsume(2)
			disableConsume(3)
			disableConsume(4)
			disableConsume(5)

			spawn_r=false
			msg1=false
			msg2=false
			enemy=false

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

			--player1 units
			createUnit('worker_', 0, startLocation(0))
			swordman_unit= lastCreatedUnit()
			setCameraPosition(unitPosition(swordman_unit))

			--player2 units
			createUnit('worker_', 1, startLocation(0))
			archer_unit= lastCreatedUnit()

			--towns units
			createUnit('worker', 2, startLocation(0))
			w1_unit= lastCreatedUnit()
			createUnit('worker', 2, startLocation(0))
			w2_unit= lastCreatedUnit()
			createUnit('worker', 2, startLocation(0))
			w3_unit= lastCreatedUnit()
			createUnit('castle', 2, startLocation(0))
			castle_unit= lastCreatedUnit()
			createUnit('barracks', 2, {startLocation(0)[1] + 12, startLocation(0)[2] + 2})
			b1_unit= lastCreatedUnit()
			createUnit('blacksmith', 2, {startLocation(0)[1] + 8, startLocation(0)[2] + 10})
			b2_unit= lastCreatedUnit()

			createUnit('horseman', 2, {startLocation(0)[1] - 22, startLocation(0)[2] + 2})
			h0_unit= lastCreatedUnit()
			createUnit('horseman', 2, {startLocation(0)[1] - 21, startLocation(0)[2] + 10})
			h1_unit= lastCreatedUnit()
			createUnit('horseman', 2, {startLocation(0)[1] - 10, startLocation(0)[2] + 22})
			h2_unit= lastCreatedUnit()
			createUnit('horseman', 2, {startLocation(0)[1] - 2, startLocation(0)[2] + 22})
			h3_unit= lastCreatedUnit()
			createUnit('horseman', 2, {startLocation(0)[1] + 37, startLocation(0)[2] + 17})
			h4_unit= lastCreatedUnit()

			createUnit('trigger', 2, {startLocation(0)[1] + 50, startLocation(0)[2] + 0})
			trigger1_unit= lastCreatedUnit()


    			 cell_event0 = registerCellTriggerEventForUnitToUnit(swordman_unit,h0_unit)
    			 cell_event1 = registerCellTriggerEventForUnitToUnit(swordman_unit,h1_unit)
    			 cell_event2 = registerCellTriggerEventForUnitToUnit(swordman_unit,h2_unit)
    			 cell_event3 = registerCellTriggerEventForUnitToUnit(swordman_unit,h3_unit)
    			 cell_event4 = registerCellTriggerEventForUnitToUnit(swordman_unit,h4_unit)

    			 cell_event5 = registerCellTriggerEventForUnitToUnit(archer_unit,h0_unit)
    			 cell_event6 = registerCellTriggerEventForUnitToUnit(archer_unit,h1_unit)
    			 cell_event7 = registerCellTriggerEventForUnitToUnit(archer_unit,h2_unit)
    			 cell_event8 = registerCellTriggerEventForUnitToUnit(archer_unit,h3_unit)
    			 cell_event9 = registerCellTriggerEventForUnitToUnit(archer_unit,h4_unit)

    			 cell_event10 = registerCellTriggerEventForUnitToUnit(swordman_unit,trigger1_unit)

					<!--timer_event2 = startTimerEvent()-->
			
		</startup>

		<cellTriggerEvent>   

		       if triggeredCellEventId() == cell_event0 then 
				giveAttackCommand(h0_unit, swordman_unit)
 		         unregisterCellTriggerEvent(cell_event0)

		       end

		       if triggeredCellEventId() == cell_event1 then 
				giveAttackCommand(h1_unit, swordman_unit)
 		         unregisterCellTriggerEvent(cell_event1)

		       end

		       if triggeredCellEventId() == cell_event2 then 
				giveAttackCommand(h2_unit, swordman_unit)
 		         unregisterCellTriggerEvent(cell_event3)

		       end

		       if triggeredCellEventId() == cell_event3 then 
				giveAttackCommand(h3_unit, swordman_unit)
 		         unregisterCellTriggerEvent(cell_event3)

		       end

		       if triggeredCellEventId() == cell_event4 then 
				giveAttackCommand(h4_unit, swordman_unit)
 		         unregisterCellTriggerEvent(cell_event4)

		       end

		       if triggeredCellEventId() == cell_event5 then 
				giveAttackCommand(h0_unit, archer_unit)
 		         unregisterCellTriggerEvent(cell_event5)

		       end

		       if triggeredCellEventId() == cell_event6 then 
				giveAttackCommand(h1_unit, archer_unit)
 		         unregisterCellTriggerEvent(cell_event6)

		       end

		       if triggeredCellEventId() == cell_event7 then 
				giveAttackCommand(h2_unit, archer_unit)
 		         unregisterCellTriggerEvent(cell_event7)

		       end

		       if triggeredCellEventId() == cell_event8 then 
				giveAttackCommand(h3_unit, archer_unit)
 		         unregisterCellTriggerEvent(cell_event8)

		       end

		       if triggeredCellEventId() == cell_event9 then 
				giveAttackCommand(h4_unit, archer_unit)
 		         unregisterCellTriggerEvent(cell_event9)

		       end

		       if triggeredCellEventId() == cell_event10 then 

			showMessage('start3', 'game')
			clearDisplayText()
			setDisplayText('find')

			--robinhoods camp
			createUnit('bone_tent', 3, startLocation(1))
			rcamp_unit= lastCreatedUnit()

    			 cell_event11 = registerCellTriggerEventForUnitToUnit(archer_unit,rcamp_unit)
    			 cell_event12 = registerCellTriggerEventForUnitToUnit(swordman_unit,rcamp_unit)

 		         unregisterCellTriggerEvent(cell_event10)

		        end

		        if triggeredCellEventId() == cell_event11 then 

			if msg2 == false then
			togglePauseGame(1);
			showMessage('ro1', 'ro')
			showMessage('players1', 'pl')
			showMessage('ro2', 'ro')
			showMessage('players2', 'pl')
			showMessage('ro3', 'ro')
			msg2=true
			end
			showMessage('ro4', 'ro')

			if spawn_r == false then
			--robinhoods units
			createUnit('robinhood', 3, startLocation(1))
			rhood_unit= lastCreatedUnit()
			createUnit('archer', 3, startLocation(1))
			rm1_unit= lastCreatedUnit()
			createUnit('archer', 3, startLocation(1))
			rm2_unit= lastCreatedUnit()
			createUnit('archer', 3, startLocation(1))
			rm3_unit= lastCreatedUnit()
			spawn_r=true
		        end

			timer_event0 = startTimerEvent()

 		        unregisterCellTriggerEvent(cell_event11)
			end

		        if triggeredCellEventId() == cell_event12 then 

			if msg2 == false then
			togglePauseGame(1);
			showMessage('ro1', 'ro')
			showMessage('players1', 'pl')
			showMessage('ro2', 'ro')
			showMessage('players2', 'pl')
			showMessage('ro3', 'ro')
			msg2=true
			end
			showMessage('ro4', 'ro')

			if spawn_r == false then
			--robinhoods units
			createUnit('robinhood', 3, startLocation(1))
			rhood_unit= lastCreatedUnit()
			createUnit('archer', 3, startLocation(1))
			rm1_unit= lastCreatedUnit()
			createUnit('archer', 3, startLocation(1))
			rm2_unit= lastCreatedUnit()
			createUnit('archer', 3, startLocation(1))
			rm3_unit= lastCreatedUnit()
			spawn_r=true
		        end

			timer_event1 = startTimerEvent()

 		         unregisterCellTriggerEvent(cell_event12)

		       end

		       if triggeredCellEventId() == cell_event13 then 


			--churchman's units
			createUnit('churchman', 4, startLocation(2))
			cm_unit= lastCreatedUnit()
			givePositionCommand(cm_unit, 'attack', {startLocation(0)[1] - 2, startLocation(0)[2] + 38})
			for i=1, 2 do
			createUnit('swordman', 4, startLocation(2))
			givePositionCommand(lastCreatedUnit(), 'attack', {startLocation(0)[1] - 2, startLocation(0)[2] + 38})	
			end

 		         unregisterCellTriggerEvent(cell_event13)

		       end

		       if triggeredCellEventId() == cell_event14 then 

			showMessage('ro6', 'ro')
			showMessage('ro7', 'ro')
			showMessage('ro8', 'ro')
			clearDisplayText()
			setDisplayText('filt')

			giveResource('gold', 1, -100);
			giveResource('gold', 0, -100);

			--banker's units
			createUnit('technician', 4, startLocation(5))
			createUnit('technodrome', 4, startLocation(5))
			bank_unit= lastCreatedUnit()
			createUnit('guard', 4, startLocation(5))
			for i=1, 2 do
			createUnit('archer', 4, startLocation(5))
			end

			--farmer's units
			createUnit('worker', 2, startLocation(4))
			poor_unit= lastCreatedUnit()
			createUnit('farm', 2, startLocation(4))
			for i=1, 4 do
			createUnit('cow', 2, startLocation(4))
			end


 		         unregisterCellTriggerEvent(cell_event14)

		       end

		       if triggeredCellEventId() == cell_event15 then 

			togglePauseGame(1);
			showMessage('po1', 'po')
			showMessage('po2', 'po')
			clearDisplayText()
			setDisplayText('rep')

			giveResource('gold', 1, -1000);
			giveResource('gold', 0, -1000);

    			cell_event16 = registerCellTriggerEventForUnitToUnit(archer_unit,rhood_unit)


 		         unregisterCellTriggerEvent(cell_event15)

		       end

		       if triggeredCellEventId() == cell_event16 then 

			showMessage('ro9', 'ro')
			clearDisplayText()
			setDisplayText('win')
			objective= 'sss'

			--destroy towns units
			destroyUnit(b1_unit);
			destroyUnit(b2_unit);
			destroyUnit(w1_unit);
			destroyUnit(w2_unit);
			destroyUnit(w3_unit);
			destroyUnit(castle_unit);
			destroyUnit(h0_unit);
			destroyUnit(h1_unit);
			destroyUnit(h2_unit);
			destroyUnit(h3_unit);
			destroyUnit(h4_unit);

			--towns new units
			createUnit('worker', 4, startLocation(0))
			createUnit('worker', 4, startLocation(0))
			createUnit('worker', 4, startLocation(0))
			createUnit('castle', 4, startLocation(0))
			e_castle_unit= lastCreatedUnit()
			createUnit('barracks', 4, {startLocation(0)[1] + 12, startLocation(0)[2] + 2})
			createUnit('blacksmith', 4, {startLocation(0)[1] + 8, startLocation(0)[2] + 10})
			createUnit('horseman', 4, {startLocation(0)[1] - 22, startLocation(0)[2] + 2})
			h5_unit= lastCreatedUnit()
			createUnit('horseman', 4, {startLocation(0)[1] - 21, startLocation(0)[2] + 10})
			h6_unit= lastCreatedUnit()
			createUnit('horseman', 4, {startLocation(0)[1] - 10, startLocation(0)[2] + 22})
			createUnit('horseman', 4, {startLocation(0)[1] - 2, startLocation(0)[2] + 22})
			createUnit('horseman', 4, {startLocation(0)[1] + 37, startLocation(0)[2] + 17})

			--robinhood's actions
			givePositionCommand(rhood_unit, 'attack', startLocation(0))
			givePositionCommand(rm1_unit, 'attack', startLocation(0))
			givePositionCommand(rm2_unit, 'attack', startLocation(0))
			givePositionCommand(rm3_unit, 'attack', startLocation(0))
			createUnit('crossbow', 3, startLocation(1))
			rm4_unit= lastCreatedUnit()
			givePositionCommand(rm4_unit, 'attack', startLocation(0))



 		         unregisterCellTriggerEvent(cell_event16)

		       end

		       if triggeredCellEventId() == cell_event17 then 

			showMessage('po3', 'po')
			clearDisplayText()
			setDisplayText('rep')

    			cell_event18 = registerCellTriggerEventForUnitToUnit(archer_unit,rhood_unit)

 		         unregisterCellTriggerEvent(cell_event17)

		       end

		       if triggeredCellEventId() == cell_event18 then 
				clearDisplayText()
				setPlayerAsWinner(0)	
				setPlayerAsWinner(1)		
				setPlayerAsWinner(2)	
				setPlayerAsWinner(3)	
				setPlayerAsWinner(4)	
				setPlayerAsWinner(5)	
				endGame()
 		         unregisterCellTriggerEvent(cell_event18)

		       end


		</cellTriggerEvent>

		<timerTriggerEvent>

			if triggeredTimerEventId() == timer_event0 then
				if timerEventSecondsElapsed(triggeredTimerEventId()) >= 5 then
					togglePauseGame(1);

					morphToUnit(archer_unit,'ach',1)

					if msg1 == false then
					clearDisplayText()
					setDisplayText('kill')
					showMessage('ro5', 'ro')

			createUnit('trigger', 2, {startLocation(2)[1] + 11, startLocation(2)[2] - 35})
			trigger2_unit= lastCreatedUnit()
    			cell_event13 = registerCellTriggerEventForUnitToUnit(archer_unit,trigger2_unit)


					msg1=true
					end

					stopTimerEvent(timer_event0)
				end
			end

			if triggeredTimerEventId() == timer_event1 then
				if timerEventSecondsElapsed(triggeredTimerEventId()) >= 5 then
					togglePauseGame(1);

					morphToUnit(swordman_unit,'ach',1)

					if msg1 == false then
					clearDisplayText()
					setDisplayText('kill')
					showMessage('ro5', 'ro')

			createUnit('trigger', 2, {startLocation(2)[1] + 13, startLocation(2)[2] - 25})
			trigger2_unit= lastCreatedUnit()
    			cell_event13 = registerCellTriggerEventForUnitToUnit(archer_unit,trigger2_unit)

					msg1=true
					end

					stopTimerEvent(timer_event1)
				end
			end

			if triggeredTimerEventId() == timer_event2 then
				if timerEventSecondsElapsed(triggeredTimerEventId()) >= 2 then

					moveToUnit(archer_unit, swordman_unit)

					stopTimerEvent(timer_event2)
					timer_event2 = startTimerEvent()
				end
			end

			if triggeredTimerEventId() == timer_event3 then
				if timerEventSecondsElapsed(triggeredTimerEventId()) >= 5 then

					morphToUnit(swordman_unit,'hor',1)
					morphToUnit(archer_unit,'hor',1)
					givePositionCommand(rm5_unit, 'attack', startLocation(3))

					stopTimerEvent(timer_event3)
				end
			end

		</timerTriggerEvent>
		
		<unitDied>

			--check for swordman dying
			if lastDeadUnit()==swordman_unit then
				clearDisplayText()
				setPlayerAsWinner(7)	
				setPlayerAsWinner(6)		
				endGame()
			end

			--check for archer dying
			if lastDeadUnit()==archer_unit then
				clearDisplayText()
				setPlayerAsWinner(7)	
				setPlayerAsWinner(6)			
				endGame()
			end

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

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

			--check for churchman dying
			if lastDeadUnit()==cm_unit then

			giveResource('gold', 1, 100);
			giveResource('gold', 0, 100);

			clearDisplayText()
			setDisplayText('rep')
			showMessage('players3', 'pl')
    			cell_event14 = registerCellTriggerEventForUnitToUnit(swordman_unit,rhood_unit)

			end

			--check for bank dying
			if lastDeadUnit()==bank_unit then

			giveResource('gold', 1, 1000);
			giveResource('gold', 0, 1000);

			clearDisplayText()
			setDisplayText('poor')
			showMessage('players3', 'pl')
    			cell_event15 = registerCellTriggerEventForUnitToUnit(swordman_unit,poor_unit)

			end

			--check for e castle dying
			if lastDeadUnit()==e_castle_unit then
				if h5_unit ~= nil then
				<!--givePositionCommand(h5_unit, 'attack', startLocation(0))-->
				destroyUnit(h5_unit);
				end
				if h6_unit ~= nil then
				<!--givePositionCommand(h6_unit, 'attack', startLocation(0))-->
				destroyUnit(h6_unit);
				end
				objective= 'none'
				togglePauseGame(1);
				showMessage('ro10', 'ro')
				showMessage('ro11', 'ro')
				showMessage('ro12', 'ro')
				clearDisplayText()
				setDisplayText('save')

				timer_event3 = startTimerEvent()

				--robinhood's man
				createUnit('archer', 3, startLocation(0))
				rm5_unit= lastCreatedUnit()

				--the tower
				createUnit('tower', 4, startLocation(3))
				tower_unit= lastCreatedUnit()

				for i=1, 4 do
				createUnit('swordman', 4, startLocation(3))
				end
				for i=1, 4 do
				createUnit('archer', 4, startLocation(3))
				end
			end

			--check for tower dying
			if lastDeadUnit()==tower_unit then

			createUnit('daughter', 2, startLocation(3))
			daughter_unit= lastCreatedUnit()

			moveToUnit(daughter_unit, poor_unit)

    			cell_event17 = registerCellTriggerEventForUnitToUnit(daughter_unit,poor_unit)

			for i=1, 2 do
			createUnit('swordman', 4, {startLocation(4)[1] - 12, startLocation(4)[2] - 40})
			end

			clearDisplayText()
			setDisplayText('save2')

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