LUADOC - Farming Simulator 19

Script v1.7.1.0

Engine v1.7.1.0

Foundation Reference

AttacherJointControl

Description
Specialization to steplessly control the height of an attacherJoint
Functions

actionEventAttacherJointControl

Description
Definition
actionEventAttacherJointControl()
Code
446function AttacherJointControl.actionEventAttacherJointControl(self, actionName, inputValue, callbackState, isAnalog)
447 if math.abs(inputValue) > 0 then
448 local spec = self.spec_attacherJointControl
449 local control = spec.nameToControl[actionName]
450
451 -- multiply by 0.025 to have to same speed as in fs17
452 local changedAlpha = inputValue * control.mouseSpeedFactor * 0.025
453 if control.invertAxis then
454 changedAlpha = -changedAlpha
455 end
456
457 self:controlAttacherJoint(control, control.moveAlpha + changedAlpha)
458 end
459end

actionEventAttacherJointControlSetPoint

Description
Definition
actionEventAttacherJointControlSetPoint()
Code
463function AttacherJointControl.actionEventAttacherJointControlSetPoint(self, actionName, inputValue, callbackState, isAnalog)
464 local spec = self.spec_attacherJointControl
465 if spec.jointDesc ~= nil then
466 if self:getControlAttacherJointDirection() then
467 spec.heightTargetAlpha = spec.jointDesc.lowerAlpha
468 else
469 spec.heightTargetAlpha = spec.jointDesc.upperAlpha
470 end
471 end
472end

controlAttacherJoint

Description
Updates attacher joint move alpha and update joint frame
Definition
controlAttacherJoint(table control, float moveAlpha)
Arguments
tablecontrolcontrol
floatmoveAlphamove alpha
Code
245function AttacherJointControl:controlAttacherJoint(control, moveAlpha)
246 local spec = self.spec_attacherJointControl
247 local jointDesc = spec.jointDesc
248
249 if jointDesc ~= nil then
250 moveAlpha = control.func(self, moveAlpha)
251
252 local attacherVehicle = self:getAttacherVehicle()
253 attacherVehicle:updateAttacherJointRotation(jointDesc, self)
254 if self.isServer and jointDesc.jointIndex ~= 0 then
255 setJointFrame(jointDesc.jointIndex, 0, jointDesc.jointTransform)
256 end
257 end
258
259 spec.lastMoveTime = g_time
260
261 control.moveAlpha = moveAlpha
262 self:raiseDirtyFlags(spec.dirtyFlag)
263end

controlAttacherJointHeight

Description
Updates attacher joint height by given mve alpha value
Definition
controlAttacherJointHeight(float moveAlpha)
Arguments
floatmoveAlphamove alpha
Return Values
floatmoveAlphalimited move alpha
Code
269function AttacherJointControl:controlAttacherJointHeight(moveAlpha)
270 local spec = self.spec_attacherJointControl
271 local jointDesc = spec.jointDesc
272
273 if moveAlpha == nil then
274 moveAlpha = jointDesc.moveAlpha
275 end
276
277 moveAlpha = MathUtil.clamp(moveAlpha, jointDesc.upperAlpha, jointDesc.lowerAlpha)
278 if jointDesc.rotationNode ~= nil then
279 setRotation(jointDesc.rotationNode, MathUtil.vector3ArrayLerp(jointDesc.upperRotation, jointDesc.lowerRotation, moveAlpha))
280 end
281 if jointDesc.rotationNode2 ~= nil then
282 setRotation(jointDesc.rotationNode2, MathUtil.vector3ArrayLerp(jointDesc.upperRotation2, jointDesc.lowerRotation2, moveAlpha))
283 end
284
285 spec.lastHeightAlpha = moveAlpha
286
287 return moveAlpha
288end

controlAttacherJointTilt

Description
Updates attacher joint tilt by given mve alpha value
Definition
controlAttacherJointTilt(float moveAlpha)
Arguments
floatmoveAlphamove alpha
Return Values
floatmoveAlphalimited move alpha
Code
294function AttacherJointControl:controlAttacherJointTilt(moveAlpha)
295 local spec = self.spec_attacherJointControl
296
297 if moveAlpha == nil then
298 moveAlpha = 0.5
299 end
300
301 moveAlpha = MathUtil.clamp(moveAlpha, 0, 1)
302 local angle = spec.maxTiltAngle * -(moveAlpha - 0.5)
303
304 spec.jointDesc.upperRotationOffset = spec.jointDesc.upperRotationOffsetBackup + angle
305 spec.jointDesc.lowerRotationOffset = spec.jointDesc.lowerRotationOffsetBackup + angle
306
307 return moveAlpha
308end

