LUADOC - Farming Simulator 22

Script v1_7_1_0

Engine v1_7_1_0

Foundation Reference

PlaceableSolarPanels

Description
Specialization for placeables
Functions

getIncomePerHourFactor

Description
Definition
getIncomePerHourFactor()
Code
178function PlaceableSolarPanels:getIncomePerHourFactor(superFunc)
179 local environment = g_currentMission.environment
180 if not environment.isSunOn then
181 return 0
182 end
183
184 local factor = superFunc(self)
185 if environment.currentSeason == Environment.SEASON.WINTER then
186 factor = factor * 0.75
187 end
188
189 if environment.weather:getIsRaining() then
190 factor = factor * 0.1
191 end
192
193 return factor
194end

getNeedHourChanged

Description
Definition
getNeedHourChanged()
Code
198function PlaceableSolarPanels:getNeedHourChanged(superFunc)
199 return true
200end

loadFromXMLFile

Description
Definition
loadFromXMLFile()
Code
92function PlaceableSolarPanels:loadFromXMLFile(xmlFile, key)
93 local spec = self.spec_solarPanels
94
95 local headRotationRandom = xmlFile:getValue(key.."#headRotationRandom")
96 if headRotationRandom == nil then
97 spec.headRotationRandom = headRotationRandom
98 end
99end

onFinalizePlacement

Description
Definition
onFinalizePlacement()
Code
86function PlaceableSolarPanels:onFinalizePlacement()
87 self:updateHeadRotation()
88end

onHourChanged

Description
Definition
onHourChanged()
Code
152function PlaceableSolarPanels:onHourChanged()
153 self:updateHeadRotation()
154end

onLoad

Description
Definition
onLoad()
Code
66function PlaceableSolarPanels:onLoad(savegame)
67 local spec = self.spec_solarPanels
68 local xmlFile = self.xmlFile
69
70
71 spec.headNode = xmlFile:getValue("placeable.solarPanels#headNode", nil, self.components, self.i3dMappings)
72 spec.randomHeadOffsetRange = xmlFile:getValue("placeable.solarPanels#randomHeadOffsetRange", 15)
73 spec.rotationSpeed = xmlFile:getValue("placeable.solarPanels#rotationSpeed", 5) / 1000
74
75 if spec.headNode ~= nil then
76 local rotVariation = spec.randomHeadOffsetRange * 0.5
77 spec.headRotationRandom = math.random(-1, 1) * rotVariation -- default random -7.5 - 7.5 degrees offset
78
79 spec.currentRotation = spec.headRotationRandom
80 spec.targetRotation = spec.headRotationRandom
81 end
82end

onReadStream

Description
Definition
onReadStream()
Code
112function PlaceableSolarPanels:onReadStream(streamId, connection)
113 local spec = self.spec_solarPanels
114 if spec.headNode ~= nil then
115 spec.headRotationRandom = NetworkUtil.readCompressedAngle(streamId)
116 end
117end

onUpdate

Description
Definition
onUpdate()
Code
130function PlaceableSolarPanels:onUpdate(dt)
131 local spec = self.spec_solarPanels
132 if spec.targetRotation ~= spec.currentRotation then
133 local limitFunc = math.min
134 local direction = 1
135 if spec.targetRotation < spec.currentRotation then
136 limitFunc = math.max
137 direction = -1
138 end
139
140 spec.currentRotation = limitFunc(spec.currentRotation + spec.rotationSpeed * dt * direction, spec.targetRotation)
141 local dx,_,dz = worldDirectionToLocal(getParent(spec.headNode), math.sin(spec.currentRotation), 0, math.cos(spec.currentRotation))
142 setDirection(spec.headNode, dx, 0, dz, 0, 1, 0)
143
144 if spec.targetRotation ~= spec.currentRotation then
145 self:raiseActive()
146 end
147 end
148end

onWriteStream

