LUADOC - Farming Simulator 22

HandTool

Description
Class for handtools
Parent
Object
Functions

delete

Description
Deleting handtool
Definition
delete()
Code
233function HandTool:delete()
234 self:removeActionEvents()
235
236 if g_currentMission ~= nil then
237 g_messageCenter:unsubscribe(MessageType.PERIOD_CHANGED, self)
238 end
239 if self.rootNode ~= nil and self.rootNode ~= 0 then
240 if self.originalHandNodeParent ~= nil and getParent(self.handNode) ~= self.originalHandNodeParent then
241 link(self.originalHandNodeParent, self.handNode)
242 end
243
244 delete(self.rootNode)
245 end
246
247 if self.sharedLoadRequestId ~= nil then
248 g_i3DManager:releaseSharedI3DFile(self.sharedLoadRequestId)
249 self.sharedLoadRequestId = nil
250 end
251
252 HandTool:superClass().delete(self)
253
254 self.isDeleted = true
255end

getDailyUpkeep

Description
Get daily up keep
Definition
getDailyUpkeep()
Return Values
floatdailyUpkeepdaily up keep
Code
314function HandTool:getDailyUpkeep()
315 local storeItem = g_storeManager:getItemByXMLFilename(self.configFileName)
316 local multiplier = 1
317
318 if storeItem.lifetime ~= nil and storeItem.lifetime ~= 0 then
319 local ageMultiplier = math.min(self.age / storeItem.lifetime, 1)
320 multiplier = EconomyManager.MAX_DAILYUPKEEP_MULTIPLIER * ageMultiplier
321 end
322
323 return StoreItemUtil.getDailyUpkeep(storeItem, nil) * multiplier
324end

getNeedCustomWorkStyle

Description
Definition
getNeedCustomWorkStyle()
Code
267function HandTool:getNeedCustomWorkStyle()
268 return true
269end

getSellPrice

Description
Get sell price
Definition
getSellPrice()
Return Values
floatsellPricesell price
Code
329function HandTool:getSellPrice()
330 local priceMultiplier = 0.5
331 local storeItem = g_storeManager:getItemByXMLFilename(self.configFileName)
332 local maxVehicleAge = storeItem.lifetime
333
334 if maxVehicleAge ~= nil and maxVehicleAge ~= 0 then
335 priceMultiplier = priceMultiplier * math.exp(-3.5 * math.min(self.age/maxVehicleAge, 1))
336 end
337
338 return math.floor(self.price * math.max(priceMultiplier, 0.05))
339end

handToolI3DLoaded

Description
Definition
handToolI3DLoaded()
Code
132function HandTool:handToolI3DLoaded(i3dNode, failedReason, args)
133 if i3dNode ~= 0 then
134
135 local xmlFile = args.xmlFile
136 local asyncCallbackFunction = args.asyncCallbackFunction
137 local asyncCallbackArguments = args.asyncCallbackArguments
138
139 if not self.isDeleted then
140 self.rootNode = getChildAt(i3dNode, 0)
141
142 -- fill components
143 local numChildren = getNumOfChildren(i3dNode)
144 for i=0, numChildren - 1 do
145 local component = {}
146 component.node = getChildAt(i3dNode, i)
147
148 table.insert(self.components, component)
149 end
150
151 unlink(self.rootNode)
152
153 I3DUtil.loadI3DMapping(xmlFile, "handTool", self.components, self.i3dMappings)
154
155 self:postLoad(xmlFile)
156 end
157
158 xmlFile:delete()
159 delete(i3dNode)
160
161 if asyncCallbackFunction ~= nil then
162 asyncCallbackFunction(self.player, self, asyncCallbackArguments)
163 end
164 end
165end

init

Description
Initialize hand tool and hand tool types
Definition
init()
Code
51function HandTool.init()
52 for _, classObject in pairs(HandTool.handToolTypes) do
53 if rawget(classObject, "init") then
54 classObject.init()
55 end
56 end
57end

isBeingUsed

Description
Definition
isBeingUsed()
Code
349function HandTool:isBeingUsed()
350 return false
351end

