AddCSLuaFile( "cl_init.lua" )
AddCSLuaFile( "shared.lua" )
include('shared.lua')

ENT.Model = "models/Dinosaurs/trex.mdl"
ENT.health = 1550
ENT.Alerted = false
ENT.Territorial = false
//ENT.State = 0;//1 is idle, 2 is alert, 3 is searching, 3 is intimadating, 4 is attacking
ENT.alert_counter = 0;   //if it gets above 100, have him attack. if above 50, he intimidates
ENT.hunger_counter = 400;//if it gets below 0 and there is food: If he is idle, and there is food, make him eat. 
			 //if it gets below 0 and there is no food:  Make him start searching, otherwise have him attack if he sees something. Minimum is -130, max is 400
ENT.Bleeds = true
ENT.Leaps = false
ENT.Chasing = false
ENT.Flinches = true
ENT.PlayerFriendly = 0
ENT.FriendlyToPlayer = false
ENT.Damage = 300
ENT.AcidBlood = false
ENT.BleedsRed = true
ENT.FixedOnEnemy = false
//ENT.LeapDamage = 15
ENT.LeapSpeed = 1000
ENT.LeapDistance = 300
ENT.MinLeapDistance = 200
ENT.MeleeAttacking = false;
ENT.Leaping = false
ENT.alert1 = "Rex/angry2.wav"
ENT.alert2 = "Rex/chase_flare.wav"
ENT.alert3 = "Rex/angry2.wav"
ENT.idle1 = "Rex/idle1.wav"
ENT.idle2 = "Rex/idle2.wav"
ENT.idle3 = "Rex/idle3.wav"
ENT.idle4 = "Rex/idle4.wav"
ENT.attack1 = "Rex/trex_bite_eat.wav"
ENT.attack2 = "Rex/trex_bite_eat.wav"
ENT.attack3 = "Rex/trex_bite_eat.wav"
ENT.attackmiss1 = "Rex/trex_bite.wav"
ENT.attackmiss2 = "Rex/trex_bite.wav"
ENT.attackmiss3 = "Rex/trex_bite.wav"
ENT.attackleap = ""
ENT.hurt1 = "Rex/pain1.wav"
ENT.hurt2 = "Rex/pain1.wav"
ENT.hurt3 = "Rex/pain1.wav"
ENT.die1 = "Rex/die.wav"
ENT.die2 = "Rex/die.wav"
ENT.die3 = "Rex/die.wav"
ENT.dead = false
ENT.togglesearch = false
ENT.BreathTime = 0

/*---------------------------------------------------------*/
function ENT:TaskStart_PlayASound( data ) 
	Msg( "Playing sound ", data, "\n" )
	self:EmitSound(data,450,math.random(94,106))
end
function ENT:Task_PlayASound( data ) 
	self:TaskComplete()
end
/*---------------------------------------------------------*/

/*---------------------------------------------------------*/
function ENT:TaskStart_RemoveEnemy( data ) 
	if self:GetEnemy() != nil && self:GetEnemy() != NULL then
	self:GetEnemy():Remove()
	self.hunger_counter = self.hunger_counter + math.random(60,120)
	Msg( "Removing Ragdoll ", data, "\n" )
	end
end
function ENT:Task_RemoveEnemy( data ) 
	self:TaskComplete()
end
/*---------------------------------------------------------*/


function ENT:Roar()
	local function setmeleefalse()
		if self:Health() < 0 then return end
		self.MeleeAttacking = false
		self.Leaping = false
	end
	
	if self.MeleeAttacking == false then
		local bar = ents.Create("env_shake")
		bar:SetPos(self:GetPos())
		bar:SetKeyValue("amplitude","12")
		bar:SetKeyValue("radius","3000")
		bar:SetKeyValue("duration","6")
		bar:SetKeyValue("frequency","128")
		bar:Fire( "StartShake", 0, 0 )
		self.BreathTime = CurTime() + 12.4
		self:SetNPCState( NPC_STATE_COMBAT ) 
		self:EmitSound("Rex/roar"..math.random(1,4)..".wav",500,100)
		if self.Leaping == false then
			self:SetSchedule( SCHED_RANGE_ATTACK1 )
		else
			self:EmitSound( self.attackleap)
			self:SetLocalVelocity( Vector( 0, 0, 0 ) )
		end
		timer.Create( "melee_attack_timer" .. self.Entity:EntIndex( ), 10, 1, setmeleefalse )
		self.MeleeAttacking = true;
	end
