LUADOC - Farming Simulator 22

Script v1_7_1_0

Engine v1_7_1_0

Foundation Reference

PlaceableHighPressureWasher

Description
Specialization for placeables
Functions

canBeSold

Description
Definition
canBeSold()
Code
310function PlaceableHighPressureWasher:canBeSold(superFunc)
311 local canBeSold, warning = superFunc(self)
312 if not canBeSold then
313 return canBeSold, warning
314 end
315
316 local spec = self.spec_highPressureWasher
317 if spec.currentPlayer ~= nil then
318 warning = g_i18n:getText("shop_messageReturnVehicleInUse")
319 return false, warning
320 end
321
322 return true, nil
323end

deactivateHighPressureWasher

Description
Definition
deactivateHighPressureWasher()
Code
285function PlaceableHighPressureWasher:deactivateHighPressureWasher()
286 local spec = self.spec_highPressureWasher
287 if self.isClient then
288 g_soundManager:playSample(spec.samples.switch)
289 spec.isTurningOff = true
290 spec.turnOffTime = g_currentMission.time + spec.turnOffDuration
291 end
292
293 spec.isTurnedOn = false
294 if spec.lanceNode ~= nil then
295 setVisibility(spec.lanceNode, true)
296 end
297
298 if spec.currentPlayer ~= nil then
299 if spec.currentPlayer:hasHandtoolEquipped() then
300 spec.currentPlayer.baseInformation.currentHandtool:removeDeleteListener(self, "onHighPressureWasherLanceDeleted")
301 spec.currentPlayer:unequipHandtool()
302 end
303 spec.currentPlayer:removeDeleteListener(self, "onHighPressureWasherPlayerDeleted")
304 spec.currentPlayer = nil
305 end
306end

getHighPressureWasherIsPlayerInRange

Description
Definition
getHighPressureWasherIsPlayerInRange()
Code
327function PlaceableHighPressureWasher:getHighPressureWasherIsPlayerInRange(actionRadius, player)
328 if self.rootNode ~= nil then
329 local distance = calcDistanceFrom(player.rootNode, self.rootNode)
330 return distance < actionRadius, player, distance
331 end
332
333 return false, nil, 0
334end

getHighPressureWasherLoad

Description
Definition
getHighPressureWasherLoad()
Code
202function PlaceableHighPressureWasher:getHighPressureWasherLoad()
203 local spec = self.spec_highPressureWasher
204 if spec.isTurningOff then
205 if g_currentMission.time < spec.turnOffTime then
206 return MathUtil.clamp((spec.turnOffTime - g_currentMission.time) / spec.turnOffDuration, 0, 1)
207 end
208
209 return 0
210 end
211
212 return 1
213end

onDelete

Description
Definition
onDelete()
Code
97function PlaceableHighPressureWasher:onDelete()
98 local spec = self.spec_highPressureWasher
99
100 if spec.isTurnedOn then
101 self:setIsHighPressureWasherTurnedOn(false, nil, false)
102 end
103
104 g_soundManager:deleteSamples(spec.samples)
105 g_currentMission.activatableObjectsSystem:removeActivatable(spec.activatable)
106end

onHighPressureWasherLanceDeleted

Description
Definition
onHighPressureWasherLanceDeleted()
Code
277function PlaceableHighPressureWasher:onHighPressureWasherLanceDeleted()
278 local spec = self.spec_highPressureWasher
279 spec.currentPlayer = nil
280 self:setIsHighPressureWasherTurnedOn(false, nil, nil)
281end

onHighPressureWasherLanceEquipped

Description
Definition
onHighPressureWasherLanceEquipped()
Code
263function PlaceableHighPressureWasher:onHighPressureWasherLanceEquipped(handTool)
264 handTool:addDeleteListener(self, "onHighPressureWasherLanceDeleted")
265end

onHighPressureWasherPlayerDeleted

