LUADOC - Farming Simulator 17

Printable Version

Script v1.4.4.0

Engine v7.0.0.2

Foundation Reference

ManureBarrel

Description
Class for all manure barrels
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
17function ManureBarrel.prerequisitesPresent(specializations)
18 return SpecializationUtil.hasSpecialization(Sprayer, specializations) and
19 SpecializationUtil.hasSpecialization(AttacherJoints, specializations);
20end;

load

Description
Called on loading
Definition
load(table savegame)
Arguments
tablesavegamesavegame
Code
25function ManureBarrel:load(savegame)
26
27 self.groundAdjustRaycastCallback = ManureBarrel.groundAdjustRaycastCallback;
28 self.getIsInWorkPosition = Utils.overwrittenFunction(self.getIsInWorkPosition, ManureBarrel.getIsInWorkPosition);
29 self.getCanBeTurnedOn = Utils.overwrittenFunction(self.getCanBeTurnedOn, ManureBarrel.getCanBeTurnedOn);
30 self.getIsTurnedOnAllowed = Utils.overwrittenFunction(self.getIsTurnedOnAllowed, ManureBarrel.getIsTurnedOnAllowed);
31 self.setIsTurnedOn = Utils.overwrittenFunction(self.setIsTurnedOn, ManureBarrel.setIsTurnedOn);
32 self.getIsReadyToSpray = Utils.overwrittenFunction(self.getIsReadyToSpray, ManureBarrel.getIsReadyToSpray);
33 self.getAreEffectsVisible = Utils.overwrittenFunction(self.getAreEffectsVisible, ManureBarrel.getAreEffectsVisible);
34 self.getIsWorkAreaActive = Utils.overwrittenFunction(self.getIsWorkAreaActive, ManureBarrel.getIsWorkAreaActive);
35
36 self.manureBarrelFoldMinLimit = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.manureBarrel#foldMinLimit"), 0);
37 self.manureBarrelFoldMaxLimit = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.manureBarrel#foldMaxLimit"), 1);
38
39 self.attachToolAnimation = getXMLString(self.xmlFile, "vehicle.manureBarrel#toolAttachAnimName");
40 self.attachToolJointIndex = getXMLInt(self.xmlFile, "vehicle.manureBarrel#attacherJointIndex");
41
42 self.groundAdjustedNodes = {};
43 local i = 0;
44 while true do
45 local key = string.format("vehicle.groundAdjustedNodes.groundAdjustedNode(%d)", i);
46 if not hasXMLProperty(self.xmlFile, key) then
47 break;
48 end;
49 local node = Utils.indexToObject(self.components, getXMLString(self.xmlFile, key.."#index"));
50 if node ~= nil then
51
52 local x,y,z = getTranslation(node);
53 local raycastNodes = {};
54
55 local j = 0;
56 while true do
57 local raycastKey = key..string.format(".raycastNode(%d)", j);
58 if not hasXMLProperty(self.xmlFile, raycastKey) then
59 break;
60 end;
61 local raycastNode = Utils.indexToObject(self.components, getXMLString(self.xmlFile, raycastKey.."#index"));
62 if raycastNode ~= nil then
63 assert(getParent(node) == getParent(raycastNode));
64
65 local _,y1,_ = getTranslation(raycastNode);
66 table.insert(raycastNodes, {node=raycastNode, yDiff=y1-y});
67 end;
68 j = j+1;
69 end;
70 if table.getn(raycastNodes) > 0 then
71
72
73 local minY = Utils.getNoNil(getXMLFloat(self.xmlFile, key.."#minY"), y-1);
74 local maxY = Utils.getNoNil(getXMLFloat(self.xmlFile, key.."#maxY"), minY+1);
75 local yOffset = Utils.getNoNil(getXMLFloat(self.xmlFile, key.."#yOffset"), 0);
76 local moveSpeed = Utils.getNoNil(getXMLFloat(self.xmlFile, key.."#moveSpeed"), 1)/1000;
77
78 local adjustedNode = {node=node, raycastNodes=raycastNodes, x=x, y=y, z=z, curY=y, targetY=y, minY=minY, maxY=maxY, yOffset=yOffset, moveSpeed=moveSpeed}
79
80 adjustedNode.foldMinLimit = Utils.getNoNil(getXMLFloat(self.xmlFile, key.."#foldMinLimit"), 0);
81 adjustedNode.foldMaxLimit = Utils.getNoNil(getXMLFloat(self.xmlFile, key.."#foldMaxLimit"), 1);
82
83 local translationNode = Utils.indexToObject(self.components, getXMLString(self.xmlFile, key.."#translationNode"));
84 if translationNode ~= nil then
85 local tx, ty, tz = getWorldTranslation(translationNode);
86 local tx, ty, tz = worldToLocal(node, tx, ty, tz);
87
88 adjustedNode.translationNode=translationNode;
89 adjustedNode.tx=tx;
90 adjustedNode.ty=ty;
91 adjustedNode.tz=tz;
92 end;
93 table.insert(self.groundAdjustedNodes, adjustedNode);
94 end;
95 end;
96 i = i + 1;
97 end;
98end;