end
	
function ENT:Initialize()
   self:SetModel( self.Model )
   self:SetHullType( HULL_LARGE )
   self:SetHullSizeNormal()
   self:SetSolid( SOLID_BBOX )
   self:SetMoveType( MOVETYPE_STEP )
   self:CapabilitiesAdd( CAP_MOVE_GROUND  )
   self:SetMaxYawSpeed( 10 )
   self:SetHealth(self.health)
end

function ENT:Think()
	if self.alert_counter < -10 then self.alert_counter = -10; end
	if self.hunger_counter < -100 then self.hunger_counter = -100; end
	if self.hunger_counter > 400 then self.hunger_counter = 400; end
	if self.alert_counter > 130 then self.alert_counter = 130; end

        //---------------
	local function setmeleefalse()
		if self:Health() < 0 then return end
		self.MeleeAttacking = false
		self.Leaping = false
		//if self.alert_counter >= 100 then
		//	self:SetSchedule(SCHED_CHASE_ENEMY)
		//end
	end
        //---------------
	local function seteatfalse()
		if self:Health() < 0 then return end
		self.MeleeAttacking = false
		self.Leaping = false
		self.FixedOnEnemy = false
		self:SetHealth(self:Health()+100)
		self:GetEnemy():Remove()
	end
        //---------------
	local function Attack_Bite()
		if self:Health() < 0 then return end
		self.alert_counter = self.alert_counter + 50
		local entstoattack = ents.FindInSphere(self:GetPos() + self:GetForward()*150,56)
		local randomsound = math.random(1,3)
		local hit = false
		if entstoattack != nil then
			for _,v in pairs(entstoattack) do
				if ( (v:IsNPC() || ( v:IsPlayer() && v:Alive())) && (v != self) /*&& (v:GetClass() != self:GetClass())*/ || (v:GetClass() == "prop_physics")) then
					v:TakeDamage( self.Damage, self ) 
					if v:IsPlayer() then
						v:ViewPunch(Angle(math.random(-1,1)*self.Damage,math.random(-1,1)*self.Damage,math.random(-1,1)*self.Damage))
					end
					//v:SetVelocity( self:GetForward() * 100 )
					hit = true
				end
			end
		end
		if hit == false then
			if randomsound == 1 then
	      		self:EmitSound( self.attackmiss1,460,math.random(80,120))
			elseif randomsound == 2 then
	      		self:EmitSound( self.attackmiss2,460,math.random(80,120))
			elseif randomsound == 3 then
	      		self:EmitSound( self.attackmiss3,460,math.random(80,120))
			end
		else
			//make the sound
			if randomsound == 1 then
	      		self:EmitSound( self.attack1,460,math.random(80,120))
			elseif randomsound == 2 then
	      		self:EmitSound( self.attack2,460,math.random(80,120))
			elseif randomsound == 3 then
	      		self:EmitSound( self.attack3,460,math.random(80,120))
			end
		end
		timer.Create( "melee_done_timer" .. self.Entity:EntIndex( ), 1, 1, setmeleefalse )
	end
	//---------------
	local function Attack_BiteHigh()
		if self:Health() < 0 then return end
		self.alert_counter = self.alert_counter + 50
		local entstoattack = ents.FindInSphere(self:GetPos() + self:GetForward()*150 + Vector(0,0,80),56)
		local randomsound = math.random(1,3)
		local hit = false
		if entstoattack != nil then
			for _,v in pairs(entstoattack) do
				if ( (v:IsNPC() || ( v:IsPlayer() && v:Alive())) && (v != self) /*&& (v:GetClass() != self:GetClass())*/ || (v:GetClass() == "prop_physics")) then
					v:TakeDamage( self.Damage, self ) 
					if v:IsPlayer() then
						v:ViewPunch(Angle(math.random(-1,1)*self.Damage,math.random(-1,1)*self.Damage,math.random(-1,1)*self.Damage))
					end
					//v:SetVelocity( self:GetForward() * 100 )
					hit = true
				end
			end
		end
		if hit == false then
			if randomsound == 1 then
	      		self:EmitSound( self.attackmiss1,460,math.random(80,120))
			elseif randomsound == 2 then
	      		self:EmitSound( self.attackmiss2,460,math.random(80,120))
			elseif randomsound == 3 then
	      		self:EmitSound( self.attackmiss3,460,math.random(80,120))
			end
		else
			//make the sound
			if randomsound == 1 then
	      		self:EmitSound( self.attack1,460,math.random(80,120))
			elseif randomsound == 2 then
	      		self:EmitSound( self.attack2,460,math.random(80,120))
			elseif randomsound == 3 then
	      		self:EmitSound( self.attack3,460,math.random(80,120))
			end
		end
		timer.Create( "melee_done_timer" .. self.Entity:EntIndex( ), 1, 1, setmeleefalse )
	end
	//---------------
	local function Attack_Headbutt()
		if self:Health() < 0 then return end
		self.alert_counter = self.alert_counter + 50
		local entstoattack = ents.FindInSphere(self:GetPos() + self:GetForward()*159,65)
		local randomsound = math.random(1,3)
		local hit = false
		if entstoattack != nil then
			for _,v in pairs(entstoattack) do
				if ( (v:IsNPC() || ( v:IsPlayer() && v:Alive())) && (v != self) /*&& (v:GetClass() != self:GetClass())*/ || (v:GetClass() == "prop_physics")) then
					v:TakeDamage( self.Damage/5, self ) 
					if v:IsPlayer() then
						v:ViewPunch(Angle(math.random(-1,1)*self.Damage,math.random(-1,1)*self.Damage,math.random(-1,1)*self.Damage))
					end
					v:SetVelocity( self:GetForward() * 600 + Vector(0,0,400))
					hit = true
				end
			end
		end
		if hit == true then
			self:EmitSound("npc/zombie/zombie_hit.wav",460,math.random(80,120))
		end
	    self:EmitSound( "npc/ichthyosaur/snap_miss.wav",460,math.random(80,120))
		timer.Create( "melee_done_timer" .. self.Entity:EntIndex( ), 1, 1, setmeleefalse )
	end
	//---------------
	local function Attack_SideSwipe()
		if self:Health() < 0 then return end
		self.alert_counter = self.alert_counter + 50
		local entstoattack = ents.FindInSphere(self:GetPos() + self:GetForward()*150,78)
		local randomsound = math.random(1,3)
		local hit = false
		if entstoattack != nil then
			for _,v in pairs(entstoattack) do
				if ( (v:IsNPC() || ( v:IsPlayer() && v:Alive())) && (v != self) /*&& (v:GetClass() != self:GetClass())*/ || (v:GetClass() == "prop_physics")) then
					v:TakeDamage( self.Damage/5, self ) 
					if v:IsPlayer() then
						v:ViewPunch(Angle(math.random(-1,1)*self.Damage,math.random(-1,1)*self.Damage,math.random(-1,1)*self.Damage))
					end
					v:SetVelocity( -1 * self:GetRight() * 600 + Vector(0,0,400))
					hit = true
				end
			end
		end
		if hit == true then
			self:EmitSound("npc/zombie/zombie_hit.wav",460,math.random(80,120))
		end
	    self:EmitSound( "npc/ichthyosaur/snap_miss.wav",460,math.random(80,120))
		timer.Create( "melee_done_timer" .. self.Entity:EntIndex( ), 1, 1, setmeleefalse )
	end
	//---------------
	local function Attack_Stomp()
		if self:Health() < 0 then return end
		self.alert_counter = self.alert_counter + 30
		local entstoattack = ents.FindInSphere(self:GetPos() + self:GetForward()*75,60)
		local hit = false
		if entstoattack != nil then
			for _,v in pairs(entstoattack) do
				if ( (v:IsNPC() || ( v:IsPlayer() && v:Alive())) && (v != self) /*&& (v:GetClass() != self:GetClass())*/ || (v:GetClass() == "prop_physics")) then
					v:TakeDamage( self.Damage, self ) 
					if v:IsPlayer() then
						v:ViewPunch(Angle(math.random(-1,1)*self.Damage,math.random(-1,1)*self.Damage,math.random(-1,1)*self.Damage))
					end
					//v:SetVelocity( self:GetForward() * 100 )
					hit = true
				end
			end
		end
	      	self:EmitSound( "Rex/trex_step.wav",460,math.random(80,120))
		timer.Create( "melee_done_timer" .. self.Entity:EntIndex( ), 1, 1, setmeleefalse )
	end
	//---------------


    //---------------
    if GetConVarNumber("ai_disabled") == 0 then
		if self:GetEnemy() != nil && self:HasLOS() then
			self.alert_counter = self.alert_counter + 0.3
			if self:GetPos():Distance(self:GetEnemy():GetPos()) < 300 then 
				self.alert_counter = self.alert_counter + 1
			else
			self:UpdateEnemyMemory(self:GetEnemy(),self:GetEnemy():GetPos())
			end
		else
			self.alert_counter = self.alert_counter - 0.5
		end
		
		self.hunger_counter = self.hunger_counter - 1
		
		//print( "Think start" );
		//Get all the npc's and other entities.
		local enttable = ents.FindByClass("npc_*")
		local monstertable = ents.FindByClass("monster_*")
		table.Add(monstertable,enttable)//merge
		
		if CurTime() > self.BreathTime then
			//make the sound
			if self.alert_counter < 100 then
				local randomsound = math.random(1,4)
				if randomsound == 1 then
					self:EmitSound( self.idle1,480,math.random(94,106))
					self.BreathTime = CurTime() + 7.9
				elseif randomsound == 2 then
					self:EmitSound( self.idle2,480,math.random(94,106))
					self.BreathTime = CurTime() + 19.59
				elseif randomsound == 3 then
					self:EmitSound( self.idle3,480,math.random(94,106))
					self.BreathTime = CurTime() + 3.33
				elseif randomsound == 4 then
					self:EmitSound( self.idle4,480,math.random(94,106))
					self.BreathTime = CurTime() + 5.46
				end
			else
				local randomsound = math.random(1,10)
				if randomsound == 1 then
					self:EmitSound( self.idle1,480,math.random(94,106))
					self.BreathTime = CurTime() + 7.9
				elseif randomsound == 2 then
					self:EmitSound( self.idle2,480,math.random(94,106))
					self.BreathTime = CurTime() + 19.59
				elseif randomsound == 3 then
					self:EmitSound( self.idle3,480,math.random(94,106))
					self.BreathTime = CurTime() + 3.33
				elseif randomsound == 4 then
					self:EmitSound( self.idle4,480,math.random(94,106))
					self.BreathTime = CurTime() + 5.46
				elseif randomsound <= 10 then
					self:EmitSound( "Rex/angry1.wav",460,math.random(94,106))
					self.BreathTime = CurTime() + 2.57
				end
			end
		end
		
		//sort through each ent.
		for _, x in pairs(monstertable) do
			if (!ents) then print( "No Entities!" ); return end
			if (/*x:GetClass() != self:GetClass() &&*/ x:GetClass() != "npc_grenade_frag" && x:IsNPC()) then
				x:AddEntityRelationship( self, 1, 10 )
			end
		end
		
		if self:GetEnemy() != nil then
				//if math.random(1,15) == 1 && self:GetPos():Distance(self:GetEnemy():GetPos()) < self.LeapDistance && self:GetPos():Distance(self:GetEnemy():GetPos()) > self.MinLeapDistance && self.Leaps == true && self.Leaping == false then
				//	self:SetSchedule(schedJump)
				//	self.Leaping = true
  				//	self:SetVelocity( (self:GetEnemy():GetPos()-self:GetPos() + Vector(0,0,50)):Normalize() * self.LeapSpeed )
				//	timer.Simple(0.7,setmeleefalse)
				//end
				if (self:GetEnemy():GetPos():Distance(self:GetPos()) < 175) && self:GetEnemy() != nil && self:GetEnemy():GetClass() == "prop_ragdoll" then
					self:SetNPCState( NPC_STATE_COMBAT ) 
					if self.MeleeAttacking == false then
						self.FixedOnEnemy = true
						self:SetSchedule( SCHED_RELOAD )
						timer.Create( "melee_attack_timer" .. self.Entity:EntIndex( ), 1.5, 1, seteatfalse )
						self.MeleeAttacking = true;
					end
				elseif (self:GetEnemy():GetPos():Distance(self:GetPos()) < 100) then
					self:SetNPCState( NPC_STATE_COMBAT ) 
					if self.MeleeAttacking == false then
						if self.Leaping == false then
							self:SetSchedule( SCHED_SPECIAL_ATTACK1 )
						else
							self:EmitSound( self.attackleap)
							self:SetLocalVelocity( Vector( 0, 0, 0 ) )
						end
						timer.Create( "melee_attack_timer" .. self.Entity:EntIndex( ), 1.1, 1, Attack_Stomp )
						self.MeleeAttacking = true;
					end
				elseif (self:GetEnemy():GetPos():Distance(self:GetPos()) < 210) then
					self:SetNPCState( NPC_STATE_COMBAT ) 
					if self.MeleeAttacking == false then
						local attackrandomizer = math.random(1,5)
						if (attackrandomizer <= 3) then//bite
							if self.Leaping == false then
								if self:GetEnemy():GetPos().z > self:GetPos().z + 100 then
									self:SetSchedule( SCHED_MELEE_ATTACK2 )
									timer.Create( "melee_attack_timer" .. self.Entity:EntIndex( ), 0.4, 1, Attack_BiteHigh )
								else
									self:SetSchedule( SCHED_MELEE_ATTACK1 )
									timer.Create( "melee_attack_timer" .. self.Entity:EntIndex( ), 0.4, 1, Attack_Bite )
								end
								//self:SetLocalVelocity( Vector( 0, 0, 0 ) )
							end
							self.MeleeAttacking = true;
						elseif (attackrandomizer == 4) then//headbutt
							if self.Leaping == false then
								self:SetSchedule( SCHED_SPECIAL_ATTACK2 )
								//self:SetLocalVelocity( Vector( 0, 0, 0 ) )
							end
							timer.Create( "melee_attack_timer" .. self.Entity:EntIndex( ), 0.4, 1, Attack_Headbutt )
							self.MeleeAttacking = true;
						elseif (attackrandomizer == 5) then
							if self.Leaping == false then
								self:SetSchedule( SCHED_RANGE_ATTACK2 )
								//self:SetLocalVelocity( Vector( 0, 0, 0 ) )
							end
							timer.Create( "melee_attack_timer" .. self.Entity:EntIndex( ), 0.4, 1, Attack_SideSwipe)
							self.MeleeAttacking = true;
						end
					end
				end

				//if (self:GetEnemy():GetPos():Distance(self:GetPos()) > 1000 && self.alert_counter < 75) then
				//	self:ResetEnemy()
				//	//self.alert_counter = self.alert_counter - 1
				//end
		elseif self.FixedOnEnemy == false then
			if self.togglesearch == true then
				self.togglesearch = false
				self:FindEnemyDan()
			else
				self:FindCloseEnemies()//get guys close to me
				self.togglesearch = true
			end
			self.MeleeAttacking = false
		end
    end
    //---------------
