scope Initialization initializer ini
	import TerrainData
	import Cinematic
	import AI
	import Units
	import Game
	import Players
	import DummyUnitStack

	constant string ORB_FX = "Abilities\\Weapons\\ChimaeraAcidMissile\\ChimaeraAcidMissile.mdl"
	constant boolean SINGLEPLAYER_ADD_AI = true
	constant integer PILLAR_GROUP_COUNT = 7
	constant integer PILLAR_GROUP_COUNT_BEAST_SPAWN = 5
	constant integer PILLARS_PER_GROUP  = 3
	constant integer OBELISK_ID  = 'h007'

	constant integer HUNTER_STARTING_GOLD = 1000

	constant real PHASE = -1. * bj_PI / 2.
	constant real OBELISK_FACING_PHASE = bj_PI / 4.
	constant real GROUP_SEPARATION_ANGLE = bj_PI / 24.
	constant real RADIUS  = 550.
	constant real RADIUS2 = 450.
	constant real OBELISK_PATH_RADIUS = 32.
	constant real PATH_ITERATE_SIZE = 8.
	constant real ORB_HEIGHT = 100.

	group grp = CreateGroup()

	unit tempUnit
	real tempX
	real tempY
	real tempRadius

	trigger diaClick
	dialog dialo
	button array btns
	timer voteTimer      = CreateTimer()
	integer votesCounted = 0

	function removeWalkability takes nothing returns nothing
		real i = tempX - OBELISK_PATH_RADIUS
		real j

		loop
			exitwhen i > tempX + OBELISK_PATH_RADIUS
			j = tempY - OBELISK_PATH_RADIUS
			loop
				exitwhen j > tempY + OBELISK_PATH_RADIUS
				if TerrainData.distanceBetweenXY(tempX, i, tempY, j) <= OBELISK_PATH_RADIUS then
					SetTerrainPathable(i, j, PATHING_TYPE_WALKABILITY, false)
				end
				j = j + PATH_ITERATE_SIZE
			end
			i = i + PATH_ITERATE_SIZE
		end
	end

	function startC takes nothing returns boolean
		ClearTextMessages()
		Cinematic.start()
		return false
	end

	function endVotes takes nothing returns nothing
		integer n

		ClearTextMessages()

		if Game.roundsToWin == 0 then
			DisplayTextToPlayer(Players.locl, 0., 0., "|cffffcc00[Voting]|r No votes registered, reverting to |cffffcc004|r rounds to win (Best-of-7)")
			Game.roundsToWin = 4
		else
			n = R2I(I2R(Game.roundsToWin) / I2R(votesCounted) + .5)
			Game.roundsToWin = n
			DisplayTextToPlayer(Players.locl, 0., 0., "|cffffcc00[Voting]|r Voting finished; |cffffcc00" + I2S(n) + "|r rounds to win (Best-of-" + I2S(n*2 - 1) + ")")
		end

		// Now cinematic
		TimerStart(voteTimer, 3., false, function startC)
	end

	function diaA takes nothing returns nothing
		integer index  = 0
		integer btnVal = 0
		button b = GetClickedButton()

		if b == btns[0] then
			btnVal = 3
		elseif b == btns[1] then
			btnVal = 5
		elseif b == btns[2] then
			btnVal = 7
		elseif b == btns[3] then
			btnVal = 9
		end

		votesCounted++
		Game.roundsToWin = Game.roundsToWin + btnVal
		if votesCounted >= Players.humanCount then
			PauseTimer(voteTimer)

			loop
				exitwhen index > Players.LAST_BEAST

				DialogDisplay(Players.fromId[index], dialo, false)

				index++
			end

			endVotes()
		end
	end

	function afterDelay takes nothing returns nothing
		integer index = 0

		DialogClear(dialo)
		DialogSetMessage(dialo, "Round wins required for victory")
		btns[0] = DialogAddButton(dialo, "3", '3')
		btns[1] = DialogAddButton(dialo, "5", '5')
		btns[2] = DialogAddButton(dialo, "7", '7')
		btns[3] = DialogAddButton(dialo, "9", '9')
		TriggerRegisterDialogEvent(diaClick, dialo)

		// Begin voting
		DisplayTextToPlayer(Players.locl, 0., 0., "|cffffcc00[Voting]|r Please wait up to |cffffcc0010|r seconds while players vote for round count.")
		TimerStart(voteTimer, 10., false, function endVotes)

		loop
			exitwhen index > Players.LAST_BEAST

			DialogDisplay(Players.fromId[index], dialo, true)

			index++
		end
	end

	function ini takes nothing returns nothing
		integer i
		integer j
		unit iter
		unit dum
		real ang

		// Setup dialog trigger
		diaClick = CreateTrigger()
		dialo    = DialogCreate()

		TriggerAddAction(diaClick, function diaA)

		SetTimeOfDayScale(0.)
		TimerStart(CreateTimer(), Game.START_DELAY, false, function afterDelay)

		// Remove markers (player 12)
		GroupEnumUnitsOfPlayer(grp, Players.fromId[Players.MARKERS], null)
		loop
			iter = FirstOfGroup(grp)
			exitwhen iter == null
			RemoveUnit(iter)
			GroupRemoveUnit(grp, iter)
		end

		// Add AI in singleplayer mode
		if (Players.playingCount - Players.aiCount) == 1 then
			Game.singlePlayer = true
		end
		if Players.playingCount == 1 and SINGLEPLAYER_ADD_AI then
			if Players.playing[0] or Players.playing[1] or Players.playing[2] then
				AI.addPlayer(Players.fromId[Players.FIRST_BEAST])
				Players.playing[Players.FIRST_BEAST] = true
			else
				AI.addPlayer(Players.fromId[Players.FIRST_HUNTER])
				Players.playing[Players.FIRST_HUNTER] = true
			end
		end

		// Rune A terrain
		i = 0
		loop
			exitwhen i >= PILLAR_GROUP_COUNT
			j = 0
			loop
				exitwhen j >= PILLARS_PER_GROUP
				ang = PHASE + 2. * bj_PI * i / PILLAR_GROUP_COUNT - GROUP_SEPARATION_ANGLE + j * GROUP_SEPARATION_ANGLE
				tempX = TerrainData.RUNE_1_X + RADIUS * Cos(ang)
				tempY = TerrainData.RUNE_1_Y + RADIUS * Sin(ang)
				tempUnit = CreateUnit(Players.fromId[Players.PASSIVE], OBELISK_ID, tempX, tempY, (ang + OBELISK_FACING_PHASE)*bj_RADTODEG)
				if j == R2I(PILLARS_PER_GROUP / 2) then
					dum = DummyUnitStack.get()
					SetUnitX(dum, tempX)
					SetUnitY(dum, tempY)
					SetUnitFlyHeight(dum, ORB_HEIGHT, 0.)
					AddSpecialEffectTarget(ORB_FX, dum, "overhead")
				end
				tempRadius = OBELISK_PATH_RADIUS
				ExecuteFunc("removeWalkability")
				j++
			end
			i++
		end

		// Rune B terrain
		i = 0
		loop
			exitwhen i >= PILLAR_GROUP_COUNT
			j = 0
			loop
				exitwhen j >= PILLARS_PER_GROUP
				ang = PHASE + 2. * bj_PI * i / PILLAR_GROUP_COUNT - GROUP_SEPARATION_ANGLE + j * GROUP_SEPARATION_ANGLE
				tempX = TerrainData.RUNE_2_X + RADIUS2 * Cos(ang)
				tempY = TerrainData.RUNE_2_Y + RADIUS2 * Sin(ang)
				tempUnit = CreateUnit(Players.fromId[Players.PASSIVE], OBELISK_ID, tempX, tempY, (ang + OBELISK_FACING_PHASE)*bj_RADTODEG)
				if j == R2I(PILLARS_PER_GROUP / 2) then
					dum = DummyUnitStack.get()
					SetUnitX(dum, tempX)
					SetUnitY(dum, tempY)
					SetUnitFlyHeight(dum, ORB_HEIGHT, 0.)
					AddSpecialEffectTarget(ORB_FX, dum, "overhead")
				end
				tempRadius = OBELISK_PATH_RADIUS
				ExecuteFunc("removeWalkability")
				j++
			end
			i++
		end

		// Beast Spawn terrain
		i = 0
		loop
			exitwhen i >= PILLAR_GROUP_COUNT_BEAST_SPAWN
			j = 0
			loop
				exitwhen j >= PILLARS_PER_GROUP
				ang = PHASE + 2. * bj_PI * i / PILLAR_GROUP_COUNT_BEAST_SPAWN - GROUP_SEPARATION_ANGLE + j * GROUP_SEPARATION_ANGLE
				tempX = TerrainData.BEAST_SPAWN_X + RADIUS * Cos(ang)
				tempY = TerrainData.BEAST_SPAWN_Y + RADIUS * Sin(ang)
				tempUnit = CreateUnit(Players.fromId[Players.PASSIVE], OBELISK_ID, tempX, tempY, (ang + OBELISK_FACING_PHASE)*bj_RADTODEG)
				if j == R2I(PILLARS_PER_GROUP / 2) then
					dum = DummyUnitStack.get()
					SetUnitX(dum, tempX)
					SetUnitY(dum, tempY)
					SetUnitFlyHeight(dum, ORB_HEIGHT, 0.)
					AddSpecialEffectTarget(ORB_FX, dum, "overhead")
				end
				tempRadius = OBELISK_PATH_RADIUS
				ExecuteFunc("removeWalkability")
				j++
			end
			i++
		end

		Units.artifact  = CreateItem(Units.ARTIFACT_ID, TerrainData.RUNE_1_X, TerrainData.RUNE_1_Y)
		Units.artifact2 = CreateItem(Units.ARTIFACT_ID, TerrainData.RUNE_2_X, TerrainData.RUNE_2_Y)

		// add AI players
		i = 0
		loop
			exitwhen i >= Players.COUNT

			if Players.aiControlled[i] then
				AI.addPlayer(Players.fromId[i])
			end
			i++
		end

		// give gold to hunters
		i = 0
		loop
			exitwhen i >= Players.FIRST_BEAST

			SetPlayerState(Players.fromId[i], PLAYER_STATE_RESOURCE_GOLD, HUNTER_STARTING_GOLD)

			i++
		end
	end
endscope
