LUADOC - Farming Simulator 22

Script v1_7_1_0

Engine v1_7_1_0

Foundation Reference

PlaceableBunkerSilo

Description
Specialization for placeables
Functions

canBeSold

Description
Definition
canBeSold()
Code
218function PlaceableBunkerSilo:canBeSold(superFunc)
219 local spec = self.spec_bunkerSilo
220
221 if spec.bunkerSilo.fillLevel > 0 then
222 return true, spec.sellWarningText
223 end
224
225 return true, nil
226end

getHasOverlap

Description
Definition
getHasOverlap()
Code
259function PlaceableBunkerSilo:getHasOverlap(superFunc, x, y, z, rotY, checkFunc)
260
261 local spec = self.spec_bunkerSilo
262 local overwrittenCheckFunc = checkFunc
263
264 if spec.foundSnappingSilo ~= nil then
265 overwrittenCheckFunc = function(hitObjectId)
266 local object = g_currentMission:getNodeObject(hitObjectId)
267 if object == spec.foundSnappingSilo then
268 return false
269 end
270
271 if checkFunc ~= nil then
272 return checkFunc(hitObjectId)
273 end
274
275 return hitObjectId ~= g_currentMission.terrainRootNode
276 end
277 end
278
279 return superFunc(self, x, y, z, rotY, overwrittenCheckFunc)
280end

getIsBunkerSiloExtendable

Description
Definition
getIsBunkerSiloExtendable()
Code
167function PlaceableBunkerSilo:getIsBunkerSiloExtendable()
168 local spec = self.spec_bunkerSilo
169 return spec.isExtendable
170end

getPlacementPosition

Description
Definition
getPlacementPosition()
Code
298function PlaceableBunkerSilo:getPlacementPosition(superFunc, x, y, z)
299 x, y, z = superFunc(self, x, y, z)
300
301 local spec = self.spec_bunkerSilo
302 if spec.foundSnappingSilo ~= nil then
303 x, y, z = localToWorld(spec.foundSnappingSilo.rootNode, spec.siloSiloDistance * spec.foundSnappingSiloSide, 0, 0)
304 end
305
306 return x, y, z
307end

getPlacementRotation

Description
Definition
getPlacementRotation()
Code
284function PlaceableBunkerSilo:getPlacementRotation(superFunc, x, y, z)
285 x, y, z = superFunc(self, x, y, z)
286
287 local spec = self.spec_bunkerSilo
288 if spec.foundSnappingSilo ~= nil then
289 local dx, _, dz = localDirectionToWorld(spec.foundSnappingSilo.rootNode, 0, 0, 1)
290 x, y, z = 0, MathUtil.getYRotationFromDirection(dx, dz), 0
291 end
292
293 return x, y, z
294end

loadFromXMLFile

Description
Definition
loadFromXMLFile()
Code
142function PlaceableBunkerSilo:loadFromXMLFile(xmlFile, key)
143 local spec = self.spec_bunkerSilo
144
145 if not spec.bunkerSilo:loadFromXMLFile(xmlFile, key) then
146 return false
147 end
148end

onDelete

Description
Definition
onDelete()
Code
98function PlaceableBunkerSilo:onDelete()
99 local spec = self.spec_bunkerSilo
100
101 self:updateBunkerSiloWalls(true)
102 if spec.bunkerSilo ~= nil then
103 spec.bunkerSilo:delete()
104 end
105 g_currentMission.placeableSystem:removeBunkerSilo(self)
106end

onFinalizePlacement

Description
Definition
onFinalizePlacement()
Code
110function PlaceableBunkerSilo:onFinalizePlacement()
111 local spec = self.spec_bunkerSilo
112 local ownerFarmId = self:getOwnerFarmId()
113
114 self:updateBunkerSiloWalls(false)
115 spec.bunkerSilo:register(true)
116 spec.bunkerSilo:setOwnerFarmId(ownerFarmId, true)
117 g_currentMission.placeableSystem:addBunkerSilo(self)
118end

onLoad

