--A replacement of how the random shadows work to (hopefully) fix the long distance
--explosions in Malicious Metropolis

--Effectively turns the things to scenery
addHook("MobjSpawn",function(mo)
	if not mapheaderinfo[gamemap].altshadows == true return end
		mo.flags = $1|MF_RUNSPAWNFUNC
		mo.flags = $1 & ~(MF_PAIN)
		mo.tics = -1
end
,MT_RANDOMSHADOW)

freeslot(
	"S_MM_TANK_ROAM"
)

states[S_MM_TANK_ROAM] = {SPR_ENM1, G, -1, None, 0,0, S_MM_TANK_ROAM}

--This is the hard coded roaming shadow effect turned into a 
--mobj thinker without the garu effect. If roaming shadows have AMBUSH enabled
--they will be turned into Turret type shadows.
addHook("MobjThinker",function(mo)
	--if not mapheaderinfo[gamemap].altshadows == true return end
	if mo.spawnpoint.options & MTF_AMBUSH
		if mo.state != S_MM_TANK_ROAM
			mo.state = S_MM_TANK_ROAM
		end
		P_SetScale(mo, FRACUNIT*3/2)
		mo.radius = $1 * 4
		mo.height = $1 * 4
		if (mo.extravalue2 <= 0)
			P_InstaThrust(mo, mo.angle, 8<<FRACBITS)	// move at 8 fracs / sec
			mo.extravalue1 = ((mo.extravalue1) and (mo.extravalue1-1) or (TICRATE*5+1))	// deplete timer if set, set to 5 ticrate otherwise.
			if (mo.extravalue1 == 1)	// if timer reaches 1, do a u-turn.
				mo.extravalue1 = 0
				mo.extravalue2 = 60
			end
		else	// Handle U-Turn
			mo.angle = $1+ANG1*3
			mo.extravalue2 = $1 -1
			return
		end
	end
end,MT_ROAMINGSHADOW)


--Transfers the Shadow's action's pain effect to a sector effect.
--Players will be flung back instead of in the angle of the shadow.
local function shadowpain(l,mo,s)
	if (not mo.player.powers[pw_flashing]
		and not mo.player.kartstuff[k_invincibilitytimer]
		and not mo.player.kartstuff[k_growshrinktimer]
		and not mo.player.kartstuff[k_spinouttimer]
		and P_IsObjectOnGround(mo)
		--[[ and mo.z == mo.z)]])
			P_DamageMobj(mo, nil, nil, 1)
			P_InstaThrust(mo,ANGLE_180+mo.angle, 16<<FRACBITS)
			local fire = P_SpawnMobj(mo.x, mo.y, mo.z, MT_THOK)
			P_SetMobjStateNF(fire,S_QUICKBOOM1)
			P_SetScale(fire, 4<<FRACBITS)
			fire.color = SKINCOLOR_KETCHUP
			S_StartSound(mo, sfx_fire2)
	else
		K_DoInstashield(mo.player)
	end
end

addHook("LinedefExecute",shadowpain,"SHDOPAIN")
