LUADOC - Farming Simulator 22

Script v1_7_1_0

Engine v1_7_1_0

Foundation Reference

SmartAttach

Description
This is the activable class for smartAttach
Functions

doSmartAttach

Description
Definition
doSmartAttach()
Code
104function SmartAttach:doSmartAttach(targetVehicle, inputJointDescIndex, jointDescIndex, noEventSend)
105 SmartAttachEvent.sendEvent(self, targetVehicle, inputJointDescIndex, jointDescIndex, noEventSend)
106
107 if self.isServer then
108 local attacherVehicle = self:getAttacherVehicle()
109 if attacherVehicle ~= nil then
110 attacherVehicle:detachImplementByObject(self)
111 end
112
113 targetVehicle:attachImplement(self, inputJointDescIndex, jointDescIndex, false)
114 end
115end

getCanBeSmartAttached

Description
Definition
getCanBeSmartAttached()
Code
119function SmartAttach:getCanBeSmartAttached()
120 local spec = self.spec_smartAttach
121
122 local targetVehicle = spec.targetVehicle
123 if targetVehicle == nil then
124 return false
125 end
126
127 local activeForInput = self:getIsActiveForInput(true) or spec.targetVehicle:getIsActiveForInput(true)
128 if not activeForInput then
129 return false
130 end
131
132 local attacherJoint = targetVehicle:getAttacherJoints()[spec.jointDescIndex].jointTransform
133 local inputAttacherJoint = self:getInputAttacherJoints()[spec.inputJointDescIndex].node
134 local x1, _, z1 = getWorldTranslation(attacherJoint)
135 local x2, _, z2 = getWorldTranslation(inputAttacherJoint)
136
137 local distance = MathUtil.vector2Length(x1-x2, z1-z2)
138 local yRot = Utils.getYRotationBetweenNodes(attacherJoint, inputAttacherJoint)
139
140 return distance < SmartAttach.DISTANCE_THRESHOLD and math.abs(yRot) < SmartAttach.ABS_ANGLE_THRESHOLD
141end

initSpecialization

Description
Definition
initSpecialization()
Code
22function SmartAttach.initSpecialization()
23 local schema = Vehicle.xmlSchema
24 schema:setXMLSpecializationType("SmartAttach")
25
26 schema:register(XMLValueType.STRING, "vehicle.smartAttach#jointType", "Joint type name")
27 schema:register(XMLValueType.NODE_INDEX, "vehicle.smartAttach#trigger", "Trigger node")
28
29 schema:setXMLSpecializationType()
30end

onDelete

Description
Definition
onDelete()
Code
88function SmartAttach:onDelete()
89 local spec = self.spec_smartAttach
90
91 if spec.activatable ~= nil then
92 g_currentMission.activatableObjectsSystem:removeActivatable(spec.activatable)
93 spec.activatable = nil
94 end
95
96 if spec.trigger ~= nil then
97 removeTrigger(spec.trigger)
98 spec.trigger = nil
99 end
100end

onLoad

Description
Definition
onLoad()
Code
50function SmartAttach:onLoad(savegame)
51 local spec = self.spec_smartAttach
52
53 spec.inputJointDescIndex = nil
54 local jointTypeStr = self.xmlFile:getValue("vehicle.smartAttach#jointType")
55 if jointTypeStr ~= nil then
56 local jointType = AttacherJoints.jointTypeNameToInt[jointTypeStr]
57 if jointType ~= nil then
58 for inputJointDescIndex, inputAttacherJoint in pairs(self:getInputAttacherJoints()) do
59 if inputAttacherJoint.jointType == jointType then
60 spec.inputJointDescIndex = inputJointDescIndex
61 break
62 end
63 end
64 spec.jointType = jointType
65
66 if spec.inputJointDescIndex == nil then
67 print("Warning: SmartAttach jointType not defined in '"..self.configFileName.."'!")
68 end
69 else
70 print("Warning: invalid jointType " .. jointTypeStr)
71 end
72 end
73
74 local triggerNode = self.xmlFile:getValue("vehicle.smartAttach#trigger", nil, self.components, self.i3dMappings)
75 if triggerNode ~= nil then
76 spec.trigger = triggerNode
77 addTrigger(spec.trigger, "smartAttachCallback", self)
78 end
79
80 spec.targetVehicle = nil
81 spec.targetVehicleCount = 0
82 spec.jointDescIndex = nil
83 spec.activatable = SmartAttachActivatable.new(self)
84end

