32 | function SiloPlaceable:delete() |
33 | local storageSystem = g_currentMission.storageSystem |
34 | for _, storage in ipairs(self.storages) do |
35 | if self.unloadingStation ~= nil then |
36 | storageSystem:removeStorageFromUnloadingStations(storage, {self.unloadingStation}) |
37 | end |
38 | if self.loadingStation ~= nil then |
39 | storageSystem:removeStorageFromLoadingStations(storage, {self.loadingStation}) |
40 | end |
41 | |
42 | storageSystem:removeStorage(storage) |
43 | end |
44 | |
45 | -- delete storages later to avoid access to already deleted storages |
46 | for _, storage in ipairs(self.storages) do |
47 | storage:delete() |
48 | end |
49 | |
50 | if self.unloadingStation ~= nil then |
51 | storageSystem:removeUnloadingStation(self.unloadingStation) |
52 | self.unloadingStation:delete() |
53 | end |
54 | |
55 | if self.loadingStation ~= nil then |
56 | storageSystem:removeLoadingStation(self.loadingStation) |
57 | self.loadingStation:delete() |
58 | end |
59 | |
60 | unregisterObjectClassName(self) |
61 | SiloPlaceable:superClass().delete(self) |
62 | end |
132 | function SiloPlaceable:finalizePlacement() |
133 | SiloPlaceable:superClass().finalizePlacement(self) |
134 | |
135 | local storageSystem = g_currentMission.storageSystem |
136 | |
137 | self.unloadingStation:register(true) |
138 | storageSystem:addUnloadingStation(self.unloadingStation) |
139 | |
140 | self.loadingStation:register(true) |
141 | storageSystem:addLoadingStation(self.loadingStation) |
142 | |
143 | for _, storage in ipairs(self.storages) do |
144 | if not self.storagePerFarm then |
145 | storage:setOwnerFarmId(self:getOwnerFarmId(), true) |
146 | end |
147 | |
148 | storageSystem:addStorage(storage) |
149 | |
150 | storage:register(true) |
151 | |
152 | storageSystem:addStorageToUnloadingStations(storage, {self.unloadingStation}) |
153 | storageSystem:addStorageToLoadingStations(storage, {self.loadingStation}) |
154 | end |
155 | |
156 | -- now check if there are some storages in range which can also be used |
157 | local storagesInRange = storageSystem:getStoragesInRange(self.unloadingStation, nil, self:getOwnerFarmId()) |
158 | for _, storage in ipairs(storagesInRange) do |
159 | if self.unloadingStation.targetStorages[storage] == nil then |
160 | storageSystem:addStorageToUnloadingStations(storage, {self.unloadingStation}) |
161 | end |
162 | end |
163 | |
164 | storagesInRange = storageSystem:getStoragesInRange(self.loadingStation, nil, self:getOwnerFarmId()) |
165 | for _, storage in ipairs(storagesInRange) do |
166 | if self.loadingStation.sourceStorages[storage] == nil then |
167 | storageSystem:addStorageToLoadingStations(storage, {self.loadingStation}) |
168 | end |
169 | end |
170 | |
171 | if not self.storagePerFarm then |
172 | -- For the very first silo on easy, fill it. The startSiloAmounts are only set on game creation. |
173 | -- In case of no silo on the map this code is used for the first silo placed in new savegame. |
174 | local num = 0 |
175 | for _, placeable in pairs(g_currentMission.placeables) do |
176 | if placeable:getOwnerFarmId() == self:getOwnerFarmId() and placeable:isa(SiloPlaceable) then |
177 | num = num + 1 |
178 | end |
179 | end |
180 | |
181 | if num == 1 and g_currentMission.missionInfo.difficulty == 1 and g_currentMission.missionInfo.startSiloAmounts ~= nil and not g_currentMission.missionInfo:getIsLoadedFromSavegame() and not g_currentMission.missionInfo.hasLoadedFirstFilledSilo then |
182 | -- This is to circumvent a cheat: load new easy game, sell silo, place silo (it is filled now), sell silo, etc. |
183 | -- No need to save it as this code does not run on savegames |
184 | g_currentMission.missionInfo.hasLoadedFirstFilledSilo = true |
185 | |
186 | for fillTypeName, amount in pairs(g_currentMission.missionInfo.startSiloAmounts) do |
187 | local fillTypeIndex = g_fillTypeManager:getFillTypeIndexByName(fillTypeName) |
188 | self:setAmount(fillTypeIndex, amount) |
189 | end |
190 | end |
191 | end |
192 | end |
75 | function SiloPlaceable:load(xmlFilename, x,y,z, rx,ry,rz, initRandom) |
76 | if not SiloPlaceable:superClass().load(self, xmlFilename, x,y,z, rx,ry,rz, initRandom) then |
77 | return false |
78 | end |
79 | |
80 | local xmlFile = loadXMLFile("TempXML", xmlFilename) |
81 | |
82 | self.storagePerFarm = Utils.getNoNil(getXMLBool(xmlFile, "placeable.storages#perFarm"), false) |
83 | self.foreignSilo = Utils.getNoNil(getXMLBool(xmlFile, "placeable.storages#foreignSilo"), self.storagePerFarm) -- Shows as foreign silo in the menu |
84 | |
85 | self.unloadingStation = UnloadingStation:new(self.isServer, self.isClient) |
86 | self.unloadingStation:load(self.nodeId, xmlFile, "placeable.unloadingStation") |
87 | self.unloadingStation.owningPlaceable = self |
88 | self.unloadingStation.hasStoragePerFarm = self.storagePerFarm |
89 | |
90 | self.loadingStation = LoadingStation:new(self.isServer, self.isClient) |
91 | self.loadingStation:load(self.nodeId, xmlFile, "placeable.loadingStation") |
92 | self.loadingStation.owningPlaceable = self |
93 | self.loadingStation.hasStoragePerFarm = self.storagePerFarm |
94 | |
95 | local numStorageSets = self.storagePerFarm and FarmManager.MAX_NUM_FARMS or 1 |
96 | if not g_currentMission.missionDynamicInfo.isMultiplayer then |
97 | numStorageSets = 1 |
98 | end |
99 | |
100 | self.storages = {} |
101 | local i = 0 |
102 | while true do |
103 | local storageKey = string.format("placeable.storages.storage(%d)", i) |
104 | if not hasXMLProperty(xmlFile, storageKey) then |
105 | break |
106 | end |
107 | |
108 | local storageNode = I3DUtil.indexToObject(self.nodeId, getXMLString(xmlFile, storageKey.."#node")) |
109 | if storageNode ~= nil then |
110 | for i = 1, numStorageSets do |
111 | local storage = Storage:new(self.isServer, self.isClient) |
112 | if storage:load(storageNode, xmlFile, storageKey) then |
113 | storage.ownerFarmId = i |
114 | storage.foreignSilo = self.foreignSilo -- Pass along for usage by prices menu |
115 | table.insert(self.storages, storage) |
116 | end |
117 | end |
118 | else |
119 | g_logManager:xmlWarning(xmlFilename, "Missing 'node' for storage '%s'!", storageKey) |
120 | end |
121 | |
122 | i = i + 1 |
123 | end |
124 | |
125 | delete(xmlFile) |
126 | |
127 | return true |
128 | end |
284 | function SiloPlaceable:loadFromXMLFile(xmlFile, key, resetVehicles) |
285 | if not SiloPlaceable:superClass().loadFromXMLFile(self, xmlFile, key, resetVehicles) then |
286 | return false |
287 | end |
288 | |
289 | if not self.unloadingStation:loadFromXMLFile(xmlFile, key..".unloadingStation") then |
290 | return false |
291 | end |
292 | |
293 | local i = 0 |
294 | while true do |
295 | local storageKey = string.format("%s.storage(%d)", key, i) |
296 | if not hasXMLProperty(xmlFile, storageKey) then |
297 | break |
298 | end |
299 | |
300 | local index = getXMLInt(xmlFile, storageKey.."#index") |
301 | |
302 | if index ~= nil then |
303 | if self.storages[index] ~= nil then |
304 | if not self.storages[index]:loadFromXMLFile(xmlFile, storageKey) then |
305 | return false |
306 | end |
307 | end |
308 | end |
309 | |
310 | i = i + 1 |
311 | end |
312 | |
313 | return true |
314 | end |
198 | function SiloPlaceable:readStream(streamId, connection) |
199 | SiloPlaceable:superClass().readStream(self, streamId, connection) |
200 | if connection:getIsServer() then |
201 | local unloadingStationId = NetworkUtil.readNodeObjectId(streamId) |
202 | self.unloadingStation:readStream(streamId, connection) |
203 | g_client:finishRegisterObject(self.unloadingStation, unloadingStationId) |
204 | |
205 | local loadingStationId = NetworkUtil.readNodeObjectId(streamId) |
206 | self.loadingStation:readStream(streamId, connection) |
207 | g_client:finishRegisterObject(self.loadingStation, loadingStationId) |
208 | |
209 | for _, storage in ipairs(self.storages) do |
210 | local storageId = NetworkUtil.readNodeObjectId(streamId) |
211 | storage:readStream(streamId, connection) |
212 | g_client:finishRegisterObject(storage, storageId) |
213 | end |
214 | end |
215 | end |
321 | function SiloPlaceable:saveToXMLFile(xmlFile, key, usedModNames) |
322 | SiloPlaceable:superClass().saveToXMLFile(self, xmlFile, key, usedModNames) |
323 | |
324 | self.unloadingStation:saveToXMLFile(xmlFile, key..".unloadingStation", usedModNames) |
325 | |
326 | for k, storage in ipairs(self.storages) do |
327 | local storageKey = string.format("%s.storage(%d)", key, k-1) |
328 | setXMLInt(xmlFile, storageKey .. "#index", k) |
329 | storage:saveToXMLFile(xmlFile, storageKey, usedModNames) |
330 | end |
331 | end |
221 | function SiloPlaceable:writeStream(streamId, connection) |
222 | SiloPlaceable:superClass().writeStream(self, streamId, connection) |
223 | if not connection:getIsServer() then |
224 | NetworkUtil.writeNodeObjectId(streamId, NetworkUtil.getObjectId(self.unloadingStation)) |
225 | self.unloadingStation:writeStream(streamId, connection) |
226 | g_server:registerObjectInStream(connection, self.unloadingStation) |
227 | |
228 | NetworkUtil.writeNodeObjectId(streamId, NetworkUtil.getObjectId(self.loadingStation)) |
229 | self.loadingStation:writeStream(streamId, connection) |
230 | g_server:registerObjectInStream(connection, self.loadingStation) |
231 | |
232 | for _, storage in ipairs(self.storages) do |
233 | NetworkUtil.writeNodeObjectId(streamId, NetworkUtil.getObjectId(storage)) |
234 | storage:writeStream(streamId, connection) |
235 | g_server:registerObjectInStream(connection, storage) |
236 | end |
237 | end |
238 | end |