LUADOC - Farming Simulator 17

Printable Version

Script v1.4.4.0

Engine v7.0.0.2

Foundation Reference

Foldable

Description
Class for vehicles which have folding parts
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
19function Foldable.prerequisitesPresent(specializations)
20 return true;
21end

preLoad

Description
Called before loading
Definition
preLoad(table savegame)
Arguments
tablesavegamesavegame
Code
26function Foldable:preLoad(savegame)
27 self.loadSpeedRotatingPartFromXML = Utils.overwrittenFunction(self.loadSpeedRotatingPartFromXML, Foldable.loadSpeedRotatingPartFromXML);
28 self.loadWorkAreaFromXML = Utils.overwrittenFunction(self.loadWorkAreaFromXML, Foldable.loadWorkAreaFromXML);
29 self.loadLevelerNodeFromXML = Utils.overwrittenFunction(self.loadLevelerNodeFromXML, Foldable.loadLevelerNodeFromXML);
30 self.loadDynamicWheelDataFromXML = Utils.overwrittenFunction(self.loadDynamicWheelDataFromXML, Foldable.loadDynamicWheelDataFromXML);
31end

load

Description
Called on loading
Definition
load(table savegame)
Arguments
tablesavegamesavegame
Code
36function Foldable:load(savegame)
37
38 self.setFoldDirection = SpecializationUtil.callSpecializationsFunction("setFoldDirection");
39 self.setFoldState = SpecializationUtil.callSpecializationsFunction("setFoldState");
40 self.getToggledFoldDirection = Foldable.getToggledFoldDirection;
41 self.getToggledFoldMiddleDirection = Foldable.getToggledFoldMiddleDirection;
42
43 self.getIsFoldAllowed = Utils.overwrittenFunction(self.getIsFoldAllowed, Foldable.getIsFoldAllowed);
44 self.getIsFoldMiddleAllowed = Utils.overwrittenFunction(self.getIsFoldMiddleAllowed, Foldable.getIsFoldMiddleAllowed);
45 self.getIsSpeedRotatingPartActive = Utils.overwrittenFunction(self.getIsSpeedRotatingPartActive, Foldable.getIsSpeedRotatingPartActive);
46 self.getIsVersatileYRotActive = Utils.overwrittenFunction(self.getIsVersatileYRotActive, Foldable.getIsVersatileYRotActive);
47 self.getIsLevelerPickupNodeActive = Utils.overwrittenFunction(self.getIsLevelerPickupNodeActive, Foldable.getIsLevelerPickupNodeActive);
48 self.getIsWorkAreaActive = Utils.overwrittenFunction(self.getIsWorkAreaActive, Foldable.getIsWorkAreaActive);
49 self.getIsTurnedOnAllowed = Utils.overwrittenFunction(self.getIsTurnedOnAllowed, Foldable.getIsTurnedOnAllowed);
50 self.getIsToggleCoverAllowed = Utils.overwrittenFunction(self.getIsToggleCoverAllowed, Foldable.getIsToggleCoverAllowed);
51 self.getIsUnfolded = Utils.overwrittenFunction(self.getIsUnfolded, Foldable.getIsUnfolded);
52 self.getIsInWorkPosition = Utils.overwrittenFunction(self.getIsInWorkPosition, Foldable.getIsInWorkPosition);
53 self.isLowered = Utils.overwrittenFunction(self.isLowered, Foldable.isLowered);
54 self.getTurnedOnNotAllowedWarning = Utils.overwrittenFunction(self.getTurnedOnNotAllowedWarning, Foldable.getTurnedOnNotAllowedWarning);
55 self.isDetachAllowed = Utils.overwrittenFunction(self.isDetachAllowed, Foldable.isDetachAllowed);
56 self.applyInitialAnimation = Utils.overwrittenFunction(self.applyInitialAnimation, Foldable.applyInitialAnimation);
57 self.allowLoadMovingToolStates = Utils.overwrittenFunction(self.allowLoadMovingToolStates, Foldable.allowLoadMovingToolStates);
58
59 self.posDirectionText = Utils.getNoNil(getXMLString(self.xmlFile, "vehicle.foldingParts#posDirectionText"), "action_foldOBJECT");
60 self.negDirectionText = Utils.getNoNil(getXMLString(self.xmlFile, "vehicle.foldingParts#negDirectionText"), "action_unfoldOBJECT");
61 self.middlePosDirectionText = Utils.getNoNil(getXMLString(self.xmlFile, "vehicle.foldingParts#middlePosDirectionText"), "action_liftOBJECT");
62 self.middleNegDirectionText = Utils.getNoNil(getXMLString(self.xmlFile, "vehicle.foldingParts#middleNegDirectionText"), "action_lowerOBJECT");
63 self.startAnimTime = getXMLFloat(self.xmlFile, "vehicle.foldingParts#startAnimTime");
64 self.foldMoveDirection = 0;
65 self.moveToMiddle = false;
66 if self.startAnimTime == nil then
67 self.startAnimTime = 0;
68 local startMoveDirection = Utils.getNoNil(getXMLInt(self.xmlFile, "vehicle.foldingParts#startMoveDirection"), 0);
69 if startMoveDirection > 0.1 then
70 self.startAnimTime = 1;
71 end
72 end
73 self.turnOnFoldDirection = 1;
74 if self.startAnimTime > 0.5 then
75 self.turnOnFoldDirection = -1;
76 end
77
78 self.turnOnFoldDirection = Utils.sign(Utils.getNoNil(getXMLInt(self.xmlFile, "vehicle.foldingParts#turnOnFoldDirection"), self.turnOnFoldDirection));
79
80 local foldInputButtonStr = getXMLString(self.xmlFile, "vehicle.foldingParts#foldInputButton");
81 if foldInputButtonStr ~= nil then
82 self.foldInputButton = InputBinding[foldInputButtonStr];
83 end
84 self.foldInputButton = Utils.getNoNil(self.foldInputButton, InputBinding.IMPLEMENT_EXTRA2);
85
86 local foldMiddleInputButtonStr = getXMLString(self.xmlFile, "vehicle.foldingParts#foldMiddleInputButton");
87 if foldMiddleInputButtonStr ~= nil then
88 self.foldMiddleInputButton = InputBinding[foldMiddleInputButtonStr];
89 end
90 self.foldMiddleInputButton = Utils.getNoNil(self.foldMiddleInputButton, InputBinding.LOWER_IMPLEMENT);
91
92 self.foldMiddleAnimTime = getXMLFloat(self.xmlFile, "vehicle.foldingParts#foldMiddleAnimTime");
93 self.foldMiddleDirection = Utils.getNoNil(getXMLInt(self.xmlFile, "vehicle.foldingParts#foldMiddleDirection"), 1);
94 self.foldMiddleAIRaiseDirection = Utils.getNoNil(getXMLInt(self.xmlFile, "vehicle.foldingParts#foldMiddleAIRaiseDirection"), self.foldMiddleDirection);
95
96 self.turnOnFoldMaxLimit = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.foldingParts#turnOnFoldMaxLimit"), 1);
97 self.turnOnFoldMinLimit = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.foldingParts#turnOnFoldMinLimit"), 0);
98 self.toggleCoverMaxLimit = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.foldingParts#toggleCoverMaxLimit"), 1);
99 self.toggleCoverMinLimit = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.foldingParts#toggleCoverMinLimit"), 0);
100 self.detachingMaxLimit = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.foldingParts#detachingMaxLimit"), 1);
101 self.detachingMinLimit = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.foldingParts#detachingMinLimit"), 0);
102 self.loadMovingToolStatesMaxLimit = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.foldingParts#loadMovingToolStatesMaxLimit"), 1);
103 self.loadMovingToolStatesMinLimit = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.foldingParts#loadMovingToolStatesMinLimit"), 0);
104 self.turnOffOnFold = Utils.getNoNil(getXMLBool(self.xmlFile, "vehicle.foldingParts#turnOffOnFold"), false);
105 self.closeCoverOnFold = Utils.getNoNil(getXMLBool(self.xmlFile, "vehicle.foldingParts#closeCoverOnFold"), false);
106 self.onlyFoldOnDetach = Utils.getNoNil(getXMLBool(self.xmlFile, "vehicle.foldingParts#onlyFoldOnDetach"), false);
107 self.onlyFoldOnEmpty = Utils.getNoNil(getXMLBool(self.xmlFile, "vehicle.foldingParts#onlyFoldOnEmpty"), false);
108 self.unfoldWarning = string.format(g_i18n:getText(Utils.getNoNil(getXMLString(self.xmlFile, "vehicle.foldingParts#unfoldWarning"), "warning_firstUnfoldTheTool")), self.typeDesc);
109
110 self.foldAnimTime = 0;
111 self.maxFoldAnimDuration = 0.0001;
112
113 self.foldingParts = {};
114 local i=0;
115 while true do
116 local baseName = string.format("vehicle.foldingParts.foldingPart(%d)", i);
117 if not hasXMLProperty(self.xmlFile, baseName) then
118 break;
119 end
120
121 local isValid = false;
122 local entry = {};
123 entry.speedScale = Utils.getNoNil(getXMLFloat(self.xmlFile, baseName.."#speedScale"), 1);
124 local componentJointIndex = getXMLInt(self.xmlFile, baseName.. "#componentJointIndex");
125 local componentJoint = nil;
126 if componentJointIndex ~= nil then
127 componentJoint = self.componentJoints[componentJointIndex+1];
128 entry.componentJoint = componentJoint;
129 end
130 entry.anchorActor = Utils.getNoNil(getXMLInt(self.xmlFile, baseName.."#anchorActor"), 0);
131
132 entry.animCharSet = 0;
133
134 local rootNode = Utils.indexToObject(self.components, getXMLString(self.xmlFile, baseName.."#rootNode"));
135 if rootNode ~= nil then
136 local animCharSet = getAnimCharacterSet(rootNode);
137 if animCharSet ~= 0 then
138 local clip = getAnimClipIndex(animCharSet, getXMLString(self.xmlFile, baseName.."#animationClip"));
139 if clip >= 0 then
140 isValid = true;
141
142 entry.animCharSet = animCharSet;
143 assignAnimTrackClip(entry.animCharSet, 0, clip);
144 setAnimTrackLoopState(entry.animCharSet, 0, false);
145 entry.animDuration = getAnimClipDuration(entry.animCharSet, clip);
146
147 end
148 end
149 end
150 -- try AnimatedVehicle specialization support
151 if not isValid and self.playAnimation ~= nil and self.animations ~= nil then
152 local animationName = getXMLString(self.xmlFile, baseName.."#animationName");
153 if animationName ~= nil then
154 if self.animations[animationName] ~= nil then
155 isValid = true;
156 entry.animDuration = self:getAnimationDuration(animationName);
157 entry.animationName = animationName;
158 end
159 end
160 end
161
162 if isValid then
163 self.maxFoldAnimDuration = math.max(self.maxFoldAnimDuration, entry.animDuration);
164 if componentJoint ~= nil then
165 local node = self.components[componentJoint.componentIndices[((entry.anchorActor+1)%2)+1] ].node;
166 entry.x,entry.y,entry.z = worldToLocal(componentJoint.jointNode, getWorldTranslation(node));
167 entry.upX,entry.upY,entry.upZ = worldDirectionToLocal(componentJoint.jointNode, localDirectionToWorld(node, 0, 1, 0));
168 entry.dirX,entry.dirY,entry.dirZ = worldDirectionToLocal(componentJoint.jointNode, localDirectionToWorld(node, 0, 0, 1));
169 end
170
171 table.insert(self.foldingParts, entry);
172 end
173 i = i+1;
174 end
175
176 if table.getn(self.foldingParts) > 0 then
177 self.isSelectable = true;
178 end
179end

