LUADOC - Farming Simulator 19

Script v1.7.1.0

Engine v1.7.1.0

Foundation Reference

PlayerStyle

Description
This class handles player avatar customization
Functions

applySelection

Description
Definition
applySelection()
Code
66function PlayerStyle:applySelection()
67 self:setBody(self.selectedBodyIndex)
68 self:setHair(self.selectedHairIndex)
69 self:setHat(self.selectedHatIndex)
70 self:setAccessory(self.selectedAccessoryIndex)
71 self:setJacket(self.selectedJacketIndex)
72 self:setColor(self.selectedColorIndex)
73end

copySelection

Description
Definition
copySelection()
Code
52function PlayerStyle:copySelection(other)
53 self.selectedModelIndex = other.selectedModelIndex
54 self.selectedColorIndex = other.selectedColorIndex
55 self.selectedBodyIndex = other.selectedBodyIndex
56 self.selectedHatIndex = other.selectedHatIndex
57 self.selectedAccessoryIndex = other.selectedAccessoryIndex
58 self.selectedHairIndex = other.selectedHairIndex
59 self.selectedJacketIndex = other.selectedJacketIndex
60 self.playerName = other.playerName
61 self.useDefault = other.useDefault
62end

getProtectiveVisibility

Description
Definition
getProtectiveVisibility()
Code
430function PlayerStyle:getProtectiveVisibility()
431 return self.protection.isVisible
432end

linkProtectiveWear

Description
Definition
linkProtectiveWear()
Code
158function PlayerStyle:linkProtectiveWear(linkNode)
159 if self.protection.glovesNode ~= nil then
160 link(linkNode, self.protection.glovesNode)
161 end
162end

loadXML

