
freeslot(
"MT_SHELLCOMMANDER",
"MT_HEXPLODE",
"S_SHELLCOMPATROL",
"S_SHELLCOMLEER",
"S_SHELLCOMDIVE",
"S_SHELLCOMPAIN",
"S_SHELLCOMDED",
"S_HEXPLODE1",
"S_HEXPLODE2",
"SPR_SCOM",
"SPR_HXPL"
)



function A_CommanderPatrol(actor, var1, var2) 

	A_Look(actor, (800<<16)+1, 0)

	if (P_RandomChance((FRACUNIT*9)/10) ) then
		actor.reactiontime = $+1
	end
		actor.mass = 0
	
		actor.frame = (leveltime%3)
		
	if (actor.reactiontime >= 65) then
		actor.reactiontime = 0
		local leashx
		local leashy
		local leashz
		if (actor.spawnpoint ~= nil) then
			leashx = actor.spawnpoint.x*FRACUNIT
			leashy = actor.spawnpoint.y*FRACUNIT
			leashz = actor.floorz
		else
			leashx = actor.x
			leashy = actor.y
			leashz = actor.floorz
		end
		
		local targetx = leashx + (P_RandomRange(-1*var1, var1)*FRACUNIT)
		local targety = leashy + (P_RandomRange(-1*var1, var1)*FRACUNIT)
		local targetz = leashz + (P_RandomRange(0, 256)*FRACUNIT)
		
		actor.angle = R_PointToAngle2(actor.x, actor.y, targetx, targety)
		actor.momx = cos(actor.angle)*3
		actor.momy = sin(actor.angle)*3
		actor.momz = (targetz-actor.z)/50
	else
		actor.momx = (actor.momx*99)/100
		actor.momy = (actor.momy*99)/100
		actor.momz = (actor.momz*99)/100
	end


end


function A_CommanderLeer(actor, var1, var2) 

	if  not (actor.target.valid) then
		actor.state = mobjinfo[actor.type].spawnstate
		return
	end
	
	actor.mass = $+1
	
	if (actor.mass < 90) then
		actor.frame = (leveltime%3)
	else
		actor.frame = 3+(leveltime%3)
	end
	
	if (actor.mass == 90) then
		S_StartSound(actor, sfx_s3kc5s)
	end
	
	if (actor.mass == 100) then
		actor.mass = 0
		actor.state = mobjinfo[actor.type].missilestate
	end
	actor.angle = R_PointToAngle2(actor.x, actor.y, actor.target.x, actor.target.y)
	local dist = R_PointToDist2(actor.x, actor.y, actor.target.x, actor.target.y)
	
	if (actor.z > actor.target.z+(128*FRACUNIT)) then
		actor.momz = -4*FRACUNIT
	elseif (actor.z < actor.target.z+(64*FRACUNIT))
		actor.momz = 1*FRACUNIT
	else
		actor.momz = (actor.momz*9)/10
	end
	if (dist < 256*FRACUNIT) then
		actor.momx =  cos(actor.angle)*-10
		actor.momy = sin(actor.angle)*-10
	elseif (dist < 512*FRACUNIT) then
		actor.momx = (actor.momx*9)/10
		actor.momy = (actor.momy*9)/10
	elseif (dist < 1028*FRACUNIT) then
		actor.momx =  cos(actor.angle)*6
		actor.momy = sin(actor.angle)*6
	else
		actor.state = mobjinfo[actor.type].spawnstate
	end


end


function A_CommanderDive(actor, var1, var2) 
	if  not (actor.target.valid) then
		actor.state = mobjinfo[actor.type].spawnstate
		return
	end
	actor.frame = 3+(leveltime%3)
	
	actor.mass = $+1
	P_SpawnGhostMobj(actor)
	if (actor.mass == 1) then
		local dist = R_PointToDist2(actor.x, actor.y, actor.target.x, actor.target.y)
		actor.angle = R_PointToAngle2(actor.x, actor.y, actor.target.x, actor.target.y)
		local zangle = R_PointToAngle2(0, actor.z, dist, actor.target.z)
		
		actor.momx = FixedMul(cos(actor.angle), cos(zangle))*30
		actor.momy = FixedMul(sin(actor.angle), cos(zangle))*30
		actor.momz = sin(zangle)*30
		actor.painchance = actor.momz
		S_StartSound(actor, sfx_spdpad)
	end
	
	if (actor.mass == 20) then
		actor.momx = $*-1
		actor.momy = $*-1
		actor.momz = actor.painchance*-1
	end
	if (actor.mass == 40) then
		actor.momx = 0
		actor.momy = 0
		actor.momz = 0
		actor.mass = 0
		actor.state = mobjinfo[actor.type].seestate
	end
end

mobjinfo[MT_SHELLCOMMANDER] = {
spawnstate = S_SHELLCOMPATROL,
seestate = S_SHELLCOMLEER,
seesound = sfx_s3k89,
missilestate = S_SHELLCOMDIVE,
painstate = S_SHELLCOMPAIN, 
painsound = sfx_dmpain,
deathstate = S_SHELLCOMDED,
attacksound = sfx_s3k4d,
deathsound = sfx_pop,
doomednum = 1517,
reactiontime = 65,
speed = 6*FRACUNIT,
radius = 28*FRACUNIT,
height = 52*FRACUNIT,
damage = 9,
spawnhealth = 2,
dispoffset = 0,
flags = MF_ENEMY|MF_SPECIAL|MF_SHOOTABLE|MF_NOGRAVITY
}


mobjinfo[MT_HEXPLODE] = {
spawnstate = S_HEXPLODE1,
reactiontime = 65,
speed = 6*FRACUNIT,
radius = 28*FRACUNIT,
height = 52*FRACUNIT,
damage = 9,
spawnhealth = 1,
dispoffset = 0,
flags = MF_NOGRAVITY|MF_NOBLOCKMAP
}

states[S_SHELLCOMPATROL] = {
sprite = SPR_SCOM,
frame = A,
tics = 1,
action = A_CommanderPatrol,
var1 = 600,
nextstate = S_SHELLCOMPATROL
}



states[S_SHELLCOMLEER] = {
sprite = SPR_SCOM,
frame = D,
action = A_CommanderLeer,
tics = 1,
nextstate = S_SHELLCOMLEER
}


states[S_SHELLCOMDIVE] = {
sprite = SPR_SCOM,
frame = D,
action = A_CommanderDive,
tics = 1,
nextstate = S_SHELLCOMDIVE
}


states[S_SHELLCOMPAIN] = {
sprite = SPR_SCOM,
frame = D,
action = A_Pain,
tics = 35,
nextstate = S_SHELLCOMPATROL
}

states[S_SHELLCOMDED] = {
sprite = SPR_SCOM,
frame = D,
action = A_SpawnObjectRelative,
var2 = MT_HEXPLODE,
tics = 0,
nextstate = S_XPLD1
}

states[S_HEXPLODE1] = {
sprite = SPR_HXPL,
frame = A|FF_ANIMATE|FF_ADD,
var1 = 4,
var2 = 4,
tics = 16,
nextstate = S_HEXPLODE2
}

states[S_HEXPLODE2] = {
sprite = SPR_HXPL,
frame = F|FF_ANIMATE|FF_ADD,
var1 = 1,
var2 = 4,
tics = 16,
nextstate = S_NULL
}


addHook("MobjCollide", function(actor, collider) 

if collider.type == MT_SHELLCOMMANDER then
	return false
end
	return nil
	
end, MT_SHELLCOMMANDER)