From a22cbeddc5f8fb61e87a30aa14ba354de5cf4431 Mon Sep 17 00:00:00 2001 From: Alexander Pickering Date: Sat, 18 Feb 2017 21:55:55 -0500 Subject: Updates --- entities/entities/art_chest/init.lua | 122 +++++++++------------- entities/entities/npc_shop/init.lua | 20 +++- entities/entities/npc_townie/init.lua | 174 ++++++++++++++++++++++---------- entities/entities/npc_townie/shared.lua | 82 ++------------- entities/weapons/hands.lua | 101 ++++++------------ 5 files changed, 221 insertions(+), 278 deletions(-) (limited to 'entities') diff --git a/entities/entities/art_chest/init.lua b/entities/entities/art_chest/init.lua index cd39eab..f33b57a 100644 --- a/entities/entities/art_chest/init.lua +++ b/entities/entities/art_chest/init.lua @@ -1,88 +1,62 @@ -AddCSLuaFile( "cl_init.lua" ) -AddCSLuaFile( "shared.lua" ) - +AddCSLuaFile("cl_init.lua") +AddCSLuaFile("shared.lua") include("shared.lua") -local invfuncs = include("../../../gamemode/shared/inventory_common.lua") +local inv = nrequire("inventory/inventory.lua") +local track = nrequire("inventory/sv_invtracker.lua") +--local invfuncs = include("../../../gamemode/shared/inventory_common.lua") function ENT:Initialize() - self.Openedby = {} - self:SetModel("models/props_junk/Rock001a.mdl") - self:PhysicsInit( SOLID_VPHYSICS ) - self:SetMoveType( MOVETYPE_NONE ) - self:SetSolid( SOLID_VPHYSICS ) - self:SetCollisionGroup( COLLISION_GROUP_INTERACTIVE ) - self:SetUseType(SIMPLE_USE) - - local phys = self:GetPhysicsObject() - self.Inventory = {} - self.Inventory.Backpacks = {} - self.Inventory.Backpacks[1] = invfuncs.CreateBackpack("Chest",3,3) - - phys:EnableMotion(false) - phys:Sleep() - - self.StoredItems = {} + self.Openedby = {} + self:SetModel("models/props_junk/Rock001a.mdl") + self:PhysicsInit(SOLID_VPHYSICS) + self:SetMoveType(MOVETYPE_NONE) + self:SetSolid(SOLID_VPHYSICS) + self:SetCollisionGroup(COLLISION_GROUP_INTERACTIVE) + self:SetUseType(SIMPLE_USE) + local phys = self:GetPhysicsObject() + phys:EnableMotion(false) + phys:Sleep() + + self.data = { + inventories = {} + } + + local myinv = inv.CreateInventory("Shaped Inventory") + myinv.Owner = self + + timer.Simple(0, function() + print("Chest has creation id", self:GetCreationID()) + myinv.id = self:GetCreationID() + self.data.inventories[myinv.id] = myinv + end) --Wait until the entity is initalized + + self.Observers = {} end -local uans = { - "openchestinv", - "closechestinv", - "requestchestinv", - "requestchestname", - "informchestname", -} -for k,v in pairs(uans) do - util.AddNetworkString(v) -end +local uans = {"openchestinv", "closechestinv"} -net.Receive("requestchestname",function(ln,ply) - local e = net.ReadEntity() - local s = e:GetName() - net.Start("informchestname") - net.WriteEntity(e) - net.WriteString(s) - net.Send(ply) -end) - -net.Receive("requestchestinv",function(ln,ply) - print("Received request for chest") - local chest = net.ReadEntity() - print("Chest:",chest) - chest:SendInventory(ply) -end) +for k, v in pairs(uans) do + util.AddNetworkString(v) +end -net.Receive("closechestinv",function(ln,ply) - local chest = net.ReadEntity() - print("closeing chest of ", ply,chest) - chest.Openedby[ply] = nil +net.Receive("closechestinv", function(ln, ply) + local chest = net.ReadEntity() + print("closeing chest of ", ply, chest) + local obsid = chest.Observers[ply] + chest.data.inventories[chest:GetCreationID()]:RemoveObserver(obsid) + chest.Observers[ply] = nil end) +--NOTE:Why is it important to allow a table to this argument? function ENT:SendInventory(towho) - print("Sending inventory to",towho,type(towho)) - if type(towho) == "table" then - print("who:") - PrintTable(towho) - print("opendby:") - PrintTable(self.Openedby) - assert(#towho != 0,"Sending inventory to no players!") - else - print("towho is not a table!") - self.Openedby[towho] = true - end - print("about to start net message") - net.Start("openchestinv") - net.WriteEntity(self) - invfuncs.SerializeBackpack(self.Inventory.Backpacks[1]) - net.Send(towho) -end - -function ENT:SynchronizeInventory() - print("Entity synchronize called") - self:SendInventory(table.GetKeys(self.Openedby)) + print("Sending inventory to ", towho) + local observer = track.MakeInventoryObserver(towho, self:GetCreationID()) + local obsid = self.data.inventories[self:GetCreationID()]:AddObserver(observer) + self.Observers[towho] = obsid + track.NotifyPlayerOfInventory(towho, self.data.inventories[self:GetCreationID()]) end function ENT:Use(ply) - print("Ply opened:",ply) - self.Openedby[ply] = true - self:SendInventory(ply) + print("Ply opened:", ply) + self:SendInventory(ply) end diff --git a/entities/entities/npc_shop/init.lua b/entities/entities/npc_shop/init.lua index a6d49d3..796b389 100644 --- a/entities/entities/npc_shop/init.lua +++ b/entities/entities/npc_shop/init.lua @@ -3,8 +3,12 @@ AddCSLuaFile( "shared.lua" ) include("shared.lua") -function CreateRandomLoot(time) - +for k,v in pairs({ + "art_openshop", + "art_buyitem", + "art_sellitem", +}) do + util.AddNetworkString(v) end function ENT:Initialize() @@ -35,7 +39,17 @@ function ENT:Initialize() self:SetUseType( SIMPLE_USE ) end +function OpenShop(tbl,ply) + print("Called openshop!") + print("tbl was") + PrintTable(tbl) + if CLIENT then return end + net.Start("art_openshop") + net.WriteTable(tbl) + net.Send(ply) +end + function ENT:Use(ply) --TODO:Fix shop - ART.OpenShop(self.shopitems,ply) + OpenShop(self.shopitems,ply) end diff --git a/entities/entities/npc_townie/init.lua b/entities/entities/npc_townie/init.lua index a444348..a110699 100644 --- a/entities/entities/npc_townie/init.lua +++ b/entities/entities/npc_townie/init.lua @@ -2,79 +2,147 @@ AddCSLuaFile( "cl_init.lua" ) AddCSLuaFile( "shared.lua" ) -include('shared.lua') +include("shared.lua") + +util.AddNetworkString( "opennpcdialog" ) +util.AddNetworkString( "closenpcdialog" ) +util.AddNetworkString( "updatenpcdialog") function ENT:Initialize() - --print("NPC spawned!") - --self:SetMoveType(MOVETYPE_VPHYSICS) - self:SetSolid(SOLID_BBOX ) - self:SetCollisionGroup(COLLISION_GROUP_NPC ) + --print("NPC spawned!") + --self:SetMoveType(MOVETYPE_VPHYSICS) + self:SetSolid(SOLID_BBOX ) + self:SetCollisionGroup(COLLISION_GROUP_NPC ) self:SetHealth(50) - self.loco:SetDeathDropHeight(500) //default 200 - self.loco:SetAcceleration(400) //default 400 - self.loco:SetDeceleration(400) //default 400 - self.loco:SetStepHeight(18) //default 18 - self.loco:SetJumpHeight(25) //default 58 - self.loco:SetDesiredSpeed( 50 ) + self.loco:SetDeathDropHeight(500) --default 200 + self.loco:SetAcceleration(400) --default 400 + self.loco:SetDeceleration(400) --default 400 + self.loco:SetStepHeight(18) --default 18 + self.loco:SetJumpHeight(25) --default 58 + self.loco:SetDesiredSpeed( 50 ) - self.nodereached = false + self.nodereached = false - self.lastdooropen = CurTime() + self.lastdooropen = CurTime() - if(self.Model) then self:SetModel(self.Model) - else print("NPC created without model, this might be a bug!") end + if self.Model then self:SetModel(self.Model) + else print("NPC created without model, this might be a bug!") end - if self.Pos then self:SetPos(self.Pos) - else print("NPC created without a position, this might be a bug!") end + if self.Pos then self:SetPos(self.Pos) + else print("NPC created without a position, this might be a bug!") end - self.talking = false + self.talking = false - if self.Name then self:SetName(self.Name) - else print("NPC created without a name! They won't be able to open doors!") end + if self.Name then self:SetName(self.Name) + else print("NPC created without a name! They won't be able to open doors!") end - if self.OnSpawn then self.OnSpawn(self) end + if self.OnSpawn then self.OnSpawn(self) end - self.allowedNodes = {} + self.allowedNodes = {} - for k,v in pairs(ents.FindByClass( "info_townienode" ) ) do - if self.NavNodes[v.Name] then - table.insert(self.allowedNodes,v) - end - end + for k,v in pairs(ents.FindByClass( "info_townienode" ) ) do + if self.NavNodes[v.Name] then + table.insert(self.allowedNodes,v) + end + end - self:SetUseType( SIMPLE_USE ) + self:SetUseType( SIMPLE_USE ) - self.DialogCursors = {} + self.DialogCursors = {} end function ENT:OnInjured(dmg) - --print("Taking some dammage") - local itempos = self:GetPos() - --print("Takeing " .. dmg:GetDamage() .. " health from our " .. self:Health()) - --self:SetHealth(self:Health() - dmg:GetDamage()) - --print("Health is now" .. self:Health()) - if self.OnDammage != nil then self:OnDammage(dmg) end + if self.OnDammage ~= nil then self:OnDammage(dmg) end +end + +local removeblock = { + ["prop_door_rotating"] = function(bot,ent) + ent:Fire("OpenAwayFrom",bot:GetName()) + timer.Simple(3,function() + ent:Fire("Close") + end) + end +} + +function ENT:BehaveUpdate(num) + if not self.BehaveThread then return end + if self.curnode and self.curnode:IsValid() then + if self.curnode:GetPos():Distance(self:GetPos()) < 5 then + if self.nodereached == false then + self.curnode.OnReached(self) + self.nodereached = true + else + if self.curnode.IsDone(self) then + print("Finished action") + self.curnode = self:selectNewNode() + self.nodereached = false + end + end + end + end + local trdata = { + ["start"] = self:GetPos() + (self:GetUp() * 72), + ["endpos"] = self:GetPos() + (self:GetUp() * 72) + (self:GetForward() * 32), + ["filter"] = self + } + local tr = util.TraceLine(trdata) + local ecl + --print("tr",tr,"tr.entity",tr.Entity) + if (tr ~= nil) and (tr.Entity ~= nil) and tr.Entity:IsValid() then + --print("Not returning") + ecl = tr.Entity:GetClass() + end + if tr.Hit and removeblock[ecl] ~= nil then + removeblock[ecl](self,tr.Entity) + end + local ok, message = coroutine.resume( self.BehaveThread ) + if not ok then + self.BehaveThread = nil + Msg( self, "error: ", message, "\n" ); + end + end function ENT:OnKilled(dmg) - if(CLIENT) then return end - if not self.Drops then return end - --print("Looks like we have some drops") - for k,v in pairs(self.Drops) do - local rng = math.random(0,100) - local itemname = self.Drops[k][1] - local itemchance = self.Drops[k][2] - local heightoffset = 10 - if rng < itemchance then - local drop = ents.Create("ws_item") - drop.Item = GetItemByName(itemname) - drop:SetModel(drop.Item.Model) - drop:SetPos(self:GetPos() + (self:GetUp()*heightoffset)) - drop:Spawn() - heightoffset = heightoffset + 10 - end - end - self:BecomeRagdoll( dmg ) + if not self.Drops then return end + --print("Looks like we have some drops") + for k,v in pairs(self.Drops) do + local rng = math.random(0,100) + local itemname = self.Drops[k][1] + local itemchance = self.Drops[k][2] + local heightoffset = 10 + if rng < itemchance then + local drop = ents.Create("ws_item") + drop.Item = GetItemByName(itemname) + drop:SetModel(drop.Item.Model) + drop:SetPos(self:GetPos() + (self:GetUp() * heightoffset)) + drop:Spawn() + heightoffset = heightoffset + 10 + end + end + self:BecomeRagdoll( dmg ) +end + +function ENT:Use(ply) + self.DialogCursors[ply] = self.getDialogFor(ply) + -- + if self.oyaw ~= 0 and self.oacc ~= 0 then + self.oyaw, self.oacc,self.onod = self.loco:GetMaxYawRate(), self.loco:GetAcceleration(), self.curnode + end + self.loco:SetAcceleration(0) + self.loco:SetVelocity(Vector(0,0,0)) + self.loco:SetMaxYawRate(9999990) + self.loco:FaceTowards(ply:GetPos()) + self.loco:SetMaxYawRate(0) + timer.Simple(0.5,function() + --self.loco:FaceTowards(ply:GetPos()) + + + end) + net.Start("opennpcdialog") + net.WriteEntity(self) + net.WriteTable(SendableDialog(self.DialogCursors[ply])) + net.Send(ply) end diff --git a/entities/entities/npc_townie/shared.lua b/entities/entities/npc_townie/shared.lua index e7f02c7..bb07b51 100644 --- a/entities/entities/npc_townie/shared.lua +++ b/entities/entities/npc_townie/shared.lua @@ -27,62 +27,18 @@ function ENT:selectNewNode() return rnode end -local removeblock = { - ["prop_door_rotating"] = function(bot,ent) - ent:Fire("OpenAwayFrom",bot:GetName()) - timer.Simple(3,function() - ent:Fire("Close") - end) - end -} - -function ENT:BehaveUpdate(num) - if not self.BehaveThread then return end - if self.curnode and self.curnode:IsValid() then - if self.curnode:GetPos():Distance(self:GetPos()) < 5 then - if self.nodereached == false then - self.curnode.OnReached(self) - self.nodereached = true - else - if self.curnode.IsDone(self) then - print("Finished action") - self.curnode = self:selectNewNode() - self.nodereached = false - end - end - end - end - local trdata = { - ["start"] = self:GetPos() + (self:GetUp()*72), - ["endpos"] = self:GetPos() + (self:GetUp()*72) + (self:GetForward()*32), - ["filter"] = self - } - local tr = util.TraceLine(trdata) - local ecl - --print("tr",tr,"tr.entity",tr.Entity) - if (tr ~= nil) and (tr.Entity ~= nil) and tr.Entity:IsValid() then - --print("Not returning") - ecl = tr.Entity:GetClass() - end - if tr.Hit and removeblock[ecl] ~= nil then - removeblock[ecl](self,tr.Entity) - end - local ok, message = coroutine.resume( self.BehaveThread ) - if not ok then - self.BehaveThread = nil - Msg( self, "error: ", message, "\n" ); - end -end + + function ENT:RunBehaviour() while true do --print("In runbehaviour") - local opts = { lookahead = 300, + local opts = { lookahead = 50, tolerance = 20, - draw = true, + draw = false, maxage = 1, - repath = 0.1 } + repath = 10 } if self.curnode == nil or not self.curnode:IsValid() or self.curnode:GetPos():Distance(self:GetPos()) < 5 then self.curnode = self:selectNewNode() end @@ -101,11 +57,7 @@ function ENT:RunBehaviour() end end -if SERVER then - util.AddNetworkString( "opennpcdialog" ) - util.AddNetworkString( "closenpcdialog" ) - util.AddNetworkString( "updatenpcdialog") -end + --Fix the dialog so we aren't sending functions function SendableDialog(tbl) @@ -137,28 +89,6 @@ net.Receive("updatenpcdialog",function(len,ply) net.Send(ply) end) -function ENT:Use(ply) - self.DialogCursors[ply] = self.getDialogFor(ply) - -- - if self.oyaw ~= 0 and self.oacc ~= 0 then - self.oyaw, self.oacc,self.onod = self.loco:GetMaxYawRate(), self.loco:GetAcceleration(), self.curnode - end - self.loco:SetAcceleration(0) - self.loco:SetVelocity(Vector(0,0,0)) - self.loco:SetMaxYawRate(9999990) - self.loco:FaceTowards(ply:GetPos()) - self.loco:SetMaxYawRate(0) - timer.Simple(0.5,function() - --self.loco:FaceTowards(ply:GetPos()) - - - end) - net.Start("opennpcdialog") - net.WriteEntity(self) - net.WriteTable(SendableDialog(self.DialogCursors[ply])) - net.Send(ply) -end - function ENT:OnClose() self.loco:SetMaxYawRate(self.oyaw) self.loco:SetAcceleration(self.oacc) diff --git a/entities/weapons/hands.lua b/entities/weapons/hands.lua index d55c416..1354941 100644 --- a/entities/weapons/hands.lua +++ b/entities/weapons/hands.lua @@ -36,81 +36,38 @@ function SWEP:ShouldDropOnDie() return false end -local Box = Vector(8,8,8) - function SWEP:PrimaryAttack() - if not self.Owner.Inventory then return end - local rightitem = self.Owner.Inventory.Equiped["Left"] - if rightitem ~= false and rightitem.onClick ~= nil then - rightitem:onClick(self.Owner) - end - --[[ - if (CLIENT and !IsFirstTimePredicted()) then return end - if (!self.Owner.Weapons or !self.Owner.Weapons[self.Owner.Select]) then return end - if (self.Owner.CD and self.Owner.CD > CurTime()) then return end - - local item = self.Owner.Weapons[self.Owner.Select].Item - - if (!item or !item.OnPrimary) then return end - - self.Owner:SetAnimation( PLAYER_ATTACK1 ) - - local Trace = { - start = self.Owner:GetShootPos(), - endpos = self.Owner:GetShootPos()+self.Owner:GetAimVector()*item.Range, - filter = self.Owner, - mins = Box*-1, - maxs = Box, - } - - local Tr = util.TraceHull(Trace) - - item:OnPrimary(self.Owner,Tr) - - self.Owner.CD = CurTime()+item.CD - ]] + if CLIENT then return end + + --Make sure we have an equipment inventory + if not self.Owner then return end + if not self.Owner.data then return end + if not self.Owner.data.inventories then return end + if not self.Owner.data.inventories[1] then return end + local eqpd = self.Owner.data.inventories[1] + + --Get the weapon we want to fire, and fire it! + local weapon = eqpd:Get({"Left Hand"}) or eqpd:Get({"Dual"}) + if not weapon then return end --Make sure we have a weapon + if weapon.onClick ~= nil then + weapon:onClick(self.Owner) + end end function SWEP:SecondaryAttack() - local rightitem = self.Owner.Inventory.Equiped["Right"] - if rightitem ~= false and rightitem.onClick ~= nil then - rightitem:onClick(self.Owner) - end -end - -if (CLIENT) then - local Zero = Vector(1,1,1) - --[[ - function SWEP:DrawWorldModel() - if (!self.Owner.Weapons or !self.Owner.Weapons[self.Owner.Select]) then return end - - local item = self.Owner.Weapons[self.Owner.Select].Item - - for k,v in pairs(item.Structure) do - local ID = self.Owner:LookupBone(v.Bone) - local Pos,Ang = self.Owner:GetBonePosition(ID) - - local Offset = v.Pos*1 - Offset:Rotate(Ang) - Pos = Pos + Offset - - local Dang = Ang*1 - - Ang:RotateAroundAxis(Dang:Right(),v.Ang.p) - Ang:RotateAroundAxis(Dang:Up(),v.Ang.y) - Ang:RotateAroundAxis(Dang:Forward(),v.Ang.r) - - self.MOB:SetModel(v.Model) - self.MOB:SetRenderOrigin(Pos) - self.MOB:SetRenderAngles(Ang) - - local mat = Matrix() - mat:Scale( v.Size or Zero ) - - self.MOB:EnableMatrix( "RenderMultiply", mat ) - self.MOB:SetupBones() - self.MOB:DrawModel() - end + if CLIENT then return end + + --Make sure we have an equipment inventory + if not self.Owner then return end + if not self.Owner.data then return end + if not self.Owner.data.inventories then return end + if not self.Owner.data.inventories[1] then return end + local eqpd = self.Owner.data.inventories[1] + + --Get the weapon we want to fire, and fire it! + local weapon = eqpd:Get({"Right Hand"}) or eqpd:Get({"Dual"}) + if not weapon then return end --Make sure we have a weapon + if weapon.onClick ~= nil then + weapon:onClick(self.Owner) end - ]] end -- cgit v1.2.3-70-g09d2