postLoad

Description
Called after loading
Definition
postLoad(table savegame)
Arguments
tablesavegamesavegame
Code
184function Foldable:postLoad(savegame)
185 local animTime = nil
186 if savegame ~= nil and not savegame.resetVehicles then
187 animTime = getXMLFloat(savegame.xmlFile, savegame.key.."#foldAnimTime");
188 end
189 if animTime == nil then
190 animTime = self.startAnimTime;
191 end
192
193 Foldable.setAnimTime(self, animTime, false)
194end

applyInitialAnimation

Description
Called at the end of loading to apply an initial animation state
Definition
applyInitialAnimation()
Code
198function Foldable:applyInitialAnimation(superFunc)
199 self.foldAnimTime = 1-self.startAnimTime; -- make sure that foldAnimTime ~= startAnimTime
200 Foldable.setAnimTime(self, self.startAnimTime)
201 if superFunc ~= nil then
202 superFunc(self);
203 end
204end

readStream

Description
Called on client side on join
Definition
readStream(integer streamId, integer connection)
Arguments
integerstreamIdstreamId
integerconnectionconnection
Code
213function Foldable:readStream(streamId, connection)
214 local direction = streamReadUIntN(streamId, 2)-1;
215 local moveToMiddle = streamReadBool(streamId);
216 local animTime = streamReadFloat32(streamId);
217 Foldable.setAnimTime(self, animTime, false);
218 self:setFoldState(direction, moveToMiddle, true);
219end

writeStream

Description
Called on server side on join
Definition
writeStream(integer streamId, integer connection)
Arguments
integerstreamIdstreamId
integerconnectionconnection
Code
225function Foldable:writeStream(streamId, connection)
226 local direction = Utils.sign(self.foldMoveDirection)+1;
227 streamWriteUIntN(streamId, direction, 2);
228 streamWriteBool(streamId, self.moveToMiddle);
229 streamWriteFloat32(streamId, self.foldAnimTime);
230end

getSaveAttributesAndNodes

Description
Returns attributes and nodes to save
Definition
getSaveAttributesAndNodes(table nodeIdent)
Arguments
tablenodeIdentnode ident
Return Values
stringattributesattributes
stringnodesnodes
Code
243function Foldable:getSaveAttributesAndNodes(nodeIdent)
244 local attributes = 'foldAnimTime="'..self.foldAnimTime..'"';
245 return attributes, nil;
246end

setRelativePosition

Description
Called on setting position of vehicle (e. g. loading or reseting vehicle)
Definition
setRelativePosition(float positionX, float offsetY, float positionZ, float yRot)
Arguments
floatpositionXx position
floatoffsetYy offset
floatpositionZz position
floatyRoty rotation
Code
254function Foldable:setRelativePosition(positionX, offsetY, positionZ, yRot)
255 if self.startAnimTime ~= nil then
256 self.foldAnimTime = 1-self.startAnimTime; -- make sure that foldAnimTime ~= startAnimTime
257 Foldable.setAnimTime(self, self.startAnimTime);
258 end
259end

keyEvent

Description
Called on key event
Definition
keyEvent(integer unicode, integer sym, integer modifier, boolean isDown)
Arguments
integerunicodeunicode
integersymsym
integermodifiermodifier
booleanisDownis button down
Code
270function Foldable:keyEvent(unicode, sym, modifier, isDown)
271end

update

Description
Called on update
Definition
update(float dt)
Arguments
floatdttime since last call in ms
Code
276function Foldable:update(dt)
277
278 if self:getIsActive() then
279
280 if math.abs(self.foldMoveDirection) > 0.1 then
281 local isInvalid = false;
282 local foldAnimTime = 0;
283 if self.foldMoveDirection < -0.1 then
284 foldAnimTime = 1;
285 end
286 for _,foldingPart in pairs(self.foldingParts) do
287 local charSet = foldingPart.animCharSet;
288 if self.foldMoveDirection > 0 then
289 local animTime = 0;
290 if charSet ~= 0 then
291 animTime = getAnimTrackTime(charSet, 0);
292 else
293 animTime = self:getRealAnimationTime(foldingPart.animationName);
294 end
295 if animTime < foldingPart.animDuration then
296 isInvalid = true;
297 end
298 foldAnimTime = math.max(foldAnimTime, animTime / self.maxFoldAnimDuration);
299 elseif self.foldMoveDirection < 0 then
300 local animTime = 0;
301 if charSet ~= 0 then
302 animTime = getAnimTrackTime(charSet, 0);
303 else
304 animTime = self:getRealAnimationTime(foldingPart.animationName);
305 end
306 if animTime > 0 then
307 isInvalid = true;
308 end
309 foldAnimTime = math.min(foldAnimTime, animTime / self.maxFoldAnimDuration);
310 end
311 end
312 self.foldAnimTime = Utils.clamp(foldAnimTime, 0, 1);
313
314 if isInvalid and self.isServer then
315 for _,foldingPart in pairs(self.foldingParts) do
316 if foldingPart.componentJoint ~= nil then
317 self:setComponentJointFrame(foldingPart.componentJoint, foldingPart.anchorActor)
318 end
319 end
320 end
321 end
322 end
323
324 if self.isClient and table.getn(self.foldingParts) > 0 and self:getIsActiveForInput(true) then
325 if InputBinding.hasEvent(self.foldInputButton) then
326 if self:getIsFoldAllowed() then
327 if self:getToggledFoldDirection() == self.turnOnFoldDirection then
328 self:setFoldState(self:getToggledFoldDirection(), true);
329 else
330 self:setFoldState(self:getToggledFoldDirection(), false);
331 end
332 end
333 end
334 if self.foldMiddleAnimTime ~= nil and self.foldMiddleInputButton ~= nil then
335 if InputBinding.hasEvent(self.foldMiddleInputButton) then
336 if self:getIsFoldMiddleAllowed() then
337 local direction = self:getToggledFoldMiddleDirection();
338 if direction ~= 0 then
339 if direction == self.turnOnFoldDirection then
340 self:setFoldState(direction, false)
341 else
342 self:setFoldState(direction, true)
343 end
344 end
345 end
346 end
347 end
348 end
349end

updateTick

