

local function hasFireBasedShield(player)
	local pshield = (player.powers[pw_shield] & SH_NOSTACK)
	return pshield == SH_FLAMEAURA or pshield == SH_ELEMENTAL
end


local function initTables(mobj)
	if mobj.heatAura == nil
		mobj.heatAura = {
			afterfire = 0,
			heating = 0
		}
	end
end

local function ParticleSpawn(actor, momZ, mobj, fuse)
	if actor.player
		if actor.movecount == nil
			actor.movecount = 0
		end
		actor.movecount = $ + 4*16
		actor.movecount = $ % 360
		local fa = FixedAngle(P_RandomKey(360)*FRACUNIT)
		local x = actor.x + FixedMul(cos(fa),actor.radius/2)
		local y = actor.y + FixedMul(sin(fa),actor.radius/2)
		local z
		if (actor.eflags & MFE_VERTICALFLIP)
			z = actor.z + actor.height - mobjinfo[mobj].height - FixedMul((P_RandomByte()<<(FRACBITS-2)) - 8*FRACUNIT, actor.scale)
		else
			z = actor.z + FixedMul((P_RandomByte()<<(FRACBITS-2)) - 8*FRACUNIT, actor.scale)
		end
		local mo = P_SpawnMobj(x, y, z, mobj)
		if (actor.eflags & MFE_VERTICALFLIP) 
			mo.eflags = $|MFE_VERTICALFLIP
		end
		mo.destscale = actor.scale
		P_SetScale(mo, mo.destscale)
		P_SetObjectMomZ(mo, momZ)
		mo.fuse = fuse
	    mo.blendmode = AST_ADD
	end
end



addHook("ShouldDamage", function(target, inflictor, source, damage)
    // I am immune to fiery things
    if (target.skin == "flame")
        // Fire objects
        if (inflictor and inflictor.flags & MF_FIRE)
		    target.heatAura.heating = 4*TICRATE
            return false
        end
        // Lava
        if not(inflictor or source)
		and not(CBW_Battle and CBW_Battle.BattleGametype())
		and P_PlayerTouchingSectorSpecial(target.player, 1, 3)
		    target.heatAura.heating = 4*TICRATE
            return false
        end
        if not(inflictor or source)
		and (CBW_Battle and CBW_Battle.BattleGametype())
		and CBW_Chaos
		and P_PlayerTouchingSectorSpecial(target.player, 1, 3)
		    target.heatAura.heating = 4*TICRATE
            return false
        end		
    end
end, MT_PLAYER) 

addHook("PlayerThink", function(p)
	if (p.mo and p.mo.valid and p.mo.skin == "flame")
	local mo = p.mo
	    if p.mrce != nil
		 p.mrce.hyperimages = true
		 p.mrce.canhyper = true
		 p.mrce.ultrastar = false
		end 
		if mo.eflags&MFE_UNDERWATER and not (mo.eflags&MFE_TOUCHLAVA)
		and p.mo.heatAura ~= nil
	        if p.mo.heatAura.heating ~= 0
			  p.mo.heatAura.heating = 0
			  S_StartSound(mo, sfx_s3k4b)
			end  
		end
		if (mo.sprite2 == SPR2_SHIT or (p.solchar and p.solchar.istransformed))
		and not (mo.eflags & (MFE_UNDERWATER|MFE_GOOWATER))
		    p.mo.heatAura.heating = 1*TICRATE
		end

	end
end)





addHook("MobjDamage",function(targ, inf, src, dmg, dmgType)
  if not (targ and targ.valid and targ.player) return end
  if targ.skin == "flame" and dmgType == DMG_FIRE and not(CBW_Battle and CBW_Battle.BattleGametype())
    return true
  end
end,MT_PLAYER)



// ON FIRE!!
addHook("MobjThinker", function(mobj)
	if mobj.player 
	and mobj.skin == "flame"
	and mobj.health > 0
		initTables(mobj)
		if (mobj.heatAura.afterfire > 0)
			mobj.heatAura.afterfire = $ - 1
		end
		if mobj.heatAura.heating > 0
		    mobj.heatAura.heating = $ - 1
		    if not hasFireBasedShield(mobj.player)
		    and not (mobj.eflags & (MFE_UNDERWATER|MFE_GOOWATER))
		    and (leveltime % 7 == 0)
			   ParticleSpawn(mobj, 2*FRACUNIT, MT_FTDFLAME, 20)
		    end
		end
	end
end, MT_PLAYER)	



//Pain stops fire?
addHook("MobjDamage", function(target, inflictor, source, damage)
	if target.skin == "flame"
		target.heatAura.heating = 0
		target.heatAura.afterfire = 0
	end
end, MT_PLAYER)



// Abilites table for Kirby to eat
if not(kirbyabilitytable)
	rawset(_G, "kirbyabilitytable", {})
end
// Fireball
kirbyabilitytable["flame"] = 1
kirbyabilitytable[MT_FTDBALL] = 1
kirbyabilitytable[MT_FTDDOWNBALL] = 1


if not solchars
    rawset(_G, "solchars", {})
end
solchars["flame"] = {SKINCOLOR_SUPER_KETCHUP_1, 2}




addHook("MobjCollide", function(thing, tmthing)
   	if not (thing and thing.valid and tmthing and tmthing.valid) return end
	  if tmthing.z + tmthing.height*3 < thing.z or thing.z + thing.height < tmthing.z then return end
       if tmthing.player and tmthing.skin == "flame" and tmthing.state == S_PLAY_FCRASH
            P_KillMobj(thing, tmthing)
			return true
	   end	 
end, MT_WALLSPIKE)
addHook("MobjCollide", function(thing, tmthing)
   	if not (thing and thing.valid and tmthing and tmthing.valid) return end
	  if tmthing.z + tmthing.height*1 < thing.z or thing.z + thing.height*2 < tmthing.z then return end
       if tmthing.player and tmthing.skin == "flame" and tmthing.state == S_PLAY_FCRASH
            P_KillMobj(thing, tmthing)
			return true	
       end 
end, MT_SPIKE)



