-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsaveloadpeds_server.lua
More file actions
562 lines (493 loc) · 15 KB
/
saveloadpeds_server.lua
File metadata and controls
562 lines (493 loc) · 15 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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
local currentMapName = nil
local scriptContent = [[
local mapPedSequences = {}
local markerPeds = {}
function mapPlaySequenceOnPed(sequenceData, ped)
if not sequenceData then
return
end
if not isElement(ped) then
return
end
local frames = sequenceData.frames or sequenceData
local loopSequence = sequenceData.loopSequence or false
if not frames or #frames == 0 then
return
end
playSequenceForPed(ped, frames, loopSequence)
end
function isSequencePlayingOnPed(ped)
return mapPedSequences[ped] ~= nil
end
function stopSequenceForPed(ped)
if mapPedSequences[ped] then
if isTimer(mapPedSequences[ped].timer) then
killTimer(mapPedSequences[ped].timer)
end
mapPedSequences[ped] = nil
end
end
function playSequenceForPed(ped, sequence, loopSequence)
if not isElement(ped) or not sequence or #sequence == 0 then
return
end
stopSequenceForPed(ped)
mapPedSequences[ped] = {
sequence = sequence,
currentIndex = 1,
lastProgress = 0,
loopSequence = loopSequence or false,
timer = nil
}
mapPedSequences[ped].timer = setTimer(checkAnimationProgressForPed, getFPSLimit() or 50, 0, ped)
playNextAnimationForPed(ped)
end
function checkAnimationProgressForPed(ped)
if not mapPedSequences[ped] or not isElement(ped) then
stopSequenceForPed(ped)
return
end
local state = mapPedSequences[ped]
local currentProgress = getPedAnimationProgress(ped)
local frame = state.sequence[state.currentIndex]
if currentProgress == -1 then
state.currentIndex = state.currentIndex + 1
if state.currentIndex > #state.sequence then
if state.loopSequence then
state.currentIndex = 1
else
stopSequenceForPed(ped)
state.lastProgress = currentProgress
return
end
end
playNextAnimationForPed(ped)
end
state.lastProgress = currentProgress
end
function playNextAnimationForPed(ped)
if not mapPedSequences[ped] or not isElement(ped) then
return
end
local state = mapPedSequences[ped]
if state.currentIndex > #state.sequence and not state.loopSequence then
stopSequenceForPed(ped)
return
elseif state.currentIndex > #state.sequence and state.loopSequence then
state.currentIndex = 1
end
local frame = state.sequence[state.currentIndex]
setPedAnimation(
ped,
frame.category,
frame.animation,
-1,
frame.loop,
frame.updatePosition,
frame.interruptable,
frame.freezeLastFrame,
250
)
end
function createPedFromData(anim, startAnimation)
local ped = createPed(anim.id, anim.x, anim.y, anim.z, anim.rz or 0.0)
if not ped then
return nil
end
setElementFrozen(ped, true)
setElementDimension(ped, getElementDimension(localPlayer))
anim.pedElement = ped
if startAnimation then
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
mapPlaySequenceOnPed(sequenceData, ped)
end
end
else
setPedAnimation(ped, anim.category, anim.animation, -1, anim.loop, anim.updatePosition, anim.interruptable, anim.freezeLastFrame)
end
end
if anim.giveWeapon then
giveWeapon(ped, getWeaponIDFromName(anim.weapon) or 22, anim.ammo or 9999, true)
end
if anim.setAimTarget then
setPedAimTarget(ped, anim.aimX or 0.0, anim.aimY or 0.0, anim.aimZ or 0.0)
end
return ped
end
function createMarkersForPeds(animatedPeds)
local markersCreated = {}
for _, anim in ipairs(animatedPeds) do
if anim.markerData and anim.triggerMarker and not markersCreated[anim.triggerMarker] then
local marker = createMarker(
anim.markerData.x,
anim.markerData.y,
anim.markerData.z,
anim.markerData.markerType or "cylinder",
anim.markerData.size or 1.0,
255, 0, 0, 100
)
if marker then
anim.markerElement = marker
markersCreated[anim.triggerMarker] = true
end
end
end
end
function createBatchApplyAnimations(animatedPeds)
for _, anim in ipairs(animatedPeds) do
local startAnimation = not anim.triggerMarker
createPedFromData(anim, startAnimation)
end
end
function playAnimationForPedsOnMarkerHit(markerElement)
if not isElement(markerElement) then
return
end
for i, anim in ipairs(animatedPeds) do
if anim.markerElement == markerElement then
local ped = anim.pedElement
if ped and isElement(ped) and getElementType(ped) == "ped" then
stopSequenceForPed(ped)
setElementFrozen(ped, false)
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
mapPlaySequenceOnPed(sequenceData, ped)
if not markerPeds[markerElement] then
markerPeds[markerElement] = {}
end
table.insert(markerPeds[markerElement], ped)
end
end
else
setPedAnimation(ped, anim.category, anim.animation, -1, anim.loop, anim.updatePosition, anim.interruptable, anim.freezeLastFrame, 250, false)
if not markerPeds[markerElement] then
markerPeds[markerElement] = {}
end
table.insert(markerPeds[markerElement], ped)
end
end
end
end
end
function stopAnimationForPedsOnMarkerLeave(markerElement)
if not markerPeds[markerElement] then
return
end
for _, ped in ipairs(markerPeds[markerElement]) do
if isElement(ped) then
stopSequenceForPed(ped)
setPedAnimation(ped)
setElementFrozen(ped, true)
end
end
markerPeds[markerElement] = nil
end
function cleanupBatchApplyAnimations()
for ped, _ in pairs(mapPedSequences) do
if isElement(ped) then
stopSequenceForPed(ped)
end
end
for _, anim in ipairs(animatedPeds) do
if anim.pedElement and isElement(anim.pedElement) then
stopSequenceForPed(anim.pedElement)
destroyElement(anim.pedElement)
anim.pedElement = nil
end
if anim.markerElement and isElement(anim.markerElement) then
destroyElement(anim.markerElement)
anim.markerElement = nil
end
end
markerPeds = {}
mapPedSequences = {}
end
addEventHandler("onClientResourceStart", resourceRoot, function()
setTimer(function()
createMarkersForPeds(animatedPeds)
createBatchApplyAnimations(animatedPeds)
end, 100, 1)
end)
addEventHandler("onClientResourceStop", resourceRoot, function()
cleanupBatchApplyAnimations()
end)
addEventHandler("onClientMarkerHit", root, function(player)
if player == localPlayer then
playAnimationForPedsOnMarkerHit(source)
end
end)
addEventHandler("onClientMarkerLeave", root, function(player)
--if player == localPlayer then
-- stopAnimationForPedsOnMarkerLeave(source)
--end
end)
addEventHandler("onClientPlayerWasted", localPlayer, function()
cleanupBatchApplyAnimations()
setTimer(function()
createMarkersForPeds(animatedPeds)
createBatchApplyAnimations(animatedPeds)
end, 100, 1)
end)
]]
addEventHandler("onResourceStart", resourceRoot, function()
-- Sadly have to delay this a bit to ensure everything is loaded
setTimer(function()
if not currentMapName and exports.editor_main:getCurrentMapName() then
currentMapName = exports.editor_main:getCurrentMapName()
loadPedsFromLua()
else
local filePath = "data/lastMapName.json"
local file = fileOpen(filePath)
if file then
local size = fileGetSize(file)
local content = fileRead(file, size)
fileClose(file)
local data = fromJSON(content)
if data and data.lastMapName then
currentMapName = data.lastMapName
loadPedsFromLua()
end
end
end
end, 50, 1)
addEvent("onNewMap")
addEventHandler("onNewMap", root, function(mapName)
currentMapName = mapName
appliedAnimations = {}
triggerClientEvent(root, "onReceiveAppliedAnimationsFromServer", root, appliedAnimations)
for _, ped in ipairs(getElementsByType("ped")) do
if getElementID(ped) and getElementID(ped):find("^pas") then
destroyElement(ped)
end
end
end)
addEvent("onMapOpened")
addEventHandler("onMapOpened", root, function(mapName)
currentMapName = getResourceName(mapName)
for _, ped in ipairs(getElementsByType("ped")) do
if getElementID(ped) and getElementID(ped):find("^pas") then
destroyElement(ped)
end
end
loadPedsFromLua()
local filePath = "data/lastMapName.json"
local file = fileCreate(filePath)
if file then
local jsonData = toJSON({ lastMapName = currentMapName })
fileWrite(file, jsonData)
fileClose(file)
end
end)
addEvent("saveResource", true)
addEventHandler("saveResource", root, function(resName)
if exports.editor_main:getCurrentMapName() then
currentMapName = exports.editor_main:getCurrentMapName()
end
triggerClientEvent(root, "onServerRequestAppliedAnimations", root)
end)
addEvent("quickSaveResource", true)
addEventHandler("quickSaveResource", root, function(resName)
if exports.editor_main:getCurrentMapName() then
currentMapName = exports.editor_main:getCurrentMapName()
end
triggerClientEvent(root, "onServerRequestAppliedAnimations", root)
end)
addEvent("onReceiveAppliedAnimationsForSave", true)
addEventHandler("onReceiveAppliedAnimationsForSave", root, function(receivedAnimations)
outputDebugString("Received appliedAnimations: " .. tostring(receivedAnimations))
appliedAnimations = receivedAnimations or {}
savePedsToLua(appliedAnimations)
copySequenceJsonFiles(appliedAnimations)
addScriptNodeToMeta(appliedAnimations)
end)
end)
function copySequenceJsonFiles(animations)
if not currentMapName then
outputDebugString("[PAS] ERROR: currentMapName is nil, cannot copy files", 1)
return
end
for i, anim in ipairs(animations) do
if anim.type == "sequence" and anim.sequenceName then
local sourcePath = ":"
.. getResourceName(getThisResource())
.. "/anim_sequences/"
.. anim.sequenceName
.. ".json"
local destPath = ":" .. currentMapName .. "/anim_sequences/" .. anim.sequenceName .. ".json"
if fileExists(sourcePath) then
if fileExists(destPath) then
fileDelete(destPath)
end
fileCopy(sourcePath, destPath)
else
outputDebugString("[PAS] WARNING: Sequence JSON not found: " .. sourcePath, 2)
end
end
end
end
function savePedsToLua(animations)
local path = ":" .. currentMapName .. "/pedAnimationSequencer.lua"
local file = fileCreate(path)
if file then
local text = "local animatedPeds = {\n"
for _, data in ipairs(animations) do
local markerDataStr = "nil"
if data.markerData then
markerDataStr = string.format(
'{id = "%s", x = %.2f, y = %.2f, z = %.2f, size = %.2f, markerType = "%s"}',
data.markerData.id or "",
data.markerData.x or 0,
data.markerData.y or 0,
data.markerData.z or 0,
data.markerData.size or 1.0,
data.markerData.markerType or "cylinder"
)
end
if data.type == "sequence" then
text = text
.. string.format(
' {type = "sequence", sequenceName = "%s", name = "%s", id = %d, x = %.2f, y = %.2f, z = %.2f, rz = %.2f, category = "%s", animation = "%s", loop = %s, updatePosition = %s, interruptable = %s, freezeLastFrame = %s, ammo = %d, giveWeapon = %s, weapon = "%s", setAimTarget = %s, aimX = %.2f, aimY = %.2f, aimZ = %.2f, triggerMarker = %s, markerData = %s},\n',
data.sequenceName,
tostring(data.name:gsub("[%s%(%)]+", "")),
data.id,
data.x,
data.y,
data.z,
data.rz,
data.category,
data.animation,
tostring(data.loop),
tostring(data.updatePosition),
tostring(data.interruptable),
tostring(data.freezeLastFrame),
data.ammo or 0,
tostring(data.giveWeapon),
data.weapon or "",
tostring(data.setAimTarget),
data.aimX or 0,
data.aimY or 0,
data.aimZ or 0,
data.triggerMarker and string.format('"%s"', data.triggerMarker) or "nil",
markerDataStr
)
else
text = text
.. string.format(
' {name = "%s", id = %d, x = %.2f, y = %.2f, z = %.2f, rz = %.2f, category = "%s", animation = "%s", loop = %s, updatePosition = %s, interruptable = %s, freezeLastFrame = %s, ammo = %d, giveWeapon = %s, weapon = "%s", setAimTarget = %s, aimX = %.2f, aimY = %.2f, aimZ = %.2f, triggerMarker = %s, markerData = %s},\n',
tostring(data.name:gsub("[%s%(%)]+", "")),
data.id,
data.x,
data.y,
data.z,
data.rz,
data.category,
data.animation,
tostring(data.loop),
tostring(data.updatePosition),
tostring(data.interruptable),
tostring(data.freezeLastFrame),
data.ammo or 0,
tostring(data.giveWeapon),
data.weapon or "",
tostring(data.setAimTarget),
data.aimX or 0,
data.aimY or 0,
data.aimZ or 0,
data.triggerMarker and string.format('"%s"', data.triggerMarker) or "nil",
markerDataStr
)
end
end
fileWrite(file, text .. "}" .. "\n\n" .. scriptContent)
fileClose(file)
return true
else
return false
end
end
function addScriptNodeToMeta(animations)
local metaPath = ":" .. currentMapName .. "/meta.xml"
if not fileExists(metaPath) then
return false
end
local file = fileOpen(metaPath)
if not file then
return false
end
local size = fileGetSize(file)
local content = fileRead(file, size)
fileClose(file)
local modified = false
if not content:find("pedAnimationSequencer.lua", 1, true) then
local scriptNode = ' <script src="pedAnimationSequencer.lua" type="client" />'
content = content:gsub("</meta>", scriptNode .. "\n</meta>")
modified = true
end
for _, anim in ipairs(animations) do
if anim.type == "sequence" and anim.sequenceName then
local jsonFileName = anim.sequenceName .. ".json"
if not content:find(jsonFileName, 1, true) then
local fileNode = ' <file src="anim_sequences/' .. jsonFileName .. '" />'
content = content:gsub("</meta>", fileNode .. "\n</meta>")
modified = true
end
end
end
if modified then
local file = fileCreate(metaPath)
if file then
fileWrite(file, content)
fileClose(file)
outputDebugString("[PAS] Updated meta with script and sequence files", 4, 100, 255, 100)
return true
end
else
outputDebugString("[PAS] meta and script already up to date, no changes needed", 4, 100, 255, 100)
end
return true
end
function loadPedsFromLua()
local path = ":" .. currentMapName .. "/pedAnimationSequencer.lua"
if not fileExists(path) then
return
end
local file = fileOpen(path)
if file then
local size = fileGetSize(file)
local content = fileRead(file, size)
fileClose(file)
local tableChunk = content:match("local%s+animatedPeds%s*=%s*(%b{})")
if tableChunk then
local func = loadstring("return " .. tableChunk)
if func then
local ok, data = pcall(func)
if ok and type(data) == "table" then
appliedAnimations = data
outputDebugString("[PAS] Loaded " .. #appliedAnimations .. " animations from map", 4, 100, 255, 100)
else
appliedAnimations = {}
end
else
appliedAnimations = {}
end
else
appliedAnimations = {}
end
else
appliedAnimations = {}
end
triggerClientEvent(root, "onReceiveAppliedAnimationsFromServer", root, appliedAnimations)
end