Description
Called on loading
Definition
onLoad(table savegame)
Arguments
tablesavegamesavegame
Code
74function PlaceableBunkerSilo:onLoad(savegame)
75 local spec = self.spec_bunkerSilo
76
77 spec.bunkerSilo = BunkerSilo.new(self.isServer, self.isClient)
78 if not spec.bunkerSilo:load(self.components, self.xmlFile, "placeable.bunkerSilo", self.i3dMappings) then
79 spec.bunkerSilo:delete()
80 end
81
82 spec.isExtendable = self.xmlFile:getValue("placeable.bunkerSilo#isExtendable", false)
83 if spec.isExtendable then
84 spec.siloSiloDistance = self.xmlFile:getValue("placeable.bunkerSilo#siloToSiloDistance")
85 if spec.siloSiloDistance == nil then
86 Logging.xmlError(self.xmlFile, "Bunker Silo is marked as extendable but 'placeable.bunkerSilo#siloToSiloDistance' is not set")
87 self:setLoadingState(Placeable.LOADING_STATE_ERROR)
88 return
89 end
90 spec.snapDistance = self.xmlFile:getValue("placeable.bunkerSilo#snapDistance") or spec.siloSiloDistance * 1.1
91 end
92
93 spec.sellWarningText = g_i18n:convertText(self.xmlFile:getValue("placeable.bunkerSilo#sellWarningText", "$l10n_info_bunkerSiloNotEmpty"))
94end

onReadStream

Description
Definition
onReadStream()
Code
122function PlaceableBunkerSilo:onReadStream(streamId, connection)
123 local spec = self.spec_bunkerSilo
124
125 local bunkerSiloId = NetworkUtil.readNodeObjectId(streamId)
126 spec.bunkerSilo:readStream(streamId, connection)
127 g_client:finishRegisterObject(spec.bunkerSilo, bunkerSiloId)
128end

onSell

Description
Definition
onSell()
Code
210function PlaceableBunkerSilo:onSell()
211 local spec = self.spec_bunkerSilo
212
213 spec.bunkerSilo:clearSiloArea()
214end

onWriteStream

Description
Definition
onWriteStream()
Code
132function PlaceableBunkerSilo:onWriteStream(streamId, connection)
133 local spec = self.spec_bunkerSilo
134
135 NetworkUtil.writeNodeObjectId(streamId, NetworkUtil.getObjectId(spec.bunkerSilo))
136 spec.bunkerSilo:writeStream(streamId, connection)
137 g_server:registerObjectInStream(connection, spec.bunkerSilo)
138end

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

registerEventListeners

Description
Definition
registerEventListeners()
Code
24function PlaceableBunkerSilo.registerEventListeners(placeableType)
25 SpecializationUtil.registerEventListener(placeableType, "onLoad", PlaceableBunkerSilo)
26 SpecializationUtil.registerEventListener(placeableType, "onDelete", PlaceableBunkerSilo)
27 SpecializationUtil.registerEventListener(placeableType, "onFinalizePlacement", PlaceableBunkerSilo)
28 SpecializationUtil.registerEventListener(placeableType, "onReadStream", PlaceableBunkerSilo)
29 SpecializationUtil.registerEventListener(placeableType, "onWriteStream", PlaceableBunkerSilo)
30 SpecializationUtil.registerEventListener(placeableType, "onSell", PlaceableBunkerSilo)
31end

registerFunctions

Description
Definition
registerFunctions()
Code
35function PlaceableBunkerSilo.registerFunctions(placeableType)
36 SpecializationUtil.registerFunction(placeableType, "updateBunkerSiloWalls", PlaceableBunkerSilo.updateBunkerSiloWalls)
37 SpecializationUtil.registerFunction(placeableType, "setWallVisibility", PlaceableBunkerSilo.setWallVisibility)
38 SpecializationUtil.registerFunction(placeableType, "getIsBunkerSiloExtendable", PlaceableBunkerSilo.getIsBunkerSiloExtendable)
39end

registerOverwrittenFunctions

Description
Definition
registerOverwrittenFunctions()
Code
43function PlaceableBunkerSilo.registerOverwrittenFunctions(placeableType)
44 SpecializationUtil.registerOverwrittenFunction(placeableType, "getPlacementPosition", PlaceableBunkerSilo.getPlacementPosition)
45 SpecializationUtil.registerOverwrittenFunction(placeableType, "getPlacementRotation", PlaceableBunkerSilo.getPlacementRotation)
46 SpecializationUtil.registerOverwrittenFunction(placeableType, "getHasOverlap", PlaceableBunkerSilo.getHasOverlap)
47 SpecializationUtil.registerOverwrittenFunction(placeableType, "startPlacementCheck", PlaceableBunkerSilo.startPlacementCheck)
48 SpecializationUtil.registerOverwrittenFunction(placeableType, "canBeSold", PlaceableBunkerSilo.canBeSold)
49end

registerSavegameXMLPaths

