LUADOC - Farming Simulator 22

Script v1_7_1_0

Engine v1_7_1_0

Foundation Reference

HookLiftTrailer

Description
Specialization for hooklift trailers providing separate load-up and tipping animations
Functions

getCanDetachContainer

Description
Definition
getCanDetachContainer()
Code
207function HookLiftTrailer:getCanDetachContainer()
208 local spec = self.spec_hookLiftTrailer
209 return self:getAnimationTime(spec.refAnimation) == 1
210end

getDoConsumePtoPower

Description
Returns if should consume pto power
Definition
getDoConsumePtoPower()
Return Values
booleanconsumeconsumePtoPower
Code
238function HookLiftTrailer:getDoConsumePtoPower(superFunc)
239 local spec = self.spec_hookLiftTrailer
240 local doConsume = superFunc(self)
241
242 return doConsume or self:getIsAnimationPlaying(spec.refAnimation) or self:getIsAnimationPlaying(spec.unloadingAnimation)
243end

getIsFoldAllowed

Description
Returns if fold is allowed
Definition
getIsFoldAllowed()
Return Values
booleanallowsFoldallows folding
Code
215function HookLiftTrailer:getIsFoldAllowed(superFunc, direction, onAiTurnOn)
216 local spec = self.spec_hookLiftTrailer
217 if self:getAnimationTime(spec.unloadingAnimation) > 0 then
218 return false
219 end
220
221 return superFunc(self, direction, onAiTurnOn)
222end

getIsTippingAllowed

Description
Returns if tipping is allowed
Definition
getIsTippingAllowed()
Return Values
booleantippingAllowedtipping is allowed
Code
200function HookLiftTrailer:getIsTippingAllowed()
201 local spec = self.spec_hookLiftTrailer
202 return self:getAnimationTime(spec.refAnimation) == 0
203end

getPtoRpm

Description
Returns rpm of pto
Definition
getPtoRpm()
Return Values
floatrpmrpm of pto
Code
248function HookLiftTrailer:getPtoRpm(superFunc)
249 local spec = self.spec_hookLiftTrailer
250 local rpm = superFunc(self)
251
252 if self:getIsAnimationPlaying(spec.refAnimation) or self:getIsAnimationPlaying(spec.unloadingAnimation) then
253 return self.spec_powerConsumer.ptoRpm
254 else
255 return rpm
256 end
257end

initSpecialization

Description
Called on specialization initializing
Definition
initSpecialization()
Code
23function HookLiftTrailer.initSpecialization()
24 local schema = Vehicle.xmlSchema
25 schema:setXMLSpecializationType("HookLiftTrailer")
26
27 schema:register(XMLValueType.FLOAT, "vehicle.hookLiftTrailer.jointLimits.key(?)#time", "Key time")
28 schema:register(XMLValueType.VECTOR_ROT, "vehicle.hookLiftTrailer.jointLimits.key(?)#rotLimit", "Rotation limit", "0 0 0")
29 schema:register(XMLValueType.VECTOR_TRANS, "vehicle.hookLiftTrailer.jointLimits.key(?)#transLimit", "Translation limit", "0 0 0")
30
31 schema:register(XMLValueType.STRING, "vehicle.hookLiftTrailer.jointLimits#refAnimation", "Reference animation", "unfoldHand")
32 schema:register(XMLValueType.STRING, "vehicle.hookLiftTrailer.unloadingAnimation#name", "Unload animation", "unloading")
33 schema:register(XMLValueType.FLOAT, "vehicle.hookLiftTrailer.unloadingAnimation#speed", "Unload animation speed", 1)
34 schema:register(XMLValueType.FLOAT, "vehicle.hookLiftTrailer.unloadingAnimation#reverseSpeed", "Unload animation reverse speed", -1)
35
36 schema:register(XMLValueType.STRING, "vehicle.hookLiftTrailer.texts#unloadContainer", "Unload container text", "unload_container")
37 schema:register(XMLValueType.STRING, "vehicle.hookLiftTrailer.texts#loadContainer", "Load container text", "load_container")
38 schema:register(XMLValueType.STRING, "vehicle.hookLiftTrailer.texts#unloadArm", "Unload arm text", "unload_arm")
39 schema:register(XMLValueType.STRING, "vehicle.hookLiftTrailer.texts#loadArm", "Load arm text", "load_arm")
40
41 schema:setXMLSpecializationType()
42end

isDetachAllowed

Description
Returns true if detach is allowed
Definition
isDetachAllowed()
Return Values
booleandetachAlloweddetach is allowed
Code
227function HookLiftTrailer:isDetachAllowed(superFunc)
228 if self:getAnimationTime(self.spec_hookLiftTrailer.unloadingAnimation) == 0 then
229 return superFunc(self)
230 else
231 return false, nil
232 end
233end