getCanBeSelected

Description
Definition
getCanBeSelected()
Code
376function AttacherJointControl:getCanBeSelected(superFunc)
377 return true
378end

getControlAttacherJointDirection

Description
Returns direction boolean for lifting and lowering the implement by button press
Definition
getControlAttacherJointDirection()
Return Values
booleandirectiondirection
Code
313function AttacherJointControl:getControlAttacherJointDirection()
314 local spec = self.spec_attacherJointControl
315 if spec.heightTargetAlpha ~= -1 then
316 return spec.heightTargetAlpha == spec.jointDesc.upperAlpha
317 end
318
319 local lastAlpha = spec.heightController.moveAlpha
320 return math.abs(lastAlpha - spec.jointDesc.lowerAlpha) > math.abs(lastAlpha - spec.jointDesc.upperAlpha)
321end

getLoweringActionEventState

Description
Definition
getLoweringActionEventState()
Code
354function AttacherJointControl:getLoweringActionEventState(superFunc)
355 local spec = self.spec_attacherJointControl
356 if spec.heightController then
357 local showText = spec.jointDesc ~= nil
358
359 local text
360 if showText then
361 if self:getControlAttacherJointDirection() then
362 text = string.format(g_i18n:getText("action_lowerOBJECT"), self.typeDesc)
363 else
364 text = string.format(g_i18n:getText("action_liftOBJECT"), self.typeDesc)
365 end
366 end
367
368 return showText, text
369 end
370
371 return superFunc(self)
372end

loadInputAttacherJoint

Description
Called on loading
Definition
loadInputAttacherJoint(table savegame)
Arguments
tablesavegamesavegame
Code
326function AttacherJointControl:loadInputAttacherJoint(superFunc, xmlFile, key, inputAttacherJoint, i)
327 if not superFunc(self, xmlFile, key, inputAttacherJoint, i) then
328 return false
329 end
330
331 inputAttacherJoint.isControllable = Utils.getNoNil(getXMLBool(xmlFile, key .. "#isControllable"), false)
332
333 return true
334end

onDelete

Description
Definition
onDelete()
Code
131function AttacherJointControl:onDelete()
132 if self.isClient then
133 local spec = self.spec_attacherJointControl
134 g_soundManager:deleteSample(spec.samples.hydraulic)
135 end
136end

onLoad