Description
Definition
onHighPressureWasherPlayerDeleted()
Code
269function PlaceableHighPressureWasher:onHighPressureWasherPlayerDeleted()
270 local spec = self.spec_highPressureWasher
271 spec.currentPlayer = nil
272 self:setIsHighPressureWasherTurnedOn(false, nil, nil)
273end

onInfoTriggerEnter

Description
Definition
onInfoTriggerEnter()
Code
187function PlaceableHighPressureWasher:onInfoTriggerEnter()
188 local spec = self.spec_highPressureWasher
189 spec.isPlayerInTrigger = true
190 self:raiseActive()
191end

onInfoTriggerLeave

Description
Definition
onInfoTriggerLeave()
Code
195function PlaceableHighPressureWasher:onInfoTriggerLeave()
196 local spec = self.spec_highPressureWasher
197 spec.isPlayerInTrigger = false
198end

onLoad

Description
Called on loading
Definition
onLoad(table savegame)
Arguments
tablesavegamesavegame
Code
72function PlaceableHighPressureWasher:onLoad(savegame)
73 local spec = self.spec_highPressureWasher
74 local xmlFile = self.xmlFile
75
76 spec.lanceNode = xmlFile:getValue("placeable.highPressureWasher.lance#node", nil, self.components, self.i3dMappings)
77 spec.handtoolXML = Utils.getFilename(xmlFile:getValue("placeable.highPressureWasher.handtool#filename"), self.baseDirectory)
78 spec.playerInRangeDistance = xmlFile:getValue("placeable.highPressureWasher.playerInRangeDistance", 3)
79 spec.actionRadius = xmlFile:getValue("placeable.highPressureWasher.actionRadius#distance", 10)
80
81 if self.isClient then
82 spec.samples = {}
83 spec.samples.compressor = g_soundManager:loadSampleFromXML(xmlFile, "placeable.highPressureWasher.sounds", "compressor", self.baseDirectory, self.components, 0, AudioGroup.VEHICLE, self.i3dMappings, self)
84 spec.samples.switch = g_soundManager:loadSampleFromXML(xmlFile, "placeable.highPressureWasher.sounds", "switch", self.baseDirectory, self.components, 1, AudioGroup.VEHICLE, self.i3dMappings, self)
85 end
86
87 spec.isTurnedOn = false
88 spec.isTurningOff = false
89 spec.turnOffTime = 0
90 spec.turnOffDuration = 500
91 spec.numPlayersInTrigger = 0
92 spec.activatable = HighPressureWasherActivatable.new(self)
93end

onReadStream

Description
Called on client side on join
Definition
onReadStream(integer streamId, table connection)
Arguments
integerstreamIdstream ID
tableconnectionconnection
Code
112function PlaceableHighPressureWasher:onReadStream(streamId, connection)
113 local isTurnedOn = streamReadBool(streamId)
114 if isTurnedOn then
115 local player = NetworkUtil.readNodeObject(streamId)
116 if player ~= nil then
117 self:setIsHighPressureWasherTurnedOn(isTurnedOn, player, true)
118 end
119 end
120end

onUpdate

Description
Definition
onUpdate()
Code
136function PlaceableHighPressureWasher:onUpdate(dt)
137 local spec = self.spec_highPressureWasher
138 if spec.currentPlayer ~= nil then
139 local isPlayerInRange, _, distance = self:getHighPressureWasherIsPlayerInRange(spec.actionRadius, spec.currentPlayer)
140 if not isPlayerInRange then
141 local maxRadius = spec.actionRadius + 5
142 local distanceLeft = maxRadius - distance
143
144 if distanceLeft > 0 then
145 if spec.currentPlayer == g_currentMission.player then
146 g_currentMission:showBlinkingWarning(string.format(g_i18n:getText("warning_hpwRangeRestriction"), distanceLeft), 100)
147 end
148 else
149 if self.isServer then
150 self:setIsHighPressureWasherTurnedOn(false, nil, false)
151 end
152 end
153 end
154 end
155
156 if self.isClient then
157 if spec.isTurningOff then
158 if g_currentMission.time > spec.turnOffTime then
159 spec.isTurningOff = false
160 g_soundManager:stopSample(spec.samples.compressor)
161 end
162 end
163 end
164
165 if spec.currentPlayer ~= nil or spec.isTurningOff or spec.isPlayerInTrigger then
166 self:raiseActive()
167 end
168end

