// Midiman's Quick hack that prevents the lava from eating rings
-- Modified so it actually works, kinda, by Re

local function NoRingEatingLava(target, inflictor, source, damagetype)
	if not (target and target.valid) then return end
	
	if damagetype and damagetype == DMG_FIRE then
		target.flags = target.info.flags
		target.health = target.info.spawnhealth
		--Find the lowest fof top below the ceiling
		if watertop ~= target.z - 1000*FRACUNIT then --only do this if watertop is NOT the default value
			local oldlowest = 8192*FRACUNIT --set the vars up
			local snapfloor = 8192*FRACUNIT
			for fof in target.subsector.sector.ffloors() do --ITERATION TIIIIIIME
				if fof.topheight < oldlowest then --if this fof is lower than the lowest one found...
					oldlowest = fof.topheight --save its height
					snapfloor = fof.topheight
					--print("Found new lowest FOF with height ".. fof.topheight / FRACUNIT)
				end
			end
			target.z = snapfloor --snap to the lowest FOF found
		end
		P_SetObjectMomZ(target, abs(target.momz*FRACUNIT) / FRACUNIT, false)
		return true
	end
end

addHook("MobjDeath", NoRingEatingLava, MT_RING)
addHook("MobjDeath", NoRingEatingLava, MT_FLINGRING)