Description
Called on update tick
Definition
updateTick(float dt)
Arguments
floatdttime since last call in ms
Code
354function Foldable:updateTick(dt)
355 if self:getIsActive() then
356 if self.turnOffOnFold then
357 if not self:getIsTurnedOnAllowed(true) then
358 if self.setIsTurnedOn ~= nil and self:getIsTurnedOn() then
359 self:setIsTurnedOn(false, true);
360 elseif self.attacherVehicle ~= nil and self.attacherVehicle.setIsTurnedOn ~= nil and self.attacherVehicle:getIsTurnedOn() then
361 self.attacherVehicle:setIsTurnedOn(false, true);
362 end
363 end
364 end
365 if self.closeCoverOnFold then
366 if not self:getIsToggleCoverAllowed(true) then
367 if self.setCoverState ~= nil and self.isCoverOpen then
368 self:setCoverState(false, true);
369 end;
370 end;
371 end;
372 if self.onlyFoldOnEmpty then
373 if self:getFillLevel() > 0 and self.foldAnimTime == 1 then
374 self:setFoldState(self:getToggledFoldDirection(), false, true);
375 end;
376 end;
377 end
378end

draw

Description
Called on draw
Definition
draw()
Code
382function Foldable:draw()
383 if self.isClient then
384 if table.getn(self.foldingParts) > 0 and self:getIsActiveForInput(true) then
385 local i18n = g_i18n;
386 if self.customEnvironment ~= nil then
387 i18n = _G[self.customEnvironment].g_i18n;
388 end
389 if self:getIsFoldAllowed() then
390 if self.foldMiddleAnimTime == nil or self.foldMiddleAnimTime ~= 1 then
391 if self:getToggledFoldDirection() > 0 then
392 g_currentMission:addHelpButtonText(string.format(i18n:getText(self.posDirectionText), self.typeDesc), self.foldInputButton, nil, GS_PRIO_HIGH);
393 else
394 g_currentMission:addHelpButtonText(string.format(i18n:getText(self.negDirectionText), self.typeDesc), self.foldInputButton, nil, GS_PRIO_HIGH);
395 end
396 end
397 end
398 if self:getIsFoldMiddleAllowed() then
399 if self.foldMiddleAnimTime ~= nil and self.foldMiddleInputButton ~= nil then
400 local direction = self:getToggledFoldMiddleDirection();
401 if direction > 0 then
402 g_currentMission:addHelpButtonText(string.format(i18n:getText(self.middlePosDirectionText), self.typeDesc), self.foldMiddleInputButton, nil, GS_PRIO_HIGH);
403 elseif direction < 0 then
404 g_currentMission:addHelpButtonText(string.format(i18n:getText(self.middleNegDirectionText), self.typeDesc), self.foldMiddleInputButton, nil, GS_PRIO_HIGH);
405 end
406 end
407 end
408 end
409 end
410end

onDeactivate

Description
Called on deactivate
Definition
onDeactivate()
Code
414function Foldable:onDeactivate()
415 self:setFoldDirection(0, true);
416end

onAiTurnOn

Description
Called on ai turn on
Definition
onAiTurnOn()
Code
420function Foldable:onAiTurnOn()
421 if self:getIsFoldAllowed(true) then
422 if self.turnOnFoldDirection ~= 0 then
423 --if self.foldMiddleAnimTime ~= nil then
424 -- self:setFoldState(-self.foldMiddleAIRaiseDirection, true, false);
425 --else
426 self:setFoldState(self.turnOnFoldDirection, false, false);
427 --end
428 end
429 end
430end

onAiTurnOff

Description
Called on ai turn off
Definition
onAiTurnOff()
Code
434function Foldable:onAiTurnOff()
435 if self.foldMiddleAnimTime ~= nil and self.foldMiddleAIRaiseDirection ~= 0 then
436 if self.foldMiddleAIRaiseDirection > 0 then
437 if self.foldAnimTime > self.foldMiddleAnimTime then
438 self:setFoldState(self.foldMiddleAIRaiseDirection, false, true);
439 else
440 self:setFoldState(self.foldMiddleAIRaiseDirection, true, true);
441 end
442 else
443 if self.foldAnimTime < self.foldMiddleAnimTime then
444 self:setFoldState(self.foldMiddleAIRaiseDirection, false, true);
445 else
446 self:setFoldState(self.foldMiddleAIRaiseDirection, true, true);
447 end
448 end
449 end
450end

onAiLower

Description
Called on ai lower
Definition
onAiLower()
Code
454function Foldable:onAiLower()
455 if self.foldMiddleAnimTime ~= nil and self.foldMiddleAIRaiseDirection ~= 0 then
456 self:setFoldState(-self.foldMiddleAIRaiseDirection, false, false);
457 end
458end

onAiRaise

Description
Called on ai raise
Definition
onAiRaise()
Code
462function Foldable:onAiRaise()
463 if self.foldMiddleAnimTime ~= nil and self.foldMiddleAIRaiseDirection ~= 0 then
464 if self.foldMiddleAIRaiseDirection > 0 then
465 if self.foldAnimTime < self.foldMiddleAnimTime then
466 self:setFoldState(1, true, false);
467 end
468 else
469 if self.foldAnimTime > self.foldMiddleAnimTime then
470 self:setFoldState(-1, true, false);
471 end
472 end
473 end
474end

getIsAIReadyForWork

Description
Returns true if vehicle is ready for ai work
Definition
getIsAIReadyForWork()
Return Values
booleanisReadyis ready for ai work
Code
479function Foldable:getIsAIReadyForWork()
480 if table.getn(self.foldingParts) > 0 then
481 if self.turnOnFoldDirection == -1 then
482 if self.foldAnimTime > 0 then
483 if self.foldMoveDirection ~= -1 or (self.foldMiddleAnimTime ~= nil and self.moveToMiddle == true) then
484 self:setFoldState(-1, false);
485 end
486 return false;
487 else
488 return true;
489 end
490 else
491 if self.foldAnimTime < 1 then
492 if self.foldMoveDirection ~= 1 or (self.foldMiddleAnimTime ~= nil and self.moveToMiddle == true) then
493 self:setFoldState(1, false);
494 end
495 return false;
496 else
497 return true;
498 end
499 end
500 else
501 return true;
502 end
503end

startThreshing