onUpdateTick

Description
Definition
onUpdateTick()
Code
172function PlaceableHighPressureWasher:onUpdateTick(dt)
173 local spec = self.spec_highPressureWasher
174
175 if g_currentMission.accessHandler:canPlayerAccess(self, g_currentMission.player) then
176 local isPlayerInRange, _ = self:getHighPressureWasherIsPlayerInRange(spec.playerInRangeDistance, g_currentMission.player)
177 if isPlayerInRange then
178 g_currentMission.activatableObjectsSystem:addActivatable(spec.activatable)
179 else
180 g_currentMission.activatableObjectsSystem:removeActivatable(spec.activatable)
181 end
182 end
183end

onWriteStream

Description
Called on server side on join
Definition
onWriteStream(integer streamId, table connection)
Arguments
integerstreamIdstream ID
tableconnectionconnection
Code
126function PlaceableHighPressureWasher:onWriteStream(streamId, connection)
127 local spec = self.spec_highPressureWasher
128 streamWriteBool(streamId, spec.isTurnedOn)
129 if spec.isTurnedOn then
130 NetworkUtil.writeNodeObject(streamId, spec.currentPlayer)
131 end
132end

prerequisitesPresent

Description
Checks if all prerequisite specializations are loaded
Definition
prerequisitesPresent(table specializations)
Arguments
tablespecializationsspecializations
Return Values
booleanhasPrerequisitetrue if all prerequisite specializations are loaded
Code
19function PlaceableHighPressureWasher.prerequisitesPresent(specializations)
20 return true
21end

registerEventListeners

Description
Definition
registerEventListeners()
Code
43function PlaceableHighPressureWasher.registerEventListeners(placeableType)
44 SpecializationUtil.registerEventListener(placeableType, "onLoad", PlaceableHighPressureWasher)
45 SpecializationUtil.registerEventListener(placeableType, "onDelete", PlaceableHighPressureWasher)
46 SpecializationUtil.registerEventListener(placeableType, "onReadStream", PlaceableHighPressureWasher)
47 SpecializationUtil.registerEventListener(placeableType, "onWriteStream", PlaceableHighPressureWasher)
48 SpecializationUtil.registerEventListener(placeableType, "onUpdate", PlaceableHighPressureWasher)
49 SpecializationUtil.registerEventListener(placeableType, "onUpdateTick", PlaceableHighPressureWasher)
50 SpecializationUtil.registerEventListener(placeableType, "onInfoTriggerEnter", PlaceableHighPressureWasher)
51 SpecializationUtil.registerEventListener(placeableType, "onInfoTriggerLeave", PlaceableHighPressureWasher)
52end

registerFunctions

Description
Definition
registerFunctions()
Code
25function PlaceableHighPressureWasher.registerFunctions(placeableType)
26 SpecializationUtil.registerFunction(placeableType, "deactivateHighPressureWasher", PlaceableHighPressureWasher.deactivateHighPressureWasher)
27 SpecializationUtil.registerFunction(placeableType, "onHighPressureWasherLanceDeleted", PlaceableHighPressureWasher.onHighPressureWasherLanceDeleted)
28 SpecializationUtil.registerFunction(placeableType, "onHighPressureWasherLanceEquipped", PlaceableHighPressureWasher.onHighPressureWasherLanceEquipped)
29 SpecializationUtil.registerFunction(placeableType, "onHighPressureWasherPlayerDeleted", PlaceableHighPressureWasher.onHighPressureWasherPlayerDeleted)
30 SpecializationUtil.registerFunction(placeableType, "setIsHighPressureWasherTurnedOn", PlaceableHighPressureWasher.setIsHighPressureWasherTurnedOn)
31 SpecializationUtil.registerFunction(placeableType, "getHighPressureWasherLoad", PlaceableHighPressureWasher.getHighPressureWasherLoad)
32 SpecializationUtil.registerFunction(placeableType, "getHighPressureWasherIsPlayerInRange", PlaceableHighPressureWasher.getHighPressureWasherIsPlayerInRange)
33end

