LUADOC - Farming Simulator 19

Script v1.7.1.0

Engine v1.7.1.0

Foundation Reference

SplineVehicle

Description
Specialization for vehicles driving on a spline only (e.g. train)
Functions

alignToSplineTime

Description
Definition
alignToSplineTime()
Code
195function SplineVehicle:alignToSplineTime(spline, yOffset, tFront)
196 if self.trainSystem == nil then
197 return
198 end
199
200 local spec = self.spec_splineVehicle
201
202 local maxDiff = math.max(self.trainSystem.trainLengthSplineTime, 0.25)
203
204 local delta = tFront - spec.splinePosition
205 if math.abs(delta) > maxDiff then
206 if delta > 0 then
207 delta = delta - 1
208 else
209 delta = delta + 1
210 end
211 end
212
213 self.movingDirection = 1
214 if delta < 0 then
215 self.movingDirection = -1
216 end
217
218 local p1x,p1y,p1z,t = self:getSplinePositionAndTimeFromDistance(tFront, spec.rootNodeToFrontDistance, -1.2*spec.rootNodeToFrontSplineTime)
219
220 local wp1x,wp1y,wp1z = localToWorld(getParent(spline), p1x,p1y,p1z)
221
222 local p2x,p2y,p2z, t2 = self:getSplinePositionAndTimeFromDistance(t, spec.dollyToDollyDistance, -1.2*spec.dollyToDollySplineTime)
223
224 local wp2x,wp2y,wp2z = localToWorld(getParent(spline), p2x,p2y,p2z)
225
226 setDirection(self.rootNode, wp1x-wp2x,wp1y-wp2y,wp1z-wp2z, 0,1,0)
227 local qx, qy, qz, qw = getWorldQuaternion(self.rootNode)
228 self:setWorldPositionQuaternion(wp1x, wp1y+yOffset, wp1z, qx,qy,qz,qw, 1, true)
229
230 if spec.alignDollys then
231 local d1x,d1y,d1z = getSplineDirection(spline, t)
232 local d2x,d2y,d2z = getSplineDirection(spline, t2)
233
234 d1x,d1y,d1z = localDirectionToLocal(spline, getParent(spec.dolly1Node), d1x,d1y,d1z)
235 d2x,d2y,d2z = localDirectionToLocal(spline, getParent(spec.dolly2Node), d2x,d2y,d2z)
236
237 setDirection(spec.dolly1Node, d1x,d1y,d1z, 0,1,0)
238 setDirection(spec.dolly2Node, d2x,d2y,d2z, 0,1,0)
239 end
240
241 spec.lastSplinePositionDelta = delta
242 spec.splinePosition = tFront
243
244 if spec.firstUpdate then
245 spec.lastSplinePositionDelta = 0
246 spec.firstUpdate = false
247 end
248
249 -- return spline time for backNode
250 local tBack = self:getSplineTimeFromDistance(tFront, spec.frontToBackDistance, -1.2*spec.frontToBackSplineTime)
251 return tBack
252end

getAreSurfaceSoundsActive

Description
Definition
getAreSurfaceSoundsActive()
Code
268function SplineVehicle:getAreSurfaceSoundsActive(superFunc)
269 local rootVehicle = self:getRootVehicle()
270 if rootVehicle ~= nil then
271 return rootVehicle:getAreSurfaceSoundsActive()
272 end
273
274 return true
275end

getCurrentSplinePosition

Description
Definition
getCurrentSplinePosition()
Code
98function SplineVehicle:getCurrentSplinePosition()
99 return self.spec_splineVehicle.splinePosition
100end

getCurrentSurfaceSound

Description
Definition
getCurrentSurfaceSound()
Code
262function SplineVehicle:getCurrentSurfaceSound()
263 return self.spec_wheels.surfaceNameToSound["railroad"]
264end

getFrontToBackDistance

Description
Definition
getFrontToBackDistance()
Code
104function SplineVehicle:getFrontToBackDistance()
105 return self.spec_splineVehicle.frontToBackDistance
106end

getLastSpeed

