aboutsummaryrefslogtreecommitdiff
path: root/gamemode/itemsystem/item_common.lua
blob: acfc3e547beec99a8e41768bb6cf2d25c3be19a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
if CLIENT then
	ART.DropItem = function(item)
		print("Trying to drop item, Item was ")
		PrintTable(item)
		return function()
			local data = item:Serialize()
			net.Start("dropitem")
			net.WriteString(item.Name)
			net.WriteUInt(#data,32)
			net.WriteData(data,#data)
			net.SendToServer()
		end
	end

else
	util.AddNetworkString("dropitem")

	function gencompareto(item)
		--Two items are equal if their Serialize()s are the same.
		local tserialize = item:Serialize()
		return function(otheritem)
			local oserialize = otheritem:Serialize()
			return tserialize == oserialize
		end
	end

	function ART.CreateDroppedItem(itemtbl, where)
	    local e = ents.Create("art_droppeditem")
	    e.Model = "models/props_junk/Rock001a.mdl"
	    e.Item = itemtbl
	    e:SetPos(where)
	    e:Spawn()
	end

	net.Receive("dropitem",function(len,ply)
		local itemtype = net.ReadString()
		local itemdatalen = net.ReadUInt(32)
		local itemdata = net.ReadData(itemdatalen)
		local itemtbl = ART.GetItemByName(itemtype)
		local item = itemtbl:DeSerialize(itemdata)

		print("I want to drop:")
		PrintTable(item)
		print("Do I have one?")
		local row,col,n = ply:HasItem(gencompareto(item))
		print("row",row,"col",col,"n",n)
		--To find out where to drop the item, go out from player's view, then down.
		local pes = ply:GetPos() + Vector(0,0,64)
		local pee = ply:GetForward() * 50 + Vector(0,0,64) + ply:GetPos()
		local tr1d = {
			["start"] = pes,
			["endpos"] = pee,
			["filter"] = ply,
		}
		local tr1r = util.TraceLine(tr1d)
		local fes
		if tr1r.hit then --Use where it hit and go down
			fes = tr1r.HitPos
		else --Use the spot 50 units in front of us
			fes = pee
		end
		local tr2d = {
			["start"] = fes,
			["endpos"] = fes + Vector(0,0,-64),
		}
		local tr2r = util.TraceLine(tr2d)
		local itempos = tr2r.HitPos
		print("Dropping item at",itempos)
		if row and col and n then
			print("DropItem is",ART.DropItem)
			ART.CreateDroppedItem(item,itempos)
			ply:RemoveItemAt(n,row,col)
		end
	end)
end