print(self.alert_counter)
print(self.hunger_counter)
end

function ENT:SelectSchedule()
	if self.alert_counter < 50 || (self:GetEnemy() == nil || self:GetEnemy() == NULL) then //he's normal
		if self:GetEnemy() != nil && self:GetEnemy() != NULL && self:GetPos():Distance(self:GetEnemy():GetPos()) < 3000 then
			self:SetSchedule(SCHED_COMBAT_FACE)
		else
			//if self.hunger_counter < 150 then
				//self:FindFood()
			if math.random(1,3) == 1 then
			self:SetSchedule(SCHED_IDLE_WANDER)
			else
			self:SetSchedule(SCHED_IDLE_STAND)
			end
			//end
		end
	elseif self.alert_counter < 100 then //he's a little concerned, he'll sit there and roar. This will increase steadily.
		if math.random(1,5) != 1 then
			self:SetSchedule( SCHED_COMBAT_FACE )
		elseif self:GetPos():Distance(self:GetEnemy():GetPos()) < 1500 then
			self:PrintBehaviour( "Rexy: RAWR" )
			self:Roar();
		end
	else //he's pissed, he'll search for you until you get out of range.
		//self:UpdateEnemyMemory(self:GetEnemy(),self:GetEnemy():GetPos())
		self:SetSchedule( SCHED_CHASE_ENEMY )
		self.Chasing = true
	end