Description
Called on loading
Definition
onLoad(table savegame)
Arguments
tablesavegamesavegame
Code
57function AttacherJointControl:onLoad(savegame)
58 local spec = self.spec_attacherJointControl
59
60 XMLUtil.checkDeprecatedXMLElements(self.xmlFile, self.configFileName, "vehicle.attacherJointControl.control1", "vehicle.attacherJointControl.control with #controlFunction 'controlAttacherJointHeight'") -- FS17
61 XMLUtil.checkDeprecatedXMLElements(self.xmlFile, self.configFileName, "vehicle.attacherJointControl.control2", "vehicle.attacherJointControl.control with #controlFunction 'controlAttacherJointTilt'") -- FS17
62
63 local baseKey = "vehicle.attacherJointControl"
64 spec.maxTiltAngle = Utils.getNoNilRad(getXMLFloat(self.xmlFile, baseKey .. "#maxTiltAngle"), math.rad(25))
65 spec.heightTargetAlpha = -1
66
67 spec.controls = {}
68 spec.nameToControl = {}
69 local i = 0
70 while true do
71 local key = string.format("%s.control(%d)", baseKey, i)
72 if not hasXMLProperty(self.xmlFile, key) then
73 break
74 end
75
76 local control = {}
77
78 local controlFunc = getXMLString(self.xmlFile, key .. "#controlFunction")
79 if controlFunc ~= nil and self[controlFunc] ~= nil then
80 control.func = self[controlFunc]
81
82 if control.func == self.controlAttacherJointHeight then
83 spec.heightController = control
84 end
85 else
86 g_logManager:xmlWarning(self.configFileName, "Unknown control function '%s' for attacher joint control '%s'", tostring(controlFunc), key)
87 break
88 end
89
90 local actionBindingName = getXMLString(self.xmlFile, key .. "#controlAxis")
91 if actionBindingName ~= nil and InputAction[actionBindingName] ~= nil then
92 control.controlAction = InputAction[actionBindingName]
93 else
94 g_logManager:xmlWarning(self.configFileName, "Unknown control axis '%s' for attacher joint control '%s'", tostring(actionBindingName), key)
95 break
96 end
97
98 XMLUtil.checkDeprecatedXMLElements(self.xmlFile, self.configFileName, key.."#controlAxisIcon", key.."#iconName") --FS17 to FS19
99
100 local iconName = getXMLString(self.xmlFile, key .. "#iconName") or ""
101 if InputHelpElement.AXIS_ICON[iconName] == nil then
102 -- add the mod name as a prefix to match axis icon loading name collision avoidance
103 iconName = (self.customEnvironment or "") .. iconName
104 end
105
106 control.axisActionIcon = iconName
107
108 control.invertAxis = Utils.getNoNil( getXMLString(self.xmlFile, key .. "#invertControlAxis"), false )
109 control.mouseSpeedFactor = Utils.getNoNil( getXMLFloat(self.xmlFile, key .. "#mouseSpeedFactor"), 1.0 )
110 control.moveAlpha = 0
111
112 spec.nameToControl[actionBindingName] = control
113 table.insert(spec.controls, control)
114
115 i = i + 1
116 end
117
118 if self.isClient then
119 spec.lastMoveTime = 0
120
121 spec.samples = {}
122 spec.samples.hydraulic = g_soundManager:loadSampleFromXML(self.xmlFile, baseKey..".sounds", "hydraulic", self.baseDirectory, self.components, 0, AudioGroup.VEHICLE, self.i3dMappings, self)
123 end
124
125 spec.jointDesc = nil
126 spec.dirtyFlag = self:getNextDirtyFlag()
127end

onPostAttach

Description
Called if vehicle gets attached
Definition
onPostAttach(table attacherVehicle, integer inputJointDescIndex, integer jointDescIndex)
Arguments
tableattacherVehicleattacher vehicle
integerinputJointDescIndexindex of input attacher joint
integerjointDescIndexindex of attacher joint it gets attached to
Code
403function AttacherJointControl:onPostAttach(attacherVehicle, inputJointDescIndex, jointDescIndex)
404 local spec = self.spec_attacherJointControl
405
406 local inputAttacherJoints = self:getInputAttacherJoints()
407 if inputAttacherJoints[inputJointDescIndex] ~= nil and inputAttacherJoints[inputJointDescIndex].isControllable then
408 local attacherJoints = attacherVehicle:getAttacherJoints()
409 local jointDesc = attacherJoints[jointDescIndex]
410 jointDesc.allowsLoweringBackup = jointDesc.allowsLowering
411 jointDesc.allowsLowering = false
412 jointDesc.upperRotationOffsetBackup = jointDesc.upperRotationOffset
413 jointDesc.lowerRotationOffsetBackup = jointDesc.lowerRotationOffset
414
415 spec.jointDesc = jointDesc
416
417 -- reset control values
418 for _, control in ipairs(spec.controls) do
419 control.moveAlpha = control.func(self)
420 end
421
422 --lift after attach
423 spec.heightTargetAlpha = spec.jointDesc.upperAlpha
424
425 self:requestActionEventUpdate()
426 end
427end

onPreDetach

Description
Called if vehicle gets detached
Definition
onPreDetach(table attacherVehicle, table implement)
Arguments
tableattacherVehicleattacher vehicle
tableimplementimplement
Code
433function AttacherJointControl:onPreDetach(attacherVehicle, implement)
434 local spec = self.spec_attacherJointControl
435
436 if spec.jointDesc ~= nil then
437 spec.jointDesc.allowsLowering = spec.jointDesc.allowsLoweringBackup
438 spec.jointDesc.upperRotationOffset = spec.jointDesc.upperRotationOffsetBackup
439 spec.jointDesc.lowerRotationOffset = spec.jointDesc.lowerRotationOffsetBackup
440 spec.jointDesc = nil
441 end
442end

onReadStream

