This repository has been archived by the owner on Nov 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
sv_uteknark.lua
426 lines (393 loc) · 13.5 KB
/
sv_uteknark.lua
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
local ESX = nil
local oneSyncEnabled = GetConvar('onesync_enabled', false)
local VERBOSE = false
local lastPlant = {}
local tickTimes = {}
local tickPlantCount = 0
local VERSION = '1.1.4'
AddEventHandler('playerDropped',function(why)
lastPlant[source] = nil
end)
function log (...)
local numElements = select('#',...)
local elements = {...}
local line = ''
local prefix = '['..os.date("%H:%M:%S")..'] '
suffix = '\n'
local resourceName = '<'..GetCurrentResourceName()..'>'
for i=1,numElements do
local entry = elements[i]
line = line..' '..tostring(entry)
end
Citizen.Trace(prefix..resourceName..line..suffix)
end
function verbose(...)
if VERBOSE then
log(...)
end
end
if not oneSyncEnabled then
log('OneSync not available: Will have to trust client for locations!')
end
function HasItem(who, what, count)
count = count or 1
if ESX == nil then
log("HasItem: No ESX Object!")
return false
end
local xPlayer = ESX.GetPlayerFromId(who)
if xPlayer == nil then
log("HasItem: Failed to resolve xPlayer from", who)
return false
end
local itemspec = xPlayer.getInventoryItem(what)
if itemspec then
if itemspec.count >= count then
return true
else
return false
end
else
log("HasItem: Failed to get item data for item", what)
return false
end
end
function TakeItem(who, what, count)
count = count or 1
if ESX == nil then
log("TakeItem: No ESX Object!")
return false
end
local xPlayer = ESX.GetPlayerFromId(who)
if xPlayer == nil then
log("TakeItem: Failed to resolve xPlayer from", who)
return false
end
local itemspec = xPlayer.getInventoryItem(what)
if itemspec then
if itemspec.count >= count then
xPlayer.removeInventoryItem(what, count)
return true
else
return false
end
else
log("TakeItem: Failed to get item data for item", what)
return false
end
end
function GiveItem(who, what, count)
count = count or 1
if ESX == nil then
log("GiveItem: No ESX Object!")
return false
end
local xPlayer = ESX.GetPlayerFromId(who)
if xPlayer == nil then
log("GiveItem: Failed to resolve xPlayer from", who)
return false
end
local itemspec = xPlayer.getInventoryItem(what)
if itemspec then
if not itemspec.limit or itemspec.limit == -1 or itemspec.count + count <= itemspec.limit then
xPlayer.addInventoryItem(what, count)
return true
else
return false
end
else
log("GiveItem: Failed to get item data for item", what)
return false
end
end
function makeToast(target, subject, message)
TriggerClientEvent('esx_uteknark:make_toast', target, subject, message)
end
function inChat(target, message)
if target == 0 then
log(message)
else
TriggerClientEvent('chat:addMessage',target,{args={'UteKnark', message}})
end
end
function plantSeed(location, soil)
local hits = cropstate.octree:searchSphere(location, Config.Distance.Space)
if #hits > 0 then
return false
end
verbose('Planting at',location,'in soil', soil)
cropstate:plant(location, soil)
return true
end
function doScenario(who, what, where)
verbose('Telling', who,'to',what,'at',where)
TriggerClientEvent('esx_uteknark:do', who, what, where)
end
RegisterNetEvent('esx_uteknark:success_plant')
AddEventHandler ('esx_uteknark:success_plant', function(location, soil)
local src = source
if oneSyncEnabled and false then -- "and false" because something is weird in my OneSync stuff
local ped = GetPlayerPed(src)
--log('ped:',ped)
local pedLocation = GetEntityCoords(ped)
--log('pedLocation:',pedLocation)
--log('location:', location)
local distance = #(pedLocation - location)
if distance > Config.Distance.Interact then
if distance > 10 then
log(GetPlayerName(src),'attempted planting at',distance..'m - Cheating?')
end
makeToast(src, _U('planting_text'), _U('planting_too_far'))
return
end
end
if soil and Config.Soil[soil] then
local hits = cropstate.octree:searchSphere(location, Config.Distance.Space)
if #hits == 0 then
if TakeItem(src, Config.Items.Seed) then
if plantSeed(location, soil) then
makeToast(src, _U('planting_text'), _U('planting_ok'))
doScenario(src, 'Plant', location)
else
GiveItem(src, Config.Items.Seed)
makeToast(src, _U('planting_text'), _U('planting_failed'))
end
else
makeToast(src, _U('planting_text'), _U('planting_no_seed'))
end
else
makeToast(src, _U('planting_text'), _U('planting_too_close'))
end
else
makeToast(src, _U('planting_text'), _U('planting_not_suitable_soil'))
end
end)
RegisterNetEvent('esx_uteknark:log')
AddEventHandler ('esx_uteknark:log',function(...)
local src = source
log(src,GetPlayerName(src),...)
end)
RegisterNetEvent('esx_uteknark:test_forest')
AddEventHandler ('esx_uteknark:test_forest',function(forest)
local src = source
if IsPlayerAceAllowed(src, 'command.uteknark') then
local soil
for candidate, quality in pairs(Config.Soil) do
soil = candidate
if quality >= 1.0 then
break
end
end
log(GetPlayerName(src),'('..src..') is magically planting a forest of',#forest,'plants')
for i, tree in ipairs(forest) do
cropstate:plant(tree.location, soil, tree.stage)
if i % 25 == 0 then
Citizen.Wait(0)
end
end
else
log('OY!', GetPlayerName(src),'with ID',src,'tried to spawn a test forest, BUT IS NOT ALLOWED!')
end
end)
function keyCount(tbl)
local count = 0
if type(tbl) == 'table' then
for key, value in pairs(tbl) do
count = count + 1
end
end
return count
end
Citizen.CreateThread(function()
local ESXTries = 60
local itemsLoaded = false
while not itemsLoaded and ESXTries > 0 do
TriggerEvent('esx:getSharedObject', function(obj)
ESX = obj
if keyCount(ESX.Items) > 0 then
itemsLoaded = true
for forWhat,itemName in pairs(Config.Items) do
if ESX.Items[itemName] then
log(forWhat,'item in configuration ('..itemName..') found in ESX: Good!')
else
log('WARNING:',forWhat,'item in cofiguration ('..itemName..') does not exist!')
end
end
ESX.RegisterUsableItem(Config.Items.Seed, function(source)
local now = os.time()
local last = lastPlant[source] or 0
if now > last + (Config.ActionTime/1000) then
if HasItem(source, Config.Items.Seed) then
TriggerClientEvent('esx_uteknark:attempt_plant', source)
lastPlant[source] = now
else
makeToast(source, _U('planting_text'), _U('planting_no_seed'))
end
else
makeToast(source, _U('planting_text'), _U('planting_too_fast'))
end
end)
end
end)
Citizen.Wait(1000)
ESXTries = ESXTries - 1
end
if not ESX then
log("CRITICAL ERROR: Could not obtain ESX object!\n")
end
end)
Citizen.CreateThread(function()
local databaseReady = false
while not databaseReady do
Citizen.Wait(500)
local state = GetResourceState('mysql-async')
if state == "started" then
Citizen.Wait(500)
cropstate:load(function(plantCount)
if plantCount == 1 then
log('Uteknark loaded a single plant!')
else
log('Uteknark loaded',plantCount,'plants')
end
end)
databaseReady = true
end
end
while true do
Citizen.Wait(0)
local now = os.time()
local begin = GetGameTimer()
local plantsHandled = 0
for id, plant in pairs(cropstate.index) do
if type(id) == 'number' then -- Because of the whole "hashtable = true" thing
local stageData = Growth[plant.data.stage]
local growthTime = (stageData.time * 60 * Config.TimeMultiplier)
local soilQuality = Config.Soil[plant.data.soil] or 1.0
if stageData.interact then
local relevantTime = plant.data.time + ((growthTime / soilQuality) * Config.TimeMultiplier)
if now >= relevantTime then
verbose('Plant',id,'has died: No interaction in time')
cropstate:remove(id)
end
else
local relevantTime = plant.data.time + ((growthTime * soilQuality) * Config.TimeMultiplier)
if now >= relevantTime then
if plant.data.stage < #Growth then
verbose('Plant',id,'has grown to stage',plant.data.stage + 1)
cropstate:update(id, plant.data.stage + 1)
else
verbose('Plant',id,'has died: Ran out of stages')
cropstate:remove(id)
end
end
end
plantsHandled = plantsHandled + 1
if plantsHandled % 10 == 0 then
Citizen.Wait(0)
end
end
end
tickPlantCount = plantsHandled
local tickTime = GetGameTimer() - begin
table.insert(tickTimes, tickTime)
while #tickTimes > 20 do
table.remove(tickTimes, 1)
end
end
end)
local commands = {
debug = function(source, args)
if source == 0 then
log('Client debugging on the console? Nope.')
else
TriggerClientEvent('esx_uteknark:toggle_debug', source)
end
end,
stage = function(source, args)
if args[1] and string.match(args[1], "^%d+$") then
local plant = tonumber(args[1])
if cropstate.index[plant] then
if args[2] and string.match(args[2], "^%d+$") then
local stage = tonumber(args[2])
if stage > 0 and stage <= #Growth then
log(source,GetPlayerName(source),'set plant',plant,'to stage',stage)
cropstate:update(plant, stage)
else
inChat(source, string.format("%i is an invalid stage", stage))
end
else
inChat(source, "What stage?")
end
else
inChat(source,string.format("Plant %i does not exist!", plant))
end
else
inChat(source, "What plant, you say?")
end
end,
forest = function(source, args)
if source == 0 then
log('Forests can\'t grow in a console, buddy!')
else
local count = #Growth * #Growth
if args[1] and string.match(args[1], "%d+$") then
count = tonumber(args[1])
end
local randomStage = false
if args[2] then randomStage = true end
TriggerClientEvent('esx_uteknark:test_forest', source, count, randomStage)
end
end,
stats = function(source, args)
if cropstate.loaded then
local totalTime = 0
for i,time in ipairs(tickTimes) do
totalTime = totalTime + time
end
local tickTimeAverage = totalTime / #tickTimes
inChat(source, string.format("Tick time average: %.1fms", tickTimeAverage))
inChat(source, string.format("Plant count: %i", tickPlantCount))
else
inChat(source,'Not loaded yet')
end
end,
groundmat = function(source, args)
if source == 0 then
log('Console. The ground material is CONSOLE.')
else
TriggerClientEvent('esx_uteknark:groundmat', source)
end
end,
pyro = function(source, args)
if source == 0 then
log('You can\'t really test particle effects on the console.')
else
TriggerClientEvent('esx_uteknark:pyromaniac', source)
end
end,
}
RegisterCommand('uteknark', function(source, args, raw)
if #args > 0 then
local directive = string.lower(args[1])
if commands[directive] then
if #args > 1 then
local newArgs = {}
for i,entry in ipairs(args) do
if i > 1 then
table.insert(newArgs, entry)
end
end
args = newArgs
else
args = {}
end
commands[directive](source,args)
elseif source == 0 then
log('Invalid directive: ' .. directive)
else
inChat(source,_U('command_invalid', directive))
end
else
inChat(source, _U('command_empty', VERSION))
end
end,true)