end

function ENT:OnTakeDamage(dmg)
   if self.Bleeds == true then
	self:SpawnBlood(dmg)
   end
   
   self:SetHealth(self:Health() - dmg:GetDamage())
   if self:Health() <= 0 && !self.dead then //run on death
	self.dead = true;
	self:KilledDan()
	return
   end
   
	if self.alert_counter < 100 then
		local randomsound = math.random(1,3)
		if randomsound == 1 then
	      self:EmitSound( self.alert1,500,math.random(94,106))
		elseif randomsound == 2 then
	      self:EmitSound( self.alert2,500,math.random(94,106))
		elseif randomsound == 3 then
	      self:EmitSound( self.alert3,500,math.random(94,106))
		end
	end
	
	if self.Flinches == true && dmg:GetDamage() > 40 then
		self:EmitSound( self.hurt1,500,math.random(94,106))
		self.alert_counter = self.alert_counter + 110
		if self:Health() < self.health/4 then
			self:PrintBehaviour( "Rexy: OUCH! Fuck this, I'm outta here!" )
			self:SetSchedule( SCHED_CHASE_ENEMY_FAILED )
		else
			self:PrintBehaviour( "Rexy: OUCH!" )
			self:SetSchedule(SCHED_BIG_FLINCH)
		end
	else
		if self:Health() < self.health/3 || self:HasCondition(COND_ENEMY_UNREACHABLE) then
			self:SetSchedule( SCHED_CHASE_ENEMY_FAILED )
		end
		self.alert_counter = self.alert_counter + 70
	end
	
	if self:GetEnemy() != nil then
		if ( dmg:GetAttacker():GetPos():Distance(self:GetPos()) < self:GetEnemy():GetPos():Distance(self:GetPos()) ) && (math.random(1,4) == 1 || self.Alerted == false) then
			self:AddEntityRelationship( dmg:GetAttacker(), 1, 10 )
			self:SetEnemy( dmg:GetAttacker() )
			self.FixedOnEnemy = false
			self.Alerted = true
			self.Chasing = true
		end//doesn't change enemy if we have an enemy and it's closer.
	else
		self:AddEntityRelationship( dmg:GetAttacker(), 1, 10 )
		self:SetEnemy( dmg:GetAttacker() )
		self.FixedOnEnemy = false
		self.Alerted = true
		self.Chasing = true
	end
	
	if self.Alerted == false then
		self:PrintBehaviour( "Rexy: Ouch! I'm pissed now!" )
		self:SetNPCState( NPC_STATE_COMBAT ) 
		self:SetSchedule( SCHED_CHASE_ENEMY )
	end
	

