-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathped_server.lua
More file actions
456 lines (396 loc) · 12.9 KB
/
ped_server.lua
File metadata and controls
456 lines (396 loc) · 12.9 KB
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
local destroyingElement = false
local animations = {}
local markers = {}
local peds = {}
addEventHandler("onResourceStart", resourceRoot, function()
addEvent("stopPedAnimationForAll", true)
addEventHandler("stopPedAnimationForAll", root, stopPedAnimationForAll)
addEvent("syncAnimations", true)
addEventHandler("syncAnimations", root, function(clientAnimations)
if type(clientAnimations) == "table" then
animations = clientAnimations
outputDebugString("[PAS]: Animation table successfully synced.", 4, 100, 255, 100)
end
end)
addEvent("applyPedAnimation", true)
addEventHandler(
"applyPedAnimation",
root,
function(ped, category, animName, loop, updatePosition, interruptable, freezeLastFrame, blend)
applyPedAnimation(ped, category, animName, loop, updatePosition, interruptable, freezeLastFrame, blend)
end
)
addEvent("stopPedAnimation", true)
addEventHandler("stopPedAnimation", root, function(ped)
stopPedAnimation(ped)
end)
addEvent("onRequestDestroyPed", true)
addEventHandler("onRequestDestroyPed", root, function(ped)
if isElement(ped) and getElementType(ped) == "ped" then
destroyingElement = true
destroyElement(ped)
destroyingElement = false
updatePedList()
end
end)
-- Update dimensions on test start/stop
--[[
addEvent("testResource", true)
addEventHandler("testResource", root, function()
for _, ped in ipairs(getElementsByType("ped")) do
if isElement(ped) then
setElementDimension(ped, 0)
end
end
end)
addEvent("stopTest", true)
addEventHandler("stopTest", root, function()
for _, ped in ipairs(getElementsByType("ped")) do
if isElement(ped) then
setElementDimension(ped, 200)
end
end
end)]]
-- Update all markers
addEvent("requestMarkerList", true)
addEventHandler("requestMarkerList", root, updateMarkersList)
-- Update ped list for all clients when a ped is created/destroyed or its ID changes
addEvent("requestPedList", true)
addEventHandler("requestPedList", root, updatePedList)
addEventHandler("onElementCreate", root, function()
if getElementType(source) == "ped" then
updatePedList()
elseif getElementType(source) == "marker" then
updateMarkersList()
end
end)
addEventHandler("onElementDestroy", root, function()
if getElementType(source) == "ped" then
if isElement(source) and not destroyingElement then
destroyingElement = true
destroyElement(source)
updatePedList()
destroyingElement = false
end
elseif getElementType(source) == "marker" then
if isElement(source) and not destroyingElement then
destroyingElement = true
destroyElement(source)
updateMarkersList()
destroyingElement = false
end
end
end)
addEventHandler("onElementDataChange", root, function(dataName)
if dataName == "id" and getElementType(source) == "ped" then
updatePedList()
elseif dataName == "id" and getElementType(source) == "marker" then
updateMarkersList()
end
end)
end)
-- General ped animation functions
function stopPedAnimationForAll()
local allPeds = getElementsByType("ped")
for _, ped in ipairs(allPeds) do
if isElement(ped) then
setPedAnimation(ped, nil)
end
end
end
function stopPedAnimation(ped)
if isElement(ped) then
setPedAnimation(ped, nil)
end
end
function applyPedAnimation(ped, category, animName, loop, updatePosition, interruptable, freezeLastFrame, blend)
if animations[category] and table.find(animations[category], animName) then
blend = blend or 250
setElementFrozen(ped, true)
setPedAnimation(
ped,
category,
animName,
-1,
loop,
updatePosition,
interruptable,
freezeLastFrame,
blend or 250,
true
)
end
end
-- File management functions
function addFileListEntry(path, arguments)
local fileListPath = "anim_sequences/file_list.json"
local file = fileOpen(fileListPath, true)
if not file then
return false
end
local content = fileRead(file, fileGetSize(file))
fileClose(file)
local fileList = {}
if content and content ~= "" then
local success, data = pcall(fromJSON, content)
if success and type(data) == "table" then
fileList = data
end
end
table.insert(fileList, {
path = path,
})
local file = fileCreate(fileListPath)
if file then
fileWrite(file, toJSON(fileList, true))
fileClose(file)
return true
end
return false
end
-- Request Markers
function updateMarkersList()
markers = {}
local allMarkers = getElementsByType("marker")
for i, marker in ipairs(allMarkers) do
if isElement(marker) then
local x, y, z = getElementPosition(marker)
local id = getElementID(marker) or "unnamed"
if id and id ~= "unnamed" and id ~= "" then
local displayText = string.format("%s (%.2f, %.2f, %.2f)", id, x, y, z)
table.insert(markers, {
id = id,
position = { x = x, y = y, z = z },
displayText = displayText,
})
end
end
end
triggerClientEvent("updateMarkersList", root, markers)
end
-- Ped list management
function updatePedList()
peds = {}
local allPeds = getElementsByType("ped")
for i, ped in ipairs(allPeds) do
if isElement(ped) then
local x, y, z = getElementPosition(ped)
local id = getElementID(ped) or "unnamed"
if id and id ~= "unnamed" and id ~= "" then
local displayText = string.format("%s (%.2f, %.2f, %.2f)", id, x, y, z)
table.insert(peds, {
id = id,
position = { x = x, y = y, z = z },
displayText = displayText,
})
end
end
end
triggerClientEvent("updatePedList", root, peds)
end
addEvent("onRequestBatchApplyAnimations", true)
addEventHandler("onRequestBatchApplyAnimations", root, function(animationTable)
for _, anim in ipairs(animationTable) do
local ped = nil
for _, p in ipairs(getElementsByType("ped")) do
if isElement(p) and getElementID(p) == anim.name then
ped = p
break
end
end
if not ped then
ped = createPed(anim.id, anim.x, anim.y, anim.z, anim.rz or 0.0)
setElementDimension(ped, anim.elementDimension or 200)
setElementID(ped, anim.name)
updatePedList()
end
if isElement(ped) then
setTimer(function()
if anim.type == "sequence" and anim.sequenceName then
local filePath = "anim_sequences/" .. anim.sequenceName .. ".json"
local file = fileOpen(filePath)
if file then
local content = fileRead(file, fileGetSize(file))
fileClose(file)
local sequenceData = fromJSON(content)
if sequenceData then
triggerClientEvent("onApplySequenceToPed", root, sequenceData, ped)
else
outputDebugString("[PAS] ERROR: Failed to parse JSON from " .. filePath, 4, 255, 165, 0)
end
else
outputDebugString("[PAS] ERROR: Could not open file " .. filePath, 4, 255, 165, 0)
end
else
-- Apply normal animation
applyPedAnimation(
ped,
anim.category,
anim.animation,
anim.loop,
anim.updatePosition,
anim.interruptable,
anim.freezeLastFrame
)
end
end, 50, 1)
end
end
end)
addEvent("onRequestPedFileCreation", true)
addEventHandler("onRequestPedFileCreation", root, function(filePath, sequenceData)
local client = source
outputDebugString("[PAS]: Received file creation request from client: " .. tostring(filePath), 4, 100, 255, 100)
local fullPath = "anim_sequences/" .. filePath .. ".json"
local fileCounts = 1
-- Ensure unique filename
while fileExists(fullPath) do
fileCounts = fileCounts + 1
fullPath = "anim_sequences/" .. filePath .. tostring(fileCounts) .. ".json"
end
outputDebugString("[PAS]: Using path: " .. fullPath, 4, 100, 255, 100)
-- Add to file list first
local success = addFileListEntry(fullPath, sequenceData)
if not success then
outputDebugString("[PAS]: Failed to add entry to file_list.json", 0, 255, 100, 100)
triggerClientEvent(client, "onFileCreationError", client, "Failed to add to file list")
return
end
-- Create and open the file
local file = fileCreate(fullPath)
if not file then
outputDebugString("[PAS]: Failed to create output file: " .. fullPath, 0, 255, 100, 100)
triggerClientEvent(client, "onFileCreationError", client, "Failed to create file")
return
end
-- Convert sequence data to JSON string
local jsonData = toJSON(sequenceData, true)
if not jsonData then
fileClose(file)
fileDelete(fullPath)
outputDebugString("[PAS]: Failed to convert data to JSON", 0, 255, 100, 100)
triggerClientEvent(client, "onFileCreationError", client, "Failed to convert data")
return
end
-- Write the JSON data to file
local written = fileWrite(file, jsonData)
fileClose(file)
if not written then
fileDelete(fullPath)
outputDebugString("[PAS]: Failed to write to file: " .. fullPath, 0, 255, 100, 100)
triggerClientEvent(client, "onFileCreationError", client, "Failed to write data")
return
end
-- Notify the client
triggerClientEvent(client, "onFileCreationSuccess", client, fullPath)
outputDebugString("[PAS]: File creation successful: " .. fullPath, 4, 100, 255, 100)
end)
addEvent("onRequestPedFileList", true)
addEventHandler("onRequestPedFileList", root, function()
local client = source
local directory = "anim_sequences/"
local fileListPath = directory .. "file_list.json"
local fileList = {}
-- Create file_list.json if it doesn't exist
if not fileExists(fileListPath) then
outputDebugString("[PAS]: File list does not exist, creating: 'file_list.json'", 4, 100, 255, 100)
local newFile = fileCreate(fileListPath)
if newFile then
fileWrite(newFile, "[]")
fileClose(newFile)
else
outputDebugString("[PAS]: Failed to create file_list.json", 0, 255, 100, 100)
triggerClientEvent(client, "onReceivePedFileList", client, {}, "Failed to create file list")
return
end
end
-- Open and read the file
local file = fileOpen(fileListPath, true)
if not file then
outputDebugString("[PAS]: Failure to open 'file_list.json'", 0, 255, 100, 100)
triggerClientEvent(client, "onReceivePedFileList", client, {}, "Failed to open file list")
return
end
-- Read file content
local fileContent = fileRead(file, fileGetSize(file))
fileClose(file)
-- Parse JSON content
if fileContent and fileContent ~= "" then
local success, data = pcall(fromJSON, fileContent)
if success and type(data) == "table" then
fileList = data
-- Validate each entry
for i = #fileList, 1, -1 do
if not fileExists(fileList[i].path) then
outputDebugString("[PAS]: Removing invalid entry: " .. tostring(fileList[i].path), 4, 255, 165, 0)
table.remove(fileList, i)
end
end
-- Update file_list.json if entries were removed
if #data ~= #fileList then
local updatedFile = fileCreate(fileListPath)
if updatedFile then
fileWrite(updatedFile, toJSON(fileList, true))
fileClose(updatedFile)
outputDebugString("[PAS]: Updated file_list.json after removing invalid entries", 4, 100, 255, 100)
end
end
else
outputDebugString("[PAS]: Failed to parse JSON content", 0, 255, 100, 100)
triggerClientEvent(client, "onReceivePedFileList", client, {}, "Failed to parse file list")
return
end
end
outputDebugString("[PAS]: Sending file list to client with " .. #fileList .. " entries", 4, 100, 255, 100)
triggerClientEvent(client, "onReceivePedFileList", client, fileList)
end)
addEvent("onRequestSequenceLoad", true)
addEventHandler("onRequestSequenceLoad", root, function(filePath)
local client = source
if not fileExists(filePath) then
outputDebugString("[PAS]: Failed to load sequence, file not found: " .. tostring(filePath), 0, 255, 100, 100)
triggerClientEvent(client, "onSequenceLoadError", client, "File not found")
return
end
local file = fileOpen(filePath, true)
if not file then
outputDebugString("[PAS]: Failed to open file: " .. filePath, 0, 255, 100, 100)
triggerClientEvent(client, "onSequenceLoadError", client, "Could not open file")
return
end
local fileContent = fileRead(file, fileGetSize(file))
fileClose(file)
if not fileContent or fileContent == "" then
outputDebugString("[PAS]: File is empty: " .. filePath, 0, 255, 100, 100)
triggerClientEvent(client, "onSequenceLoadError", client, "File is empty")
return
end
local success, data = pcall(fromJSON, fileContent)
if not success or type(data) ~= "table" then
outputDebugString("[PAS]: Failed to parse JSON content from: " .. filePath, 0, 255, 100, 100)
triggerClientEvent(client, "onSequenceLoadError", client, "Invalid file format")
return
end
outputDebugString("[PAS]: Successfully loaded sequence from: " .. filePath, 4, 100, 255, 100)
triggerClientEvent(client, "onSequenceLoadSuccess", client, data)
end)
addEvent("onPedRequestSequenceLoad", true)
addEventHandler("onPedRequestSequenceLoad", root, function(filePath, targetPed)
local client = client
if not client then
return
end
local file = fileOpen(filePath)
if not file then
triggerClientEvent("onSequenceLoadError", client, "File not found: " .. filePath)
return
end
local content = fileRead(file, fileGetSize(file))
fileClose(file)
local sequenceData = fromJSON(content)
if not sequenceData then
triggerClientEvent("onSequenceLoadError", client, "Invalid JSON format")
return
end
triggerClientEvent("onPedSequenceLoadSuccess", client, sequenceData, targetPed)
end)