elimination = {
	name = GAMEMODES.elimination,
}
for k, v in pairs(GAMEMODES_PARAMETERS[GAMEMODES.elimination]) do elimination[k] = v end

function elimination:Init()
    self:FillParamsAndStats()
	banger:Init(false)
	eliminateTimer = 0
	SetFloat("level.damage-mp", 1.2)
	SetInt('level.eliminate', 0)
	required_alive_places = 3
    SetInt("level.LAPS", 8)
end

function elimination:FillParamsAndStats()
	self.EXPLOSION_POWER = HasKey("level.EXPLOSION_POWER") and GetInt("level.EXPLOSION_POWER") or self.EXPLOSION_POWER
	self.ELIMINATION_TIME = HasKey("level.ELIMINATION_TIME") and GetInt("level.ELIMINATION_TIME") or self.ELIMINATION_TIME
end

function elimination:FirstTick()
	banger:FirstTick()
end

function elimination:GameOver()
	banger:GameOver()
end

function elimination:Save()
	banger:Save()
end

function elimination:Update(dt)
	banger:Update(dt)

	if HasKey("level.gameover") then return end
	local num_alive = GetInt("level.numalive")
	if num_alive <= 1 or (GetVehicleHealth(banger.playerVeh) == 0 and GetInt("level.playerplace") <= required_alive_places and num_alive <= required_alive_places) then
		SetString("level.gameover.type", "loc@WIN_UPPERCASE")
        if GetBool("level.debug_enabled") then
            UnregisterListener(EVENTS.AI_RUBBERBAND, "CanOut")
        end
		TriggerEvent(EVENTS.GAME_OVER, 'win')
		return
	end
	if GetFloat("level.time") == 0 then return end

	eliminateTimer = eliminateTimer + dt
	if eliminateTimer >= self.ELIMINATION_TIME then

        local veh = GetInt('level.eliminate')
        if veh > 0 then
			local center = GetBodyCenterOfMass(GetVehicleBody(veh))
			center = TransformToParentPoint(GetVehicleTransform(veh), center)
			Explosion(center, self.EXPLOSION_POWER)
			SetVehicleHealth(veh, 0)
			SetVehicleEngineHealth(veh, 0)
			utils:SetUnbreakable(veh, false, true)
            eliminateTimer = 0
			if veh == banger.playerVeh and num_alive <= required_alive_places then
				SetString("level.gameover.type", "loc@WIN_UPPERCASE")
                if GetBool("level.debug_enabled") then
                    UnregisterListener(EVENTS.AI_RUBBERBAND, "CanOut")
                end
				TriggerEvent(EVENTS.GAME_OVER, 'win')
			end
        end
	end
	SetFloat("level.elimination_time_left", self.ELIMINATION_TIME-eliminateTimer)
end