end


function ENT:FindFood()
	local MyNearbyTargets = ents.FindInSphere(self:GetPos(),1300)
	if (!MyNearbyTargets) then print( "No Targets!" ); return end
	for k,v in pairs(MyNearbyTargets) do
	    if v:GetClass() == "prop_ragdoll" then
			self:ResetEnemy()
			self:AddEntityRelationship( v, 1, 10 )
			self:SetEnemy(v)
			self:SetSchedule(SCHED_CHASE_ENEMY)
			self:UpdateEnemyMemory(v,v:GetPos())
			self.Alerted = true
			self.Chasing = false
			self.FixedOnEnemy = true
			Msg( "Found enemy" )
			//self.alert_counter = self.alert_counter + 10
	      	return
	    end
	end
	self:SetSchedule(SCHED_IDLE_WANDER)
end

function ENT:FindEnemyDan()
	local MyNearbyTargets = ents.FindInCone(self:GetPos(),self:GetForward(),1500,85)
	if (!MyNearbyTargets) then print( "No Targets!" ); return end
	for k,v in pairs(MyNearbyTargets) do
	    if (v:IsNPC() || (v:IsPlayer() && self.PlayerFriendly == 0 && GetConVarNumber("ai_ignoreplayers") == 0)) && v != self then
			if self:HasLOS() then print( "can't see" );  break end
			
			//self:ResetEnemy()
			self:AddEntityRelationship( v, 1, 10 )
			self:SetEnemy(v)
			self:UpdateEnemyMemory(v,v:GetPos())
			self.Chasing = false
			Msg( "Found enemy" )
			self.alert_counter = self.alert_counter + 10
			self:PrintBehaviour( "Rexy: Whats that?" )
			if self.Alerted == false then
				self:SetNPCState( NPC_STATE_ALERT ) 
				self:SetSchedule( SCHED_COMBAT_FACE )
				self:EmitSound( "Rex/surprised.wav",460,math.random(104,108))
			end
			self.Alerted = true
			return
		end
	end
