forked from TES3MP/CoreScripts
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbase.lua
More file actions
1894 lines (1404 loc) · 67.5 KB
/
base.lua
File metadata and controls
1894 lines (1404 loc) · 67.5 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
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
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
require("patterns")
contentFixer = require("contentFixer")
tableHelper = require("tableHelper")
inventoryHelper = require("inventoryHelper")
packetBuilder = require("packetBuilder")
local BaseCell = class("BaseCell")
function BaseCell:__init(cellDescription)
self.data =
{
entry = {
description = cellDescription,
creationTime = os.time()
},
lastVisit = {},
objectData = {},
packets = {},
recordLinks = {}
}
self:EnsurePacketTables()
self.description = cellDescription
self.visitors = {}
self.authority = nil
self.isRequestingContainers = false
self.containerRequestPid = nil
self.isRequestingActorList = false
self.actorListRequestPid = nil
self.unusableContainerUniqueIndexes = {}
self.isExterior = false
if string.match(cellDescription, patterns.exteriorCell) then
self.isExterior = true
local _, _, gridX, gridY = string.find(cellDescription, patterns.exteriorCell)
self.gridX = tonumber(gridX)
self.gridY = tonumber(gridY)
end
end
function BaseCell:ContainsPosition(posX, posY)
local cellSize = 8192
if self.isExterior then
local correctGridX = math.floor(posX / cellSize)
local correctGridY = math.floor(posY / cellSize)
if self.gridX ~= correctGridX or self.gridY ~= correctGridY then
return false
end
end
return true
end
function BaseCell:HasEntry()
return self.hasEntry
end
-- Iterate through the packets table and ensure all packet types are included in it
function BaseCell:EnsurePacketTables()
if self.data.packets == nil then self.data.packets = {} end
for _, packetType in pairs(config.cellPacketTypes) do
if self.data.packets[packetType] == nil then
self.data.packets[packetType] = {}
end
end
end
-- Iterate through saved packets and ensure the object uniqueIndexes they refer to
-- actually exist
function BaseCell:EnsurePacketValidity()
for packetType, packetArray in pairs(self.data.packets) do
for arrayIndex, uniqueIndex in pairs(self.data.packets[packetType]) do
if self.data.objectData[uniqueIndex] == nil then
tableHelper.removeValue(self.data.packets[packetType], uniqueIndex)
end
end
end
end
-- Adding record links to cells is special because we'll keep track of the uniqueIndex
-- of every object that uses a particular generated record
function BaseCell:AddLinkToRecord(storeType, recordId, uniqueIndex)
if self.data.recordLinks == nil then self.data.recordLinks = {} end
local recordStore = RecordStores[storeType]
if recordStore ~= nil then
local recordLinks = self.data.recordLinks
if recordLinks[storeType] == nil then recordLinks[storeType] = {} end
if recordLinks[storeType][recordId] == nil then recordLinks[storeType][recordId] = {} end
if not tableHelper.containsValue(self.data.recordLinks[storeType][recordId], uniqueIndex) then
table.insert(self.data.recordLinks[storeType][recordId], uniqueIndex)
end
recordStore:AddLinkToCell(recordId, self)
recordStore:QuicksaveToDrive()
end
end
function BaseCell:RemoveLinkToRecord(storeType, recordId, uniqueIndex)
local recordStore = RecordStores[storeType]
if recordStore ~= nil then
local recordLinks = self.data.recordLinks
if recordLinks ~= nil and recordLinks[storeType] ~= nil and recordLinks[storeType][recordId] ~= nil then
local linkIndex = tableHelper.getIndexByValue(recordLinks[storeType][recordId], uniqueIndex)
if linkIndex ~= nil then
recordLinks[storeType][recordId][linkIndex] = nil
end
local remainingIndexCount = tableHelper.getCount(recordLinks[storeType][recordId])
if remainingIndexCount == 0 then
recordLinks[storeType][recordId] = nil
recordStore:RemoveLinkToCell(recordId, self)
recordStore:QuicksaveToDrive()
end
end
end
end
function BaseCell:GetVisitorCount()
return tableHelper.getCount(self.visitors)
end
function BaseCell:AddVisitor(pid)
-- Only add new visitor if we don't already have them
if not tableHelper.containsValue(self.visitors, pid) then
table.insert(self.visitors, pid)
-- Also add a record to the player's list of loaded cells
Players[pid]:AddCellLoaded(self.description)
self:LoadGeneratedRecords(pid)
local shouldSendInfo = false
local lastVisitTimestamp = self.data.lastVisit[Players[pid].accountName]
-- If this player has never been in this cell, they should be
-- sent its cell data
if lastVisitTimestamp == nil then
shouldSendInfo = true
-- Otherwise, send them the cell data only if they haven't
-- visited since last connecting to the server
elseif Players[pid].initTimestamp > lastVisitTimestamp then
shouldSendInfo = true
end
if shouldSendInfo == true then
-- First, fix whatever quest problems exist in this cell
contentFixer.FixCell(pid, self.description)
self:LoadInitialCellData(pid)
end
self:LoadMomentaryCellData(pid)
end
end
function BaseCell:RemoveVisitor(pid)
-- Only remove visitor if they are actually recorded as one
if tableHelper.containsValue(self.visitors, pid) then
tableHelper.removeValue(self.visitors, pid)
-- Also remove the record from the player's list of loaded cells
Players[pid]:RemoveCellLoaded(self.description)
-- Remember when this visitor left
self:SaveLastVisit(Players[pid].accountName)
-- Were we waiting on a container request from this pid?
if self.isRequestingContainers == true and self.containerRequestPid == pid then
self.isRequestingContainers = false
self.containerRequestPid = nil
end
-- Were we waiting on an actorList request from this pid?
if self.isRequestingActorList == true and self.actorListRequestPid == pid then
self.isRequestingActorList = false
self.actorListRequestPid = nil
end
end
end
function BaseCell:GetAuthority()
return self.authority
end
function BaseCell:SetAuthority(pid)
self.authority = pid
tes3mp.LogMessage(enumerations.log.INFO, "Authority of cell " .. self.data.entry.description ..
" is now " .. logicHandler.GetChatName(pid))
self:LoadActorAuthority(pid)
end
-- Check whether an object is in this cell
function BaseCell:ContainsObject(uniqueIndex)
if self.data.objectData[uniqueIndex] ~= nil and self.data.objectData[uniqueIndex].refId ~= nil then
return true
end
return false
end
function BaseCell:HasContainerData()
if tableHelper.isEmpty(self.data.packets.container) == true then
return false
end
return true
end
function BaseCell:HasActorData()
if tableHelper.isEmpty(self.data.packets.actorList) == true then
return false
end
return true
end
function BaseCell:InitializeObjectData(uniqueIndex, refId)
if uniqueIndex ~= nil and refId ~= nil and self.data.objectData[uniqueIndex] == nil then
self.data.objectData[uniqueIndex] = {}
self.data.objectData[uniqueIndex].refId = refId
end
end
function BaseCell:DeleteObjectData(uniqueIndex)
if self.data.objectData[uniqueIndex] == nil then
return
end
-- Is this a player's summon? If so, remove it from the summons tracked
-- for the player
local summon = self.data.objectData[uniqueIndex].summon
if summon ~= nil then
if summon.summonerPlayer ~= nil and logicHandler.IsPlayerNameLoggedIn(summon.summonerPlayer) then
logicHandler.GetPlayerByName(summon.summonerPlayer).summons[uniqueIndex] = nil
end
end
-- Delete all packets associated with an object
for packetIndex, packetType in pairs(self.data.packets) do
tableHelper.removeValue(self.data.packets[packetIndex], uniqueIndex)
end
-- Delete all object data
self.data.objectData[uniqueIndex] = nil
end
function BaseCell:MoveObjectData(uniqueIndex, newCell)
-- Ensure we're not trying to move the object to the cell it's already in
if self.description == newCell.description then return end
-- Move all packets about this uniqueIndex from the old cell to the new cell
for packetIndex, packetType in pairs(self.data.packets) do
if tableHelper.containsValue(self.data.packets[packetIndex], uniqueIndex) then
table.insert(newCell.data.packets[packetIndex], uniqueIndex)
tableHelper.removeValue(self.data.packets[packetIndex], uniqueIndex)
end
end
newCell.data.objectData[uniqueIndex] = self.data.objectData[uniqueIndex]
self.data.objectData[uniqueIndex] = nil
end
function BaseCell:SaveLastVisit(playerName)
self.data.lastVisit[playerName] = os.time()
end
function BaseCell:SaveObjectsDeleted(objects)
local temporaryLoadedCells = {}
for uniqueIndex, object in pairs(objects) do
local refId = object.refId
-- Check whether this object was moved to this cell from another one
local wasMovedHere = tableHelper.containsValue(self.data.packets.cellChangeFrom, uniqueIndex)
if wasMovedHere == true then
local originalCellDescription = self.data.objectData[uniqueIndex].cellChangeFrom
-- If the new cell is not loaded, load it temporarily
if LoadedCells[originalCellDescription] == nil then
logicHandler.LoadCell(originalCellDescription)
table.insert(temporaryLoadedCells, originalCellDescription)
end
local originalCell = LoadedCells[originalCellDescription]
originalCell:DeleteObjectData(uniqueIndex)
table.insert(originalCell.data.packets.delete, uniqueIndex)
originalCell:InitializeObjectData(uniqueIndex, refId)
self:DeleteObjectData(uniqueIndex)
else
-- Check whether this is a placed or spawned object
local wasPlacedHere = tableHelper.containsValue(self.data.packets.place, uniqueIndex) or
tableHelper.containsValue(self.data.packets.spawn, uniqueIndex)
self:DeleteObjectData(uniqueIndex)
-- If this is an object from the game's data files, we should keep sending ObjectDelete
-- packets for it to visitors
if not wasPlacedHere then
table.insert(self.data.packets.delete, uniqueIndex)
self:InitializeObjectData(uniqueIndex, refId)
-- If this is an object based on a generated record, we need to remove the link to it
elseif logicHandler.IsGeneratedRecord(refId) then
local recordStore = logicHandler.GetRecordStoreByRecordId(refId)
if recordStore ~= nil then
self:RemoveLinkToRecord(recordStore.storeType, refId, uniqueIndex)
end
end
end
end
-- Go through every temporary loaded cell and unload it
for arrayIndex, originalCellDescription in pairs(temporaryLoadedCells) do
logicHandler.UnloadCell(originalCellDescription)
end
end
function BaseCell:SaveObjectsPlaced(objects)
for uniqueIndex, object in pairs(objects) do
local location = object.location
-- Ensure data integrity before proceeeding
if tableHelper.getCount(location) == 6 and tableHelper.usesNumericalValues(location) and
self:ContainsPosition(location.posX, location.posY) then
local refId = object.refId
self:InitializeObjectData(uniqueIndex, refId)
local count = object.count
local charge = object.charge
local enchantmentCharge = object.enchantmentCharge
local soul = object.soul
local goldValue = object.goldValue
-- Only save count if it isn't the default value of 1
if count ~= 1 then
self.data.objectData[uniqueIndex].count = count
end
-- Only save charge if it isn't the default value of -1
if charge ~= -1 then
self.data.objectData[uniqueIndex].charge = charge
end
-- Only save enchantment charge if it isn't the default value of -1
if enchantmentCharge ~= -1 then
self.data.objectData[uniqueIndex].enchantmentCharge = enchantmentCharge
end
if soul ~= "" then
self.data.objectData[uniqueIndex].soul = soul
end
-- Only save goldValue if it isn't the default value of 1
if goldValue ~= 1 then
self.data.objectData[uniqueIndex].goldValue = goldValue
end
self.data.objectData[uniqueIndex].location = location
tes3mp.LogAppend(enumerations.log.INFO, "- " .. uniqueIndex .. ", refId: " .. refId ..
", count: " .. count .. ", charge: " .. charge .. ", enchantmentCharge: " .. enchantmentCharge ..
", soul: " .. soul .. ", goldValue: " .. goldValue)
table.insert(self.data.packets.place, uniqueIndex)
if logicHandler.IsGeneratedRecord(refId) then
local recordStore = logicHandler.GetRecordStoreByRecordId(refId)
if recordStore ~= nil then
self:AddLinkToRecord(recordStore.storeType, refId, uniqueIndex)
end
end
end
end
end
function BaseCell:SaveObjectsSpawned(objects)
for uniqueIndex, object in pairs(objects) do
local location = object.location
-- Ensure data integrity before proceeeding
if tableHelper.getCount(location) == 6 and tableHelper.usesNumericalValues(location) and
self:ContainsPosition(location.posX, location.posY) then
local refId = object.refId
self:InitializeObjectData(uniqueIndex, refId)
tes3mp.LogAppend(enumerations.log.INFO, "- " .. uniqueIndex .. ", refId: " .. refId)
self.data.objectData[uniqueIndex].location = location
if object.summon ~= nil then
local summonDuration = object.summon.duration
if summonDuration > 0 then
local summon = {}
summon.duration = object.summon.duration
summon.effectId = object.summon.effectId
summon.spellId = object.summon.spellId
summon.startTime = object.summon.startTime
local hasPlayerSummoner = object.hasPlayerSummoner
if hasPlayerSummoner then
local summonerPid = object.summon.summonerPid
tes3mp.LogAppend(enumerations.log.INFO, "- summoned by player " ..
logicHandler.GetChatName(summonerPid))
-- Track the player and the summon for each other
summon.summonerPlayer = object.summon.summonerPlayer
Players[summonerPid].summons[uniqueIndex] = refId
else
summon.summonerUniqueIndex = object.summon.summonerUniqueIndex
summon.summonerRefId = object.summon.summonerRefId
tes3mp.LogAppend(enumerations.log.INFO, "- summoned by actor " .. summon.summonerUniqueIndex ..
", refId: " .. summon.summonerRefId)
end
self.data.objectData[uniqueIndex].summon = summon
end
end
table.insert(self.data.packets.spawn, uniqueIndex)
table.insert(self.data.packets.actorList, uniqueIndex)
if logicHandler.IsGeneratedRecord(refId) then
local recordStore = logicHandler.GetRecordStoreByRecordId(refId)
if recordStore ~= nil then
self:AddLinkToRecord(recordStore.storeType, refId, uniqueIndex)
end
end
end
end
end
function BaseCell:SaveObjectsLocked(objects)
for uniqueIndex, object in pairs(objects) do
local refId = object.refId
local lockLevel = object.lockLevel
self:InitializeObjectData(uniqueIndex, refId)
self.data.objectData[uniqueIndex].lockLevel = lockLevel
tes3mp.LogAppend(enumerations.log.INFO, "- " .. uniqueIndex .. ", refId: " .. refId ..
", lockLevel: " .. lockLevel)
tableHelper.insertValueIfMissing(self.data.packets.lock, uniqueIndex)
end
end
function BaseCell:SaveObjectTrapsTriggered(objects)
for uniqueIndex, object in pairs(objects) do
local refId = object.refId
self:InitializeObjectData(uniqueIndex, refId)
tes3mp.LogAppend(enumerations.log.INFO, "- " .. uniqueIndex .. ", refId: " .. refId)
tableHelper.insertValueIfMissing(self.data.packets.trap, uniqueIndex)
end
end
function BaseCell:SaveObjectsScaled(objects)
for uniqueIndex, object in pairs(objects) do
local refId = object.refId
local scale = object.scale
self:InitializeObjectData(uniqueIndex, refId)
self.data.objectData[uniqueIndex].scale = scale
tes3mp.LogAppend(enumerations.log.INFO, "- " .. uniqueIndex .. ", refId: " .. refId ..
", scale: " .. scale)
tableHelper.insertValueIfMissing(self.data.packets.scale, uniqueIndex)
end
end
function BaseCell:SaveObjectStates(objects)
for uniqueIndex, object in pairs(objects) do
local refId = object.refId
local state = object.state
self:InitializeObjectData(uniqueIndex, refId)
self.data.objectData[uniqueIndex].state = state
tes3mp.LogAppend(enumerations.log.INFO, "- " .. uniqueIndex .. ", refId: " .. refId ..
", state: " .. tostring(state))
tableHelper.insertValueIfMissing(self.data.packets.state, uniqueIndex)
end
end
function BaseCell:SaveDoorStates(objects)
for uniqueIndex, object in pairs(objects) do
local refId = object.refId
local doorState = object.doorState
self:InitializeObjectData(uniqueIndex, refId)
self.data.objectData[uniqueIndex].doorState = doorState
tableHelper.insertValueIfMissing(self.data.packets.doorState, uniqueIndex)
end
end
function BaseCell:SaveDoorDestinations(objects)
for uniqueIndex, object in pairs(objects) do
self:InitializeObjectData(uniqueIndex, object.refId)
self.data.objectData[uniqueIndex].doorDestination = object.doorDestination
tableHelper.insertValueIfMissing(self.data.packets.doorDestination, uniqueIndex)
end
end
function BaseCell:SaveContainers(pid)
tes3mp.ReadReceivedObjectList()
tes3mp.CopyReceivedObjectListToStore()
tes3mp.LogMessage(enumerations.log.INFO, "Saving Container from " .. logicHandler.GetChatName(pid) ..
" about " .. self.description)
local packetOrigin = tes3mp.GetObjectListOrigin()
local action = tes3mp.GetObjectListAction()
local subAction = tes3mp.GetObjectListContainerSubAction()
for objectIndex = 0, tes3mp.GetObjectListSize() - 1 do
local uniqueIndex = tes3mp.GetObjectRefNum(objectIndex) .. "-" .. tes3mp.GetObjectMpNum(objectIndex)
local refId = tes3mp.GetObjectRefId(objectIndex)
-- Deny players using "take all" on actors while sneaking
if tes3mp.GetSneakState(pid) and subAction == enumerations.containerSub.TAKE_ALL then
if tableHelper.containsValue(self.data.packets.actorList, uniqueIndex) then
return
end
end
tes3mp.LogAppend(enumerations.log.INFO, "- " .. uniqueIndex .. ", refId: " .. refId)
self:InitializeObjectData(uniqueIndex, refId)
tableHelper.insertValueIfMissing(self.data.packets.container, uniqueIndex)
local inventory = self.data.objectData[uniqueIndex].inventory
-- If this object's inventory is nil, or if the action is SET,
-- change the inventory to an empty table
if inventory == nil or action == enumerations.container.SET then
inventory = {}
end
for itemIndex = 0, tes3mp.GetContainerChangesSize(objectIndex) - 1 do
local itemRefId = tes3mp.GetContainerItemRefId(objectIndex, itemIndex)
local itemCount = tes3mp.GetContainerItemCount(objectIndex, itemIndex)
local itemCharge = tes3mp.GetContainerItemCharge(objectIndex, itemIndex)
local itemEnchantmentCharge = tes3mp.GetContainerItemEnchantmentCharge(objectIndex, itemIndex)
local itemSoul = tes3mp.GetContainerItemSoul(objectIndex, itemIndex)
local actionCount = tes3mp.GetContainerItemActionCount(objectIndex, itemIndex)
-- Check if the object's stored inventory contains this item already
if inventoryHelper.containsItem(inventory, itemRefId, itemCharge, itemEnchantmentCharge, itemSoul) then
local foundIndex = inventoryHelper.getItemIndex(inventory, itemRefId, itemCharge,
itemEnchantmentCharge, itemSoul)
local item = inventory[foundIndex]
if action == enumerations.container.ADD then
tes3mp.LogAppend(enumerations.log.VERBOSE, "- Adding count of " .. itemCount .. " to existing item " ..
item.refId .. " with current count of " .. item.count)
item.count = item.count + itemCount
elseif action == enumerations.container.REMOVE then
local newCount = item.count - actionCount
-- The item will still exist in the container with a lower count
if newCount > 0 then
tes3mp.LogAppend(enumerations.log.VERBOSE, "- Removed count of " .. actionCount .. " from item " ..
item.refId .. " that had count of " .. item.count .. ", resulting in remaining count of " .. newCount)
item.count = newCount
-- The item is to be completely removed
elseif newCount == 0 then
inventory[foundIndex] = nil
else
actionCount = item.count
tes3mp.LogAppend(enumerations.log.WARN, "- Attempt to remove count of " .. actionCount ..
" from item" .. item.refId .. " that only had count of " .. item.count)
tes3mp.LogAppend(enumerations.log.WARN, "- Removed just " .. actionCount .. " instead")
tes3mp.SetContainerItemActionCountByIndex(objectIndex, itemIndex, actionCount)
inventory[foundIndex] = nil
end
-- Is this a generated record? If so, remove the link to it
if inventory[foundIndex] == nil and logicHandler.IsGeneratedRecord(itemRefId) then
local recordStore = logicHandler.GetRecordStoreByRecordId(itemRefId)
if recordStore ~= nil then
self:RemoveLinkToRecord(recordStore.storeType, itemRefId, uniqueIndex)
end
end
end
else
if action == enumerations.container.REMOVE then
tes3mp.LogAppend(enumerations.log.WARN, "- Attempt to remove count of " .. actionCount ..
" from non-existent item " .. itemRefId)
tes3mp.SetContainerItemActionCountByIndex(objectIndex, itemIndex, 0)
else
tes3mp.LogAppend(enumerations.log.VERBOSE, "- Added new item " .. itemRefId .. " with count of " ..
itemCount)
inventoryHelper.addItem(inventory, itemRefId, itemCount,
itemCharge, itemEnchantmentCharge, itemSoul)
-- Is this a generated record? If so, add a link to it
if logicHandler.IsGeneratedRecord(itemRefId) then
local recordStore = logicHandler.GetRecordStoreByRecordId(itemRefId)
if recordStore ~= nil then
self:AddLinkToRecord(recordStore.storeType, itemRefId, uniqueIndex)
end
end
end
end
end
tableHelper.cleanNils(inventory)
self.data.objectData[uniqueIndex].inventory = inventory
end
-- Is this a player replying to our request for container contents?
-- If so, only send the reply to other players
-- i.e. sendToOtherPlayers is true and skipAttachedPlayer is true
if subAction == enumerations.containerSub.REPLY_TO_REQUEST then
tes3mp.SendContainer(true, true)
-- Is this a container packet originating from a client script or
-- dialogue? If so, its effects have already taken place on the
-- sending client, so only send it to other players
elseif packetOrigin == enumerations.packetOrigin.CLIENT_SCRIPT_LOCAL or
packetOrigin == enumerations.packetOrigin.CLIENT_SCRIPT_GLOBAL or
packetOrigin == enumerations.packetOrigin.CLIENT_DIALOGUE then
tes3mp.SendContainer(true, true)
-- Otherwise, send the received packet to everyone, including the
-- player who sent it (because no clientside changes will be made
-- to the related container otherwise)
-- i.e. sendToOtherPlayers is true and skipAttachedPlayer is false
else
tes3mp.SendContainer(true, false)
end
if action == enumerations.container.SET then
self.isRequestingContainers = false
end
end
function BaseCell:SaveActorsByPacketType(packetType, actors)
if packetType == "list" then
self:SaveActorList(actors)
elseif packetType == "equipment" then
self:SaveActorEquipment(actors)
elseif packetType == "death" then
self:SaveActorDeath(actors)
elseif packetType == "cellchange" then
self:SaveActorCellChanges(actors)
end
end
function BaseCell:SaveObjectsByPacketType(packetType, objects)
if packetType == "place" then
self:SaveObjectsPlaced(objects)
elseif packetType == "spawn" then
self:SaveObjectsSpawned(objects)
elseif packetType == "delete" then
self:SaveObjectsDeleted(objects)
elseif packetType == "lock" then
self:SaveObjectsLocked(objects)
elseif packetType == "trap" then
self:SaveObjectTrapsTriggered(objects)
elseif packetType == "scale" then
self:SaveObjectsScaled(objects)
elseif packetType == "state" then
self:SaveObjectStates(objects)
elseif packetType == "doorState" then
self:SaveDoorStates(objects)
end
end
function BaseCell:SaveActorList(actors)
for uniqueIndex, actor in pairs(actors) do
self:InitializeObjectData(uniqueIndex, actor.refId)
tes3mp.LogAppend(enumerations.log.INFO, "- " .. uniqueIndex .. ", refId: " .. actor.refId)
tableHelper.insertValueIfMissing(self.data.packets.actorList, uniqueIndex)
end
self.isRequestingActorList = false
end
function BaseCell:SaveActorPositions()
if tableHelper.getCount(self.visitors) == 0 then
return
end
tes3mp.ReadCellActorList(self.description)
local actorListSize = tes3mp.GetActorListSize()
if actorListSize == 0 then
return
end
for objectIndex = 0, actorListSize - 1 do
local uniqueIndex = tes3mp.GetActorRefNum(objectIndex) .. "-" .. tes3mp.GetActorMpNum(objectIndex)
if tes3mp.DoesActorHavePosition(objectIndex) == true and self:ContainsObject(uniqueIndex) then
self.data.objectData[uniqueIndex].location = {
posX = tes3mp.GetActorPosX(objectIndex),
posY = tes3mp.GetActorPosY(objectIndex),
posZ = tes3mp.GetActorPosZ(objectIndex),
rotX = tes3mp.GetActorRotX(objectIndex),
rotY = tes3mp.GetActorRotY(objectIndex),
rotZ = tes3mp.GetActorRotZ(objectIndex)
}
tableHelper.insertValueIfMissing(self.data.packets.position, uniqueIndex)
end
end
end
function BaseCell:SaveActorStatsDynamic()
if tableHelper.getCount(self.visitors) == 0 then
return
end
tes3mp.ReadCellActorList(self.description)
local actorListSize = tes3mp.GetActorListSize()
if actorListSize == 0 then
return
end
for objectIndex = 0, actorListSize - 1 do
local uniqueIndex = tes3mp.GetActorRefNum(objectIndex) .. "-" .. tes3mp.GetActorMpNum(objectIndex)
if tes3mp.DoesActorHaveStatsDynamic(objectIndex) == true and self:ContainsObject(uniqueIndex) then
self.data.objectData[uniqueIndex].stats = {
healthBase = tes3mp.GetActorHealthBase(objectIndex),
healthCurrent = tes3mp.GetActorHealthCurrent(objectIndex),
healthModified = tes3mp.GetActorHealthModified(objectIndex),
magickaBase = tes3mp.GetActorMagickaBase(objectIndex),
magickaCurrent = tes3mp.GetActorMagickaCurrent(objectIndex),
magickaModified = tes3mp.GetActorMagickaModified(objectIndex),
fatigueBase = tes3mp.GetActorFatigueBase(objectIndex),
fatigueCurrent = tes3mp.GetActorFatigueCurrent(objectIndex),
fatigueModified = tes3mp.GetActorFatigueModified(objectIndex)
}
tableHelper.insertValueIfMissing(self.data.packets.statsDynamic, uniqueIndex)
end
end
end
function BaseCell:SaveActorEquipment(actors)
for uniqueIndex, actor in pairs(actors) do
tes3mp.LogAppend(enumerations.log.INFO, "- " .. uniqueIndex)
if self:ContainsObject(uniqueIndex) then
self.data.objectData[uniqueIndex].equipment = {}
for equipmentIndex, item in pairs(actor.equipment) do
self.data.objectData[uniqueIndex].equipment[equipmentIndex] = item
end
tableHelper.insertValueIfMissing(self.data.packets.equipment, uniqueIndex)
end
end
end
function BaseCell:SaveActorDeath(actors)
if self.data.packets.death == nil then
self.data.packets.death = {}
end
for uniqueIndex, actor in pairs(actors) do
if self:ContainsObject(uniqueIndex) then
self.data.objectData[uniqueIndex].deathState = actor.deathState
if actor.killerPid ~= nil then
self.data.objectData[uniqueIndex].killer = {
player = Players[actor.killerPid].accountName
}
elseif actor.killerName ~= "" then
self.data.objectData[uniqueIndex].killer = {
refId = actor.killerRefId,
uniqueIndex = actor.killerUniqueIndex
}
end
tableHelper.insertValueIfMissing(self.data.packets.death, uniqueIndex)
end
end
end
function BaseCell:SaveActorCellChanges(actors)
local temporaryLoadedCells = {}
for actorIndex, actor in pairs(actors) do
local uniqueIndex = actor.uniqueIndex
local newCellDescription = actor.cellDescription
if newCellDescription == self.description then
tes3mp.LogAppend(enumerations.log.INFO, "- Ignored invalid cell change that was moving " .. uniqueIndex .. " to " ..
self.description .. " despite that actor already being in that cell")
else
tes3mp.LogAppend(enumerations.log.INFO, "- " .. uniqueIndex .. " moved to " .. newCellDescription)
-- If the new cell is not loaded, load it temporarily
if LoadedCells[newCellDescription] == nil then
logicHandler.LoadCell(newCellDescription)
table.insert(temporaryLoadedCells, newCellDescription)
end
local newCell = LoadedCells[newCellDescription]
-- Only proceed if this Actor is actually supposed to exist in this cell
if self.data.objectData[uniqueIndex] ~= nil then
-- Was this actor spawned in the old cell, instead of being a pre-existing actor?
-- If so, delete it entirely from the old cell and make it get spawned in the new cell
if tableHelper.containsValue(self.data.packets.spawn, uniqueIndex) == true then
tes3mp.LogAppend(enumerations.log.INFO, "-- As a server-only object, it was moved entirely")
-- If this object is based on a generated record, move its record link
-- to the new cell
local refId = self.data.objectData[uniqueIndex].refId
if logicHandler.IsGeneratedRecord(refId) then
local recordStore = logicHandler.GetRecordStoreByRecordId(refId)
if recordStore ~= nil then
newCell:AddLinkToRecord(recordStore.storeType, refId, uniqueIndex)
self:RemoveLinkToRecord(recordStore.storeType, refId, uniqueIndex)
end
-- Send this generated record to every visitor in the new cell
for _, visitorPid in pairs(newCell.visitors) do
recordStore:LoadGeneratedRecords(visitorPid, recordStore.data.generatedRecords, { refId })
end
end
-- This actor won't exist at all for players who have not loaded the actor's original
-- cell and were not online when it was first spawned, so send all of its details to them
for _, player in pairs(Players) do
if not tableHelper.containsValue(self.visitors, player.pid) then
self:LoadActorPackets(player.pid, self.data.objectData, { uniqueIndex })
end
end
self:MoveObjectData(uniqueIndex, newCell)
-- Was this actor moved to the old cell from another cell?
elseif tableHelper.containsValue(self.data.packets.cellChangeFrom, uniqueIndex) == true then
local originalCellDescription = self.data.objectData[uniqueIndex].cellChangeFrom
-- Is the new cell actually this actor's original cell?
-- If so, move its data back and remove all of its cell change data
if originalCellDescription == newCellDescription then
tes3mp.LogAppend(enumerations.log.INFO, "-- It is now back in its original cell " .. originalCellDescription)
self:MoveObjectData(uniqueIndex, newCell)
tableHelper.removeValue(newCell.data.packets.cellChangeTo, uniqueIndex)
tableHelper.removeValue(newCell.data.packets.cellChangeFrom, uniqueIndex)
newCell.data.objectData[uniqueIndex].cellChangeTo = nil
newCell.data.objectData[uniqueIndex].cellChangeFrom = nil
-- Otherwise, move its data to the new cell, delete it from the old cell, and update its
-- information in its original cell
else
self:MoveObjectData(uniqueIndex, newCell)
-- If the original cell is not loaded, load it temporarily
if LoadedCells[originalCellDescription] == nil then
logicHandler.LoadCell(originalCellDescription)
table.insert(temporaryLoadedCells, originalCellDescription)
end
local originalCell = LoadedCells[originalCellDescription]
if originalCell.data.objectData[uniqueIndex] ~= nil then
tes3mp.LogAppend(enumerations.log.INFO, "-- This is now referenced in its original cell " ..
originalCellDescription)
originalCell.data.objectData[uniqueIndex].cellChangeTo = newCellDescription
else
tes3mp.LogAppend(enumerations.log.ERROR, "-- It does not exist in its original cell " ..
originalCellDescription .. "! Please report this to a developer")
end
end
-- Otherwise, simply move this actor's data to the new cell and mark it as being moved there
-- in its old cell, as long as it's not supposed to already be in the new cell
elseif self.data.objectData[uniqueIndex].cellChangeTo ~= newCellDescription then
tes3mp.LogAppend(enumerations.log.INFO, "-- This was its first move away from its original cell")
self:MoveObjectData(uniqueIndex, newCell)
table.insert(self.data.packets.cellChangeTo, uniqueIndex)
if self.data.objectData[uniqueIndex] == nil then
self.data.objectData[uniqueIndex] = {}
end
self.data.objectData[uniqueIndex].cellChangeTo = newCellDescription
table.insert(newCell.data.packets.cellChangeFrom, uniqueIndex)
newCell.data.objectData[uniqueIndex].cellChangeFrom = self.description
end