Copyright (c) 2008-2015 GIANTS Software GmbH, Confidential, All Rights Reserved.
This document is to be published solely by ls-mods.de
1 | -- Copyright (C) GIANTS Software GmbH, Confidential, All Rights Reserved. |
2 | |
3 | Bga = {}; |
4 | Bga_mt = Class(Bga, Object); |
5 | |
6 | InitStaticObjectClass(Bga, "Bga", ObjectIds.OBJECT_BGA); |
7 | |
8 | function Bga:onCreate(id) |
9 | local object = Bga:new(g_server ~= nil, g_client ~= nil); |
10 | if object:load(id) then |
11 | g_currentMission:addOnCreateLoadedObject(object); |
12 | object:register(true); |
13 | else |
14 | object:delete(); |
15 | end; |
16 | end; |
17 | |
18 | function Bga:new(isServer, isClient, customMt) |
19 | local mt = customMt; |
20 | if mt == nil then |
21 | mt = Bga_mt; |
22 | end; |
23 | |
24 | local self = Object:new(isServer, isClient, mt); |
25 | self.nodeId = 0; |
26 | self.tipTriggers = {}; |
27 | self.bunkerFillLevel = 0; |
28 | self.sentBunkerFillLevel = self.bunkerFillLevel; |
29 | |
30 | self.bunkerUseTime = 0; |
31 | self.bunkerUseSpeed = 25; -- liter per ingame seconds |
32 | |
33 | self.bunkerPlaneMinY = 0; |
34 | self.bunkerPlaneMaxY = 1; |
35 | self.bunkerPlaneAlpha = 0; |
36 | self.bunkerPlaneMoveScale = 0.2; |
37 | |
38 | self.bunkerRolls = {}; |
39 | self.bunkerRollsSpeed = -0.01; |
40 | |
41 | self.bunkerCapacity = 20000; |
42 | |
43 | self.fillDeltaToManureDeltaScale = 0.3; |
44 | self.baleValueToFillDeltaScale = 1; |
45 | |
46 | self.bgaDirtyFlag = self:getNextDirtyFlag(); |
47 | return self; |
48 | end; |
49 | |
50 | function Bga:delete() |
51 | g_currentMission:removeOnCreateLoadedObjectToSave(self); |
52 | if self.liquidManureSiloTrigger ~= nil then |
53 | self.liquidManureSiloTrigger:delete(); |
54 | end; |
55 | if self.isServer then |
56 | for _,trigger in pairs(self.tipTriggers) do |
57 | if trigger.isRegistered then |
58 | trigger:unregister(); |
59 | trigger:delete(); |
60 | end; |
61 | end; |
62 | end; |
63 | if self.objectDeleteTriggerId ~= nil then |
64 | removeTrigger(self.objectDeleteTriggerId); |
65 | end; |
66 | if self.silageCatcherId ~= nil then |
67 | g_currentMission:removeNodeObject(self.silageCatcherId); |
68 | end; |
69 | if self.nodeId ~= 0 then |
70 | g_currentMission:removeNodeObject(self.nodeId); |
71 | end; |
72 | Bga:superClass().delete(self); |
73 | end; |
74 | |
75 | function Bga:readStream(streamId, connection) |
76 | --Bga:superClass().readStream(self, streamId, connection); |
77 | if connection:getIsServer() then |
78 | local bunkerFillLevel = streamReadFloat32(streamId); |
79 | self:setBunkerFillLevel(bunkerFillLevel); |
80 | if self.liquidManureSiloTrigger ~= nil then |
81 | self.liquidManureSiloTrigger:setFillLevel(streamReadFloat32(streamId)); |
82 | end |
83 | end; |
84 | end; |
85 | |
86 | function Bga:writeStream(streamId, connection) |
87 | --Bga:superClass().writeStream(self, streamId, connection); |
88 | if not connection:getIsServer() then |
89 | streamWriteFloat32(streamId, self.bunkerFillLevel); |
90 | if self.liquidManureSiloTrigger ~= nil then |
91 | streamWriteFloat32(streamId, self.liquidManureSiloTrigger.fillLevel); |
92 | end |
93 | end; |
94 | end; |
95 | |
96 | function Bga:readUpdateStream(streamId, timestamp, connection) |
97 | if connection:getIsServer() then |
98 | local bunkerFillLevel = streamReadFloat32(streamId); |
99 | self:setBunkerFillLevel(bunkerFillLevel); |
100 | if self.liquidManureSiloTrigger ~= nil then |
101 | self.liquidManureSiloTrigger:setFillLevel(streamReadFloat32(streamId)); |
102 | end |
103 | end; |
104 | end; |
105 | |
106 | function Bga:writeUpdateStream(streamId, connection, dirtyMask) |
107 | if not connection:getIsServer() then |
108 | streamWriteFloat32(streamId, self.bunkerFillLevel); |
109 | if self.liquidManureSiloTrigger ~= nil then |
110 | streamWriteFloat32(streamId, self.liquidManureSiloTrigger.fillLevel); |
111 | end |
112 | end; |
113 | end; |
114 | |
115 | function Bga:load(nodeId) |
116 | |
117 | self.nodeId = nodeId; |
118 | |
119 | local liquidManureSiloIndex = getUserAttribute(nodeId, "liquidManureSiloIndex"); |
120 | if liquidManureSiloIndex ~= nil then |
121 | local liquidManureSiloId = Utils.indexToObject(nodeId, liquidManureSiloIndex); |
122 | if liquidManureSiloId ~= nil then |
123 | self.liquidManureSiloTrigger = LiquidManureFillTrigger:new(); |
124 | if not self.liquidManureSiloTrigger:load(liquidManureSiloId, self) then |
125 | self.liquidManureSiloTrigger:delete(); |
126 | self.liquidManureSiloTrigger = nil; |
127 | end |
128 | end; |
129 | end; |
130 | |
131 | local tipTriggersIndex = getUserAttribute(nodeId, "tipTriggersIndex"); |
132 | if tipTriggersIndex ~= nil then |
133 | local tipTriggersId = Utils.indexToObject(nodeId, tipTriggersIndex); |
134 | if tipTriggersId ~= nil then |
135 | local numChildren = getNumOfChildren(tipTriggersId); |
136 | for i=1,numChildren do |
137 | local childId = getChildAt(tipTriggersId, i-1); |
138 | local tipTrigger = BgaTipTrigger:new(self.isServer, self.isClient); |
139 | tipTrigger:load(childId, self); |
140 | g_currentMission:addOnCreateLoadedObject(tipTrigger); |
141 | tipTrigger:register(true); |
142 | table.insert(self.tipTriggers, tipTrigger); |
143 | end; |
144 | end; |
145 | end; |
146 | |
147 | if self.isServer then |
148 | local objectDeleteTriggerIndex = getUserAttribute(nodeId, "objectDeleteTriggerIndex"); |
149 | if objectDeleteTriggerIndex ~= nil then |
150 | self.objectDeleteTriggerId = Utils.indexToObject(nodeId, objectDeleteTriggerIndex); |
151 | if self.objectDeleteTriggerId ~= nil then |
152 | addTrigger(self.objectDeleteTriggerId, "objectDeleteTriggerCallback", self); |
153 | end; |
154 | end; |
155 | |
156 | local silageCatcherIndex = getUserAttribute(nodeId, "silageCatcherIndex"); |
157 | if silageCatcherIndex ~= nil then |
158 | self.silageCatcherId = Utils.indexToObject(nodeId, silageCatcherIndex); |
159 | if self.silageCatcherId ~= nil then |
160 | g_currentMission:addNodeObject(self.silageCatcherId, self); |
161 | end; |
162 | end; |
163 | end; |
164 | |
165 | local bunkerPlaneIndex = getUserAttribute(nodeId, "bunkerPlaneIndex"); |
166 | if bunkerPlaneIndex ~= nil then |
167 | self.bunkerPlaneId = Utils.indexToObject(nodeId, bunkerPlaneIndex); |
168 | end; |
169 | local minY, maxY = Utils.getVectorFromString(getUserAttribute(nodeId, "bunkerPlaneMinMaxY")); |
170 | if minY ~= nil and maxY ~= nil then |
171 | self.bunkerPlaneMinY = minY; |
172 | self.bunkerPlaneMaxY = maxY; |
173 | end; |
174 | |
175 | local bunkerRollsIndex = getUserAttribute(nodeId, "bunkerRollsIndex"); |
176 | if bunkerRollsIndex ~= nil then |
177 | local bunkerRollsIndices = Utils.splitString(" ", bunkerRollsIndex); |
178 | for _, index in pairs(bunkerRollsIndices) do |
179 | local bunkerRollsId = Utils.indexToObject(nodeId, index); |
180 | if bunkerRollsId ~= nil then |
181 | local numChildren = getNumOfChildren(bunkerRollsId); |
182 | for i=1,numChildren do |
183 | local childId = getChildAt(bunkerRollsId, i-1); |
184 | table.insert(self.bunkerRolls, childId); |
185 | end; |
186 | end; |
187 | end; |
188 | end; |
189 | local bunkerRollsSpeed = getUserAttribute(nodeId, "bunkerRollsSpeed"); |
190 | if bunkerRollsSpeed ~= nil then |
191 | self.bunkerRollsSpeed = math.rad(bunkerRollsSpeed/1000); |
192 | end; |
193 | |
194 | local weightDisplayIndex = getUserAttribute(nodeId, "weightDisplayIndex"); |
195 | if weightDisplayIndex ~= nil then |
196 | self.displayNumbers = Utils.indexToObject(nodeId, weightDisplayIndex); |
197 | end; |
198 | |
199 | local bunkerCapacityStr = getUserAttribute(nodeId, "bunkerCapacity"); |
200 | if bunkerCapacityStr ~= nil then |
201 | self.bunkerCapacity = Utils.getNoNil(tonumber(bunkerCapacityStr), self.bunkerCapacity); |
202 | end; |
203 | |
204 | local bunkerUseSpeedStr = getUserAttribute(nodeId, "bunkerUseSpeed"); |
205 | if bunkerUseSpeedStr ~= nil then |
206 | self.bunkerUseSpeed = Utils.getNoNil(tonumber(bunkerUseSpeedStr), self.bunkerUseSpeed); |
207 | end; |
208 | |
209 | local bunkerPlaneMoveScaleStr = getUserAttribute(nodeId, "bunkerPlaneMoveScale"); |
210 | if bunkerPlaneMoveScaleStr ~= nil then |
211 | self.bunkerPlaneMoveScale = Utils.getNoNil(tonumber(bunkerPlaneMoveScaleStr), self.bunkerPlaneMoveScale); |
212 | end; |
213 | |
214 | local fillDeltaToManureDeltaScaleStr = getUserAttribute(nodeId, "fillDeltaToManureDeltaScale"); |
215 | if fillDeltaToManureDeltaScaleStr ~= nil then |
216 | self.fillDeltaToManureDeltaScale = Utils.getNoNil(tonumber(fillDeltaToManureDeltaScaleStr), self.fillDeltaToManureDeltaScale); |
217 | end; |
218 | |
219 | local baleValueToFillDeltaScaleStr = getUserAttribute(nodeId, "baleValueToFillDeltaScale"); |
220 | if baleValueToFillDeltaScaleStr ~= nil then |
221 | self.baleValueToFillDeltaScale = Utils.getNoNil(tonumber(baleValueToFillDeltaScaleStr), self.baleValueToFillDeltaScale); |
222 | end; |
223 | |
224 | self.bunkerFillLevel = 0; |
225 | self.sentBunkerFillLevel = self.bunkerFillLevel; |
226 | self:setBunkerFillLevel(0); |
227 | |
228 | self.bunkerSoundId = Utils.indexToObject(nodeId, getUserAttribute(nodeId, "bunkerSoundIndex")); |
229 | self.bunkerSoundPlaying = false; |
230 | if self.bunkerSoundId ~= nil then |
231 | setVisibility(self.bunkerSoundId, false); |
232 | end |
233 | |
234 | g_currentMission:addNodeObject(nodeId, self); |
235 | |
236 | g_currentMission:addOnCreateLoadedObjectToSave(self); |
237 | |
238 | self.moneyChangeId = getMoneyTypeId(); |
239 | self.lastMoneyChange = -1; |
240 | |
241 | return true; |
242 | end; |
243 | |
244 | function Bga:loadFromAttributesAndNodes(xmlFile, key) |
245 | |
246 | local liquidManureSiloFillLevel = getXMLFloat(xmlFile, key.."#liquidManureSiloFillLevel"); |
247 | if liquidManureSiloFillLevel ~= nil then |
248 | if self.liquidManureSiloTrigger ~= nil then |
249 | self.liquidManureSiloTrigger:setFillLevel(liquidManureSiloFillLevel, true); |
250 | end; |
251 | end; |
252 | local bunkerFillLevel = getXMLFloat(xmlFile, key.."#bunkerFillLevel"); |
253 | if bunkerFillLevel ~= nil then |
254 | self:setBunkerFillLevel(bunkerFillLevel); |
255 | end; |
256 | return true; |
257 | end; |
258 | |
259 | function Bga:getSaveAttributesAndNodes(nodeIdent) |
260 | |
261 | local liquidManureSiloFillLevel = 0; |
262 | if self.liquidManureSiloTrigger ~= nil then |
263 | liquidManureSiloFillLevel = self.liquidManureSiloTrigger.fillLevel; |
264 | end; |
265 | local attributes = 'liquidManureSiloFillLevel="'..liquidManureSiloFillLevel..'" bunkerFillLevel="'..self.bunkerFillLevel..'"'; |
266 | local nodes = ""; |
267 | return attributes, nodes; |
268 | end; |
269 | |
270 | function Bga:update(dt) |
271 | |
272 | if self.isClient then |
273 | if self.liquidManureSiloTrigger ~= nil then |
274 | self.liquidManureSiloTrigger:update(dt); |
275 | end |
276 | |
277 | --self.bunkerPlaneAlpha = math.max(self.bunkerPlaneAlpha - self.bunkerUseSpeed*g_currentMission.missionStats.timeScale*0.001/self.bunkerCapacity * dt * self.bunkerPlaneMoveScale, 0); |
278 | local planeAlpha = math.min(1, self.bunkerFillLevel / self.bunkerCapacity); |
279 | local diff = planeAlpha - self.bunkerPlaneAlpha; |
280 | local movement = math.max(0.01, math.abs(diff)); |
281 | if diff > 0 then |
282 | self.bunkerPlaneAlpha = math.min( planeAlpha, self.bunkerPlaneAlpha + movement*(dt/1000) ); |
283 | elseif diff < 0 then |
284 | self.bunkerPlaneAlpha = math.max( planeAlpha, self.bunkerPlaneAlpha - movement*(dt/1000) ); |
285 | end; |
286 | if self.bunkerPlaneId ~= nil then |
287 | local x, y, z = getTranslation(self.bunkerPlaneId); |
288 | setTranslation(self.bunkerPlaneId, x, self.bunkerPlaneMinY + self.bunkerPlaneAlpha * (self.bunkerPlaneMaxY - self.bunkerPlaneMinY), z); |
289 | setScale(self.bunkerPlaneId, 1, math.max(0.01, self.bunkerPlaneAlpha), 1); |
290 | end; |
291 | |
292 | if self.bunkerPlaneAlpha > 0 then |
293 | for i=1, table.getn(self.bunkerRolls) do |
294 | rotate(self.bunkerRolls[i], 0, self.bunkerRollsSpeed*dt,0); |
295 | end; |
296 | |
297 | if not self.bunkerSoundPlaying and self.bunkerSoundId ~= nil then |
298 | setVisibility(self.bunkerSoundId, true); |
299 | self.bunkerSoundPlaying = true; |
300 | end; |
301 | |
302 | else |
303 | if self.bunkerSoundPlaying then |
304 | setVisibility(self.bunkerSoundId, false); |
305 | self.bunkerSoundPlaying = false; |
306 | end; |
307 | end; |
308 | |
309 | if self.lastMoneyChange > 0 then |
310 | self.lastMoneyChange = self.lastMoneyChange - 1; |
311 | if self.lastMoneyChange == 0 then |
312 | g_currentMission:showMoneyChange(self.moneyChangeId); |
313 | end; |
314 | end; |
315 | end; |
316 | |
317 | end; |
318 | |
319 | function Bga:updateTick(dt) |
320 | if self.isServer then |
321 | self.bunkerUseTime = self.bunkerUseTime + dt; |
322 | if self.bunkerUseTime > 1000 then |
323 | |
324 | if self.bunkerFillLevel > 0 then |
325 | local oldFillLevel = self.bunkerFillLevel; |
326 | |
327 | local bunkerUseSpeed = self.bunkerUseSpeed*g_currentMission.missionStats.timeScale*0.001; |
328 | --if self.bunkerFillLevel < 0.2*self.bunkerCapacity then |
329 | -- bunkerUseSpeed = bunkerUseSpeed * 0.3; |
330 | --end; |
331 | self:setBunkerFillLevel(self.bunkerFillLevel - self.bunkerUseTime * bunkerUseSpeed); |
332 | |
333 | local delta = oldFillLevel-self.bunkerFillLevel; |
334 | |
335 | if delta > 0 and self.liquidManureSiloTrigger ~= nil then |
336 | self.liquidManureSiloTrigger:setFillLevel(self.liquidManureSiloTrigger.fillLevel + delta*self.fillDeltaToManureDeltaScale); |
337 | end; |
338 | end; |
339 | |
340 | self.bunkerUseTime = 0; |
341 | end; |
342 | |
343 | if self.bunkerFillLevel ~= self.sentBunkerFillLevel then |
344 | self:raiseDirtyFlags(self.bgaDirtyFlag); |
345 | |
346 | self.sentBunkerFillLevel = self.bunkerFillLevel; |
347 | end; |
348 | end; |
349 | end; |
350 | |
351 | function Bga:setBunkerFillLevel(fillLevel) |
352 | local oldBunkerFillLevel = self.bunkerFillLevel; |
353 | self.bunkerFillLevel = fillLevel; |
354 | |
355 | if self.bunkerFillLevel > self.bunkerCapacity then |
356 | self.bunkerFillLevel = self.bunkerCapacity; |
357 | end; |
358 | if self.bunkerFillLevel < 0 then |
359 | self.bunkerFillLevel = 0; |
360 | end; |
361 | |
362 | if self.bunkerFillLevel > oldBunkerFillLevel then |
363 | --self.bunkerPlaneAlpha = math.min(self.bunkerPlaneAlpha + (self.bunkerFillLevel - oldBunkerFillLevel)/self.bunkerCapacity, 1); |
364 | self.bunkerPlaneAlpha = math.min(1, self.bunkerFillLevel / self.bunkerCapacity); |
365 | --if self.bunkerPlaneId ~= nil then |
366 | -- local x, y, z = getTranslation(self.bunkerPlaneId); |
367 | -- setTranslation(self.bunkerPlaneId, x, self.bunkerPlaneMinY + self.bunkerPlaneAlpha * (self.bunkerPlaneMaxY - self.bunkerPlaneMinY), z); |
368 | -- setScale(self.bunkerPlaneId, 1, math.max(0.01, self.bunkerPlaneAlpha), 1); |
369 | --end; |
370 | end; |
371 | self:updateDisplayNumbers(self.bunkerFillLevel); |
372 | end; |
373 | |
374 | function Bga:updateDisplayNumbers(fillLevel) |
375 | if self.displayNumbers ~= nil then |
376 | Utils.setNumberShaderByValue(self.displayNumbers, math.floor(fillLevel), 0); |
377 | end; |
378 | end; |
379 | |
380 | function Bga:objectDeleteTriggerCallback(triggerId, otherId, onEnter, onLeave, onStay) |
381 | |
382 | if onEnter then |
383 | -- this happens, if a compound child of a deleted compound is entering |
384 | if otherId ~= 0 then |
385 | local object = g_currentMission:getNodeObject(otherId); |
386 | if object ~= nil then |
387 | if object:isa(Bale) then |
388 | local difficultyMultiplier = 2 ^ (3 - g_currentMission.missionStats.difficulty); |
389 | local value = object:getValue() * difficultyMultiplier; |
390 | g_currentMission:addSharedMoney(value); |
391 | g_currentMission:addMoneyChange(value, FSBaseMission.MONEY_TYPE_SINGLE, true); |
392 | self:setBunkerFillLevel(self.bunkerFillLevel + value * self.baleValueToFillDeltaScale); |
393 | |
394 | object:delete(); |
395 | end; |
396 | end; |
397 | |
398 | end; |
399 | end; |
400 | |
401 | end; |
402 | |
403 | function Bga:getAllowShovelFillType(fillType) |
404 | return fillType == Fillable.FILLTYPE_SILAGE or fillType == Fillable.FILLTYPE_MANURE; |
405 | end; |
406 | |
407 | function Bga:addShovelFillLevel(shovel, fillLevelDelta, fillType) |
408 | if self:getAllowShovelFillType(fillType) then |
409 | local oldFillLevel = self.bunkerFillLevel; |
410 | self:setBunkerFillLevel(self.bunkerFillLevel+fillLevelDelta); |
411 | fillLevelDelta = self.bunkerFillLevel - oldFillLevel; |
412 | |
413 | local difficultyMultiplier = math.max(2 * (3 - g_currentMission.missionStats.difficulty), 1); -- 1 2 4 |
414 | local money = Fillable.fillTypeIndexToDesc[fillType].pricePerLiter * difficultyMultiplier * fillLevelDelta; |
415 | if self.tipTriggers[1] ~= nil then |
416 | if self.tipTriggers[1].priceMultipliers[fillType] ~= nil then |
417 | money = money * self.tipTriggers[1].priceMultipliers[fillType]; |
418 | end; |
419 | |
420 | -- check if a great demand pertaining to this fill type / station is currently running |
421 | local greatDemand = g_currentMission.economyManager:getCurrentGreatDemand(self.tipTriggers[1].stationName, fillType); |
422 | if greatDemand ~= nil then |
423 | money = money * greatDemand.demandMultiplier; |
424 | end; |
425 | end; |
426 | |
427 | g_currentMission:addSharedMoney(money, "harvestIncome"); |
428 | g_currentMission:addMoneyChange(money, self.moneyChangeId); |
429 | self.lastMoneyChange = 30; |
430 | |
431 | return fillLevelDelta; |
432 | end; |
433 | return 0; |
434 | end; |
435 | |
436 | function Bga:liquidManureFillLevelChanged(fillLevel, fillType, fillTrigger) |
437 | if self.isServer then |
438 | self:raiseDirtyFlags(self.bgaDirtyFlag); |
439 | end |
440 | end;
|
Copyright (c) 2008-2015 GIANTS Software GmbH, Confidential, All Rights Reserved.
This document is to be published solely by ls-mods.de