end

function ENT:ResetEnemy()
	/*
	local enttable = ents.FindByClass("npc_*")
	local monstertable = ents.FindByClass("monster_*")
	table.Add(monstertable,enttable)//merge

	//sort through each ent.
	for _, x in pairs(monstertable) do
		//print(x)
		if (!ents) then print( "No Entities!" ); return end
		if (x:GetClass() != self:GetClass()) then
			self:AddEntityRelationship( x, 2, 10 )
		end
	end
	self:SetEnemy(NULL)
	self:AddRelationship("player D_NU 10")
	Msg( "Reset enemy" )
	*/
end

function ENT:TaskFinished()
	return true
end 

function ENT:FindCloseEnemies()
	local MyNearbyTargets = ents.FindInCone(self:GetPos(),self:GetForward(),250,85)
	//local ClosestDistance = 3000
	if (!MyNearbyTargets) then print( "No Targets!" ); return end
	for k,v in pairs(MyNearbyTargets) do
	    if (v:IsNPC() || (v:IsPlayer() && self.PlayerFriendly == 0 && GetConVarNumber("ai_ignoreplayers") == 0)) && v != self then
			if self:HasLOS() then print( "can't see" );  break end
		
			self:AddEntityRelationship( v, 1, 10 )
			self:SetEnemy(v)
			self.Alerted = true
			self.Chasing = false

			self.alert_counter = self.alert_counter + 1
			self:PrintBehaviour( "Rexy: What the?" )
	      	return
	    end
	end