Description
Start threshing
Definition
startThreshing()
Code
507function Foldable:startThreshing()
508 if self.foldMiddleAnimTime ~= nil and self.foldMiddleAIRaiseDirection ~= 0 then
509 self:setFoldState(-self.foldMiddleAIRaiseDirection, false, true);
510 end
511end

stopThreshing

Description
Stop threshing
Definition
stopThreshing()
Code
515function Foldable:stopThreshing()
516 if self.foldMiddleAnimTime ~= nil and self.foldMiddleAIRaiseDirection ~= 0 then
517 self:setFoldState(self.foldMiddleAIRaiseDirection, true, true);
518 end
519end

setFoldDirection

Description
Set fold direction
Definition
setFoldDirection(integer direction, boolean noEventSend)
Arguments
integerdirectionnew direction
booleannoEventSendno event send
Code
525function Foldable:setFoldDirection(direction, noEventSend)
526 self:setFoldState(direction, false, noEventSend);
527end

setFoldState

Description
Set fold state
Definition
setFoldState(integer direction, boolean moveToMiddle, boolean noEventSend)
Arguments
integerdirectiondirection
booleanmoveToMiddlemove to middle position
booleannoEventSendno event send
Code
534function Foldable:setFoldState(direction, moveToMiddle, noEventSend)
535 if self.foldMiddleAnimTime == nil then
536 moveToMiddle = false;
537 end
538 if self.foldMoveDirection ~= direction or self.moveToMiddle ~= moveToMiddle then
539 if noEventSend == nil or noEventSend == false then
540 if g_server ~= nil then
541 g_server:broadcastEvent(FoldableSetFoldDirectionEvent:new(self, direction, moveToMiddle), nil, nil, self);
542 else
543 g_client:getServerConnection():sendEvent(FoldableSetFoldDirectionEvent:new(self, direction, moveToMiddle));
544 end
545 end
546 self.foldMoveDirection = direction;
547 self.moveToMiddle = moveToMiddle;
548
549 for _,foldingPart in pairs(self.foldingParts) do
550 local speedScale = nil;
551 -- We don't do any animations if we are already past the middle time
552 if self.foldMoveDirection > 0.1 then
553 if not self.moveToMiddle or self.foldAnimTime < self.foldMiddleAnimTime then
554 speedScale = foldingPart.speedScale;
555 end
556 elseif self.foldMoveDirection < -0.1 then
557 if not self.moveToMiddle or self.foldAnimTime > self.foldMiddleAnimTime then
558 speedScale = -foldingPart.speedScale;
559 end
560 end
561
562 local charSet = foldingPart.animCharSet;
563 if charSet ~= 0 then
564 if speedScale ~= nil then
565 if speedScale > 0 then
566 if getAnimTrackTime(charSet, 0) < 0.0 then
567 setAnimTrackTime(charSet, 0, 0.0);
568 end
569 else
570 if getAnimTrackTime(charSet, 0) > foldingPart.animDuration then
571 setAnimTrackTime(charSet, 0, foldingPart.animDuration);
572 end
573 end
574 setAnimTrackSpeedScale(charSet, 0, speedScale);
575 enableAnimTrack(charSet, 0);
576 else
577 disableAnimTrack(charSet, 0);
578 end
579 else
580 -- always stop to make sure the animation state is reset
581 local animTime;
582 if self:getIsAnimationPlaying(foldingPart.animationName) then
583 animTime = self:getAnimationTime(foldingPart.animationName);
584 else
585 animTime = (self.foldAnimTime*self.maxFoldAnimDuration)/self:getAnimationDuration(foldingPart.animationName);
586 end
587 self:stopAnimation(foldingPart.animationName, true);
588 if speedScale ~= nil then
589 self:playAnimation(foldingPart.animationName, speedScale, animTime, true);
590
591 if moveToMiddle then
592 local stopAnimTime = (self.foldMiddleAnimTime*self.maxFoldAnimDuration)/self:getAnimationDuration(foldingPart.animationName);
593 self:setAnimationStopTime(foldingPart.animationName, stopAnimTime);
594 end
595 end
596 end
597 end
598 -- slightly move fold anim time, so that fold limits can trigger for different actions
599 if self.foldMoveDirection > 0.1 then
600 self.foldAnimTime = math.min(self.foldAnimTime + 0.0001, math.max(self.foldAnimTime, 1));
601 elseif self.foldMoveDirection < -0.1 then
602 self.foldAnimTime = math.max(self.foldAnimTime - 0.0001, math.min(self.foldAnimTime, 0));
603 end
604 end
605end

loadSpeedRotatingPartFromXML

Description
Loads speed rotating parts from xml
Definition
loadSpeedRotatingPartFromXML(table speedRotatingPart, integer xmlFile, string key)
Arguments
tablespeedRotatingPartspeedRotatingPart
integerxmlFileid of xml object
stringkeykey
Return Values
booleansuccesssuccess
Code
613function Foldable:loadSpeedRotatingPartFromXML(superFunc, speedRotatingPart, xmlFile, key)
614 if superFunc ~= nil then
615 if not superFunc(self, speedRotatingPart, xmlFile, key) then
616 return false;
617 end
618 end
619 speedRotatingPart.foldLimitedOuterRange = Utils.getNoNil(getXMLBool(xmlFile, key.."#foldLimitedOuterRange"), false);
620 local minFoldLimit = 0;
621 local maxFoldLimit = 1;
622 if speedRotatingPart.foldLimitedOuterRange then
623 minFoldLimit = 0.5;
624 maxFoldLimit = 0.5;
625 end
626 speedRotatingPart.foldMinLimit = Utils.getNoNil(getXMLFloat(xmlFile, key.."#foldMinLimit"), minFoldLimit);
627 speedRotatingPart.foldMaxLimit = Utils.getNoNil(getXMLFloat(xmlFile, key.."#foldMaxLimit"), maxFoldLimit);
628
629 return true;
630end

getIsSpeedRotatingPartActive

