Advertisement
Guest User

Untitled

a guest
Sep 13th, 2013
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.97 KB | None | 0 0
  1. /*-------------------------------------------------
  2. Documentation (for other coders)
  3.  
  4. Player:SetupDefaultInventory()
  5. Creates the player's inventory table if it doesn't exist already. You should have to touch this.
  6.  
  7. Player:HasItem(<string>)
  8. Returns
  9. -------------------------------------------------*/
  10.  
  11.  
  12. if (SERVER) then
  13. AddCSLuaFile("rp_inventory.lua")
  14. AddCSLuaFile("rp_inventory_items.lua")
  15.  
  16. hook.Add("ShowHelp", "OpenInventory", function(ply)
  17. ply:ConCommand("rp_inventory")
  18. end)
  19. end
  20.  
  21. RPI = {}
  22. RPI.Items = {}
  23. RPI.Shipments = {}
  24.  
  25. include("rp_inventory_items.lua")
  26.  
  27. /*-------------------------------------------------*/
  28. MAX_ITEMS = 30 -- Maximum default number of items
  29.  
  30. PICKUP_SOUND = Sound("weapons/zoom.wav")
  31. /*-------------------------------------------------*/
  32.  
  33. // Thanks JetBoom!
  34. function Deserialize(sIn)
  35. SRL = nil
  36.  
  37. RunString(sIn)
  38.  
  39. return SRL
  40. end
  41.  
  42. local allowedtypes = {}
  43. allowedtypes["string"] = true
  44. allowedtypes["number"] = true
  45. allowedtypes["table"] = true
  46. allowedtypes["Vector"] = true
  47. allowedtypes["Angle"] = true
  48. allowedtypes["boolean"] = true
  49. local function MakeTable(tab, done)
  50. local str = ""
  51. local done = done or {}
  52.  
  53. local sequential = table.IsSequential(tab)
  54.  
  55. for key, value in pairs(tab) do
  56. local keytype = type(key)
  57. local valuetype = type(value)
  58.  
  59. if allowedtypes[keytype] and allowedtypes[valuetype] then
  60. if sequential then
  61. key = ""
  62. else
  63. if keytype == "number" or keytype == "boolean" then
  64. key ="["..tostring(key).."]="
  65. else
  66. key = "["..string.format("%q", tostring(key)).."]="
  67. end
  68. end
  69.  
  70. if valuetype == "table" and not done[value] then
  71. done[value] = true
  72. if type(value._serialize) == "function" then
  73. str = str..key..value:_serialize()..","
  74. else
  75. str = str..key.."{"..MakeTable(value, done).."},"
  76. end
  77. else
  78. if valuetype == "string" then
  79. value = string.format("%q", value)
  80. elseif valuetype == "Vector" then
  81. value = "Vector("..value.x..","..value.y..","..value.z..")"
  82. elseif valuetype == "Angle" then
  83. value = "Angle("..value.pitch..","..value.yaw..","..value.roll..")"
  84. else
  85. value = tostring(value)
  86. end
  87.  
  88. str = str .. key .. value .. ","
  89. end
  90. end
  91. end
  92.  
  93. if string.sub(str, -1) == "," then
  94. return string.sub(str, 1, #str - 1)
  95. else
  96. return str
  97. end
  98. end
  99.  
  100. function Serialize(tIn, bRaw)
  101. if bRaw then
  102. return "{"..MakeTable(tIn).."}"
  103. end
  104.  
  105. return "SRL={"..MakeTable(tIn).."}"
  106. end
  107.  
  108. local meta = FindMetaTable("Player")
  109.  
  110. function meta:SetupDefaultInventory()
  111. self.Inventory = self.Inventory or {}
  112.  
  113. self.Inventory.Items = self.Inventory.Items or {}
  114. self.Inventory.SlotSpace = self.Inventory.SlotSpace or MAX_ITEMS
  115. end
  116.  
  117. function meta:HasItem(itm)
  118. return table.HasValue(self.Inventory.Items, itm)
  119. end
  120.  
  121. function meta:GetItems()
  122. return self.Inventory.Items
  123. end
  124.  
  125. function meta:GetItemCount(itm)
  126. if (itm) then
  127. local count = 0
  128. for _, v in pairs (self.Inventory.Items) do
  129. if (v == itm) then
  130. count = count + 1
  131. end
  132. end
  133.  
  134. return count
  135. else
  136. return #self.Inventory.Items
  137. end
  138. end
  139.  
  140. function meta:AddItem(itm)
  141. table.insert(self.Inventory.Items, itm)
  142. end
  143.  
  144. function meta:RemoveItem(itm, all)
  145. for _, v in pairs (self.Inventory.Items) do
  146. if (v == itm) then
  147. table.remove(self.Inventory.Items, _)
  148.  
  149. if (!all) then
  150. break
  151. end
  152. end
  153. end
  154. end
  155.  
  156. function meta:GetMaxItems()
  157. return self.Inventory.SlotSpace
  158. end
  159.  
  160. if (SERVER) then
  161. util.AddNetworkString("RPI_OpenInventory")
  162. util.AddNetworkString("RPI_DropSpecial")
  163.  
  164. function meta:SavePlayerInventory()
  165. if (!self.Inventory) then return end
  166.  
  167. local steamid = string.gsub(string.sub(self:SteamID(), 7), ":", "_")
  168. local result = Serialize(self.Inventory)
  169. file.Write("rp_inventory/" .. steamid .. ".txt", result)
  170. end
  171.  
  172. function meta:OpenInventory()
  173. net.Start("RPI_OpenInventory")
  174. net.WriteTable(self.Inventory.Items)
  175. net.WriteInt(self.Inventory.SlotSpace, 32)
  176. net.Send(self)
  177. end
  178.  
  179. hook.Add("InitPostEntity", "InitRPInventory", function()
  180. if (!file.IsDir("rp_inventory", "DATA")) then
  181. file.CreateDir("rp_inventory")
  182. end
  183. end)
  184.  
  185. hook.Add("PlayerInitialSpawn", "InitPlayerInventory", function(ply)
  186. local steamid = string.gsub(string.sub(ply:SteamID(), 7), ":", "_")
  187.  
  188. if (file.Exists("rp_inventory/" .. steamid .. ".txt", "DATA")) then
  189. local inventory = Deserialize(file.Read("rp_inventory/" .. steamid .. ".txt", "DATA"))
  190. ply.Inventory = table.Copy(inventory)
  191. else
  192. ply:SetupDefaultInventory()
  193. end
  194. end)
  195.  
  196. hook.Add("PlayerUse", "HandleItemPickup", function(ply, ent)
  197. if (!ply:KeyDown(IN_WALK)) then return end
  198. if (!RPI.Items[ent:GetClass()]) then return false end
  199. if (ply.NextPickup and ply.NextPickup > CurTime()) then return end
  200.  
  201. ply.NextPickup = CurTime() + 0.2
  202.  
  203. if (ply:GetItemCount() >= ply:GetMaxItems()) then
  204. ply:ChatPrint("Your inventory is full.")
  205. return
  206. end
  207.  
  208. if (ent:GetClass() == "spawned_shipment") then
  209. local id = ent:Getcontents()
  210. local shipment = CustomShipments[id]
  211.  
  212. ply:AddItem({
  213. class = "spawned_shipment",
  214. name = shipment.name,
  215. count = ent:Getcount(),
  216. id = ent:Getcontents()
  217. })
  218. elseif (ent:GetClass() == "spawned_weapon") then
  219. local class = ent.weaponclass
  220. local weapon = {}
  221.  
  222. for _, v in pairs (CustomShipments) do
  223. if (v.entity == class) then
  224. weapon = v
  225. break
  226. end
  227. end
  228.  
  229. ply:AddItem({
  230. class = "spawned_weapon",
  231. name = weapon.name,
  232. model = weapon.model,
  233. weaponclass = class
  234. })
  235. elseif (ent:GetClass() == "spawned_food") then
  236. ply:AddItem({
  237. class = "spawned_food",
  238. name = "Food",
  239. model = ent:GetModel(),
  240. foodenergy = ent.FoodEnergy
  241. })
  242. elseif (ent:GetClass() == "armor") then
  243. ply:AddItem({
  244. class = "armor",
  245. name = "Armor",
  246. model = ent:GetModel(),
  247. armorcharge = ent.ArmorCharge
  248. })
  249. else
  250. ply:AddItem(ent:GetClass())
  251. end
  252. ply:SavePlayerInventory()
  253.  
  254. ply:EmitSound(PICKUP_SOUND)
  255. ent:Remove()
  256.  
  257. return false
  258. end)
  259.  
  260. hook.Add("PlayerDisconnected", "SavePlayerInventory", function(ply)
  261. ply:SavePlayerInventory()
  262. end)
  263.  
  264. concommand.Add("rp_inventory", function(ply)
  265. if (!ply.Inventory) then
  266. ply:ChatPrint("There was en error while loading your inventory. If after rejoining this doesn't fix, contact the server administrator.")
  267. return
  268. end
  269.  
  270. ply:OpenInventory()
  271. end)
  272.  
  273. concommand.Add("rp_dropitem", function(ply, cmd, args)
  274. if (!ply.Inventory) then
  275. ply:ChatPrint("There was en error while loading your inventory. If after rejoining this doesn't fix, contact the server administrator.")
  276. return
  277. end
  278.  
  279. local itm = tostring(args[1] or "")
  280. if (!RPI.Items[itm]) then return end
  281.  
  282. if (ply:GetItemCount(itm) <= 0) then return end
  283.  
  284. local pos = ply:GetShootPos()
  285. local ang = ply:GetAimVector()
  286.  
  287. local tracedata = {}
  288. tracedata.start = pos
  289. tracedata.endpos = pos + (ang * 100)
  290. tracedata.filter = ply
  291. local trace = util.TraceLine(tracedata)
  292.  
  293. local ent = ents.Create(itm)
  294. if (!ent or !ent:IsValid()) then GAMEMODE:Notify(ply, 1,4, "Invalid!") return end
  295.  
  296. if (ply.SID) then ent.SID = ply.SID end
  297. if (ent.Setowning_ent) then ent:Setowning_ent(ply) end
  298.  
  299. ent.nodupe = true
  300. ent:SetPos(trace.HitPos)
  301. ent:Spawn()
  302.  
  303. ply:RemoveItem(itm)
  304. ply:SavePlayerInventory()
  305.  
  306. ply:OpenInventory()
  307. end)
  308.  
  309. net.Receive("RPI_DropSpecial", function(length, client)
  310. local special = net.ReadTable()
  311.  
  312. local drop = false
  313. for _, item in pairs (client:GetItems()) do
  314. if (type(item) ~= "table") then continue end
  315.  
  316. if (special.count and item.count and special.count == item.count) then
  317. table.remove(client:GetItems(), _)
  318. drop = true
  319.  
  320. break
  321. elseif (special.weaponclass and item.weaponclass and special.weaponclass == item.weaponclass) then
  322. table.remove(client:GetItems(), _)
  323. drop = true
  324.  
  325. break
  326. elseif (special.foodenergy and item.foodenergy and special.foodenergy == item.foodenergy) and (special.model and item.model and special.model == item.model) then
  327. table.remove(client:GetItems(), _)
  328. drop = true
  329.  
  330. break
  331. elseif (special.armorcharge and item.armorcharge and special.armorcharge == item.armorcharge) and (special.model and item.model and special.model == item.model) then
  332. table.remove(client:GetItems(), _)
  333. drop = true
  334.  
  335. break
  336. end
  337. end
  338.  
  339. if (drop) then
  340. local pos = client:GetShootPos()
  341. local ang = client:GetAimVector()
  342.  
  343. local tracedata = {}
  344. tracedata.start = pos
  345. tracedata.endpos = pos + (ang * 100)
  346. tracedata.filter = client
  347. local trace = util.TraceLine(tracedata)
  348.  
  349. local ent = ents.Create(special.class)
  350. if (!ent or !ent:IsValid()) then GAMEMODE:Notify(ply, 1,4, "Invalid!") return end
  351.  
  352. if (special.model) then ent:SetModel(Model(special.model)) end
  353.  
  354. if (client.SID) then ent.SID = client.SID end
  355. if (ent.Setowning_ent) then ent:Setowning_ent(client) end
  356.  
  357. if (ent.Setcontents and special.id) then ent:Setcontents(special.id) end
  358. if (ent.Setcount and special.count) then ent:Setcount(special.count) end
  359.  
  360. if (special.weaponclass) then ent.weaponclass = special.weaponclass end
  361. if (special.foodenergy) then ent.FoodEnergy = special.foodenergy end
  362. if (special.armorcharge) then ent.ArmorCharge = special.armorcharge end
  363.  
  364. ent:SetPos(trace.HitPos)
  365. ent:Spawn()
  366.  
  367. client:SavePlayerInventory()
  368. client:OpenInventory()
  369. end
  370. end)
  371.  
  372. hook.Add("PlayerSay", "InvCommand", function(ply, say, team)
  373. if (say:lower() == "!inv" or say:lower() == "/inv") then
  374. ply:OpenInventory()
  375. return ""
  376. end
  377. end)
  378. else
  379. pInventory = pInventory or NULL
  380. net.Receive("RPI_OpenInventory", function()
  381. local inventory = net.ReadTable()
  382. local maxspace = net.ReadInt(32)
  383.  
  384. local items = {}
  385. local count = 0
  386. for _, v in pairs (inventory) do
  387. if (type(v) == "table") then
  388. if (v.weaponclass) then
  389. if (items[v.weaponclass]) then
  390. items[v.weaponclass].count = items[v.weaponclass].count + 1
  391. else
  392. items[v.weaponclass] = {name = v.name, model = v.model, count = 1, weaponclass = v.weaponclass, class = v.weaponclass}
  393. end
  394. elseif (v.foodenergy) then
  395. if (items[v.model]) then
  396. items[v.model].count = items[v.model].count + 1
  397. else
  398. items[v.model] = {name = "Food", model = v.model, count = 1, class = "spawned_food", foodenergy = v.foodenergy}
  399. end
  400. elseif (v.armorcharge) then
  401. if (items[v.model]) then
  402. items[v.model].count = items[v.model].count + 1
  403. else
  404. items[v.model] = {name = "Armor", model = v.model, count = 1, class = "armor", armorcharge = v.armorcharge}
  405. end
  406. else
  407. table.insert(items, v)
  408. end
  409. else
  410. items[v] = (items[v] or 0) + 1
  411. end
  412. count = count + 1
  413. end
  414.  
  415. local x, y
  416. if (pInventory and pInventory:IsValid()) then
  417. x, y = pInventory:GetPos()
  418.  
  419. pInventory:Remove()
  420. pInventory = nil
  421. end
  422.  
  423. local frame = vgui.Create("DFrame")
  424. frame:SetTitle("Inventory")
  425. frame:SetSize(500, 500)
  426. if (x and y) then
  427. frame:SetPos(x, y)
  428. else
  429. frame:Center()
  430. end
  431. frame:MakePopup()
  432. pInventory = frame
  433.  
  434. local ilist = vgui.Create("DPanelList", frame)
  435. ilist:EnableHorizontal(true)
  436. ilist:EnableVerticalScrollbar(true)
  437. ilist:StretchToParent(5, 27, 5, 5)
  438. ilist.Paint = function(me)
  439. local w, h = me:GetSize()
  440. draw.RoundedBox(4, 0, 0, w, h, Color(75, 75, 75, 255))
  441.  
  442. draw.WordBox(4, 5, h - 26, "Space left: " .. maxspace - count, "DermaDefault", Color(0, 0, 0, 160), color_white)
  443. end
  444.  
  445. for k, v in pairs (items) do
  446. if (type(v) == "table") then
  447. local item = v
  448.  
  449. local icon = vgui.Create("SpawnIcon")
  450. icon:SetSize(64, 64)
  451. if (v.model) then
  452. icon:SetModel(Model(v.model))
  453. else
  454. icon:SetModel(Model("models/items/item_item_crate.mdl"))
  455. end
  456. icon:SetToolTip(item.name)
  457. icon.PaintOver = function(me)
  458. local w, h = me:GetSize()
  459.  
  460. draw.SimpleTextOutlined(item.name or "", "DermaDefault", w * 0.5, 5, color_white, TEXT_ALIGN_CENTER, TEXT_ALIGN_LEFT, 1, color_black)
  461.  
  462. if (item.class and item.class == "spawned_shipment") then
  463. draw.SimpleTextOutlined("x" .. item.count, "DermaDefault", w - 5, h - 5, color_white, TEXT_ALIGN_RIGHT, TEXT_ALIGN_TOP, 1, color_black)
  464. draw.SimpleTextOutlined("Shipment", "DermaDefault", w * 0.5, 18, color_white, TEXT_ALIGN_CENTER, TEXT_ALIGN_LEFT, 1, color_black)
  465. elseif (item.count) then
  466. draw.SimpleTextOutlined("x" .. item.count, "DermaDefault", w - 5, h - 5, color_white, TEXT_ALIGN_RIGHT, TEXT_ALIGN_TOP, 1, color_black)
  467. end
  468. end
  469. icon.DoClick = function()
  470. local menu = DermaMenu()
  471.  
  472. menu:AddOption("Drop", function()
  473. net.Start("RPI_DropSpecial")
  474. net.WriteTable(item)
  475. net.SendToServer()
  476.  
  477. surface.PlaySound(Sound("buttons/button5.wav"))
  478. end)
  479.  
  480. menu:Open()
  481. end
  482.  
  483. ilist:AddItem(icon)
  484. else
  485. local itm = k
  486. local count = v
  487.  
  488. local item = RPI.Items[itm]
  489.  
  490. local icon = vgui.Create("SpawnIcon")
  491. icon:SetSize(64, 64)
  492. icon:SetModel(item.model)
  493. icon:SetToolTip(item.name .. [[
  494.  
  495. ]] .. (item.description or "") .. [[
  496.  
  497.  
  498. Category: ]] .. item.category)
  499. icon.PaintOver = function(me)
  500. local w, h = me:GetSize()
  501.  
  502. draw.SimpleTextOutlined(item.name, "DermaDefault", w * 0.5, 5, color_white, TEXT_ALIGN_CENTER, TEXT_ALIGN_LEFT, 1, color_black)
  503. draw.SimpleTextOutlined("x" .. count, "DermaDefault", w - 5, h - 5, color_white, TEXT_ALIGN_RIGHT, TEXT_ALIGN_TOP, 1, color_black)
  504. end
  505. icon.DoClick = function()
  506. local menu = DermaMenu()
  507.  
  508. menu:AddOption("Drop", function()
  509. RunConsoleCommand("rp_dropitem", itm)
  510. surface.PlaySound(Sound("buttons/button5.wav"))
  511. end)
  512.  
  513. menu:Open()
  514. end
  515.  
  516. ilist:AddItem(icon)
  517. end
  518. end
  519. end)
  520. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement