48 | function Baler:load(savegame) |
49 | |
50 | Utils.checkDeprecatedXMLElements(self.xmlFile, self.configFileName, "vehicle.fillScale#value", "vehicle.baler#fillScale") |
51 | Utils.checkDeprecatedXMLElements(self.xmlFile, self.configFileName, "vehicle.baleAnimation", "vehicle.baler.baleAnimation") |
52 | Utils.checkDeprecatedXMLElements(self.xmlFile, self.configFileName, "vehicle.baleAnimation", "vehicle.baler.baleAnimation") |
53 | Utils.checkDeprecatedXMLElements(self.xmlFile, self.configFileName, "vehicle.balerUVScrollParts.balerUVScrollPart", "vehicle.baler.uvScrollParts.uvScrollPart") |
54 | Utils.checkDeprecatedXMLElements(self.xmlFile, self.configFileName, "vehicle.knotingAnimation", "vehicle.baler.knotingAnimation") |
55 | Utils.checkDeprecatedXMLElements(self.xmlFile, self.configFileName, "vehicle.balingAnimation", "vehicle.baler.balingAnimation") |
56 | Utils.checkDeprecatedXMLElements(self.xmlFile, self.configFileName, "vehicle.baleUnloading#allowed", "vehicle.baler.baleUnloading#allowed") |
57 | Utils.checkDeprecatedXMLElements(self.xmlFile, self.configFileName, "vehicle.baleUnloading#time", "vehicle.baler.baleUnloading#time") |
58 | Utils.checkDeprecatedXMLElements(self.xmlFile, self.configFileName, "vehicle.baleUnloading#foldThreshold", "vehicle.baler.baleUnloading#foldThreshold") |
59 | |
60 | self.doCheckSpeedLimit = Utils.overwrittenFunction(self.doCheckSpeedLimit, Baler.doCheckSpeedLimit) |
61 | self.getIsSpeedRotatingPartActive = Utils.overwrittenFunction(self.getIsSpeedRotatingPartActive, Baler.getIsSpeedRotatingPartActive) |
62 | self.allowPickingUp = Utils.overwrittenFunction(self.allowPickingUp, Baler.allowPickingUp) |
63 | self.getIsTurnedOnAllowed = Utils.overwrittenFunction(self.getIsTurnedOnAllowed, Baler.getIsTurnedOnAllowed) |
64 | self.getIsFoldAllowed = Utils.overwrittenFunction(self.getIsFoldAllowed, Baler.getIsFoldAllowed) |
65 | self.setUnitFillLevel = Utils.appendedFunction(self.setUnitFillLevel, Baler.setUnitFillLevel) |
66 | self.isUnloadingAllowed = Baler.isUnloadingAllowed |
67 | self.getTimeFromLevel = Baler.getTimeFromLevel |
68 | self.moveBales = SpecializationUtil.callSpecializationsFunction("moveBales") |
69 | self.moveBale = SpecializationUtil.callSpecializationsFunction("moveBale") |
70 | self.allowFillType = Baler.allowFillType |
71 | self.setIsUnloadingBale = Baler.setIsUnloadingBale |
72 | self.dropBale = Baler.dropBale |
73 | self.createBale = Baler.createBale |
74 | self.setBaleTime = Baler.setBaleTime |
75 | self.processBalerAreas = Baler.processBalerAreas |
76 | |
77 | self.baler = {} |
78 | self.baler.fillScale = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.fillScale#value"), 1) |
79 | self.baler.fillUnitIndex = Utils.getNoNil(getXMLInt(self.xmlFile, "vehicle.baler#fillUnitIndex"), 1) |
80 | self.baler.unloadInfoIndex = Utils.getNoNil(getXMLInt(self.xmlFile, "vehicle.baler#unloadInfoIndex"), 1) |
81 | self.baler.loadInfoIndex = Utils.getNoNil(getXMLInt(self.xmlFile, "vehicle.baler#loadInfoIndex"), 1) |
82 | self.baler.dischargeInfoIndex = Utils.getNoNil(getXMLInt(self.xmlFile, "vehicle.baler#dischargeInfoIndex"), 1) |
83 | |
84 | local firstBaleMarker = getXMLFloat(self.xmlFile, "vehicle.baler.baleAnimation#firstBaleMarker") |
85 | if firstBaleMarker ~= nil then |
86 | local baleAnimCurve = AnimCurve:new(linearInterpolatorN) |
87 | local keyI = 0 |
88 | while true do |
89 | local key = string.format("vehicle.baler.baleAnimation.key(%d)", keyI) |
90 | local t = getXMLFloat(self.xmlFile, key.."#time") |
91 | local x,y,z = Utils.getVectorFromString(getXMLString(self.xmlFile, key.."#pos")) |
92 | if x == nil or y == nil or z == nil then |
93 | break |
94 | end |
95 | local rx, ry, rz = Utils.getVectorFromString(getXMLString(self.xmlFile, key.."#rot")) |
96 | rx = math.rad(Utils.getNoNil(rx, 0)) |
97 | ry = math.rad(Utils.getNoNil(ry, 0)) |
98 | rz = math.rad(Utils.getNoNil(rz, 0)) |
99 | baleAnimCurve:addKeyframe({ v={x, y, z, rx, ry, rz}, time = t}) |
100 | keyI = keyI +1 |
101 | end |
102 | if keyI > 0 then |
103 | self.baler.baleAnimCurve = baleAnimCurve |
104 | self.baler.firstBaleMarker = firstBaleMarker |
105 | end |
106 | end |
107 | self.baler.baleAnimRoot, self.baler.baleAnimRootComponent = Utils.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.baler.baleAnimation#node")) |
108 | if self.baler.baleAnimRoot == nil then |
109 | self.baler.baleAnimRoot = self.components[1].node |
110 | self.baler.baleAnimRootComponent = self.components[1].node |
111 | end |
112 | -- there is no standard bale animation, load the unload animation (for round baler) |
113 | if self.baler.firstBaleMarker == nil then |
114 | local unloadAnimationName = getXMLString(self.xmlFile, "vehicle.baler.baleAnimation#unloadAnimationName") |
115 | local closeAnimationName = getXMLString(self.xmlFile, "vehicle.baler.baleAnimation#closeAnimationName") |
116 | local unloadAnimationSpeed = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.baler.baleAnimation#unloadAnimationSpeed"), 1) |
117 | local closeAnimationSpeed = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.baler.baleAnimation#closeAnimationSpeed"), 1) |
118 | if unloadAnimationName ~= nil and closeAnimationName ~= nil then |
119 | if self.playAnimation ~= nil and self.animations ~= nil then |
120 | if self.animations[unloadAnimationName] ~= nil and self.animations[closeAnimationName] ~= nil then |
121 | --print("has unload animation") |
122 | self.baler.baleUnloadAnimationName = unloadAnimationName |
123 | self.baler.baleUnloadAnimationSpeed = unloadAnimationSpeed |
124 | |
125 | self.baler.baleCloseAnimationName = closeAnimationName |
126 | self.baler.baleCloseAnimationSpeed = closeAnimationSpeed |
127 | |
128 | self.baler.baleDropAnimTime = getXMLFloat(self.xmlFile, "vehicle.baler.baleAnimation#baleDropAnimTime") |
129 | if self.baler.baleDropAnimTime == nil then |
130 | self.baler.baleDropAnimTime = self:getAnimationDuration(self.baler.baleUnloadAnimationName) |
131 | else |
132 | self.baler.baleDropAnimTime = self.baler.baleDropAnimTime * 1000 |
133 | end |
134 | else |
135 | print("Error: Failed to find unload animations '"..unloadAnimationName.."' and '"..closeAnimationName.."' in '"..self.configFileName.."'.") |
136 | end |
137 | else |
138 | print("Error: There is an unload animation in '"..self.configFileName.."' but it is not a AnimatedVehicle. Change to a vehicle type which has the AnimatedVehicle specialization.") |
139 | end |
140 | end |
141 | end |
142 | |
143 | self.baler.baleTypes = {} |
144 | local i = 0 |
145 | while true do |
146 | local key = string.format("vehicle.baler.baleTypes.baleType(%d)", i) |
147 | if not hasXMLProperty(self.xmlFile, key) then |
148 | break |
149 | end |
150 | local isRoundBale = Utils.getNoNil(getXMLBool(self.xmlFile, key.."#isRoundBale"), false) |
151 | local width = Utils.round(Utils.getNoNil(getXMLFloat(self.xmlFile, key.."#width"), 1.2), 2) |
152 | local height = Utils.round(Utils.getNoNil(getXMLFloat(self.xmlFile, key.."#height"), 0.9), 2) |
153 | local length = Utils.round(Utils.getNoNil(getXMLFloat(self.xmlFile, key.."#length"), 2.4), 2) |
154 | local diameter = Utils.round(Utils.getNoNil(getXMLFloat(self.xmlFile, key.."#diameter"), 1.8), 2) |
155 | table.insert(self.baler.baleTypes, {isRoundBale=isRoundBale, width=width, height=height, length=length, diameter=diameter}) |
156 | i = i + 1 |
157 | end |
158 | self.baler.currentBaleTypeId = 1 |
159 | |
160 | if table.getn(self.baler.baleTypes) == 0 then |
161 | self.baler.baleTypes = nil |
162 | end |
163 | |
164 | if self.isClient then |
165 | self.baler.sampleBaler = SoundUtil.loadSample(self.xmlFile, {}, "vehicle.baler.balerSound", nil, self.baseDirectory) |
166 | self.baler.sampleBalerAlarm = SoundUtil.loadSample(self.xmlFile, {}, "vehicle.baler.balerAlarm", nil, self.baseDirectory) |
167 | self.baler.sampleBalerEject = SoundUtil.loadSample(self.xmlFile, {}, "vehicle.baler.balerBaleEject", nil, self.baseDirectory) |
168 | self.baler.sampleBalerDoor = SoundUtil.loadSample(self.xmlFile, {}, "vehicle.baler.balerDoor", nil, self.baseDirectory) |
169 | self.baler.sampleBalerKnotCleaning = SoundUtil.loadSample(self.xmlFile, {}, "vehicle.baler.balerKnotCleaning", nil, self.baseDirectory) |
170 | self.baler.knotCleaningTime = 10000 |
171 | |
172 | self.baler.uvScrollParts = Utils.loadScrollers(self.components, self.xmlFile, "vehicle.baler.uvScrollParts.uvScrollPart", {}, false) |
173 | self.baler.turnedOnRotationNodes = Utils.loadRotationNodes(self.xmlFile, {}, "vehicle.turnedOnRotationNodes.turnedOnRotationNode", "baler", self.components) |
174 | |
175 | self.baler.fillEffects = EffectManager:loadEffect(self.xmlFile, "vehicle.baler.fillEffect", self.components, self) |
176 | |
177 | self.baler.knotingAnimation = getXMLString(self.xmlFile, "vehicle.baler.knotingAnimation#name") |
178 | self.baler.knotingAnimationSpeed = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.knotingAnimation#speed"), 1) |
179 | end |
180 | |
181 | self.baler.balingAnimationName = Utils.getNoNil(getXMLString(self.xmlFile, "vehicle.baler.balingAnimation#name"), "") |
182 | if self.playAnimation == nil or self.getIsAnimationPlaying == nil then |
183 | self.baler.balingAnimationName = "" |
184 | end |
185 | |
186 | self.baler.lastAreaBiggerZero = false |
187 | self.baler.lastAreaBiggerZeroSent = false |
188 | self.baler.lastAreaBiggerZeroTime = 0 |
189 | |
190 | self.baler.unloadingState = Baler.UNLOADING_CLOSED |
191 | self.baler.pickupFillTypes = {} |
192 | |
193 | self.baler.bales = {} |
194 | self.baler.hasBaler = true |
195 | |
196 | self.baler.dummyBale = {} |
197 | self.baler.dummyBale.scaleNode = Utils.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.baler.baleAnimation#scaleNode")) |
198 | self.baler.dummyBale.baleNode = Utils.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.baler.baleAnimation#baleNode")) |
199 | self.baler.dummyBale.currentBaleFillType = FillUtil.FILLTYPE_UNKNOWN |
200 | self.baler.dummyBale.currentBale = nil |
201 | |
202 | self.baler.allowsBaleUnloading = Utils.getNoNil(getXMLBool(self.xmlFile, "vehicle.baler.baleUnloading#allowed"), false) |
203 | self.baler.baleUnloadingTime = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.baler.baleUnloading#time"), 4) * 1000 |
204 | self.baler.baleFoldThreshold = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.baler.baleUnloading#foldThreshold"), 0.25) * self:getCapacity() |
205 | |
206 | self.baler.isBaleUnloading = false |
207 | self.baler.isSpeedLimitActive = false |
208 | |
209 | self.baler.dirtyFlag = self:getNextDirtyFlag() |
210 | end |
215 | function Baler:postLoad(savegame) |
216 | |
217 | for fillType, enabled in pairs(self:getUnitFillTypes(self.baler.fillUnitIndex)) do |
218 | if enabled and fillType ~= FillUtil.FILLTYPE_UNKNOWN then |
219 | if FruitUtil.fillTypeIsWindrow[fillType] then |
220 | table.insert(self.baler.pickupFillTypes, fillType) |
221 | end |
222 | end |
223 | end |
224 | |
225 | if self.isClient then |
226 | self.baler.fillParticleSystems = {} |
227 | self.baler.currentFillParticleSystem = nil |
228 | local i = 0 |
229 | while true do |
230 | local key = string.format("vehicle.baler.fillParticleSystems.emitterShape(%d)", i) |
231 | if not hasXMLProperty(self.xmlFile, key) then |
232 | break |
233 | end |
234 | |
235 | local emitterShape = Utils.indexToObject(self.components, getXMLString(self.xmlFile, key.."#node")) |
236 | local particleType = getXMLString(self.xmlFile, key.."#particleType") |
237 | if emitterShape ~= nil then |
238 | for _, fillType in pairs(self.baler.pickupFillTypes) do |
239 | local particleSystem = MaterialUtil.getParticleSystem(fillType, particleType) |
240 | if particleSystem ~= nil then |
241 | if self.baler.fillParticleSystems[fillType] == nil then |
242 | self.baler.fillParticleSystems[fillType] = {} |
243 | end |
244 | table.insert(self.baler.fillParticleSystems[fillType], ParticleUtil.copyParticleSystem(self.xmlFile, key, particleSystem, emitterShape)) |
245 | end |
246 | end |
247 | end |
248 | i = i + 1 |
249 | end |
250 | end |
251 | |
252 | if savegame ~= nil and not savegame.resetVehicles then |
253 | local numBales = getXMLInt(savegame.xmlFile, savegame.key.."#numBales") |
254 | if numBales ~= nil then |
255 | self.baler.balesToLoad = {} |
256 | for i=1, numBales do |
257 | local baleKey = savegame.key..string.format(".bale(%d)", i-1) |
258 | local bale = {} |
259 | local fillTypeStr = getXMLString(savegame.xmlFile, baleKey.."#fillType") |
260 | local fillType = FillUtil.fillTypeNameToInt[fillTypeStr] |
261 | bale.fillType = fillType |
262 | bale.fillLevel = getXMLFloat(savegame.xmlFile, baleKey.."#fillLevel") |
263 | bale.baleTime = getXMLFloat(savegame.xmlFile, baleKey.."#baleTime") |
264 | table.insert(self.baler.balesToLoad, bale) |
265 | end |
266 | end |
267 | end |
268 | |
269 | if self.baler.firstBaleMarker == nil then |
270 | if self:getUnitFillLevel(self.baler.fillUnitIndex) >= self:getUnitCapacity(self.baler.fillUnitIndex) then |
271 | self.baler.baleToCreate = true; |
272 | end; |
273 | end; |
274 | end |
429 | function Baler:update(dt) |
430 | |
431 | if self.firstTimeRun and self.baler.balesToLoad ~= nil then |
432 | if table.getn(self.baler.balesToLoad) > 0 then |
433 | local v = self.baler.balesToLoad[1]; |
434 | |
435 | if v.targetBaleTime == nil then |
436 | self:createBale(v.fillType, v.fillLevel) |
437 | self:setBaleTime(table.getn(self.baler.bales), 0, true); |
438 | v.targetBaleTime = v.baleTime; |
439 | v.baleTime = 0; |
440 | else |
441 | v.baleTime = math.min(v.baleTime + dt / 1000, v.targetBaleTime); |
442 | self:setBaleTime(table.getn(self.baler.bales), v.baleTime, true); |
443 | |
444 | if v.baleTime == v.targetBaleTime then |
445 | |
446 | local index = table.getn(self.baler.balesToLoad); |
447 | if index == 1 then |
448 | self.baler.balesToLoad = nil; |
449 | else |
450 | table.remove(self.baler.balesToLoad, 1); |
451 | end; |
452 | end; |
453 | end; |
454 | end; |
455 | end |
456 | |
457 | if self.firstTimeRun and self.baler.baleToCreate ~= nil then |
458 | self:createBale(self:getUnitFillType(self.baler.fillUnitIndex), self:getUnitCapacity(self.baler.fillUnitIndex)) |
459 | g_server:broadcastEvent(BalerCreateBaleEvent:new(self, self:getUnitFillType(self.baler.fillUnitIndex), 0), nil, nil, self) |
460 | |
461 | self.baler.baleToCreate = nil; |
462 | end; |
463 | |
464 | if self:getIsActiveForInput() then |
465 | if InputBinding.hasEvent(InputBinding.IMPLEMENT_EXTRA3) then |
466 | if self:isUnloadingAllowed() then |
467 | if self.baler.baleUnloadAnimationName ~= nil or self.baler.allowsBaleUnloading then |
468 | if self.baler.unloadingState == Baler.UNLOADING_CLOSED then |
469 | if table.getn(self.baler.bales) > 0 then |
470 | self:setIsUnloadingBale(true) |
471 | end |
472 | elseif self.baler.unloadingState == Baler.UNLOADING_OPEN then |
473 | if self.baler.baleUnloadAnimationName ~= nil then |
474 | self:setIsUnloadingBale(false) |
475 | end |
476 | end |
477 | end |
478 | end |
479 | end |
480 | end |
481 | |
482 | if self.isClient then |
483 | Utils.updateRotationNodes(self, self.baler.turnedOnRotationNodes, dt, self:getIsActive() and self:getIsTurnedOn() ) |
484 | Utils.updateScrollers(self.baler.uvScrollParts, dt, self:getIsActive() and self:getIsTurnedOn()) |
485 | end |
486 | end |
491 | function Baler:updateTick(dt) |
492 | self.baler.isSpeedLimitActive = false |
493 | |
494 | if self.isServer then |
495 | self.baler.lastAreaBiggerZero = false |
496 | end |
497 | |
498 | if self:getIsActive() then |
499 | if self:getIsTurnedOn() then |
500 | if self:allowPickingUp() then |
501 | if g_currentMission:getCanAddLimitedObject(FSBaseMission.LIMITED_OBJECT_TYPE_BALE) then |
502 | self.baler.isSpeedLimitActive = true |
503 | if self.isServer then |
504 | local workAreas, _, _ = self:getTypedNetworkAreas(WorkArea.AREATYPE_BALER, false) |
505 | local totalLiters = 0 |
506 | local usedFillType = FillUtil.FILLTYPE_UNKNOWN |
507 | if table.getn(workAreas) > 0 then |
508 | totalLiters, usedFillType = self:processBalerAreas(workAreas, self.baler.pickupFillTypes) |
509 | end |
510 | |
511 | if totalLiters > 0 then |
512 | self.baler.lastAreaBiggerZero = true |
513 | if self.baler.lastAreaBiggerZero ~= self.baler.lastAreaBiggerZeroSent then |
514 | self:raiseDirtyFlags(self.baler.dirtyFlag) |
515 | self.baler.lastAreaBiggerZeroSent = self.baler.lastAreaBiggerZero |
516 | end |
517 | |
518 | local deltaLevel = totalLiters * self.baler.fillScale |
519 | |
520 | if self.baler.baleUnloadAnimationName == nil then |
521 | -- move all bales |
522 | local deltaTime = self:getTimeFromLevel(deltaLevel) |
523 | self:moveBales(deltaTime) |
524 | end |
525 | |
526 | local oldFillLevel = self:getUnitFillLevel(self.baler.fillUnitIndex) |
527 | self:setUnitFillLevel(self.baler.fillUnitIndex, oldFillLevel+deltaLevel, usedFillType, true) |
528 | if self:getUnitFillLevel(self.baler.fillUnitIndex) >= self:getUnitCapacity(self.baler.fillUnitIndex) then |
529 | if self.baler.baleTypes ~= nil then |
530 | -- create bale |
531 | if self.baler.baleAnimCurve ~= nil then |
532 | local restDeltaFillLevel = deltaLevel - (self:getUnitFillLevel(self.baler.fillUnitIndex)-oldFillLevel) |
533 | self:setUnitFillLevel(self.baler.fillUnitIndex, restDeltaFillLevel, usedFillType, true) |
534 | |
535 | self:createBale(usedFillType, self:getUnitCapacity(self.baler.fillUnitIndex)) |
536 | |
537 | local numBales = table.getn(self.baler.bales) |
538 | local bale = self.baler.bales[numBales] |
539 | |
540 | self:moveBale(numBales, self:getTimeFromLevel(restDeltaFillLevel), true) |
541 | -- note: self.baler.bales[numBales] can not be accessed anymore since the bale might be dropped already |
542 | g_server:broadcastEvent(BalerCreateBaleEvent:new(self, usedFillType, bale.time), nil, nil, self) |
543 | elseif self.baler.baleUnloadAnimationName ~= nil then |
544 | self:createBale(usedFillType, self:getUnitCapacity(self.baler.fillUnitIndex)) |
545 | g_server:broadcastEvent(BalerCreateBaleEvent:new(self, usedFillType, 0), nil, nil, self) |
546 | end |
547 | end |
548 | end |
549 | end |
550 | end |
551 | else |
552 | g_currentMission:showBlinkingWarning(g_i18n:getText("warning_tooManyBales"), 500) |
553 | end |
554 | end |
555 | |
556 | |
557 | if self.baler.lastAreaBiggerZero and self.fillUnits[self.baler.fillUnitIndex].lastValidFillType ~= FillUtil.FILLTYPE_UNKNOWN then |
558 | self.baler.lastAreaBiggerZeroTime = 500 |
559 | else |
560 | if self.baler.lastAreaBiggerZeroTime > 0 then |
561 | self.baler.lastAreaBiggerZeroTime = self.baler.lastAreaBiggerZeroTime - dt |
562 | end |
563 | end |
564 | |
565 | if self.isClient then |
566 | if self.baler.fillEffects ~= nil then |
567 | if self.baler.lastAreaBiggerZeroTime > 0 then |
568 | EffectManager:setFillType(self.baler.fillEffects, self.fillUnits[self.baler.fillUnitIndex].lastValidFillType) |
569 | EffectManager:startEffects(self.baler.fillEffects) |
570 | else |
571 | EffectManager:stopEffects(self.baler.fillEffects) |
572 | end |
573 | end |
574 | |
575 | local currentFillParticleSystem = self.baler.fillParticleSystems[self.fillUnits[self.baler.fillUnitIndex].lastValidFillType] |
576 | if currentFillParticleSystem ~= self.baler.currentFillParticleSystem then |
577 | if self.baler.currentFillParticleSystem ~= nil then |
578 | for _, ps in pairs(self.baler.currentFillParticleSystem) do |
579 | ParticleUtil.setEmittingState(ps, false) |
580 | end |
581 | self.baler.currentFillParticleSystem = nil |
582 | end |
583 | self.baler.currentFillParticleSystem = currentFillParticleSystem |
584 | end |
585 | |
586 | if self.baler.currentFillParticleSystem ~= nil then |
587 | for _, ps in pairs(self.baler.currentFillParticleSystem) do |
588 | ParticleUtil.setEmittingState(ps, self.baler.lastAreaBiggerZeroTime > 0) |
589 | end |
590 | end |
591 | |
592 | if self:getIsActiveForSound() then |
593 | if self.baler.knotCleaningTime <= g_currentMission.time then |
594 | SoundUtil.playSample(self.baler.sampleBalerKnotCleaning, 1, 0, nil) |
595 | self.baler.knotCleaningTime = g_currentMission.time + 120000 |
596 | end |
597 | SoundUtil.playSample(self.baler.sampleBaler, 0, 0, nil) |
598 | end |
599 | end |
600 | else |
601 | if self.baler.isBaleUnloading and self.isServer then |
602 | local deltaTime = dt / self.baler.baleUnloadingTime |
603 | self:moveBales(deltaTime) |
604 | end |
605 | end |
606 | |
607 | if self.isClient then |
608 | if not self:getIsTurnedOn() then |
609 | SoundUtil.stopSample(self.baler.sampleBalerKnotCleaning) |
610 | SoundUtil.stopSample(self.baler.sampleBaler) |
611 | end |
612 | |
613 | if self:getIsTurnedOn() and self:getUnitFillLevel(self.baler.fillUnitIndex) > (self:getUnitCapacity(self.baler.fillUnitIndex) * 0.68) and self:getUnitFillLevel(self.baler.fillUnitIndex) < self:getUnitCapacity(self.baler.fillUnitIndex) then |
614 | -- start alarm sound |
615 | if self:getIsActiveForSound() then |
616 | SoundUtil.playSample(self.baler.sampleBalerAlarm, 0, 0, nil) |
617 | end |
618 | else |
619 | SoundUtil.stopSample(self.baler.sampleBalerAlarm) |
620 | end |
621 | |
622 | --delete dummy bale on client after physical bale is displayed |
623 | if self.baler.unloadingState == Baler.UNLOADING_OPEN then |
624 | if getNumOfChildren(self.baler.baleAnimRoot) > 0 then |
625 | delete(getChildAt(self.baler.baleAnimRoot, 0)); |
626 | end; |
627 | end; |
628 | end; |
629 | |
630 | if self.baler.unloadingState == Baler.UNLOADING_OPENING then |
631 | local isPlaying = self:getIsAnimationPlaying(self.baler.baleUnloadAnimationName) |
632 | local animTime = self:getRealAnimationTime(self.baler.baleUnloadAnimationName) |
633 | if not isPlaying or animTime >= self.baler.baleDropAnimTime then |
634 | if table.getn(self.baler.bales) > 0 then |
635 | self:dropBale(1) |
636 | if self.isServer then |
637 | self:setUnitFillLevel(self.baler.fillUnitIndex, 0, self:getUnitFillType(self.baler.fillUnitIndex), true) |
638 | end |
639 | end |
640 | if not isPlaying then |
641 | self.baler.unloadingState = Baler.UNLOADING_OPEN |
642 | |
643 | if self.isClient then |
644 | SoundUtil.stopSample(self.baler.sampleBalerEject) |
645 | SoundUtil.stopSample(self.baler.sampleBalerDoor) |
646 | end |
647 | end |
648 | end |
649 | elseif self.baler.unloadingState == Baler.UNLOADING_CLOSING then |
650 | if not self:getIsAnimationPlaying(self.baler.baleCloseAnimationName) then |
651 | self.baler.unloadingState = Baler.UNLOADING_CLOSED |
652 | if self.isClient then |
653 | SoundUtil.stopSample(self.baler.sampleBalerDoor) |
654 | end |
655 | end |
656 | end |
657 | end |
658 | end |
947 | function Baler:createBale(baleFillType, fillLevel) |
948 | |
949 | if self.baler.knotingAnimation ~= nil then |
950 | self:playAnimation(self.baler.knotingAnimation, self.baler.knotingAnimationSpeed, nil, true) |
951 | end |
952 | |
953 | if self.baler.dummyBale.currentBale ~= nil then |
954 | delete(self.baler.dummyBale.currentBale) |
955 | self.baler.dummyBale.currentBale = nil |
956 | end |
957 | |
958 | local t = self.baler.baleTypes[self.baler.currentBaleTypeId] |
959 | local baleType = BaleUtil.getBale(baleFillType, t.width, t.height, t.length, t.diameter, t.isRoundBale) |
960 | |
961 | local bale = {} |
962 | bale.filename = Utils.getFilename(baleType.filename, self.baseDirectory) |
963 | bale.time = 0 |
964 | bale.fillType = baleFillType |
965 | bale.fillLevel = fillLevel |
966 | |
967 | if self.baler.baleUnloadAnimationName ~= nil then |
968 | local baleRoot = Utils.loadSharedI3DFile(baleType.filename, self.baseDirectory, false, false) |
969 | local baleId = getChildAt(baleRoot, 0) |
970 | link(self.baler.baleAnimRoot, baleId) |
971 | delete(baleRoot) |
972 | bale.id = baleId |
973 | end |
974 | |
975 | if self.isServer and self.baler.baleUnloadAnimationName == nil then |
976 | local x,y,z = getWorldTranslation(self.baler.baleAnimRoot) |
977 | local rx,ry,rz = getWorldRotation(self.baler.baleAnimRoot) |
978 | |
979 | local baleObject = Bale:new(self.isServer, self.isClient) |
980 | baleObject:load(bale.filename, x,y,z,rx,ry,rz, bale.fillLevel) |
981 | baleObject:register() |
982 | baleObject:setCanBeSold(false) |
983 | |
984 | local baleJointNode = createTransformGroup("BaleJointTG") |
985 | link(self.baler.baleAnimRoot, baleJointNode) |
986 | setTranslation(baleJointNode, 0,0,0) |
987 | setRotation(baleJointNode, 0,0,0) |
988 | |
989 | local constr = JointConstructor:new() |
990 | constr:setActors(self.baler.baleAnimRootComponent, baleObject.nodeId) |
991 | constr:setJointTransforms(baleJointNode, baleObject.nodeId) |
992 | for i=1, 3 do |
993 | constr:setRotationLimit(i-1, 0, 0) |
994 | constr:setTranslationLimit(i-1, true, 0, 0) |
995 | end |
996 | constr:setEnableCollision(false) |
997 | local baleJointIndex = constr:finalize() |
998 | |
999 | g_currentMission:removeItemToSave(baleObject) |
1000 | |
1001 | bale.baleJointNode = baleJointNode |
1002 | bale.baleJointIndex = baleJointIndex |
1003 | bale.baleObject = baleObject |
1004 | end |
1005 | |
1006 | table.insert(self.baler.bales, bale) |
1007 | end |
1134 | function Baler:processBalerAreas(workAreas, fillTypes) |
1135 | |
1136 | local totalLiters = 0 |
1137 | local usedFillType = FillUtil.FILLTYPE_UNKNOWN |
1138 | |
1139 | local numAreas = table.getn(workAreas) |
1140 | for i=1, numAreas do |
1141 | local x0 = workAreas[i][1] |
1142 | local z0 = workAreas[i][2] |
1143 | local x1 = workAreas[i][3] |
1144 | local z1 = workAreas[i][4] |
1145 | local x2 = workAreas[i][5] |
1146 | local z2 = workAreas[i][6] |
1147 | |
1148 | local hx = x2 - x0 |
1149 | local hz = z2 - z0 |
1150 | local hLength = Utils.vector2Length(hx, hz) |
1151 | local hLength_2 = 0.5 * hLength |
1152 | |
1153 | local wx = x1 - x0 |
1154 | local wz = z1 - z0 |
1155 | local wLength = Utils.vector2Length(wx, wz) |
1156 | |
1157 | local sx = x0 + (hx * 0.5) + ((wx/wLength)*hLength_2) |
1158 | local sz = z0 + (hz * 0.5) + ((wz/wLength)*hLength_2) |
1159 | |
1160 | local ex = x1 + (hx * 0.5) - ((wx/wLength)*hLength_2) |
1161 | local ez = z1 + (hz * 0.5) - ((wz/wLength)*hLength_2) |
1162 | |
1163 | local sy = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, sx,0,sz) |
1164 | local ey = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, ex,0,ez) |
1165 | |
1166 | |
1167 | if usedFillType == FillUtil.FILLTYPE_UNKNOWN then |
1168 | for _, fillType in pairs(fillTypes) do |
1169 | local liters = -TipUtil.tipToGroundAroundLine(self, -math.huge, fillType, sx,sy,sz, ex,ey,ez, hLength_2, nil, nil, false, nil) |
1170 | if liters > 0 then |
1171 | usedFillType = fillType |
1172 | totalLiters = totalLiters + liters |
1173 | break |
1174 | end |
1175 | end |
1176 | else |
1177 | totalLiters = totalLiters - TipUtil.tipToGroundAroundLine(self, -math.huge, usedFillType, sx,sy,sz, ex,ey,ez, hLength_2, nil, nil, false, nil) |
1178 | end |
1179 | |
1180 | end |
1181 | |
1182 | return totalLiters, usedFillType |
1183 | end |