-
Notifications
You must be signed in to change notification settings - Fork 3
/
ParkVehicle.lua
271 lines (241 loc) · 8.71 KB
/
ParkVehicle.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
-- ParkVehicle
--
-- @author Grisu118 - VertexDezign.net
-- @history v1.0.0.0 - 2017-09-15 - Initial implementation
-- v1.0.1.0 - 2017-10-15 - Fix random toggle
-- v2.0.0.0 - 2018-12-03 - FS19
-- v2.1.0.0 - 2019-04-01 - Support all enterable vehicles, create modSettings folder
-- v3.0.0.0 - 2021-11-19 - FS22
-- v3.1.0.0 - 2022-04-23 - Add possibiltiy to unpark all, fix issue with registration in loader
-- @Descripion: Allows temporary disabling of the tab function
-- @web: https://grisu118.ch or https://vertexdezign.net
-- Copyright (C) Grisu118, All Rights Reserved.
---@class ParkVehicle
ParkVehicle = {}
ParkVehicle.inputName = "parkVehicle"
ParkVehicle.modDir = g_parkVehicleSystem.modDir
function ParkVehicle.prerequisitesPresent(specializations)
return SpecializationUtil.hasSpecialization(Enterable, specializations)
end
function ParkVehicle.registerFunctions(vehicleType)
SpecializationUtil.registerFunction(vehicleType, "getParkVehicleState", ParkVehicle.getParkVehicleState)
SpecializationUtil.registerFunction(vehicleType, "setParkVehicleState", ParkVehicle.setParkVehicleState)
end
function ParkVehicle.registerEventListeners(vehicleType)
SpecializationUtil.registerEventListener(vehicleType, "onLoad", ParkVehicle)
SpecializationUtil.registerEventListener(vehicleType, "onUpdate", ParkVehicle)
SpecializationUtil.registerEventListener(vehicleType, "onDraw", ParkVehicle)
SpecializationUtil.registerEventListener(vehicleType, "onWriteStream", ParkVehicle)
SpecializationUtil.registerEventListener(vehicleType, "onReadStream", ParkVehicle)
SpecializationUtil.registerEventListener(vehicleType, "onWriteUpdateStream", ParkVehicle)
SpecializationUtil.registerEventListener(vehicleType, "onReadUpdateStream", ParkVehicle)
SpecializationUtil.registerEventListener(vehicleType, "onRegisterActionEvents", ParkVehicle)
SpecializationUtil.registerEventListener(vehicleType, "onDelete", ParkVehicle)
end
function ParkVehicle:onLoad(savegame)
self.spec_parkvehicle = {}
local spec = self.spec_parkvehicle
if g_dedicatedServerInfo == nil then
local modSettingsDir = getUserProfileAppPath() .. "modSettings"
local xmlFile = modSettingsDir .. "/parkVehicle.xml"
local id = nil
if not fileExists(xmlFile) then
createFolder(modSettingsDir)
local xml = createXMLFile("ParkVehicle", xmlFile, "ParkVehicle")
id = ParkVehicle.randomString(25)
setXMLString(xml, "ParkVehicle#uniqueUserId", id)
saveXMLFile(xml)
delete(xml)
else
local xml = loadXMLFile("ParkVehicle", xmlFile)
id = getXMLString(xml, "ParkVehicle#uniqueUserId")
delete(xml)
end
spec.uniqueUserId = id
else
spec.uniqueUserId = "dedi"
end
spec.inputPressed = false
spec.registrationKey = nil
spec.actionEvents = {}
spec.icon = createImageOverlay(ParkVehicle.modDir .. "icon.png")
spec.overlay = createImageOverlay(ParkVehicle.modDir .. "overlay.png")
spec.dirtyFlag = self:getNextDirtyFlag()
spec.state = {}
local isEmpty = true
if savegame ~= nil then
local i = 0
while true do
local legacykey = string.format("%s.ParkVehicle.player(%d)", savegame.key, i) -- TODO Remove with next release
local key = string.format("%s.%s.ParkVehicle.player(%d)", savegame.key, g_parkVehicleSystem.modName, i)
if not hasXMLProperty(savegame.xmlFile.handle, key) then
key = legacykey
end
if not hasXMLProperty(savegame.xmlFile.handle, key) then
break
end
local id = getXMLString(savegame.xmlFile.handle, key .. "#id")
local value = getXMLBool(savegame.xmlFile.handle, key .. "#isParked")
if id ~= nil and value ~= nil then
spec.state[id] = value
isEmpty = false
end
i = i + 1
end
end
if isEmpty or spec.state[spec.uniqueUserId] == nil then
spec.state[spec.uniqueUserId] = false
end
self.spec_enterable:setIsTabbable(not spec.state[spec.uniqueUserId])
spec.registrationKey = g_parkVehicleSystem:registerInstance(self)
end
function ParkVehicle:onUpdate(dt, isActiveForInput, isSelected)
if self.isClient then
local spec = self.spec_parkvehicle
if spec.inputPressed then
local newValue = not self:getParkVehicleState()
self:setParkVehicleState(newValue)
spec.inputPressed = false
end
end
end
---@param newValue boolean
function ParkVehicle:setParkVehicleState(newValue)
local spec = self.spec_parkvehicle
self.spec_enterable:setIsTabbable(not newValue)
spec.state[spec.uniqueUserId] = newValue
self:raiseDirtyFlags(spec.dirtyFlag)
end
---@return boolean
function ParkVehicle:getParkVehicleState()
local spec = self.spec_parkvehicle
return spec.state[spec.uniqueUserId]
end
function ParkVehicle:onDraw()
if self.isClient and self:getIsActive() then
local spec = self.spec_parkvehicle
local uiScale = g_gameSettings:getValue("uiScale")
local startX = 1 - 0.0755 * uiScale + (0.04 * (uiScale - 0.5))
local startY = 0.05 * uiScale - (0.08 * (uiScale - 0.5))
local iconWidth = 0.01 * uiScale
local iconHeight = iconWidth * g_screenAspectRatio
renderOverlay(spec.icon, startX, startY, iconWidth, iconHeight)
if spec.state[spec.uniqueUserId] then
renderOverlay(spec.overlay, startX, startY, iconWidth, iconHeight)
end
end
end
function ParkVehicle:onDelete()
local spec = self.spec_parkvehicle
g_parkVehicleSystem:unregisterInstance(spec.registrationKey)
end
--Called on server side on join
-- @param integer streamId streamId
-- @param integer connection connection
function ParkVehicle:onWriteStream(streamId, connection)
local spec = self.spec_parkvehicle
local count = 0
for k in pairs(spec.state) do
count = count + 1
end
streamWriteInt32(streamId, count)
for k, v in pairs(spec.state) do
streamWriteString(streamId, k)
streamWriteBool(streamId, v)
end
end
--Called on client side on join
-- @param integer streamId streamId
-- @param integer connection connection
function ParkVehicle:onReadStream(streamId, connection)
local spec = self.spec_parkvehicle
local state = {}
local count = streamReadInt32(streamId)
local i = 0
while i < count do
local id = streamReadString(streamId)
local value = streamReadBool(streamId)
state[id] = value
if id == spec.uniqueUserId then
self.spec_enterable:setIsTabbable(not value)
end
i = i + 1
end
spec.state = state
end
function ParkVehicle:onWriteUpdateStream(streamId, connection, dirtyMask)
if connection:getIsServer() then
local spec = self.spec_parkvehicle
if streamWriteBool(streamId, bitAND(dirtyMask, spec.dirtyFlag) ~= 0) then
streamWriteString(streamId, spec.uniqueUserId)
streamWriteBool(streamId, spec.state[spec.uniqueUserId])
end
end
end
function ParkVehicle:onReadUpdateStream(streamId, timestamp, connection)
if not connection:getIsServer() then
local spec = self.spec_parkvehicle
if streamReadBool(streamId) then
local id = streamReadString(streamId)
local value = streamReadBool(streamId)
if id == spec.uniqueUserId then
self.spec_enterable:setIsTabbable(not value)
end
spec.state[id] = value
end
end
end
function ParkVehicle:onRegisterActionEvents(isActiveForInput)
if self.isClient then
local spec = self.spec_parkvehicle
self:clearActionEventsTable(spec.actionEvents)
if self:getIsActiveForInput(true) then
local _, actionEventId =
self:addActionEvent(
spec.actionEvents,
"PARKVEHICLE_01",
self,
ParkVehicle.actionEventParkVehicle,
false,
true,
false,
true,
nil
)
g_inputBinding:setActionEventTextPriority(actionEventId, GS_PRIO_VERY_LOW)
end
end
end
function ParkVehicle.actionEventParkVehicle(self, actionName, inputValue, callbackState, isAnalog)
local spec = self.spec_parkvehicle
spec.inputPressed = true
end
function ParkVehicle:saveToXMLFile(xmlFile, path)
local spec = self.spec_parkvehicle
local i = 0
for id, value in pairs(spec.state) do
setXMLString(xmlFile.handle, string.format("%s.player(%d)#id", path, i), id)
setXMLBool(xmlFile.handle, string.format("%s.player(%d)#isParked", path, i), value)
i = i + 1
end
end
function ParkVehicle.randomString(length)
local charset = {} -- [0-9a-zA-Z]
for c = 48, 57 do
table.insert(charset, string.char(c))
end
for c = 65, 90 do
table.insert(charset, string.char(c))
end
for c = 97, 122 do
table.insert(charset, string.char(c))
end
local function randomString(length)
if not length or length <= 0 then
return ""
end
math.randomseed(getDate("%d%m%y%H%M%S"))
return randomString(length - 1) .. charset[math.random(1, #charset)]
end
return randomString(length)
end