onPreAttach

Description
Definition
onPreAttach()
Code
145function SmartAttach:onPreAttach()
146 local spec = self.spec_smartAttach
147
148 spec.targetVehicle = nil
149 spec.targetVehicleCount = 0
150end

prerequisitesPresent

Description
Definition
prerequisitesPresent()
Code
16function SmartAttach.prerequisitesPresent(specializations)
17 return true
18end

registerEventListeners

Description
Definition
registerEventListeners()
Code
42function SmartAttach.registerEventListeners(vehicleType)
43 SpecializationUtil.registerEventListener(vehicleType, "onLoad", SmartAttach)
44 SpecializationUtil.registerEventListener(vehicleType, "onDelete", SmartAttach)
45 SpecializationUtil.registerEventListener(vehicleType, "onPreAttach", SmartAttach)
46end

registerFunctions

Description
Definition
registerFunctions()
Code
34function SmartAttach.registerFunctions(vehicleType)
35 SpecializationUtil.registerFunction(vehicleType, "smartAttachCallback", SmartAttach.smartAttachCallback)
36 SpecializationUtil.registerFunction(vehicleType, "getCanBeSmartAttached", SmartAttach.getCanBeSmartAttached)
37 SpecializationUtil.registerFunction(vehicleType, "doSmartAttach", SmartAttach.doSmartAttach)
38end

smartAttachCallback

Description
Trigger callback
Definition
smartAttachCallback(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
160function SmartAttach:smartAttachCallback(triggerId, otherActorId, onEnter, onLeave, onStay, otherShapeId)
161 local spec = self.spec_smartAttach
162
163 if onEnter then
164 local vehicle = g_currentMission.nodeToObject[otherActorId]
165 if vehicle ~= nil then
166 if spec.targetVehicle == nil then
167 if vehicle ~= nil and vehicle ~= self and vehicle.getAttacherJoints ~= nil then
168 for i, jointDesc in ipairs(vehicle:getAttacherJoints()) do
169 if jointDesc.jointIndex == 0 and jointDesc.jointType == spec.jointType then
170 spec.targetVehicle = vehicle
171 spec.jointDescIndex = i
172 spec.targetVehicleCount = 0
173
174 local name = Utils.getNoNil(self.typeDesc, "")
175 local storeItem = g_storeManager:getItemByXMLFilename(self.configFileName:lower())
176 if storeItem ~= nil then
177 name = storeItem.name
178 end
179
180 if self:getAttacherVehicle() == nil then
181 spec.activatable.activateText = string.format(g_i18n:getText("action_doSmartAttachGround", self.customEnvironment), name)
182 else
183 spec.activatable.activateText = string.format(g_i18n:getText("action_doSmartAttachTransform", self.customEnvironment), name)
184 end
185
186 g_currentMission.activatableObjectsSystem:addActivatable(spec.activatable)
187 break
188 end
189 end
190 end
191 end
192
193 if vehicle == spec.targetVehicle then
194 spec.targetVehicleCount = spec.targetVehicleCount + 1
195 end
196 end
197 elseif onLeave then
198 if spec.targetVehicle ~= nil then
199 local object = g_currentMission.nodeToObject[otherActorId]
200 if object ~= nil and object == spec.targetVehicle then
201 spec.targetVehicleCount = spec.targetVehicleCount - 1
202 if spec.targetVehicleCount <= 0 then
203 spec.targetVehicle = nil
204 g_currentMission.activatableObjectsSystem:removeActivatable(spec.activatable)
205 spec.targetVehicleCount = 0
206 end
207 end
208 end
209 end
210end