Description
Definition
onWriteStream()
Code
121function PlaceableSolarPanels:onWriteStream(streamId, connection)
122 local spec = self.spec_solarPanels
123 if spec.headNode ~= nil then
124 NetworkUtil.writeCompressedAngle(streamId, spec.headRotationRandom)
125 end
126end

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

registerEventListeners

Description
Definition
registerEventListeners()
Code
37function PlaceableSolarPanels.registerEventListeners(placeableType)
38 SpecializationUtil.registerEventListener(placeableType, "onLoad", PlaceableSolarPanels)
39 SpecializationUtil.registerEventListener(placeableType, "onFinalizePlacement", PlaceableSolarPanels)
40 SpecializationUtil.registerEventListener(placeableType, "onReadStream", PlaceableSolarPanels)
41 SpecializationUtil.registerEventListener(placeableType, "onWriteStream", PlaceableSolarPanels)
42 SpecializationUtil.registerEventListener(placeableType, "onHourChanged", PlaceableSolarPanels)
43 SpecializationUtil.registerEventListener(placeableType, "onUpdate", PlaceableSolarPanels)
44end

registerFunctions

Description
Definition
registerFunctions()
Code
31function PlaceableSolarPanels.registerFunctions(placeableType)
32 SpecializationUtil.registerFunction(placeableType, "updateHeadRotation", PlaceableSolarPanels.updateHeadRotation)
33end

registerOverwrittenFunctions

Description
Definition
registerOverwrittenFunctions()
Code
24function PlaceableSolarPanels.registerOverwrittenFunctions(placeableType)
25 SpecializationUtil.registerOverwrittenFunction(placeableType, "getIncomePerHourFactor", PlaceableSolarPanels.getIncomePerHourFactor)
26 SpecializationUtil.registerOverwrittenFunction(placeableType, "getNeedHourChanged", PlaceableSolarPanels.getNeedHourChanged)
27end

registerSavegameXMLPaths

Description
Definition
registerSavegameXMLPaths()
Code
58function PlaceableSolarPanels.registerSavegameXMLPaths(schema, basePath)
59 schema:setXMLSpecializationType("SolarPanels")
60 schema:register(XMLValueType.FLOAT, basePath .. "#headRotationRandom", "Head random rotation")
61 schema:setXMLSpecializationType()
62end

registerXMLPaths

Description
Definition
registerXMLPaths()
Code
48function PlaceableSolarPanels.registerXMLPaths(schema, basePath)
49 schema:setXMLSpecializationType("SolarPanels")
50 schema:register(XMLValueType.NODE_INDEX, basePath .. ".solarPanels#headNode", "Head Node")
51 schema:register(XMLValueType.ANGLE, basePath .. ".solarPanels#randomHeadOffsetRange", "Range of random offset", 15)
52 schema:register(XMLValueType.ANGLE, basePath .. ".solarPanels#rotationSpeed", "Rotation Speed (deg/sec)", 5)
53 schema:setXMLSpecializationType()
54end

saveToXMLFile

Description
Definition
saveToXMLFile()
Code
103function PlaceableSolarPanels:saveToXMLFile(xmlFile, key, usedModNames)
104 local spec = self.spec_solarPanels
105 if spec.headNode ~= nil then
106 xmlFile:setValue(key .. "#headRotationRandom", spec.headRotationRandom)
107 end
108end

updateHeadRotation

Description
Definition
updateHeadRotation()
Code
158function PlaceableSolarPanels:updateHeadRotation()
159 local spec = self.spec_solarPanels
160 if spec.headNode ~= nil and g_currentMission ~= nil and g_currentMission.environment ~= nil then
161
162 local sunLight = g_currentMission.environment.lighting.sunLightId
163 if sunLight ~= nil then
164 local dx, _, dz = localDirectionToWorld(sunLight, 0, 0, 1)
165 local headRotation = math.atan2(dx, dz)
166 if math.abs(dx) > 0.3 then
167 headRotation = headRotation + spec.headRotationRandom
168
169 spec.targetRotation = headRotation
170 self:raiseActive()
171 end
172 end
173 end
174end