local s = {}
s[S_PLAY_MELEE_LANDING] = true

freeslot("SPR_PKRG", "MT_PIKO_RING", "S_PIKO_RING")
mobjinfo[MT_PIKO_RING] = {
    doomednum = -1,
    spawnstate = S_PIKO_RING,
    flags = MF_SCENERY|MF_NOCLIP|MF_NOGRAVITY
}
states[S_PIKO_RING] = {
    sprite = SPR_PKRG,
    frame = A|FF_PAPERSPRITE|FF_FLOORSPRITE
}

addHook("MobjThinker", function(mo)
  local fusefade = {
    [0] = 0,
    [1] = FF_TRANS90,
    [2] = FF_TRANS80,
    [3] = FF_TRANS70,
    [4] = FF_TRANS60,
    [5] = FF_TRANS50,
    [6] = FF_TRANS40,
    [7] = FF_TRANS30,
    [8] = FF_TRANS20,
    [9] = FF_TRANS10
	}
  if mo and mo.valid
    if mo.fuse < 10
    and fusefade[mo.fuse/2]
      local fade = fusefade[mo.fuse/2]
      mo.frame = $|fade 
    end
  end
end,MT_PIKO_RING)

addHook("PlayerThink", function(player)
    local mo = player.mo -- Less hassle

	
	player.charflags = $|SF_SUPER
	
		mo.hasbounced = true or false
		if mo.state != S_PLAY_FIRE
	mo.hasbounced = false
	end
		mo.hammertime = $ or 0 --Just in case, if $ exists then use $ (current number), else use 0 (will be a number but wont work with "if mo.hammertime")
		if mo.state != S_PLAY_MELEE_LANDING
	mo.hammertime = 2
		elseif mo.hammertime --mo.hammertime must exist!
	mo.hammertime = $-1
	end
	
    if not(mo and mo.skin == "originsamy")
	or P_PlayerInPain(player)
	or mo.state == S_PLAY_DEAD
		mo.hammerjumped = 0 -- Discontinue animation, if currently happening
        return  -- Exits the script if not the correct character
    end
	
	if mo.state == S_PLAY_MELEE_LANDING and mo.hammertime and max(mo.hammertime,0)
	P_RadiusAttack(mo, mo, 96*FRACUNIT)
		local pkrg = P_SpawnMobjFromMobj(mo, -45*cos(player.drawangle + ANGLE_180), -45*sin(player.drawangle + ANGLE_180), FRACUNIT, MT_PIKO_RING)
		pkrg.scale = 1
		pkrg.destscale = mo.scale*10
		pkrg.scalespeed = FRACUNIT/2
		pkrg.fuse = 20
		pkrg.state = S_PIKO_RING
		--pkrg.scale = ease.linear(3*FRACUNIT/10, pkrg.scale, destscale)
		
			if pkrg.fuse < 10
			local bop = byebye[pkrg.fuse/2]
			pkrg.frame = $|bop
			end
	
	local t = 10 - (pkrg.fuse/2)
	pkrg.frame = $ | (t << FF_TRANSSHIFT)
	end
	
	local diff = FRACUNIT-mo.scale
local factor = 8
diff = $/factor
mo.scale = $+diff
	
    -- Spin and fall
    if not (player.mo.eflags & MFE_VERTICALFLIP) and mo.hammerjumped == 1 and mo.momz >= 16 -- Rising
        mo.hammerjumped = 2
        player.panim = PA_ROLL
		mo.state = S_PLAY_ROLL
	elseif (player.mo.eflags & MFE_VERTICALFLIP) and mo.hammerjumped == 1 and mo.momz <= -16 -- Rising in Reverse
        mo.hammerjumped = 2
		player.panim = PA_ROLL
        mo.state = S_PLAY_ROLL
end
    
	 -- Do extra stuff if we're super
    if player.powers[pw_super] and mo.state == S_PLAY_MELEE_LANDING and P_IsObjectOnGround(mo) and mo.hammertime and max(mo.hammertime,0)
		or player.powers[pw_super] and mo.hasbounced == true
        P_Earthquake(mo,mo, 320*FRACUNIT)
		S_StartSound(mo, sfx_s3k49)
        --P_FlashPal(player, PAL_WHITE, 8)
    end
	
	if mo.state == S_PLAY_MELEE_LANDING and P_IsObjectOnGround(mo) and mo.hammertime and max(mo.hammertime,0)
		or mo.hasbounced == true
	end
	
    if player.exiting or (player.pflags & PF_THOKKED) or player.gotflagdebuff == true or not(player.cmd.buttons & BT_JUMP) or not P_IsObjectOnGround(mo) or s[mo.state] != true or not (mo.hammertime and max(mo.hammertime,0))
		return -- All code after this is for performing the action
    end

    -- Play sfx
    S_StartSound(mo, sfx_jump)
    -- Change state (SKIP)
    -- mo.state = S_PLAY_MELEE_LANDING
    -- Add flags
    player.pflags = $|PF_JUMPED|PF_SPINNING|PF_THOKKED
    
    -- Define our multipliers for thrust
    local waterfactor = mo.eflags & MFE_UNDERWATER and 6*FRACUNIT/10 or FRACUNIT
    local superfactor = player.powers[pw_super] and 5 or 4
	--local jumpfactor = (FixedMul(mo.scale, player.jumpfactor * 44/3))
	
    -- Define our final thrust amount
    local zthrust = 16 * superfactor * waterfactor / 4 -- superfactor / 4 will give us either 125% or 100%. 
    --P_DoJump(player, false)
	P_SetObjectMomZ(mo, zthrust, true)
	
	mo.hammerjumped = 1
end)

addHook("PostThinkFrame", do
    for player in players.iterate() do
        player.buttonslast = player.cmd.buttons
    end
end)