LUADOC - Farming Simulator 17

Printable Version

Script v1.4.4.0

Engine v7.0.0.2

Foundation Reference

AttacherJointControl

Description
Specialization to control the attacherJoint height
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 AttacherJointControl.prerequisitesPresent(specializations)
18 return SpecializationUtil.hasSpecialization(Attachable, specializations);
19end;

preLoad

Description
Called before loading
Definition
preLoad(table savegame)
Arguments
tablesavegamesavegame
Code
24function AttacherJointControl:preLoad(savegame)
25 self.loadInputAttacherJoint = Utils.overwrittenFunction(self.loadInputAttacherJoint, AttacherJointControl.loadInputAttacherJoint);
26end;

load

Description
Called on loading
Definition
load(table savegame)
Arguments
tablesavegamesavegame
Code
31function AttacherJointControl:load(savegame)
32
33 self.updateAttacherJoint = AttacherJointControl.updateAttacherJoint;
34
35 self.attacherJointControl = {};
36
37 local uiScale = g_gameSettings:getValue("uiScale")
38
39 self.attacherJointControl.controls = {};
40 for i=1,2 do
41 local key = string.format("vehicle.attacherJointControl.control%d", i);
42 if not hasXMLProperty(self.xmlFile, key) then
43 break;
44 end
45 local control = {};
46
47 control.controlAxis = getXMLString(self.xmlFile, key .. "#controlAxis");
48
49 if control.controlAxis ~= nil then
50 control.controlActionIndex = InputBinding[control.controlAxis];
51 if control.controlActionIndex ~= nil then
52 self:addConflictCheckedInput(control.controlActionIndex);
53 end
54 end
55
56 control.invertControlAxis = Utils.getNoNil( getXMLString(self.xmlFile, key .. "#invertControlAxis"), false );
57 control.mouseSpeedFactor = Utils.getNoNil( getXMLFloat(self.xmlFile, key .. "#mouseSpeedFactor"), 1.0 );
58
59 control.iconFilename = getXMLString(self.xmlFile, key .. "#controlAxisIcon");
60 if control.iconFilename ~= nil then
61 local iconFilename = Utils.getFilename(control.iconFilename, self.baseDirectory);
62 local iconWidth, iconHeight = getNormalizedScreenValues(40*uiScale, 40*uiScale);
63 control.controlAxisIcon = Overlay:new("axisIcon", iconFilename, 0, 0, iconWidth, iconHeight);
64 control.controlAxisIcon:setAlignment(Overlay.ALIGN_VERTICAL_MIDDLE, Overlay.ALIGN_HORIZONTAL_LEFT);
65 end
66
67 control.moveAlpha = 0;
68 control.moveAlphaSend = 0;
69
70 table.insert(self.attacherJointControl.controls, control);
71 end
72
73 self.lastDirtyTime = -10000;
74
75 self.attacherJointControl.dirtyFlag = self:getNextDirtyFlag();
76
77end;

loadInputAttacherJoint

Description
Called on loading
Definition
loadInputAttacherJoint(table savegame)
Arguments
tablesavegamesavegame
Code
82function AttacherJointControl:loadInputAttacherJoint(superFunc, xmlFile, key, inputAttacherJoint, i)
83 if superFunc ~= nil then
84 if not superFunc(self, xmlFile, key, inputAttacherJoint, i) then
85 return false;
86 end
87 end;
88
89 inputAttacherJoint.isControllable = Utils.getNoNil(getXMLBool(xmlFile, key .. "#isControllable"), false);
90
91 return true;
92end

readStream

Description
Called on client side on join
Definition
readStream(integer streamId, integer connection)
Arguments
integerstreamIdstreamId
integerconnectionconnection
Code
101function AttacherJointControl:readStream(streamId, connection)
102 if connection:getIsServer() then
103 if streamReadBool(streamId) then
104 local moveAlpha = {};
105 for i=1,2 do
106 moveAlpha[i] = streamReadFloat32(streamId);
107 end
108 self:updateAttacherJoint(moveAlpha[1], moveAlpha[2]);
109 end
110 end
111end;

