LUADOC - Farming Simulator 22

Script v1_7_1_0

Engine v1_7_1_0

Foundation Reference

BigBag

Description
Specialization for big bags to control joint limits and size animation
Functions

initSpecialization

Description
Definition
initSpecialization()
Code
23function BigBag.initSpecialization()
24 local schema = Vehicle.xmlSchema
25 schema:setXMLSpecializationType("BigBag")
26
27 schema:register(XMLValueType.INT, "vehicle.bigBag#fillUnitIndex", "Fill unit index")
28
29 schema:register(XMLValueType.STRING, "vehicle.bigBag.sizeAnimation#name", "Name of size animation")
30 schema:register(XMLValueType.FLOAT, "vehicle.bigBag.sizeAnimation#minTime", "Min. animation that is used while it's empty", 0)
31 schema:register(XMLValueType.FLOAT, "vehicle.bigBag.sizeAnimation#maxTime", "Max. animation that is used while it's full", 1)
32
33 schema:register(XMLValueType.FLOAT, "vehicle.bigBag.sizeAnimation#liftShrinkTime", "Time of animation that is reduced while the big bag is lifted", 0.2)
34
35 schema:register(XMLValueType.INT, "vehicle.bigBag.componentJoint#index", "Component Joint Index", 1)
36 schema:register(XMLValueType.VECTOR_ROT, "vehicle.bigBag.componentJoint#minRotLimit", "Rot Limit if trans limit is at min")
37 schema:register(XMLValueType.VECTOR_ROT, "vehicle.bigBag.componentJoint#maxRotLimit", "Rot Limit if trans limit is at max")
38 schema:register(XMLValueType.VECTOR_TRANS, "vehicle.bigBag.componentJoint#minTransLimit", "Trans Limit if big bag is empty")
39 schema:register(XMLValueType.VECTOR_TRANS, "vehicle.bigBag.componentJoint#maxTransLimit", "Trans Limit if big bag is full")
40
41 schema:register(XMLValueType.FLOAT, "vehicle.bigBag.componentJoint#angularDamping", "Angular damping of components", 0.01)
42
43 schema:setXMLSpecializationType()
44end

onFillUnitFillLevelChanged

Description
Definition
onFillUnitFillLevelChanged()
Code
156function BigBag:onFillUnitFillLevelChanged(fillUnitIndex, fillLevelDelta, fillType, toolType, fillPositionData, appliedDelta)
157 local spec = self.spec_bigBag
158 if spec.fillUnitIndex == fillUnitIndex then
159 local fillLevelPct = self:getFillUnitFillLevelPercentage(fillUnitIndex)
160 spec.currentSizeTime = fillLevelPct * (spec.sizeAnimationMaxTime - spec.sizeAnimationMinTime) + spec.sizeAnimationMinTime
161
162 spec.currentAnimationTime = spec.currentSizeTime * (1 - spec.currentShrinkTime)
163 self:setAnimationTime(spec.sizeAnimationName, spec.currentAnimationTime)
164
165 if self.isServer then
166 if spec.minTransLimit ~= nil and spec.maxTransLimit ~= nil then
167 local x, y, z = MathUtil.vector3ArrayLerp(spec.minTransLimit, spec.maxTransLimit, fillLevelPct)
168
169 self:setComponentJointTransLimit(spec.componentJoint, 1, -x, x)
170 self:setComponentJointTransLimit(spec.componentJoint, 2, -y, y)
171 self:setComponentJointTransLimit(spec.componentJoint, 3, -z, z)
172 end
173 end
174 end
175end

onLoad