registerOverwrittenFunctions

Description
Definition
registerOverwrittenFunctions()
Code
37function PlaceableHighPressureWasher.registerOverwrittenFunctions(placeableType)
38 SpecializationUtil.registerOverwrittenFunction(placeableType, "canBeSold", PlaceableHighPressureWasher.canBeSold)
39end

registerXMLPaths

Description
Definition
registerXMLPaths()
Code
56function PlaceableHighPressureWasher.registerXMLPaths(schema, basePath)
57 schema:setXMLSpecializationType("highPressureWasher")
58 schema:register(XMLValueType.NODE_INDEX, basePath .. ".highPressureWasher.lance#node", "Lance node")
59 schema:register(XMLValueType.STRING, basePath .. ".highPressureWasher.handtool#filename", "Hand tool xml filename")
60 schema:register(XMLValueType.FLOAT, basePath .. ".highPressureWasher.playerInRangeDistance", "Player in range distance", 3)
61 schema:register(XMLValueType.FLOAT, basePath .. ".highPressureWasher.actionRadius#distance", "Action radius distance", 15)
62 SoundManager.registerSampleXMLPaths(schema, basePath .. ".highPressureWasher.sounds", "compressor")
63 SoundManager.registerSampleXMLPaths(schema, basePath .. ".highPressureWasher.sounds", "switch")
64 schema:register(XMLValueType.STRING, basePath .. ".highPressureWasher.exhaust#filename", "Exhaust effect i3d filename")
65 schema:register(XMLValueType.NODE_INDEX, basePath .. ".highPressureWasher.exhaust#index", "Exhaust effect link node")
66 schema:setXMLSpecializationType()
67end

setIsHighPressureWasherTurnedOn

Description
Definition
setIsHighPressureWasherTurnedOn()
Code
219function PlaceableHighPressureWasher:setIsHighPressureWasherTurnedOn(isTurnedOn, player, noEventSend)
220 local spec = self.spec_highPressureWasher
221 if spec.isTurnedOn ~= isTurnedOn then
222 if noEventSend == nil or noEventSend == false then
223 if g_server ~= nil then
224 g_server:broadcastEvent(PlaceableHighPressureWasherStateEvent.new(self, isTurnedOn, player), nil, nil, self)
225 else
226 g_client:getServerConnection():sendEvent(PlaceableHighPressureWasherStateEvent.new(self, isTurnedOn, player))
227 end
228 end
229
230 if isTurnedOn then
231 spec.isTurnedOn = isTurnedOn
232
233 if player ~= nil then
234 spec.currentPlayer = player
235 spec.currentPlayer:addDeleteListener(self, "onHighPressureWasherPlayerDeleted")
236 if noEventSend ~= true then
237 spec.currentPlayer:equipHandtool(spec.handtoolXML, true, noEventSend, self.onHighPressureWasherLanceEquipped, self)
238 end
239 end
240
241 if spec.isClient then
242 g_soundManager:playSample(spec.samples.switch)
243 g_soundManager:playSample(spec.samples.compressor)
244
245 if spec.isTurningOff then
246 spec.isTurningOff = false
247 end
248 setVisibility(spec.lanceNode, false)
249 end
250 else
251 self:deactivateHighPressureWasher()
252 end
253 if spec.exhaustNode ~= nil then
254 setVisibility(spec.exhaustNode, isTurnedOn)
255 end
256
257 self:raiseActive()
258 end
259end