LUADOC - Farming Simulator 19

Script v1.7.1.0

Engine v1.7.1.0

Foundation Reference

LivestockTrailer

Description
Specialization for livestrock trailers with animals as cargo
Functions

addAnimal

Description
Definition
addAnimal()
Code
276function LivestockTrailer:addAnimal(animal)
277 local spec = self.spec_livestockTrailer
278 local success, _ = ListUtil.addElementToList(spec.loadedAnimals, animal)
279 if success then
280 spec.currentAnimalType = animal:getSubType().type
281
282 spec.loadedAnimalsIds[NetworkUtil.getObjectId(animal)] = true
283 spec.animalToSlots[animal] = {}
284
285 local place = spec.animalTypeToPlaces[animal.subType.type]
286 for _, slot in ipairs(place.slots) do
287 if slot.loadedMesh == nil then
288 local animalMeshRoot = g_i3DManager:loadSharedI3DFile(animal.subType.dummy.filename)
289 local animalRoot = getChildAt(animalMeshRoot, 0)
290 local animalMesh = I3DUtil.indexToObject(animalMeshRoot, animal.subType.dummy.meshNodeStr)
291 local animalHairMesh = I3DUtil.indexToObject(animalMeshRoot, animal.subType.dummy.hairNodeStr)
292
293 link(slot.linkNode, animalRoot)
294 delete(animalMeshRoot)
295
296 -- set dirt
297 local x, y, z, w = getShaderParameter(animalMesh, "RDT")
298 local dirt = animal:getDirtScale()
299 setShaderParameter(animalMesh, "RDT", x, dirt, z, w, false)
300
301 -- set texture
302 local x, y, _, _ = getShaderParameter(animalMesh, "atlasInvSizeAndOffsetUV")
303
304 local numTilesU = 1 / x
305 local numTilesV = 1 / y
306 local subType = animal:getSubType()
307 local tileUIndex = subType.texture.tileUIndex
308 local tileVIndex = subType.texture.tileVIndex
309 local tileU = tileUIndex / numTilesU
310 local tileV = tileVIndex / numTilesV
311
312 setShaderParameter(animalMesh, "atlasInvSizeAndOffsetUV", x, y, tileU, tileV, false)
313
314 if animalHairMesh ~= nil then
315 local x, y, _, _ = getShaderParameter(animalHairMesh, "atlasInvSizeAndOffsetUV")
316 setShaderParameter(animalHairMesh, "atlasInvSizeAndOffsetUV", x, y, tileU, tileV, false)
317 end
318
319 slot.loadedMesh = animalRoot
320
321 table.insert(spec.animalToSlots[animal], slot)
322
323 place.numUsed = place.numUsed + 1
324
325 break
326 end
327 end
328
329 self:setMassDirty()
330
331 if self.isServer then
332 self:raiseDirtyFlags(spec.dirtyFlag)
333 end
334 end
335end

addAnimals

Description
Definition
addAnimals()
Code
268function LivestockTrailer:addAnimals(animals)
269 for _, animal in ipairs(animals) do
270 self:addAnimal(animal)
271 end
272end

getAdditionalComponentMass

Description
Definition
getAdditionalComponentMass()
Code
404function LivestockTrailer:getAdditionalComponentMass(superFunc, component)
405 local additionalMass = superFunc(self, component)
406 local spec = self.spec_livestockTrailer
407
408 for _, animal in ipairs(spec.loadedAnimals) do
409 local fillTypeIndex = animal:getFillTypeIndex()
410 local desc = g_fillTypeManager:getFillTypeByIndex(fillTypeIndex)
411 additionalMass = additionalMass + desc.massPerLiter
412 end
413
414 return additionalMass
415end

getAnimalPlaces

Description
Definition
getAnimalPlaces()
Code
392function LivestockTrailer:getAnimalPlaces()
393 return self.spec_livestockTrailer.animalPlaces
394end

getAnimals

Description
Definition
getAnimals()
Code
386function LivestockTrailer:getAnimals()
387 return self.spec_livestockTrailer.loadedAnimals
388end

getCurrentAnimalType

Description
Definition
getCurrentAnimalType()
Code
380function LivestockTrailer:getCurrentAnimalType()
381 return self.spec_livestockTrailer.currentAnimalType
382end

getSpecValueNumberAnimals

Description
Definition
getSpecValueNumberAnimals()
Code
467function LivestockTrailer.getSpecValueNumberAnimals(storeItem, realItem, specName)
468 if storeItem.specs[specName] == nil then
469 return nil
470 end
471 return string.format("%d %s", storeItem.specs[specName], g_i18n:getText("unit_pieces"))
472end

getSpecValueNumberAnimalsCow

Description
Definition
getSpecValueNumberAnimalsCow()
Code
476function LivestockTrailer.getSpecValueNumberAnimalsCow(storeItem, realItem)
477 return LivestockTrailer.getSpecValueNumberAnimals(storeItem, realItem, "numAnimalsCow")
478end

getSpecValueNumberAnimalsHorse

Description
Definition
getSpecValueNumberAnimalsHorse()
Code
494function LivestockTrailer.getSpecValueNumberAnimalsHorse(storeItem, realItem)
495 return LivestockTrailer.getSpecValueNumberAnimals(storeItem, realItem, "numAnimalsHorse")
496end

getSpecValueNumberAnimalsPig

Description
Definition
getSpecValueNumberAnimalsPig()
Code
482function LivestockTrailer.getSpecValueNumberAnimalsPig(storeItem, realItem)
483 return LivestockTrailer.getSpecValueNumberAnimals(storeItem, realItem, "numAnimalsPig")
484end

getSpecValueNumberAnimalsSheep

Description
Definition
getSpecValueNumberAnimalsSheep()
Code
488function LivestockTrailer.getSpecValueNumberAnimalsSheep(storeItem, realItem)
489 return LivestockTrailer.getSpecValueNumberAnimals(storeItem, realItem, "numAnimalsSheep")
490end

getSupportsAnimalType

Description
Definition
getSupportsAnimalType()
Code
262function LivestockTrailer:getSupportsAnimalType(animalType)
263 return self.spec_livestockTrailer.animalTypeToPlaces[animalType]
264end

initSpecialization

Description
Definition
initSpecialization()
Code
15function LivestockTrailer.initSpecialization()
16 g_storeManager:addSpecType("numAnimalsCow", "shopListAttributeIconCow", LivestockTrailer.loadSpecValueNumberAnimalsCow, LivestockTrailer.getSpecValueNumberAnimalsCow)
17 g_storeManager:addSpecType("numAnimalsPig", "shopListAttributeIconPig", LivestockTrailer.loadSpecValueNumberAnimalsPig, LivestockTrailer.getSpecValueNumberAnimalsPig)
18 g_storeManager:addSpecType("numAnimalsSheep", "shopListAttributeIconSheep", LivestockTrailer.loadSpecValueNumberAnimalsSheep, LivestockTrailer.getSpecValueNumberAnimalsSheep)
19 g_storeManager:addSpecType("numAnimalsHorse", "shopListAttributeIconHorse", LivestockTrailer.loadSpecValueNumberAnimalsHorse, LivestockTrailer.getSpecValueNumberAnimalsHorse)
20end

loadSpecValueNumberAnimalsCow

Description
Definition
loadSpecValueNumberAnimalsCow()
Code
443function LivestockTrailer.loadSpecValueNumberAnimalsCow(xmlFile, customEnvironment)
444 return LivestockTrailer.loadSpecValueNumberAnimals(xmlFile, customEnvironment, "cow")
445end

loadSpecValueNumberAnimalsHorse

Description
Definition
loadSpecValueNumberAnimalsHorse()
Code
461function LivestockTrailer.loadSpecValueNumberAnimalsHorse(xmlFile, customEnvironment)
462 return LivestockTrailer.loadSpecValueNumberAnimals(xmlFile, customEnvironment, "horse")
463end

loadSpecValueNumberAnimalsPig