update

Description
Called on update
Definition
update(float dt)
Arguments
floatdttime since last call in ms
Code
112function ManureBarrel:update(dt)
113 if self:getIsActive() then
114 if self.isClient then
115 for _, node in pairs(self.groundAdjustedNodes) do
116 if node.targetY ~= node.curY then
117 if node.targetY > node.curY then
118 node.curY = math.min(node.curY + node.moveSpeed*dt, node.targetY);
119 else
120 node.curY = math.max(node.curY - node.moveSpeed*dt, node.targetY);
121 end;
122 setTranslation(node.node, node.x, node.curY, node.z);
123
124
125 if node.translationNode ~= nil then
126 local tx, ty, tz = localToWorld(node.node, node.tx, node.ty, node.tz);
127 local tx, ty, tz = worldToLocal(getParent(node.translationNode), tx, ty, tz);
128 setTranslation(node.translationNode, tx, ty, tz);
129 end;
130 end;
131 end;
132 end;
133 end;
134end;

updateTick

Description
Called on update tick
Definition
updateTick(float dt)
Arguments
floatdttime since last call in ms
Code
139function ManureBarrel:updateTick(dt)
140 if self:getIsActive() then
141
142 if self.isClient then
143
144 local foldAnimTime = self.foldAnimTime;
145
146 for _, node in pairs(self.groundAdjustedNodes) do
147 if foldAnimTime == nil or (foldAnimTime <= node.foldMaxLimit and foldAnimTime >= node.foldMinLimit) then
148
149 local newY = node.minY;
150
151 for _, raycastNode in pairs(node.raycastNodes) do
152 local x,y,z = getWorldTranslation(raycastNode.node);
153 local dx,dy,dz = localDirectionToWorld(raycastNode.node, 0, -1, 0);
154
155 self.lastRaycastDistance = 0;
156 raycastClosest(x, y, z, dx, dy, dz, "groundAdjustRaycastCallback", 4, self);
157 if self.lastRaycastDistance ~= 0 then
158 newY = math.max(newY, node.y - self.lastRaycastDistance + raycastNode.yDiff - node.yOffset);
159 end;
160 end;
161 newY = Utils.clamp(newY, node.minY, node.maxY);
162 node.targetY = newY;
163 _, node.curY, _ = getTranslation(node.node);
164 end;
165 end;
166 end;
167 end;
168end;

draw

Description
Called on draw
Definition
draw()
Code
172function ManureBarrel:draw()
173end;

onAttachImplement

