LUADOC - Farming Simulator 22

Script v1_7_1_0

Engine v1_7_1_0

Foundation Reference

PlaceableWindTurbine

Description
Specialization for placeables
Functions

getIncomePerHourFactor

Description
Definition
getIncomePerHourFactor()
Code
218function PlaceableWindTurbine:getIncomePerHourFactor(superFunc)
219 local factor = superFunc(self)
220
221 return factor * self.spec_windTurbine.rotSpeedFactor
222end

getWindTurbineLoad

Description
Definition
getWindTurbineLoad()
Code
210function PlaceableWindTurbine:getWindTurbineLoad()
211 local spec = self.spec_windTurbine
212 return spec.rotSpeedFactor
213end

loadFromXMLFile

Description
Definition
loadFromXMLFile()
Code
151function PlaceableWindTurbine:loadFromXMLFile(xmlFile, key)
152 local spec = self.spec_windTurbine
153 if spec.headNode ~= nil then
154 spec.headRotation = xmlFile:getValue(key.."#headRotation")
155 self:updateHeadRotation()
156 end
157end

onDelete

Description
Definition
onDelete()
Code
98function PlaceableWindTurbine:onDelete()
99 g_currentMission.environment.weather.windUpdater:removeWindChangedListener(self)
100
101 local spec = self.spec_windTurbine
102 g_soundManager:deleteSamples(spec.samples)
103 g_animationManager:deleteAnimations(spec.animationNodes)
104end

onFinalizePlacement

Description
Definition
onFinalizePlacement()
Code
108function PlaceableWindTurbine:onFinalizePlacement()
109 local spec = self.spec_windTurbine
110
111 local windUpdater = g_currentMission.environment.weather.windUpdater
112 windUpdater:addWindChangedListener(self)
113
114 local windDirX, windDirZ, windVelocity = windUpdater:getCurrentValues()
115
116 if spec.headNode ~= nil then
117 spec.headRotation = MathUtil.getYRotationFromDirection(windDirX, windDirZ)
118 if not spec.headAdjustToWind then
119 local rotVariation = 0.2
120 spec.headRotation = 0.7 + math.random() * 2*rotVariation - rotVariation
121 end
122
123 self:updateHeadRotation()
124 end
125
126 self:updateRotorRotSpeed(windVelocity)
127
128 g_animationManager:startAnimations(spec.animationNodes)
129end

onLoad

Description
Called on loading
Definition
onLoad(table savegame)
Arguments
tablesavegamesavegame
Code
71function PlaceableWindTurbine:onLoad(savegame)
72 local spec = self.spec_windTurbine
73
74 local headNode = self.xmlFile:getValue("placeable.windTurbine#headNode", nil, self.components, self.i3dMappings)
75 if headNode ~= nil then
76 spec.headNode = headNode
77 spec.headAdjustToWind = self.xmlFile:getValue("placeable.windTurbine#headAdjustToWind", false)
78 spec.headRotation = 0
79 end
80
81 spec.rotorOptimalWindSpeed = self.xmlFile:getValue("placeable.windTurbine#optimalWindSpeed", 15)
82 spec.rotSpeedFactor = 1
83
84 if self.isClient then
85 spec.samples = {}
86 spec.samples.idle = g_soundManager:loadSampleFromXML(self.xmlFile, "placeable.windTurbine.sounds", "idle", self.baseDirectory, self.components, 1, AudioGroup.ENVIRONMENT, self.i3dMappings, self)
87 spec.animationNodes = g_animationManager:loadAnimations(self.xmlFile, "placeable.windTurbine.animationNodes", self.components, self, self.i3dMappings)
88 for _, anim in ipairs(spec.animationNodes) do
89 anim.speedFunc = function()
90 return spec.rotSpeedFactor
91 end
92 end
93 end
94end

onReadStream

Description
Definition
onReadStream()
Code
133function PlaceableWindTurbine:onReadStream(streamId, connection)
134 local spec = self.spec_windTurbine
135 if spec.headNode ~= nil then
136 spec.headRotation = NetworkUtil.readCompressedAngle(streamId)
137 end
138end

onWriteStream

Description
Definition
onWriteStream()
Code
142function PlaceableWindTurbine:onWriteStream(streamId, connection)
143 local spec = self.spec_windTurbine
144 if spec.headNode ~= nil then
145 NetworkUtil.writeCompressedAngle(streamId, spec.headRotation)
146 end
147end

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
18function PlaceableWindTurbine.prerequisitesPresent(specializations)
19 return SpecializationUtil.hasSpecialization(PlaceableIncomePerHour, specializations)
20end

registerEventListeners

