diff options
| author | Alexander Pickering <alexandermpickering@gmail.com> | 2017-04-02 20:05:56 -0400 |
|---|---|---|
| committer | Alexander Pickering <alexandermpickering@gmail.com> | 2017-04-02 20:05:56 -0400 |
| commit | 191ba416c8b611ea4901cead138789a357c56134 (patch) | |
| tree | b30ae3473c16028a14d3ed0c80633f360cc1c914 /gamemode/core/inventory | |
| parent | a22cbeddc5f8fb61e87a30aa14ba354de5cf4431 (diff) | |
| download | artery-191ba416c8b611ea4901cead138789a357c56134.tar.gz artery-191ba416c8b611ea4901cead138789a357c56134.tar.bz2 artery-191ba416c8b611ea4901cead138789a357c56134.zip | |
I finally had some time to work on this... dependency added on bobbleheadbob's zone addon
Diffstat (limited to 'gamemode/core/inventory')
| -rw-r--r-- | gamemode/core/inventory/common/items.lua | 31 | ||||
| -rw-r--r-- | gamemode/core/inventory/common/weapons.lua | 126 | ||||
| -rw-r--r-- | gamemode/core/inventory/inventory.lua | 33 | ||||
| -rw-r--r-- | gamemode/core/inventory/item.lua | 8 | ||||
| -rw-r--r-- | gamemode/core/inventory/sv_invtracker.lua | 78 |
5 files changed, 243 insertions, 33 deletions
diff --git a/gamemode/core/inventory/common/items.lua b/gamemode/core/inventory/common/items.lua new file mode 100644 index 0000000..03113f5 --- /dev/null +++ b/gamemode/core/inventory/common/items.lua @@ -0,0 +1,31 @@ + +local items = {} + +local function drop_provided(ent,invid,frompos) + assert(CLIENT,"requested to drop an item when we are not the client!") + net.Start("art_RequestInvDrop") + net.WriteEntity(ent) + net.WriteUInt(invid,32) + net.WriteTable(frompos) + net.SendToServer() +end + +local function drop_self(tbl) + +end + +function items.DropItem(ent_or_tbl,invid,frompos) + if type(ent_or_tbl) == "table" then + drop_self(ent_or_tbl) + else + drop_provided(ent_or_tbl,invid,frompos) + end + assert(CLIENT,"requested to drop an item when we are not the client!") + net.Start("art_RequestInvDrop") + net.WriteEntity(ent) + net.WriteUInt(invid,32) + net.WriteTable(frompos) + net.SendToServer() +end + +return items diff --git a/gamemode/core/inventory/common/weapons.lua b/gamemode/core/inventory/common/weapons.lua new file mode 100644 index 0000000..9e6eead --- /dev/null +++ b/gamemode/core/inventory/common/weapons.lua @@ -0,0 +1,126 @@ +--[[ + Some common functionality that many weapons need +]] + +local com = {} + +--- Finds the direction a player is moveing. +-- @param player The player to find the move direction of +-- @return The string "forward", "backward", "right", or "left" +function com.playermovedir(player) + local vel = player:GetVelocity():GetNormalized() + vel.z = 0 + local swings = { + {player:GetForward(),"forward"}, + {-player:GetForward(),"backward"}, + {player:GetRight(),"right"}, + {-player:GetRight(),"left"} + } + table.sort(swings,function(a,b) + return vel:Dot(a[1]) > vel:Dot(b[1]) + end) + return swings[1][2] +end + +--- The arc swing of a weapon. +-- Finds anything that a weapon should hit in it's swing, and calls a function on it. +-- @param player The player that's swinging the weapon +-- @param tiems A table of times that the trace calculations should be done at, this table needs to be the same length as the positions table +-- @param positions The position offsets from the player that swung that should be the start/end points of the arc +-- @param onhit A function to call on any entities that were hit in the swing of the weapon. +function com.swingarc(player,times,positions,onhit) + local positionpoints = {} + --table.insert(positionset,positionpoints) + for k,v in ipairs(times) do + timer.Simple(v,function() + ART.TraceWeapon = true + ART.TraceStart = CurTime() + local add = Vector(0,0,64) + if player:Crouching() then + add = Vector(0,0,32) + end + print("positions[k]",positions[k],"playerpos",player:GetPos(),"add",add) + local weaponpos = positions[k] + player:GetPos() + add + table.insert(positionpoints,weaponpos) + if #positionpoints > 1 then + --print("Trace from ", positionpoints[#positionpoints-1], " to ", positionpoints[#positionpoints]) + local tr = util.TraceLine({ + start = positionpoints[#positionpoints-1], + endpos = positionpoints[#positionpoints], + }) + onhit(tr) + end + end) + end + timer.Simple(times[#times],function() + net.Start("setwepswing") + net.WriteTable(positionpoints) + net.Broadcast() + --print("Inserted swing, drawn positions are now:") + --PrintTable(positionset) + ART.TraceWeapon = false + end) +end + +--Creates a table of ["forward|backward|left|right"] = function() +--When called, the function does an attack. +function com.createattackstable(tbl_attackdata,attacker) + local movementtbl = {} + for k,v in pairs(tbl_attackdata) do + movementtbl[k] = function() + attacker:SetLuaAnimation(v.anim) + timer.Simple(v.animtime,function() + attacker:StopLuaAnimation(v.anim) + end) + local times, pos = {},{} + for i,j in pairs(v.data) do + times[i] = 1 + j[1] + pos[i] = Vector(j[2]) + pos[i]:Rotate(attacker:GetAimVector():Angle()) + end + if pos[1] == nil then return end + com.swingarc(attacker,times,pos,function(tr) + if not tr.Hit then return end + if tr.Entity.Blocking ~= nil and tr.Entity.Blocking == k then + eff() + elseif tr.Entity.TakeDamage ~= nil and tr.Entity ~= attacker then + tr.Entity:TakeDamage(v.dammage,attacker,attacker:GetActiveWeapon()) + end + end) + end + end + + --Empty functions for attacks not defined + for k,v in pairs({"left","right","forward","backward"}) do + if movementtbl[k] == nil then + movementtbl[k] = function() end + end + end + + return movementtbl +end + +if SERVER then + util.AddNetworkString("setwepswing") +end +local positionset = {} + +net.Receive("setwepswing",function() + positionset = net.ReadTable() + print("Got message to read positionset, is now") + PrintTable(positionset) +end) + +hook.Add( "HUDPaint", "weaponswings", function() + cam.Start3D() -- Start the 3D function so we can draw onto the screen. + local v = positionset + for i = 1,#v-1 do + render.DrawLine( v[i], v[i + 1], Color(255,0,0,255), false ) + render.DrawLine( v[i], v[i] + Vector(0,0,20),Color(0,255,0,255),false) + end + --render.SetMaterial( material ) -- Tell render what material we want, in this case the flash from the gravgun + --render.DrawSprite( pos, 16, 16, white ) -- Draw the sprite in the middle of the map, at 16x16 in it's original colour with full alpha. + cam.End3D() +end ) + +return com diff --git a/gamemode/core/inventory/inventory.lua b/gamemode/core/inventory/inventory.lua index b4c025c..6b6f6f4 100644 --- a/gamemode/core/inventory/inventory.lua +++ b/gamemode/core/inventory/inventory.lua @@ -1,20 +1,33 @@ --[[ Public functions: RegisterInventory(tbl_inventory) ::nil + Registers a new inventory prototype, see below CreateInventory(string_name) ::table_inventory + Creates a new inventory be sure to set the .owner and .id fields! CreateInventoryFromData(string_name,string_data)::table_inventory) + Just deserializes an inventory. You still need to set .owner and .id! DeriveInventory(string_name) ::table_inventory + Creates a new inventory from an old, allows for heiarchy. Inventories have the following structure - field returns description - inv.Name ::string The name! - inv:FindPlaceFor(item) ::table_position or nil Finds a place for the item - inv:CanFitIn(table_position,item) ::boolean Check if the item can fit in the position - inv:Put(table_position,item) ::nil Put an item in at the position - inv:Has(string_or_compare_func) ::table_position or nil find an item in the inventory - inv:Remove(position) ::table_item Remove an item from the position - inv:Get(position) ::table_item Get the item at a position - inv:Serialize() ::string Serialize the item to store it in a db - inv:DeSerialize(str) ::table_inventory recreate the item from data in serialize + field returns + inv.Name ::string + The name! + inv:FindPlaceFor(item) ::table_position or nil + Finds a place for the item + inv:CanFitIn(table_position,item) ::boolean + Check if the item can fit in the position + inv:Put(table_position,item) ::nil + Put an item in at the position + inv:Has(string_or_compare_func) ::table_position or nil + find an item in the inventory + inv:Remove(position) ::table_item + Remove an item from the position + inv:Get(position) ::table_item + Get the item at a position + inv:Serialize() ::string + Serialize the item to store it in a db + inv:DeSerialize(str) ::table_inventory + recreate the item from data in serialize The above fields must be defined for new inventories. ----------------------------------------------------- The below are automatically made if they do not exist. diff --git a/gamemode/core/inventory/item.lua b/gamemode/core/inventory/item.lua index dd788d6..103da7f 100644 --- a/gamemode/core/inventory/item.lua +++ b/gamemode/core/inventory/item.lua @@ -35,7 +35,13 @@ end function itm.GetItemByName(name) assert(items[name] ~= nil,string.format("Attempted to get item with invalid name %q Valid item names are:\n\t%s",name,table.concat(table.GetKeys(items),"\n\t"))) - return items[name] + local item + if items[name].init then + item = items[name]:init() + else + item = table.Copy(items[name]) + end + return item end function itm.GetItemFromData(name,data) diff --git a/gamemode/core/inventory/sv_invtracker.lua b/gamemode/core/inventory/sv_invtracker.lua index 5331261..4c1fc1b 100644 --- a/gamemode/core/inventory/sv_invtracker.lua +++ b/gamemode/core/inventory/sv_invtracker.lua @@ -12,6 +12,7 @@ for k,v in pairs({ "art_CloseInventory", "art_load_player_data", "art_RequestInvMove", + "art_RequestInvDrop", }) do util.AddNetworkString(v) end --[[ net.Receive("art_ObserveInventory",function() @@ -62,6 +63,21 @@ net.Receive("art_RequestInvMove",function(len,ply) toinv:Put(topos,item) end) +net.Receive("art_RequestInvDrop",function(len,ply) + local froment = net.ReadEntity() + local frominvid = net.ReadUInt(32) + local frompos = net.ReadTable() + assert(not froment:IsPlayer() or froment == ply, "Player tried to drop an item that was from another players inventory") + local frominv = froment.data.inventories[frominvid] + local item = frominv:Get(frompos) + frominv:Remove(frompos) + local e = ents.Create("art_droppeditem") + e.Item = {Name = item.Name, Data = item:Serialize()} + e:SetPos((ply:GetForward() * 20) + ply:GetPos()) + e:Spawn() + +end) + function track.ClearInventories(ply) ply.data = {} ply.data.inventories = {} @@ -150,7 +166,7 @@ end local plymeta = FindMetaTable("Player") function plymeta:HasItem(str) - for k,v in pairs(self.inventories) do + for k,v in pairs(self.data.inventories) do local p = v:Has(str) if type(p) == "table" then return {k,p} @@ -160,23 +176,52 @@ function plymeta:HasItem(str) end function plymeta:RemoveItem(tbl) + print("Got remove table, it was",tbl) + PrintTable(tbl) local nid = tbl[1] local pos = tbl[2] - self.inventories[nid]:Remove(pos) + print("Self inventory was") + PrintTable(self.data.inventories[nid]) + self.data.inventories[nid]:Remove(pos) +end + +function plymeta:GiveItem(tbl) + for k,v in pairs(self.data.inventories) do + local p = v:FindPlaceFor(tbl) + if type(p) == "table" then + v:Put(p,tbl) + return + end + end + error("Unable to find place to put item") +end + +function plymeta:GetCredits() + return self.data.credits +end + +function plymeta:SetCredits(num) + print("Set credits called") + self.data.credits = num + net.Start("art_load_player_data") + net.WriteTable({ + credits = num + }) + net.Send(self) end function track.SendPlayerData(ply) net.Start("art_load_player_data") - net.WriteTable({}) + net.WriteTable({ + credits = ply.data.credits + }) net.Send(ply) end concommand.Add("SendMeData",function(ply,cmd,args) track.ClearInventories(ply) track.GiveInventoryTo(ply,"Equipment") - net.Start("art_load_player_data") - net.WriteTable({}) - net.Send(ply) + track.SendPlayerData(ply) end) concommand.Add("ShowMyInventories",function(ply,cmd,args) @@ -188,22 +233,11 @@ concommand.Add("AddInventory",function(ply,cmd,args) end) concommand.Add("GiveItem",function(ply,cmd,args) - local itmname = args[1] - local item = itm.GetItemByName(itmname) - local foundplacefor = false - for k,v in pairs(ply.data.inventories) do - print("Trying to find a place in ", v.Name) - local pf = v:FindPlaceFor(item) - print("It returned ", pf) - if pf ~= nil then - v:Put(pf,item) - foundplacefor = true - break - end - end - if not foundplacefor then - print("I couldn't find a place to put it!") - end + xpcall(function() + ply:GiveItem(itm.GetItemByName(args[1])) + end,function() + print("Could not find a position to put that in!") + end) end) return track |
