scope HeroDeath initializer i
	import Units
	import Cinematic
	import RegisterPlayerUnitEvent
	import TextTag
	import Players
	import Statistics
	import Game

	native UnitAlive takes unit u returns boolean

	constant integer CONTRACT_ID = 'I002'

	constant integer GOLD_BONUS           = 30
	constant integer GOLD_BONUS_LESSER    = 15
	constant integer MAJOR_CONTRACT_BONUS = 50
	constant integer MINOR_CONTRACT_BONUS = 25

	timer time = CreateTimer()

	function noMoreUnits takes integer id returns boolean
		return not (UnitAlive(Units.hero[id]) or UnitAlive(Units.subHero[id]))
	end

	function a takes nothing returns nothing
		unit u = GetTriggerUnit()
		player p = GetOwningPlayer(u)
		integer id = GetPlayerId(p)

		if (not Cinematic.enabled) and (u == Units.hero[id] or u == Units.subHero[id]) and Game.roundEnabled then
			if id >= Players.FIRST_BEAST then
				Statistics.hunterKills++
				Statistics.beastsAlive--

				// bounty
				unit k = GetKillingUnit()
				player o = k.getOwner()
				integer t = GetUnitTypeId(u)
				integer bounty = 0
				if t == Units.BEAST_ID then
					bounty += GOLD_BONUS
					if UnitHasItemOfTypeBJ(k, CONTRACT_ID) then
						bounty += MAJOR_CONTRACT_BONUS
					end
				elseif t == Units.LESSER_BEAST_ID then
					bounty += GOLD_BONUS_LESSER
					if UnitHasItemOfTypeBJ(k, CONTRACT_ID) then
						bounty += MINOR_CONTRACT_BONUS
					end
				end
				SetPlayerState(o, PLAYER_STATE_RESOURCE_GOLD, GetPlayerState(o, PLAYER_STATE_RESOURCE_GOLD) + bounty)
				TextTag.bounty(u.getX(), u.getY(), bounty, o)

				if Statistics.beastsAlive < 1 then
					PanCameraToTimed(GetUnitX(u), GetUnitY(u), 0.)
					SetCameraField(CAMERA_FIELD_TARGET_DISTANCE, 1024., 0.)
					Game.hunterWin()
				elseif noMoreUnits(id) then
					DisplayTextToPlayer(p, 0., 0., "|cffffcc00[Game]|r You have been slain! You will revive in the next round.")
				end
			else
				Statistics.beastKills++
				Statistics.huntersAlive--

				if Statistics.huntersAlive < 1 then
					PanCameraToTimed(GetUnitX(u), GetUnitY(u), 0.)
					SetCameraField(CAMERA_FIELD_TARGET_DISTANCE, 1024., 0.)
					Game.beastWin()
				else
					DisplayTextToPlayer(p, 0., 0., "|cffffcc00[Game]|r You have been slain! You will revive in the next round.")
				end
			end
		end
	end

	function i takes nothing returns nothing
		registerPlayerUnitEvent(EVENT_PLAYER_UNIT_DEATH, function a)
	end
endscope
