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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
------------------------------------------------------------------
------------ Inventory script by Joni And Friends ----------------
------------------------------------------------------------------
------------------------------------------------------------------
function initArray(m)
local array = {}
for i = 1, m do
array[i]=0
end
return array
end
c="\169"
inv_wpn=initArray(32)
inv_wpn_many=initArray(32)
inv_item=initArray(32)
inv_item_many=initArray(32)
max_inv=20 ----- maximum item on inventory
function inv_reset(id)
inv_wpn[id]={}
inv_wpn_many[id]={}
inv_item[id]={}
inv_item_many[id]={}
for g=1,max_inv do
inv_wpn[id][g]=0
inv_wpn_many[id][g]=0
inv_item[id][g]=0
inv_item_many[id][g]=0
end
end
moreitem={
{name="food",
effect=function(id)
parse("sethealth "..id.." "..player(id,"health")+10)
end};
}
function wpn_refresh(id)
z={}
b={}
for g=1,max_inv do
if inv_wpn_many[id][g]>0 then
z[g]=inv_wpn[id][g]
b[g]=inv_wpn_many[id][g]
inv_wpn[id][g]=0
inv_wpn_many[id][g]=0
set_wpn_inv(id,z[g],b[g])
end
end
end
function item_refresh(id)
z={}
b={}
for g=1,max_inv do
if inv_item_many[id][g]>0 then
z[g]=inv_item[id][g]
b[g]=inv_item_many[id][g]
inv_item[id][g]=0
inv_item_many[id][g]=0
set_item_inv(id,z[g],b[g])
end
end
end
function InvMenu(id,page)
for g=1,max_inv do
if inv_wpn_many[id][g]<1 then
inv_wpn[id][g]=0
end
if inv_item_many[id][g]<1 then
inv_item[id][g]=0
end
end
item_refresh(id)
wpn_refresh(id)
local page=page or 1
local pages=math.ceil(max_inv/7)
if page<1 then
page=pages
end
if page>pages then
page=1
end
local m='Inventory | Page '..page
for i=7*page-6, 7*page do
if inv_wpn[id][i] and inv_wpn[id][i]>0 then
m=m..','..itemtype(inv_wpn[id][i],'name')..'|'..inv_wpn_many[id][i]..' item(s)'
elseif inv_item[id][i] and inv_item[id][i]>0 then
m=m..','..moreitem[inv_item[id][i]].name..'|'..inv_item_many[id][i]..' item(s)'
else
m=m..','
end
end
if page == pages then
m = m ..',First page|Page 1'
else
m = m ..',Next|Page '..(page+1)..''
end
if page == 1 then
m = m ..',Last page|Page '..pages..''
else
m = m ..',Previous|Page '..(page-1)..''
end
menu(id, m)
end
function set_wpn_inv(id,a,b)
for g=1,max_inv do
if inv_wpn[id][g]<1 and inv_item[id][g]<1 then
inv_wpn[id][g]=a
inv_wpn_many[id][g]=b
return 1
end
if inv_wpn[id][g]==a then
inv_wpn_many[id][g]=inv_wpn_many[id][g]+b
return 1
end
end
end
function set_item_inv(id,a,b)
for g=1,max_inv do
if inv_item[id][g]<1 and inv_wpn[id][g]<1 then
inv_item[id][g]=a
inv_item_many[id][g]=b
return 1
end
if inv_item[id][g]==a then
inv_item_many[id][g]=inv_item_many[id][g]+b
return 1
end
end
end
addhook("join","_join")
function _join(id)
inv_reset(id)
user.load(id)
end
addhook("leave", "_leave")
function _leave(id)
user.leave(id)
end
addhook("menu", "_menu")
function _menu(id, title, button)
if title:sub(1,17)=="Inventory | Page " then
local page=tonumber(title:sub(18))
if button==8 then
InvMenu(id,page+1,code)
end
if button==9 then
InvMenu(id,page-1,code)
end
if button==0 then
return 1
end
if button<=7 then
if inv_wpn[id][(page-1)*7+ button]>0 then
parse("equip "..id.." "..inv_wpn[id][(page-1)*7+ button])
inv_wpn_many[id][(page-1)*7+ button]=inv_wpn_many[id][(page-1)*7+ button]-1
msg2(id,"[Server] "..c.."000555000You has taking "..itemtype(inv_wpn[id][(page-1)*7+ button],'name'))
elseif inv_item[id][(page-1)*7+ button]>0 then
moreitem[inv_item[id][(page-1)*7+ button]].effect(id)
inv_item_many[id][(page-1)*7+ button]=inv_item_many[id][(page-1)*7+ button]-1
msg2(id,"[Server] "..c.."000555000You has taking "..moreitem[inv_item[id][(page-1)*7+ button]].name)
end
end
end
end
addhook("serveraction","_serveraction")
function _serveraction(id,a)
if a==3 then
InvMenu(id,1)
end
end
inv_np = {45,82} --added laser & super armor
addhook("walkover","_walkover")
function _walkover(id, od, a,ain)
-- where the no inv item pickup array kicks in
for i,v in pairs(inv_np) do
if od == i then msg2(id,"cant pickup") return 1 end
end
if od>0 then
parse("removeitem "..a)
set_wpn_inv(id,od,1)
return 1
end
end
--
-- Load / Save Var & Func
--
if user == nil then user = {} end
user.save_dir = "sys/lua/data/"
user.ext = "inv"
user.delimiter = ","
-- will be saved here: sys/lua/data/116310.inv
-- Main
function user.save(id)
usgn = player(id, "usgn")
local file = io.open(user.save_dir..usgn..user.ext, "w")
-- slot1 inv_wep
-- slot2 inv_item
local slot1 = implode(inv_wep[id])
local slot2 = implode(inv_item[id])
file:write(slot1..user.delimiter..slot2)
file:close()
end
function user.load(id)
usgn = player(id, "usgn")
if user.exists(usgn) then
local file = io.open(user.save_dir..usgn..user.ext, "r")
local inv = file:read()
if is_empty(inv) ~= true then
local _inv = explode(user.delimiter, inv)
if is_empty(_inv[1]) ~= true then inv_wep[id] = _inv[1] end
if is_empty(_inv[2]) ~= true then inv_item[id] = _inv[2] end
end
else user.create(usgn) end
end
-- Self
function user.exists(usgn)
local file = io.open(user.save_dir..usgn..user.ext)
if file ~= nil then file:close() else return false end
return true
end
function user.create(usgn)
local file = io.open(user.save_dir..usgn..user.ext)
file:write(user.delimiter)
file:close()
end
function explode(div,str) if (div=='') then return false end local pos,arr = 0,{} for st,sp in function() return string.find(str,div,pos,true) end do table.insert(arr,string.sub(str,pos,st-1)) pos = sp + 1 end table.insert(arr,string.sub(str,pos)) return arr end
function implode(array) if array ~= nil then local final_text = "" for _,i in pairs(array) do if _ ~= #array then final_text = final_text .. tostring(i) .. "," else final_text = final_text .. tostring(i) end end return final_text else return "" end end
function is_empty(obj) if type(obj) == "string" then if obj == '' or obj == ' ' then return true else return false end elseif type(obj) == "table" then if #obj == 0 then return true else return false end end end