
freeslot(
"MT_CIVCRAWLA",
"S_CIVCRAWLAWANDER",
"S_CIVCRAWLAPAIN",
"S_CIVCRAWLAFLEE",
"SPR_CIVC"
)

function A_CrawlaWander(actor, var1, var2) 

	if (P_RandomChance((FRACUNIT*9)/10) ) then
		actor.reactiontime = $+1
	end
	if (actor.mass) then
		actor.mass = $+1
		actor.frame = 4
		A_FindTarget(actor, MT_PLAYER, 0)
		A_FaceTarget(actor, 0, 0)
		if (actor.mass > 10) then
			actor.reactiontime = 0
			actor.mass = 0
			actor.state = mobjinfo[actor.type].seestate
		end
		return
	end
	
		actor.frame = ((leveltime/4)%3)
		
	if (actor.reactiontime >= 200) then
		actor.reactiontime = 0
		local leashx
		local leashy
		if (actor.spawnpoint ~= nil) then
			leashx = actor.spawnpoint.x*FRACUNIT
			leashy = actor.spawnpoint.y*FRACUNIT
		else
			leashx = actor.x
			leashy = actor.y
		end
		
		local targetx = leashx + (P_RandomRange(-1*var1, var1)*FRACUNIT)
		local targety = leashy + (P_RandomRange(-1*var1, var1)*FRACUNIT)
		
	
		actor.angle = R_PointToAngle2(actor.x, actor.y, targetx, targety)
		actor.angle = $-($%ANGLE_45)
	end
	P_TryMove(actor, actor.x+(cos(actor.angle)*var2), actor.y+(sin(actor.angle)*var2), false)

end


local function CrawlaFleeSearch(refmob, foundmob)

	if not (foundmob) then
		return nil
	end
	if not (foundmob.valid) then
		return nil
	end
	if (foundmob.type ~= refmob.type) then
		return nil
	end
	if (foundmob == refmob) then
		return nil
	end
	if (foundmob.state ~= S_CIVCRAWLAWANDER) then
		return nil
	end
	
	local dist = R_PointToDist2(refmob.x, refmob.y, foundmob.x, foundmob.y) 
	if (dist < 1024*FRACUNIT) then
		foundmob.mass = 1
	end
	
	return nil
	
end


function A_SparkyPain(actor, var1, var2) 
	A_Pain(actor, 0, 0)
	for i = 0, 8
			local sparkitem = P_SpawnMobj(actor.x, actor.y, actor.z, MT_THUNDERCOIN_SPARK)
			sparkitem.momx = P_RandomRange(-10, 10)*FRACUNIT
			sparkitem.momy = P_RandomRange(-10, 10)*FRACUNIT
			sparkitem.momz = P_RandomRange(-2, 5)*FRACUNIT
			sparkitem.scale = P_RandomFixed()+(FRACUNIT/2)
			sparkitem.fuse = 10
	end
	actor.reactiontime = 0
	
	local radius = 1024*FRACUNIT
	searchBlockmap("objects", CrawlaFleeSearch, actor,
		actor.x-radius, actor.x+radius, actor.y-radius, actor.y+radius)
end


function A_CrawlaFlee(actor, var1, var2) 
	if not (actor.target) then
		A_FindTarget(actor, MT_PLAYER, 0)
	end
	if (actor.reactiontime % 10 == 0) then
		actor.flags2 = $1&~MF2_FRET
		P_NewChaseDir(actor)
		actor.movedir = ($+4)%8
		actor.angle = actor.movedir*ANGLE_45
	end
	P_Move(actor, 15)
	actor.frame = ((leveltime)%3)
	if (actor.reactiontime >= 400) then
		actor.reactiontime = 0
		actor.state = mobjinfo[actor.type].spawnstate
		
	end


	actor.reactiontime = $+1
end


mobjinfo[MT_CIVCRAWLA] = {
spawnstate = S_CIVCRAWLAWANDER,
painstate = S_CIVCRAWLAPAIN, 
seestate = S_CIVCRAWLAFLEE, 
painsound = sfx_dmpain,
deathstate = S_XPLD_FLICKY,
attacksound = sfx_s3k4d,
painsound = sfx_dmpain,
deathsound = sfx_pop,
doomednum = 1518,
reactiontime = 65,
speed = -6,
radius = 27*FRACUNIT,
height = 44*FRACUNIT,
damage = 9,
spawnhealth = 2,
dispoffset = 0,
mass = 0,
flags = MF_ENEMY|MF_SPECIAL|MF_SHOOTABLE
}

states[S_CIVCRAWLAWANDER] = {
sprite = SPR_CIVC,
frame = A,
tics = 1,
action = A_CrawlaWander,
var1 = 600,
var2 = 1,
nextstate = S_CIVCRAWLAWANDER
}



states[S_CIVCRAWLAPAIN] = {
sprite = SPR_CIVC,
frame = E,
tics = 30,
action = A_SparkyPain,
nextstate = S_CIVCRAWLAFLEE
}

states[S_CIVCRAWLAFLEE] = {
sprite = SPR_CIVC,
frame = E,
tics = 2,
action = A_CrawlaFlee,
nextstate = S_CIVCRAWLAFLEE
}

addHook("MobjSpawn", function(actor) 
	actor.color = P_RandomRange(1, 68)
	
end, MT_CIVCRAWLA)
