-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.lua
More file actions
231 lines (182 loc) · 6.11 KB
/
main.lua
File metadata and controls
231 lines (182 loc) · 6.11 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
local FootPrints = {}
FootPrints.scriptName = "FootPrints"
FootPrints.defaultConfig = {
interval = 4,
refId = "footprints_key",
baseId = "key_dwe_satchel00",
name = "footprint",
model = "",
limit = 30,
Z = 0
}
FootPrints.config = DataManager.loadConfiguration(FootPrints.scriptName, FootPrints.defaultConfig)
FootPrints.timers = {}
FootPrints.prints = {}
FootPrints.markToDelete = {}
function FootPrints.LogMessage(mes)
tes3mp.LogMessage(enumerations.log.INFO, "[FootPrints] "..mes)
end
function FootPrints.OnServerPostInit()
local recordStore = RecordStores["miscellaneous"]
local refId = FootPrints.config.refId
if recordStore.data.permanentRecords[refId] == nil then
recordStore.data.permanentRecords[refId] = {
baseId = FootPrints.config.baseId,
model = FootPrints.config.model,
name = FootPrints.config.name
}
recordStore:Save()
end
end
customEventHooks.registerHandler("OnServerPostInit", FootPrints.OnServerPostInit)
function FootPrints.deletePrint(cellDescription, uniqueIndex)
if LoadedCells[cellDescription] == nil then
if FootPrints.markToDelete[cellDescription] == nil then
FootPrints.markToDelete[cellDescription] = {}
end
table.insert(FootPrints.markToDelete[cellDescription], uniqueIndex)
return
end
if next(Players) ~= nil then
logicHandler.DeleteObjectForEveryone(cellDescription, uniqueIndex)
end
LoadedCells[cellDescription]:DeleteObjectData(uniqueIndex)
end
function FootPrints.addPrint(pid, print)
local playerPrints = FootPrints.prints[pid]
if playerPrints.count == FootPrints.config.limit then
--remove the oldest print if we reached the limit
local head = playerPrints.head
local second = head.next
local tail = head.prev
tail.next = second
second.prev = tail
playerPrints.head = second
FootPrints.deletePrint(head.value.cellDescription, head.value.uniqueIndex)
playerPrints.count = playerPrints.count - 1
end
local t = {
value = print
}
if playerPrints.count == 0 then
t.next = t
t.prev = t
playerPrints.head = t
else
local head = playerPrints.head
local tail = head.prev
t.next = head
t.prev = tail
head.prev = t
tail.next = t
end
playerPrints.count = playerPrints.count + 1
end
function FootPrintsPlayerTimer(pid)
local location = {
posX = tes3mp.GetPosX(pid),
posY = tes3mp.GetPosY(pid),
posZ = tes3mp.GetPosZ(pid) + FootPrints.config.Z,
rotX = 0,
rotY = 0,
rotZ = 0
}
local cellDescription = tes3mp.GetCell(pid)
local unload = false
if LoadedCells[cellDescription] == nil then
logicHandler.LoadCell(cellDescription)
unload = true
end
local uniqueIndex = logicHandler.CreateObjectAtLocation(
cellDescription,
location,
FootPrints.config.refId,
"place"
)
FootPrints.addPrint(pid, {
cellDescription = cellDescription,
uniqueIndex = uniqueIndex
})
tes3mp.RestartTimer(FootPrints.timers[pid], time.seconds(FootPrints.config.interval))
if unload then
logicHandler.UnloadCell(cellDescription)
end
end
function FootPrints.OnPlayerAuthentified(eventStatus, pid)
local timerId = tes3mp.CreateTimerEx(
"FootPrintsPlayerTimer",
time.seconds(FootPrints.config.interval),
"i",
pid
)
FootPrints.timers[pid] = timerId
FootPrints.prints[pid] = {
head = nil,
count = 0
}
tes3mp.StartTimer(timerId)
end
customEventHooks.registerHandler("OnPlayerAuthentified", FootPrints.OnPlayerAuthentified)
function FootPrints.clearPlayer(pid)
FootPrints.LogMessage("Clearing player "..pid)
if FootPrints.timers[pid] ~= nil then
tes3mp.StopTimer(FootPrints.timers[pid])
FootPrints.LogMessage("Stopped the timer")
FootPrints.timers[pid] = nil
end
if FootPrints.prints[pid] ~= nil then
FootPrints.LogMessage(string.format("Removing %d prints", FootPrints.prints[pid].count))
if FootPrints.prints[pid].count ~= 0 then
local t = FootPrints.prints[pid].head
for i = 1, FootPrints.prints[pid].count do
FootPrints.deletePrint(t.value.cellDescription, t.value.uniqueIndex)
t = t.next
end
FootPrints.prints[pid] = nil
end
end
end
function FootPrints.OnPlayerDisconnect(eventStatus, pid)
if eventStatus.validCustomHandlers then
FootPrints.clearPlayer(pid)
end
end
customEventHooks.registerHandler("OnPlayerDisconnect", FootPrints.OnPlayerDisconnect)
function FootPrints.clearCell(cellDescription)
if FootPrints.markToDelete[cellDescription] ~= nil then
FootPrints.LogMessage(string.format(
"Removing %d prints from %s",
#FootPrints.markToDelete[cellDescription],
cellDescription
))
for _, uniqueIndex in pairs(FootPrints.markToDelete[cellDescription]) do
FootPrints.deletePrint(cellDescription, uniqueIndex)
end
FootPrints.markToDelete[cellDescription] = nil
end
end
function FootPrints.OnCellLoad(eventStatus, pid, cellDescription)
if eventStatus.validCustomHandlers then
FootPrints.clearCell(cellDescription)
end
end
customEventHooks.registerHandler("OnCellLoad", FootPrints.OnCellLoad)
function FootPrints.OnServerExit(eventStatus)
for pid, player in pairs(Players) do
FootPrints.clearPlayer(pid)
end
FootPrints.LogMessage("Clearing cells")
for cellDescription, list in pairs(FootPrints.markToDelete) do
local unload = false
if LoadedCells[cellDescription] == nil then
logicHandler.LoadCell(cellDescription)
unload = true
end
FootPrints.clearCell(cellDescription)
if unload then
logicHandler.UnloadCell(cellDescription)
end
end
end
customEventHooks.registerHandler("OnServerExit", FootPrints.OnServerExit)
return FootPrints