-- Smooth Trail script by ffoxD.
-- Reusable, use with credit.
local function SmoothTrail(mo, trailmo, freq)
    -- Real Z momentum thingy plus leveltime checks.
    -- You can replace all this with mo.momz+mo.pmomz but that stays at 0 when on slopes and on ground.
    if not mo.prevz or not mo.prevleveltime or (mo.prevleveltime ~= leveltime - 1) then
       mo.prevz = mo.z
    end
    local rmomz = mo.z - mo.prevz
    mo.prevz = mo.z
    mo.prevleveltime = leveltime
    -- Handy local variables.
    local mospeed = R_PointToDist2(0, 0, R_PointToDist2(0, 0, mo.momx, mo.momy), rmomz)
    local dist = (mospeed/freq)
    -- The loop, repeats until it spawns all the thok objects.
    for i = dist, 1, -1 do
        local spawnedmo = P_SpawnMobjFromMobj(mo, (mo.momx/dist)*i-mo.momx, (mo.momy/dist)*i-mo.momy, (rmomz/dist)*i-mo.momz, trailmo)
        spawnedmo.color = mo.color
        spawnedmo.blendmode = AST_ADD -- blend
    end
end

addHook("PlayerThink", function(player)
	if player.mo.skin ~= "ruby" then
		return
	end
	
    if player and player.valid and player.mo and player.mo.valid then
		if player.stompin then
            SmoothTrail(player.mo, MT_THOK, 4*FRACUNIT)
		end
    end
end)