end


function ENT:HasLOS()
	if self:GetEnemy() != nil then
	//local shootpos = self:GetAimVector()*(self:GetPos():Distance(self:GetEnemy():GetPos())) + self:GetPos()
	//local shootpos = self:GetEnemy():GetPos()
	local tracedata = {}

	tracedata.start = self:GetPos() + Vector(0,0,80)
	tracedata.endpos = self:GetEnemy():GetPos()
	tracedata.filter = self

	local trace = util.TraceLine(tracedata)
	if trace.HitWorld == false then
		return true
	else 
		return false
	end
	end
	print ("no enemy!")
	return false
end


function ENT:SpawnBlood(dmg)
   if self.AcidBlood then
	local entstoattack = ents.FindInSphere(self:GetPos(),75)
	for _,v in pairs(entstoattack) do
		if ( (v:IsNPC() || ( v:IsPlayer() && v:Alive())) && (v != self) && (v:GetClass() != self:GetClass()) || (v:GetClass() == "prop_physics")) then
			v:TakeDamage( 3, self ) 
		end
	self:EmitSound( "alien/alien_acid.wav" ,460,math.random(80,120))
	end
   end
   if (self.Bleeds == true) then
   	local bloodeffect = ents.Create( "info_particle_system" )
	if self.BleedsRed == true then
   		bloodeffect:SetKeyValue( "effect_name", "blood_impact_red_01" )
	else
		bloodeffect:SetKeyValue( "effect_name", "blood_impact_yellow_01")
	end
        bloodeffect:SetPos( dmg:GetDamagePosition() ) 
	bloodeffect:Spawn()
	bloodeffect:Activate() 
	bloodeffect:Fire( "Start", "", 0 )
	bloodeffect:Fire( "Kill", "", 0.1 )
   end
   
