_Msg("Physgun Upright mode enhancment by JetBoom\n")


-- This is <NOT> a core function. It's here so you can use it
-- in gamemodes, ect. DO NOT change this function. Instead, 
-- copy + paste it into your own script and edit it your liking, thus overiding this.
-- If you release your script wich uses this, then you must include this file too.
-- Returns true if the player can upright the specified entity
-- Returns false if the player can NOT upright the specified entity
-- Note that the player doesn't actually havto be holding the prop. They can also
-- simply be holding fire down while aiming at it.
function onPhysUpright(player, entity)
	local mytype = _EntGetType(entity)
	if(string.find(mytype, "prop_") ~= nil
		and mytype ~= "prop_detail"
		and mytype ~= "prop_static"
		and mytype ~= "prop_thumper") then
		return true
	end
	return false
end

-----------------------------------------------
-- No editing/copy/pasting needed below this --
-----------------------------------------------

function physup_eventKeyPressed(userid, in_key)
	if(not _PlayerInfo(userid, "alive")) then return; end
	if(in_key == IN_SPEED and _EntGetType(_PlayerGetActiveWeapon(userid)) == "weapon_physgun" and _PlayerIsKeyDown(userid, IN_ATTACK)) then
		_TraceLine(_PlayerGetShootPos(userid), _PlayerGetShootAng(userid), 4064, userid)
		if(_TraceHitNonWorld()) then
			PhysUpright(userid, _TraceGetEnt())
		end
	end
end
HookEvent("eventKeyPressed", physup_eventKeyPressed)

function PhysUpright(player, entity)
	if(not onPhysUpright(player, entity)) then return; end
	local rot = _EntGetAng(entity)
	if(rot == nil) then return; end
	rot.z = 0
	_EntSetAng(entity, rot)
end
