-- File: lua/entities/ai_npc/shared.lua
ENT.Type = "ai"
ENT.Base = "base_ai"
ENT.PrintName = "Ollama NPC"
ENT.Author = "Assistant"
ENT.Spawnable = true
ENT.AdminSpawnable = true
-- File: lua/entities/ai_npc/init.lua
AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include("shared.lua")
function ENT:Initialize()
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))
self:SetUseType(SIMPLE_USE)
self:DropToFloor()
self.NextThinkTime = 0
end
function ENT:OnTakeDamage(dmginfo)
self:SetHealth(self:Health() - dmginfo:GetDamage())
if self:Health() <= 0 then
self:Remove()
end
end
function ENT:Think()
if CurTime() >= self.NextThinkTime then
self:PerformAction()
self.NextThinkTime = CurTime() + math.random(5, 10)
end
end
function ENT:PerformAction()
local data = {
position = self:GetPos(),
health = self:Health(),
actions = {"Move to player", "Move away", "Equip shotgun", "Random move", "Say hello"},
items = {"weapon_pistol", "weapon_shotgun", "weapon_crowbar"}
}
local jsonData = util.TableToJSON(data)
local url = "http://localhost:5000/ollama_endpoint"
http.Post(url, {input = jsonData}, function(result)
local response = util.JSONToTable(result)
if response and response.command then
self:ExecuteCommand(response.command)
end
end)
end
function ENT:ExecuteCommand(command)
if command == "Move to player" then
local players = player.GetAll()
if #players > 0 then
local target = players[math.random(#players)]
self:SetLastPosition(target:GetPos())
self:SetSchedule(SCHED_FORCED_GO_RUN)
end
elseif command == "Move away" then
local newPos = self:GetPos() + VectorRand() * 500
self:SetLastPosition(newPos)
self:SetSchedule(SCHED_FORCED_GO_RUN)
elseif command == "Equip shotgun" then
self:Give("weapon_shotgun")
elseif command == "Random move" then
local newPos = self:GetPos() + VectorRand() * 300
self:SetLastPosition(newPos)
self:SetSchedule(SCHED_FORCED_GO)
elseif command == "Say hello" then
self:EmitSound("vo/npc/male01/hi0"..math.random(1,2)..".wav")
end
end
-- File: lua/entities/ai_npc/cl_init.lua
include("shared.lua")
function ENT:Draw()
self:DrawModel()
end
-- To use this addon, place the 'ai_npc' folder inside 'addons' directory of Garry's Mod.
-- Then, in-game, you can spawn the NPC from the entities menu under 'Other' or the specified category.