LUADOC - Farming Simulator 19

Script v1.7.1.0

Engine v1.7.1.0

Foundation Reference

Ropes

Description
Specialization for vehicles with animated ropes
Functions

loadAdjusterNode

Description
Definition
loadAdjusterNode()
Code
123function Ropes:loadAdjusterNode(adjusterNode, xmlFile, key)
124 XMLUtil.checkDeprecatedXMLElements(xmlFile, self.configFileName, key .. "#index", key .. "#node") --FS17 to FS19
125
126 local node = I3DUtil.indexToObject(self.components, getXMLString(xmlFile, key .. "#node"), self.i3dMappings)
127 if node ~= nil then
128 adjusterNode.node = node
129
130 adjusterNode.rotationAxis = Utils.getNoNil(getXMLInt(xmlFile, key .. "#rotationAxis"), 1)
131 adjusterNode.rotationRange = StringUtil.getRadiansFromString(getXMLString(xmlFile, key .. "#rotationRange"), 2)
132
133 adjusterNode.translationAxis = Utils.getNoNil(getXMLInt(xmlFile, key .. "#translationAxis"), 1)
134 adjusterNode.translationRange = StringUtil.getVectorNFromString(getXMLString(xmlFile, key .. "#translationRange"), 2)
135
136 adjusterNode.minTargetParameters = StringUtil.getVectorNFromString(getXMLString(xmlFile, key .. "#minTargetParameters"), 4)
137 if adjusterNode.minTargetParameters == nil then
138 g_logManager:xmlWarning(self.configFileName, "Missing minTargetParameters attribute in '%s'", key)
139 return false
140 end
141
142 adjusterNode.maxTargetParameters = StringUtil.getVectorNFromString(getXMLString(xmlFile, key .. "#maxTargetParameters"), 4)
143 if adjusterNode.maxTargetParameters == nil then
144 g_logManager:xmlWarning(self.configFileName, "Missing maxTargetParameters attribute in '%s'", key)
145 return false
146 end
147
148 return true
149 else
150 g_logManager:xmlWarning(self.configFileName, "Missing node attribute in '%s'", key)
151 end
152
153 return false
154end

onLoad

Description
Called on loading
Definition
onLoad(table savegame)
Arguments
tablesavegamesavegame
Code
40function Ropes:onLoad(savegame)
41 local spec = self.spec_ropes
42
43 if self.isClient then
44 spec.ropes = {}
45 local i = 0
46 while true do
47 local key = string.format("vehicle.ropes.rope(%d)", i)
48 if not hasXMLProperty(self.xmlFile, key) then
49 break
50 end
51
52 local entry = {}
53 entry.baseNode = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, key .. "#baseNode"), self.i3dMappings)
54 entry.targetNode = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, key .. "#targetNode"), self.i3dMappings)
55 entry.baseParameters = StringUtil.getVectorNFromString(getXMLString(self.xmlFile, key .. "#baseParameters", 4))
56 entry.targetParameters = StringUtil.getVectorNFromString(getXMLString(self.xmlFile, key .. "#targetParameters", 4))
57
58 setShaderParameter(entry.baseNode, "cv0", entry.baseParameters[1], entry.baseParameters[2], entry.baseParameters[3], entry.baseParameters[4], false)
59 setShaderParameter(entry.baseNode, "cv1", 0, 0, 0, 0, false)
60 local x,y,z = localToLocal(entry.targetNode, entry.baseNode, entry.targetParameters[1], entry.targetParameters[2], entry.targetParameters[3])
61 setShaderParameter(entry.baseNode, "cv3", x, y, z, 0, false)
62
63 entry.baseParameterAdjusters = {}
64 local j = 0
65 while true do
66 local adjusterKey = string.format("%s.baseParameterAdjuster(%d)", key, j)
67 if not hasXMLProperty(self.xmlFile, adjusterKey) then
68 break
69 end
70
71 local adjusterNode = {}
72 if self:loadAdjusterNode(adjusterNode, self.xmlFile, adjusterKey)then
73 table.insert(entry.baseParameterAdjusters, adjusterNode)
74 end
75
76 j = j + 1
77 end
78
79 entry.targetParameterAdjusters = {}
80 j = 0
81 while true do
82 local adjusterKey = string.format("%s.targetParameterAdjuster(%d)", key, j)
83 if not hasXMLProperty(self.xmlFile, adjusterKey) then
84 break
85 end
86
87 local adjusterNode = {}
88 if self:loadAdjusterNode(adjusterNode, self.xmlFile, adjusterKey)then
89 table.insert(entry.targetParameterAdjusters, adjusterNode)
90 end
91
92 j = j + 1
93 end
94
95 table.insert(spec.ropes, entry)
96 i = i + 1
97 end
98 end
99end

