freeslot("MT_TRACYBEAKER", "sfx_kazoo", "sfx_nootn")

function A_PinguScared(actor, var1, var2)
	if(actor.info.seestate == S_CCPEGN3) then
		actor.info.seestate = S_CCPEGN4
		if(P_RandomChance(FRACUNIT/10)) then
			S_StartSound(actor, sfx_nootn)
		else
			S_StartSound(actor, sfx_kc5f)
		end
		
		A_BunnyHop(actor, 5, nil)
	end
end

function A_PinguMove(actor, var1, var2)
	if(actor.reactiontime) then
		actor.reactiontime = $ - 1
	end
	
	if(actor.info.seestate == S_CCPEGN4) then
		actor.info.seestate = S_CCPEGN3
	end
	
	if (((not (actor.eflags & MFE_VERTICALFLIP) and actor.z == actor.floorz)
	or (actor.eflags & MFE_VERTICALFLIP and actor.z + actor.height == actor.ceilingz))
	and not actor.reactiontime) then
	
		local p_angles = {ANGLE_45, ANGLE_67h, ANGLE_90, ANGLE_180, ANGLE_157h, ANGLE_112h}
		local p_chosenangle = p_angles[P_RandomRange(1, #p_angles)]
		if(P_RandomChance(FRACUNIT/2)) then
			actor.angle = $ + p_chosenangle
		else
			actor.angle = $ - p_chosenangle
		end
		P_InstaThrust(actor, actor.angle, FixedMul(actor.info.speed, actor.scale))
		actor.reactiontime = TICRATE/3
	end

end

function A_PinguReact(actor, var1, var2)

	if(actor.reactiontime) then 
		actor.reactiontime = $ - 1
	else
		actor.reactiontime = TICRATE
	end 
	if(not actor.target) then 
		if (not P_LookForPlayers(actor, 40*FRACUNIT, false, false))
			P_SetMobjStateNF(actor, S_CCPEGN1)
		end
	end
	
	if(actor.target) then
		if(actor.reactiontime == 0) then
			actor.angle = R_PointToAngle2(actor.x, actor.y, actor.target.x, actor.target.y);
			actor.reactiontime = TICRATE
			P_SetMobjStateNF(actor, S_CCPEGN1)
			actor.frame = B
			actor.target = nil
		end
	else
		P_SetMobjStateNF(actor, S_CCPEGN1)
		actor.info.seestate = S_CCPEGN3
	end
end

addHook("MobjThinker", function(mo)
	if(not mo.personalspace) then
		mo.personalspace = {mo.x, mo.y, mo.z}
		mo.personalspaceradius = 100*FRACUNIT
		mo.poo = TICRATE/5
	end
	
	if(mo.personalspace) then
-- 		print(R_PointToDist2(mo.x, mo.y, mo.personalspace[1], mo.personalspace[2])/FRACUNIT .. "/" .. mo.personalspaceradius/FRACUNIT)
		
		if(R_PointToDist2(mo.x, mo.y, mo.personalspace[1], mo.personalspace[2]) > mo.personalspaceradius) then 
			mo.angle = R_PointToAngle2(mo.x, mo.y, mo.personalspace[1], mo.personalspace[2])
			P_InstaThrust(mo, mo.angle, FixedMul(mo.info.speed, mo.scale))
		end
	end
end, MT_TRACYBEAKER)