//dsaltow1 = dstaleh1
//dsaltow2 = dstaleh2
//dsaltow3 = dstaleh3
//dsaltow4 = dstaleh4
//dsaltdi4 = dstalezd
//dsaltdi3 = dstalezd
//dsaltdi2 = dstalezd
//dsaltdi1 = dstalezd

local function SlopeDiv(num,den)
	local ans
	num = $<<(16-FRACBITS)
	den = $<<(16-FRACBITS)
	if (den < 512)
		return 2048
	end
	ans = (num<<3)/(den>>8)
	return ans <= 2048 and ans or 2048	
end

//im putting this in its own file because it is huge...
// https://wiki.srb2.org/wiki/A_BrakLobShot

/*
	A_BrakLobShot translated to lua
	
	var1 - object type
	var2:
		lower 16 bits: height offset
		upper 16 bits: if 0, aim 1/3 of the way, else, aim directly at the target

*/

rawset(_G,"LUA_LobShot",function(actor,target,var1,var2)
	local v = 0 //velocity
	local a1,a2,aToUse = 0,0,0 //velocity squared
	local g = 0 //gravity
	local x = 0 //horizontal difference
	local x_int = 0 //x in int
	local z = 0 //vertical difference
	local z_int = 0 //z in int
	local intHypot = 0 //x^2 + y^2
	local fixedHypot = 0
	local theta = 0 //angle of attack
	local shottype = 100
	local shot = 0 //object to shoot
	local newTargetX = 0 //if not aiming directly
	local newtargetY = 0 //if not aiming directly
	local locvar1 = var1
	local locvar2 = var2 & 0x0000FFFF
	local aimingDirectly = var2 & 0xFFFF0000
	
	if not (target) then
		return
	end
	
	if (actor.subsector.sector.gravity) then
		g = FixedMul(gravity,(FixedDiv(actor.subsector.sector.gravity>>FRACBITS,1000)))
	else
		g = gravity
	end
	
	//get the dist
	x = FixedHypot(target.x-actor.x,target.y-actor.y)
	if not (aimingDirectly) then
		//distance should be a third of the way
		x = FixedDiv(x,3<<FRACBITS)
		newTargetX = actor.x + P_ReturnThrustX(actor,actor.angle,x)
		newTargetX = actor.y + P_ReturnThrustY(actor,actor.angle,y)
		x = FixedHypot(newTargetX-actor.x, newTargetY-actor.y)
		//look up height difference between actor and ground 1/3 of the way
		z = P_FloorzAtPos(newTargetX,newTargetY,target.z,target.height) - (actor.z+FixedMul(locvar2*FU,actor.scale))
	else
		z = target.z-(actor.z+FixedMul(locvar2*FU,actor.scale))
	end
	
	//get x^2 + y^2
	x_int = x>>FRACBITS
	z_int = z>>FRACBITS
	intHypot = (x*x)+(z*z)
	fixedHypot = intHypot*256
	
	a1 = FixedMul(g,y+fixedHypot)
	a2 = FixedMul(g,y-fixedHypot)
	
	//which one isnt an imaginary number...
	if (a1 < 0 or a2 < 0) then
		//erm,, both are -, so something went wrong!!
		if (a1 < 0 and a2 < 0) then
			return
		end
		
		aToUse = max(a1,a2)
	else
		aToUse = min(a1,a2)
	end
	v = FixedSqrt(aToUse)
	theta = tantoangle[SlopeDiv(aToUse,FixedMul(g,x))]
	
	//now that all the boring math is outta the way, lets finally
	//spawn the object!
	if (locvar1 <= 0) then
		shottype = MT_CANNONBALL
	else
		shottype = locvar1
	end
	shot = P_SpawnMobj(actor.x,actor.y,actor.z + FixedMul(locvar2*FU, actor.scale), shottype)
	if (shot.info.seesound) then
		S_StartSound(shot,shot.info.seesound)
	end
	shot.tracer = actor
	
	shot.angle = actor.angle
	
	shot.momx = FixedMul(FixedMul(v,cos(theta)), cos(shot.angle))
	shot.momy = FixedMul(FixedMul(v,cos(theta)), sin(shot.angle))
	shot.momz = FixedMul(v,sin(theta))
	//i recreated this whole function just for this return lel
	return shot
end)