· 6 years ago · Jan 25, 2020, 12:04 PM
1
2[survival.lua]
3-----------------------------------------------
4function vRP.getpo(user_id)
5 local data = vRP.getUserDataTable(user_id)
6 if data then
7 return data.po
8 end
9
10 return 0
11end
12
13function vRP.getpi(user_id)
14 local data = vRP.getUserDataTable(user_id)
15 if data then
16 return data.pi
17 end
18
19 return 0
20end
21------------------------------------------------
22function vRP.setpo(user_id,value)
23 local data = vRP.getUserDataTable(user_id)
24 if data then
25 data.po = value
26 if data.po < 0 then data.po = 0
27 elseif data.po > 100 then data.po = 100
28 end
29
30 -- update bar
31 local source = vRP.getUserSource(user_id)
32 TriggerClientEvent("vrp_ladderhud:updateBasics",source, data.hunger, data.thirst, data.po, data.pi)
33 --vRPclient._setProgressBarValue(source, "vRP:po",data.po)
34 if data.po >= 100 then
35 --vRPclient._setProgressBarText(source,"vRP:po",lang.survival.starving())
36 else
37 --vRPclient._setProgressBarText(source,"vRP:po","")
38 end
39 end
40end
41
42function vRP.setpi(user_id,value)
43 local data = vRP.getUserDataTable(user_id)
44 if data then
45 data.pi = value
46 if data.pi < 0 then data.pi = 0
47 elseif data.pi > 100 then data.pi = 100
48 end
49
50 -- update bar
51 local source = vRP.getUserSource(user_id)
52 TriggerClientEvent("vrp_ladderhud:updateBasics",source, data.hunger, data.thirst, data.po, data.pi)
53 --vRPclient._setProgressBarValue(source, "vRP:pi",data.pi)
54 if data.pi >= 100 then
55 --vRPclient._setProgressBarText(source,"vRP:pi",lang.survival.piy())
56 else
57 --vRPclient._setProgressBarText(source,"vRP:pi","")
58 end
59 end
60end
61
62
63-------------------------------------------------
64function vRP.varypo(user_id, variation)
65 local data = vRP.getUserDataTable(user_id)
66 if data then
67 local was_starving = data.po >= 100
68 data.po = data.po + variation
69 local is_starving = data.po >= 100
70
71 -- apply overflow as damage
72 local overflow = data.po-100
73 if overflow > 0 then
74 vRPclient._varyHealth(vRP.getUserSource(user_id),-overflow*cfg.overflow_damage_factor)
75 end
76
77 if data.po < 0 then data.po = 0
78 elseif data.po > 100 then data.po = 100
79 end
80
81 -- set progress bar data
82 local source = vRP.getUserSource(user_id)
83 TriggerClientEvent("vrp_ladderhud:updateBasics",source, data.po, data.pi, data.po, data.pi)
84 --vRPclient._setProgressBarValue(source,"vRP:po",data.po)
85 if was_starving and not is_starving then
86 --vRPclient._setProgressBarText(source,"vRP:po","")
87 elseif not was_starving and is_starving then
88 --vRPclient._setProgressBarText(source,"vRP:po",lang.survival.starving())
89 end
90 end
91end
92
93function vRP.varypi(user_id, variation)
94 local data = vRP.getUserDataTable(user_id)
95 if data then
96 local was_piy = data.pi >= 100
97 data.pi = data.pi + variation
98 local is_piy = data.pi >= 100
99
100 -- apply overflow as damage
101 local overflow = data.pi-100
102 if overflow > 0 then
103 vRPclient._varyHealth(vRP.getUserSource(user_id),-overflow*cfg.overflow_damage_factor)
104 end
105
106 if data.pi < 0 then data.pi = 0
107 elseif data.pi > 100 then data.pi = 100
108 end
109
110 -- set progress bar data
111 local source = vRP.getUserSource(user_id)
112 TriggerClientEvent("vrp_ladderhud:updateBasics",source, data.po, data.pi, data.po, data.pi)
113 --vRPclient._setProgressBarValue(source,"vRP:pi",data.pi)
114 if was_piy and not is_piy then
115 --vRPclient._setProgressBarText(source,"vRP:pi","")
116 elseif not was_piy and is_piy then
117 --vRPclient._setProgressBarText(source,"vRP:pi",lang.survival.piy())
118 end
119 end
120end
121
122--------------------------------------------------------------------------------------------------------------------------------------
123-----------------------------------------------------------------------------------------
124-----------------------------------------------------------------------------------------
125-----------------------------------------------------------------------------------------
126[food.lua]
127local function gen(ftype, vary_hunger, vary_thirst, vary_pi, vary_po)
128 local fgen = function(args)
129 local idname = args[1]
130 local choices = {}
131 local act = "Unknown"
132 if ftype == "eat" then act = "Eat" elseif ftype == "drink" then act = "Drink" elseif ftype == "pi" then act = "Pi" elseif ftype == "po" then act = "Po" end
133 local name = vRP.getItemName(idname)
134
135 choices[act] = {function(player,choice)
136 local user_id = vRP.getUserId(player)
137 if user_id ~= nil then
138 if vRP.tryGetInventoryItem(user_id,idname,1,false) then
139 if vary_hunger ~= 0 then vRP.varyHunger(user_id,vary_hunger) end
140 if vary_thirst ~= 0 then vRP.varyThirst(user_id,vary_thirst) end
141 if vary_po ~= 0 then vRP.varypo(user_id,vary_po) end
142 if vary_pi ~= 0 then vRP.varypi(user_id,vary_pi) end
143
144 if ftype == "drink" then
145 vRPclient.notify(player,{"~b~ Drinking "..name.."."})
146 play_drink(player)
147 elseif ftype == "eat" then
148 vRPclient.notify(player,{"~o~ Eating "..name.."."})
149 play_eat(player)
150 elseif ftype == "po" then
151 vRPclient.notify(player,{"~o~ Poopping "..name.."."})
152 play_eat(player)
153 elseif ftype == "pi" then
154 vRPclient.notify(player,{"~o~ Peeing "..name.."."})
155 play_eat(player)
156 end
157
158 vRP.closeMenu(player)
159 end
160 end
161 end}
162
163 return choices
164 end
165
166 return fgen
167end
168
169
170-- DRINKS --
171
172items["acqua"] = {"Acqua","", gen("drink",0,-25),0.5}
173items["latte"] = {"Latte","", gen("drink",0,-5),0.5}
174items["caffe"] = {"Caffè","", gen("drink",0,-10),0.2}
175items["tea"] = {"Tea","", gen("drink",0,-15),0.2}
176items["icetea"] = {"Ice-Tea","", gen("drink",0,-20), 0.5}
177items["succo"] = {"Succo d'arancia","", gen("drink",0,-25),0.5}
178items["cocacola"] = {"Coca Cola","", gen("drink",0,-35),0.3}
179items["redbull"] = {"Red Bull","", gen("drink",0,-40),0.3}
180items["limonata"] = {"Limonata","", gen("drink",0,-45),0.3}
181items["vodka"] = {"Vodka","", gen("drink",15,-65),0.5}
182items["bicchiere"] = {"Bicchiere","", gen("pi",25,0,0,-25),0.5}
183
184--FOOD
185
186-- create Breed item
187items["pane"] = {"Pane","", gen("eat",-10,0),0.5}
188items["ciambella"] = {"Ciambella","", gen("eat",-15,0),0.2}
189items["tacos"] = {"Tacos","", gen("eat",-20,0),0.2}
190items["sandwich"] = {"Sandwich","A tasty snack.", gen("eat",-25,0),0.5}
191items["kebab"] = {"Kebab","", gen("eat",-45,0),0.85}
192items["cimabelladorata"] = {"Ciambella dorata","", gen("eat",-25,0),0.5}
193items["carta"] = {"Carta","", gen("po",10,0,-10,0),0.5}
194
195return items
196
197-----------------------------------------------------------------------------------------
198-----------------------------------------------------------------------------------------
199-----------------------------------------------------------------------------------------
200-----------------------------------------------------------------------------------------
201-----------------------------------------------------------------------------------------
202
203
204[Console Errors]
205
206SCRIPT ERROR: @vrp/modules/survival.lua:129: attempt to compare number with nil <----------------
207> cb (- define some basic inventory items
208
209local items = {}
210
211local function play_eat(player)
212 local seq = {
213 {"mp_player_inteat@burger", "mp_player_int_eat_burger_enter",1},
214 {"mp_player_inteat@burger", "mp_player_int_eat_burger",1},
215 {"mp_player_inteat@burger", "mp_player_int_eat_burger_fp",1},
216 {"mp_player_inteat@burger", "mp_player_int_eat_exit_burger",1}
217 }
218
219 vRPclient.playAnim(player,{true,seq,false})
220end
221
222local function play_drink(player)
223 local seq = {
224 {"mp_player_intdrink","intro_bottle",1},
225 {"mp_player_intdrink","loop_bottle",1},
226 {"mp_player_intdrink","outro_bottle",1}
227 }
228
229 vRPclient.playAnim(player,{true,seq,false})
230end
231
232-- gen food choices as genfunc
233-- idname
234-- ftype: eat or drink
235-- vary_hunger
236-- vary_thirst
237local function gen(ftype, vary_hunger, vary_thirst, vary_pi, vary_po)
238 local fgen = function(args)
239 local idname = args[1]
240 local choices = {}
241 local act = "Unknown"
242 if ftype == "eat" then act = "Eat" elseif ftype == "drink" then act = "Drink" elseif ftype == "pi" then act = "Pi" elseif ftype == "po" then act = "Po" end
243 local name = vRP.getItemName(idname)
244
245 choices[act] = {function(player,choice)
246 local user_id = vRP.getUserId(player)
247 if user_id ~= nil then
248 if vRP.tryGetInventoryItem(user_id,idname,1,false) then
249 if vary_hunger ~= 0 then vRP.varyHunger(user_id,vary_hunger) end
250 if vary_thirst ~= 0 then vRP.varyThirst(user_id,vary_thirst) end
251 if vary_po ~= 0 then vRP.varypo(user_id,vary_po) end
252 if vary_pi ~= 0 then vRP.varypi(user_id,vary_pi) end
253
254 if ftype == "drink" then
255 vRPclient.notify(player,{"~b~ Drinking "..name.."."})
256 play_drink(player)
257 elseif ftype == "eat" then
258 vRPclient.notify(player,{"~o~ Eating "..name.."."})
259 play_eat(player)
260 elseif ftype == "po" then
261 vRPclient.notify(player,{"~o~ Poopping "..name.."."})
262 play_eat(player)
263 elseif ftype == "pi" then
264 vRPclient.notify(player,{"~o~ Peeing "..name.."."})
265 play_eat(player)
266 end
267
268 vRP.closeMenu(player)
269 end
270 end
271 end}
272
273 return choices
274 end
275
276 return fgen
277end
278
279
280-- DRINKS --
281
282items["acqua"] = {"Acqua","", gen("drink",0,-25),0.5}
283items["latte"] = {"Latte","", gen("drink",0,-5),0.5}
284items["caffe"] = {"Caffè","", gen("drink",0,-10),0.2}
285items["tea"] = {"Tea","", gen("drink",0,-15),0.2}
286items["icetea"] = {"Ice-Tea","", gen("drink",0,-20), 0.5}
287items["succo"] = {"Succo d'arancia","", gen("drink",0,-25),0.5}
288items["cocacola"] = {"Coca Cola","", gen("drink",0,-35),0.3}
289items["redbull"] = {"Red Bull","", gen("drink",0,-40),0.3}
290items["limonata"] = {"Limonata","", gen("drink",0,-45),0.3}
291items["vodka"] = {"Vodka","", gen("drink",15,-65),0.5}
292items["bicchiere"] = {"Bicchiere","", gen("pi",25,0,0,-25),0.5}
293
294--FOOD
295
296-- create Breed item
297items["pane"] = {"Pane","", gen("eat",-10,0),0.5}
298items["ciambella"] = {"Ciambella","", gen("eat",-15,0),0.2}
299items["tacos"] = {"Tacos","", gen("eat",-20,0),0.2}
300items["sandwich"] = {"Sandwich","A tasty snack.", gen("eat",-25,0),0.5}
301items["kebab"] = {"Kebab","", gen("eat",-45,0),0.85}
302items["cimabelladorata"] = {"Ciambella dorata","", gen("eat",-25,0),0.5}
303items["carta"] = {"Carta","", gen("po",10,0,-10,0),0.5}
304
305return items
306:45)
307> f (@vrp/modules/gui.lua:182)
308> handler (
309local Tools = module("lib/Tools")
310local Debug = module("lib/Debug")
311
312-- this file describe a two way proxy between the server and the clients (request system)
313
314local Tunnel = {}
315
316-- define per dest regulator
317Tunnel.delays = {}
318
319-- set the base delay between Triggers for this destination in milliseconds (0 for instant trigger)
320function Tunnel.setDestDelay(dest, delay)
321 Tunnel.delays[dest] = {delay, 0}
322end
323
324local function tunnel_resolve(itable,key)
325 local mtable = getmetatable(itable)
326 local iname = mtable.name
327 local ids = mtable.tunnel_ids
328 local callbacks = mtable.tunnel_callbacks
329 local identifier = mtable.identifier
330
331 -- generate access function
332 local fcall = function(dest,args,callback)
333 if args == nil then
334 args = {}
335 end
336
337 -- get delay data
338 local delay_data = Tunnel.delays[dest]
339 if delay_data == nil then
340 delay_data = {0,0}
341 end
342
343 -- increase delay
344 local add_delay = delay_data[1]
345 delay_data[2] = delay_data[2]+add_delay
346
347 if delay_data[2] > 0 then -- delay trigger
348 SetTimeout(delay_data[2], function()
349 -- remove added delay
350 delay_data[2] = delay_data[2]-add_delay
351
352 -- send request
353 if type(callback) == "function" then -- ref callback if exists (become a request)
354 local rid = ids:gen()
355 callbacks[rid] = callback
356 TriggerClientEvent(iname..":tunnel_req",dest,key,args,identifier,rid)
357 else -- regular trigger
358 TriggerClientEvent(iname..":tunnel_req",dest,key,args,"",-1)
359 end
360 end)
361 else -- no delay
362 -- send request
363 if type(callback) == "function" then -- ref callback if exists (become a request)
364 local rid = ids:gen()
365 callbacks[rid] = callback
366 TriggerClientEvent(iname..":tunnel_req",dest,key,args,identifier,rid)
367 else -- regular trigger
368 TriggerClientEvent(iname..":tunnel_req",dest,key,args,"",-1)
369 end
370 end
371 end
372
373 itable[key] = fcall -- add generated call to table (optimization)
374 return fcall
375end
376
377-- bind an interface (listen to net requests)
378-- name: interface name
379-- interface: table containing functions
380function Tunnel.bindInterface(name,interface)
381 -- receive request
382 RegisterServerEvent(name..":tunnel_req")
383 AddEventHandler(name..":tunnel_req",function(member,args,identifier,rid)
384 local source = source
385 local delayed = false
386
387 if Debug.active then
388 Debug.pbegin("tunnelreq#"..rid.."_"..name..":"..member.." "..json.encode(Debug.safeTableCopy(args)))
389 end
390
391 local f = interface[member]
392
393 local rets = {}
394 if type(f) == "function" then
395 -- bind the global function to delay the return values using the returned function with args
396 TUNNEL_DELAYED = function()
397 delayed = true
398 return function(rets)
399 rets = rets or {}
400
401 if rid >= 0 then
402 TriggerClientEvent(name..":"..identifier..":tunnel_res",source,rid,rets)
403 end
404 end
405 end
406
407 rets = {f(table.unpack(args))} -- call function
408 -- CancelEvent() -- cancel event doesn't seem to cancel the event for the other handlers, but if it does, uncomment this
409 end
410
411 -- send response (even if the function doesn't exist)
412 if not delayed and rid >= 0 then
413 TriggerClientEvent(name..":"..identifier..":tunnel_res",source,rid,rets)
414 end
415
416 if Debug.active then
417 Debug.pend()
418 end
419 end)
420end
421
422-- get a tunnel interface to send requests
423-- name: interface name
424-- identifier: unique string to identify this tunnel interface access (the name of the current resource should be fine)
425function Tunnel.getInterface(name,identifier)
426 local ids = Tools.newIDGenerator()
427 local callbacks = {}
428
429 -- build interface
430 local r = setmetatable({},{ __index = tunnel_resolve, name = name, tunnel_ids = ids, tunnel_callbacks = callbacks, identifier = identifier })
431
432 -- receive response
433 RegisterServerEvent(name..":"..identifier..":tunnel_res")
434 AddEventHandler(name..":"..identifier..":tunnel_res",function(rid,args)
435 if Debug.active then
436 Debug.pbegin("tunnelres#"..rid.."_"..name.." "..json.encode(Debug.safeTableCopy(args)))
437 end
438
439 local callback = callbacks[rid]
440 if callback ~= nil then
441 -- free request id
442 ids:free(rid)
443 callbacks[rid] = nil
444
445 -- call
446 callback(table.unpack(args))
447 end
448
449 Debug.pend()
450 end)
451
452 return r
453end
454
455return Tunnel
456:100)