//New enemies for new map (logic)
freeslot("MT_CONE_DRONE", "MT_CONE_SPIKEBALL", "MT_SPINY", "MT_SCRAP_BOMB", "MT_MOTOBUG", "SFX_MOTO", "MT_FLASHER", "MT_FLASHER_LASER", "MT_COCONUTS", "MT_CRABMEAT")

addHook("MobjThinker", function(cone)
	if cone
	and cone.valid
	and cone.health
	and cone.type == MT_CONE_DRONE
		//we need spawn a spikeball!
		if not cone.tracer
		local spikeball = P_SpawnMobj(cone.x, cone.y, cone.z, MT_CONE_SPIKEBALL)
		//funny, we making this shit for spikeball
		spikeball.tracer = cone
		cone.tracer = spikeball
		end
	end
end, MT_CONE_DRONE)

addHook("MobjThinker", function(spikeball)
	if spikeball
	and spikeball.valid
	and spikeball.health
	and spikeball.type == MT_CONE_SPIKEBALL
		if spikeball.tracer
		spikeball.angle = spikeball.angle+ANG10
		end
		if not spikeball.tracer //heh, just making for if enemy dead
		P_RemoveMobj(spikeball)
		end
		if spikeball.tracer
		and not spikeball.tracer.health //We need this make because if we kill enemy spikeball can be save!
		P_RemoveMobj(spikeball)
		end
	end
end, MT_CONE_SPIKEBALL)

addHook("MobjThinker", function(spiny)
	if spiny
	and spiny.valid
	and spiny.health
	and spiny.type == MT_SPINY
		A_FindTarget(spiny, MT_PLAYER, 0)
		if spiny.target
		and spiny.target.type == MT_PLAYER
			if R_PointToDist2(spiny.x, spiny.y, spiny.target.x, spiny.target.y) < 256*FRACUNIT
			and not ((spiny.state == S_SPINY_SHOT1) or (spiny.state == S_SPINY_SHOT2))
			spiny.state = S_SPINY_SHOT1
			end
			if (spiny.state == S_SPINY_SHOT2)
			local bullet = P_SpawnMobj(spiny.x, spiny.y, spiny.z+spiny.height+4*FRACUNIT, MT_JETTBULLET)
			bullet.flags = $1 & ~MF_NOGRAVITY
			bullet.target = spiny
			P_SetObjectMomZ(bullet, 6*FRACUNIT)
			P_InstaThrust(bullet, R_PointToAngle2(spiny.x, spiny.y, spiny.target.x, spiny.target.y), 8*FRACUNIT)
			end
		end
	end
end, MT_SPINY)

addHook("MobjThinker", function(bomb)
	if bomb
	and bomb.valid
	and bomb.health
	and bomb.type == MT_SCRAP_BOMB
		if bomb.target
			if R_PointToDist2(bomb.x, bomb.y, bomb.target.x, bomb.target.y) < 256*FRACUNIT
			and P_CheckSight(bomb, bomb.target)
				if not ((bomb.state == S_BOMB_READYBOOM1) or (bomb.state == S_BOMB_READYBOOM2) or (bomb.state == S_BOMB_READYBOOM3) or (bomb.state == S_BOMB_EXPLODE))
				bomb.state = S_BOMB_READYBOOM1
				end
			end
			if (bomb.state == S_BOMB_EXPLODE)
			P_RadiusAttack(bomb, bomb, 256*FRACUNIT)
			if not S_SoundPlaying(bomb, sfx_s3k4e)
			S_StartSound(bomb, sfx_s3k4e)
			end
			end
		end
	end
end, MT_SCRAP_BOMB)

addHook("MobjThinker", function(motobug)
	if motobug
	and motobug.valid
	and motobug.health
	and motobug.type == MT_MOTOBUG
		if not (motobug.state == S_MOTOBUG_LOOK)
			if not S_SoundPlaying(motobug, sfx_moto)
			S_StartSound(motobug, sfx_moto)
			end
		end
		if motobug.target
		and not P_CheckSight(motobug, motobug.target)
		motobug.target = nil
		end
	end
end, MT_MOTOBUG)