Description
Definition
loadXML()
Code
77function PlayerStyle:loadXML(player, playerRootNode, xmlFile, baseKey)
78 local i = 0
79
80 self.player = player
81 -- bodies
82 i = 0
83 while true do
84 local headKey = string.format("%s.playerStyle.bodies.variant(%d)#headNode", baseKey, i)
85 local armsKey = string.format("%s.playerStyle.bodies.variant(%d)#armNode", baseKey, i)
86 if not hasXMLProperty(xmlFile, headKey) or not hasXMLProperty(xmlFile, armsKey) then
87 break
88 end
89 local headNode = I3DUtil.indexToObject(playerRootNode, getXMLString(xmlFile, headKey))
90 local armsNode = I3DUtil.indexToObject(playerRootNode, getXMLString(xmlFile, armsKey))
91 table.insert(self.bodies, {headNode=headNode, armsNode=armsNode})
92 i = i + 1
93 end
94 -- hats
95 self.playerHatNode = I3DUtil.indexToObject(playerRootNode, getXMLString(xmlFile, string.format("%s.playerStyle.hats#rootNode", baseKey)))
96 self.hatReferenceFilename = getXMLString(xmlFile, string.format("%s.playerStyle.hats#filename", baseKey))
97 -- accessories
98 self.playerAccessoryNode = I3DUtil.indexToObject(playerRootNode, getXMLString(xmlFile, string.format("%s.playerStyle.hats#rootNode", baseKey)))
99 self.accessoryReferenceFilename = getXMLString(xmlFile, string.format("%s.playerStyle.hats#filename", baseKey))
100 -- hairs
101 self.hairs.hatStyleNode = I3DUtil.indexToObject(playerRootNode, getXMLString(xmlFile, string.format("%s.playerStyle.hairStyles#hatHairNode", baseKey)))
102 i = 0
103 while true do
104 local hairStyleKey = string.format("%s.playerStyle.hairStyles.variant(%d)", baseKey, i)
105 if not hasXMLProperty(xmlFile, hairStyleKey) then
106 break
107 end
108 local node = I3DUtil.indexToObject(playerRootNode, getXMLString(xmlFile, hairStyleKey.."#node"))
109 local name = Utils.getNoNil(getXMLString(xmlFile, hairStyleKey.."#name"), "")
110 local isHatHair = Utils.getNoNil(getXMLBool(xmlFile, hairStyleKey.."#isHatHair"), false)
111
112 if isHatHair and self.hairs.hatIndex == 0 then
113 self.hairs.hatIndex = i + 1
114 end
115
116 table.insert(self.hairs.styles, {node=node, name=name})
117 i = i + 1
118 end
119 -- jackets
120 i = 0
121 while true do
122 local jacketKey = string.format("%s.playerStyle.jackets.variant(%d)", baseKey, i)
123 if not hasXMLProperty(xmlFile, jacketKey) then
124 break
125 end
126 local node = I3DUtil.indexToObject(playerRootNode, getXMLString(xmlFile, jacketKey.."#node"))
127 local name = getXMLString(xmlFile, jacketKey.."#name")
128 table.insert(self.jackets, {node=node, name=name})
129 i = i + 1
130 end
131 -- chainsaw protection
132 self.protection.helmetNode = I3DUtil.indexToObject(playerRootNode, getXMLString(xmlFile, string.format("%s.playerStyle.helmet#node", baseKey)))
133 self.protection.glovesNode = I3DUtil.indexToObject(playerRootNode, getXMLString(xmlFile, string.format("%s.playerStyle.gloves#node", baseKey)))
134
135 -- default
136 if self.useDefault then
137 local defaultHatKey = string.format("%s.playerStyle#defaultHat", baseKey)
138 if hasXMLProperty(xmlFile, defaultHatKey) then
139 self.selectedHatIndex = getXMLInt(xmlFile, defaultHatKey)
140 end
141 local defaultAccessoryKey = string.format("%s.playerStyle#defaultAccessory", baseKey)
142 if hasXMLProperty(xmlFile, defaultAccessoryKey) then
143 self.selectedAccessoryIndex = getXMLInt(xmlFile, defaultAccessoryKey)
144 end
145 local defaultHairstyleKey = string.format("%s.playerStyle#defaultHaistyle", baseKey)
146 if hasXMLProperty(xmlFile, defaultHairstyleKey) then
147 self.selectedHairIndex = getXMLInt(xmlFile, defaultHairstyleKey)
148 end
149 local defaultBodyKey = string.format("%s.playerStyle#defaultBody", baseKey)
150 if hasXMLProperty(xmlFile, defaultBodyKey) then
151 self.selectedBodyIndex = getXMLInt(xmlFile, defaultBodyKey)
152 end
153 end
154end

new

Description
Creating manager
Definition
new()
Return Values
tableinstanceinstance of object
Code
16function PlayerStyle:new(customMt)
17 local self = {}
18 setmetatable(self, customMt or PlayerStyle_mt)
19
20 self.player = nil
21 self.selectedModelIndex = 1
22 self.selectedColorIndex = 0
23 self.selectedBodyIndex = 0
24 self.selectedHatIndex = 0
25 self.selectedAccessoryIndex = 0
26 self.selectedHairIndex = 0
27 self.selectedJacketIndex = 0
28 self.playerName = "player"
29 self.bodies = {}
30 self.playerHatNode = nil
31 self.playerAccessoryNode = nil
32 self.accessoryNode = nil
33 self.hatNode = nil
34 self.hatReferenceFilename = ""
35 self.accessoryReferenceFilename = ""
36 self.accessories = {}
37 self.hairs = {}
38 self.hairs.hatStyleNode = nil
39 self.hairs.styles = {}
40 self.jackets = {}
41 self.protection = {}
42 self.protection.helmetNode = nil
43 self.protection.glovesNode = nil
44 self.protection.isVisible = false
45 self.useDefault = false
46
47 return self
48end

readStream

