LUADOC - Farming Simulator 17

Printable Version

Script v1.4.4.0

Engine v7.0.0.2

Foundation Reference

BaleWrapper

Description
Class for all BaleWrappers
Functions

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
41function BaleWrapper.prerequisitesPresent(specializations)
42 return SpecializationUtil.hasSpecialization(AnimatedVehicle, specializations);
43end

load

Description
Called on loading
Definition
load(table savegame)
Arguments
tablesavegamesavegame
Code
48function BaleWrapper:load(savegame)
49
50 self.getIsFoldAllowed = Utils.overwrittenFunction(self.getIsFoldAllowed, BaleWrapper.getIsFoldAllowed);
51 self.allowsGrabbingBale = BaleWrapper.allowsGrabbingBale;
52 self.pickupWrapperBale = BaleWrapper.pickupWrapperBale;
53 self.getWrapperBaleType = BaleWrapper.getWrapperBaleType;
54 self.updateWrappingState = BaleWrapper.updateWrappingState;
55 self.dropBaleFromWrapper = BaleWrapper.dropBaleFromWrapper;
56 self.moveBaleToWrapper = BaleWrapper.moveBaleToWrapper;
57 self.doStateChange = BaleWrapper.doStateChange;
58 self.updateWrapNodes = BaleWrapper.updateWrapNodes;
59 self.playMoveToWrapper = BaleWrapper.playMoveToWrapper;
60
61
62 for _,baleCategory in pairs({"roundBaleWrapper", "squareBaleWrapper"}) do
63 self[baleCategory] = {};
64
65 self[baleCategory].animations = {};
66 for _,animType in pairs({"moveToWrapper", "wrapBale", "dropFromWrapper", "resetAfterDrop"}) do
67 local key = string.format("vehicle.wrapper.%s.animations.%s", baleCategory, animType);
68 self[baleCategory].animations[animType] = {};
69 self[baleCategory].animations[animType].animName = getXMLString(self.xmlFile, key .. "#animName");
70 self[baleCategory].animations[animType].animSpeed = Utils.getNoNil(getXMLFloat(self.xmlFile, key .. "#animSpeed"), 1);
71 self[baleCategory].animations[animType].reverseAfterMove = Utils.getNoNil(getXMLBool(self.xmlFile, key .. "#reverseAfterMove"), true);
72 end
73
74 self[baleCategory].allowedBaleTypes = {};
75 local i=0;
76 while true do
77 local key = string.format("vehicle.wrapper.%s.baleTypes.baleType(%d)", baleCategory, i);
78 if not hasXMLProperty(self.xmlFile, key) then
79 break;
80 end
81 local wrapperBaleFilename = Utils.getFilename(getXMLString(self.xmlFile, key.."#wrapperBaleFilename"), self.baseDirectory);
82 local fillTypeStr = getXMLString(self.xmlFile, key.."#fillType");
83 if wrapperBaleFilename ~= nil and fillTypeStr ~= nil then
84 local fillType = FillUtil.fillTypeNameToInt[fillTypeStr];
85 if fillType == nil then
86 print("Warning: invalid fillType '"..tostring(fillTypeStr).."' for '"..tostring(key).."' given!");
87 break;
88 end
89 self[baleCategory].allowedBaleTypes[fillType] = {};
90 local minBaleDiameter = getXMLFloat(self.xmlFile, key.."#minBaleDiameter");
91 local maxBaleDiameter = getXMLFloat(self.xmlFile, key.."#maxBaleDiameter");
92 local minBaleWidth = getXMLFloat(self.xmlFile, key.."#minBaleWidth");
93 local maxBaleWidth = getXMLFloat(self.xmlFile, key.."#maxBaleWidth");
94 if minBaleDiameter ~= nil and maxBaleDiameter ~= nil and minBaleWidth ~= nil and maxBaleWidth ~= nil then
95 table.insert(self[baleCategory].allowedBaleTypes[fillType], {fillType=fillType, wrapperBaleFilename=wrapperBaleFilename, minBaleDiameter=minBaleDiameter, maxBaleDiameter=maxBaleDiameter, minBaleWidth=minBaleWidth, maxBaleWidth=maxBaleWidth});
96 else
97 local minBaleHeight = getXMLFloat(self.xmlFile, key.."#minBaleHeight");
98 local maxBaleHeight = getXMLFloat(self.xmlFile, key.."#maxBaleHeight");
99 local minBaleLength = getXMLFloat(self.xmlFile, key.."#minBaleLength");
100 local maxBaleLength = getXMLFloat(self.xmlFile, key.."#maxBaleLength");
101 if minBaleWidth ~= nil and maxBaleWidth ~= nil and minBaleHeight ~= nil and maxBaleHeight ~= nil and minBaleLength ~= nil and maxBaleLength ~= nil then
102 table.insert(self[baleCategory].allowedBaleTypes[fillType], {fillType=fillType, wrapperBaleFilename=wrapperBaleFilename, minBaleWidth=minBaleWidth, maxBaleWidth=maxBaleWidth, minBaleHeight=minBaleHeight, maxBaleHeight=maxBaleHeight, minBaleLength=minBaleLength, maxBaleLength=maxBaleLength});
103 end
104 end
105 end
106 i=i+1;
107 end
108
109 local key = string.format("vehicle.wrapper.%s", baleCategory);
110 self[baleCategory].baleNode = Utils.indexToObject(self.components, getXMLString(self.xmlFile, key .. "#baleIndex"));
111 self[baleCategory].wrapperNode = Utils.indexToObject(self.components, getXMLString(self.xmlFile, key .. "#wrapperIndex"));
112 self[baleCategory].wrapperRotAxis = Utils.getNoNil(getXMLInt(self.xmlFile, key.."#wrapperRotAxis"), 2);
113 local wrappingAnimCurve = AnimCurve:new(linearInterpolatorN);
114 local i = 0;
115 while true do
116 local keyI = string.format("%s.wrapperAnimation.key(%d)", key, i);
117 local t = getXMLFloat(self.xmlFile, keyI.."#time");
118 local baleX, baleY, baleZ = Utils.getVectorFromString(getXMLString(self.xmlFile, keyI.."#baleRot"));
119 if baleX == nil or baleY == nil or baleZ == nil then
120 break;
121 end
122 baleX = math.rad(Utils.getNoNil(baleX, 0));
123 baleY = math.rad(Utils.getNoNil(baleY, 0));
124 baleZ = math.rad(Utils.getNoNil(baleZ, 0));
125 local wrapperX, wrapperY, wrapperZ = Utils.getVectorFromString(getXMLString(self.xmlFile, keyI.."#wrapperRot"));
126 wrapperX = math.rad(Utils.getNoNil(wrapperX, 0));
127 wrapperY = math.rad(Utils.getNoNil(wrapperY, 0));
128 wrapperZ = math.rad(Utils.getNoNil(wrapperZ, 0));
129 wrappingAnimCurve:addKeyframe({ v={baleX, baleY, baleZ, wrapperX, wrapperY, wrapperZ}, time = t});
130 i = i +1;
131 end
132 self[baleCategory].animCurve = wrappingAnimCurve;
133 self[baleCategory].animTime = Utils.getNoNil(getXMLFloat(self.xmlFile, key .. "#wrappingTime"), 5) * 1000;
134 self[baleCategory].currentTime = 0;
135 self[baleCategory].currentBale = nil;
136
137
138 self[baleCategory].wrapAnimNodes = {};
139 local i = 0;
140 while true do
141 local wrapAnimNodeKey = string.format("vehicle.wrapper.%s.wrapAnimNodes.wrapAnimNode(%d)", baleCategory, i);
142 if not hasXMLProperty(self.xmlFile, wrapAnimNodeKey) then
143 break;
144 end
145 local nodeId = Utils.indexToObject(self.components, getXMLString(self.xmlFile, wrapAnimNodeKey.."#index"));
146 if nodeId ~= nil then
147 local animCurve = AnimCurve:new(linearInterpolatorN);
148 local keyI = 0;
149 local useWrapperRot = false;
150 while true do
151 local key = string.format(wrapAnimNodeKey..".key(%d)", keyI);
152 local wrapperRot = getXMLFloat(self.xmlFile, key.."#wrapperRot");
153 local wrapperTime = getXMLFloat(self.xmlFile, key.."#wrapperTime");
154 if wrapperRot == nil and wrapperTime == nil then
155 break;
156 end
157 useWrapperRot = wrapperRot ~= nil;
158
159 local x, y, z = Utils.getVectorFromString(getXMLString(self.xmlFile, key.."#trans"));
160 local rx, ry, rz = Utils.getVectorFromString(getXMLString(self.xmlFile, key.."#rot"));
161 local sx, sy, sz = Utils.getVectorFromString(getXMLString(self.xmlFile, key.."#scale"));
162
163 x = Utils.getNoNil(x, 0);
164 y = Utils.getNoNil(y, 0);
165 z = Utils.getNoNil(z, 0);
166 rx = math.rad(Utils.getNoNil(rx, 0));
167 ry = math.rad(Utils.getNoNil(ry, 0));
168 rz = math.rad(Utils.getNoNil(rz, 0));
169 sx = Utils.getNoNil(sx, 1);
170 sy = Utils.getNoNil(sy, 1);
171 sz = Utils.getNoNil(sz, 1);
172 if wrapperRot ~= nil then
173 animCurve:addKeyframe({ v={x, y, z, rx, ry, rz, sx,sy,sz}, time = math.rad(wrapperRot)});
174 else
175 animCurve:addKeyframe({ v={x, y, z, rx, ry, rz, sx,sy,sz}, time = wrapperTime});
176 end
177 keyI = keyI +1;
178 end
179 if keyI > 0 then
180 local repeatWrapperRot = Utils.getNoNil(getXMLBool(self.xmlFile, wrapAnimNodeKey.."#repeatWrapperRot"), false);
181 local normalizeRotationOnBaleDrop = Utils.getNoNil(getXMLInt(self.xmlFile, wrapAnimNodeKey.."#normalizeRotationOnBaleDrop"), 0);
182 table.insert(self[baleCategory].wrapAnimNodes,
183 {nodeId = nodeId, animCurve = animCurve, repeatWrapperRot = repeatWrapperRot, normalizeRotationOnBaleDrop = normalizeRotationOnBaleDrop, useWrapperRot = useWrapperRot});
184 end
185 end
186 i = i + 1;
187 end
188
189
190 self[baleCategory].wrapNodes = {};
191 local i = 0;
192 while true do
193 local wrapNodeKey = string.format("vehicle.wrapper.%s.wrapNodes.wrapNode(%d)", baleCategory, i);
194 if not hasXMLProperty(self.xmlFile, wrapNodeKey) then
195 break;
196 end
197 local nodeId = Utils.indexToObject(self.components, getXMLString(self.xmlFile, wrapNodeKey.."#index"));
198 local wrapVisibility = Utils.getNoNil(getXMLBool(self.xmlFile, wrapNodeKey.."#wrapVisibility"), false);
199 local emptyVisibility = Utils.getNoNil(getXMLBool(self.xmlFile, wrapNodeKey.."#emptyVisibility"), false);
200 if nodeId ~= nil and (wrapVisibility or emptyVisibility) then
201 local maxWrapperRot = getXMLFloat(self.xmlFile, wrapNodeKey.."#maxWrapperRot");
202 if maxWrapperRot == nil then
203 maxWrapperRot = math.huge;
204 else
205 maxWrapperRot = math.rad(maxWrapperRot);
206 end
207 table.insert(self[baleCategory].wrapNodes, {nodeId=nodeId, wrapVisibility=wrapVisibility, emptyVisibility=emptyVisibility, maxWrapperRot=maxWrapperRot});
208 end
209 i = i + 1;
210 end
211
212 local defaultText = baleCategory == "roundBaleWrapper" and "action_unloadRoundBale" or "action_unloadBaler";
213 self[baleCategory].unloadBaleText = Utils.getNoNil(getXMLString(self.xmlFile, key .. "#unloadBaleText"), defaultText);
214 end
215
216 self.currentWrapper = {};
217 self.currentWrapperFoldMinLimit = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.wrapper#foldMinLimit"), 0);
218 self.currentWrapperFoldMaxLimit = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.wrapper#foldMaxLimit"), 1);
219
220
221 self.showInvalidBaleWarning = false;
222
223 --
224 self.currentWrapper = self.roundBaleWrapper;
225 self:updateWrapNodes(false, true, 0);
226 self.currentWrapper = self.squareBaleWrapper;
227 self:updateWrapNodes(false, true, 0);
228
229
230 self.baleGrabber = {};
231 self.baleGrabber.grabNode = Utils.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.baleGrabber#index"));
232 self.baleGrabber.nearestDistance = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.baleGrabber#nearestDistance"), 3.0);
233 self.baleGrabber.origTrans = { getTranslation(self.baleGrabber.grabNode) };
234
235 if self.isClient then
236 self.currentWrapperSound = SoundUtil.loadSample(self.xmlFile, {}, "vehicle.wrapperSound", nil, self.baseDirectory);
237 self.currentWrapperStartSound = SoundUtil.loadSample(self.xmlFile, {}, "vehicle.wrapperStartSound", nil, self.baseDirectory);
238 self.currentWrapperStopSound = SoundUtil.loadSample(self.xmlFile, {}, "vehicle.wrapperStopSound", nil, self.baseDirectory);
239 end
240
241 self.baleToLoad = nil;
242 self.baleToMount = nil;
243 self.baleWrapperState = BaleWrapper.STATE_NONE;
244 self.grabberIsMoving = false;
245 self.hasBaleWrapper = true;
246
247 if savegame ~= nil and not savegame.resetVehicles then
248 local filename = getXMLString(savegame.xmlFile, savegame.key.."#baleFileName");
249 if filename ~= nil then
250 filename = Utils.convertFromNetworkFilename(filename);
251 local wrapperTime = Utils.getNoNil(getXMLFloat(savegame.xmlFile, savegame.key.."#wrapperTime"), 0);
252 local baleValueScale = Utils.getNoNil(getXMLFloat(savegame.xmlFile, savegame.key.."#baleValueScale"), 1);
253 local fillLevel = getXMLFloat(savegame.xmlFile, savegame.key.."#fillLevel");
254 local translation = {0,0,0};
255 local rotation={0,0,0};
256 self.baleToLoad = {filename=filename, translation=translation, rotation=rotation, fillLevel=fillLevel, wrapperTime=wrapperTime, baleValueScale=baleValueScale};
257 end
258 end
259end

