/*
// CUSTOM SNEAKER TRAILS
*/

freeslot("MT_DUPESNEAKERTRAIL", "S_DUPEFIRE1");

local dupelength = 1; // default is 10
states[S_DUPEFIRE1] = {SPR_NULL, 0, dupelength, nil, 0, 0, S_KARTFIRE2}

mobjinfo[MT_DUPESNEAKERTRAIL] = {
		doomednum = -1,
		spawnstate = S_DUPEFIRE1,    // spawnstate
		spawnhealth = 1,              // spawnhealth
		reactiontime = 8,              // reactiontime
		speed = 8,              // speed
		radius = 14*FRACUNIT,    // radius
		height = 14*FRACUNIT,    // height
		dispoffset = 0,              // display offset
		mass = 100,            // mass
		flags = MF_NOBLOCKMAP|MF_NOCLIP|MF_NOCLIPHEIGHT|MF_NOGRAVITY|MF_SCENERY|MF_SLIDEME|MF_DONTENCOREMAP, // flags
		raisestate = S_NULL          // raisestate
}

// lua'd this from the source
// Adds gravity flipping to an object relative to its master and shifts the z coordinate accordingly.
local function K_FlipFromObject(mo, master)
	mo.eflags = (mo.eflags & ~MFE_VERTICALFLIP)|(master.eflags & MFE_VERTICALFLIP);
	mo.flags2 = (mo.flags2 & ~MF2_OBJECTFLIP)|(master.flags2 & MF2_OBJECTFLIP);

	if (mo.eflags & MFE_VERTICALFLIP)
		mo.z = ($ + master.height - FixedMul(master.scale, mo.height));
	end
end

// this was also lua'd + slightly modified
local function K_SpawnCustomBoostTrail(player) -- adjustable boost trail
	local newx; --fixed_t
	local newy; --fixed_t
	local ground; --fixed_t
	local flame; -- obj
	local travelangle; -- angle

	if (not (player.mo and player.mo.valid)) then return end;

	if ((not P_IsObjectOnGround(player.mo)) or (G_BattleGametype() and player.kartstuff[k_bumper] <= 0 and player.kartstuff[k_comebacktimer])) // show sneaker boosts during a hyuu
		return;
    end

	if (player.mo.eflags & MFE_VERTICALFLIP)
		ground = player.mo.ceilingz - FixedMul(mobjinfo[MT_SNEAKERTRAIL].height, player.mo.scale);
	else
		ground = player.mo.floorz;
    end

	if (player.kartstuff[k_drift] != 0)
		travelangle = player.mo.angle;
	else
		travelangle = R_PointToAngle2(0, 0, player.rmomx, player.rmomy);
	end

	for i = 0, 2
		newx = player.mo.x + P_ReturnThrustX(player.mo, travelangle + ((i&1) and -1 or 1)*ANGLE_135, FixedMul(24*FRACUNIT, player.mo.scale));
		newy = player.mo.y + P_ReturnThrustY(player.mo, travelangle + ((i&1) and -1 or 1)*ANGLE_135, FixedMul(24*FRACUNIT, player.mo.scale));
		if (player.mo.standingslope)
			ground = P_GetZAt(player.mo.standingslope, newx, newy);
			if (player.mo.eflags & MFE_VERTICALFLIP)
				ground = ($ - FixedMul(mobjinfo[MT_SNEAKERTRAIL].height, player.mo.scale));
            end
		end
		flame = P_SpawnMobj(newx, newy, ground, MT_DUPESNEAKERTRAIL);

		flame.target = player.mo;
		flame.angle = travelangle;
		flame.fuse = TICRATE*2;
		flame.destscale = player.mo.scale;
		P_SetScale(flame, player.mo.scale);
		// not K_MatchGenericExtraFlags so that a stolen sneaker can be seen
		K_FlipFromObject(flame, player.mo);

		flame.momx = 8;

        if (not (flame and flame.valid)) then continue end;

		if (player.mo.eflags & MFE_VERTICALFLIP)
			if (flame.z + flame.height < flame.ceilingz)
				P_RemoveMobj(flame);
            end
		elseif (flame.z > flame.floorz)
			P_RemoveMobj(flame);
        end
	end
end

addHook("MobjThinker", function(mo) // flame trail represents your color in this mode!
	if (driftmodData.isOn ~= true) then return end; // no nitro, no trail colors (maybe a separate mod?)
	local targ = mo.target
	
	if (targ)
		if targ.player
			local p = mo.target.player
			mo.colorized = true
			mo.color = p.skincolor
		end
	end
end, MT_DUPESNEAKERTRAIL)

addHook("MobjThinker", function(mo) // hide the real flame trail
	if (driftmodData.isOn ~= true) then return end; // no nitro, no trail colors (maybe a separate mod?)
	
	mo.state = S_NULL // boosts? what boosts? :^) 
end, MT_SNEAKERTRAIL)

addHook("MobjThinker", function(mo)
    local p = mo.player;
	local onground = (P_IsObjectOnGround(p.mo) or (p.kartstuff[k_pogospring]));
	
	if (p.kartstuff[k_sneakertimer] > 0 and onground and (leveltime & 1) and (driftmodData.isOn))
		K_SpawnCustomBoostTrail(p);
	end
end, MT_PLAYER)