Description
Called on attaching a implement
Definition
onAttachImplement(table implement)
Arguments
tableimplementimplement to attach
Code
178function ManureBarrel:onAttachImplement(implement)
179 local object = implement.object;
180 if self.attachToolJointIndex ~= nil and implement.jointDescIndex then
181 self.attachedTool = object;
182 if self.attachToolAnimation ~= nil and self.playAnimation ~= nil then
183 self:playAnimation(self.attachToolAnimation, 1, self:getAnimationTime(self.attachToolAnimation), true);
184 end;
185 if self.attachedTool.aiLeftMarker ~= nil and self.attachedTool.aiRightMarker ~= nil and self.attachedTool.aiBackMarker ~= nil then
186 self.aiLeftMarkerBackup, self.aiRightMarkerBackup, self.aiBackMarkerBackup = self.aiLeftMarker, self.aiRightMarker, self.aiBackMarker;
187
188 self.aiLeftMarker, self.aiRightMarker, self.aiBackMarker = self.attachedTool.aiLeftMarker, self.attachedTool.aiRightMarker, self.attachedTool.aiBackMarker;
189 self.attachedTool.aiLeftMarker, self.attachedTool.aiRightMarker, self.attachedTool.aiBackMarker = nil, nil, nil;
190
191 self.terrainDetailRequiredValueRangesBackUp = {};
192 for _,range in pairs(self.terrainDetailRequiredValueRanges) do
193 table.insert(self.terrainDetailRequiredValueRangesBackUp, {range[1], range[2], range[3], range[4]});
194 end
195 self.terrainDetailProhibitValueRangesBackUp = {};
196 for _,range in pairs(self.terrainDetailProhibitValueRanges) do
197 table.insert(self.terrainDetailProhibitValueRangesBackUp, {range[1], range[2], range[3], range[4]});
198 end
199
200 self.terrainDetailRequiredValueRanges = self.attachedTool.terrainDetailRequiredValueRanges;
201 self.terrainDetailProhibitValueRanges = self.attachedTool.terrainDetailProhibitValueRanges;
202 end;
203 end;
204end;

onDetachImplement

Description
Called on detaching a implement
Definition
onDetachImplement(integer implementIndex)
Arguments
integerimplementIndexindex of implement to detach
Code
209function ManureBarrel:onDetachImplement(implementIndex)
210 local object = self.attachedImplements[implementIndex].object;
211 if object ~= nil then
212 if self.attachedImplements[implementIndex].jointDescIndex == self.attachToolJointIndex then
213 if self.attachToolAnimation ~= nil and self.playAnimation ~= nil then
214 self:playAnimation(self.attachToolAnimation, -1, self:getAnimationTime(self.attachToolAnimation), true);
215 end;
216 if self.aiLeftMarkerBackup ~= nil and self.aiRightMarkerBackup ~= nil and self.aiBackMarkerBackup ~= nil then
217 self.attachedTool.aiLeftMarker, self.attachedTool.aiRightMarker, self.attachedTool.aiBackMarker = self.aiLeftMarker, self.aiRightMarker, self.aiBackMarker;
218 self.aiLeftMarker, self.aiRightMarker, self.aiBackMarker = self.aiLeftMarkerBackup, self.aiRightMarkerBackup, self.aiBackMarkerBackup;
219
220 self.terrainDetailRequiredValueRanges = {};
221 for _,range in pairs(self.terrainDetailRequiredValueRangesBackUp) do
222 table.insert(self.terrainDetailRequiredValueRanges, {range[1], range[2], range[3], range[4]});
223 end
224 self.terrainDetailProhibitValueRanges = {};
225 for _,range in pairs(self.terrainDetailProhibitValueRangesBackUp) do
226 table.insert(self.terrainDetailProhibitValueRanges, {range[1], range[2], range[3], range[4]});
227 end
228 end;
229 self.attachedTool = nil;
230 end;
231 end;
232end;

getIsReadyToSpray

Description
Returns if tool is ready to spray
Definition
getIsReadyToSpray()
Return Values
booleanisReadyis ready to spray
Code
237function ManureBarrel:getIsReadyToSpray(superFunc)
238 if self.attachedTool ~= nil then
239 return false;
240 end;
241
242 if superFunc ~= nil then
243 superFunc(self);
244 end;
245 return true;
246end;

getAreEffectsVisible

