-
Notifications
You must be signed in to change notification settings - Fork 2
/
Save Storage.lua
668 lines (565 loc) · 21 KB
/
Save Storage.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
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
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
--[[ Kieron's Autosave and Retrieval ]]--
-- Setup --
-----------
function onload()
local oldStorage = findSaveStorage()
if oldStorage and not (oldStorage==nil) and not (oldStorage.getVar("ForceDestruct")) then
printToAll("Failed to deploy Save Storage: Bag already exists.", {1,0.75,0.75})
ForceDestruct = true
destroyObject(self)
return
end
ForceDestruct = nil
playerZone = {
["Black"] = {zone=getObjectFromGUID("275a5d")},
["Pink"] = {zone=getObjectFromGUID("44f05e"), prestige=getObjectFromGUID("0b4a58"), tbl=getObjectFromGUID("bb54b1")},
["Purple"] = {zone=getObjectFromGUID("63ef4e"), prestige=getObjectFromGUID("17ddfd"), tbl=getObjectFromGUID("b2ab0b")},
["Blue"] = {zone=getObjectFromGUID("423ae1"), prestige=getObjectFromGUID("f87e7b"), tbl=getObjectFromGUID("7a414f")},
["Teal"] = {zone=getObjectFromGUID("5c2692"), prestige=getObjectFromGUID("3484cc"), tbl=getObjectFromGUID("d21b66")},
["Green"] = {zone=getObjectFromGUID("595fa9"), prestige=getObjectFromGUID("a7bb1b"), tbl=getObjectFromGUID("2612ed")},
["Yellow"] = {zone=getObjectFromGUID("5b82fd"), prestige=getObjectFromGUID("944b87"), tbl=getObjectFromGUID("a7596f")},
["Orange"] = {zone=getObjectFromGUID("38b2d7"), prestige=getObjectFromGUID("844d3d"), tbl=getObjectFromGUID("efae07")},
["Red"] = {zone=getObjectFromGUID("8b37f7"), prestige=getObjectFromGUID("d8cd49"), tbl=getObjectFromGUID("b54e19")},
["Brown"] = {zone=getObjectFromGUID("1c13af"), prestige=getObjectFromGUID("6c29ce"), tbl=getObjectFromGUID("688678")},
["White"] = {zone=getObjectFromGUID("a751f4"), prestige=getObjectFromGUID("88482c"), tbl=getObjectFromGUID("33b903")},
}
hadStarter = {}
saveBag = getObjectFromGUID("4c4c08")
starterBag = getObjectFromGUID("f3ea0f")
buttonHandler = getObjectFromGUID("81dac7")
sitRetrieve = true
lockout = false
populateTable()
createButtons()
ThisScript = self.getLuaScript()
end
function populateTable()
local saveObjects = self.getObjects()
for i, object in ipairs(saveObjects) do
hadStarter[object.name or ""] = true
end
end
function findSaveStorage()
for _,obj in pairs(getAllObjects()) do
if obj.getName()==self.getName() and obj.getTable("hadStarter") and not (obj==nil) then -- Exists, matches, and initialised
return obj -- Return it
end
end
end
-- Process Save --
------------------
function doObjectName(object, color)
object.setName("Player save: " .. Player[color].steam_name)
object.setDescription(Player[color].steam_id .." - ".. Player[color].steam_name)
end
local saveQueue = {}
function DoSaveQueue()
if #saveQueue==0 then Timer.destroy("DoSaveQueue") return end
local currentSaveData = saveQueue[1]
local box,col,force = currentSaveData[1],currentSaveData[2],currentSaveData[3]
while saveQueue[1] and saveQueue[1][1]==box do
force = force or saveQueue[1][3]
table.remove(saveQueue, 1)
end
if (not box) or (box==nil) then return end
box.unlock()
local saveObjects = self.getObjects()
local params = {}
params.position = self.getPosition()
local id = box.getDescription():match("^(%d+)")
if not id then
if col then
broadcastToColor("Error: Failed to find ID in description.", col or "Black", {1,0.25,0.25})
else
print( "Failed to find ID in description. Autosave failed.")
end
return
end
if not force then
local foundCol
for _,col in pairs(getSeatedPlayers()) do
if Player[col].seated and Player[col].steam_id==id then
foundCol = col
break
end
end
if foundCol then
local set = Global.call( "forwardFunction", {function_name="findObjectSetFromColor", data={foundCol}} )
if set and set.zone then
box.setPosition( set.zone.getPosition() )
box.setRotation( {0,0,0} )
return
end
end
end
self.putObject(box)
end
function DoAutoSave( col, save, itemData, forceSave )
if itemData then
for i=1,math.min(10,#itemData) do
local obj = itemData[1]
table.remove( itemData, 1 )
if obj then
if not (obj==nil) then -- Do not remove the brackets here. `(not obj==nil)` is not the same as `not (obj==nil)`
obj.unlock()
save.putObject( obj )
end
elseif #itemData<=0 then
break
end
end
end
if itemData and #itemData>0 then
Wait.frames(function()
DoAutoSave( col, save, itemData, forceSave )
end, 2)
else
local set = Global.call( "forwardFunction", {function_name="findObjectSetFromColor", data={col}} )
if set and set.container.getQuantity()>0 then
local params = {}
params.position = set.container.getPosition()
for i=1,math.min(set.container.getQuantity(), 20) do
params.position.y = params.position.y + 0.25
local taken = set.container.takeObject(params)
taken.unlock()
save.putObject( taken )
end
Wait.frames(function()
DoAutoSave( col, save, itemData, forceSave )
end, 2)
else
table.insert(saveQueue, {save, col, forceSave})
Timer.destroy("DoSaveQueue")
Timer.create({identifier="DoSaveQueue", function_name="DoSaveQueue", delay=0.25, repetitions=0})
end
end
end
function save(o, color)
if not lockout then
lockoutTimer(1.2)
local saveObjects = self.getObjects()
local saveFound = false
local foundSaves = {}
local zoneObjects = playerZone[color].zone.getObjects()
for i, object in ipairs(zoneObjects) do
if string.find(object.getName(), 'Player save:') then
foundSaves[object] = true
-- table.insert(saveQueue, {object, color, true})
saveFound = true
end
end
if not saveFound then
broadcastToColor("Error: No save found.\nPlease position your save inside your colored zone.", color, {1,0.25,0.25})
return
end
local processed = {}
local tblObjects = playerZone[color].tbl.getObjects()
local prestigeObjects = playerZone[color].prestige.getObjects()
for saveBag in pairs(foundSaves) do
saveBag.lock()
saveBag.setRotation( {0,0,0} )
local plyID = saveBag.getDescription():match("^(%d+)")
local objectsTbl = {}
-- Table stuff
for _,objList in pairs({tblObjects,zoneObjects,prestigeObjects}) do
for _,v in pairs(objList) do
if v~=saveBag and v.interactable and v.held_by_color~=color and not (processed[v] or foundSaves[v] or v.getLock()) then
local id = v.getDescription():match("^(%d+) %- .*")
if (not id) or (id==plyID) then
table.insert(objectsTbl, v) -- Stuff to save in this loop
table.insert(processed, v) -- Stuff currently in a save queue
v.lock()
end
end
end
end
-- Delayed unfreeze and save
print( tostring(color)..": Auto saving" )
Wait.frames(function()
DoAutoSave( color, saveBag, objectsTbl, true ) -- col, bag, objects, forceSave
end, 2)
end
else
broadcastToColor("Error: Button delay is active.\nWait a moment then try again.", color, {1,0.25,0.25})
end
end
-- Process Dropped Object --
----------------------------
function getSaveFromID( id )
for _,box in ipairs(self.getObjects()) do
if box.name:find(id) then
return box
end
end
end
function onObjectEnterContainer(bag,o)
if o==self then
doPlacedInBag(bag)
return
end
if bag~=self then return end
local isSaveFormat = o.getName():match("| %d+ | %d+$")
if isSaveFormat then return end -- Proper format, we can assume it's fine (Probably added by script)
local saveBox = o.getName():find("Player save:")
local foundID = o.getDescription():match("^(%d+) %- .*")
if not foundID then return EjectGrabbedObject(o) end -- Not a saveable object (no id)
local targetPos = self.getPosition()
targetPos.y = targetPos.y - 10
targetPos.z = targetPos.z - 5
local params = {position = targetPos, smooth = false}
local matchingSave = getSaveFromID(foundID)
if matchingSave then
params.index = matchingSave.index
local deployedBox = self.takeObject(params)
if deployedBox then
local lastFoundObj
for _,obj in ipairs(self.getObjects()) do -- params.guid always starts from the bottom regardless of params.top
if obj.guid==o.getGUID() then
lastFoundObj = obj
end
end
if lastFoundObj then
-- Place object into found save box
params.index = lastFoundObj.index
local deployedInserted = self.takeObject(params)
if deployedInserted then
deployedBox.putObject(deployedInserted)
-- Wait.frames(function() -- Replace save box
self.putObject(deployedBox)
-- end, 1)
destroyObject(deployedInserted) -- This is necessary to stop duplicated. Not entirely sure why.
end
else
self.putObject(deployedBox)
end
end
return
elseif saveBox and not matchingSave then
-- Get obj
local lastFoundObj
for _,obj in ipairs(self.getObjects()) do
if obj.guid==o.getGUID() then
lastFoundObj = obj
end
end
if lastFoundObj then
params.index = lastFoundObj.index
local deployedObject = self.takeObject(params)
if deployedObject then
-- Rename and replace
deployedObject.setName( ("%s | %i | %s"):format(o.getName(), os.time(), foundID) )
deployedObject.setDescription("")
self.putObject(deployedObject)
end
end
return
end
EjectGrabbedObject(o)
end
function EjectGrabbedObject(o)
local lastFoundObj
for _,obj in ipairs(self.getObjects()) do -- params.guid always starts from the bottom regardless of params.top
if obj.guid==o.getGUID() then
lastFoundObj = obj
end
end
local params = {position = {0,10,0}, smooth = false}
if lastFoundObj then
params.index = lastFoundObj.index
local deployedObject = self.takeObject(params)
if deployedObject then
local id,name = o.getDescription():match("^(%d+) %- ([^\n]*)")
if Player["Black"].seated then
broadcastToColor( ("Save Storage: Ejected object \"%s\" owned by %s (%s)"):format(deployedObject.getName(), name or "nobody", id or "no id"), "Black", {1,0,0} )
end
for k,adminCol in pairs(getSeatedPlayers()) do
if Player[adminCol].admin then
broadcastToColor( ("Save Storage: Ejected object \"%s\" owned by %s (%s)"):format(deployedObject.getName(), name or "nobody", id or "no id"), adminCol, {1,0,0} )
end
end
end
end
end
function doPlacedInBag( bag )
local lastFoundObj
for _,obj in ipairs(bag.getObjects()) do -- params.guid always starts from the bottom regardless of params.top
if obj.guid==self.getGUID() then
lastFoundObj = obj
end
end
local params = {position = {0,10,0}, smooth = false}
if lastFoundObj then
params.index = lastFoundObj.index
local deployedObject = bag.takeObject(params)
destroyObject(deployedObject)
end
end
local RespawnPos = {-2.00,1.46,-20.50}
local RespawnRot = {0,0,0}
local RespawnScale = {0.68,0.68,0.68}
function onDestroy()
if ForceDestruct then return end -- We're destroying ourself
if self.held_by_color and Player[self.held_by_color].admin then return end -- Held by admin, assume it's legit
local json = self.getJSON()
local params = {
json = json,
position = RespawnPos,
rotation = RespawnRot,
scale = RespawnScale,
}
printToAll("Warning: Save Storage has gone missing! Attempting restore...", {1,0,0})
local newObj = spawnObjectJSON(params)
newObj.setLuaScript( ThisScript )
newObj.setLock(true)
end
-- Process Prestige --
----------------------
function doPrestige( data )
if not (data.id and (data.destroyChips or data.destroyPowerups or data.destroyPrestige)) then return end
local matchingSave = getSaveFromID( data.id )
if not matchingSave then return end
local targetPos = self.getPosition()
targetPos.y = targetPos.y - 10
targetPos.z = targetPos.z - 5
local params = {position = targetPos, smooth = false, index=matchingSave.index}
local deployedBox = self.takeObject(params)
if not deployedBox then return end
local newBox = Global.call( "forwardFunction", {function_name="beginRecursiveBagCleanup", data={deployedBox, data.id, data.destroyChips or false, data.destroyPowerups or false, data.destroyPrestige or false}} )
if newBox then
self.putObject(newBox)
else
self.putObject(deployedBox)
end
end
-- Process Load --
------------------
function claim(o, color)
if not lockout then
lockoutTimer(1.2)
local saveObjects = self.getObjects()
local saveFound = false
local params = {}
params.position = playerZone[color].zone.getPosition()
for i, object in ipairs(saveObjects) do
if object.name:match("%d+$") == Player[color].steam_id then
params.index = object.index
local foundObject = self.takeObject(params)
doObjectName(foundObject, color)
return
end
end
broadcastToColor("Error: No save found.\nDo you already have your save?", color, {1,0.25,0.25})
else
broadcastToColor("Error: Button delay is active.\nWait a moment then try again.", color, {1,0.25,0.25})
end
end
function free(o, color)
if not lockout then
lockoutTimer(1.2)
local hadSave = false
if hadStarter[Player[color].steam_id or ""] then
hadSave = true
if color~="Black" and not Player[color].admin then
broadcastToColor("Error: You either already have a save or\nyou have already claimed your free starter.", color, {1,0.25,0.25})
return
end
end
local params = {}
params.position = playerZone[color].zone.getPosition()
local saveContainer = saveBag.takeObject(params)
saveContainer.shuffle()
local playerSave = saveContainer.takeObject(params)
saveContainer.destruct()
if not hadSave then doObjectName(playerSave, color) end
params.position.y = params.position.y + 2
local starter = starterBag.takeObject(params)
local starterObjects = starter.getObjects()
for i, object in ipairs(starterObjects) do
params.position.y = params.position.y + 1.5
local taken = starter.takeObject(params)
if taken then
local oldDesc = taken.getDescription() or ""
if #oldDesc>0 then oldDesc = "\n\n"..oldDesc end
taken.setDescription( ("%s - %s%s"):format( Player[color].steam_id, Player[color].steam_name, oldDesc ) )
playerSave.putObject(taken)
end
end
starter.destruct()
hadStarter[Player[color].steam_id or ""] = true
else
broadcastToColor("Error: Button delay is active.\nWait a moment then try again.", color, {1,0.25,0.25})
end
end
-- Player Join/Leave --
-----------------------
function onPlayerChangeColor(color)
-- Auto Save --
for _,oldCol in pairs(Player.getAvailableColors()) do
local zone = playerZone[oldCol]
if oldCol~="Black" and zone.wasSeated and not (Player[oldCol].seated) then -- Player not currently seated, but was last check
local tblObjects = zone.tbl.getObjects()
local prestigeObjects = zone.prestige.getObjects()
local zoneObjects = zone.zone.getObjects()
-- Find save container
local plyID = zone.wasSeatedID
local foundSave = nil
for _,objList in pairs({tblObjects,zoneObjects,prestigeObjects}) do
for _,v in pairs(objList) do
if v.interactable and not v.getLock() and v.getName():find("Player save", 1, false) then
local id = v.getDescription():match("^(%d+) %- .*")
if (not id) or (id==plyID) then
foundSave = v
break
end
end
end
if foundSave then break end
end
if foundSave then
foundSave.lock()
foundSave.setRotation( {0,0,0} )
local objectsTbl = {}
-- Find savable objects
for _,objList in pairs({tblObjects,zoneObjects,prestigeObjects}) do
for _,v in pairs(objList) do
if v~=foundSave and v.interactable and v.held_by_color~=oldCol and not v.getLock() then
local id = v.getDescription():match("^(%d+) %- .*")
if (not id) or (id==plyID) then
table.insert(objectsTbl, v)
v.lock()
end
end
end
end
-- Delayed unfreeze and save
print( tostring(oldCol)..": Auto saving" )
Wait.frames(function()
DoAutoSave(oldCol, foundSave, objectsTbl)
end, 2)
end
end
zone.wasSeated = (color==oldCol or Player[oldCol].seated)
zone.wasSeatedID = Player[oldCol].seated and Player[oldCol].steam_id or nil
end
-- Auto load --
if sitRetrieve and (color ~= "Black" and color ~= "Grey") then
local playerID = Player[color].steam_id
local saveObjects = self.getObjects()
local params = {}
params.position = playerZone[color].zone.getPosition()
params.callback_function = function(o) loadedPlayerSave(o, color, playerID) end
for i, object in ipairs(saveObjects) do
if object.name:match("%d+$") == playerID then
params.index = object.index
local foundObject = self.takeObject(params)
doObjectName(foundObject, color)
broadcastToColor("Save found: Welcome back ".. Player[color].steam_name .."!", color, {0.25,1,0.25})
hadStarter[playerID or ""] = true
return
end
end
-- Auto New --
if not hadStarter[playerID or ""] then
local params = {}
params.position = playerZone[color].zone.getPosition()
local saveContainer = saveBag.takeObject(params)
saveContainer.shuffle()
params.callback_function = function(o) loadedPlayerSave(o, color, playerID) end
local playerSave = saveContainer.takeObject(params)
saveContainer.destruct()
doObjectName(playerSave, color)
params.position.y = params.position.y + 2
local starter = starterBag.takeObject(params)
local starterObjects = starter.getObjects()
for i, object in ipairs(starterObjects) do
params.position.y = params.position.y + 1.5
local taken = starter.takeObject(params)
if taken then
local oldDesc = taken.getDescription() or ""
if #oldDesc>0 then oldDesc = "\n\n"..oldDesc end
taken.setDescription( ("%s - %s%s"):format( playerID, Player[color].steam_name, oldDesc ) )
playerSave.putObject(taken)
end
end
starter.destruct()
hadStarter[playerID or ""] = true
else
broadcastToColor("Auto-load: Failed to load save.\nDo you already have your save?", color, {1,0.25,0.25})
end
end
end
function loadedPlayerSave( playerSave, col, id )
if not col then return end
if (Player[col].seated and Player[col].steam_id==id) then return end -- Player's still here, we don't need to do anything
-- Player gone, perform save
Wait.frames(function()
DoAutoSave( col, playerSave )
end, 2)
end
-- Buttons --
-------------
function lockoutTimer(time)
lockout = true
Timer.destroy(self.getGUID())
Timer.create({identifier=self.getGUID(), function_name='concludeLockout', delay=time})
end
function concludeLockout()
lockout = false
end
function purge(time, col) -- Based on unix time, so time argument is seconds
if not (col == "Black" or Player[col].host) then
broadcastToColor("You cannot do this.", col, {1,0,0})
return
end
local purgeTime = math.floor(os.time() - time)
lockoutTimer(1.2)
local saveObjects = self.getObjects()
local params = {}
params.position = self.getPosition()
params.position.z = params.position.z + 20
local count = 0
for i, object in ipairs(saveObjects) do
local find = object.name:match("| (%d+) | %d+$")
if find and ((tonumber(find) or 0) <= purgeTime) then
params.guid = object.guid
local foundObject = self.takeObject(params)
foundObject.destruct()
count = count + 1
end
end
if not (Player[col].host) then
print( string.format("%s old saves purged by %s (%s).", count, col, Player[col].steam_name) )
end
broadcastToColor( string.format("Purged %s old saves.", count), col, {0.9,0.2,0} )
end
-- 86400 seconds per day
function purgeWeek(_, col) return purge(604800, col) end -- 604 800 seconds per week
function purgeMonth(_, col) return purge(2592000, col) end -- 2 592 000 seconds per month
function purgeYear(_, col) return purge(31536000, col) end -- 31 536 000 seconds per year
function createButtons()
buttonHandler.clearButtons()
buttonHandler.createButton({
click_function='save', label='Save', function_owner=self,
position={-0.92,0.19,-0.19}, rotation={0,0,0}, width=450, height=300, font_size=150
})
buttonHandler.createButton({
click_function='claim', label='Claim', function_owner=self,
position={0,0.19,-0.19}, rotation={0,0,0}, width=450, height=300, font_size=150
})
buttonHandler.createButton({
click_function='free', label='Free', function_owner=self,
position={0.92,0.19,-0.19}, rotation={0,0,0}, width=450, height=300, font_size=150
})
self.createButton({
click_function='purgeWeek', label='Purge saves (Week)', function_owner=self,
position={0,0.3,1.5}, rotation={0,0,0}, width=1500, height=300, font_size=150
})
self.createButton({
click_function='purgeMonth', label='Purge saves (Month)', function_owner=self,
position={0,0.3,2.1}, rotation={0,0,0}, width=1500, height=300, font_size=150
})
self.createButton({
click_function='purgeYear', label='Purge saves (Year)', function_owner=self,
position={0,0.3,2.7}, rotation={0,0,0}, width=1500, height=300, font_size=150
})
end