Description
Definition
loadSpecValueNumberAnimalsPig()
Code
449function LivestockTrailer.loadSpecValueNumberAnimalsPig(xmlFile, customEnvironment)
450 return LivestockTrailer.loadSpecValueNumberAnimals(xmlFile, customEnvironment, "pig")
451end

loadSpecValueNumberAnimalsSheep

Description
Definition
loadSpecValueNumberAnimalsSheep()
Code
455function LivestockTrailer.loadSpecValueNumberAnimalsSheep(xmlFile, customEnvironment)
456 return LivestockTrailer.loadSpecValueNumberAnimals(xmlFile, customEnvironment, "sheep")
457end

onDelete

Description
Definition
onDelete()
Code
143function LivestockTrailer:onDelete()
144 local spec = self.spec_livestockTrailer
145 if spec.loadingTrigger ~= nil then
146 spec.loadingTrigger:setLoadingTrailer(nil)
147 end
148end

onLoad

Description
Called on loading
Definition
onLoad(table savegame)
Arguments
tablesavegamesavegame
Code
63function LivestockTrailer:onLoad(savegame)
64 local spec = self.spec_livestockTrailer
65
66 spec.animalPlaces = {}
67 spec.animalTypeToPlaces = {}
68 local i = 0
69 while true do
70 local key = string.format("vehicle.livestockTrailer.animal(%d)", i)
71 if not hasXMLProperty(self.xmlFile, key) then
72 break
73 end
74
75 local place = {}
76 place.numUsed = 0
77
78 local animalTypeStr = getXMLString(self.xmlFile, key .. "#type")
79 local animalType = g_animalManager:getAnimalsByType(animalTypeStr)
80 if animalType == nil then
81 g_logManager:xmlWarning(self.configFileName, "Animal type '%s' could not be found!", animalTypeStr)
82 break
83 end
84 place.animalType = animalType.type
85
86 XMLUtil.checkDeprecatedXMLElements(self.xmlFile, self.configFileName, key.."#index", key.."#node") --FS17 to FS19
87
88 place.slots = {}
89 local parent = I3DUtil.indexToObject(self.components, getXMLString(self.xmlFile, key .. "#node"), self.i3dMappings)
90 local numSlots = math.abs(getXMLInt(self.xmlFile, key.."#numSlots") or 0)
91 if numSlots > getNumOfChildren(parent) then
92 g_logManager:xmlWarning(self.configFileName, "numSlots is greater than available children for '%s'", key)
93 numSlots = getNumOfChildren(parent)
94 end
95 for j=0, numSlots-1 do
96 local slotNode = getChildAt(parent, j)
97 table.insert(place.slots, {linkNode=slotNode, loadedMesh=nil, place=place})
98 end
99
100 table.insert(spec.animalPlaces, place)
101 spec.animalTypeToPlaces[place.animalType] = place
102
103 i = i + 1
104 end
105
106 spec.loadedAnimals = {}
107 spec.loadedAnimalsIds = {}
108 spec.animalToSlots = {}
109 spec.animalsToLoad = nil
110 spec.loadingTrigger = nil
111
112 spec.currentAnimalType = nil
113
114 spec.dirtyFlag = self:getNextDirtyFlag()
115end

onLoadFinished

Description
Definition
onLoadFinished()
Code
119function LivestockTrailer:onLoadFinished(savegame)
120 local spec = self.spec_livestockTrailer
121
122 if savegame ~= nil and not savegame.resetVehicles then
123 local key = string.format("%s.livestockTrailer", savegame.key)
124 local i = 0
125 local xmlFile = savegame.xmlFile
126 while true do
127 local slotKey = string.format("%s.animal(%d)", key, i)
128 if not hasXMLProperty(xmlFile, slotKey) then
129 break
130 end
131
132 local animal = Animal.createFromXMLFile(xmlFile, slotKey, self.isServer, self.isClient, nil)
133 animal:register()
134 self:addAnimal(animal)
135
136 i = i + 1
137 end
138 end
139end

onReadStream

