LUADOC - Farming Simulator 22

Script v1_7_1_0

Engine v1_7_1_0

Foundation Reference

PlaceableSiloExtension

Description
Specialization for placeables
Functions

canBeSold

Description
Definition
canBeSold()
Code
179function PlaceableSiloExtension:canBeSold(superFunc)
180 local spec = self.spec_siloExtension
181
182 if spec.storage == nil then
183 return true, nil
184 end
185
186 local warning = g_i18n:getText("info_siloExtensionNotEmpty") .. "\n"
187 local totalFillLevel = 0
188
189 spec.totalFillTypeSellPrice = 0
190
191 for fillTypeIndex, fillLevel in pairs(spec.storage.fillLevels) do
192 totalFillLevel = totalFillLevel + fillLevel
193 if fillLevel > 0 then
194 local lowestSellPrice = math.huge
195
196 for _, unloadingStation in pairs(g_currentMission.storageSystem:getUnloadingStations()) do
197 if unloadingStation.owningPlaceable ~= nil
198 and unloadingStation.isSellingPoint
199 and unloadingStation.acceptedFillTypes[fillTypeIndex] then
200 local price = unloadingStation:getEffectiveFillTypePrice(fillTypeIndex)
201
202 if price > 0 then
203 lowestSellPrice = math.min(lowestSellPrice, price)
204 end
205 end
206 end
207
208 if lowestSellPrice == math.huge then
209 lowestSellPrice = 0.5
210 end
211
212 local price = fillLevel * lowestSellPrice * PlaceableSiloExtension.PRICE_SELL_FACTOR
213 local fillType = g_fillTypeManager:getFillTypeByIndex(fillTypeIndex)
214 warning = string.format("%s%s (%d %s) - %s: %s\n", warning, fillType.title, g_i18n:getFluid(fillLevel), g_i18n:getText("unit_literShort"), g_i18n:getText("ui_sellValue"), g_i18n:formatMoney(price, 0, true, true))
215 spec.totalFillTypeSellPrice = spec.totalFillTypeSellPrice + price
216 end
217 end
218
219 if totalFillLevel > 0 then
220 return true, warning
221 end
222
223 return true, nil
224end

getCanBePlacedAt

Description
Definition
getCanBePlacedAt()
Code
152function PlaceableSiloExtension:getCanBePlacedAt(superFunc, x, y, z, farmId)
153 local canBePlaced, errorMessage = superFunc(self, x, y, z, farmId)
154 if not canBePlaced then
155 return false, errorMessage
156 end
157
158 local spec = self.spec_siloExtension
159 if spec.storage == nil then
160 return false
161 end
162
163 spec.lastFoundUnloadingStations = nil
164 spec.lastFoundLoadingStations = nil
165
166 local storageSystem = g_currentMission.storageSystem
167 -- we also need to check farmId
168 spec.lastFoundUnloadingStations = storageSystem:getExtendableUnloadingStationsInRange(spec.storage, farmId, x, y, z)
169 spec.lastFoundLoadingStations = storageSystem:getExtendableLoadingStationsInRange(spec.storage, farmId, x, y, z)
170 if #spec.lastFoundUnloadingStations == 0 and #spec.lastFoundLoadingStations == 0 then
171 return false, spec.nearSiloWarning
172 end
173
174 return true
175end

getSpecValueVolume

Description
Definition
getSpecValueVolume()
Code
277function PlaceableSiloExtension.getSpecValueVolume(storeItem, realItem)
278 if storeItem.specs.siloExtensionVolume == nil then
279 return nil
280 end
281
282 return g_i18n:formatVolume(storeItem.specs.siloExtensionVolume)
283end

initSpecialization

Description
Definition
initSpecialization()
Code
63function PlaceableSiloExtension.initSpecialization()
64 g_storeManager:addSpecType("siloExtensionVolume", "shopListAttributeIconCapacity", PlaceableSiloExtension.loadSpecValueVolume, PlaceableSiloExtension.getSpecValueVolume, "placeable")
65end