Description
Definition
getLastSpeed()
Code
256function SplineVehicle:getLastSpeed(superFunc, useAttacherVehicleSpeed)
257 return math.abs(self.spec_splineVehicle.splinePositionSpeedReal * 3.6)
258end

getSplinePositionAndTimeFromDistance

Description
Definition
getSplinePositionAndTimeFromDistance()
Code
182function SplineVehicle:getSplinePositionAndTimeFromDistance(t, distance, stepSize)
183
184 if self.trainSystem == nil then
185 return
186 end
187
188 local positiveTimeOffset = stepSize >= 0
189 local x,y,z, t2 = getSplinePositionWithDistance(self.trainSystem.spline, t, distance, positiveTimeOffset, 0.01)
190 return x,y,z, SplineUtil.getValidSplineTime(t2)
191end

getSplineTimeFromDistance

Description
Definition
getSplineTimeFromDistance()
Code
128function SplineVehicle:getSplineTimeFromDistance(t, distance, stepSize)
129 if self.trainSystem == nil then
130 return
131 end
132
133 local positiveTimeOffset = stepSize >= 0
134 local x,y,z, t2 = getSplinePositionWithDistance(self.trainSystem.spline, t, distance, positiveTimeOffset, 0.01)
135 return SplineUtil.getValidSplineTime(t2)
136
137 --[[t = SplineUtil.getValidSplineTime(t)
138 local x1,y1,z1 = getSplinePosition(self.trainSystem.spline, t)
139 local stepDir = MathUtil.sign(stepSize)
140
141 local absStepSize = math.abs(stepSize)
142 local t2 = t + stepDir * absStepSize
143 absStepSize = absStepSize / 2
144
145 local stepCount = 0
146 while true do
147
148 if t2 < 0 then
149 t2 = t2 + 1
150 elseif t2 > 1 then
151 t2 = t2 - 1
152 end
153
154 local x2,y2,z2 = getSplinePosition(self.trainSystem.spline, t2)
155 local dist = MathUtil.vector3Length(x1-x2, y1-y2, z1-z2)
156
157 if math.abs(dist - distance) < 0.001 then
158 break
159 end
160
161 if dist > distance then
162 stepDir = -MathUtil.sign(stepSize)
163 else
164 stepDir = MathUtil.sign(stepSize)
165 end
166
167 absStepSize = absStepSize / 2
168 t2 = t2 + stepDir * absStepSize
169
170 stepCount = stepCount + 1
171 if stepCount > 50 then
172 break
173 end
174
175 end
176
177 return SplineUtil.getValidSplineTime(t2)]]
178end

onLoad

Description
Called on loading
Definition
onLoad(table savegame)
Arguments
tablesavegamesavegame
Code
51function SplineVehicle:onLoad(savegame)
52 local spec = self.spec_splineVehicle
53
54 spec.frontNode = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.splineVehicle.dollies#frontNode"), self.i3dMappings)
55 spec.backNode = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.splineVehicle.dollies#backNode"), self.i3dMappings)
56
57 spec.frontToBackDistance = calcDistanceFrom(spec.frontNode, spec.backNode)
58
59 spec.dolly1Node = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.splineVehicle.dollies#dolly1Node"), self.i3dMappings)
60 spec.dolly2Node = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.splineVehicle.dollies#dolly2Node"), self.i3dMappings)
61
62 spec.dollyToDollyDistance = calcDistanceFrom(spec.dolly1Node, spec.dolly2Node)
63
64 spec.rootNodeToBackDistance = calcDistanceFrom(spec.backNode, self.rootNode)
65 spec.rootNodeToFrontDistance = calcDistanceFrom(spec.frontNode, self.rootNode)
66
67 spec.alignDollys = Utils.getNoNil(getXMLBool(self.xmlFile, "vehicle.splineVehicle.dollies#alignDollys"), true)
68 spec.splinePosition = 0
69 spec.lastSplinePosition = 0
70 spec.currentSplinePosition = 0
71 spec.lastSplinePositionDelta = 0
72
73 spec.lastSplinePositionSpeed = 0
74 spec.splinePositionSpeedReal = 0
75 spec.splinePositionSpeed = 0
76 spec.splinePositionAcceleration = 0
77
78 spec.splineSpeed = 0
79
80 spec.firstUpdate = true
81end