delete

Description
Called on deleting
Definition
delete()
Code
263function BaleWrapper:delete()
264 if self.isServer then
265 local baleId;
266 if self.currentWrapper.currentBale ~= nil then
267 baleId = self.currentWrapper.currentBale;
268 end
269 if self.baleGrabber.currentBale ~= nil then
270 baleId = self.baleGrabber.currentBale;
271 end
272 if baleId ~= nil then
273 local bale = networkGetObject(baleId);
274 if bale ~= nil then
275 bale:unmount();
276 end
277 end
278 end
279 if self.isClient then
280 SoundUtil.deleteSample(self.currentWrapperSound);
281 SoundUtil.deleteSample(self.currentWrapperStartSound);
282 SoundUtil.deleteSample(self.currentWrapperStopSound);
283 end
284end

readStream

Description
Called on client side on join
Definition
readStream(integer streamId, integer connection)
Arguments
integerstreamIdstreamId
integerconnectionconnection
Code
290function BaleWrapper:readStream(streamId, connection)
291 if connection:getIsServer() then
292 local wrapperState = streamReadUIntN(streamId, BaleWrapper.STATE_NUM_BITS);
293 if wrapperState >= BaleWrapper.STATE_MOVING_BALE_TO_WRAPPER then
294 local baleServerId;
295 if wrapperState ~= BaleWrapper.STATE_WRAPPER_RESETTING_PLATFORM then
296 baleServerId = readNetworkNodeObjectId(streamId);
297 end
298
299 if wrapperState == BaleWrapper.STATE_MOVING_BALE_TO_WRAPPER then
300 self:doStateChange(BaleWrapper.CHANGE_GRAB_BALE, baleServerId);
301 AnimatedVehicle.updateAnimations(self, 99999999);
302 elseif wrapperState == BaleWrapper.STATE_MOVING_GRABBER_TO_WORK then
303 self.baleGrabber.currentBale = baleServerId;
304
305 self:doStateChange(BaleWrapper.CHANGE_DROP_BALE_AT_GRABBER);
306 AnimatedVehicle.updateAnimations(self, 99999999);
307
308 elseif wrapperState ~= BaleWrapper.STATE_WRAPPER_RESETTING_PLATFORM then
309
310 local attachNode = self.currentWrapper.baleNode;
311 self.baleToMount = {serverId=baleServerId, linkNode=attachNode, trans={0,0,0}, rot={0,0,0} };
312 self:updateWrapNodes(true, false, 0);
313 self.currentWrapper.currentBale = baleServerId;
314
315 if wrapperState == BaleWrapper.STATE_WRAPPER_WRAPPING_BALE then
316 local wrapperTime = streamReadFloat32(streamId);
317 self.currentWrapper.currentTime = wrapperTime;
318 self:updateWrappingState(self.currentWrapper.currentTime / self.currentWrapper.animTime, true);
319 else
320 self.currentWrapper.currentTime = self.currentWrapper.animTime;
321 self:updateWrappingState(1, true);
322
323 self:doStateChange(BaleWrapper.CHANGE_WRAPPING_BALE_FINSIHED);
324 AnimatedVehicle.updateAnimations(self, 99999999);
325 if wrapperState >= BaleWrapper.STATE_WRAPPER_DROPPING_BALE then
326 self:doStateChange(BaleWrapper.CHANGE_WRAPPER_START_DROP_BALE);
327 AnimatedVehicle.updateAnimations(self, 99999999);
328 end
329 end
330 else
331 -- simply set the state but do nothing else
332 self.baleWrapperState = BaleWrapper.STATE_WRAPPER_RESETTING_PLATFORM;
333 end
334 end
335 end
336end

writeStream

Description
Called on server side on join
Definition
writeStream(integer streamId, integer connection)
Arguments
integerstreamIdstreamId
integerconnectionconnection
Code
342function BaleWrapper:writeStream(streamId, connection)
343 if not connection:getIsServer() then
344 local wrapperState = self.baleWrapperState;
345 streamWriteUIntN(streamId, wrapperState, BaleWrapper.STATE_NUM_BITS);
346
347 if wrapperState >= BaleWrapper.STATE_MOVING_BALE_TO_WRAPPER and wrapperState ~= BaleWrapper.STATE_WRAPPER_RESETTING_PLATFORM then
348 if wrapperState == BaleWrapper.STATE_MOVING_BALE_TO_WRAPPER then
349 writeNetworkNodeObjectId(streamId, self.baleGrabber.currentBale);
350 else
351 writeNetworkNodeObjectId(streamId, self.currentWrapper.currentBale);
352 end
353 end
354 if wrapperState == BaleWrapper.STATE_WRAPPER_WRAPPING_BALE then
355 streamWriteFloat32(streamId, self.currentWrapper.currentTime);
356 end
357 end
358end

update

Description
Called on update
Definition
update(float dt)
Arguments
floatdttime since last call in ms
Code
369function BaleWrapper:update(dt)
370
371 if self.firstTimeRun then
372 if self.baleToLoad ~= nil then
373 local v = self.baleToLoad;
374 self.baleToLoad = nil;
375 local baleObject = Bale:new(self.isServer, self.isClient);
376 local x,y,z = unpack(v.translation);
377 local rx,ry,rz = unpack(v.rotation);
378 baleObject:load(v.filename, x,y,z,rx,ry,rz, v.fillLevel);
379 if baleObject.nodeId ~= nil and baleObject.nodeId ~= 0 then
380 self:playMoveToWrapper(baleObject);
381 baleObject.baleValueScale = v.baleValueScale;
382 local wrapperState = math.min(v.wrapperTime / self.currentWrapper.animTime, 1);
383 baleObject:setWrappingState(wrapperState);
384 baleObject:mount(self, self.currentWrapper.baleNode, x,y,z, rx,ry,rz)
385 baleObject:register();
386 self:doStateChange(BaleWrapper.CHANGE_WRAPPING_START);
387 self.currentWrapper.currentBale = networkGetObjectId(baleObject);
388 self.currentWrapper.currentTime = v.wrapperTime;
389 self:updateWrappingState(self.currentWrapper.currentTime / self.currentWrapper.animTime);
390 end
391 end
392
393 if self.baleToMount ~= nil then
394 local bale = networkGetObject(self.baleToMount.serverId);
395 if bale ~= nil then
396 local x,y,z = unpack(self.baleToMount.trans);
397 local rx,ry,rz = unpack(self.baleToMount.rot);
398 bale:mount(self, self.baleToMount.linkNode, x,y,z, rx,ry,rz);
399 self.baleToMount = nil;
400
401 if self.baleWrapperState == BaleWrapper.STATE_MOVING_BALE_TO_WRAPPER then
402 self:playMoveToWrapper(bale)
403 end
404 end
405 end
406 end
407
408 if self:getIsActive() then
409 if self:getIsActiveForInput() then
410 if InputBinding.hasEvent(InputBinding.IMPLEMENT_EXTRA3) then
411 if self.baleWrapperState == BaleWrapper.STATE_WRAPPER_FINSIHED then
412 g_client:getServerConnection():sendEvent(BaleWrapperStateEvent:new(self, BaleWrapper.CHANGE_BUTTON_EMPTY));
413 end
414 end
415 end
416
417 if self.baleWrapperState == BaleWrapper.STATE_WRAPPER_WRAPPING_BALE then
418 if self.isClient and self:getIsActiveForSound() then
419 if not self.currentWrapperSound.isPlaying then
420 if self.currentWrapperStartSound.sample == nil or not isSamplePlaying(self.currentWrapperStartSound.sample) or getSamplePlayOffset(self.currentWrapperStartSound.sample) >= self.currentWrapperStartSound.duration-1.8*dt then
421 SoundUtil.playSample(self.currentWrapperSound, 0, 0);
422 end
423 end
424 end
425 self.currentWrapper.currentTime = self.currentWrapper.currentTime + dt;
426 self:updateWrappingState(self.currentWrapper.currentTime / self.currentWrapper.animTime);
427 end
428 end
429end

