// ------------------------------------------
// Lua_Damage
// Tails Guy's damage hook
// ------------------------------------------
addHook("ShouldDamage", function(mobj)
	if(mobj.player and mobj.player.valid and mobj.player.tgvars)
		and(mobj.player.powers[pw_flashing])
		mobj.player.tgvars.rockethit = false
		mobj.player.tgvars.myrockethit = false
		return
	end
end)
addHook("MobjDamage", function(mobj, inflictor, source, damage)
	if not(mobj and mobj.valid)
		or not(mobj.skin == "tailsguy")
		or not(mobj.player and mobj.player.valid)
		or not(mobj.health)
		or(mobj.player.powers[pw_carry] == CR_NIGHTSMODE)
		if(mobj and mobj.valid and mobj.player and mobj.player.valid and mobj.player.tgvars)
			mobj.player.tgvars.rockethit = false
			mobj.player.tgvars.myrockethit = false
		end
		return
	end
	
	if(mobj.player.powers[pw_super])
		mobj.player.tgvars.rockethit = false
		mobj.player.tgvars.myrockethit = false
		return true
	end
	
	mobj.player.tgvars.hurttime = TICRATE
	
	// Take damage
	if(mobj.player.powers[pw_shield])
		P_RemoveShield(mobj.player)
		P_PlayerFlagBurst(mobj.player)
		if not(mariomode)
			S_StartSound(mobj, sfx_shldls)
		else
			S_StartSound(mobj, sfx_mario8)
		end
	else
		// Burst all the things! (except rings)
		if not(mobj.player.tgvars.myrockethit)
			P_PlayerWeaponAmmoBurst(mobj.player)
			P_PlayerWeaponPanelBurst(mobj.player)
			P_PlayerEmeraldBurst(mobj.player)
			P_PlayerFlagBurst(mobj.player)
		end
		
		if(mobj.player.tgvars.rockethit)
			mobj.player.tgvars.health = $1 - 20
		else
			mobj.player.tgvars.health = $1 - 10
		end
		if(mobj.player.tgvars.health < 0)
			mobj.player.tgvars.health = 0
		end
		
		if(mobj.health <= 0)				// Dead
			or(mobj.player.tgvars.health <= 0)	// Also dead
			P_KillMobj(mobj)
			mobj.health = 0
		else
			if not(mariomode)
				S_StartSound(mobj, sfx_altow1)
			else
				S_StartSound(mobj, sfx_mario8)
			end
		end
	end
	
	// Take knockback!
	if(mobj.health > 0)	// Not dead yet!
		mobj.player.powers[pw_flashing] = 50	// Apply invincibility so we don't *actually* take damage (we already did!)
		P_DoPlayerPain(mobj.player, source, inflictor)	// Do knockback stuff
	end

	// Why is this not accessable to lua?
//	P_HitDeathMessages(player, inflictor, source)
	
	mobj.player.tgvars.rockethit = false
	mobj.player.tgvars.myrockethit = false
	return true
	
end, MT_PLAYER)
addHook("MobjDeath", function(mobj, bullet)
	if(mobj and mobj.valid)
		and(mobj.flags & MF_MONITOR)
		and(bullet and bullet.valid)
		and(bullet.target and bullet.target.valid)
		and(bullet.target.player and bullet.target.player.valid)
		and(bullet.target.player.mo and bullet.target.player.mo.valid)	// Switched to specate?
		and(bullet.target.player.mo.health)
		and(bullet.target.player.mo.skin == "tailsguy")
		bullet.target.player.tgvars.facetime = -35
	end
end)