loadFromXMLFile

Description
Definition
loadFromXMLFile()
Code
228function PlaceableSiloExtension:loadFromXMLFile(xmlFile, key)
229 local spec = self.spec_siloExtension
230
231 if spec.storage ~= nil then
232 spec.storage:loadFromXMLFile(xmlFile, key)
233 end
234end

loadSpecValueVolume

Description
Definition
loadSpecValueVolume()
Code
271function PlaceableSiloExtension.loadSpecValueVolume(xmlFile, customEnvironment, baseDir)
272 return xmlFile:getValue("placeable.siloExtension.storage#capacity")
273end

onDelete

Description
Definition
onDelete()
Code
90function PlaceableSiloExtension:onDelete()
91 local spec = self.spec_siloExtension
92
93 if spec.storage ~= nil then
94 local storageSystem = g_currentMission.storageSystem
95 if storageSystem:hasStorage(spec.storage) then
96 -- just remove storage from all registered unloading and loading stations
97 storageSystem:removeStorageFromUnloadingStations(spec.storage, spec.storage.unloadingStations)
98 storageSystem:removeStorageFromLoadingStations(spec.storage, spec.storage.loadingStations)
99
100 storageSystem:removeStorage(spec.storage)
101 end
102
103 spec.storage:delete()
104 end
105end

onFinalizePlacement

Description
Definition
onFinalizePlacement()
Code
109function PlaceableSiloExtension:onFinalizePlacement()
110 local spec = self.spec_siloExtension
111 if spec.storage ~= nil then
112 local storageSystem = g_currentMission.storageSystem
113 local ownerFarmId = self:getOwnerFarmId()
114 local lastFoundUnloadingStations = storageSystem:getExtendableUnloadingStationsInRange(spec.storage, ownerFarmId)
115 local lastFoundLoadingStations = storageSystem:getExtendableLoadingStationsInRange(spec.storage, ownerFarmId)
116
117 spec.storage:setOwnerFarmId(self:getOwnerFarmId(), true)
118 storageSystem:addStorage(spec.storage)
119 spec.storage:register(true)
120
121 storageSystem:addStorageToUnloadingStations(spec.storage, lastFoundUnloadingStations)
122 storageSystem:addStorageToLoadingStations(spec.storage, lastFoundLoadingStations)
123 end
124end

onLoad

Description
Called on loading
Definition
onLoad(table savegame)
Arguments
tablesavegamesavegame
Code
70function PlaceableSiloExtension:onLoad(savegame)
71 local spec = self.spec_siloExtension
72 local xmlFile = self.xmlFile
73
74 local storageKey = "placeable.siloExtension.storage"
75 spec.foreignSilo = xmlFile:getValue(storageKey.."#foreignSilo", false) -- Shows as foreign silo in the menu
76
77 if xmlFile:hasProperty(storageKey) then
78 spec.storage = Storage.new(self.isServer, self.isClient)
79 spec.storage:load(self.components, xmlFile, storageKey, self.i3dMappings)
80 spec.storage.foreignSilo = spec.foreignSilo
81 else
82 Logging.xmlWarning(xmlFile, "Missing 'storage' for siloExtension!")
83 end
84
85 spec.nearSiloWarning = xmlFile:getValue("placeable.siloExtension#nearSiloWarning", "warning_siloExtensionNotNearSilo", self.customEnvironment)
86end

onReadStream

Description
Definition
onReadStream()
Code
128function PlaceableSiloExtension:onReadStream(streamId, connection)
129 local spec = self.spec_siloExtension
130
131 if spec.storage ~= nil then
132 local storageId = NetworkUtil.readNodeObjectId(streamId)
133 spec.storage:readStream(streamId, connection)
134 g_client:finishRegisterObject(spec.storage, storageId)
135 end
136end

onSell

Description
Definition
onSell()
Code
259function PlaceableSiloExtension:onSell()
260 local spec = self.spec_siloExtension
261
262 if self.isServer then
263 if spec.totalFillTypeSellPrice > 0 then
264 g_currentMission:addMoney(spec.totalFillTypeSellPrice, self:getOwnerFarmId(), MoneyType.HARVEST_INCOME, true, true)
265 end
266 end
267end

