function init()
	dumper = FindVehicle("dumper")
	
	dumperMain = FindJoints("dumper_main")
	
	dumperMain_spd = 0.5
	
	dumperMain_min, dumperMain_max = GetJointLimits(dumperMain[1])
	
	doDump = nil
	doRecover = nil
	
	dumped = nil
	
	uiCaption = {'Dumper Commands'}
	uiHotkeys = {{'Z', 'Dump'}, {'X', 'Recover'}}
end

function draw()
	if GetPlayerVehicle() == dumper then
		local kw = 0
		local vw = 0
		for i=1, #uiHotkeys do
			UiFont("bold.ttf", 22)
			local w0 = UiGetTextSize(uiHotkeys[i][1])
			UiFont("regular.ttf", 22)
			local w1 = UiGetTextSize(uiHotkeys[i][2])
			kw = math.max(kw,w0)
			vw = math.max(vw,w1)
		end
	
		local uiwidth = kw + vw + 120
		local uiheight = #uiHotkeys*26 + 55
		if uiwidth <= 270 then uiwidth = 270 end
		
		UiAlign("left")
		UiTranslate(UiWidth()-uiwidth-20, 20)
		UiColor(0,0,0,0.4)
		UiImageBox("ui/common/box-solid-6.png", uiwidth, uiheight, 6, 6)
		UiTranslate(uiwidth*0.5, 24)
		UiAlign("center middle")
		UiColor(1, 1, 1)
		UiFont("bold.ttf", 26)
		UiText(uiCaption[1])
		UiTranslate(-45-(uiwidth-270)*0.25, 36)
		for i=1, #uiHotkeys do
			UiFont("bold.ttf", 22)
			UiAlign("right")
			UiText(uiHotkeys[i][1])
			UiTranslate(20, 0)
			UiFont("regular.ttf", 22)
			UiAlign("left")
			UiText(uiHotkeys[i][2])
			UiTranslate(-20, 26)
		end
	end
end

function MotorInitialize()
	for i=1,#dumperMain do
		SetJointMotor(dumperMain[i], 0)
	end
end

function Dump()
	for i=1,#dumperMain do
		SetJointMotorTarget(dumperMain[i], dumperMain_max, -dumperMain_spd)
	end
end

function Recover()
	for i=1,#dumperMain do
		SetJointMotorTarget(dumperMain[i], dumperMain_min, dumperMain_spd)
	end
end

function GetPlayerCommand()
	if InputPressed("z") then
		doDump = true
		doRecover = false
	elseif InputPressed("x") then
		doDump = false
		doRecover = true
	end
end

function GetDumperStatus()
	dumperStatus = GetJointMovement(dumperMain[1])
	
	if dumperStatus >= dumperMain_max - 0.1 then
		dumped = true
		recovered = false
	elseif dumperStatus <= dumperMain_min + 0.1 then
		dumped = false
		recovered = true
	else
		dumped = false
		recovered = false
	end
end


function tick(dt)
	if GetPlayerVehicle() == dumper then
		GetDumperStatus()
		GetPlayerCommand()
		if doDump then
			Dump()
			if dumped then
				MotorInitialize()
			end
		elseif doRecover then
			Recover()
			if recovered then
				MotorInitialize()
			end
		else
			MotorInitialize()
		end
	end
		
	--DebugPrint(dumperStatus)
end