writeStream

Description
Called on server side on join
Definition
writeStream(integer streamId, integer connection)
Arguments
integerstreamIdstreamId
integerconnectionconnection
Code
117function AttacherJointControl:writeStream(streamId, connection)
118 if not connection:getIsServer() then
119 if streamWriteBool(streamId, self.attacherJointControl.jointDesc ~= nil) then
120 for i=1,2 do
121 streamWriteFloat32(streamId, self.attacherJointControl.controls[i].moveAlpha);
122 end
123 end
124 end
125end;

readUpdateStream

Description
Called on on update
Definition
readUpdateStream(integer streamId, integer timestamp, table connection)
Arguments
integerstreamIdstream ID
integertimestamptimestamp
tableconnectionconnection
Code
132function AttacherJointControl:readUpdateStream(streamId, timestamp, connection)
133 if streamReadBool(streamId) then
134 local moveAlpha = {};
135 for i=1,2 do
136 moveAlpha[i] = streamReadFloat32(streamId);
137 end
138 self:updateAttacherJoint(moveAlpha[1], moveAlpha[2]);
139 end
140end

writeUpdateStream

Description
Called on on update
Definition
writeUpdateStream(integer streamId, table connection, integer dirtyMask)
Arguments
integerstreamIdstream ID
tableconnectionconnection
integerdirtyMaskdirty mask
Code
147function AttacherJointControl:writeUpdateStream(streamId, connection, dirtyMask)
148 if streamWriteBool( streamId, bitAND(dirtyMask, self.attacherJointControl.dirtyFlag) ~= 0 and (connection:getIsServer() or connection ~= self:getOwner()) ) then
149 for i=1,2 do
150 streamWriteFloat32(streamId, self.attacherJointControl.controls[i].moveAlpha);
151 end
152 end
153end

update

Description
Called on update
Definition
update(float dt)
Arguments
floatdttime since last call in ms
Code
164function AttacherJointControl:update(dt)
165
166 if self.attacherJointControl ~= nil and self.attacherJointControl.controls ~= nil and self.attacherJointControl.jointDesc ~= nil then
167
168 if self.isClient and self:getIsActiveForInput(false) and not self:hasInputConflictWithSelection() and self.attacherJoint.isControllable then
169
170 for i=1,2 do
171 local control = self.attacherJointControl.controls[i];
172
173 if control.controlActionIndex ~= nil then
174 if control.controlAxisIcon ~= nil then
175 g_currentMission:addHelpAxis(control.controlActionIndex, control.controlAxisIcon)
176 end
177
178 local move, axisType = InputBinding.getInputAxis(control.controlActionIndex);
179 if control.invertControlAxis then
180 move = -move;
181 end
182 if axisType == InputBinding.INPUTTYPE_MOUSE_AXIS then
183 move = move * control.mouseSpeedFactor;
184 else
185 move = move * g_gameSettings:getValue("vehicleArmSensitivity")
186 end
187
188 local isAxisZero = InputBinding.isAxisZero(move);
189
190 if not isAxisZero then
191 local jointDesc = self.attacherJointControl.jointDesc;
192
193 local moveAlpha = control.moveAlpha + (0.001 * dt * move);
194 control.moveAlpha = Utils.clamp(moveAlpha, jointDesc.upperAlpha, jointDesc.lowerAlpha);
195 end
196 end
197 end
198
199 local isDirty = false;
200 for i=1,2 do
201 local control = self.attacherJointControl.controls[i];
202 if control.moveAlphaSend ~= control.moveAlpha then
203 control.moveAlphaSend = control.moveAlpha;
204 isDirty = true;
205 end
206 end
207 if isDirty then
208 self:raiseDirtyFlags(self.attacherJointControl.dirtyFlag);
209 self:updateAttacherJoint(self.attacherJointControl.controls[1].moveAlpha, self.attacherJointControl.controls[2].moveAlpha);
210 self.lastDirtyTime = g_currentMission.time;
211 end
212
213 if self:getIsActiveForSound() and self.attacherVehicle ~= nil and self.attacherVehicle.sampleHydraulic ~= nil and self.attacherVehicle.sampleHydraulic.sample ~= nil then
214 if math.abs(self.lastDirtyTime - g_currentMission.time) <= 125 then
215 if self.startedSample == nil then
216 SoundUtil.playSample(self.attacherVehicle.sampleHydraulic, 0, 0, nil);
217 self.attacherVehicle.sampleHydraulic.isPlaying = false;
218 self.startedSample = self.attacherVehicle.sampleHydraulic;
219 end
220 elseif self.startedSample ~= nil then
221 SoundUtil.stopSample(self.startedSample, true);
222 self.startedSample = nil;
223 end
224 end
225
226 end
227 end;
228end;