Description
Definition
onReadStream()
Code
165function LivestockTrailer:onReadStream(streamId, connection)
166 local spec = self.spec_livestockTrailer
167
168 spec.animalsToLoad = {}
169 local numAnimals = streamReadUInt8(streamId)
170 for i=1, numAnimals do
171 local animalId = NetworkUtil.readNodeObjectId(streamId)
172 table.insert(spec.animalsToLoad, animalId)
173 end
174end

onReadUpdateStream

Description
Definition
onReadUpdateStream()
Code
189function LivestockTrailer:onReadUpdateStream(streamId, timestamp, connection)
190
191 if connection:getIsServer() then
192 local spec = self.spec_livestockTrailer
193
194 if streamReadBool(streamId) then
195 local numAnimals = streamReadUInt8(streamId)
196 local currentAnimalIds = {}
197
198 if spec.animalsToLoad == nil then
199 spec.animalsToLoad = {}
200 end
201
202 for i=1, numAnimals do
203 local animalId = NetworkUtil.readNodeObjectId(streamId)
204
205 -- add new animals
206 if spec.loadedAnimalsIds[animalId] == nil then
207 table.insert(spec.animalsToLoad, animalId)
208 end
209 currentAnimalIds[animalId] = true
210 end
211
212 -- remove not loaded animals again
213 for i=#spec.loadedAnimals, 1, -1 do
214 local animal = spec.loadedAnimals[i]
215 local animalId = NetworkUtil.getObjectId(animal)
216
217 if animalId == nil or currentAnimalIds[animalId] == nil then
218 self:removeAnimal(animal)
219 end
220 end
221 end
222 end
223end

onUpdate

Description
Definition
onUpdate()
Code
242function LivestockTrailer:onUpdate(dt, isActiveForInput, isActiveForInputIgnoreSelection, isSelected)
243 local spec = self.spec_livestockTrailer
244 if spec.animalsToLoad ~= nil then
245 for i=#spec.animalsToLoad, 1, -1 do
246 local animalId = spec.animalsToLoad[i]
247 local animal = NetworkUtil.getObject(animalId)
248 if animal ~= nil then
249 self:addAnimal(animal)
250 table.remove(spec.animalsToLoad, i)
251 end
252 end
253
254 if #spec.animalsToLoad == 0 then
255 spec.animalsToLoad = nil
256 end
257 end
258end

onWriteStream