onWriteStream

Description
Definition
onWriteStream()
Code
140function PlaceableSiloExtension:onWriteStream(streamId, connection)
141 local spec = self.spec_siloExtension
142
143 if spec.storage ~= nil then
144 NetworkUtil.writeNodeObjectId(streamId, NetworkUtil.getObjectId(spec.storage))
145 spec.storage:writeStream(streamId, connection)
146 g_server:registerObjectInStream(connection, spec.storage)
147 end
148end

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

registerEventListeners

Description
Definition
registerEventListeners()
Code
33function PlaceableSiloExtension.registerEventListeners(placeableType)
34 SpecializationUtil.registerEventListener(placeableType, "onLoad", PlaceableSiloExtension)
35 SpecializationUtil.registerEventListener(placeableType, "onDelete", PlaceableSiloExtension)
36 SpecializationUtil.registerEventListener(placeableType, "onFinalizePlacement", PlaceableSiloExtension)
37 SpecializationUtil.registerEventListener(placeableType, "onReadStream", PlaceableSiloExtension)
38 SpecializationUtil.registerEventListener(placeableType, "onWriteStream", PlaceableSiloExtension)
39 SpecializationUtil.registerEventListener(placeableType, "onSell", PlaceableSiloExtension)
40end

registerOverwrittenFunctions

Description
Definition
registerOverwrittenFunctions()
Code
25function PlaceableSiloExtension.registerOverwrittenFunctions(placeableType)
26 SpecializationUtil.registerOverwrittenFunction(placeableType, "setOwnerFarmId", PlaceableSiloExtension.setOwnerFarmId)
27 SpecializationUtil.registerOverwrittenFunction(placeableType, "getCanBePlacedAt", PlaceableSiloExtension.getCanBePlacedAt)
28 SpecializationUtil.registerOverwrittenFunction(placeableType, "canBeSold", PlaceableSiloExtension.canBeSold)
29end

registerSavegameXMLPaths

Description
Definition
registerSavegameXMLPaths()
Code
55function PlaceableSiloExtension.registerSavegameXMLPaths(schema, basePath)
56 schema:setXMLSpecializationType("SiloExtension")
57 Storage.registerSavegameXMLPaths(schema, basePath)
58 schema:setXMLSpecializationType()
59end

registerXMLPaths

Description
Definition
registerXMLPaths()
Code
44function PlaceableSiloExtension.registerXMLPaths(schema, basePath)
45 schema:setXMLSpecializationType("SiloExtension")
46 schema:register(XMLValueType.BOOL, basePath .. ".siloExtension.storage#foreignSilo", "Shows as foreign silo in the menu", false)
47 schema:register(XMLValueType.L10N_STRING, basePath .. ".siloExtension#nearSiloWarning", "Warning that is shown if extension is not placed near another silo")
48 schema:register(XMLValueType.NODE_INDEX, basePath .. ".siloExtension.storage#node", "Storage node")
49 Storage.registerXMLPaths(schema, basePath .. ".siloExtension.storage")
50 schema:setXMLSpecializationType()
51end

saveToXMLFile

Description
Definition
saveToXMLFile()
Code
238function PlaceableSiloExtension:saveToXMLFile(xmlFile, key, usedModNames)
239 local spec = self.spec_siloExtension
240 if spec.storage ~= nil then
241 spec.storage:saveToXMLFile(xmlFile, key, usedModNames)
242 end
243end

setOwnerFarmId

Description
Definition
setOwnerFarmId()
Code
247function PlaceableSiloExtension:setOwnerFarmId(superFunc, farmId, noEventSend)
248 local spec = self.spec_siloExtension
249
250 superFunc(self, farmId, noEventSend)
251
252 if self.isServer and spec.storage ~= nil then
253 spec.storage:setOwnerFarmId(farmId, true)
254 end
255end