Description
Called on client side on join
Definition
onReadStream(integer streamId, integer connection)
Arguments
integerstreamIdstreamId
integerconnectionconnection
Code
142function AttacherJointControl:onReadStream(streamId, connection)
143 if connection:getIsServer() then
144 if streamReadBool(streamId) then
145 local spec = self.spec_attacherJointControl
146 for _, control in ipairs(spec.controls) do
147 local moveAlpha = streamReadFloat32(streamId)
148 self:controlAttacherJoint(control, moveAlpha)
149 end
150 end
151 end
152end

onReadUpdateStream

Description
Called on on update
Definition
onReadUpdateStream(integer streamId, integer timestamp, table connection)
Arguments
integerstreamIdstream ID
integertimestamptimestamp
tableconnectionconnection
Code
174function AttacherJointControl:onReadUpdateStream(streamId, timestamp, connection)
175 if not connection:getIsServer() then
176 if streamReadBool(streamId) then
177 local spec = self.spec_attacherJointControl
178 for _, control in ipairs(spec.controls) do
179 local moveAlpha = streamReadFloat32(streamId)
180 self:controlAttacherJoint(control, moveAlpha)
181 end
182 end
183 end
184end

onRegisterActionEvents

Description
Definition
onRegisterActionEvents()
Code
382function AttacherJointControl:onRegisterActionEvents(isActiveForInput, isActiveForInputIgnoreSelection)
383 if self.isClient then
384 local spec = self.spec_attacherJointControl
385 self:clearActionEventsTable(spec.actionEvents)
386
387 if isActiveForInput then
388 if spec.jointDesc ~= nil then
389 for _, control in ipairs(spec.controls) do
390 local _, actionEventId = self:addActionEvent(spec.actionEvents, control.controlAction, self, AttacherJointControl.actionEventAttacherJointControl, false, false, true, true, nil, control.axisActionIcon)
391 g_inputBinding:setActionEventTextPriority(actionEventId, GS_PRIO_NORMAL)
392 end
393 end
394 end
395 end
396end

onUpdate

Description
Called on update
Definition
onUpdate(float dt, boolean isActiveForInput, boolean isSelected)
Arguments
floatdttime since last call in ms
booleanisActiveForInputtrue if vehicle is active for input
booleanisSelectedtrue if vehicle is selected
Code
207function AttacherJointControl:onUpdate(dt, isActiveForInput, isActiveForInputIgnoreSelection, isSelected)
208 local spec = self.spec_attacherJointControl
209 local control = spec.heightController
210
211 --update attacher joint height by target alpha
212 if control ~= nil then
213 if spec.jointDesc ~= nil then
214 if spec.heightTargetAlpha ~= -1 then
215 local diff = spec.heightTargetAlpha - control.moveAlpha + 0.0001
216 local moveTime = diff / (spec.jointDesc.upperAlpha - spec.jointDesc.lowerAlpha) * spec.jointDesc.moveTime
217 local moveStep = dt / moveTime * diff
218 if diff > 0 then
219 moveStep = -moveStep
220 end
221 local newAlpha = control.moveAlpha + moveStep
222 self:controlAttacherJoint(control, newAlpha)
223 if math.abs(spec.heightTargetAlpha - newAlpha) < 0.01 then
224 spec.heightTargetAlpha = -1
225 end
226 end
227 end
228 end
229
230 if spec.lastMoveTime + 100 > g_time then
231 if not g_soundManager:getIsSamplePlaying(spec.samples.hydraulic) then
232 g_soundManager:playSample(spec.samples.hydraulic)
233 end
234 else
235 if g_soundManager:getIsSamplePlaying(spec.samples.hydraulic) then
236 g_soundManager:stopSample(spec.samples.hydraulic)
237 end
238 end
239end

onWriteStream

Description
Called on server side on join
Definition
onWriteStream(integer streamId, integer connection)
Arguments
integerstreamIdstreamId
integerconnectionconnection
Code
158function AttacherJointControl:onWriteStream(streamId, connection)
159 if not connection:getIsServer() then
160 local spec = self.spec_attacherJointControl
161 if streamWriteBool(streamId, spec.jointDesc ~= nil) then
162 for _, control in ipairs(spec.controls) do
163 streamWriteFloat32(streamId, control.moveAlpha)
164 end
165 end
166 end
167end

onWriteUpdateStream