onAttach

Description
Called if vehicle gets attached
Definition
onAttach(table attacherVehicle, integer jointDescIndex)
Arguments
tableattacherVehicleattacher vehicle
integerjointDescIndexindex of attacher joint it gets attached to
Code
240function AttacherJointControl:onAttach(attacherVehicle, jointDescIndex)
241 if attacherVehicle.attacherJoints[jointDescIndex] ~= nil then
242 for i=1,2 do
243 local control = self.attacherJointControl.controls[i];
244 if control.controlActionIndex ~= nil then
245 if not self.attacherJoint.isControllable then
246 self:removeConflictCheckedInput(control.controlActionIndex);
247 else
248 self:addConflictCheckedInput(control.controlActionIndex);
249 end;
250 end
251 end
252 end
253 local jointDesc = attacherVehicle.attacherJoints[jointDescIndex]
254 jointDesc.allowsLoweringBackup = jointDesc.allowsLowering;
255 jointDesc.allowsLowering = false;
256
257 self.attacherJointControl.jointDesc = jointDesc;
258
259 --jnself.applyInitialRotation = true;
260 self.attacherJointControl.controls[1].moveAlpha = 0.5;
261 self.attacherJointControl.controls[2].moveAlpha = 0.5;
262end

onDetach

Description
Called if vehicle gets detached
Definition
onDetach(table attacherVehicle, integer jointDescIndex)
Arguments
tableattacherVehicleattacher vehicle
integerjointDescIndexindex of attacher joint it was attached to
Code
268function AttacherJointControl:onDetach(attacherVehicle, jointDescIndex)
269 self.attacherJointControl.jointDesc.allowsLowering = self.attacherJointControl.jointDesc.allowsLoweringBackup;
270 self.attacherJointControl.jointDesc = nil;
271end

updateAttacherJoint

Description
Updates attacher joint move alpha and update joint frame
Definition
updateAttacherJoint(float moveAlpha1, float moveAlpha2)
Arguments
floatmoveAlpha1move alpha of rotation node 1
floatmoveAlpha2move alpha of rotation node 2
Code
277function AttacherJointControl:updateAttacherJoint(moveAlpha1, moveAlpha2)
278 local jointDesc = self.attacherJointControl.jointDesc;
279
280 self.attacherJointControl.controls[1].moveAlpha = moveAlpha1;
281 self.attacherJointControl.controls[2].moveAlpha = moveAlpha2;
282 self.attacherJointControl.controls[1].moveAlphaSend = moveAlpha1;
283 self.attacherJointControl.controls[2].moveAlphaSend = moveAlpha2;
284
285 if jointDesc ~= nil then
286 if jointDesc.rotationNode ~= nil then
287 setRotation(jointDesc.rotationNode, Utils.vector3ArrayLerp(jointDesc.upperRotation, jointDesc.lowerRotation, moveAlpha1));
288 end
289 if jointDesc.rotationNode2 ~= nil then
290 setRotation(jointDesc.rotationNode2, Utils.vector3ArrayLerp(jointDesc.upperRotation2, jointDesc.lowerRotation2, moveAlpha2));
291 end
292 self.attacherVehicle:updateAttacherJointRotation(jointDesc, self);
293
294 if self.isServer and jointDesc.jointIndex ~= 0 then
295 setJointFrame(jointDesc.jointIndex, 0, jointDesc.jointTransform)
296 end
297 end
298end