LUADOC - Farming Simulator 17

Printable Version

Script v1.4.4.0

Engine v7.0.0.2

Foundation Reference

BaleGrab

Description
Class for a balegrab tool
Functions

load

Description
Called on loading
Definition
load(table savegame)
Arguments
tablesavegamesavegame
Code
20function BaleGrab:load(savegame)
21
22 self.dynamicMountTriggerCallback = BaleGrab.dynamicMountTriggerCallback;
23 self.addDynamicMountedObject = BaleGrab.addDynamicMountedObject;
24 self.removeDynamicMountedObject = BaleGrab.removeDynamicMountedObject;
25
26 if self.isServer then
27 local attacherTriggerTriggerNode = Utils.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.baleGrab#triggerNode"));
28 local attacherTriggerRootNode = Utils.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.baleGrab#rootNode"));
29 local attacherTriggerJointNode = Utils.indexToObject(self.components, getXMLString(self.xmlFile, "vehicle.baleGrab#jointNode"));
30 local attacherJointTypeString = Utils.getNoNil(getXMLString(self.xmlFile, "vehicle.baleGrab#jointType"), "TYPE_AUTO_ATTACH_Y");
31 local attacherJointType = DynamicMountUtil.TYPE_AUTO_ATTACH_Y;
32 if attacherJointTypeString == "TYPE_FORK" then
33 attacherJointType = DynamicMountUtil.TYPE_FORK;
34 elseif attacherJointTypeString == "TYPE_AUTO_ATTACH_XZ" then
35 attacherJointType = DynamicMountUtil.TYPE_AUTO_ATTACH_XZ;
36 elseif attacherJointTypeString == "TYPE_AUTO_ATTACH_XYZ" then
37 attacherJointType = DynamicMountUtil.TYPE_AUTO_ATTACH_XYZ;
38 end;
39 if attacherTriggerTriggerNode ~= nil and attacherTriggerRootNode ~= nil and attacherTriggerJointNode ~= nil then
40 local forceAcceleration = Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.baleGrab#forceAcceleration"), 20);
41 addTrigger(attacherTriggerTriggerNode, "dynamicMountTriggerCallback", self);
42
43 local grabRefComponentJointIndex1 = getXMLInt(self.xmlFile, "vehicle.baleGrab#grabRefComponentJointIndex1");
44 local grabRefComponentJointIndex2 = getXMLInt(self.xmlFile, "vehicle.baleGrab#grabRefComponentJointIndex2");
45 local componentJoint1, componentJoint2;
46 if grabRefComponentJointIndex1 ~= nil then
47 componentJoint1 = self.componentJoints[grabRefComponentJointIndex1+1];
48 end
49 if grabRefComponentJointIndex2 ~= nil then
50 componentJoint2 = self.componentJoints[grabRefComponentJointIndex2+1];
51 end
52 local rotDiffThreshold1 = math.rad(Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.baleGrab#rotDiffThreshold1"), 2));
53 local rotDiffThreshold2 = math.rad(Utils.getNoNil(getXMLFloat(self.xmlFile, "vehicle.baleGrab#rotDiffThreshold2"), 2));
54 self.dynamicMountAttacherTrigger = {
55 triggerNode=attacherTriggerTriggerNode, rootNode=attacherTriggerRootNode, jointNode=attacherTriggerJointNode, attacherJointType=attacherJointType, forceAcceleration=forceAcceleration,
56 componentJoint1=componentJoint1, rotDiffThreshold1=rotDiffThreshold1, cosRotDiffThreshold1=math.cos(rotDiffThreshold1),
57 componentJoint2=componentJoint2, rotDiffThreshold2=rotDiffThreshold2, cosRotDiffThreshold2=math.cos(rotDiffThreshold2)
58 };
59 end
60 self.dynamicMountedObjects = {};
61 self.pendingDynamicMountObjects = {};
62 end
63end;

delete

Description
Called on deleting
Definition
delete()
Code
67function BaleGrab:delete()
68 if self.isServer then
69 for object,_ in pairs(self.dynamicMountedObjects) do
70 object:unmountDynamic();
71 end
72 end
73 if self.dynamicMountAttacherTrigger ~= nil then
74 removeTrigger(self.dynamicMountAttacherTrigger.triggerNode);
75 end
76end;

updateTick

Description
Called on update tick
Definition
updateTick(float dt)
Arguments
floatdttime since last call in ms
Code
90function BaleGrab:updateTick(dt)
91 if self.isServer and self:getIsActive() then
92 local attachTrigger = self.dynamicMountAttacherTrigger
93
94 local isClosed = true;
95 if attachTrigger.componentJoint1 ~= nil then
96 isClosed = BaleGrab.isComponentJointOutsideLimit(self, attachTrigger.componentJoint1, attachTrigger.rotDiffThreshold1, attachTrigger.cosRotDiffThreshold1);
97 end
98 if isClosed and attachTrigger.componentJoint2 ~= nil then
99 isClosed = BaleGrab.isComponentJointOutsideLimit(self, attachTrigger.componentJoint2, attachTrigger.rotDiffThreshold2, attachTrigger.cosRotDiffThreshold2);
100 end
101 if isClosed then
102 for object,_ in pairs(self.pendingDynamicMountObjects) do
103 if self.dynamicMountedObjects[object] == nil then
104 object:unmountDynamic();
105 if object:mountDynamic(self, self.dynamicMountAttacherTrigger.rootNode, self.dynamicMountAttacherTrigger.jointNode, self.dynamicMountAttacherTrigger.attacherJointType, self.dynamicMountAttacherTrigger.forceAcceleration) then
106 self:addDynamicMountedObject(object);
107 end;
108 end
109 end
110 else
111 for object,_ in pairs(self.dynamicMountedObjects) do
112 self:removeDynamicMountedObject(object, false);
113 object:unmountDynamic();
114 end;
115 end;
116 end
117end;

addDynamicMountedObject

Description
Add dynamic mount object
Definition
addDynamicMountedObject(table object)
Arguments
tableobjectobject
Code
125function BaleGrab:addDynamicMountedObject(object)
126 self.dynamicMountedObjects[object] = object;
127end

removeDynamicMountedObject

Description
Remove dynamic mount object
Definition
removeDynamicMountedObject(table object, boolean isDeleting)
Arguments
tableobjectobject
booleanisDeletingis deleting
Code
133function BaleGrab:removeDynamicMountedObject(object, isDeleting)
134 self.dynamicMountedObjects[object] = nil;
135 if isDeleting then
136 self.pendingDynamicMountObjects[object] = nil;
137 end
138end

dynamicMountTriggerCallback

Description
Trigger callback
Definition
dynamicMountTriggerCallback(integer triggerId, integer otherActorId, boolean onEnter, boolean onLeave, boolean onStay, integer otherShapeId)
Arguments
integertriggerIdid of trigger
integerotherActorIdid of other actor
booleanonEnteron enter
booleanonLeaveon leave
booleanonStayon stay
integerotherShapeIdid of other shape
Code
148function BaleGrab:dynamicMountTriggerCallback(triggerId, otherActorId, onEnter, onLeave, onStay, otherShapeId)
149 if onEnter then
150 local object = g_currentMission:getNodeObject(otherActorId);
151 if object == nil then
152 object = g_currentMission.nodeToVehicle[otherActorId];
153 end;
154 if object ~= nil and object ~= self and object.getSupportsMountDynamic ~= nil and object:getSupportsMountDynamic() then
155 self.pendingDynamicMountObjects[object] = Utils.getNoNil(self.pendingDynamicMountObjects[object], 0) + 1;
156 end
157 elseif onLeave then
158 local object = g_currentMission:getNodeObject(otherActorId);
159 if object == nil then
160 object = g_currentMission.nodeToVehicle[otherActorId];
161 end;
162 if object ~= nil then
163 if self.pendingDynamicMountObjects[object] ~= nil then
164 local count = self.pendingDynamicMountObjects[object]-1;
165 if count == 0 then
166 self.pendingDynamicMountObjects[object] = nil;
167
168 if self.dynamicMountedObjects[object] ~= nil then
169 self:removeDynamicMountedObject(object, false);
170 object:unmountDynamic();
171 end
172 else
173 self.pendingDynamicMountObjects[object] = count;
174 end
175 end
176 end
177 end
178end;

isComponentJointOutsideLimit

Description
Returns if component joint is outside the rotation limit
Definition
isComponentJointOutsideLimit(integer componentJoint, float maxRot, float cosMaxRot)
Arguments
integercomponentJointindex of component joint
floatmaxRotmax rotation
floatcosMaxRotcos max rotation
Return Values
booleanisOutsideis outside the rotation limit
Code
186function BaleGrab.isComponentJointOutsideLimit(self, componentJoint, maxRot, cosMaxRot)
187 local x,_,z = localDirectionToLocal(self.components[componentJoint.componentIndices[2]].node, componentJoint.jointNode, 0,0,1);
188 if (x >= 0) == (maxRot >= 0) then
189 --print("angle: "..math.deg(math.acos(z/math.sqrt(x*x + z*z))));
190 if z <= cosMaxRot*math.sqrt(x*x + z*z) then
191 return true;
192 end
193 end
194 return false;
195end