-- By Sirexer

-- Teleport player with rollout rock
addHook("LinedefExecute", function(line, mo, sector)
	local x = line.frontside.textureoffset
	local y = line.frontside.rowoffset
	local z = line.frontsector.ceilingheight
	local angle = FixedAngle(line.frontsector.floorheight)
	if mo and mo.valid and mo.tracer and mo.tracer.valid
		local rock = mo.tracer
		
		/*[8]Peg Midtexture "Relative silent" teleport*/
		if not (line.flags & ML_EFFECT3)
			
			/*If the [1]Block Enemies flag is set,
			teleportation will be without flash and without sound*/
			if not (line.flags & ML_BLOCKMONSTERS)
				P_FlashPal(mo.player, 2, TICRATE/2)
				S_StartSound(nil, sfx_mixup, mo.player)
			end
			
			/*If the [6]Not Climbable flag is set,
			the player's angle will not change*/
			if not (line.flags & ML_NOCLIMB)
				mo.rock.angle = angle
				mo.angle = angle
			end
			
			/*If the [9]Solid Midtexture flag is set,
			saves the player's momentum*/
			if not (line.flags & ML_EFFECT4)
				mo.rock.momx = 0
				mo.rock.momy = 0
				mo.rock.momz = 0
				mo.momx = 0
				mo.momy = 0
				mo.momz = 0
			end
			
			local r_z = z+rock.height
			local p_z = z
			if (mo.flags2 & MF2_OBJECTFLIP ) and (rock.flags2 & MF2_OBJECTFLIP)
				r_z = z-rock.height
				p_z = z-(mo.height+rock.height)
			end
			P_SetOrigin(mo, x, y, r_z)
			P_SetOrigin(rock, x, y, p_z)
		else
			P_SetOrigin(mo, mo.x+x, mo.y+y, mo.z+z)
			P_SetOrigin(rock, rock.x+x, rock.y+y, rock.z+z)
			if mo.player and mo.player.valid and displayplayer and displayplayer.valid
				if mo.player == displayplayer
					P_TeleportCameraMove(camera, camera.x+x, camera.y+y, camera.z+z)
				end
			end
		end
	end
end, "RKTELEPT")

-- Flip player with rollout rock
addHook("LinedefExecute", function(line, mo, sector) -- Gravity flip player
	if mo and mo.valid and mo.tracer and mo.tracer.valid
		local rock = mo.tracer
	
		if (line.flags & ML_EFFECT1) --Invert current gravity [5]Slope Skew
			if (mo.flags2 & MF2_OBJECTFLIP ) and (rock.flags2 & MF2_OBJECTFLIP)
				mo.flags2 = $ - MF2_OBJECTFLIP
				rock.flags2 = $ - MF2_OBJECTFLIP
				P_SetOrigin(rock, mo.x, mo.y, mo.z-rock.height)
			elseif not (mo.flags2 & MF2_OBJECTFLIP ) and not (rock.flags2 & MF2_OBJECTFLIP)
				mo.flags2 = $ + MF2_OBJECTFLIP
				rock.flags2 = $ + MF2_OBJECTFLIP
				P_SetOrigin(rock, mo.x, mo.y, mo.z+mo.height)
			end
			
		elseif (line.flags & ML_NOCLIMB) -- Return to normal [6]Not Climbable
			if (mo.flags2 & MF2_OBJECTFLIP ) and (rock.flags2 & MF2_OBJECTFLIP)
				mo.flags2 = $ - MF2_OBJECTFLIP
				rock.flags2 = $ - MF2_OBJECTFLIP
				P_SetOrigin(rock, mo.x, mo.y, mo.z-rock.height)
			end
			
		else -- Reverse flip (no flags)
			if not (mo.flags2 & MF2_OBJECTFLIP ) and not (rock.flags2 & MF2_OBJECTFLIP)
				mo.flags2 = $ + MF2_OBJECTFLIP
				rock.flags2 = $ + MF2_OBJECTFLIP
				P_SetOrigin(rock, mo.x, mo.y, mo.z+mo.height)
			end
		end
		
	end
end, "RKFLIP")
