LUADOC - Farming Simulator 22

PalletSpawner

Description
This class spawns pallets
Functions

delete

Description
Definition
delete()
Code
74function PalletSpawner:delete()
75end

getAllPallets

Description
Get all pallets for given fillTypeId within the spawn places
Definition
getAllPallets(int fillTypeId, function callbackFunc, table callbackTarget)
Arguments
intfillTypeIdfillTypeId to be supported by the pallet
functioncallbackFunccallback function which will receive indexed list of pallet vehicle objects
tablecallbackTargettarget for the callback function
Code
171function PalletSpawner:getAllPallets(fillTypeId, callbackFunc, callbackTarget)
172 self.getAllPalletsFoundPallets = {}
173 self.getAllPalletsFilltype = fillTypeId
174
175 for i=1, #self.spawnPlaces do
176 local place = self.spawnPlaces[i]
177 local x = place.startX + place.width/2 * place.dirX
178 local y = place.startY + place.width/2 * place.dirY
179 local z = place.startZ + place.width/2 * place.dirZ
180 overlapBox(x,y,z, place.rotX, place.rotY, place.rotZ, place.width/2, 1, 1, "onFindPallet", self, CollisionMask.VEHICLE, true, false, true)
181 end
182
183 callbackFunc(callbackTarget, table.toList(self.getAllPalletsFoundPallets))
184end

getOrSpawnPallet

Description
Get pallet for given fillTypeId, searches spawnPlaces for existing non-full pallets, spawns a new one otherwise
Definition
getOrSpawnPallet(int fillTypeId, function callback, table callbackTarget)
Arguments
intfillTypeIdfillTypeId to be supported by the pallet
functioncallbackcallback function which will receive pallet vehicle
tablecallbackTargettarget for the callback function
Code
147function PalletSpawner:getOrSpawnPallet(farmId, fillTypeId, callback, callbackTarget)
148 self.foundExistingPallet = nil
149 self.getOrSpawnPalletFilltype = fillTypeId
150
151 for i=1, #self.spawnPlaces do
152 local place = self.spawnPlaces[i]
153 local x = place.startX + place.width/2 * place.dirX
154 local y = place.startY + place.width/2 * place.dirY
155 local z = place.startZ + place.width/2 * place.dirZ
156 overlapBox(x,y,z, place.rotX, place.rotY, place.rotZ, place.width/2, 1, 1, "onFindExistingPallet", self, CollisionMask.VEHICLE, true, false, true)
157 end
158
159 if self.foundExistingPallet ~= nil then
160 callback(callbackTarget, self.foundExistingPallet, PalletSpawner.PALLET_ALREADY_PRESENT, fillTypeId)
161 else
162 self:spawnPallet(farmId, fillTypeId, callback, callbackTarget)
163 end
164end

getSupportedFillTypes

Description
Definition
getSupportedFillTypes()
Code
116function PalletSpawner:getSupportedFillTypes()
117 return self.fillTypeIdToPallet
118end

load

Description
Loads data from xml
Definition
load(table components, table xmlFile, string key, string customEnv, table i3dMappings)
Arguments
tablecomponentscomponents
tablexmlFilexml file object
stringkeyxml key
stringcustomEnvcustom environment
tablei3dMappingsi3dMappings
Return Values
booleansuccesssuccess
Code
40function PalletSpawner:load(components, xmlFile, key, customEnv, i3dMappings)
41 self.rootNode = xmlFile:getValue(key.."#node", components[1].node, components, i3dMappings)
42
43 self.spawnPlaces = {}
44 xmlFile:iterate(key..".spawnPlaces.spawnPlace", function(index, spawnPlaceKey)
45 local spawnPlace = PlacementUtil.loadPlaceFromXML(xmlFile, spawnPlaceKey, components, i3dMappings)
46 table.insert(self.spawnPlaces, spawnPlace)
47 end)
48 if #self.spawnPlaces == 0 then
49 Logging.xmlError(xmlFile, "No spawn place(s) defined for pallet spawner %s%s", key, ".spawnPlaces")
50 return false
51 end
52
53 self.pallets = {}
54 self.fillTypeIdToPallet = {}
55
56 -- load default global pallets defined at fillTypes registration
57 for fillTypeId, fillType in pairs(g_fillTypeManager.indexToFillType) do
58 if fillType.palletFilename then
59 self:loadPalletFromFilename(fillType.palletFilename, fillTypeId)
60 end
61 end
62
63 -- load pallets provided in xml file, will override fillType pallets
64 xmlFile:iterate(key .. ".pallets.pallet", function(index, palletKey)
65 local palletFilename = Utils.getFilename(xmlFile:getValue(palletKey .. "#filename"), self.baseDirectory)
66 self:loadPalletFromFilename(palletFilename)
67 end)
68
69 return true
70end

