function init()
	RegisterTool("rifle_old", "rifle_old", "MOD/data/vox/tool/rifle.vox", 4)
	SetBool("game.tool.rifle_old.enabled", true)
	SetInt("game.tool.rifle_old.ammo", 12)

	shootSnd = {}
	for i=0, 3 do
		shootSnd[i] = LoadSound("tools/rifle"..i..".ogg")
	end

	scopeOn = false
end

function rndVec(length)
	local v = VecNormalize(Vec(math.random(-100,100), math.random(-100,100), math.random(-100,100)))
	return VecScale(v, length)
end

local recoilTimer = 0.0
local recoilAnimationTimer = 0.0
local recoil = 2.0

local lightTimer = 0.0

local shootPos = Vec()

function RifleShoot()
	local currentAmmo = GetInt("game.tool.rifle_old.ammo")
	if currentAmmo <= 0 then
		return
	end
	local toolTrans = GetCameraTransform()

	shootPos = TransformToParentPoint(toolTrans, Vec(0.1, -0.1, -1.0))
	if scopeOn then
		shootPos = TransformToParentPoint(toolTrans, Vec(0.0, -0.0, -1.0))
	end
	local d = VecAdd(TransformToParentVec(toolTrans, Vec(0,0,-1)), rndVec(0.001))

	Shoot(shootPos, d, "bullet", 0.4)

	PlaySound(shootSnd[math.random(0, #shootSnd)])

	recoilTimer = recoil
	recoilAnimationTimer = 1.0
	lightTimer = 0.07
	SetInt("game.tool.rifle_old.ammo", currentAmmo - 1)
end

local x = 0.3
local y = 0.1
local z = -0.3

local updateScopeTimer = 0.0
local updateScope = 0.5
local scopeDist = 30

function tick(dt)
	if GetString("game.player.tool") == "rifle_old" then

		SetToolTransform(Transform(Vec(x, y, z)))

		if recoilTimer > 0 then
			if  recoilAnimationTimer > 0.0 then
				local t = Transform()
				local sin = math.sin(math.pi * recoilAnimationTimer)
				t.pos = VecLerp(Vec(x, y, z), Vec(x + 0.2, y + 0.2, z + 0.1), sin)
				t.rot = QuatRotateQuat(t.rot, QuatEuler(20 * sin, - 20 * sin, 0))
				SetToolTransform(t)
			end

			if lightTimer > 0.0 then
				lightTimer = lightTimer - dt
				PointLight(shootPos, 1, 0.7, 0.5, 2)
			end
		end

		if InputPressed("lmb") and recoilTimer <= 0 then
			RifleShoot()
		end

		if InputPressed("rmb") and GetPlayerPickShape() == 0 and GetPlayerPickBody() == 0 then
			scopeOn = not scopeOn
		end

		if scopeOn then
			SetCameraFov(140)
			local t = GetPlayerCameraTransform()
			t.pos = TransformToParentPoint(t, Vec(0, 0, -1))
			t.rot = QuatRotateQuat(t.rot, QuatAxisAngle(Vec(1, -0.5, 0), math.sin(math.pi * recoilAnimationTimer) * 5))
			if updateScopeTimer >= updateScope then
				_, scopeDist = QueryRaycast(t.pos, QuatRotateVec(t.rot, Vec(0, 0, -1)), 100, 0.1)
				updateScopeTimer = 0.0
			else
				updateScopeTimer = updateScopeTimer + dt
			end
			SetCameraDof(scopeDist, 0.5)
			--SetPlayerCameraOffsetTransform(t)
			SetCameraTransform(t)
		end
	else
		scopeOn = false
	end

	if recoilTimer > 0 then
		if  recoilAnimationTimer > 0.0 then
			recoilAnimationTimer = recoilAnimationTimer - 2 * dt
		end
		recoilTimer = recoilTimer - dt
	end
end

function draw()
	if scopeOn then
		local scopePath = "MOD/data/images/scope.png"
		local imgW, _ = UiGetImageSize(scopePath)
		local uiW = UiWidth()

		UiPush()
			UiTranslate(UiCenter(), UiMiddle())
			UiAlign("center middle")
			UiImage("MOD/data/images/scope.png")
		UiPop()

		if uiW > imgW then
			local rectWidth = (uiW - imgW) / 2
			local rectHeight = UiHeight()
			UiPush()
				UiColor(0, 0, 0)
				UiRect(rectWidth, rectHeight)
				UiTranslate(uiW - rectWidth, 0)
				UiRect(rectWidth, rectHeight)
			UiPop()
		end
	end
end