onUpdate

Description
Definition
onUpdate()
Code
110function SplineVehicle:onUpdate(dt, isActiveForInput, isActiveForInputIgnoreSelection, isSelected)
111 local spec = self.spec_splineVehicle
112 if spec.trainSystem ~= nil then
113 local interpDt = g_physicsDt
114 if g_server == nil then
115 -- on clients, we interpolate the vehicles with g_physicsDtUnclamped, thus we need to use the same for camera interpolation
116 interpDt = g_physicsDtUnclamped
117 end
118
119 spec.lastSplinePositionSpeed = spec.splinePositionSpeedReal
120 spec.splinePositionSpeedReal = 0.9*spec.splinePositionSpeedReal + 0.1*(spec.lastSplinePositionDelta * spec.trainSystem.splineLength * 1000/interpDt)
121 spec.splinePositionSpeed = spec.splinePositionSpeed * 0.95 + spec.splinePositionSpeedReal * 0.05
122 spec.splinePositionAcceleration = (spec.splinePositionSpeedReal - spec.lastSplinePositionSpeed) * 1000/interpDt
123 end
124end

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 SplineVehicle.prerequisitesPresent(specializations)
18 return true
19end

registerEventListeners

Description
Definition
registerEventListeners()
Code
42function SplineVehicle.registerEventListeners(vehicleType)
43 SpecializationUtil.registerEventListener(vehicleType, "onLoad", SplineVehicle)
44 SpecializationUtil.registerEventListener(vehicleType, "onUpdate", SplineVehicle)
45end

registerFunctions

Description
Definition
registerFunctions()
Code
23function SplineVehicle.registerFunctions(vehicleType)
24 SpecializationUtil.registerFunction(vehicleType, "getFrontToBackDistance", SplineVehicle.getFrontToBackDistance)
25 SpecializationUtil.registerFunction(vehicleType, "getSplineTimeFromDistance", SplineVehicle.getSplineTimeFromDistance)
26 SpecializationUtil.registerFunction(vehicleType, "getSplinePositionAndTimeFromDistance", SplineVehicle.getSplinePositionAndTimeFromDistance)
27 SpecializationUtil.registerFunction(vehicleType, "alignToSplineTime", SplineVehicle.alignToSplineTime)
28 SpecializationUtil.registerFunction(vehicleType, "getCurrentSplinePosition", SplineVehicle.getCurrentSplinePosition)
29end

registerOverwrittenFunctions

Description
Definition
registerOverwrittenFunctions()
Code
33function SplineVehicle.registerOverwrittenFunctions(vehicleType)
34 SpecializationUtil.registerOverwrittenFunction(vehicleType, "getLastSpeed", SplineVehicle.getLastSpeed)
35 SpecializationUtil.registerOverwrittenFunction(vehicleType, "setTrainSystem", SplineVehicle.setTrainSystem)
36 SpecializationUtil.registerOverwrittenFunction(vehicleType, "getCurrentSurfaceSound", SplineVehicle.getCurrentSurfaceSound)
37 SpecializationUtil.registerOverwrittenFunction(vehicleType, "getAreSurfaceSoundsActive", SplineVehicle.getAreSurfaceSoundsActive)
38end

setTrainSystem

Description
Definition
setTrainSystem()
Code
85function SplineVehicle:setTrainSystem(superFunc, trainSystem)
86 superFunc(self, trainSystem)
87
88 local spec = self.spec_splineVehicle
89 spec.splineLength = trainSystem:getSplineLength()
90 spec.frontToBackSplineTime = spec.frontToBackDistance / spec.splineLength
91 spec.dollyToDollySplineTime = spec.dollyToDollyDistance / spec.splineLength
92 spec.rootNodeToBackSplineTime = spec.rootNodeToBackDistance / spec.splineLength
93 spec.rootNodeToFrontSplineTime = spec.rootNodeToFrontDistance / spec.splineLength
94end