Description
Returns true if speed rotating part is active
Definition
getIsSpeedRotatingPartActive(table speedRotatingPart)
Arguments
tablespeedRotatingPartspeedRotatingPart
Return Values
booleanisActivespeed rotating part is active
Code
636function Foldable:getIsSpeedRotatingPartActive(superFunc, speedRotatingPart)
637 if not speedRotatingPart.foldLimitedOuterRange then
638 if self.foldAnimTime > speedRotatingPart.foldMaxLimit or self.foldAnimTime < speedRotatingPart.foldMinLimit then
639 return false;
640 end
641 else
642 if self.foldAnimTime <= speedRotatingPart.foldMaxLimit and self.foldAnimTime > speedRotatingPart.foldMinLimit then
643 return false;
644 end
645 end
646
647 if superFunc ~= nil then
648 return superFunc(self, speedRotatingPart);
649 end
650 return true;
651end

loadDynamicWheelDataFromXML

Description
Loading dynamic wheel data from xml
Definition
loadDynamicWheelDataFromXML(integer xmlFile, string key, string wheelnamei, table wheel)
Arguments
integerxmlFileid of xml object
stringkeykey to load
stringwheelnameiwheel name with index
tablewheelwheel table to write in
Return Values
booleansuccesssuccess
Code
660function Foldable:loadDynamicWheelDataFromXML(superFunc, xmlFile, key, wheelnamei, wheel)
661 local fallbackOldKey = "vehicle.wheels";
662 wheel.versatileFoldMinLimit = Vehicle.getConfigurationValue(xmlFile, key, wheelnamei, "#versatileFoldMinLimit", getXMLFloat, 0, nil, fallbackOldKey);
663 wheel.versatileFoldMaxLimit = Vehicle.getConfigurationValue(xmlFile, key, wheelnamei, "#versatileFoldMaxLimit", getXMLFloat, 1, nil, fallbackOldKey);
664
665 if superFunc ~= nil then
666 return superFunc(self, xmlFile, key, wheelnamei, wheel);
667 end
668 return true;
669end

getIsVersatileYRotActive

Description
Returns of versatile y rot is active
Definition
getIsVersatileYRotActive(table wheel)
Arguments
tablewheelwheel to check
Return Values
booleanisActiveis active
Code
675function Foldable:getIsVersatileYRotActive(superFunc, wheel)
676 if self.foldAnimTime > wheel.versatileFoldMaxLimit or self.foldAnimTime < wheel.versatileFoldMinLimit then
677 return false;
678 end
679
680 if superFunc ~= nil then
681 return superFunc(self, wheel);
682 end
683 return true;
684end

loadWorkAreaFromXML

Description
Loads work areas from xml
Definition
loadWorkAreaFromXML(table workArea, integer xmlFile, string key)
Arguments
tableworkAreaworkArea
integerxmlFileid of xml object
stringkeykey
Return Values
booleansuccesssuccess
Code
692function Foldable:loadWorkAreaFromXML(superFunc, workArea, xmlFile, key)
693 workArea.foldLimitedOuterRange = Utils.getNoNil(getXMLBool(xmlFile, key.."#foldLimitedOuterRange"), false);
694 local minFoldLimit = 0;
695 local maxFoldLimit = 1;
696 if workArea.foldLimitedOuterRange then
697 minFoldLimit = 0.5;
698 maxFoldLimit = 0.5;
699 end
700 workArea.foldMinLimit = Utils.getNoNil(getXMLFloat(xmlFile, key.."#foldMinLimit"), minFoldLimit);
701 workArea.foldMaxLimit = Utils.getNoNil(getXMLFloat(xmlFile, key.."#foldMaxLimit"), maxFoldLimit);
702
703
704 if superFunc ~= nil then
705 return superFunc(self, workArea, xmlFile, key);
706 end
707 return true;
708end

getIsWorkAreaActive

Description
Returns true if work area is active
Definition
getIsWorkAreaActive(table workArea)
Arguments
tableworkAreaworkArea
Return Values
booleanisActivework area is active
Code
714function Foldable:getIsWorkAreaActive(superFunc, workArea)
715 if not workArea.foldLimitedOuterRange then
716 if self.foldAnimTime > workArea.foldMaxLimit or self.foldAnimTime < workArea.foldMinLimit then
717 return false;
718 end
719 else
720 if self.foldAnimTime <= workArea.foldMaxLimit and self.foldAnimTime > workArea.foldMinLimit then
721 return false;
722 end
723 end
724 if superFunc ~= nil then
725 return superFunc(self, workArea);
726 end
727 return true;
728end

loadLevelerNodeFromXML

Description
Loads leveler node from xml
Definition
loadLevelerNodeFromXML(table entry, integer xmlFile, string key)
Arguments
tableentryleveler node data
integerxmlFileid of xml object
stringkeykey
Return Values
booleansuccesssuccess
Code
737function Foldable:loadLevelerNodeFromXML(superFunc, levelerNode, xmlFile, key)
738 levelerNode.foldLimitedOuterRange = Utils.getNoNil(getXMLBool(xmlFile, key.."#foldLimitedOuterRange"), false);
739 local minFoldLimit = 0;
740 local maxFoldLimit = 1;
741 if levelerNode.foldLimitedOuterRange then
742 minFoldLimit = 0.5;
743 maxFoldLimit = 0.5;
744 end
745 levelerNode.foldMinLimit = Utils.getNoNil(getXMLFloat(xmlFile, key.."#foldMinLimit"), minFoldLimit);
746 levelerNode.foldMaxLimit = Utils.getNoNil(getXMLFloat(xmlFile, key.."#foldMaxLimit"), maxFoldLimit);
747
748
749 if superFunc ~= nil then
750 return superFunc(self, levelerNode, xmlFile, key);
751 end
752 return true;
753end

getIsLevelerPickupNodeActive

