-- Input detection script from SRB2P

addHook("MobjThinker", function(mo)
	if not mo.player return end
	local p = mo.player

	if not mo.P_inputs then mo.P_inputs = {} end	--setup input

	local buttons = {
	BT_ACCELERATE, BT_BRAKE, BT_DRIFT, BT_ATTACK}

	-- handle these normal buttons

	for i = 1, #buttons
		if p.cmd.buttons & buttons[i]
			if not mo.P_inputs[buttons[i]] then mo.P_inputs[buttons[i]] = 0 end
			mo.P_inputs[buttons[i]] = $+1
		else
			mo.P_inputs[buttons[i]] = 0
		end
	end

	-- handle weaponmask?
	-- get those with buttons 1 through 7

	/*for i = 1,7
		if p.cmd.buttons & BT_WEAPONMASK == i
			if not mo.P_inputs[i] then mo.P_inputs[i] = 0 end
			mo.P_inputs[i] = $+1
		else
			mo.P_inputs[i] = 0
		end
	end*/
	-- ^ not in SRB2K

	-- handle directions
	-- normal directional taps

	local directionfunc = {
		function(p) return p.cmd.forwardmove > 0 end,
		function(p) return p.cmd.forwardmove < 0 end,
		function(p) return p.cmd.sidemove > 0 end,
		function(p) return p.cmd.sidemove < 0 end,
	}

	local dirs = {"up", "down", "right", "left"}	-- < not really ued anymore all that much

	for k,v in ipairs(dirs)
		if directionfunc[k](p)
			if not mo.P_inputs[v] then mo.P_inputs[v] = 0 end
			mo.P_inputs[v] = $+1
		else
			mo.P_inputs[v] = 0
		end
	end

end, MT_PLAYER)