LUADOC - Farming Simulator 19

Script v1.7.1.0

Engine v1.7.1.0

Foundation Reference

GroundReference

Description
Specialization for determining if nodes are within a given distance threshold to the terrain/gorund.
Functions

getGroundReferenceNodeFromIndex

Description
Definition
getGroundReferenceNodeFromIndex()
Code
168function GroundReference:getGroundReferenceNodeFromIndex(refNodeIndex)
169 local spec = self.spec_groundReference
170 return spec.groundReferenceNodes[refNodeIndex]
171end

getIsGroundReferenceNodeActive

Description
Definition
getIsGroundReferenceNodeActive()
Code
175function GroundReference:getIsGroundReferenceNodeActive(groundReferenceNode)
176 return groundReferenceNode.isActive
177end

getIsSpeedRotatingPartActive

Description
Definition
getIsSpeedRotatingPartActive()
Code
227function GroundReference:getIsSpeedRotatingPartActive(superFunc, speedRotatingPart)
228 if speedRotatingPart.groundReferenceNodeIndex ~= nil then
229 local spec = self.spec_groundReference
230
231 if spec.groundReferenceNodes[speedRotatingPart.groundReferenceNodeIndex] ~= nil then
232 if not spec.groundReferenceNodes[speedRotatingPart.groundReferenceNodeIndex].isActive then
233 return false
234 end
235 else
236 g_logManager:xmlWarning(self.configFileName, "Unknown ground reference node index '%d' for speed rotating part '%s'! Indices start with 1!", speedRotatingPart.groundReferenceNodeIndex, getName(speedRotatingPart.repr or speedRotatingPart.shaderNode))
237 speedRotatingPart.groundReferenceNodeIndex = nil
238 end
239 end
240
241 return superFunc(self, speedRotatingPart)
242end

getPowerMultiplier

Description
Definition
getPowerMultiplier()
Code
181function GroundReference:getPowerMultiplier(superFunc)
182 local powerMultiplier = superFunc(self)
183
184 local spec = self.spec_groundReference
185 if #(spec.groundReferenceNodes) > 0 then
186 local factor = 0
187 if spec.hasForceFactors then
188 for _, refNode in ipairs(spec.groundReferenceNodes) do
189 if refNode.isActive then
190 factor = factor + refNode.forceFactor
191 end
192 end
193 else
194 for _, refNode in ipairs(spec.groundReferenceNodes) do
195 if refNode.isActive then
196 factor = refNode.chargeValue
197 end
198 end
199 end
200
201 powerMultiplier = powerMultiplier * factor
202 end
203 return powerMultiplier
204end

loadGroundReferenceNode

Description
Definition
loadGroundReferenceNode()
Code
127function GroundReference:loadGroundReferenceNode(xmlFile, baseName, entry)
128 XMLUtil.checkDeprecatedXMLElements(xmlFile, self.configFileName, baseName .. "#index", baseName .. "#node") --FS17 to FS19
129 local spec = self.spec_groundReference
130
131 local node = I3DUtil.indexToObject(self.components, getXMLString(xmlFile, baseName .. "#node"), self.i3dMappings)
132 if node ~= nil then
133 entry.node = node
134 entry.threshold = Utils.getNoNil(getXMLFloat(xmlFile, baseName .. "#threshold"), 0)
135 entry.chargeValue = Utils.getNoNil(getXMLFloat(xmlFile, baseName .. "#chargeValue"), 1)
136 entry.forceFactor = getXMLFloat(xmlFile, baseName .. "#forceFactor")
137 if entry.forceFactor ~= nil then
138 spec.hasForceFactors = true
139 end
140 entry.forceFactor = entry.forceFactor or 1
141 entry.maxActivationDepth = Utils.getNoNil(getXMLFloat(xmlFile, baseName .. "#maxActivationDepth"), 10) -- maximum depth below the terrain -> prevents display in 3d shop
142 entry.isActive = false
143 return true
144 end
145 return false
146end

loadSpeedRotatingPartFromXML

Description
Definition
loadSpeedRotatingPartFromXML()
Code
209function GroundReference:loadSpeedRotatingPartFromXML(superFunc, speedRotatingPart, xmlFile, key)
210 if not superFunc(self, speedRotatingPart, xmlFile, key) then
211 return false
212 end
213
214 XMLUtil.checkDeprecatedXMLElements(self.xmlFile, self.configFileName, key.."#refNodeIndex", key.."#groundReferenceNodeIndex") -- FS17 to FS19
215
216 speedRotatingPart.groundReferenceNodeIndex = getXMLInt(xmlFile, key.."#groundReferenceNodeIndex")
217
218 if speedRotatingPart.groundReferenceNodeIndex ~= nil and speedRotatingPart.groundReferenceNodeIndex == 0 then
219 g_logManager:xmlWarning(self.configFileName, "Unknown ground reference node index '%d' in '%s'! Indices start with 1!", speedRotatingPart.groundReferenceNodeIndex, key)
220 end
221
222 return true
223end

onLoad