Description
Returns true if leveler pickup node is active
Definition
getIsLevelerPickupNodeActive(table levelerNode)
Arguments
tablelevelerNodepickup node data
Return Values
booleanisActivepickup node is active
Code
759function Foldable:getIsLevelerPickupNodeActive(superFunc, levelerNode)
760 if not levelerNode.foldLimitedOuterRange then
761 if self.foldAnimTime > levelerNode.foldMaxLimit or self.foldAnimTime < levelerNode.foldMinLimit then
762 return false;
763 end
764 else
765 if self.foldAnimTime <= levelerNode.foldMaxLimit and self.foldAnimTime > levelerNode.foldMinLimit then
766 return false;
767 end
768 end
769 if superFunc ~= nil then
770 return superFunc(self, levelerNode);
771 end
772 return true;
773end

getIsTurnedOnAllowed

Description
Returns if turn on is allowed
Definition
getIsTurnedOnAllowed(boolean isTurnedOn)
Arguments
booleanisTurnedOnis turned on
Return Values
booleanallowallow turn on
Code
779function Foldable:getIsTurnedOnAllowed(superFunc, isTurnOn)
780 if isTurnOn then
781 if self.foldAnimTime > self.turnOnFoldMaxLimit or self.foldAnimTime < self.turnOnFoldMinLimit then
782 return false;
783 end
784 end
785
786 if superFunc ~= nil then
787 return superFunc(self, isTurnOn);
788 end
789 return true;
790end

getIsToggleCoverAllowed

Description
Returns if toggle cover is allowed
Definition
getIsToggleCoverAllowed(boolean isCoverOpen)
Arguments
booleanisCoverOpenis cover open
Return Values
booleanallowallows cover toggle
Code
796function Foldable:getIsToggleCoverAllowed(superFunc, isCoverOpen)
797 if isCoverOpen then
798 if self.foldAnimTime > self.toggleCoverMaxLimit or self.foldAnimTime < self.toggleCoverMinLimit then
799 return false;
800 end
801 end
802
803 if superFunc ~= nil then
804 return superFunc(self, isCoverOpen);
805 end
806 return true;
807end

getIsUnfolded

Description
Returns if vehicle is unfolded
Definition
getIsUnfolded()
Return Values
booleanisUnfoldedis unfolded
Code
812function Foldable:getIsUnfolded()
813 if table.getn(self.foldingParts) > 0 then
814 if self.foldMiddleAnimTime ~= nil then
815 if (self.turnOnFoldDirection == -1 and self.foldAnimTime < self.foldMiddleAnimTime + 0.01) or
816 (self.turnOnFoldDirection == 1 and self.foldAnimTime > self.foldMiddleAnimTime - 0.01)
817 then
818 return true;
819 else
820 return false;
821 end
822 else
823 if (self.turnOnFoldDirection == -1 and self.foldAnimTime == 0) or (self.turnOnFoldDirection == 1 and self.foldAnimTime == 1) then
824 return true;
825 else
826 return false;
827 end
828 end
829 else
830 return true;
831 end
832end

getIsInWorkPosition

Description
Returns true if it is in work position
Definition
getIsInWorkPosition()
Return Values
booleaninWorkPositionis in work position
Code
837function Foldable:getIsInWorkPosition(superFunc)
838 if self.turnOnFoldDirection ~= 0 and not (table.getn(self.foldingParts) == 0 or (self.turnOnFoldDirection == -1 and self.foldAnimTime == 0) or (self.turnOnFoldDirection == 1 and self.foldAnimTime == 1)) then
839 return false;
840 end
841
842 if superFunc ~= nil then
843 return superFunc(self);
844 end
845 return true;
846end

getTurnedOnNotAllowedWarning

Description
Returns turn on not allowed warning text
Definition
getTurnedOnNotAllowedWarning()
Return Values
stringwarningTextturn on not allowed warning text
Code
851function Foldable:getTurnedOnNotAllowedWarning(superFunc)
852 if self.foldAnimTime > self.turnOnFoldMaxLimit or self.foldAnimTime < self.turnOnFoldMinLimit then
853 return self.unfoldWarning;
854 end
855
856 if superFunc ~= nil then
857 return superFunc(self);
858 end
859 return nil;
860end

isDetachAllowed

Description
Returns true if detach is allowed
Definition
isDetachAllowed()
Return Values
booleandetachAlloweddetach is allowed
Code
865function Foldable:isDetachAllowed(superFunc)
866 if self.foldAnimTime > self.detachingMaxLimit or self.foldAnimTime < self.detachingMinLimit then
867 return false;
868 end
869
870
871 if superFunc ~= nil then
872 return superFunc(self);
873 end
874 return true;
875end

isLowered

Description
Returns if vehicle is lowered
Definition
isLowered(boolean default)
Arguments
booleandefaultdefault value
Return Values
booleanisLoweredvehicle is lowered
Code
881function Foldable:isLowered(superFunc, default)
882
883 if self:getIsFoldMiddleAllowed() then
884 if self.foldMiddleAnimTime ~= nil and self.foldMiddleInputButton ~= nil then
885
886 if self.foldMoveDirection ~= 0 then
887 if self.foldMiddleDirection > 0 then
888 if self.foldAnimTime < self.foldMiddleAnimTime + 0.01 then
889 return self.foldMoveDirection < 0 and self.moveToMiddle ~= true;
890 end
891 else
892 if self.foldAnimTime > self.foldMiddleAnimTime - 0.01 then
893 return self.foldMoveDirection > 0 and self.moveToMiddle ~= true;
894 end
895 end
896 else
897 if self.foldMiddleDirection > 0 and self.foldAnimTime < 0.01 then
898 return true;
899 elseif self.foldMiddleDirection < 0 and math.abs(1.0 - self.foldAnimTime) < 0.01 then
900 return true;
901 end
902 end
903
904 return false;
905
906 end
907 end
908
909 if superFunc ~= nil then
910 return superFunc(self, default);
911 end
912 return default;
913end

getIsFoldAllowed

Description
Returns if fold is allowed
Definition
getIsFoldAllowed(boolean onAiTurnOn)
Arguments
booleanonAiTurnOncalled on ai turn on
Return Values
booleanallowsFoldallows folding
Code
919function Foldable:getIsFoldAllowed(superFunc, onAiTurnOn)
920 if self.onlyFoldOnDetach and self.attacherVehicle ~= nil then
921 return false;
922 end;
923 if self.onlyFoldOnEmpty and self:getFillLevel() > 0 then
924 return false;
925 end;
926
927 if superFunc ~= nil then
928 return superFunc(self);
929 end
930 return true;
931end