updateTick

Description
Called on update tick
Definition
updateTick(float dt)
Arguments
floatdttime since last call in ms
Code
434function BaleWrapper:updateTick(dt)
435 if self:getIsActive() then
436 self.showInvalidBaleWarning = false;
437 if self:allowsGrabbingBale() then
438 if self.baleGrabber.grabNode ~= nil and self.baleGrabber.currentBale == nil then
439 -- find nearest bale
440 local nearestBale, nearestBaleType = BaleWrapper.getBaleInRange(self, self.baleGrabber.grabNode, self.baleGrabber.nearestDistance);
441 if nearestBale ~= nil then
442 if nearestBaleType == nil then
443 if self.lastDroppedBale ~= nearestBale then
444 self.showInvalidBaleWarning = true;
445 end
446 elseif self.isServer then
447 self:pickupWrapperBale(nearestBale, nearestBaleType);
448 end
449 end
450 end
451 end
452 if self.isServer then
453 if self.baleWrapperState ~= BaleWrapper.STATE_NONE then
454 if self.baleWrapperState == BaleWrapper.STATE_MOVING_BALE_TO_WRAPPER then
455 if not self:getIsAnimationPlaying(self.currentWrapper.animations["moveToWrapper"].animName) then
456 g_server:broadcastEvent(BaleWrapperStateEvent:new(self, BaleWrapper.CHANGE_DROP_BALE_AT_GRABBER), true, nil, self);
457 end
458 elseif self.baleWrapperState == BaleWrapper.STATE_MOVING_GRABBER_TO_WORK then
459 if not self:getIsAnimationPlaying(self.currentWrapper.animations["moveToWrapper"].animName) then
460 local bale = networkGetObject(self.currentWrapper.currentBale);
461 if bale ~= nil and not bale.supportsWrapping then
462 g_server:broadcastEvent(BaleWrapperStateEvent:new(self, BaleWrapper.CHANGE_WRAPPER_START_DROP_BALE), true, nil, self);
463 else
464 g_server:broadcastEvent(BaleWrapperStateEvent:new(self, BaleWrapper.CHANGE_WRAPPING_START), true, nil, self);
465 end
466 end
467 elseif self.baleWrapperState == BaleWrapper.STATE_WRAPPER_DROPPING_BALE then
468 if not self:getIsAnimationPlaying(self.currentWrapper.animations["dropFromWrapper"].animName) then
469 g_server:broadcastEvent(BaleWrapperStateEvent:new(self, BaleWrapper.CHANGE_WRAPPER_BALE_DROPPED), true, nil, self);
470 end
471 elseif self.baleWrapperState == BaleWrapper.STATE_WRAPPER_RESETTING_PLATFORM then
472 if not self:getIsAnimationPlaying(self.currentWrapper.animations["resetAfterDrop"].animName) then
473 g_server:broadcastEvent(BaleWrapperStateEvent:new(self, BaleWrapper.CHANGE_WRAPPER_PLATFORM_RESET), true, nil, self);
474 end
475 end
476 end
477 end
478 end
479end