Description
Definition
onWriteStream()
Code
178function LivestockTrailer:onWriteStream(streamId, connection)
179 local spec = self.spec_livestockTrailer
180
181 streamWriteUInt8(streamId, #spec.loadedAnimals)
182 for _, animal in ipairs(spec.loadedAnimals) do
183 NetworkUtil.writeNodeObject(streamId, animal)
184 end
185end

onWriteUpdateStream

Description
Definition
onWriteUpdateStream()
Code
227function LivestockTrailer:onWriteUpdateStream(streamId, connection, dirtyMask)
228 if not connection:getIsServer() then
229 local spec = self.spec_livestockTrailer
230
231 if streamWriteBool(streamId, bitAND(dirtyMask, spec.dirtyFlag) ~= 0) then
232 streamWriteUInt8(streamId, #spec.loadedAnimals)
233 for _, animal in ipairs(spec.loadedAnimals) do
234 NetworkUtil.writeNodeObject(streamId, animal)
235 end
236 end
237 end
238end

prerequisitesPresent

Description
Definition
prerequisitesPresent()
Code
24function LivestockTrailer.prerequisitesPresent(specializations)
25 return true
26end

registerEventListeners

Description
Definition
registerEventListeners()
Code
50function LivestockTrailer.registerEventListeners(vehicleType)
51 SpecializationUtil.registerEventListener(vehicleType, "onLoad", LivestockTrailer)
52 SpecializationUtil.registerEventListener(vehicleType, "onLoadFinished", LivestockTrailer)
53 SpecializationUtil.registerEventListener(vehicleType, "onReadStream", LivestockTrailer)
54 SpecializationUtil.registerEventListener(vehicleType, "onWriteStream", LivestockTrailer)
55 SpecializationUtil.registerEventListener(vehicleType, "onReadUpdateStream", LivestockTrailer)
56 SpecializationUtil.registerEventListener(vehicleType, "onWriteUpdateStream", LivestockTrailer)
57 SpecializationUtil.registerEventListener(vehicleType, "onUpdate", LivestockTrailer)
58end

registerFunctions

Description
Definition
registerFunctions()
Code
30function LivestockTrailer.registerFunctions(vehicleType)
31 SpecializationUtil.registerFunction(vehicleType, "addAnimal", LivestockTrailer.addAnimal)
32 SpecializationUtil.registerFunction(vehicleType, "addAnimals", LivestockTrailer.addAnimals)
33 SpecializationUtil.registerFunction(vehicleType, "removeAnimal", LivestockTrailer.removeAnimal)
34 SpecializationUtil.registerFunction(vehicleType, "removeAnimals", LivestockTrailer.removeAnimals)
35 SpecializationUtil.registerFunction(vehicleType, "getCurrentAnimalType", LivestockTrailer.getCurrentAnimalType)
36 SpecializationUtil.registerFunction(vehicleType, "getSupportsAnimalType", LivestockTrailer.getSupportsAnimalType)
37 SpecializationUtil.registerFunction(vehicleType, "getAnimals", LivestockTrailer.getAnimals)
38 SpecializationUtil.registerFunction(vehicleType, "getAnimalPlaces", LivestockTrailer.getAnimalPlaces)
39 SpecializationUtil.registerFunction(vehicleType, "setLoadingTrigger", LivestockTrailer.setLoadingTrigger)
40end

registerOverwrittenFunctions

Description
Definition
registerOverwrittenFunctions()
Code
44function LivestockTrailer.registerOverwrittenFunctions(vehicleType)
45 SpecializationUtil.registerOverwrittenFunction(vehicleType, "getAdditionalComponentMass", LivestockTrailer.getAdditionalComponentMass)
46end

removeAnimal

Description
Definition
removeAnimal()
Code
347function LivestockTrailer:removeAnimal(animal)
348 local spec = self.spec_livestockTrailer
349 local success = ListUtil.removeElementFromList(spec.loadedAnimals, animal)
350 if success then
351 local animalId = NetworkUtil.getObjectId(animal)
352 if animalId ~= nil then
353 spec.loadedAnimalsIds[animalId] = nil
354 end
355
356 for _, slot in ipairs(spec.animalToSlots[animal]) do
357 delete(slot.loadedMesh)
358 slot.loadedMesh = nil
359 g_i3DManager:releaseSharedI3DFile(animal.subType.dummy.filename, nil, true)
360
361 slot.place.numUsed = slot.place.numUsed - 1
362 end
363
364 spec.animalToSlots[animal] = nil
365
366 self:setMassDirty()
367
368 if self.isServer then
369 self:raiseDirtyFlags(spec.dirtyFlag)
370 end
371
372 if #spec.loadedAnimals == 0 then
373 spec.currentAnimalType = nil
374 end
375 end
376end

removeAnimals

Description
Definition
removeAnimals()
Code
339function LivestockTrailer:removeAnimals(animals)
340 for _, animal in ipairs(animals) do
341 self:removeAnimal(animal)
342 end
343end

saveToXMLFile

Description
Definition
saveToXMLFile()
Code
152function LivestockTrailer:saveToXMLFile(xmlFile, key, usedModNames)
153 local spec = self.spec_livestockTrailer
154
155 local num = 0
156 for _, animal in ipairs(spec.loadedAnimals) do
157 local animalKey = string.format("%s.animal(%d)", key, num)
158 animal:saveToXMLFile(xmlFile, animalKey, usedModNames)
159 num = num + 1
160 end
161end

setLoadingTrigger

Description
Definition
setLoadingTrigger()
Code
398function LivestockTrailer:setLoadingTrigger(trigger)
399 self.spec_livestockTrailer.loadingTrigger = trigger
400end