summaryrefslogtreecommitdiff
path: root/gamemode/itemsystem/common_dropable.lua
blob: 34ca905afeb131cae6c6414e98fc976baa4a1eac (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
76
77
78
--Adds a function to to allow the player to drop this item, in quantities of 1, half, all, or X, which brings up a menu asking the player how many they would like to drop
--[[
  genericMakeDroppable(ITEM)
]]

function genericMakeDroppable(tbl)
  if(SERVER) then return end
  local drop1 = function(player)
    genericDropResource(player,tbl.Name,1)
  end
  local dropall = function(player)
    PrintTable(Resources)
    print("Ammount:" .. Resources[tbl.Name])
    genericDropResource(player,tbl.Name,Resources[tbl.Name])
  end
  local drophalf = function(player)
    genericDropResource(player,tbl.Name,math.ceil(Resources[tbl.Name]/2.0))
  end
  local dropx = function(player)
    if(SERVER) then return end
    local frame = vgui.Create( "DFrame" )
    frame:SetSize( 400, 60 )
    frame:Center()
    frame:MakePopup()

    local TextEntry = vgui.Create( "DTextEntry", frame )	-- create the form as a child of frame
    TextEntry:SetPos( 20, 30 )
    TextEntry:SetSize( 360, 20 )
    TextEntry:SetText( "Number to drop:" )
    TextEntry.OnEnter = function( self )
      if(tonumber(self:GetValue(),10) == nil) then return end
      genericDropResource(player,tbl.Name,self:GetValue())
      frame:Close()
    end

  end
  if(tbl.Actions == nil) then
    tbl.Actions = {}
  end
  if(tbl.Actions["Drop"] == nil) then
    tbl.Actions["Drop"] = {}
  end
  tbl.Actions["Drop"]["Drop 1"] = drop1
  tbl.Actions["Drop"]["Drop Half"] = drophalf
  tbl.Actions["Drop"]["Drop all"] = dropall
  tbl.Actions["Drop"]["Drop X"] = dropx
end


if(SERVER) then
  util.AddNetworkString( "gms_dropresources" )
end
function genericDropResource(player, resource, ammount)
  if(CLIENT) then
    net.Start("gms_dropresources")
    net.WriteString(resource)
    net.WriteInt(ammount,GMS.NETINT_BITCOUNT)
    net.SendToServer()
  end
  if(SERVER) then
    if(player.Resources[resource] < ammount) then
      player:SendMessage("You don't have that many to drop!", 3, Color(255, 255, 255, 255))
      return
    end
    local res = player.Resources[resource]

    if ( ammount > res ) then
      ammount = res
    end
    player:DropResource( resource, ammount )
    player:DecResource( resource, ammount )
  end
end
net.Receive( "gms_dropresources", function(len,pl)
  local resourcename = net.ReadString()
  local resourcenum = net.ReadInt(GMS.NETINT_BITCOUNT)
  genericDropResource(pl,resourcename,resourcenum)
end)