Description
Definition
registerEventListeners()
Code
39function PlaceableWindTurbine.registerEventListeners(placeableType)
40 SpecializationUtil.registerEventListener(placeableType, "onLoad", PlaceableWindTurbine)
41 SpecializationUtil.registerEventListener(placeableType, "onDelete", PlaceableWindTurbine)
42 SpecializationUtil.registerEventListener(placeableType, "onFinalizePlacement", PlaceableWindTurbine)
43 SpecializationUtil.registerEventListener(placeableType, "onReadStream", PlaceableWindTurbine)
44 SpecializationUtil.registerEventListener(placeableType, "onWriteStream", PlaceableWindTurbine)
45end

registerFunctions

Description
Definition
registerFunctions()
Code
24function PlaceableWindTurbine.registerFunctions(placeableType)
25 SpecializationUtil.registerFunction(placeableType, "updateHeadRotation", PlaceableWindTurbine.updateHeadRotation)
26 SpecializationUtil.registerFunction(placeableType, "updateRotorRotSpeed", PlaceableWindTurbine.updateRotorRotSpeed)
27 SpecializationUtil.registerFunction(placeableType, "setWindValues", PlaceableWindTurbine.setWindValues)
28 SpecializationUtil.registerFunction(placeableType, "getWindTurbineLoad", PlaceableWindTurbine.getWindTurbineLoad)
29end

registerOverwrittenFunctions

Description
Definition
registerOverwrittenFunctions()
Code
33function PlaceableWindTurbine.registerOverwrittenFunctions(placeableType)
34 SpecializationUtil.registerOverwrittenFunction(placeableType, "getIncomePerHourFactor", PlaceableWindTurbine.getIncomePerHourFactor)
35end

registerSavegameXMLPaths

Description
Definition
registerSavegameXMLPaths()
Code
62function PlaceableWindTurbine.registerSavegameXMLPaths(schema, basePath)
63 schema:setXMLSpecializationType("WindTurbine")
64 schema:register(XMLValueType.ANGLE, basePath .. "#headRotation", "Current head rotation")
65 schema:setXMLSpecializationType()
66end

registerXMLPaths

Description
Definition
registerXMLPaths()
Code
49function PlaceableWindTurbine.registerXMLPaths(schema, basePath)
50 schema:setXMLSpecializationType("WindTurbine")
51 schema:register(XMLValueType.NODE_INDEX, basePath .. ".windTurbine#headNode", "Head node")
52 schema:register(XMLValueType.BOOL, basePath .. ".windTurbine#headAdjustToWind", "Adjust head node to current wind direction")
53 schema:register(XMLValueType.NODE_INDEX, basePath .. ".windTurbine#rotationNode", "Rotor rotation node, rotated on z-axis")
54 schema:register(XMLValueType.FLOAT, basePath .. ".windTurbine#optimalWindSpeed", "Wind speed in m/s at which rotor reaches max rpm")
55 SoundManager.registerSampleXMLPaths(schema, basePath .. ".windTurbine.sounds", "idle")
56 AnimationManager.registerAnimationNodesXMLPaths(schema, basePath .. ".windTurbine.animationNodes")
57 schema:setXMLSpecializationType()
58end

saveToXMLFile

Description
Definition
saveToXMLFile()
Code
161function PlaceableWindTurbine:saveToXMLFile(xmlFile, key, usedModNames)
162 local spec = self.spec_windTurbine
163 if spec.headNode ~= nil then
164 xmlFile:setValue(key.."#headRotation", spec.headRotation)
165 end
166end

setWindValues

Description
Definition
setWindValues()
Code
198function PlaceableWindTurbine:setWindValues(windDirX, windDirZ, windVelocity, cirrusCloudSpeedFactor)
199 local spec = self.spec_windTurbine
200 if spec.headAdjustToWind and spec.headNode ~= nil then
201 spec.headRotation = MathUtil.getYRotationFromDirection(windDirX, windDirZ)
202 self:updateHeadRotation()
203 end
204
205 self:updateRotorRotSpeed(windVelocity)
206end

updateHeadRotation

Description
Definition
updateHeadRotation()
Code
170function PlaceableWindTurbine:updateHeadRotation()
171 local spec = self.spec_windTurbine
172 if spec.headNode ~= nil then
173 setWorldRotation(spec.headNode, 0, spec.headRotation, 0)
174 end
175end

updateRotorRotSpeed

Description
Definition
updateRotorRotSpeed()
Code
179function PlaceableWindTurbine:updateRotorRotSpeed(windVelocity)
180 local spec = self.spec_windTurbine
181 spec.rotSpeedFactor = MathUtil.clamp(windVelocity / spec.rotorOptimalWindSpeed, 0, 1)
182
183 if self.isClient then
184 if spec.rotSpeedFactor > 0 then
185 if not g_soundManager:getIsSamplePlaying(spec.samples.idle) then
186 g_soundManager:playSample(spec.samples.idle, 0)
187 end
188 else
189 if g_soundManager:getIsSamplePlaying(spec.samples.idle) then
190 g_soundManager:stopSample(spec.samples.idle)
191 end
192 end
193 end
194end