onLoad

Description
Called on loading
Definition
onLoad(table savegame)
Arguments
tablesavegamesavegame
Code
75function HookLiftTrailer:onLoad(savegame)
76 local spec = self.spec_hookLiftTrailer
77
78 spec.jointLimits = AnimCurve.new(linearInterpolatorN)
79 local i = 0
80 while true do
81 local key = string.format("vehicle.hookLiftTrailer.jointLimits.key(%d)", i)
82 if not self.xmlFile:hasProperty(key) then
83 if i == 0 then
84 spec.jointLimits = nil
85 end
86 break
87 end
88
89 local t = self.xmlFile:getValue(key.."#time")
90 local rx, ry, rz = self.xmlFile:getValue(key.."#rotLimit", "0 0 0")
91 local tx, ty, tz = self.xmlFile:getValue(key.."#transLimit", "0 0 0")
92 spec.jointLimits:addKeyframe({rx, ry, rz, tx, ty, tz, time = t})
93
94 i = i + 1
95 end
96
97 spec.refAnimation = self.xmlFile:getValue("vehicle.hookLiftTrailer.jointLimits#refAnimation", "unfoldHand")
98 spec.unloadingAnimation = self.xmlFile:getValue("vehicle.hookLiftTrailer.unloadingAnimation#name", "unloading")
99 spec.unloadingAnimationSpeed = self.xmlFile:getValue("vehicle.hookLiftTrailer.unloadingAnimation#speed", 1)
100 spec.unloadingAnimationReverseSpeed = self.xmlFile:getValue("vehicle.hookLiftTrailer.unloadingAnimation#reverseSpeed", -1)
101
102 spec.texts = {}
103 spec.texts.unloadContainer = g_i18n:getText(self.xmlFile:getValue("vehicle.hookLiftTrailer.texts#unloadContainer", "unload_container"), self.customEnvironment)
104 spec.texts.loadContainer = g_i18n:getText(self.xmlFile:getValue("vehicle.hookLiftTrailer.texts#loadContainer", "load_container"), self.customEnvironment)
105 spec.texts.unloadArm = g_i18n:getText(self.xmlFile:getValue("vehicle.hookLiftTrailer.texts#unloadArm", "unload_arm"), self.customEnvironment)
106 spec.texts.loadArm = g_i18n:getText(self.xmlFile:getValue("vehicle.hookLiftTrailer.texts#loadArm", "load_arm"), self.customEnvironment)
107end

onPostAttachImplement

Description
Called on attaching a implement
Definition
onPostAttachImplement(table implement)
Arguments
tableimplementimplement to attach
Code
148function HookLiftTrailer:onPostAttachImplement(attachable, inputJointDescIndex, jointDescIndex)
149 local spec = self.spec_hookLiftTrailer
150
151 local attacherJoint = attachable:getActiveInputAttacherJoint()
152 if attacherJoint ~= nil then
153 if attacherJoint.jointType == AttacherJoints.JOINTTYPE_HOOKLIFT then
154 local jointDesc = self:getAttacherJointByJointDescIndex(jointDescIndex)
155 spec.attachedContainer = {}
156 spec.attachedContainer.jointIndex = jointDesc.jointIndex
157 spec.attachedContainer.implement = self:getImplementByObject(attachable)
158 spec.attachedContainer.object = attachable
159 spec.attachedContainer.limitLocked = false
160
161 local foldableSpec = self.spec_foldable
162 foldableSpec.posDirectionText = spec.texts.unloadContainer
163 foldableSpec.negDirectionText = spec.texts.loadContainer
164 end
165 end
166end

onPostLoad

Description
Called after loading
Definition
onPostLoad(table savegame)
Arguments
tablesavegamesavegame
Code
112function HookLiftTrailer:onPostLoad(savegame)
113 local spec = self.spec_hookLiftTrailer
114 local foldableSpec = self.spec_foldable
115 foldableSpec.posDirectionText = spec.texts.unloadArm
116 foldableSpec.negDirectionText = spec.texts.loadArm
117end

onPreDetachImplement

Description
Called on detaching a implement
Definition
onPreDetachImplement(integer implementIndex)
Arguments
integerimplementIndexindex of implement to detach
Code
171function HookLiftTrailer:onPreDetachImplement(implement)
172 local spec = self.spec_hookLiftTrailer
173 if spec.attachedContainer ~= nil then
174 if implement == spec.attachedContainer.implement then
175 local foldableSpec = self.spec_foldable
176 foldableSpec.posDirectionText = spec.texts.unloadArm
177 foldableSpec.negDirectionText = spec.texts.loadArm
178 spec.attachedContainer = nil
179 end
180 end
181end

