-- File: lua/entities/ainpc/init.lua AddCSLuaFile("clinit.lua")…
- User
-- File: lua/entities/ai_npc/init.lua
AddCSLuaFile("cl_init.lua") AddCSLuaFile("shared.lua")
include("shared.lua")
-- Define valid actions and items as entity properties for accessibility ENT.validActions = { "Move to player", "Move away", "Equip shotgun", "Random move", "Say hello", "Dance", "Jump", "Give item to player", "Attack player", "Wave", "Sit down", "Follow player", "Stop following", "Tell joke", "Compliment player", "Insult player", "Pickup item", "Drop item" }
ENT.validItems = {"weapon_pistol", "weapon_shotgun", "weapon_crowbar"}
-- Initialize the entity function ENT:Initialize() print("Initializing NPC:", self:GetName())
local models = { "models/Humans/Group01/male_01.mdl", "models/Humans/Group01/male_02.mdl", "models/Humans/Group01/male_03.mdl", "models/Humans/Group01/male_04.mdl", "models/Humans/Group01/male_05.mdl", "models/Humans/Group01/male_06.mdl", "models/Humans/Group01/male_07.mdl", "models/Humans/Group01/male_08.mdl", "models/Humans/Group01/male_09.mdl", "models/Humans/Group01/female_01.mdl", "models/Humans/Group01/female_02.mdl", "models/Humans/Group01/female_03.mdl", "models/Humans/Group01/female_04.mdl", "models/Humans/Group01/female_06.mdl", "models/Humans/Group01/female_07.mdl" } self:SetModel(models[math.random(#models)]) self:SetHullType(HULL_HUMAN) self:SetHullSizeNormal() self:SetNPCState(NPC_STATE_SCRIPT) self:SetSolid(SOLID_BBOX) self:CapabilitiesAdd(bit.bor( CAP_MOVE_GROUND, CAP_ANIMATEDFACE, CAP_USE_WEAPONS, CAP_WEAPON_MELEE_ATTACK1, CAP_INNATE_MELEE_ATTACK1 )) self:SetUseType(SIMPLE_USE) self:DropToFloor() self.NextThinkTime = 0 self:SetHealth(100) self:SetMaxHealth(100) self:SetName("NPC_" .. math.random(1000, 9999)) self.recentMessages = {} -- Ensure recentMessages is initialized self.FollowTarget = nil self.IsThinking = false -- **Flag to manage thinking state** print("NPC Initialized with model:", self:GetModel()) -- Capture 'self' in a local variable for use within the hook local entity = self -- Hook to listen to chat messages with a unique identifier using EntIndex hook.Add("PlayerSay", "ChatListener_" .. self:EntIndex(), function(ply, text) if not IsValid(ply) or not ply:IsPlayer() then return end if not entity.recentMessages then -- Initialize recentMessages if it's somehow nil entity.recentMessages = {} print("Warning: recentMessages was nil. It has been re-initialized.") end table.insert(entity.recentMessages, {player = ply:Nick(), text = text}) if #entity.recentMessages > 5 then table.remove(entity.recentMessages, 1) end end) print("Hook for PlayerSay added for NPC:", self:GetName())end
-- Handle damage taken by the NPC function ENT:OnTakeDamage(dmginfo) self:SetHealth(self:Health() - dmginfo:GetDamage()) if self:Health() <= 0 then self:Remove() end end
-- Function to schedule the next Think cycle function ENT:ScheduleNextThink() -- Schedule the next Think between 15 to 30 seconds self.NextThinkTime = CurTime() + math.random(15, 30) self.IsThinking = false -- print(os.date("[%Y-%m-%d %H:%M:%S]") .. " Next Think scheduled at", os.date("%X", self.NextThinkTime)) end
- Assistant