<?xml version="1.0" standalone="yes" ?>
<scenario>
	<difficulty value="4"/>
	<players>
		<player control="human" faction="tech" team="1"/>
		<player control="cpu-ultra" faction="norsemen" team="2"/>
		<player control="cpu-ultra" faction="tech" team="1"/>
		<player control="closed"/>
	</players>
	<map value="in_the_forest"/>
	<tileset value="forest"/>
	<tech-tree value="megapack"/>
	<default-resources value="false"/>
	<default-units value="false"/>
	<default-victory-conditions value="false"/>
	<scripts>
		<global>
			-- global vars
			isSent=0

			function addAttackingEnemy(unit, amount, startPosition)
				for i=1, amount do
					enemyCount=enemyCount+1
					if startPosition ~= 'far' then
						createUnit(unit, 1, startLocation(1))
					else
						createUnit(unit, 1, {startLocation(1)[1] - 30, startLocation(1)[2] + 30})
					end
					enemies[#enemies+1]=lastCreatedUnit()
					givePositionCommand(lastCreatedUnit(), 'attack', startLocation(0))
				end
			end
		</global>
		<startup>
			--my castle
			createUnit('castle', 0, {startLocation(0)[1] - 2, startLocation(0)[2] - 1})

			--allied castle
			createUnit('castle', 2, {startLocation(0)[1] + 8, startLocation(0)[2] + 5})
			alliedCastle=lastCreatedUnit()
			disableAi(2)

			--my units
			createUnit('cow', 0, startLocation(0))
			for i=1, 3 do
				createUnit('worker', 0, startLocation(0))
				createUnit('swordman', 0, startLocation(0))
			end
			for i=1, 2 do
				createUnit('archer', 0, startLocation(0))
			end
			giveResource('gold', 0, 1000);
			giveResource('wood', 0, 1200);
			giveResource('stone', 0, 350);
			giveResource('food', 0, 150);

			-- enemy units
			createUnit('castle', 1, startLocation(1))
			for i=1, 30 do
				createUnit('valkyrie', 1, startLocation(1))
				givePositionCommand(lastCreatedUnit(), 'move', startLocation(1))
			end
			disableAi(1)
			disableConsume(1)
			giveResource('food', 1, 150)

			enemies={}
			enemyCount=0;
			addAttackingEnemy('cudgel_lady', 10)

			--objectives
			objective='Firstwave'
			showMessage('Brief1', 'Amazones')
			setDisplayText('Firstwave')

			counterMode='Wait'
			TimeRatio=50
			timer_event1 = startTimerEvent()
			timer_event2 = startTimerEvent()
		</startup>

		<unitCreated>
		</unitCreated>

		<timerTriggerEvent>
			-- displayFormattedText('counterMode=%s, %s/%s, %s/100; enemyCount=%s; objective=%s', counterMode, timerEventSecondsElapsed(timer_event1), TimeRatio, timerEventSecondsElapsed(timer_event2), enemyCount, objective);
			if triggeredTimerEventId() == timer_event1 then
				if timerEventSecondsElapsed(triggeredTimerEventId()) >= TimeRatio then
	    				TimeRatio=50
					counterMode='NextObjective'
				end
				if counterMode=='KillDetected' then
					resetTimerEvent(triggeredTimerEventId())
					counterMode='Kill'
				end
				if counterMode=='Kill' then
					resetTimerEvent(timer_event2)
				elseif counterMode=='End' then
					stopTimerEvent(triggeredTimerEventId())
				end
			end
			if triggeredTimerEventId() == timer_event2 then
				if timerEventSecondsElapsed(triggeredTimerEventId()) >= 100 then
	    				counterMode='Kill'
				end
				if counterMode=='NextObjective' or counterMode=='FinishHim' then
					if(objective=="Fourthwave") then
						addAttackingEnemy('cudgel_lady', 40)
						addAttackingEnemy('valkyrie', 10, 'far')
						objective='Lastwave';
					elseif(objective=="Thirdwave") then
						addAttackingEnemy('cudgel_lady', 30)
						addAttackingEnemy('valkyrie', 5, 'far')
						objective='Fourthwave';
					elseif(objective=="Secondwave") then
						addAttackingEnemy('cudgel_lady', 50)
						objective='Thirdwave';
					elseif(objective=="Firstwave") then
						addAttackingEnemy('cudgel_lady', 23)
						objective='Secondwave';
					end
					if counterMode~='End' and counterMode~='FinishHim' then
						showMessage(objective, 'Amazones');
						setDisplayText(objective);
					end
					if counterMode=='FinishHim' then
						-- AI will check the whole map
						enableAi(1)
						counterMode='End'
					else
						counterMode='Wait'
					end
				end
				if counterMode=='Wait' then
					resetTimerEvent(timer_event1)
				elseif counterMode=='End' then
					stopTimerEvent(triggeredTimerEventId())
				end
			end
			if triggeredTimerEventId() == timer_event3 then
				if timerEventSecondsElapsed(triggeredTimerEventId()) >= 3 then
					endGame()
					stopTimerEvent(triggeredTimerEventId())
				end
			end
		</timerTriggerEvent>
		<unitDied>
			--check for all enemies dead
			for i=1, #enemies do
				if lastDeadUnit()==enemies[i] then
					enemyCount=enemyCount-1

					-- only if enemy is killed
					if objective=="Lastwave" then
						if isSent==0 then
							-- delayed attack
							addAttackingEnemy('flying_valkyrie', 5, 'far')
							isSent=1
						end
					end
					if counterMode~='Wait' and counterMode~='End' then
						counterMode='KillDetected'
					end
					if counterMode~='Wait' then
						if enemyCount>=25 then
							TimeRatio=55
						elseif enemyCount>=18 then
							TimeRatio=50
						elseif enemyCount>=12 then
							TimeRatio=45
						elseif enemyCount>=7 then
							TimeRatio=40
						elseif enemyCount>=3 then
							TimeRatio=35
						else
							TimeRatio=30
						end
					end
				end
			end
			if lastDeadUnit()==alliedCastle then
				counterMode='FinishHim'
				setPlayerAsWinner(1)
				clearDisplayText()
				timer_event3 = startTimerEvent()
			elseif enemyCount==0 and counterMode~='End' then
				if(objective=="Lastwave") then
					counterMode='End'
					setPlayerAsWinner(0)
					setPlayerAsWinner(2)
					clearDisplayText()
					timer_event3 = startTimerEvent()
				else
					-- enemies={}
					counterMode='NextObjective'
				end
			end
		</unitDied>
	</scripts>
</scenario>
