Copyright (c) 2008-2015 GIANTS Software GmbH, Confidential, All Rights Reserved.
This document is to be published solely by ls-mods.de
1 | -- |
2 | -- BaleWrapper |
3 | -- Class for all BaleWrappers |
4 | -- |
5 | -- @author Manuel Leithner |
6 | -- @date 12/09/12 |
7 | -- |
8 | -- Copyright (C) GIANTS Software GmbH, Confidential, All Rights Reserved. |
9 | |
10 | source("dataS/scripts/vehicles/specializations/BaleWrapperStateEvent.lua"); |
11 | BaleWrapper = {}; |
12 | |
13 | -- sorted wrapping states |
14 | BaleWrapper.STATE_NONE = 0; |
15 | BaleWrapper.STATE_MOVING_BALE_TO_WRAPPER = 1; |
16 | BaleWrapper.STATE_MOVING_GRABBER_TO_WORK = 2; |
17 | BaleWrapper.STATE_WRAPPER_WRAPPING_BALE = 3; |
18 | BaleWrapper.STATE_WRAPPER_FINSIHED = 4; |
19 | BaleWrapper.STATE_WRAPPER_DROPPING_BALE = 5; |
20 | BaleWrapper.STATE_WRAPPER_RESETTING_PLATFORM = 6; |
21 | |
22 | BaleWrapper.STATE_NUM_BITS = 3; |
23 | |
24 | -- wrapping events |
25 | BaleWrapper.CHANGE_GRAB_BALE = 1; |
26 | BaleWrapper.CHANGE_DROP_BALE_AT_GRABBER = 2; |
27 | BaleWrapper.CHANGE_WRAPPING_START = 3; |
28 | BaleWrapper.CHANGE_WRAPPING_BALE_FINSIHED = 4; |
29 | BaleWrapper.CHANGE_WRAPPER_START_DROP_BALE = 5; |
30 | BaleWrapper.CHANGE_WRAPPER_BALE_DROPPED = 6; |
31 | BaleWrapper.CHANGE_WRAPPER_PLATFORM_RESET = 7; |
32 | -- Button-Event to drop bale from grabber |
33 | BaleWrapper.CHANGE_BUTTON_EMPTY = 8; |
34 | |
35 | function BaleWrapper.prerequisitesPresent(specializations) |
36 | return SpecializationUtil.hasSpecialization(AnimatedVehicle, specializations); |
37 | end; |
38 | |
39 | function BaleWrapper:load(xmlFile) |
40 | |
41 | self.getIsFoldAllowed = Utils.overwrittenFunction(self.getIsFoldAllowed, BaleWrapper.getIsFoldAllowed); |
42 | self.allowsGrabbingBale = BaleWrapper.allowsGrabbingBale; |
43 | self.pickupWrapperBale = BaleWrapper.pickupWrapperBale; |
44 | self.getWrapperBaleType = BaleWrapper.getWrapperBaleType; |
45 | self.updateWrappingState = BaleWrapper.updateWrappingState; |
46 | self.dropBaleFromWrapper = BaleWrapper.dropBaleFromWrapper; |
47 | self.moveBaleToWrapper = BaleWrapper.moveBaleToWrapper; |
48 | self.doStateChange = BaleWrapper.doStateChange; |
49 | self.updateWrapNodes = BaleWrapper.updateWrapNodes; |
50 | |
51 | local wrapper = {}; |
52 | wrapper.baleNode = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.wrapper#baleIndex")); |
53 | wrapper.wrapperNode = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.wrapper#index")); |
54 | local wrappingAnimCurve = AnimCurve:new(linearInterpolatorN); |
55 | local keyI = 0; |
56 | while true do |
57 | local key = string.format("vehicle.wrapper.key(%d)", keyI); |
58 | local t = getXMLFloat(xmlFile, key.."#time"); |
59 | local baleX, baleY, baleZ = Utils.getVectorFromString(getXMLString(xmlFile, key.."#baleRot")); |
60 | if baleX == nil or baleY == nil or baleZ == nil then |
61 | break; |
62 | end; |
63 | baleX = math.rad(Utils.getNoNil(baleX, 0)); |
64 | baleY = math.rad(Utils.getNoNil(baleY, 0)); |
65 | baleZ = math.rad(Utils.getNoNil(baleZ, 0)); |
66 | local wrapperX, wrapperY, wrapperZ = Utils.getVectorFromString(getXMLString(xmlFile, key.."#wrapperRot")); |
67 | wrapperX = math.rad(Utils.getNoNil(wrapperX, 0)); |
68 | wrapperY = math.rad(Utils.getNoNil(wrapperY, 0)); |
69 | wrapperZ = math.rad(Utils.getNoNil(wrapperZ, 0)); |
70 | wrappingAnimCurve:addKeyframe({ v={baleX, baleY, baleZ, wrapperX, wrapperY, wrapperZ}, time = t}); |
71 | keyI = keyI +1; |
72 | end; |
73 | wrapper.animCurve = wrappingAnimCurve; |
74 | wrapper.animTime = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.wrapper#wrappingTime"), 5) * 1000; |
75 | wrapper.currentTime = 0; |
76 | wrapper.currentBale = nil; |
77 | wrapper.foldMinLimit = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.wrapper#foldMinLimit"), 0); |
78 | wrapper.foldMaxLimit = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.wrapper#foldMaxLimit"), 1); |
79 | self.wrapper = wrapper; |
80 | |
81 | self.showInvalidBaleWarning = false; |
82 | self.allowedBaleTypes = {}; |
83 | local i =0; |
84 | while true do |
85 | local key = string.format("vehicle.wrapper.baleType(%d)", i); |
86 | if not hasXMLProperty(xmlFile, key) then |
87 | break; |
88 | end |
89 | local wrapperBaleFilename = Utils.getFilename(getXMLString(xmlFile, key.."#wrapperBaleFilename"), self.baseDirectory); |
90 | local fillTypeStr = getXMLString(xmlFile, key.."#fillType"); |
91 | if fillTypeStr ~= nil then |
92 | local fillType = Fillable.fillTypeNameToInt[fillTypeStr]; |
93 | |
94 | local minBaleDiameter = getXMLFloat(xmlFile, key.."#minBaleDiameter"); |
95 | local maxBaleDiameter = getXMLFloat(xmlFile, key.."#maxBaleDiameter"); |
96 | local minBaleWidth = getXMLFloat(xmlFile, key.."#minBaleWidth"); |
97 | local maxBaleWidth = getXMLFloat(xmlFile, key.."#maxBaleWidth"); |
98 | if wrapperBaleFilename ~= nil and minBaleDiameter ~= nil and maxBaleDiameter ~= nil and minBaleWidth ~= nil and maxBaleWidth ~= nil and fillType ~= nil then |
99 | if self.allowedBaleTypes[fillType] == nil then |
100 | self.allowedBaleTypes[fillType] = {}; |
101 | end |
102 | table.insert(self.allowedBaleTypes[fillType], {wrapperBaleFilename=wrapperBaleFilename,minBaleDiameter=minBaleDiameter,maxBaleDiameter=maxBaleDiameter, minBaleWidth=minBaleWidth, maxBaleWidth=maxBaleWidth, fillType=fillType}); |
103 | end |
104 | end |
105 | i = i + 1; |
106 | end |
107 | |
108 | local wrapAnimNodes = {}; |
109 | local wrapAnimNodeI = 0; |
110 | while true do |
111 | local wrapAnimNodeKey = string.format("vehicle.wrapper.wrapAnimNode(%d)", wrapAnimNodeI); |
112 | if not hasXMLProperty(xmlFile, wrapAnimNodeKey) then |
113 | break; |
114 | end |
115 | local nodeId = Utils.indexToObject(self.components, getXMLString(xmlFile, wrapAnimNodeKey.."#index")); |
116 | if nodeId ~= nil then |
117 | local animCurve = AnimCurve:new(linearInterpolatorN); |
118 | local keyI = 0; |
119 | while true do |
120 | local key = string.format(wrapAnimNodeKey..".key(%d)", keyI); |
121 | local wrapperRot = getXMLFloat(xmlFile, key.."#wrapperRot"); |
122 | if wrapperRot == nil then |
123 | break; |
124 | end; |
125 | local x, y, z = Utils.getVectorFromString(getXMLString(xmlFile, key.."#trans")); |
126 | local rx, ry, rz = Utils.getVectorFromString(getXMLString(xmlFile, key.."#rot")); |
127 | local sx, sy, sz = Utils.getVectorFromString(getXMLString(xmlFile, key.."#scale")); |
128 | |
129 | x = Utils.getNoNil(x, 0); |
130 | y = Utils.getNoNil(y, 0); |
131 | z = Utils.getNoNil(z, 0); |
132 | rx = math.rad(Utils.getNoNil(rx, 0)); |
133 | ry = math.rad(Utils.getNoNil(ry, 0)); |
134 | rz = math.rad(Utils.getNoNil(rz, 0)); |
135 | sx = Utils.getNoNil(sx, 1); |
136 | sy = Utils.getNoNil(sy, 1); |
137 | sz = Utils.getNoNil(sz, 1); |
138 | animCurve:addKeyframe({ v={x, y, z, rx, ry, rz, sx,sy,sz}, time = math.rad(wrapperRot)}); |
139 | keyI = keyI +1; |
140 | end; |
141 | if keyI > 0 then |
142 | local repeatWrapperRot = Utils.getNoNil(getXMLBool(xmlFile, wrapAnimNodeKey.."#repeatWrapperRot"), false); |
143 | table.insert(wrapAnimNodes, {nodeId=nodeId, animCurve=animCurve, repeatWrapperRot=repeatWrapperRot}); |
144 | end |
145 | end |
146 | wrapAnimNodeI = wrapAnimNodeI + 1; |
147 | end |
148 | self.wrapAnimNodes = wrapAnimNodes; |
149 | |
150 | local wrapNodes = {}; |
151 | local wrapNodeI = 0; |
152 | while true do |
153 | local wrapNodeKey = string.format("vehicle.wrapper.wrapNode(%d)", wrapNodeI); |
154 | if not hasXMLProperty(xmlFile, wrapNodeKey) then |
155 | break; |
156 | end |
157 | local nodeId = Utils.indexToObject(self.components, getXMLString(xmlFile, wrapNodeKey.."#index")); |
158 | local wrapVisibility = Utils.getNoNil(getXMLBool(xmlFile, wrapNodeKey.."#wrapVisibility"), false); |
159 | local emptyVisibility = Utils.getNoNil(getXMLBool(xmlFile, wrapNodeKey.."#emptyVisibility"), false); |
160 | if nodeId ~= nil and (wrapVisibility or emptyVisibility) then |
161 | local maxWrapperRot = getXMLFloat(xmlFile, wrapNodeKey.."#maxWrapperRot"); |
162 | if maxWrapperRot == nil then |
163 | maxWrapperRot = math.huge; |
164 | else |
165 | maxWrapperRot = math.rad(maxWrapperRot); |
166 | end |
167 | table.insert(wrapNodes, {nodeId=nodeId, wrapVisibility=wrapVisibility, emptyVisibility=emptyVisibility, maxWrapperRot=maxWrapperRot}); |
168 | end |
169 | wrapNodeI = wrapNodeI + 1; |
170 | end |
171 | self.wrapNodes = wrapNodes; |
172 | self:updateWrapNodes(false, true, 0); |
173 | |
174 | |
175 | self.baleGrabber = {}; |
176 | self.baleGrabber.grabNode = Utils.indexToObject(self.components, getXMLString(xmlFile, "vehicle.baleGrabber#index")); |
177 | self.baleGrabber.nearestDistance = Utils.getNoNil(getXMLFloat(xmlFile, "vehicle.baleGrabber#nearestDistance"), 3.0); |
178 | |
179 | if self.isClient then |
180 | self.wrapperSound = Utils.loadSample(xmlFile, {}, "vehicle.wrapperSound", nil, self.baseDirectory); |
181 | self.wrapperStartSound = Utils.loadSample(xmlFile, {}, "vehicle.wrapperStartSound", nil, self.baseDirectory); |
182 | self.wrapperStopSound = Utils.loadSample(xmlFile, {}, "vehicle.wrapperStopSound", nil, self.baseDirectory); |
183 | end |
184 | |
185 | self.baleToLoad = nil; |
186 | self.baleToMount = nil; |
187 | self.baleWrapperState = BaleWrapper.STATE_NONE; |
188 | self.grabberIsMoving = false; |
189 | self.hasBaleWrapper = true; |
190 | |
191 | end; |
192 | |
193 | function BaleWrapper:delete() |
194 | if self.isClient then |
195 | Utils.deleteSample(self.wrapperSound); |
196 | Utils.deleteSample(self.wrapperStartSound); |
197 | Utils.deleteSample(self.wrapperStopSound); |
198 | end |
199 | end; |
200 | |
201 | function BaleWrapper:readStream(streamId, connection) |
202 | if connection:getIsServer() then |
203 | local wrapperState = streamReadUIntN(streamId, BaleWrapper.STATE_NUM_BITS); |
204 | if wrapperState >= BaleWrapper.STATE_MOVING_BALE_TO_WRAPPER then |
205 | local baleServerId; |
206 | if wrapperState ~= BaleWrapper.STATE_WRAPPER_RESETTING_PLATFORM then |
207 | baleServerId = streamReadInt32(streamId); |
208 | end |
209 | |
210 | if wrapperState == BaleWrapper.STATE_MOVING_BALE_TO_WRAPPER then |
211 | self:doStateChange(BaleWrapper.CHANGE_GRAB_BALE, baleServerId); |
212 | AnimatedVehicle.updateAnimations(self, 99999999); |
213 | elseif wrapperState == BaleWrapper.STATE_MOVING_GRABBER_TO_WORK then |
214 | self.baleGrabber.currentBale = baleServerId; |
215 | |
216 | self:doStateChange(BaleWrapper.CHANGE_DROP_BALE_AT_GRABBER); |
217 | AnimatedVehicle.updateAnimations(self, 99999999); |
218 | |
219 | elseif wrapperState ~= BaleWrapper.STATE_WRAPPER_RESETTING_PLATFORM then |
220 | |
221 | local attachNode = self.wrapper.baleNode; |
222 | self.baleToMount = {serverId=baleServerId, linkNode=attachNode, trans={0,0,0}, rot={0,0,0} }; |
223 | self:updateWrapNodes(true, false, 0); |
224 | self.wrapper.currentBale = baleServerId; |
225 | |
226 | if wrapperState == BaleWrapper.STATE_WRAPPER_WRAPPING_BALE then |
227 | local wrapperTime = streamReadFloat32(streamId); |
228 | self.wrapper.currentTime = wrapperTime; |
229 | self:updateWrappingState(self.wrapper.currentTime / self.wrapper.animTime, true); |
230 | else |
231 | self.wrapper.currentTime = self.wrapper.animTime; |
232 | self:updateWrappingState(1, true); |
233 | |
234 | self:doStateChange(BaleWrapper.CHANGE_WRAPPING_BALE_FINSIHED); |
235 | AnimatedVehicle.updateAnimations(self, 99999999); |
236 | if wrapperState >= BaleWrapper.STATE_WRAPPER_DROPPING_BALE then |
237 | self:doStateChange(BaleWrapper.CHANGE_WRAPPER_START_DROP_BALE); |
238 | AnimatedVehicle.updateAnimations(self, 99999999); |
239 | end |
240 | end |
241 | else |
242 | -- simply set the state but do nothing else |
243 | self.baleWrapperState = BaleWrapper.STATE_WRAPPER_RESETTING_PLATFORM; |
244 | end |
245 | end |
246 | end |
247 | end; |
248 | |
249 | function BaleWrapper:writeStream(streamId, connection) |
250 | if not connection:getIsServer() then |
251 | local wrapperState = self.baleWrapperState; |
252 | streamWriteUIntN(streamId, wrapperState, BaleWrapper.STATE_NUM_BITS); |
253 | |
254 | if wrapperState >= BaleWrapper.STATE_MOVING_BALE_TO_WRAPPER and wrapperState ~= BaleWrapper.STATE_WRAPPER_RESETTING_PLATFORM then |
255 | if wrapperState == BaleWrapper.STATE_MOVING_BALE_TO_WRAPPER then |
256 | streamWriteInt32(streamId, self.baleGrabber.currentBale); |
257 | else |
258 | streamWriteInt32(streamId, self.wrapper.currentBale); |
259 | end |
260 | end |
261 | if wrapperState == BaleWrapper.STATE_WRAPPER_WRAPPING_BALE then |
262 | streamWriteFloat32(streamId, self.wrapper.currentTime); |
263 | end |
264 | end |
265 | end; |
266 | |
267 | function BaleWrapper:mouseEvent(posX, posY, isDown, isUp, button) |
268 | end; |
269 | |
270 | function BaleWrapper:keyEvent(unicode, sym, modifier, isDown) |
271 | end; |
272 | |
273 | function BaleWrapper:update(dt) |
274 | |
275 | if self.firstTimeRun then |
276 | if self.baleToLoad ~= nil then |
277 | local v = self.baleToLoad; |
278 | self.baleToLoad = nil; |
279 | local baleObject = Bale:new(self.isServer, self.isClient); |
280 | local x,y,z = unpack(v.translation); |
281 | local rx,ry,rz = unpack(v.rotation); |
282 | baleObject:load(v.filename, x,y,z,rx,ry,rz, v.fillLevel); |
283 | if baleObject.nodeId ~= nil and baleObject.nodeId ~= 0 then |
284 | baleObject.baleValueScale = v.baleValueScale; |
285 | local wrapperState = math.min(v.wrapperTime / self.wrapper.animTime, 1); |
286 | baleObject:setWrappingState(wrapperState); |
287 | baleObject:mount(self, v.parentNode, x,y,z, rx,ry,rz) |
288 | baleObject:register(); |
289 | self:doStateChange(BaleWrapper.CHANGE_WRAPPING_START); |
290 | self.wrapper.currentBale = networkGetObjectId(baleObject); |
291 | self.wrapper.currentTime = v.wrapperTime; |
292 | self:updateWrappingState(self.wrapper.currentTime / self.wrapper.animTime); |
293 | end; |
294 | end; |
295 | |
296 | if self.baleToMount ~= nil then |
297 | local bale = networkGetObject(self.baleToMount.serverId); |
298 | if bale ~= nil then |
299 | local x,y,z = unpack(self.baleToMount.trans); |
300 | local rx,ry,rz = unpack(self.baleToMount.rot); |
301 | bale:mount(self, self.baleToMount.linkNode, x,y,z, rx,ry,rz); |
302 | self.baleToMount = nil; |
303 | end; |
304 | end; |
305 | end; |
306 | |
307 | if self:getIsActive() then |
308 | if self:getIsActiveForInput() then |
309 | if self.baleWrapperState == BaleWrapper.STATE_WRAPPER_FINSIHED then |
310 | if InputBinding.hasEvent(InputBinding.IMPLEMENT_EXTRA3) then |
311 | g_client:getServerConnection():sendEvent(BaleWrapperStateEvent:new(self, BaleWrapper.CHANGE_BUTTON_EMPTY)); |
312 | end; |
313 | end; |
314 | end; |
315 | |
316 | if self.baleWrapperState == BaleWrapper.STATE_WRAPPER_WRAPPING_BALE then |
317 | if self.isClient and self:getIsActiveForSound() then |
318 | if not self.wrapperSound.isPlaying then |
319 | if self.wrapperStartSound.sample == nil or not isSamplePlaying(self.wrapperStartSound.sample) or getSamplePlayOffset(self.wrapperStartSound.sample) >= self.wrapperStartSound.duration-1.8*dt then |
320 | Utils.playSample(self.wrapperSound, 0, 0); |
321 | end |
322 | end |
323 | end |
324 | self.wrapper.currentTime = self.wrapper.currentTime + dt; |
325 | self:updateWrappingState(self.wrapper.currentTime / self.wrapper.animTime); |
326 | end; |
327 | end |
328 | end; |
329 | |
330 | function BaleWrapper:updateTick(dt) |
331 | if self:getIsActive() then |
332 | self.showInvalidBaleWarning = false; |
333 | if self:allowsGrabbingBale() then |
334 | if self.baleGrabber.grabNode ~= nil and self.baleGrabber.currentBale == nil then |
335 | -- find nearest bale |
336 | local nearestBale, nearestBaleType = BaleWrapper.getBaleInRange(self, self.baleGrabber.grabNode); |
337 | if nearestBale ~= nil then |
338 | if nearestBaleType == nil then |
339 | self.showInvalidBaleWarning = true; |
340 | elseif self.isServer then |
341 | self:pickupWrapperBale(nearestBale, nearestBaleType); |
342 | end |
343 | end; |
344 | end; |
345 | end; |
346 | if self.isServer then |
347 | |
348 | if self.baleWrapperState ~= BaleWrapper.STATE_NONE then |
349 | if self.baleWrapperState == BaleWrapper.STATE_MOVING_BALE_TO_WRAPPER then |
350 | if not self:getIsAnimationPlaying("moveBaleToWrapper") then |
351 | g_server:broadcastEvent(BaleWrapperStateEvent:new(self, BaleWrapper.CHANGE_DROP_BALE_AT_GRABBER), true, nil, self); |
352 | end; |
353 | elseif self.baleWrapperState == BaleWrapper.STATE_MOVING_GRABBER_TO_WORK then |
354 | if not self:getIsAnimationPlaying("moveBaleToWrapper") then |
355 | local bale = networkGetObject(self.wrapper.currentBale); |
356 | if bale ~= nil and not bale.supportsWrapping then |
357 | g_server:broadcastEvent(BaleWrapperStateEvent:new(self, BaleWrapper.CHANGE_WRAPPER_START_DROP_BALE), true, nil, self); |
358 | else |
359 | g_server:broadcastEvent(BaleWrapperStateEvent:new(self, BaleWrapper.CHANGE_WRAPPING_START), true, nil, self); |
360 | end; |
361 | end; |
362 | elseif self.baleWrapperState == BaleWrapper.STATE_WRAPPER_DROPPING_BALE then |
363 | if not self:getIsAnimationPlaying("dropBaleFromWrapper") then |
364 | g_server:broadcastEvent(BaleWrapperStateEvent:new(self, BaleWrapper.CHANGE_WRAPPER_BALE_DROPPED), true, nil, self); |
365 | end; |
366 | elseif self.baleWrapperState == BaleWrapper.STATE_WRAPPER_RESETTING_PLATFORM then |
367 | if not self:getIsAnimationPlaying("resetWrapperAfterBaleDrop") then |
368 | g_server:broadcastEvent(BaleWrapperStateEvent:new(self, BaleWrapper.CHANGE_WRAPPER_PLATFORM_RESET), true, nil, self); |
369 | end; |
370 | end; |
371 | end; |
372 | end; |
373 | end; |
374 | end; |
375 | |
376 | function BaleWrapper:draw() |
377 | if self.isClient then |
378 | if self:getIsActiveForInput(true) then |
379 | |
380 | if self.baleWrapperState == BaleWrapper.STATE_WRAPPER_FINSIHED then |
381 | g_currentMission:addHelpButtonText(g_i18n:getText("BaleWrapper_unload"), InputBinding.IMPLEMENT_EXTRA3); |
382 | end; |
383 | end |
384 | |
385 | if self.showInvalidBaleWarning then |
386 | g_currentMission:showBlinkingWarning(g_i18n:getText("InvalidBaleWarning")); |
387 | end |
388 | end; |
389 | end; |
390 | |
391 | |
392 | function BaleWrapper:loadFromAttributesAndNodes(xmlFile, key, resetVehicles) |
393 | |
394 | if not resetVehicles then |
395 | local filename = getXMLString(xmlFile, key.."#baleFileName"); |
396 | if filename ~= nil then |
397 | filename = Utils.convertFromNetworkFilename(filename); |
398 | local wrapperTime = Utils.getNoNil(getXMLFloat(xmlFile, key.."#wrapperTime"), 0); |
399 | local baleValueScale = Utils.getNoNil(getXMLFloat(xmlFile, key.."#baleValueScale"), 1); |
400 | local fillLevel = getXMLFloat(xmlFile, key.."#fillLevel"); |
401 | local translation = {0,0,0}; |
402 | local rotation={0,0,0}; |
403 | local parentNode = self.wrapper.baleNode; |
404 | self.baleToLoad = {parentNode=parentNode, filename=filename, translation=translation, rotation=rotation, fillLevel=fillLevel, wrapperTime=wrapperTime, baleValueScale=baleValueScale}; |
405 | end; |
406 | end |
407 | return BaseMission.VEHICLE_LOAD_OK; |
408 | end; |
409 | |
410 | function BaleWrapper:getSaveAttributesAndNodes(nodeIdent) |
411 | local attributes = ""; |
412 | local baleServerId = self.baleGrabber.currentBale; |
413 | if baleServerId == nil then |
414 | baleServerId = self.wrapper.currentBale; |
415 | end; |
416 | |
417 | if baleServerId ~= nil then |
418 | local bale = networkGetObject(baleServerId); |
419 | if bale ~= nil then |
420 | local fillLevel = bale:getFillLevel(); |
421 | local baleValueScale = bale.baleValueScale; |
422 | attributes = 'baleFileName="'..Utils.encodeToHTML(Utils.convertToNetworkFilename(bale.i3dFilename))..'" fillLevel="'..fillLevel..'" wrapperTime="'..tostring(self.wrapper.currentTime)..'" baleValueScale="'..baleValueScale..'"'; |
423 | end; |
424 | end; |
425 | |
426 | return attributes, nil; |
427 | end |
428 | |
429 | function BaleWrapper:onDeactivate() |
430 | self.showInvalidBaleWarning = false; |
431 | end; |
432 | |
433 | function BaleWrapper:onDeactivateSounds() |
434 | if self.isClient then |
435 | Utils.stopSample(self.wrapperStartSound, true); |
436 | Utils.stopSample(self.wrapperStopSound, true); |
437 | Utils.stopSample(self.wrapperSound, true); |
438 | end |
439 | end; |
440 | |
441 | function BaleWrapper:allowsGrabbingBale() |
442 | local foldAnimTime = self.foldAnimTime; |
443 | if foldAnimTime ~= nil and (foldAnimTime > self.wrapper.foldMaxLimit or foldAnimTime < self.wrapper.foldMinLimit) then |
444 | return false; |
445 | end |
446 | --if self.hasBaler == nil or not self.hasBaler then |
447 | -- return true; |
448 | --end; |
449 | return self.baleWrapperState == BaleWrapper.STATE_NONE; |
450 | end; |
451 | |
452 | function BaleWrapper:updateWrapNodes(isWrapping, isEmpty, wrapperRot) |
453 | for _, wrapNode in pairs(self.wrapNodes) do |
454 | setVisibility(wrapNode.nodeId, ((isWrapping and wrapNode.wrapVisibility) or (isEmpty and wrapNode.emptyVisibility)) and (wrapperRot < wrapNode.maxWrapperRot) ); |
455 | end |
456 | |
457 | if isWrapping then |
458 | local wrapperRotRepeat = wrapperRot % math.pi; |
459 | if wrapperRotRepeat < 0 then |
460 | wrapperRotRepeat = wrapperRotRepeat + math.pi; |
461 | end |
462 | for _,wrapAnimNode in pairs(self.wrapAnimNodes) do |
463 | local rot = wrapperRot; |
464 | if wrapAnimNode.repeatWrapperRot then |
465 | rot = wrapperRotRepeat; |
466 | end |
467 | local v = wrapAnimNode.animCurve:get(rot); |
468 | setTranslation(wrapAnimNode.nodeId, v[1], v[2], v[3]); |
469 | setRotation(wrapAnimNode.nodeId, v[4], v[5], v[6]); |
470 | setScale(wrapAnimNode.nodeId, v[7], v[8], v[9]); |
471 | end |
472 | end |
473 | end |
474 | |
475 | function BaleWrapper:updateWrappingState(t, noEventSend) |
476 | t = math.min(t, 1); |
477 | local wrapperRot = 0; |
478 | if self.wrapper.animCurve ~= nil then |
479 | local v = self.wrapper.animCurve:get(t); |
480 | setRotation(self.wrapper.baleNode, v[1]%(math.pi*2), v[2]%(math.pi*2), v[3]%(math.pi*2)); |
481 | setRotation(self.wrapper.wrapperNode, v[4]%(math.pi*2), v[5]%(math.pi*2), v[6]%(math.pi*2)); |
482 | wrapperRot = v[5]; |
483 | if self.wrapper.currentBale ~= nil and self.isServer then |
484 | local bale = networkGetObject(self.wrapper.currentBale); |
485 | if bale ~= nil then |
486 | bale:setWrappingState(t); |
487 | end |
488 | end; |
489 | end; |
490 | self:updateWrapNodes(t > 0, false, wrapperRot); |
491 | if t == 1 then |
492 | if self.isServer and self.baleWrapperState == BaleWrapper.STATE_WRAPPER_WRAPPING_BALE and not noEventSend then |
493 | g_server:broadcastEvent(BaleWrapperStateEvent:new(self, BaleWrapper.CHANGE_WRAPPING_BALE_FINSIHED), true, nil, self); |
494 | end; |
495 | end; |
496 | end; |
497 | |
498 | function BaleWrapper:moveBaleToWrapper(bale) |
499 | local baleType = self:getWrapperBaleType(bale); |
500 | self:pickupWrapperBale(bale, baleType); |
501 | end; |
502 | |
503 | function BaleWrapper:doStateChange(id, nearestBaleServerId) |
504 | |
505 | if id == BaleWrapper.CHANGE_GRAB_BALE then |
506 | local bale = networkGetObject(nearestBaleServerId); |
507 | self.baleGrabber.currentBale = nearestBaleServerId; |
508 | if bale ~= nil then |
509 | bale:mount(self, self.baleGrabber.grabNode, 0,0,0, 0,0,0); |
510 | self.baleToMount = nil; |
511 | else |
512 | self.baleToMount = {serverId=nearestBaleServerId, linkNode=self.baleGrabber.grabNode, trans={0,0,0}, rot={0,0,0} }; |
513 | end; |
514 | self.baleWrapperState = BaleWrapper.STATE_MOVING_BALE_TO_WRAPPER; |
515 | self:playAnimation("moveBaleToWrapper", 1, nil, true); |
516 | |
517 | elseif id == BaleWrapper.CHANGE_DROP_BALE_AT_GRABBER then |
518 | -- drop bale at wrapper |
519 | local attachNode = self.wrapper.baleNode; |
520 | local bale = networkGetObject(self.baleGrabber.currentBale); |
521 | if bale ~= nil then |
522 | bale:mount(self, attachNode, 0,0,0, 0,0,0); |
523 | self.baleToMount = nil; |
524 | else |
525 | self.baleToMount = {serverId=self.baleGrabber.currentBale, linkNode=attachNode, trans={0,0,0}, rot={0,0,0} }; |
526 | end; |
527 | self:updateWrapNodes(true, false, 0); |
528 | |
529 | self.wrapper.currentBale = self.baleGrabber.currentBale; |
530 | self.baleGrabber.currentBale = nil; |
531 | |
532 | self:playAnimation("moveBaleToWrapper", -1, nil, true); |
533 | self.baleWrapperState = BaleWrapper.STATE_MOVING_GRABBER_TO_WORK; |
534 | |
535 | elseif id == BaleWrapper.CHANGE_WRAPPING_START then |
536 | self.baleWrapperState = BaleWrapper.STATE_WRAPPER_WRAPPING_BALE; |
537 | if self.isClient and self:getIsActiveForSound() then |
538 | Utils.playSample(self.wrapperStartSound, 1, 0); |
539 | end |
540 | elseif id == BaleWrapper.CHANGE_WRAPPING_BALE_FINSIHED then |
541 | if self.isClient then |
542 | Utils.stopSample(self.wrapperSound); |
543 | if self.isClient and self:getIsActiveForSound() then |
544 | Utils.playSample(self.wrapperStopSound, 1, 0); |
545 | end |
546 | end |
547 | self:updateWrappingState(1, true); |
548 | self.baleWrapperState = BaleWrapper.STATE_WRAPPER_FINSIHED; |
549 | elseif id == BaleWrapper.CHANGE_WRAPPER_START_DROP_BALE then |
550 | self:updateWrapNodes(false, false, 0); |
551 | self:playAnimation("dropBaleFromWrapper", 1, nil, true); |
552 | self.baleWrapperState = BaleWrapper.STATE_WRAPPER_DROPPING_BALE; |
553 | elseif id == BaleWrapper.CHANGE_WRAPPER_BALE_DROPPED then |
554 | local bale = networkGetObject(self.wrapper.currentBale); |
555 | if bale ~= nil then |
556 | bale:unmount(); |
557 | end; |
558 | self.wrapper.currentBale = nil; |
559 | self.wrapper.currentTime = 0; |
560 | self:playAnimation("resetWrapperAfterBaleDrop", 1, nil, true); |
561 | self.baleWrapperState = BaleWrapper.STATE_WRAPPER_RESETTING_PLATFORM; |
562 | |
563 | elseif id == BaleWrapper.CHANGE_WRAPPER_PLATFORM_RESET then |
564 | self:updateWrappingState(0); |
565 | self:updateWrapNodes(false, true, 0); |
566 | self.baleWrapperState = BaleWrapper.STATE_NONE; |
567 | elseif id == BaleWrapper.CHANGE_BUTTON_EMPTY then |
568 | -- Server only code |
569 | assert(self.isServer); |
570 | if self.baleWrapperState == BaleWrapper.STATE_WRAPPER_FINSIHED then |
571 | g_server:broadcastEvent(BaleWrapperStateEvent:new(self, BaleWrapper.CHANGE_WRAPPER_START_DROP_BALE), true, nil, self); |
572 | end; |
573 | end; |
574 | end; |
575 | |
576 | function BaleWrapper:setIsUnloadingBale(isUnloadingBale, noEventSend) |
577 | if self.baleUnloadAnimationName ~= nil then |
578 | if isUnloadingBale then |
579 | if self.BaleWrapperUnloadingState ~= BaleWrapper.UNLOADING_OPENING then |
580 | BaleWrapperSetIsUnloadingBaleEvent.sendEvent(self, isUnloadingBale, noEventSend) |
581 | self.BaleWrapperUnloadingState = BaleWrapper.UNLOADING_OPENING; |
582 | self:playAnimation(self.baleUnloadAnimationName, self.baleUnloadAnimationSpeed, nil, true); |
583 | end; |
584 | else |
585 | if self.BaleWrapperUnloadingState ~= BaleWrapper.UNLOADING_CLOSING then |
586 | BaleWrapperSetIsUnloadingBaleEvent.sendEvent(self, isUnloadingBale, noEventSend) |
587 | self.BaleWrapperUnloadingState = BaleWrapper.UNLOADING_CLOSING; |
588 | self:playAnimation(self.baleCloseAnimationName, self.baleCloseAnimationSpeed, nil, true); |
589 | end; |
590 | end; |
591 | end; |
592 | end; |
593 | |
594 | function BaleWrapper:getWrapperBaleType(bale) |
595 | local baleTypes = self.allowedBaleTypes[bale:getFillType()]; |
596 | if baleTypes ~= nil then |
597 | for _, baleType in pairs(baleTypes) do |
598 | if bale.baleDiameter ~= nil and bale.baleWidth ~= nil and |
599 | bale.baleDiameter >= baleType.minBaleDiameter and bale.baleDiameter <= baleType.maxBaleDiameter and |
600 | bale.baleWidth >= baleType.minBaleWidth and bale.baleWidth <= baleType.maxBaleWidth |
601 | then |
602 | return baleType; |
603 | end |
604 | end |
605 | end |
606 | return nil; |
607 | end |
608 | |
609 | function BaleWrapper:pickupWrapperBale(bale, baleType) |
610 | if baleType ~= nil and bale.i3dFilename ~= baleType.wrapperBaleFilename then |
611 | local fillLevel = bale.fillLevel; |
612 | local baleValueScale = bale.baleValueScale; |
613 | bale:delete(); |
614 | |
615 | bale = Bale:new(self.isServer, self.isClient); |
616 | bale:load(baleType.wrapperBaleFilename, 0,0,0,0,0,0, fillLevel); |
617 | bale.baleValueScale = baleValueScale; |
618 | bale:register(); |
619 | end |
620 | |
621 | -- found bale |
622 | g_server:broadcastEvent(BaleWrapperStateEvent:new(self, BaleWrapper.CHANGE_GRAB_BALE, networkGetObjectId(bale)), true, nil, self); |
623 | end |
624 | |
625 | function BaleWrapper.getBaleInRange(self, refNode) |
626 | local px, py, pz = getWorldTranslation(refNode); |
627 | local nearestDistance = 3.0; |
628 | local nearestBale = nil; |
629 | local nearestBaleType; |
630 | |
631 | for index, item in pairs(g_currentMission.itemsToSave) do |
632 | local bale = item.item; |
633 | if bale:isa(Bale) then |
634 | local vx, vy, vz = getWorldTranslation(bale.nodeId); |
635 | local distance = Utils.vector3Length(px-vx, py-vy, pz-vz); |
636 | if distance < nearestDistance then |
637 | local foundBaleType; |
638 | if not bale.supportsWrapping or bale.wrappingState < 0.99 then |
639 | foundBaleType = self:getWrapperBaleType(bale); |
640 | end |
641 | if foundBaleType ~= nil or nearestBaleType == nil then |
642 | if foundBaleType ~= nil then |
643 | nearestDistance = distance; |
644 | end |
645 | nearestBale = bale; |
646 | nearestBaleType = foundBaleType; |
647 | end |
648 | end |
649 | end |
650 | end |
651 | return nearestBale, nearestBaleType; |
652 | end; |
653 | |
654 | function BaleWrapper:getIsFoldAllowed(superFunc) |
655 | if self.baleWrapperState ~= BaleWrapper.STATE_NONE then |
656 | return false; |
657 | end |
658 | if superFunc ~= nil then |
659 | return superFunc(self); |
660 | end |
661 | return true; |
662 | end
|
Copyright (c) 2008-2015 GIANTS Software GmbH, Confidential, All Rights Reserved.
This document is to be published solely by ls-mods.de