Description
Called on loading
Definition
onLoad(table savegame)
Arguments
tablesavegamesavegame
Code
67function BigBag:onLoad(savegame)
68 local spec = self.spec_bigBag
69
70 spec.fillUnitIndex = self.xmlFile:getValue("vehicle.bigBag#fillUnitIndex", 1)
71
72 spec.sizeAnimationName = self.xmlFile:getValue("vehicle.bigBag.sizeAnimation#name")
73 spec.sizeAnimationMinTime = self.xmlFile:getValue("vehicle.bigBag.sizeAnimation#minTime", 0)
74 spec.sizeAnimationMaxTime = self.xmlFile:getValue("vehicle.bigBag.sizeAnimation#maxTime", 1)
75
76 spec.sizeAnimationLiftShrinkTime = self.xmlFile:getValue("vehicle.bigBag.sizeAnimation#liftShrinkTime", 0.2)
77
78 spec.componentJointIndex = self.xmlFile:getValue("vehicle.bigBag.componentJoint#index", 1)
79 local jointDesc = self.componentJoints[spec.componentJointIndex]
80 if jointDesc ~= nil then
81 spec.minRotLimit = self.xmlFile:getValue("vehicle.bigBag.componentJoint#minRotLimit", nil, true)
82 spec.maxRotLimit = self.xmlFile:getValue("vehicle.bigBag.componentJoint#maxRotLimit", nil, true)
83 if #spec.minRotLimit ~= 3 or #spec.maxRotLimit ~= 3 then
84 spec.minRotLimit = nil
85 spec.maxRotLimit = nil
86 end
87
88 spec.minTransLimit = self.xmlFile:getValue("vehicle.bigBag.componentJoint#minTransLimit", nil, true)
89 spec.maxTransLimit = self.xmlFile:getValue("vehicle.bigBag.componentJoint#maxTransLimit", nil, true)
90 if #spec.minTransLimit ~= 3 or #spec.maxTransLimit ~= 3 then
91 spec.minTransLimit = nil
92 spec.maxTransLimit = nil
93 end
94
95 spec.componentJoint = jointDesc
96 spec.jointNode = jointDesc.jointNode
97 spec.jointNodeReferenceNode = createTransformGroup("jointNodeReference")
98 link(self.components[jointDesc.componentIndices[2]].node, spec.jointNodeReferenceNode)
99 setWorldTranslation(spec.jointNodeReferenceNode, getWorldTranslation(spec.jointNode))
100 setWorldRotation(spec.jointNodeReferenceNode, getWorldRotation(spec.jointNode))
101
102 spec.component1 = self.components[spec.componentJoint.componentIndices[1]]
103 spec.component2 = self.components[spec.componentJoint.componentIndices[2]]
104
105 spec.angularDamping = self.xmlFile:getValue("vehicle.bigBag.componentJoint#angularDamping", 0.01)
106 setAngularDamping(spec.component1.node, spec.angularDamping)
107 setAngularDamping(spec.component2.node, spec.angularDamping)
108
109 spec.lastJointLimitAlpha = -1
110 end
111
112 spec.currentShrinkTime = 0
113 spec.currentSizeTime = 1
114 spec.currentAnimationTime = 1
115
116end

onUpdate

Description
Called on update
Definition
onUpdate(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
123function BigBag:onUpdate(dt, isActiveForInput, isActiveForInputIgnoreSelection, isSelected)
124 local spec = self.spec_bigBag
125
126 if spec.jointNode ~= nil then
127 local xLimit, _, _ = MathUtil.lerp(spec.minTransLimit[1], spec.maxTransLimit[1], self:getFillUnitFillLevelPercentage(spec.fillUnitIndex))
128 local xOffset, _, _ = localToLocal(spec.jointNode, spec.jointNodeReferenceNode, 0, 0, 0)
129 local alpha = 1 - MathUtil.clamp((xOffset / xLimit + 1) / 2, 0, 1)
130 spec.currentShrinkTime = alpha * spec.sizeAnimationLiftShrinkTime
131
132 if self.isServer then
133 if math.abs(spec.lastJointLimitAlpha - alpha) > 0.05 then
134 if spec.minRotLimit ~= nil and spec.maxRotLimit ~= nil then
135 local rx, ry, rz = MathUtil.vector3ArrayLerp(spec.minRotLimit, spec.maxRotLimit, alpha)
136
137 self:setComponentJointRotLimit(spec.componentJoint, 1, -rx, rx)
138 self:setComponentJointRotLimit(spec.componentJoint, 2, -ry, ry)
139 self:setComponentJointRotLimit(spec.componentJoint, 3, -rz, rz)
140 end
141
142 spec.lastJointLimitAlpha = alpha
143 end
144 end
145
146 local newAnimationTime = spec.currentSizeTime * (1 - spec.currentShrinkTime)
147 if math.abs(newAnimationTime - spec.currentAnimationTime) > 0.01 then
148 self:setAnimationTime(spec.sizeAnimationName, newAnimationTime)
149 spec.currentAnimationTime = newAnimationTime
150 end
151 end
152end

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 BigBag.prerequisitesPresent(specializations)
18 return true
19end

registerEventListeners

Description
Definition
registerEventListeners()
Code
58function BigBag.registerEventListeners(vehicleType)
59 SpecializationUtil.registerEventListener(vehicleType, "onLoad", BigBag)
60 SpecializationUtil.registerEventListener(vehicleType, "onUpdate", BigBag)
61 SpecializationUtil.registerEventListener(vehicleType, "onFillUnitFillLevelChanged", BigBag)
62end

registerFunctions

Description
Definition
registerFunctions()
Code
48function BigBag.registerFunctions(vehicleType)
49end

registerOverwrittenFunctions

Description
Definition
registerOverwrittenFunctions()
Code
53function BigBag.registerOverwrittenFunctions(vehicleType)
54end