-- LUA_STOMP
-- Stomp (pisoton) tomado de L_ModernAbilities e integrado al sistema de "habilidad secundaria"
-- del mod de wisps: se activa con player.stompattack (comando "wisp_spinability 2").
-- No depende de SOC: MT_STOMPDUST se define aqui en Lua.
--
-- Uso: en el aire, pulsa SPIN para caer en picada; al aterrizar suelta una onda de choque
-- que dana enemigos/jefes y rompe monitores cercanos.

freeslot("MT_STOMPDUST", "sfx_stomp", "sfx_ground") -- ya estan en LUA_0INIT; repetir es inofensivo

local STOMP_FALLSPEED = 60*FRACUNIT   -- velocidad de caida durante el stomp
local STOMP_RADIUS    = 100*FRACUNIT  -- radio de la onda de choque al aterrizar

mobjinfo[MT_STOMPDUST] = {
	doomednum = -1,
	spawnstate = S_XPLD1,
	spawnhealth = 1000,
	speed = 20*FRACUNIT,
	radius = 16*FRACUNIT,
	height = 32*FRACUNIT,
	mass = 0,
	damage = 0,
	flags = MF_MISSILE|MF_NOGRAVITY|MF_NOBLOCKMAP
}

local function GravScale(mo)
	return P_MobjFlip(mo)*mo.scale
end

-- true si otra habilidad de wisp ya esta usando el boton de spin / el control del jugador
local function StompBusy(player)
	local mo = player.mo
	return (player.wisptimer and player.wisptimer > 0)
		or player.boosting
		or player.airboost
		or player.laserphase1
		or player.laserzoom
		or player.drilling
		or player.exiting
		or (player.pflags & PF_STASIS)
		or (player.powers[pw_carry] ~= 0)
		or (mo.tracer and mo.tracer.valid and mo.tracer.type == MT_TUBEWAYPOINT)
		or P_PlayerInPain(player)
end

-- Onda de choque al aterrizar: dust visual + dano en area
local function StompImpact(player)
	local mo = player.mo
	S_StartSound(mo, sfx_ground)

	for i = 0, 31 do
		local dust = P_SpawnMobj(mo.x, mo.y, mo.z + FixedMul(FRACUNIT, GravScale(mo)), MT_STOMPDUST)
		dust.target = mo
		dust.scale = mo.scale
		if mo.eflags & MFE_VERTICALFLIP then
			dust.flags2 = $1|MF2_OBJECTFLIP
		end
		local an = i*ANGLE_11hh
		local speed = dust.info.speed/FRACUNIT
		dust.momx = speed*cos(an)
		dust.momy = speed*sin(an)
		dust.momz = 0
	end

	local r = FixedMul(STOMP_RADIUS, mo.scale)
	searchBlockmap("objects", function(refmo, found)
		if not (found and found.valid and found.health > 0) then return end
		if found == refmo then return end
		if R_PointToDist2(refmo.x, refmo.y, found.x, found.y) > r + found.radius then return end
		if abs((found.z + found.height/2) - (refmo.z + refmo.height/2)) > r then return end

		if (found.flags & (MF_ENEMY|MF_BOSS)) and (found.flags & MF_SHOOTABLE) then
			P_DamageMobj(found, refmo, refmo, 1)
		elseif found.flags & MF_MONITOR then
			P_KillMobj(found, refmo, refmo)
		end
	end, mo, mo.x - r, mo.x + r, mo.y - r, mo.y + r)
end

addHook("ThinkFrame", function()
	for player in players.iterate do
		-- el boton se registra siempre para detectar "pulsacion nueva" y no stompear
		-- solo por llevar SPIN mantenido desde el salto
		local spin = (player.cmd.buttons & BT_SPIN) ~= 0
		local pressed = spin and not player.stompspinheld
		player.stompspinheld = spin

		local mo = player.mo
		if not (mo and mo.valid and mo.skin == "sonic")
		or player.playerstate ~= PST_LIVE
		or not player.stompattack then
			player.stomping = false
			player.stomptics = 0
			continue
		end

		if player.stomping then
			player.stomptics = $1 + 1
			local grounded = P_IsObjectOnGround(mo)

			if grounded and player.stomptics > 1 then
				-- aterrizaje: onda de choque
				player.stomping = false
				StompImpact(player)
			elseif grounded
			or mo.state == S_PLAY_SPRING
			or (mo.eflags & MFE_GOOWATER)
			or StompBusy(player)
			or (player.stomptics > 2 and mo.momz == 0)
			or (player.powers[pw_super] and not spin) then
				-- cancelado (resorte, agua pegajosa, otra habilidad, techo, super sin mantener...)
				player.stomping = false
			else
				-- cayendo en picada
				if mo.state ~= S_PLAY_FALL then
					mo.state = S_PLAY_FALL
				end
				mo.momx = 0
				mo.momy = 0
				mo.momz = FixedMul(-STOMP_FALLSPEED, GravScale(mo))
				P_SpawnGhostMobj(mo)
			end
		elseif pressed
		and not P_IsObjectOnGround(mo)
		and not (mo.eflags & MFE_GOOWATER)
		and not StompBusy(player) then
			-- iniciar stomp
			player.stomping = true
			player.stomptics = 0
			mo.state = S_PLAY_FALL
			S_StartSound(mo, sfx_stomp)
			mo.momx = 0
			mo.momy = 0
			mo.momz = FixedMul(-STOMP_FALLSPEED, GravScale(mo))
		end
	end
end)

-- Las "esquirlas" de la onda de choque tambien danan lo que toquen en su camino
addHook("MobjMoveCollide", function(dust, found)
	if not (dust and dust.valid and found and found.valid) then return end
	local source = dust.target
	if not (source and source.valid) then return end
	if found == source or not found.health then return end

	if (found.flags & (MF_ENEMY|MF_BOSS)) and (found.flags & MF_SHOOTABLE) then
		P_DamageMobj(found, dust, source, 1)
	elseif found.flags & MF_MONITOR then
		P_KillMobj(found, dust, source)
	end
end, MT_STOMPDUST)