load

Description
Load chainsaw from xml file
Definition
load(string xmlFilename, table player)
Arguments
stringxmlFilenamexml file name
tableplayerplayer
Return Values
booleansuccesssuccess
Code
92function HandTool:load(xmlFilename, player, asyncCallbackFunction, asyncCallbackArguments)
93 self.configFileName = xmlFilename
94
95 self.customEnvironment, self.baseDirectory = Utils.getModNameAndBaseDirectory(xmlFilename)
96
97 local xmlFile = XMLFile.load("TempXML", xmlFilename, HandTool.xmlSchema)
98 if xmlFile == nil then
99 return false
100 end
101
102 local i3dFilename = xmlFile:getValue("handTool.filename")
103 if i3dFilename == nil then
104 xmlFile:delete()
105 return false
106 end
107
108 self.i3dFilename = Utils.getFilename(i3dFilename, self.baseDirectory)
109 self.player = player
110
111 g_i3DManager:pinSharedI3DFileInCache(self.i3dFilename)
112
113 local arguments = {
114 xmlFile = xmlFile,
115 asyncCallbackFunction = asyncCallbackFunction,
116 asyncCallbackArguments = asyncCallbackArguments
117 }
118
119 if asyncCallbackFunction ~= nil then
120 self.sharedLoadRequestId = g_i3DManager:loadSharedI3DFileAsync(self.i3dFilename, false, false, self.handToolI3DLoaded, self, arguments)
121 else
122 local i3dNode, sharedLoadRequestId, failedReason = g_i3DManager:loadSharedI3DFile(self.i3dFilename, false, false)
123 self.sharedLoadRequestId = sharedLoadRequestId
124 self:handToolI3DLoaded(i3dNode, failedReason, arguments)
125 end
126
127 return true
128end

loadFromXMLFile

Description
Definition
loadFromXMLFile()
Code
301function HandTool:loadFromXMLFile(xmlFile, key, resetVehicles)
302 return true
303end

new

Description
Creating handtool object
Definition
new(boolean isServer, boolean isClient, table customMt)
Arguments
booleanisServeris server
booleanisClientis client
tablecustomMtcustom metatable
Return Values
tableinstanceInstance of object
Code
65function HandTool.new(isServer, isClient, customMt)
66 local mt = customMt
67 if mt == nil then
68 mt = HandTool_mt
69 end
70
71 local self = Object.new(isServer, isClient, mt)
72 self.static = true
73 self.player = nil
74 self.owner = nil
75 self.currentPlayerHandNode = nil
76 self.price = 0
77 self.age = 0
78 self.activatePressed = false
79 self.isDeleted = false
80
81 self.components = {}
82 self.i3dMappings = {}
83
84 return self
85end

onActivate

Description
On activate
Definition
onActivate(boolean allowInput)
Arguments
booleanallowInputallow input
Code
274function HandTool:onActivate(allowInput)
275 setVisibility(self.rootNode, true)
276 self.isActive = true
277 self:raiseActive()
278
279 if self.player.isOwner then
280 self:registerActionEvents()
281 end
282
283 if self:getNeedCustomWorkStyle() then
284 self.player:setCustomWorkStylePreset(self.customWorkStylePresetName)
285 end
286end

onDeactivate

Description
On deactivate
Definition
onDeactivate(boolean allowInput)
Arguments
booleanallowInputallow input
Code
291function HandTool:onDeactivate(allowInput)
292 setVisibility(self.rootNode, false)
293 self.isActive = false
294 self:removeActionEvents()
295
296 self.player:setCustomWorkStylePreset(nil)
297end

periodChanged

Description
Called if day changed
Definition
periodChanged()
Code
343function HandTool:periodChanged()
344 self.age = self.age + 1
345end

postLoad