draw

Description
Called on draw
Definition
draw()
Code
483function BaleWrapper:draw()
484 if self.isClient then
485 if self:getIsActiveForInput(true) then
486
487 if self.baleWrapperState == BaleWrapper.STATE_WRAPPER_FINSIHED then
488 local i18n = g_i18n;
489 if self.customEnvironment ~= nil then
490 i18n = _G[self.customEnvironment].g_i18n;
491 end
492
493 g_currentMission:addHelpButtonText(i18n:getText(self.currentWrapper.unloadBaleText), InputBinding.IMPLEMENT_EXTRA3, nil, GS_PRIO_HIGH);
494 end
495 end
496
497 if self.showInvalidBaleWarning then
498 g_currentMission:showBlinkingWarning(g_i18n:getText("warning_baleNotSupported"));
499 end
500 end
501end

getSaveAttributesAndNodes

Description
Returns attributes and nodes to save
Definition
getSaveAttributesAndNodes(table nodeIdent)
Arguments
tablenodeIdentnode ident
Return Values
stringattributesattributes
stringnodesnodes
Code
508function BaleWrapper:getSaveAttributesAndNodes(nodeIdent)
509 local attributes = "";
510 local baleServerId = self.baleGrabber.currentBale;
511 if baleServerId == nil then
512 baleServerId = self.currentWrapper.currentBale;
513 end
514
515 if baleServerId ~= nil then
516 local bale = networkGetObject(baleServerId);
517 if bale ~= nil then
518 local fillLevel = bale:getFillLevel();
519 local baleValueScale = bale.baleValueScale;
520 attributes = 'baleFileName="'..Utils.encodeToHTML(Utils.convertToNetworkFilename(bale.i3dFilename))..'" fillLevel="'..fillLevel..'" wrapperTime="'..tostring(self.currentWrapper.currentTime)..'" baleValueScale="'..baleValueScale..'"';
521 end
522 end
523
524 return attributes, nil;
525end

onDeactivate

Description
Called on deactivate
Definition
onDeactivate()
Code
529function BaleWrapper:onDeactivate()
530 self.showInvalidBaleWarning = false;
531end

onDeactivateSounds

Description
Called on deactivating sounds
Definition
onDeactivateSounds()
Code
535function BaleWrapper:onDeactivateSounds()
536 if self.isClient then
537 SoundUtil.stopSample(self.currentWrapperStartSound, true);
538 SoundUtil.stopSample(self.currentWrapperStopSound, true);
539 SoundUtil.stopSample(self.currentWrapperSound, true);
540 end
541end

allowsGrabbingBale

Description
Returns if allows bale grabbing
Definition
allowsGrabbingBale()
Return Values
booleanallowsallows bale grabbing
Code
546function BaleWrapper:allowsGrabbingBale()
547 local foldAnimTime = self.foldAnimTime;
548 if foldAnimTime ~= nil and (foldAnimTime > self.currentWrapperFoldMaxLimit or foldAnimTime < self.currentWrapperFoldMinLimit) then
549 return false;
550 end
551 return self.baleWrapperState == BaleWrapper.STATE_NONE;
552end