getIsFoldMiddleAllowed

Description
Returns if fold middle is allowed
Definition
getIsFoldMiddleAllowed()
Return Values
booleanallowsFoldMiddleallows folding to middle position
Code
936function Foldable:getIsFoldMiddleAllowed(superFunc)
937 if superFunc ~= nil then
938 return superFunc(self);
939 end
940 return true;
941end

getToggledFoldDirection

Description
Returns toggled fold direction
Definition
getToggledFoldDirection()
Return Values
integerdirectiondirection
Code
946function Foldable:getToggledFoldDirection()
947 local foldMidTime = 0.5;
948 if self.foldMiddleAnimTime ~= nil then
949 if self.foldMiddleDirection > 0 then
950 foldMidTime = (1+self.foldMiddleAnimTime) * 0.5;
951 else
952 foldMidTime = self.foldMiddleAnimTime * 0.5;
953 end
954 end
955 if self.moveToMiddle then
956 return self.foldMiddleDirection;
957 elseif self.foldMoveDirection > 0.1 or (self.foldMoveDirection == 0 and self.foldAnimTime > foldMidTime) then
958 return -1;
959 else
960 return 1;
961 end
962end

getToggledFoldMiddleDirection

Description
Returns toggled fold middle direction
Definition
getToggledFoldMiddleDirection()
Return Values
integerdirectiondirection
Code
967function Foldable:getToggledFoldMiddleDirection()
968 local ret = 0;
969 if self.foldMiddleAnimTime ~= nil then
970 if self.foldMoveDirection > 0.1 then
971 ret = -1;
972 else
973 ret = 1;
974 end
975 if self.foldMiddleDirection > 0 then
976 if self.foldAnimTime >= self.foldMiddleAnimTime - 0.01 then
977 ret = -1;
978 end
979 else
980 if self.foldAnimTime <= self.foldMiddleAnimTime + 0.01 then
981 ret = 1;
982 end
983 end
984 end
985 return ret;
986end

onLowerAll

Description
Called on lower
Definition
onLowerAll(boolean doLowering, integer jointIndex)
Arguments
booleandoLoweringdo lowering
integerjointIndexindex of attacher joint
Code
992function Foldable:onLowerAll(doLowering, jointIndex)
993 if self.foldMiddleAnimTime ~= nil then
994 if self:getIsFoldMiddleAllowed() then
995 if doLowering then
996 self:setFoldState(-self.foldMiddleAIRaiseDirection, false, true);
997 else
998 self:setFoldState(self.foldMiddleAIRaiseDirection, true, true);
999 end
1000 end
1001 end
1002end

setAnimTime

Description
Sets animation time
Definition
setAnimTime(float animTime, boolean placeComponents)
Arguments
floatanimTimenew animation time
booleanplaceComponentsplace components
Code
1008function Foldable.setAnimTime(self, animTime, placeComponents)
1009 local currentTime = self.foldAnimTime;
1010 self.foldAnimTime = animTime;
1011 for _,foldingPart in pairs(self.foldingParts) do
1012 if foldingPart.animCharSet ~= 0 then
1013 enableAnimTrack(foldingPart.animCharSet, 0);
1014 setAnimTrackTime(foldingPart.animCharSet, 0, self.foldAnimTime*foldingPart.animDuration, true);
1015 disableAnimTrack(foldingPart.animCharSet, 0);
1016 else
1017 local speed = 1;
1018 if self.foldAnimTime < currentTime then
1019 speed = -1;
1020 end
1021 local stopAnimTime = (self.foldAnimTime*self.maxFoldAnimDuration)/self:getAnimationDuration(foldingPart.animationName);
1022 self:playAnimation(foldingPart.animationName, speed, nil, true);
1023 self:setAnimationStopTime(foldingPart.animationName, stopAnimTime);
1024 AnimatedVehicle.updateAnimationByName(self, foldingPart.animationName, 99999999);
1025 end
1026 end
1027
1028 if placeComponents == nil then
1029 placeComponents = true;
1030 end
1031
1032 if self.updateCylinderedInitial ~= nil then
1033 self:updateCylinderedInitial(placeComponents);
1034 end
1035
1036 if placeComponents then
1037 if self.isServer then
1038 for _,foldingPart in pairs(self.foldingParts) do
1039 if foldingPart.componentJoint ~= nil then
1040 local componentJoint = foldingPart.componentJoint;
1041
1042 local jointNode = componentJoint.jointNode;
1043 if foldingPart.anchorActor == 1 then
1044 jointNode = componentJoint.jointNodeActor1;
1045 end
1046
1047 local node = self.components[componentJoint.componentIndices[((foldingPart.anchorActor+1)%2)+1] ].node;
1048 local x,y,z = localToWorld(jointNode, foldingPart.x, foldingPart.y, foldingPart.z);
1049 local upX,upY,upZ = localDirectionToWorld(jointNode, foldingPart.upX,foldingPart.upY,foldingPart.upZ);
1050 local dirX,dirY,dirZ = localDirectionToWorld(jointNode, foldingPart.dirX,foldingPart.dirY,foldingPart.dirZ);
1051 Utils.setWorldTranslation(node, x,y,z);
1052 Utils.setWorldDirection(node, dirX,dirY,dirZ, upX,upY,upZ);
1053
1054 self:setComponentJointFrame(componentJoint, foldingPart.anchorActor)
1055 end
1056 end
1057 end
1058 end
1059end

allowLoadMovingToolStates

Description
Returns if loading of moving tool stats from savegame is allowed
Definition
allowLoadMovingToolStates()
Return Values
booleanisAllowedis allowed
Code
1064function Foldable:allowLoadMovingToolStates(superFunc)
1065 if self.foldAnimTime > self.loadMovingToolStatesMaxLimit or self.foldAnimTime < self.loadMovingToolStatesMinLimit then
1066 return false;
1067 end
1068
1069 if superFunc ~= nil then
1070 return superFunc(self);
1071 end
1072 return true;
1073end