Description
Reads from network stream
Definition
readStream(integer streamId, table connection)
Arguments
integerstreamIdid of the stream to read
tableconnectionconnection information
Code
176function PlayerStyle:readStream(streamId, connection)
177 self.selectedModelIndex = streamReadUIntN(streamId, PlayerModelManager.SEND_NUM_BITS)
178 self.selectedColorIndex = streamReadUInt8(streamId)
179 self.selectedBodyIndex = streamReadUInt8(streamId)
180 self.selectedHatIndex = streamReadUInt8(streamId)
181 self.selectedAccessoryIndex = streamReadUInt8(streamId)
182 self.selectedHairIndex = streamReadUInt8(streamId)
183 self.selectedJacketIndex = streamReadUInt8(streamId)
184 self.playerName = streamReadString(streamId)
185end

removeAccessory

Description
Definition
removeAccessory()
Code
256function PlayerStyle:removeAccessory()
257 local result = false
258
259 if self.player ~= nil and self.accessoryNode ~= nil then
260 unlink(self.accessoryNode)
261 delete(self.accessoryNode)
262 self.accessoryNode = nil
263 self.selectedAccessoryIndex = 0
264 result = true
265 end
266 return result
267end

removeHat

Description
Definition
removeHat()
Code
241function PlayerStyle:removeHat()
242 local result = false
243
244 if self.player ~= nil and self.hatNode ~= nil then
245 unlink(self.hatNode)
246 delete(self.hatNode)
247 self.hatNode = nil
248 self.selectedAccessoryIndex = 0
249 result = true
250 end
251 return result
252end

setAccessory

Description
Definition
setAccessory()
Code
363function PlayerStyle:setAccessory(accessoryIndex)
364 local result = false
365
366 if self.player ~= nil and self.playerAccessoryNode ~= nil then
367 if accessoryIndex == 0 and self.selectedAccessoryIndex > 0 then
368 result = self:removeHat()
369 self:setHair(self.selectedHairIndex)
370 elseif accessoryIndex ~= self.selectedAccessoryIndex or self.accessoryNode == nil then
371 self:removeAccessory()
372 local xmlFilename = Utils.getFilename(self.accessoryReferenceFilename)
373
374 if xmlFilename ~= nil and xmlFilename ~= "" then
375 local xmlFile = loadXMLFile("accessoryXML", xmlFilename)
376
377 if xmlFile ~= nil and xmlFile ~= 0 then
378 local accessoryKey = string.format("playerClothing.accessories.accessory(%d)#filename", accessoryIndex - 1)
379 if hasXMLProperty(xmlFile, accessoryKey) then
380 local i3dFilename = getXMLString(xmlFile, accessoryKey)
381 self.accessoryNode = loadI3DFile(i3dFilename, false, false, false)
382
383 if self.accessoryNode ~= nil then
384 link(self.playerAccessoryNode, self.accessoryNode)
385 self.selectedAccessoryIndex = accessoryIndex
386 result = true
387 end
388 end
389 delete(xmlFile)
390 end
391 end
392 end
393 else
394 g_logManager:devError("-- [PlayerStyle:setAccessory] Player not set. Missing call to PlayerStyle:loadXML().")
395 end
396 return result
397end

setBody

Description
Definition
setBody()
Code
219function PlayerStyle:setBody(bodyIndex)
220 local result = false
221 if self.player ~= nil then
222 for key, body in ipairs(self.bodies) do
223 if key == bodyIndex then
224 result = true
225 setVisibility(body.headNode, true)
226 setVisibility(body.armsNode, true)
227 self.selectedBodyIndex = key
228 else
229 setVisibility(body.headNode, false)
230 setVisibility(body.armsNode, false)
231 end
232 end
233 else
234 g_logManager:devError("-- [PlayerStyle:setBody] Player not set. Missing call to PlayerStyle:loadXML().")
235 end
236 return result
237end

setColor

