<?xml version="1.0" standalone="yes" ?>
<scenario>
	<difficulty value="3"/>
	<players>
		<player control="cpu-ultra" faction="romans" team="9"/>
		<player control="human" faction="tech" team="1"/>
		<player control="network" faction="indian" team="2"/>
	</players>
	<map value="island_siege"/>
	<tileset value="forest"/>
	<tech-tree value="megapack"/>
	<default-resources value="false"/>
	<default-units value="false"/>
	<default-victory-conditions value="false"/>
	<fog-of-war value="explored"/>
	<scripts>
		<global>
			-- global vars
			player2_flag_location = {20,186}
			player3_flag_location = {230,206}
			warning_zone_radius = 70
			home_zone_radius = 20

			flag_bearer_selection_radius = 1.3 -- can be -1 to default to system default
			flag_bearer_selection_thickness = 0.2 -- can be -1 to default to system default
			flag_bearer_selection_color = {-1,-1,-1,-1} -- can be -1 to default to system default, last value is alpha value
		</global>
		<startup>
			-- x1, y1, x2, y2
			player2_flag_zone_quad = {0,player2_flag_location[2]-warning_zone_radius,player2_flag_location[1]+warning_zone_radius,255}
			player3_flag_zone_quad = {player3_flag_location[1]-warning_zone_radius,player3_flag_location[2]-warning_zone_radius,255,255}
			player2_flag_small_zone_quad = {player2_flag_location[1]-1,player2_flag_location[2]-1,player2_flag_location[1]+1,player2_flag_location[2]+1}
			player3_flag_small_zone_quad = {player3_flag_location[1]-1,player3_flag_location[2]-1,player3_flag_location[1]+1,player3_flag_location[2]+1}
			player2_home_zone_quad = {0,0,startLocation(1)[1]+home_zone_radius,startLocation(1)[2]+home_zone_radius}
			player3_home_zone_quad = {startLocation(2)[1]-home_zone_radius,0,255,startLocation(2)[2]+home_zone_radius}

			-- create player2 units
			createUnit('castle', 1, startLocation(1))
			player2_home = lastCreatedUnit()
			createUnit('swordman', 1, startLocation(1))
			createUnit('archer', 1, startLocation(1))
			createUnit('battle_machine', 1, startLocation(1))
			disableConsume(1)
			giveResource('gold', 1, 250);
			giveResource('wood', 1, 150);
			giveResource('food', 1, 150);

			-- create player3 units
			createUnit('main_teepee', 2, startLocation(2))
			player3_home = lastCreatedUnit()
			createUnit('stickfighter', 2, startLocation(2))
			createUnit('fire_archer', 2, startLocation(2))
			createUnit('fire_golem', 2, startLocation(2))
			disableConsume(2)
			giveResource('gold', 2, 250);
			giveResource('wood', 2, 150);
			giveResource('housing', 2, 1);

			-- create player1 units
			createUnit('forum', 0, startLocation(0))
			for i=1, 3 do
				createUnit('slave', 0, startLocation(0))
				createUnit('archer', 0, startLocation(0))
				createUnit('spearman', 0, startLocation(0))
				createUnit('cow', 0, startLocation(0))
			end
			createUnit('guard_tower', 0, {startLocation(0)[1]-12,startLocation(0)[2]-10})
			createUnit('guard_tower', 0, {startLocation(0)[1]-12,startLocation(0)[2]+12})
			createUnit('guard_tower', 0, {startLocation(0)[1]+12,startLocation(0)[2]-8})
			createUnit('guard_tower', 0, {startLocation(0)[1]+12,startLocation(0)[2]+12})
			giveResource('gold', 0, 250);
			giveResource('wood', 0, 150);
			giveResource('food', 0, 150);

			player1_in_home2_zone_event = registerCellAreaTriggerEventForFactionToLocation(0, player2_home_zone_quad)
			player1_in_home3_zone_event = registerCellAreaTriggerEventForFactionToLocation(0, player3_home_zone_quad)
			player2_in_flag_zone_event = registerCellAreaTriggerEventForFactionToLocation(1, player3_flag_zone_quad)
			player3_in_flag_zone_event = registerCellAreaTriggerEventForFactionToLocation(2, player2_flag_zone_quad)

			-- setup the flags
			addCellMarker(2,'Catch this flag!','',player2_flag_location)
			addCellMarker(1,'Catch this flag!','',player3_flag_location)
			if humanFaction() == 1 then
				-- addCellMarker(-1,'The flag for capture by green player.','',{player2_flag_location[1]-2, player2_flag_location[2]})
				showMarker(500, 1,'The flag for player 2!','',player2_flag_location)
				showMarker(1000, -1,'The flag for player 3!','',player3_flag_location)
			end
			if humanFaction() == 2 then
				-- addCellMarker(-1,'The flag for capture by blue player.','',{player3_flag_location[1]+2, player3_flag_location[2]})
				showMarker(1000, -1,'The flag for player 2!','',player2_flag_location)
				showMarker(500, 2,'The flag for player 3!','',player3_flag_location)
			end

			player1_in_home_zone = 0
			player1_attacker_command = 'GoHome'
			player2_status = 'None'
			player3_status = 'None'
			player2_has_flag_unit = 0
			player3_has_flag_unit = 0
			flag_bearers_status = 0
			destroyed_home_base = 'None'
			ScenarioStatus = 'Game'
			showMessage('Brief1', 'CaptureFlag')
			timer_event1 = startTimerEvent()
			timer_event2 = startTimerEvent()
			timer1_status = 'None'
		</startup>
		<unitCreated>
		</unitCreated>
		<unitDied>
			if lastDeadUnit() == player3_has_flag_unit and player3_has_flag_unit > 0 then
				addCellMarker(2,'Catch this flag!','',player2_flag_location)
				if humanFaction() == 1 then
					-- addCellMarker(-1,'The flag for capture by green player.','',{player2_flag_location[1]-2, player2_flag_location[2]})
				end
				if humanFaction() == 2 then
					clearDisplayText()
				end
				unhighlightUnit(player3_has_flag_unit)
				player3_has_flag_unit = 0
				player3_status = 'Start'
				flag_bearers_status = flag_bearers_status + 1
				if flag_bearers_status == 2 then
					player2_status = 'Won'
					if humanFaction() == 1 then
						setDisplayText('PlayerWins')
					end
					if humanFaction() == 2 then
						setDisplayText('PlayerLoses')
					end
					setPlayerAsWinner(1)
					ScenarioStatus = 'End'
					endGame()
				end
			elseif lastDeadUnit() == player2_has_flag_unit and player2_has_flag_unit > 0 then
				addCellMarker(1,'Catch this flag!','',player3_flag_location)
				if humanFaction() == 2 then
					-- addCellMarker(-1,'The flag for capture by blue player.','',{player3_flag_location[1]+2, player3_flag_location[2]})
				end
				if humanFaction() == 1 then
					clearDisplayText()
				end
				unhighlightUnit(player2_has_flag_unit)
				player2_has_flag_unit = 0
				player2_status = 'Start'
				flag_bearers_status = flag_bearers_status - 1
				if flag_bearers_status == -2 then
					player3_status = 'Won'
					if humanFaction() == 2 then
						setDisplayText('PlayerWins')
					end
					if humanFaction() == 1 then
						setDisplayText('PlayerLoses')
					end
					setPlayerAsWinner(2)
					ScenarioStatus = 'End'
					endGame()
				end
			end
			if lastDeadUnit() == player3_home or lastDeadUnit() == player2_home then
				if destroyed_home_base == 'None' then
					if ScenarioStatus ~= 'VeryLongGame' then
						ScenarioStatus = 'SomeoneUnfair'
					end
					destroyed_home_base = 'One'
				elseif destroyed_home_base == 'One' then
					destroyed_home_base = 'Both'
					--setPlayerAsWinner(0)
					ScenarioStatus = 'End'
					endGame()
				end
			end
		</unitDied>
		<cellTriggerEvent>
			if triggeredCellEventId() == player2_in_flag_zone_event then
				if ScenarioStatus == 'End' then
					unregisterCellTriggerEvent(triggeredCellEventId())
				else
					if player2_status == 'None' then
						-- registerCellTriggerEventForFactionToLocation is very hard to fulfill with Cell Markers so we need small area
						player2_has_flag_event = registerCellAreaTriggerEventForFactionToLocation(1, player3_flag_small_zone_quad)
						player2_status = 'Start'
					end
					if player2_status == 'Start' then
						if humanFaction() == 2 then
							networkShowMessageForFaction('Player3InFlagZoneWarning','Warning',2)
							showMarker(1000, 2,'The flag for player 3!','',player3_flag_location)
						end
						-- FIXME: there should be something like "if faction 1 is controlled by cpu (globally/"on the any PC") then"
							--givePositionCommand(triggeredCellEventUnitId(), 'move', player2_flag_location)
						-- end
						player2_status = 'FlagZone'
					end
				end
			end
			if triggeredCellEventId() == player3_in_flag_zone_event then
				if ScenarioStatus == 'End' then
					unregisterCellTriggerEvent(triggeredCellEventId())
				else
					if player3_status == 'None' then
						-- ... so we need small area
						player3_has_flag_event = registerCellAreaTriggerEventForFactionToLocation(2, player2_flag_small_zone_quad)
						player3_status = 'Start'
					end
					if player3_status == 'Start' then
						if humanFaction() == 1 then
							networkShowMessageForFaction('Player2InFlagZoneWarning','Warning',1)
							showMarker(1000, 1,'The flag for player 2!','',player2_flag_location)
						end
						-- FIXME: ..."if faction 2 is controlled by cpu then"
							--givePositionCommand(triggeredCellEventUnitId(), 'move', player3_flag_location)
						-- end
						player3_status = 'FlagZone'
					end
				end
			end
			if triggeredCellEventId() == player2_has_flag_event then
				if ScenarioStatus == 'End' then
					unregisterCellTriggerEvent(triggeredCellEventId())
				else
					if player2_status == 'FlagZone' then
						removeCellMarker(1, player3_flag_location)
						if humanFaction() == 2 then
							-- removeCellMarker(-1, {player3_flag_location[1]+2, player3_flag_location[2]})
						end
						player2_has_flag_unit = triggeredCellEventUnitId()
						highlightUnit(player2_has_flag_unit,flag_bearer_selection_radius,flag_bearer_selection_thickness,flag_bearer_selection_color)
						player2_touch_home_event = registerCellTriggerEventForUnitToUnit(triggeredCellEventUnitId(), player2_home)
						if humanFaction() == 1 then
							setDisplayText('PlayerHeadHome')
						end
						if humanFaction() == 2 then
							networkShowMessageForFaction('Player3HasFlagWarning','Crisis',2)
						end
						-- FIXME: there should be something like "if faction 2 is controlled by cpu then"
							player3_attackers=getUnitsForFaction(2,'attack',getUnitCurrentField(triggeredCellEventUnitId()))
							for i=1, #player3_attackers do
								giveAttackCommand(player3_attackers[i],triggeredCellEventUnitId())
								--print('Unit id: ' .. player3_attackers[i] .. ' is attacking unit id: ' .. triggeredCellEventUnitId())
							end
						-- end
						player2_status = 'HasFlag'
					end
				end
			end
			if triggeredCellEventId() == player3_has_flag_event then
				if ScenarioStatus == 'End' then
					unregisterCellTriggerEvent(triggeredCellEventId())
				else
					if player3_status == 'FlagZone' then
						removeCellMarker(2, player2_flag_location)
						if humanFaction() == 1 then
							-- removeCellMarker(-1, {player2_flag_location[1]-2, player2_flag_location[2]})
						end
						player3_has_flag_unit = triggeredCellEventUnitId()
						highlightUnit(player3_has_flag_unit,flag_bearer_selection_radius,flag_bearer_selection_thickness,flag_bearer_selection_color)
						player3_touch_home_event = registerCellTriggerEventForUnitToUnit(triggeredCellEventUnitId(), player3_home)
						if humanFaction() == 2 then
							setDisplayText('PlayerHeadHome')
						end
						if humanFaction() == 1 then
							networkShowMessageForFaction('Player2HasFlagWarning','Crisis',1)
						end
						-- FIXME: ..."if faction 1 is controlled by cpu then"
							player2_attackers=getUnitsForFaction(1,'attack',getUnitCurrentField(triggeredCellEventUnitId()))
							for i=1, #player2_attackers do
								giveAttackCommand(player2_attackers[i],triggeredCellEventUnitId())
								--print('Unit id: ' .. player2_attackers[i] .. ' is attacking unit id: ' .. triggeredCellEventUnitId())
							end
						-- end
						player3_status = 'HasFlag'
					end
				end
			end
			if triggeredCellEventId() == player2_touch_home_event and player2_status == 'HasFlag' then
				if ScenarioStatus == 'End' then
					unregisterCellTriggerEvent(triggeredCellEventId())
				else
					player2_status = 'Won'
					unhighlightUnit(player2_has_flag_unit)
					player2_has_flag_unit = 0
					if humanFaction() == 1 then
						setDisplayText('PlayerWins')
					end
					if humanFaction() == 2 then
						setDisplayText('PlayerLoses')
					end
					setPlayerAsWinner(1)
					ScenarioStatus = 'End'
					endGame()
					unregisterCellTriggerEvent(triggeredCellEventId())
				end
			elseif triggeredCellEventId() == player3_touch_home_event and player3_status == 'HasFlag' then
				if ScenarioStatus == 'End' then
					unregisterCellTriggerEvent(triggeredCellEventId())
				else
					player3_status = 'Won'
					unhighlightUnit(player3_has_flag_unit)
					player3_has_flag_unit = 0
					if humanFaction() == 2 then
						setDisplayText('PlayerWins')
					end
					if humanFaction() == 1 then
						setDisplayText('PlayerLoses')
					end
					setPlayerAsWinner(2)
					ScenarioStatus = 'End'
					endGame()
					unregisterCellTriggerEvent(triggeredCellEventId())
				end
			end
			if triggeredCellEventId() == player1_in_home3_zone_event or triggeredCellEventId() == player1_in_home2_zone_event then
				if ScenarioStatus == 'End' or ScenarioStatus == 'SomeoneUnfair' or ScenarioStatus == 'VeryLongGame' then
					unregisterCellTriggerEvent(triggeredCellEventId())
				else
					if triggeredCellEventUnitId() ~= player1_in_home_zone then
						disableAi(0)
						timer1_status = 'None'
						arbiter_attackers=getUnitsForFaction(0,'attack',getUnitCurrentField(triggeredCellEventUnitId()))
						for i=1, #arbiter_attackers do
							if player1_attacker_command == 'GoHome' then
								givePositionCommand(arbiter_attackers[i], 'move', startLocation(0))
								player1_attacker_command = 'GoToBetweenBases'
							elseif player1_attacker_command == 'GoToBetweenBases' then
								givePositionCommand(arbiter_attackers[i], 'move', {113,57})
								player1_attacker_command = 'GoToBetweenBases2'
							elseif player1_attacker_command == 'GoToBetweenBases2' then
								givePositionCommand(arbiter_attackers[i], 'move', {113,57})
								player1_attacker_command = 'GoToBetweenFlags'
							elseif player1_attacker_command == 'GoToBetweenFlags' then
								givePositionCommand(arbiter_attackers[i], 'move', {110,234})
								player1_attacker_command = 'GoToLeftSide'
							elseif player1_attacker_command == 'GoToLeftSide' then
								if player3_status ~= 'FlagZone' then
									givePositionCommand(arbiter_attackers[i], 'move', {20,128})
								else
									givePositionCommand(arbiter_attackers[i], 'move', startLocation(0))
								end
								player1_attacker_command = 'GoToRightSide'
							elseif player1_attacker_command == 'GoToRightSide' then
								if player2_status ~= 'FlagZone' then
									givePositionCommand(arbiter_attackers[i], 'move', {235,128})
								else
									givePositionCommand(arbiter_attackers[i], 'move', startLocation(0))
								end
								player1_attacker_command = 'GoHome'
							end
						end
						givePositionCommand(triggeredCellEventUnitId(), 'move', startLocation(0))
						player1_in_home_zone = triggeredCellEventUnitId()
					end
				end
			end
		</cellTriggerEvent>
		<timerTriggerEvent>
			-- displayFormattedText('BlueStatus=%s, GreenStatus=%s, ScenarioStatus=%s, FlagBearersStatus=%s', player2_status, player3_status, ScenarioStatus, flag_bearers_status);
			if triggeredTimerEventId() == timer_event1 then
				if timer1_status == 'Reset' and timerEventSecondsElapsed(triggeredTimerEventId()) == 10 then
					resetTimerEvent(triggeredTimerEventId())
				elseif timerEventSecondsElapsed(triggeredTimerEventId()) >= 40 then
					if getAiEnabled(0) == 0 then
						enableAi(0)
					end
					timer1_status = 'Reset'
					resetTimerEvent(triggeredTimerEventId())
				end
				if ScenarioStatus == 'End' then
					if getAiEnabled(0) == 0 then
						enableAi(0)
					end
					stopTimerEvent(triggeredTimerEventId())
				end
			end
			if triggeredTimerEventId() == timer_event2 then
				if timerEventSecondsElapsed(triggeredTimerEventId()) >= 1200 then
					ScenarioStatus = 'VeryLongGame'
					stopTimerEvent(triggeredTimerEventId())
				end
				if ScenarioStatus == 'End' then
					stopTimerEvent(triggeredTimerEventId())
				end
			end
		</timerTriggerEvent>
	</scripts>
</scenario>