onLoadFinished

Description
Called on load finished
Definition
onLoadFinished(table savegame)
Arguments
tablesavegamesavegame
Code
104function Ropes:onLoadFinished(savegame)
105 if self.isClient then
106 self:updateRopes(9999)
107 end
108end

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
115function Ropes:onUpdate(dt, isActiveForInput, isActiveForInputIgnoreSelection, isSelected)
116 if self.isClient then
117 self:updateRopes(dt)
118 end
119end

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

registerEventListeners

Description
Definition
registerEventListeners()
Code
31function Ropes.registerEventListeners(vehicleType)
32 SpecializationUtil.registerEventListener(vehicleType, "onLoad", Ropes)
33 SpecializationUtil.registerEventListener(vehicleType, "onLoadFinished", Ropes)
34 SpecializationUtil.registerEventListener(vehicleType, "onUpdate", Ropes)
35end

registerFunctions

Description
Definition
registerFunctions()
Code
23function Ropes.registerFunctions(vehicleType)
24 SpecializationUtil.registerFunction(vehicleType, "loadAdjusterNode", Ropes.loadAdjusterNode)
25 SpecializationUtil.registerFunction(vehicleType, "updateRopes", Ropes.updateRopes)
26 SpecializationUtil.registerFunction(vehicleType, "updateAdjusterNodes", Ropes.updateAdjusterNodes)
27end

updateAdjusterNodes

Description
Definition
updateAdjusterNodes()
Code
176function Ropes:updateAdjusterNodes(adjusterNodes)
177 local xRet, yRet, zRet = 0, 0, 0
178 for _, adjusterNode in pairs(adjusterNodes) do
179 if adjusterNode.rotationAxis ~= nil and adjusterNode.rotationRange ~= nil then
180 local rotations = { getRotation(adjusterNode.node) }
181 local rot = rotations[adjusterNode.rotationAxis]
182 local alpha = math.max(0, math.min(1, (rot - adjusterNode.rotationRange[1]) / (adjusterNode.rotationRange[2] - adjusterNode.rotationRange[1]) ))
183 local x, y, z = MathUtil.vector3ArrayLerp(adjusterNode.minTargetParameters, adjusterNode.maxTargetParameters, alpha)
184 xRet, yRet, zRet = xRet+x, yRet+y, zRet+z
185 elseif adjusterNode.translationAxis ~= nil and adjusterNode.translationRange ~= nil then
186 local translations = { getTranslation(adjusterNode.node) }
187 local trans = translations[adjusterNode.translationAxis]
188 local alpha = math.max(0, math.min(1, (trans - adjusterNode.translationRange[1]) / (adjusterNode.translationRange[2] - adjusterNode.translationRange[1]) ))
189 local x, y, z = MathUtil.vector3ArrayLerp(adjusterNode.minTargetParameters, adjusterNode.maxTargetParameters, alpha)
190 xRet, yRet, zRet = xRet+x, yRet+y, zRet+z
191 end
192 end
193
194 return xRet, yRet, zRet
195end

updateRopes

Description
Definition
updateRopes()
Code
158function Ropes:updateRopes(dt)
159 local spec = self.spec_ropes
160 for _, rope in pairs(spec.ropes) do
161 local x, y, z = self:updateAdjusterNodes(rope.baseParameterAdjusters)
162 setShaderParameter(rope.baseNode, "cv0", rope.baseParameters[1]+x, rope.baseParameters[2]+y, rope.baseParameters[3]+z, 0, false)
163
164 x, y, z = localToLocal(rope.targetNode, rope.baseNode, 0, 0, 0)
165 setShaderParameter(rope.baseNode, "cv2", 0, 0, 0, 0, false)
166 setShaderParameter(rope.baseNode, "cv3", x, y, z, 0, false)
167
168 x, y, z = self:updateAdjusterNodes(rope.targetParameterAdjusters)
169 x, y, z = localToLocal(rope.targetNode, rope.baseNode, rope.targetParameters[1]+x, rope.targetParameters[2]+y, rope.targetParameters[3]+z)
170 setShaderParameter(rope.baseNode, "cv4", x,y,z,0, false)
171 end
172end