Description
Definition
setColor()
Code
204function PlayerStyle:setColor(playerColorIndex)
205 if self.player ~= nil then
206 if self.player.meshThirdPerson ~= nil and self.player.meshThirdPerson ~= 0 and playerColorIndex > 0 and playerColorIndex <= table.getn(g_playerColors) then
207 local r, g, b, a = unpack(g_playerColors[playerColorIndex].value)
208
209 self.selectedColorIndex = playerColorIndex
210 setShaderParameter(self.player.meshThirdPerson, "colorScaleR", r, g, b, a, false)
211 end
212 else
213 g_logManager:devError("-- [PlayerStyle:setColor] Player not set. Missing call to PlayerStyle:loadXML().")
214 end
215end

setHair

Description
Definition
setHair()
Code
271function PlayerStyle:setHair(hairIndex, isHatHair)
272 local result = false
273
274 if self.player ~= nil then
275 if isHatHair ~= nil and isHatHair then
276 if self.selectedHairIndex > 0 then
277 local currentHairStyleNode = self.hairs.styles[self.selectedHairIndex].node
278 setVisibility(currentHairStyleNode, false)
279 setVisibility(self.hairs.hatStyleNode, true)
280 end
281 result = true
282 else
283 for key, hairStyle in ipairs(self.hairs.styles) do
284 if key == hairIndex then
285 if self.selectedHatIndex == 0 then
286 setVisibility(hairStyle.node, true)
287 end
288 self.selectedHairIndex = key
289 result = true
290 else
291 setVisibility(hairStyle.node, false)
292 end
293 end
294 end
295 else
296 g_logManager:devError("-- [PlayerStyle:setHair] Player not set. Missing call to PlayerStyle:loadXML().")
297 end
298 return result
299end

setHat

Description
Definition
setHat()
Code
303function PlayerStyle:setHat(hatIndex)
304 local result = false
305
306 if self.player ~= nil and self.playerHatNode ~= nil then
307 if hatIndex == 0 and self.selectedHatIndex > 0 then
308 result = self:removeHat()
309 self:setHair(self.selectedHairIndex)
310 elseif hatIndex ~= self.selectedHatIndex or self.hatNode == nil then
311 self:removeHat()
312 local xmlFilename = Utils.getFilename(self.hatReferenceFilename)
313
314 if xmlFilename ~= nil and xmlFilename ~= "" then
315 local xmlFile = loadXMLFile("hatXML", xmlFilename)
316
317 if xmlFile ~= nil and xmlFile ~= 0 then
318 local hatKey = string.format("playerClothing.hats.hat(%d)#filename", hatIndex - 1)
319 if hasXMLProperty(xmlFile, hatKey) then
320 local i3dFilename = getXMLString(xmlFile, hatKey)
321 self.hatNode = loadI3DFile(i3dFilename, false, false, false)
322
323 if self.hatNode ~= nil then
324 self:setHair(0, true)
325 link(self.playerHatNode, self.hatNode)
326 self.selectedHatIndex = hatIndex
327 result = true
328 end
329 end
330 delete(xmlFile)
331 end
332 end
333 end
334 else
335 g_logManager:devError("-- [PlayerStyle:setHat] Player not set. Missing call to PlayerStyle:loadXML().")
336 end
337 return result
338end

setJacket

Description
Definition
setJacket()
Code
342function PlayerStyle:setJacket(jacketIndex)
343 local result = false
344
345 if self.player ~= nil then
346 for key, jacket in ipairs(self.jackets) do
347 if key == jacketIndex then
348 result = true
349 setVisibility(jacket.node, true)
350 self.selectedJacketIndex = key
351 else
352 setVisibility(jacket.node, false)
353 end
354 end
355 else
356 g_logManager:devError("-- [PlayerStyle:setJacket] Player not set. Missing call to PlayerStyle:loadXML().")
357 end
358 return result
359end

setProtectiveUV

Description
Definition
setProtectiveUV()
Code
419function PlayerStyle:setProtectiveUV(uvs)
420 if self.protection.helmetNode ~= nil then
421 setShaderParameter(self.protection.helmetNode, "offsetUV", uvs[1], uvs[2], 0, 0, false)
422 end
423 if self.protection.glovesNode ~= nil then
424 setShaderParameter(self.protection.glovesNode, "offsetUV", uvs[1], uvs[2], 0, 0, false)
425 end
426end