loadPalletFromFilename

Description
Definition
loadPalletFromFilename()
Code
79function PalletSpawner:loadPalletFromFilename(palletFilename, limitFillTypeId)
80 if palletFilename ~= nil then
81 local pallet = {}
82 pallet.filename = palletFilename
83 pallet.size = StoreItemUtil.getSizeValues(palletFilename, "vehicle", 0, {})
84
85 local palletXmlFile = XMLFile.load("palletXmlFilename", palletFilename, Vehicle.xmlSchema)
86 local fillTypeNamesAndCategories = FillUnit.getFillTypeNamesFromXML(palletXmlFile)
87 pallet.capacity = FillUnit.getCapacityFromXml(palletXmlFile)
88 palletXmlFile:delete()
89
90 local palletFillTypes = g_fillTypeManager:getFillTypesByCategoryNames(fillTypeNamesAndCategories.fillTypeCategoryNames)
91
92 local fillTypes = g_fillTypeManager:getFillTypesByNames(fillTypeNamesAndCategories.fillTypeNames)
93 for i=1, #fillTypes do
94 table.insert(palletFillTypes, fillTypes[i])
95 end
96
97 local hadMatchingFillType = false
98 for i=1, #palletFillTypes do
99 local fillTypeId = palletFillTypes[i]
100 if limitFillTypeId == nil or limitFillTypeId == fillTypeId then
101 self.fillTypeIdToPallet[fillTypeId] = pallet
102 hadMatchingFillType = true
103 end
104 end
105
106 if hadMatchingFillType then
107 table.insert(self.pallets, pallet)
108 return pallet
109 end
110 end
111 return nil
112end

new

Description
Definition
new()
Code
22function PalletSpawner.new(baseDirectory, customMt)
23 local self = setmetatable({}, customMt or PalletSpawner_mt)
24
25 self.baseDirectory = baseDirectory
26 self.spawnQueue = {}
27 self.currentObjectToSpawn = nil
28
29 return self
30end

onFindExistingPallet

Description
Definition
onFindExistingPallet()
Code
235function PalletSpawner:onFindExistingPallet(node)
236 local object = g_currentMission.nodeToObject[node]
237 if object ~= nil and object.isa ~= nil and object:isa(Vehicle) and object.isPallet then
238 if object:getFillUnitSupportsFillType(1, self.getOrSpawnPalletFilltype) and object:getFillUnitFreeCapacity(1, self.getOrSpawnPalletFilltype) > 0 then
239 self.foundExistingPallet = object
240 return false
241 end
242 end
243end

onFindPallet

Description
Definition
onFindPallet()
Code
247function PalletSpawner:onFindPallet(node)
248 local object = g_currentMission.nodeToObject[node]
249 if object ~= nil and object.isa ~= nil and object:isa(Vehicle) and object.isPallet then
250 if object:getFillUnitFillType(1) == self.getAllPalletsFilltype then
251 self.getAllPalletsFoundPallets[object] = true
252 end
253 end
254end

onFinishLoadingPallet