onUpdateTick

Description
Called on update tick
Definition
onUpdateTick(float dt, boolean isActiveForInput, boolean isSelected)
Arguments
floatdttime since last call in ms
booleanisActiveForInputtrue if vehicle is active for input
booleanisSelectedtrue if vehicle is selected
Code
124function HookLiftTrailer:onUpdateTick(dt, isActiveForInput, isActiveForInputIgnoreSelection, isSelected)
125 local spec = self.spec_hookLiftTrailer
126
127 if spec.attachedContainer ~= nil then
128 local animTime = self:getAnimationTime(spec.refAnimation)
129 spec.attachedContainer.object.allowsDetaching = animTime > 0.95
130
131 if (self:getIsAnimationPlaying(spec.refAnimation) or not spec.attachedContainer.limitLocked) and spec.jointLimits ~= nil and not spec.attachedContainer.implement.attachingIsInProgress then
132 local v = spec.jointLimits:get(animTime)
133 for i=1,3 do
134 setJointRotationLimit(spec.attachedContainer.jointIndex, i-1, true, -v[i], v[i])
135 setJointTranslationLimit(spec.attachedContainer.jointIndex, i+2, true, -v[i+3], v[i+3])
136 end
137
138 if animTime >= 0.99 then
139 spec.attachedContainer.limitLocked = true
140 end
141 end
142 end
143end

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 HookLiftTrailer.prerequisitesPresent(specializations)
18 return SpecializationUtil.hasSpecialization(AnimatedVehicle, specializations) and SpecializationUtil.hasSpecialization(Foldable, specializations)
19end

registerEventListeners

Description
Definition
registerEventListeners()
Code
64function HookLiftTrailer.registerEventListeners(vehicleType)
65 SpecializationUtil.registerEventListener(vehicleType, "onLoad", HookLiftTrailer)
66 SpecializationUtil.registerEventListener(vehicleType, "onPostLoad", HookLiftTrailer)
67 SpecializationUtil.registerEventListener(vehicleType, "onUpdateTick", HookLiftTrailer)
68 SpecializationUtil.registerEventListener(vehicleType, "onPostAttachImplement", HookLiftTrailer)
69 SpecializationUtil.registerEventListener(vehicleType, "onPreDetachImplement", HookLiftTrailer)
70end

registerFunctions

Description
Definition
registerFunctions()
Code
46function HookLiftTrailer.registerFunctions(vehicleType)
47 SpecializationUtil.registerFunction(vehicleType, "startTipping", HookLiftTrailer.startTipping)
48 SpecializationUtil.registerFunction(vehicleType, "stopTipping", HookLiftTrailer.stopTipping)
49 SpecializationUtil.registerFunction(vehicleType, "getIsTippingAllowed", HookLiftTrailer.getIsTippingAllowed)
50 SpecializationUtil.registerFunction(vehicleType, "getCanDetachContainer", HookLiftTrailer.getCanDetachContainer)
51end

registerOverwrittenFunctions

Description
Definition
registerOverwrittenFunctions()
Code
55function HookLiftTrailer.registerOverwrittenFunctions(vehicleType)
56 SpecializationUtil.registerOverwrittenFunction(vehicleType, "getIsFoldAllowed", HookLiftTrailer.getIsFoldAllowed)
57 SpecializationUtil.registerOverwrittenFunction(vehicleType, "isDetachAllowed", HookLiftTrailer.isDetachAllowed)
58 SpecializationUtil.registerOverwrittenFunction(vehicleType, "getDoConsumePtoPower", HookLiftTrailer.getDoConsumePtoPower)
59 SpecializationUtil.registerOverwrittenFunction(vehicleType, "getPtoRpm", HookLiftTrailer.getPtoRpm)
60end

startTipping

Description
Called on start tipping
Definition
startTipping()
Code
185function HookLiftTrailer:startTipping()
186 local spec = self.spec_hookLiftTrailer
187 self:playAnimation(spec.unloadingAnimation, spec.unloadingAnimationSpeed, self:getAnimationTime(spec.unloadingAnimation), true)
188end

stopTipping

Description
Called on stop tipping
Definition
stopTipping()
Code
192function HookLiftTrailer:stopTipping()
193 local spec = self.spec_hookLiftTrailer
194 self:playAnimation(spec.unloadingAnimation, spec.unloadingAnimationReverseSpeed, self:getAnimationTime(spec.unloadingAnimation), true)
195end