Description
Returns if effects are visible
Definition
getAreEffectsVisible()
Return Values
booleanareVisibleeffects are visible
Code
251function ManureBarrel:getAreEffectsVisible(superFunc)
252 if self.attachedTool ~= nil then
253 return false;
254 end;
255
256 if superFunc ~= nil then
257 return superFunc(self);
258 end;
259 return true;
260end;

getIsWorkAreaActive

Description
Returns true if work area is active
Definition
getIsWorkAreaActive(table workArea)
Arguments
tableworkAreaworkArea
Return Values
booleanisActivework area is active
Code
266function ManureBarrel:getIsWorkAreaActive(superFunc, workArea)
267 if self.attachedTool ~= nil then
268 return false;
269 end;
270
271 if superFunc ~= nil then
272 return superFunc(self, workArea);
273 end;
274 return true;
275end;

getLitersPerSecond

Description
Returns usage in liter per second
Definition
getLitersPerSecond(integer fillType)
Arguments
integerfillTypefill type
Return Values
floatusageusage in liter per second
Code
281function ManureBarrel:getLitersPerSecond(superFunc, fillType)
282 if self.attachedTool ~= nil and self.attachedTool.getLitersPerSecond ~= nil then
283 return self.attachedTool:getLitersPerSecond(fillType);
284 end;
285
286 if superFunc ~= nil then
287 return superFunc(self, fillType);
288 end;
289 return self.defaultSprayLitersPerSecond;
290end;

getIsInWorkPosition

Description
Returns true if it is in work position
Definition
getIsInWorkPosition()
Return Values
booleaninWorkPositionis in work position
Code
295function ManureBarrel:getIsInWorkPosition()
296 if self.attachedTool == nil then
297 if foldAnimTime ~= nil and (foldAnimTime > self.manureBarrelFoldMaxLimit or foldAnimTime < self.manureBarrelFoldMinLimit) then
298 return false;
299 end;
300 else
301 --return self.attachedTool:getIsInWorkPosition();
302 if self.attachedTool:getIsInWorkPosition() then
303 local implement = self:getImplementByObject(self.attachedTool);
304 if implement ~= nil then
305 local jointDesc = self.attacherJoints[implement.jointDescIndex];
306 if jointDesc.allowsLowering and self.attachedTool.aiNeedsLowering then
307 if jointDesc.moveAlpha == jointDesc.lowerAlpha then
308 return true;
309 else
310 self:setJointMoveDown(implement.jointDescIndex, true, true);
311 return false;
312 end
313 end;
314 end;
315 else
316 return false;
317 end;
318 end;
319
320 if superFunc ~= nil then
321 return superFunc(self);
322 end
323
324 return true;
325end;

getCanBeTurnedOn

Description
Returns if can be turned on
Definition
getCanBeTurnedOn()
Return Values
booleancanBeTurnedOnvehicle can be turned on
Code
330function ManureBarrel:getCanBeTurnedOn(superFunc)
331 if self.attachedTool ~= nil then
332 if self.attachedTool.isAlwaysTurnedOn and self.attachedTool.needsTankActivation and self.attachedTool.activateTankOnLowering then
333 return false;
334 end;
335 end;
336
337 if superFunc ~= nil then
338 return superFunc(self);
339 end
340 return true;
341end;

getIsTurnedOnAllowed

Description
Returns if turn on is allowed
Definition
getIsTurnedOnAllowed(boolean isTurnedOn)
Arguments
booleanisTurnedOnis turned on
Return Values
booleanallowallow turn on
Code
347function ManureBarrel:getIsTurnedOnAllowed(superFunc, isTurnedOn)
348 if isTurnedOn then
349 if self.attachedTool == nil then
350 local foldAnimTime = self.foldAnimTime;
351 if foldAnimTime ~= nil and (foldAnimTime > self.manureBarrelFoldMaxLimit or foldAnimTime < self.manureBarrelFoldMinLimit) then
352 return false;
353 end;
354 else
355 if self.attachedTool:getIsInWorkPosition() then
356 local implement = self:getImplementByObject(self.attachedTool);
357 if implement ~= nil then
358 local jointDesc = self.attacherJoints[implement.jointDescIndex];
359 if jointDesc.allowsLowering and self.attachedTool.aiNeedsLowering then
360 return jointDesc.moveDown;
361 end;
362 end;
363 else
364 return false;
365 end;
366 end;
367 end;
368
369 if superFunc ~= nil then
370 return superFunc(self, isTurnedOn);
371 end
372
373 return true;
374end;