end

function ENT:PrintBehaviour(whattosay)
	local blah = player.GetAll()
	for _,v in pairs(blah) do
		v:PrintMessage(HUD_PRINTTALK,whattosay);
	end
end

function ENT:KilledDan()
	/*I Took some of silverlan's code for ragdolling, cause I didn't want to recode it :P*/
	//emit cry of death
	local deathseed = math.random(1,3)
	if     deathseed == 1 then
	  self:EmitSound( self.die1,460,math.random(80,120))
	elseif deathseed == 2 then
	  self:EmitSound( self.die2,460,math.random(80,120))
	elseif deathseed == 3 then
	  self:EmitSound( self.die3,460,math.random(80,120))
	end

	//create ragdoll
	local ragdoll = ents.Create( "prop_ragdoll" )
	ragdoll:SetModel( self:GetModel() )
	ragdoll:SetPos( self:GetPos() )
	ragdoll:SetAngles( self:GetAngles() )
	ragdoll:Spawn()
	ragdoll:SetSkin( self:GetSkin() )
	ragdoll:SetColor( self:GetColor() )
	ragdoll:SetMaterial( self:GetMaterial() )
	
	//my code
	undo.ReplaceEntity(self,ragdoll)
	cleanup.ReplaceEntity(self,ragdoll)

	//ignight ragdoll if on fire.
	if self:IsOnFire() then ragdoll:Ignite( math.Rand( 8, 10 ), 0 ) end

	//position bones the same way.
        for i=1,128 do
		local bone = ragdoll:GetPhysicsObjectNum( i )
		if ValidEntity( bone ) then
			local bonepos, boneang = self:GetBonePosition( ragdoll:TranslatePhysBoneToBone( i ) )
			bone:SetPos( bonepos )
			bone:SetAngle( boneang )
		end
	end
	if( GetConVarNumber("ai_keepragdolls") == 0 ) then
		ragdoll:SetCollisionGroup( 1 )//COLLISION_GROUP_DEBRIS )
		ragdoll:Fire( "FadeAndRemove", "", 7)
	end
	self:Remove()
end