Description
Called on on update
Definition
onWriteUpdateStream(integer streamId, table connection, integer dirtyMask)
Arguments
integerstreamIdstream ID
tableconnectionconnection
integerdirtyMaskdirty mask
Code
191function AttacherJointControl:onWriteUpdateStream(streamId, connection, dirtyMask)
192 if connection:getIsServer() then
193 local spec = self.spec_attacherJointControl
194 if streamWriteBool(streamId, bitAND(dirtyMask, spec.dirtyFlag) ~= 0) then
195 for _, control in ipairs(spec.controls) do
196 streamWriteFloat32(streamId, control.moveAlpha)
197 end
198 end
199 end
200end

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
17function AttacherJointControl.prerequisitesPresent(specializations)
18 return SpecializationUtil.hasSpecialization(Attachable, specializations)
19end

registerEventListeners

Description
Definition
registerEventListeners()
Code
41function AttacherJointControl.registerEventListeners(vehicleType)
42 SpecializationUtil.registerEventListener(vehicleType, "onLoad", AttacherJointControl)
43 SpecializationUtil.registerEventListener(vehicleType, "onDelete", AttacherJointControl)
44 SpecializationUtil.registerEventListener(vehicleType, "onReadStream", AttacherJointControl)
45 SpecializationUtil.registerEventListener(vehicleType, "onWriteStream", AttacherJointControl)
46 SpecializationUtil.registerEventListener(vehicleType, "onReadUpdateStream", AttacherJointControl)
47 SpecializationUtil.registerEventListener(vehicleType, "onWriteUpdateStream", AttacherJointControl)
48 SpecializationUtil.registerEventListener(vehicleType, "onUpdate", AttacherJointControl)
49 SpecializationUtil.registerEventListener(vehicleType, "onRegisterActionEvents", AttacherJointControl)
50 SpecializationUtil.registerEventListener(vehicleType, "onPostAttach", AttacherJointControl)
51 SpecializationUtil.registerEventListener(vehicleType, "onPreDetach", AttacherJointControl)
52end

registerFunctions

Description
Definition
registerFunctions()
Code
23function AttacherJointControl.registerFunctions(vehicleType)
24 SpecializationUtil.registerFunction(vehicleType, "controlAttacherJoint", AttacherJointControl.controlAttacherJoint)
25 SpecializationUtil.registerFunction(vehicleType, "controlAttacherJointHeight", AttacherJointControl.controlAttacherJointHeight)
26 SpecializationUtil.registerFunction(vehicleType, "controlAttacherJointTilt", AttacherJointControl.controlAttacherJointTilt)
27 SpecializationUtil.registerFunction(vehicleType, "getControlAttacherJointDirection", AttacherJointControl.getControlAttacherJointDirection)
28end

registerLoweringActionEvent

Description
Definition
registerLoweringActionEvent()
Code
338function AttacherJointControl:registerLoweringActionEvent(superFunc, actionEventsTable, inputAction, target, callback, triggerUp, triggerDown, triggerAlways, startActive, callbackState, customIconName)
339 local spec = self.spec_attacherJointControl
340 if spec.heightController then
341 local _, actionEventId = self:addActionEvent(actionEventsTable, InputAction.LOWER_IMPLEMENT, self, AttacherJointControl.actionEventAttacherJointControlSetPoint, triggerUp, triggerDown, triggerAlways, startActive, callbackState, customIconName)
342 g_inputBinding:setActionEventTextPriority(actionEventId, GS_PRIO_HIGH)
343
344 if inputAction == InputAction.LOWER_IMPLEMENT then
345 return
346 end
347 end
348
349 superFunc(self, actionEventsTable, inputAction, target, callback, triggerUp, triggerDown, triggerAlways, startActive, callbackState, customIconName)
350end

registerOverwrittenFunctions

Description
Definition
registerOverwrittenFunctions()
Code
32function AttacherJointControl.registerOverwrittenFunctions(vehicleType)
33 SpecializationUtil.registerOverwrittenFunction(vehicleType, "loadInputAttacherJoint", AttacherJointControl.loadInputAttacherJoint)
34 SpecializationUtil.registerOverwrittenFunction(vehicleType, "registerLoweringActionEvent", AttacherJointControl.registerLoweringActionEvent)
35 SpecializationUtil.registerOverwrittenFunction(vehicleType, "getLoweringActionEventState", AttacherJointControl.getLoweringActionEventState)
36 SpecializationUtil.registerOverwrittenFunction(vehicleType, "getCanBeSelected", AttacherJointControl.getCanBeSelected)
37end