updateWrapNodes

Description
Update bale wrap nodes
Definition
updateWrapNodes(boolean isWrapping, boolean isEmpty, float t, float wrapperRot)
Arguments
booleanisWrappingis wrapping
booleanisEmptyis empty
floattanimation time
floatwrapperRotrotation of wrapper
Code
560function BaleWrapper:updateWrapNodes(isWrapping, isEmpty, t, wrapperRot)
561
562 if wrapperRot == nil then
563 wrapperRot = 0;
564 end
565
566 for _, wrapNode in pairs(self.currentWrapper.wrapNodes) do
567 local doShow = true;
568 if wrapNode.maxWrapperRot ~= nil then
569 doShow = wrapperRot < wrapNode.maxWrapperRot;
570 end
571 setVisibility(wrapNode.nodeId, doShow and ((isWrapping and wrapNode.wrapVisibility) or (isEmpty and wrapNode.emptyVisibility)));
572 end
573
574 if isWrapping then
575 local wrapperRotRepeat = Utils.sign(wrapperRot) * (wrapperRot % math.pi);
576 if wrapperRotRepeat < 0 then
577 wrapperRotRepeat = wrapperRotRepeat + math.pi;
578 end
579
580 for _,wrapAnimNode in pairs(self.currentWrapper.wrapAnimNodes) do
581 local v;
582 if wrapAnimNode.useWrapperRot then
583 local rot = wrapperRot;
584 if wrapAnimNode.repeatWrapperRot then
585 rot = wrapperRotRepeat;
586 end
587 v = wrapAnimNode.animCurve:get(rot);
588 else
589 v = wrapAnimNode.animCurve:get(t);
590 end
591 if v ~= nil then
592 setTranslation(wrapAnimNode.nodeId, v[1], v[2], v[3]);
593 setRotation(wrapAnimNode.nodeId, v[4], v[5], v[6]);
594 setScale(wrapAnimNode.nodeId, v[7], v[8], v[9]);
595 end
596 end
597 else
598 if not isEmpty then
599 for _,wrapAnimNode in pairs(self.currentWrapper.wrapAnimNodes) do
600 if wrapAnimNode.normalizeRotationOnBaleDrop ~= 0 then
601 local rot = { getRotation(wrapAnimNode.nodeId) };
602 for i=1,3 do
603 rot[i] = wrapAnimNode.normalizeRotationOnBaleDrop * Utils.sign(rot[i]) * ( rot[i] % (2*math.pi) );
604 end
605 setRotation(wrapAnimNode.nodeId, rot[1],rot[2],rot[3]);
606 end
607 end
608 end
609 end
610end

updateWrappingState

Description
Update wrapping state
Definition
updateWrappingState(float t, boolean noEventSend)
Arguments
floattanimation time
booleannoEventSendno event send
Code
616function BaleWrapper:updateWrappingState(t, noEventSend)
617 t = math.min(t, 1);
618 local wrapperRot = 0;
619 if self.currentWrapper.animCurve ~= nil then
620 local v = self.currentWrapper.animCurve:get(t);
621 if v ~= nil then
622 setRotation(self.currentWrapper.baleNode, v[1]%(math.pi*2), v[2]%(math.pi*2), v[3]%(math.pi*2));
623 setRotation(self.currentWrapper.wrapperNode, v[4]%(math.pi*2), v[5]%(math.pi*2), v[6]%(math.pi*2));
624 wrapperRot = v[3 + self.currentWrapper.wrapperRotAxis];
625 elseif self.currentWrapper.animations["wrapBale"].animName ~= nil then
626 t = self:getAnimationTime(self.currentWrapper.animations["wrapBale"].animName);
627 end
628 if self.currentWrapper.currentBale ~= nil and self.isServer then
629 local bale = networkGetObject(self.currentWrapper.currentBale);
630 if bale ~= nil then
631 bale:setWrappingState(t);
632 end
633 end
634 end
635 self:updateWrapNodes(t > 0, false, t, wrapperRot);
636 if t == 1 then
637 if self.isServer and self.baleWrapperState == BaleWrapper.STATE_WRAPPER_WRAPPING_BALE and not noEventSend then
638 g_server:broadcastEvent(BaleWrapperStateEvent:new(self, BaleWrapper.CHANGE_WRAPPING_BALE_FINSIHED), true, nil, self);
639 end
640 end
641end

moveBaleToWrapper

Description
Move bale to wrapper
Definition
moveBaleToWrapper(table bale)
Arguments
tablebalebale to move
Code
646function BaleWrapper:moveBaleToWrapper(bale)
647 local baleType = self:getWrapperBaleType(bale);
648 self:pickupWrapperBale(bale, baleType);
649end

playMoveToWrapper

Description
Play move to wrapper animation
Definition
playMoveToWrapper(table bale)
Arguments
tablebalebale to move
Code
654function BaleWrapper:playMoveToWrapper(bale)
655 self.currentWrapper = self.roundBaleWrapper;
656 if bale.baleDiameter == nil then
657 self.currentWrapper = self.squareBaleWrapper;
658 end
659
660 if self.currentWrapper.animations["moveToWrapper"].animName ~= nil then
661 self:playAnimation(self.currentWrapper.animations["moveToWrapper"].animName, self.currentWrapper.animations["moveToWrapper"].animSpeed, nil, true);
662 end
663end

doStateChange