Description
Called after hand tool i3d file was loaded
Definition
postLoad(table xmlFile)
Arguments
tablexmlFilexmlFile
Code
170function HandTool:postLoad(xmlFile)
171 self.handNodePosition = {}
172 self.handNodeRotation = {}
173 self.handNode = nil
174 self.originalHandNodeParent = nil
175 self.referenceNode = nil
176 if self.player == g_currentMission.player then
177 self.handNodePosition = xmlFile:getValue("handTool.handNode.firstPerson#position", "0 0 0", true)
178 self.handNodeRotation = xmlFile:getValue("handTool.handNode.firstPerson#rotation", "0 0 0", true)
179 self.handNode = xmlFile:getValue("handTool.handNode.firstPerson#node", self.rootNode, self.components, self.i3dMappings)
180 self.referenceNode = xmlFile:getValue("handTool.handNode.firstPerson#referenceNode", nil, self.components, self.i3dMappings)
181 else
182 self.handNodePosition = xmlFile:getValue("handTool.handNode.thirdPerson#position", "0 0 0", true)
183 self.handNodeRotation = xmlFile:getValue("handTool.handNode.thirdPerson#rotation", "0 0 0", true)
184 self.handNode = xmlFile:getValue("handTool.handNode.thirdPerson#node", self.rootNode, self.components, self.i3dMappings)
185 self.referenceNode = xmlFile:getValue("handTool.handNode.thirdPerson#referenceNode", nil, self.components, self.i3dMappings)
186 end
187
188 if self.rootNode ~= self.handNode then
189 self.originalHandNodeParent = getParent(self.handNode)
190 end
191
192 self.customWorkStylePresetName = xmlFile:getValue("handTool.playerWorkStylePreset", nil)
193
194 setTranslation(self.handNode, unpack(self.handNodePosition))
195 setRotation(self.handNode, unpack(self.handNodeRotation))
196
197 local storeItem = g_storeManager:getItemByXMLFilename(self.configFileName)
198 if self.price == 0 or self.price == nil then
199 self.price = StoreItemUtil.getDefaultPrice(storeItem)
200 end
201
202 if g_currentMission ~= nil and storeItem.canBeSold then
203 g_messageCenter:subscribe(MessageType.PERIOD_CHANGED, self.periodChanged, self)
204 end
205
206 self.targets = {}
207 IKUtil.loadIKChainTargets(xmlFile, "handTool.targets", self.components, self.targets, self.i3dMappings)
208
209 setVisibility(self.rootNode, false)
210 self.isActive = false
211
212 return true
213end

registerActionEvents

Description
Definition
registerActionEvents()
Code
355function HandTool:registerActionEvents()
356 g_inputBinding:beginActionEventsModification(Player.INPUT_CONTEXT_NAME)
357 -- @Note: register general handtool actions here
358 g_inputBinding:endActionEventsModification()
359end

removeActionEvents

Description
Definition
removeActionEvents()
Code
363function HandTool:removeActionEvents()
364 g_inputBinding:beginActionEventsModification(Player.INPUT_CONTEXT_NAME)
365 g_inputBinding:removeActionEventsByTarget(self)
366 g_inputBinding:endActionEventsModification()
367end

saveToXMLFile

Description
Definition
saveToXMLFile()
Code
307function HandTool:saveToXMLFile(xmlFile, key, usedModNames)
308 xmlFile:setValue(key.."#filename", HTMLUtil.encodeToHTML(NetworkUtil.convertToNetworkFilename(self.configFileName)))
309end

setHandNode

Description
Definition
setHandNode()
Code
217function HandTool:setHandNode(playerHandNode)
218 if self.currentPlayerHandNode ~= playerHandNode then
219 self.currentPlayerHandNode = playerHandNode
220 link(playerHandNode, self.handNode)
221
222 if self.referenceNode ~= nil then
223 local x, y, z = getWorldTranslation(self.referenceNode)
224 x, y, z = worldToLocal(getParent(self.handNode), x, y, z)
225 local a, b, c = getTranslation(self.handNode)
226 setTranslation(self.handNode, a - x, b - y, c - z)
227 end
228 end
229end

update

Description
Definition
update()
Code
259function HandTool:update(dt, allowInput)
260 if self.isActive then
261 self:raiseActive()
262 end
263end