addHook("MobjThinker", function(flash)
	if flash
	and flash.valid
	and flash.health
	and flash.type == MT_FLASHER
		if (flash.state == S_FLASHER_LOOK)
		P_LookForPlayers(flash, 1024*FRACUNIT, 0, 0)
		end
		if flash.target
			if (flash.state == S_FLASHER_LOOK)
			flash.state = S_FLASHER_FLOAT
			end
			if (flash.state == S_FLASHER_FLOAT)
			P_InstaThrust(flash, R_PointToAngle2(flash.x, flash.y, flash.target.x, flash.target.y), 15*FRACUNIT)
			flash.angle = R_PointToAngle2(flash.x, flash.y, flash.target.x, flash.target.y)
			flash.momz = 0
				if (flash.z < flash.floorz+128*FRACUNIT)
				flash.z = $ + 8*FRACUNIT
				end
			end
			if R_PointToDist2(flash.x, flash.y, flash.target.x, flash.target.y) < 256*FRACUNIT
			and (flash.state == S_FLASHER_FLOAT)
			flash.state = S_FLASHER_SHOOT1
			end
			if (flash.state == S_FLASHER_SHOOT1)
			flash.momx = flash.target.momx
			flash.momy = flash.target.momy
			flash.momz = flash.target.momz
			P_SpawnGhostMobj(flash)
			end
			if (flash.state == S_FLASHER_SHOOT2)
			//flash.z = $ - 8*FRACUNIT
			//P_SetObjectMomZ(flash, -8*FRACUNIT)
			local laser = P_SpawnMobj(flash.x, flash.y, flash.z-16*FRACUNIT, MT_FLASHER_LASER)
			P_SetObjectMomZ(laser, -20*FRACUNIT)
			laser.color = SKINCOLOR_YELLOW
			P_InstaThrust(flash, flash.angle, 20*FRACUNIT)
				if flash.tics >= 34
				flash.angle = R_PointToAngle2(flash.x, flash.y, flash.target.x, flash.target.y)
				end
			end
			if (flash.state == S_FLASHER_COOLDOWN)
				if (flash.flags & MF_NOGRAVITY)
				flash.flags = $1 & ~MF_NOGRAVITY
				end
				if (flash.eflags & MFE_JUSTHITFLOOR)
				S_StartSound(flash, sfx_s3k5d)
					for i = 1, 6
					local dust = P_SpawnMobj(flash.x, flash.y, flash.z, MT_SPINDUST)
					dust.angle = dust.angle+ANG60*i
					P_InstaThrust(dust, dust.angle, 12*FRACUNIT)
					end
				end
			end
			if (flash.state == S_FLASHER_RISING)
			P_SetObjectMomZ(flash, 8*FRACUNIT)
			P_SpawnGhostMobj(flash)
			flash.flags = $1|MF_NOGRAVITY
				if (flash.z < flash.floorz+128*FRACUNIT)
				flash.state = S_FLASHER_LOOK
				end
			end
		end
	end
end, MT_FLASHER)

addHook("MobjThinker", function(missile)
	if missile
	and missile.valid
	and missile.health
	and missile.type == MT_FLASHER_LASER
		if P_IsObjectOnGround(missile)
		P_KillMobj(missile)
		for i = 1, 6
			local dust = P_SpawnMobj(missile.x, missile.y, missile.z, MT_SPINDUST)
			dust.angle = dust.angle+ANG60*i
			P_InstaThrust(dust, dust.angle+P_RandomRange(-30, 30)*ANG1, 10*FRACUNIT+P_RandomRange(0, 5)*FRACUNIT)
			P_SetObjectMomZ(dust, 1*FRACUNIT+P_RandomRange(0, 2)*FRACUNIT)
			dust.tics = dust.tics+P_RandomRange(0, 10)
			end
		end
	end
end, MT_FLASHER_LASER)

addHook("MobjThinker", function(coconuts)
	if coconuts
	and coconuts.valid
	and coconuts.health
	and coconuts.type == MT_COCONUTS
	A_FindTarget(coconuts, MT_PLAYER, 0)
		if coconuts.prevangle == nil
		coconuts.prevangle = 0
		end
		if coconuts.chooseclimb == nil
		coconuts.chooseclimb = 0
		end
		if coconuts.timeclimb == nil
		coconuts.timeclimb = 0
		end
		if coconuts.chooseclimb > 1
		coconuts.chooseclimb = 0
		end
		if (coconuts.state == S_COCONUTS_CLIMB)
		coconuts.timeclimb = $1 + 1
		if coconuts.chooseclimb == 0
		P_SetObjectMomZ(coconuts, 2*FRACUNIT)
		end
		if coconuts.chooseclimb == 1
		P_SetObjectMomZ(coconuts, -2*FRACUNIT)
		end
		end
		if (coconuts.state == S_COCONUTS_CLIMB_START)
		coconuts.momz = 0
		end
		if coconuts.timeclimb >= 2*TICRATE
		coconuts.timeclimb = 0
		coconuts.state = S_COCONUTS_CLIMB_START
		coconuts.chooseclimb = $1 + 1
		end
		if coconuts.target
			if R_PointToDist2(coconuts.x, coconuts.y, coconuts.target.x, coconuts.target.y) < 512*FRACUNIT
			and not ((coconuts.state == S_COCONUTS_ATTACK1) or (coconuts.state == S_COCONUTS_ATTACK2) or (coconuts.state == S_COCONUTS_ATTACK3))
			coconuts.prevangle = coconuts.angle
			coconuts.state = S_COCONUTS_ATTACK1
			coconuts.momz = 0
			end
		end
		if (coconuts.state == S_COCONUTS_ATTACK2)
		coconuts.angle = coconuts.prevangle
		end
	end
end, MT_COCONUTS)

addHook("MobjThinker", function(crabmeat)
	if crabmeat
	and crabmeat.valid
	and crabmeat.health
	and crabmeat.type == MT_CRABMEAT
		if (crabmeat.state == S_CRABMEAT_ATTACK2)
		local ball = P_SPMAngle(crabmeat, MT_JETTBULLET, crabmeat.angle+ANGLE_90)
		local ball2 = P_SPMAngle(crabmeat, MT_JETTBULLET, crabmeat.angle-ANGLE_90)
		ball.flags = $1 & ~MF_NOGRAVITY
		ball2.flags = $1 & ~MF_NOGRAVITY
		P_SetObjectMomZ(ball, 8*FRACUNIT)
		P_SetObjectMomZ(ball2, 8*FRACUNIT)
		P_InstaThrust(ball, ball.angle, 6*FRACUNIT)
		P_InstaThrust(ball2, ball2.angle, 6*FRACUNIT)
		end
	end
end, MT_CRABMEAT)