setIsTurnedOn

Description
Set is turned on state
Definition
setIsTurnedOn(boolean isTurnedOn, boolean noEventSend)
Arguments
booleanisTurnedOnnew is is turned on state
booleannoEventSendno event send
Code
380function ManureBarrel:setIsTurnedOn(superFunc, isTurnedOn, noEventSend)
381 superFunc(self, isTurnedOn, noEventSend);
382end;

onAiTurnOn

Description
Called on ai turn on
Definition
onAiTurnOn()
Code
386function ManureBarrel:onAiTurnOn()
387 if self.attachedTool ~= nil then
388 self.attachedTool:onAiTurnOn();
389 end;
390end;

onAiTurnOff

Description
Called on ai turn off
Definition
onAiTurnOff()
Code
394function ManureBarrel:onAiTurnOff()
395 if self.attachedTool ~= nil then
396 self.attachedTool:onAiTurnOff();
397 end;
398end;

onAiLower

Description
Called on ai lower
Definition
onAiLower()
Code
402function ManureBarrel:onAiLower()
403 if self.attachedTool ~= nil then
404 local implement = self:getImplementByObject(self.attachedTool);
405 if implement ~= nil then
406 local jointDesc = self.attacherJoints[implement.jointDescIndex];
407 if jointDesc.allowsLowering and self.attachedTool.aiNeedsLowering then
408 if jointDesc.moveAlpha ~= jointDesc.lowerAlpha then
409 self:setJointMoveDown(implement.jointDescIndex, true, true);
410 end;
411 end;
412 end;
413 end;
414end;

onAiRaise

Description
Called on ai raise
Definition
onAiRaise()
Code
418function ManureBarrel:onAiRaise()
419 if self.attachedTool ~= nil then
420 local implement = self:getImplementByObject(self.attachedTool);
421 if implement ~= nil then
422 local jointDesc = self.attacherJoints[implement.jointDescIndex];
423 if jointDesc.allowsLowering and self.attachedTool.aiNeedsLowering then
424 self:setJointMoveDown(implement.jointDescIndex, false, true);
425 end;
426 end;
427 end;
428end;

getIsAIReadyForWork

Description
Returns true if vehicle is ready for ai work
Definition
getIsAIReadyForWork()
Return Values
booleanisReadyis ready for ai work
Code
433function ManureBarrel:getIsAIReadyForWork()
434 local isReady = true;
435 if self.attachedTool ~= nil then
436 local implement = self:getImplementByObject(self.attachedTool);
437 local jointDesc = self.attacherJoints[implement.jointDescIndex];
438 local isAttachedToolReady = self.attachedTool:getIsAIReadyForWork();
439 if jointDesc.allowsLowering and self.attachedTool.aiNeedsLowering then
440 if jointDesc.moveDown then
441 return jointDesc.moveAlpha == jointDesc.lowerAlpha and isAttachedToolReady;
442 else
443 self:setJointMoveDown(implement.jointDescIndex, true, false);
444 return false;
445 end
446 else
447 return isReady and isAttachedToolReady;
448 end
449 end
450 return isReady;
451end

groundAdjustRaycastCallback

Description
Raycast callback
Definition
groundAdjustRaycastCallback(integer transformId, float x, float y, float z, float distance)
Arguments
integertransformIdid raycasted object
floatxx raycast position
floatyy raycast position
floatzz raycast position
floatdistancedistance to raycast position
Code
460function ManureBarrel:groundAdjustRaycastCallback(transformId, x, y, z, distance)
461 self.lastRaycastDistance = distance;
462end;