//wall LAUNCH lua
//launches you up when you drive towards a wall
//connected to a steepy slope
//done by Lighto for the set of mapping tools i mdeveloping
//is free to use btw, just give credit, and contact me if you found any issue
//lua written by Lighto
//ps: it wont work on gravflip, yet
if LTB_WALLLAUNCH then return end //loado nce
addHook("ThinkFrame",do
	for p in players.iterate do
		if not p
		or not p.mo
		or (pmo and not p.no.valid) then continue end
		local pmo,mang,wall,slope,slopeang,slopedir
		pmo = p.mo
		mang = R_PointToAngle2(0,0,pmo.momx,pmo.momy)
		slope = pmo.standingslope
		if slope then
			slopedir = slope.xydirection
			slopeang = slope.zangle
		end
		
		if pmo 
		and P_IsObjectOnGround(pmo) then
			p.ltb_walllaunched = nil
			wall = nil
		end
		
		if p.ltb_tobelaunched then
			pmo.momx,pmo.momy = 0,0
			p.speed = 0
			pmo.momz = pmo.ltb_fakemomz 
			pmo.ltb_fakemomz = nil
			p.ltb_tobelaunched = nil
			p.ltb_walllaunched = true
		end
		
		if slope and pmo
		and not(slope.flags & SL_NOPHYSICS)
		and slopeang > 32*ANG1
		and abs(mang - slopedir) <=64*ANG1
		and p.speed > 10*FRACUNIT
		and P_IsObjectOnGround(pmo) then
			local sized = (pmo.radius) + p.speed
			
			searchBlockmap("lines", function(mo,  l)
				local mos = mo.subsector.sector
				
				
				for i = 0, 1 do
					local l2 
					if l and l.valid then
						l2 = sides[l.sidenum[i]]
					end
					if l2
					and l2.valid
					and l2.sector
					and l2.sector.valid
					and l2.sector ~= mos 
					and (l.special < 700
					or l.special > 722)
					and l.special ~= 799
					and l.special == 730
					and(( not l2.sector.f_slope
					and l2.sector.floorheight >= mo.z+mo.height)
					or (l2.sector.f_slope
					and P_GetZAt(l2.sector.f_slope,mo.x+P_ReturnThrustX(nil,mang,mo.radius),mo.y+P_ReturnThrustY(nil,mang,mo.radius)) >= mo.z+mo.height ))then
					--mos.floorheight + mo.height then
						local dx,dy = P_ClosestPointOnLine(mo.x,  mo.y, l.v1.x, l.v1.y, l.v2.x,l.v2.y)
						if FixedHypot(dx-mo.x,dy-mo.y) <= sized  then
							wall = l
							//try move near to the wall
							P_TryMove(mo,
							dx+P_ReturnThrustX(nil,mang-ANGLE_180,(mo.radius+(FRACUNIT*2))),
							dy+P_ReturnThrustY(nil,mang-ANGLE_180,(mo.radius+(FRACUNIT*2))),						
							true)
							//P_MoveOrigin(mo,
							//dx+P_ReturnThrustX(nil,mang,-(mo.radius+(FRACUNIT*8))),
							//dy+P_ReturnThrustY(nil,mang,-(mo.radius+(FRACUNIT*8))),
							//mo.z)
							//mo.z = P_FloorzAtPos(mo.x,mo.y,mo.z,mo.height) 
						end
					end
				end
			end, pmo)--,pmo.x-sized, pmo.x+sized, pmo.y+sized, pmo.y-sized)
			
			
			
			
			
			if wall and not p.ltb_tobelaunched
			and P_IsObjectOnGround(pmo) then
				p.ltb_tobelaunched = true
				//print("dfsdfdsfsdf")
				pmo.ltb_fakemomz = p.speed - (p.speed/2)
				pmo.momx,pmo.momy,pmo.momz = 0,0,0
				pmo.z = $ + (P_GetMobjGravity(pmo)*-1)+1
			end
		end
		
		
	end

end)

rawset(_G,"LTB_WALLLAUNCH",true) //make it to load it once