Description
Definition
onFinishLoadingPallet()
Code
225function PalletSpawner:onFinishLoadingPallet(vehicle, vehicleLoadState)
226 local objectToSpawn = self.currentObjectToSpawn
227 local statusCode = vehicleLoadState == VehicleLoadingUtil.VEHICLE_LOAD_OK and PalletSpawner.RESULT_SUCCESS or PalletSpawner.RESULT_ERROR_LOADING_PALLET
228 objectToSpawn.callback(objectToSpawn.callbackTarget, vehicle, statusCode, objectToSpawn.fillType)
229 self.currentObjectToSpawn = nil
230 table.remove(self.spawnQueue, 1)
231end

onSpawnSearchFinished

Description
Definition
onSpawnSearchFinished()
Code
206function PalletSpawner:onSpawnSearchFinished(location)
207 local objectToSpawn = self.currentObjectToSpawn
208 if location ~= nil then
209 local terrainHeight = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, location.x, 0, location.z)
210 -- ensure y is at least terrain height
211 -- using only terrain height + offset for pallets might be problematic if spawn area is on top of a mesh with collision
212 -- add saftey y offset to avoid pallets clipping into the ground/collision
213 location.y = math.max(terrainHeight, location.y) + 0.2
214 VehicleLoadingUtil.loadVehicle(objectToSpawn.pallet.filename, location, true, 0, Vehicle.PROPERTY_STATE_OWNED, objectToSpawn.farmId, nil, nil, self.onFinishLoadingPallet, self)
215 else
216 -- unable to find space
217 objectToSpawn.callback(objectToSpawn.callbackTarget, nil, PalletSpawner.RESULT_NO_SPACE)
218 self.currentObjectToSpawn = nil
219 table.remove(self.spawnQueue, 1)
220 end
221end

registerXMLPaths

Description
Definition
registerXMLPaths()
Code
258function PalletSpawner.registerXMLPaths(schema, basePath)
259 schema:register(XMLValueType.NODE_INDEX, basePath .. "#node", "root node")
260
261 PlacementUtil.registerXMLPaths(schema, basePath)
262
263 schema:register(XMLValueType.STRING, basePath .. ".pallets.pallet(?)#filename", "Path to pallet xml file")
264end

spawnPallet

Description
Spawn and get pallet for given fillTypeId
Definition
spawnPallet(int fillTypeId, function callback, table callbackTarget)
Arguments
intfillTypeIdfillTypeId to be supported by the pallet
functioncallbackcallback function which will receive pallet vehicle
tablecallbackTargettarget for the callback function
Code
125function PalletSpawner:spawnPallet(farmId, fillTypeId, callback, callbackTarget)
126
127 if not g_currentMission.slotSystem:getCanAddLimitedObjects(SlotSystem.LIMITED_OBJECT_PALLET, 1) then
128 callback(callbackTarget, nil, PalletSpawner.PALLET_LIMITED_REACHED)
129 return
130 end
131
132 local pallet = self.fillTypeIdToPallet[fillTypeId]
133 if pallet ~= nil then
134 table.insert(self.spawnQueue, {pallet=pallet, fillType=fillTypeId, farmId=farmId, callback=callback, callbackTarget=callbackTarget})
135 g_currentMission:addUpdateable(self)
136 else
137 Logging.devError("PalletSpawner: no pallet for fillTypeId", fillTypeId)
138 callback(callbackTarget, nil, PalletSpawner.NO_PALLET_FOR_FILLTYPE, fillTypeId)
139 end
140end

update

Description
Definition
update()
Code
188function PalletSpawner:update(dt)
189 if #self.spawnQueue > 0 then
190 -- DebugUtil.renderTable(0.1, 0.88, 0.015, {{name="palletSpawnerQueue", value="#="..#self.spawnQueue}})
191
192 if self.currentObjectToSpawn == nil then
193 self.currentObjectToSpawn = self.spawnQueue[1]
194
195 -- DebugUtil.renderTable(0.1, 0.85, 0.015, {{name="currentPallet", value=self.currentObjectToSpawn.pallet.filename}})
196
197 g_currentMission.placementManager:getPlaceAsync(self.spawnPlaces, self.currentObjectToSpawn.pallet.size, self.onSpawnSearchFinished, self)
198 end
199 else
200 g_currentMission:removeUpdateable(self) -- dont update unless there are pallets to spawn
201 end
202end