Description
Definition
registerSavegameXMLPaths()
Code
65function PlaceableBunkerSilo.registerSavegameXMLPaths(schema, basePath)
66 schema:setXMLSpecializationType("BunkerSilo")
67 BunkerSilo.registerSavegameXMLPaths(schema, basePath)
68 schema:setXMLSpecializationType()
69end

registerXMLPaths

Description
Definition
registerXMLPaths()
Code
53function PlaceableBunkerSilo.registerXMLPaths(schema, basePath)
54 schema:setXMLSpecializationType("BunkerSilo")
55 BunkerSilo.registerXMLPaths(schema, basePath .. ".bunkerSilo")
56 schema:register(XMLValueType.BOOL, basePath .. ".bunkerSilo#isExtendable", "Checks if silo is extendable. If set 'siloToSiloDistance' needs to be provided as well", false)
57 schema:register(XMLValueType.FLOAT, basePath .. ".bunkerSilo#siloToSiloDistance", "Silo to silo distance required for aligning multiple silos of the same type next to each other")
58 schema:register(XMLValueType.FLOAT, basePath .. ".bunkerSilo#snapDistance", "Snap distance for building an array of the same silo", "siloToSiloDistance * 1.1")
59 schema:register(XMLValueType.STRING, basePath .. ".bunkerSilo#sellWarningText", "Sell warning text")
60 schema:setXMLSpecializationType()
61end

saveToXMLFile

Description
Definition
saveToXMLFile()
Code
152function PlaceableBunkerSilo:saveToXMLFile(xmlFile, key, usedModNames)
153 local spec = self.spec_bunkerSilo
154
155 spec.bunkerSilo:saveToXMLFile(xmlFile, key, usedModNames)
156end

setWallVisibility

Description
Definition
setWallVisibility()
Code
160function PlaceableBunkerSilo:setWallVisibility(isLeftVisible, isRightVisible)
161 local spec = self.spec_bunkerSilo
162 spec.bunkerSilo:setWallVisibility(isLeftVisible, isRightVisible)
163end

startPlacementCheck

Description
Definition
startPlacementCheck()
Code
230function PlaceableBunkerSilo:startPlacementCheck(superFunc, x, y, z, rotY)
231 local spec = self.spec_bunkerSilo
232 superFunc(self, x, y, z, rotY)
233
234 if not spec.isExtendable then
235 return
236 end
237
238 spec.foundSnappingSilo = nil
239 spec.foundSnappingSiloSide = 0
240 local nearestDistance = spec.snapDistance
241 for _, placeable in ipairs(g_currentMission.placeableSystem:getBunkerSilos()) do
242 if placeable:getOwnerFarmId() == g_currentMission.player.farmId then
243 if placeable.configFileName == self.configFileName then
244
245 local lx, _, lz = worldToLocal(placeable.rootNode, x, y, z)
246 local distance = MathUtil.vector2Length(lx, lz)
247 if distance < nearestDistance then
248 nearestDistance = distance
249 spec.foundSnappingSilo = placeable
250 spec.foundSnappingSiloSide = MathUtil.sign(lx)
251 end
252 end
253 end
254 end
255end

updateBunkerSiloWalls

Description
Definition
updateBunkerSiloWalls()
Code
174function PlaceableBunkerSilo:updateBunkerSiloWalls(isDeleting)
175 local spec = self.spec_bunkerSilo
176 if self.rootNode ~= nil then
177 local x, y, z = getWorldTranslation(self.rootNode)
178 local placeableSystem = g_currentMission.placeableSystem
179 for _, placeable in ipairs(placeableSystem:getBunkerSilos()) do
180 if placeable:getIsBunkerSiloExtendable() and placeable ~= self and placeable:getOwnerFarmId() == self:getOwnerFarmId() then
181 if placeable.configFileName == self.configFileName then
182 local lx, _, lz = worldToLocal(placeable.rootNode, x, y, z)
183 local distance = MathUtil.vector2Length(lx, lz)
184 if distance < spec.siloSiloDistance + 0.5 then
185 local isLeft = lx > 0
186
187 if isDeleting then
188 -- always set walls visible
189 if isLeft then
190 placeable:setWallVisibility(true, nil)
191 else
192 placeable:setWallVisibility(nil, true)
193 end
194 else
195 if isLeft then
196 placeable:setWallVisibility(false, nil)
197 else
198 placeable:setWallVisibility(nil, false)
199 end
200 end
201 end
202 end
203 end
204 end
205 end
206end