Description
Definition
onLoad()
Code
52function GroundReference:onLoad(savegame)
53 local spec = self.spec_groundReference
54
55 spec.hasForceFactors = false
56 spec.groundReferenceNodes = {}
57 local i = 0
58 while true do
59 local baseName = string.format("vehicle.groundReferenceNodes.groundReferenceNode(%d)", i)
60 if not hasXMLProperty(self.xmlFile, baseName) then
61 break
62 end
63 local entry = {}
64 if self:loadGroundReferenceNode(self.xmlFile, baseName, entry) then
65 table.insert(spec.groundReferenceNodes, entry)
66 end
67 i = i + 1
68 end
69
70 -- normalize chargeValues
71 -- deprecated: to be removed in FS21
72 local totalCharge = 0
73 for _, refNode in pairs(spec.groundReferenceNodes) do
74 totalCharge = totalCharge + refNode.chargeValue
75 end
76 if totalCharge > 0 then
77 for _, refNode in pairs(spec.groundReferenceNodes) do
78 refNode.chargeValue = refNode.chargeValue / totalCharge
79 end
80 end
81
82 -- normalize forceFactors
83 local forceFactorSum = 0
84 for _, refNode in pairs(spec.groundReferenceNodes) do
85 forceFactorSum = forceFactorSum + refNode.forceFactor
86 end
87 if forceFactorSum > 0 then
88 for _, refNode in pairs(spec.groundReferenceNodes) do
89 refNode.forceFactor = refNode.forceFactor / forceFactorSum
90 end
91 end
92end

onReadUpdateStream

Description
Definition
onReadUpdateStream()
Code
96function GroundReference:onReadUpdateStream(streamId, timestamp, connection)
97 local spec = self.spec_groundReference
98 if connection:getIsServer() then
99 for _,groundReferenceNode in ipairs(spec.groundReferenceNodes) do
100 groundReferenceNode.isActive = streamReadBool(streamId)
101 end
102 end
103end

onUpdateTick

Description
Definition
onUpdateTick()
Code
118function GroundReference:onUpdateTick(dt, isActiveForInput, isActiveForInputIgnoreSelection, isSelected)
119 local spec = self.spec_groundReference
120 for _,groundReferenceNode in ipairs(spec.groundReferenceNodes) do
121 self:updateGroundReferenceNode(groundReferenceNode)
122 end
123end

onWriteUpdateStream

Description
Definition
onWriteUpdateStream()
Code
107function GroundReference:onWriteUpdateStream(streamId, connection, dirtyMask)
108 local spec = self.spec_groundReference
109 if not connection:getIsServer() then
110 for _,groundReferenceNode in ipairs(spec.groundReferenceNodes) do
111 streamWriteBool(streamId, groundReferenceNode.isActive)
112 end
113 end
114end

prerequisitesPresent

Description
Definition
prerequisitesPresent()
Code
15function GroundReference.prerequisitesPresent(specializations)
16 return true
17end

registerEventListeners

Description
Definition
registerEventListeners()
Code
43function GroundReference.registerEventListeners(vehicleType)
44 SpecializationUtil.registerEventListener(vehicleType, "onLoad", GroundReference)
45 SpecializationUtil.registerEventListener(vehicleType, "onReadUpdateStream", GroundReference)
46 SpecializationUtil.registerEventListener(vehicleType, "onWriteUpdateStream", GroundReference)
47 SpecializationUtil.registerEventListener(vehicleType, "onUpdateTick", GroundReference)
48end

registerEvents

Description
Definition
registerEvents()
Code
21function GroundReference.registerEvents(vehicleType)
22end

registerFunctions

Description
Definition
registerFunctions()
Code
26function GroundReference.registerFunctions(vehicleType)
27 SpecializationUtil.registerFunction(vehicleType, "loadGroundReferenceNode", GroundReference.loadGroundReferenceNode)
28 SpecializationUtil.registerFunction(vehicleType, "updateGroundReferenceNode", GroundReference.updateGroundReferenceNode)
29 SpecializationUtil.registerFunction(vehicleType, "getGroundReferenceNodeFromIndex", GroundReference.getGroundReferenceNodeFromIndex)
30 SpecializationUtil.registerFunction(vehicleType, "getIsGroundReferenceNodeActive", GroundReference.getIsGroundReferenceNodeActive)
31end

registerOverwrittenFunctions

Description
Definition
registerOverwrittenFunctions()
Code
35function GroundReference.registerOverwrittenFunctions(vehicleType)
36 SpecializationUtil.registerOverwrittenFunction(vehicleType, "getPowerMultiplier", GroundReference.getPowerMultiplier)
37 SpecializationUtil.registerOverwrittenFunction(vehicleType, "loadSpeedRotatingPartFromXML", GroundReference.loadSpeedRotatingPartFromXML)
38 SpecializationUtil.registerOverwrittenFunction(vehicleType, "getIsSpeedRotatingPartActive", GroundReference.getIsSpeedRotatingPartActive)
39end

updateGroundReferenceNode

Description
Definition
updateGroundReferenceNode()
Code
150function GroundReference:updateGroundReferenceNode(groundReferenceNode)
151 if self.isServer then
152 local x,y,z = getWorldTranslation(groundReferenceNode.node)
153 local terrainHeight = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, x,y,z)
154
155 local terrainDiff = (terrainHeight + groundReferenceNode.threshold) - y
156 local terrainActiv = terrainDiff > 0 and terrainDiff < groundReferenceNode.maxActivationDepth
157
158 local densityHeight, _ = DensityMapHeightUtil.getHeightAtWorldPos(x,y,z)
159 local densityDiff = (densityHeight + groundReferenceNode.threshold) - y
160 local densityActiv = densityDiff > 0 and densityDiff < groundReferenceNode.maxActivationDepth
161
162 groundReferenceNode.isActive = terrainActiv or densityActiv
163 end
164end