Description
Changed wrapper state
Definition
doStateChange(integer id, integer nearestBaleServerId)
Arguments
integeridid of new state
integernearestBaleServerIdserver id of nearest bale
Code
669function BaleWrapper:doStateChange(id, nearestBaleServerId)
670 if id == BaleWrapper.CHANGE_GRAB_BALE then
671
672 local bale = networkGetObject(nearestBaleServerId);
673 self.baleGrabber.currentBale = nearestBaleServerId;
674 if bale ~= nil then
675 local x,y,z = localToLocal(bale.nodeId, getParent(self.baleGrabber.grabNode), 0,0,0);
676 setTranslation(self.baleGrabber.grabNode, x,y,z);
677 bale:mount(self, self.baleGrabber.grabNode, 0,0,0, 0,0,0);
678 self.baleToMount = nil;
679 self:playMoveToWrapper(bale)
680 else
681 self.baleToMount = {serverId=nearestBaleServerId, linkNode=self.baleGrabber.grabNode, trans={0,0,0}, rot={0,0,0} };
682 end
683 self.baleWrapperState = BaleWrapper.STATE_MOVING_BALE_TO_WRAPPER;
684
685 elseif id == BaleWrapper.CHANGE_DROP_BALE_AT_GRABBER then
686 -- drop bale at wrapper
687 local attachNode = self.currentWrapper.baleNode;
688 local bale = networkGetObject(self.baleGrabber.currentBale);
689 if bale ~= nil then
690 bale:mount(self, attachNode, 0,0,0, 0,0,0);
691 self.baleToMount = nil;
692 else
693 self.baleToMount = {serverId=self.baleGrabber.currentBale, linkNode=attachNode, trans={0,0,0}, rot={0,0,0} };
694 end
695 self:updateWrapNodes(true, false, 0);
696
697 self.currentWrapper.currentBale = self.baleGrabber.currentBale;
698 self.baleGrabber.currentBale = nil;
699
700 if self.currentWrapper.animations["moveToWrapper"].animName ~= nil then
701 if self.currentWrapper.animations["moveToWrapper"].reverseAfterMove then
702 self:playAnimation(self.currentWrapper.animations["moveToWrapper"].animName, -self.currentWrapper.animations["moveToWrapper"].animSpeed, nil, true);
703 end
704 end
705
706 self.baleWrapperState = BaleWrapper.STATE_MOVING_GRABBER_TO_WORK;
707
708 elseif id == BaleWrapper.CHANGE_WRAPPING_START then
709
710 self.baleWrapperState = BaleWrapper.STATE_WRAPPER_WRAPPING_BALE;
711 if self.isClient and self:getIsActiveForSound() then
712 SoundUtil.playSample(self.currentWrapperStartSound, 1, 0);
713 end
714
715 if self.currentWrapper.animations["wrapBale"].animName ~= nil then
716 self:playAnimation(self.currentWrapper.animations["wrapBale"].animName, self.currentWrapper.animations["wrapBale"].animSpeed, nil, true);
717 end
718
719 elseif id == BaleWrapper.CHANGE_WRAPPING_BALE_FINSIHED then
720
721 if self.isClient then
722 SoundUtil.stopSample(self.currentWrapperSound);
723 if self.isClient and self:getIsActiveForSound() then
724 SoundUtil.playSample(self.currentWrapperStopSound, 1, 0);
725 end
726 end
727 self:updateWrappingState(1, true);
728 self.baleWrapperState = BaleWrapper.STATE_WRAPPER_FINSIHED;
729
730 elseif id == BaleWrapper.CHANGE_WRAPPER_START_DROP_BALE then
731
732 self:updateWrapNodes(false, false, 0);
733 if self.currentWrapper.animations["dropFromWrapper"].animName ~= nil then
734 self:playAnimation(self.currentWrapper.animations["dropFromWrapper"].animName, self.currentWrapper.animations["dropFromWrapper"].animSpeed, nil, true);
735 end
736 self.baleWrapperState = BaleWrapper.STATE_WRAPPER_DROPPING_BALE;
737
738 elseif id == BaleWrapper.CHANGE_WRAPPER_BALE_DROPPED then
739
740 local bale = networkGetObject(self.currentWrapper.currentBale);
741 if bale ~= nil then
742 bale:unmount();
743 end
744 self.lastDroppedBale = bale;
745 self.currentWrapper.currentBale = nil;
746 self.currentWrapper.currentTime = 0;
747 if self.currentWrapper.animations["resetAfterDrop"].animName ~= nil then
748 self:playAnimation(self.currentWrapper.animations["resetAfterDrop"].animName, self.currentWrapper.animations["resetAfterDrop"].animSpeed, nil, true);
749 end
750 self.baleWrapperState = BaleWrapper.STATE_WRAPPER_RESETTING_PLATFORM;
751
752 elseif id == BaleWrapper.CHANGE_WRAPPER_PLATFORM_RESET then
753
754 self:updateWrappingState(0);
755 self:updateWrapNodes(false, true, 0);
756 self.baleWrapperState = BaleWrapper.STATE_NONE;
757
758 elseif id == BaleWrapper.CHANGE_BUTTON_EMPTY then
759
760 -- Server only code
761 assert(self.isServer);
762 if self.baleWrapperState == BaleWrapper.STATE_WRAPPER_FINSIHED then
763 g_server:broadcastEvent(BaleWrapperStateEvent:new(self, BaleWrapper.CHANGE_WRAPPER_START_DROP_BALE), true, nil, self);
764 end
765
766 end
767end

setIsUnloadingBale