setProtectiveVisibility

Description
Definition
setProtectiveVisibility()
Code
401function PlayerStyle:setProtectiveVisibility(state)
402 self.protection.isVisible = state
403
404 if self.player ~= nil then
405 if self.protection.helmetNode ~= nil then
406 setVisibility(self.protection.helmetNode, state)
407 end
408 if self.protection.glovesNode ~= nil then
409 setVisibility(self.protection.glovesNode, state)
410 end
411 self:updateHeadWearVisibility()
412 else
413 g_logManager:devError("-- [PlayerStyle:setProtectiveVisibility] Player not set. Missing call to PlayerStyle:loadXML().")
414 end
415end

setVisibility

Description
Definition
setVisibility()
Code
454function PlayerStyle:setVisibility(state)
455 if self.player ~= nil then
456 if self.selectedBodyIndex > 0 then
457 local body = self.bodies[self.selectedBodyIndex]
458 setVisibility(body.headNode, state)
459 setVisibility(body.armsNode, state)
460 end
461 if self.hatNode ~= nil then
462 setVisibility(self.hatNode, state)
463 if self.selectedHairIndex > 0 then
464 setVisibility(self.hairs.hatStyleNode, state)
465 end
466 else
467 if self.selectedHairIndex > 0 then
468 local currentHairStyleNode = self.hairs.styles[self.selectedHairIndex].node
469 setVisibility(currentHairStyleNode, state)
470 end
471 end
472 if self.accessoryNode ~= nil then
473 setVisibility(self.accessoryNode, state)
474 end
475 if self.selectedJacketIndex > 0 then
476 local jacket = self.jackets[self.selectedJacketIndex]
477 setVisibility(jacket.node, state)
478 end
479 else
480 g_logManager:devError("-- [PlayerStyle:setVisibility] Player not set. Missing call to PlayerStyle:loadXML().")
481 end
482end

unlinkProtectiveWear

Description
Definition
unlinkProtectiveWear()
Code
166function PlayerStyle:unlinkProtectiveWear()
167 if self.protection.glovesNode ~= nil then
168 unlink(self.protection.glovesNode)
169 end
170end

updateHeadWearVisibility

Description
Definition
updateHeadWearVisibility()
Code
436function PlayerStyle:updateHeadWearVisibility()
437 local isThirdPerson = getVisibility(self.player.meshThirdPerson)
438
439 if isThirdPerson then
440 local protectiveGearVisibility = self:getProtectiveVisibility()
441
442 if self.selectedHatIndex > 0 then
443 setVisibility(self.hatNode, not protectiveGearVisibility and isThirdPerson)
444 elseif self.selectedHairIndex > 0 then
445 local currentHairStyleNode = self.hairs.styles[self.selectedHairIndex].node
446 setVisibility(currentHairStyleNode, not protectiveGearVisibility)
447 setVisibility(self.hairs.hatStyleNode, protectiveGearVisibility)
448 end
449 end
450end

writeStream

Description
Writes in network stream
Definition
writeStream(integer streamId, table connection)
Arguments
integerstreamIdid of the stream to read
tableconnectionconnection information
Code
191function PlayerStyle:writeStream(streamId, connection)
192 streamWriteUIntN(streamId, self.selectedModelIndex, PlayerModelManager.SEND_NUM_BITS)
193 streamWriteUInt8(streamId, self.selectedColorIndex)
194 streamWriteUInt8(streamId, self.selectedBodyIndex)
195 streamWriteUInt8(streamId, self.selectedHatIndex)
196 streamWriteUInt8(streamId, self.selectedAccessoryIndex)
197 streamWriteUInt8(streamId, self.selectedHairIndex)
198 streamWriteUInt8(streamId, self.selectedJacketIndex)
199 streamWriteString(streamId, self.playerName)
200end