Description
Set bale unloading
Definition
setIsUnloadingBale(boolean isUnloadingBale, boolean noEventSend)
Arguments
booleanisUnloadingBaleis unloading bale
booleannoEventSendno event send
Code
773function BaleWrapper:setIsUnloadingBale(isUnloadingBale, noEventSend)
774 if self.baler ~= nil and self.baler.baleUnloadAnimationName ~= nil then
775 if isUnloadingBale then
776 if self.baleWrapperUnloadingState ~= BaleWrapper.UNLOADING_OPENING then
777 BaleWrapperSetIsUnloadingBaleEvent.sendEvent(self, isUnloadingBale, noEventSend)
778 self.baleWrapperUnloadingState = BaleWrapper.UNLOADING_OPENING;
779 self:playAnimation(self.baler.baleUnloadAnimationName, self.baler.baleUnloadAnimationSpeed, nil, true);
780 end
781 else
782 if self.baleWrapperUnloadingState ~= BaleWrapper.UNLOADING_CLOSING then
783 BaleWrapperSetIsUnloadingBaleEvent.sendEvent(self, isUnloadingBale, noEventSend)
784 self.baleWrapperUnloadingState = BaleWrapper.UNLOADING_CLOSING;
785 self:playAnimation(self.baler.baleCloseAnimationName, self.baler.baleCloseAnimationSpeed, nil, true);
786 end
787 end
788 end
789end

getWrapperBaleType

Description
Returns type of wrapped bale
Definition
getWrapperBaleType(table bale)
Arguments
tablebaleunwrapped bale
Return Values
tablebaleTypewrapped bale
Code
795function BaleWrapper:getWrapperBaleType(bale)
796 local baleTypes;
797 if bale.baleDiameter ~= nil then
798 baleTypes = self.roundBaleWrapper.allowedBaleTypes[bale:getFillType()];
799 else
800 baleTypes = self.squareBaleWrapper.allowedBaleTypes[bale:getFillType()];
801 end
802 if baleTypes ~= nil then
803 for _, baleType in pairs(baleTypes) do
804 if bale.baleDiameter ~= nil and bale.baleWidth ~= nil then
805 if bale.baleDiameter >= baleType.minBaleDiameter and bale.baleDiameter <= baleType.maxBaleDiameter and
806 bale.baleWidth >= baleType.minBaleWidth and bale.baleWidth <= baleType.maxBaleWidth
807 then
808 return baleType;
809 end
810 elseif bale.baleHeight ~= nil and bale.baleWidth ~= nil and bale.baleLength ~= nil then
811 if bale.baleHeight >= baleType.minBaleHeight and bale.baleHeight <= baleType.maxBaleHeight and
812 bale.baleWidth >= baleType.minBaleWidth and bale.baleWidth <= baleType.maxBaleWidth and
813 bale.baleLength >= baleType.minBaleLength and bale.baleLength <= baleType.maxBaleLength
814 then
815 return baleType;
816 end
817 end
818 end
819 end
820 return nil;
821end

pickupWrapperBale

Description
Pickup bale to wrap
Definition
pickupWrapperBale(table bale, integer baleType)
Arguments
tablebalebale to pickup
integerbaleTypetype of bale
Code
827function BaleWrapper:pickupWrapperBale(bale, baleType)
828 if baleType ~= nil and bale.i3dFilename ~= baleType.wrapperBaleFilename then
829 local x,y,z = getWorldTranslation(bale.nodeId);
830 local rx,ry,rz = getWorldRotation(bale.nodeId);
831 local fillLevel = bale.fillLevel;
832 local baleValueScale = bale.baleValueScale;
833 bale:delete();
834
835 bale = Bale:new(self.isServer, self.isClient);
836 bale:load(baleType.wrapperBaleFilename, x,y,z, rx,ry,rz, fillLevel);
837 bale.baleValueScale = baleValueScale;
838 bale:register();
839 end
840
841 -- found bale
842 g_server:broadcastEvent(BaleWrapperStateEvent:new(self, BaleWrapper.CHANGE_GRAB_BALE, networkGetObjectId(bale)), true, nil, self);
843end

getBaleInRange

Description
Returns if nearest bale in range
Definition
getBaleInRange(integer refNode, float distance)
Arguments
integerrefNodeid of reference node
floatdistancemax distance
Return Values
tablenearestBalenearest bale
integernearestBaleTypeid of bale type
Code
851function BaleWrapper.getBaleInRange(self, refNode, distance)
852 local px, py, pz = getWorldTranslation(refNode);
853 local nearestDistance = distance;
854 local nearestBale = nil;
855 local nearestBaleType;
856
857 for _, item in pairs(g_currentMission.itemsToSave) do
858 local bale = item.item;
859 if bale:isa(Bale) then
860 local vx, vy, vz = getWorldTranslation(bale.nodeId);
861 local maxDist;
862 if bale.baleDiameter ~= nil then
863 maxDist = math.min(bale.baleDiameter, bale.baleWidth);
864 else
865 maxDist = math.min(bale.baleLength, bale.baleHeight, bale.baleWidth);
866 end
867 local _,_,z = localToLocal(bale.nodeId, refNode, 0,0,0);
868 if math.abs(z) < nearestDistance and Utils.vector3Length(px-vx, py-vy, pz-vz) < maxDist then
869 local foundBaleType;
870 if not bale.supportsWrapping or bale.wrappingState < 0.99 then
871 foundBaleType = self:getWrapperBaleType(bale);
872 end
873 if foundBaleType ~= nil or nearestBaleType == nil then
874 if foundBaleType ~= nil then
875 nearestDistance = distance;
876 end
877 nearestBale = bale;
878 nearestBaleType = foundBaleType;
879 end
880 end
881 end
882 end
883 return nearestBale, nearestBaleType;
884end

getIsFoldAllowed

Description
Returns if fold is allowed
Definition
getIsFoldAllowed(boolean onAiTurnOn)
Arguments
booleanonAiTurnOncalled on ai turn on
Return Values
booleanallowsFoldallows folding
Code
890function BaleWrapper:getIsFoldAllowed(superFunc, onAiTurnOn)
891 if self.baleWrapperState ~= BaleWrapper.STATE_NONE then
892 return false;
893 end
894 if superFunc ~= nil then
895 return superFunc(self, onAiTurnOn);
896 end
897 return true;
898end