Script v1_7_1_0
- AI
- Animals
- Collections
- Contracts
- Debug
- Economy
- Elements
- EnvironmentalScore
- Errors
- Events
- GUI
- Handtools
- Hud
- I3d
- Input
- Jobs
- Maps
- Materials
- Misc
- Objects
- Parameters
- Placeables
- Placement
- Player
- Shop
- Sounds
- Specialization
- Specializations
- StateMachine
- Statistics
- Tasks
- Triggers
- Utils
- Vehicles
Engine v1_7_1_0
- AI
- Animation
- Camera
- Entity
- Fillplanes
- general
- General
- I3D
- Input
- Lighting
- Math
- Network
- Node
- NoteNode
- Overlays
- Particle System
- Physics
- Rendering
- Scenegraph
- Shape
- Sound
- Spline
- String
- Terrain Detail
- Text Rendering
- Tire Track
- VoiceChat
- XML
Foundation Reference
PalletSpawner
DescriptionThis class spawns palletsFunctions
- delete
- getAllPallets
- getOrSpawnPallet
- getSupportedFillTypes
- load
- loadPalletFromFilename
- new
- onFindExistingPallet
- onFindPallet
- onFinishLoadingPallet
- onSpawnSearchFinished
- registerXMLPaths
- spawnPallet
- update
delete
DescriptionDefinitiondelete()Code
74 | function PalletSpawner:delete() |
75 | end |
getAllPallets
DescriptionGet all pallets for given fillTypeId within the spawn placesDefinition
getAllPallets(int fillTypeId, function callbackFunc, table callbackTarget)Arguments
int | fillTypeId | fillTypeId to be supported by the pallet |
function | callbackFunc | callback function which will receive indexed list of pallet vehicle objects |
table | callbackTarget | target for the callback function |
171 | function 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)) |
184 | end |
getOrSpawnPallet
DescriptionGet pallet for given fillTypeId, searches spawnPlaces for existing non-full pallets, spawns a new one otherwiseDefinition
getOrSpawnPallet(int fillTypeId, function callback, table callbackTarget)Arguments
int | fillTypeId | fillTypeId to be supported by the pallet |
function | callback | callback function which will receive pallet vehicle |
table | callbackTarget | target for the callback function |
147 | function 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 |
164 | end |
getSupportedFillTypes
DescriptionDefinitiongetSupportedFillTypes()Code
116 | function PalletSpawner:getSupportedFillTypes() |
117 | return self.fillTypeIdToPallet |
118 | end |
load
DescriptionLoads data from xmlDefinition
load(table components, table xmlFile, string key, string customEnv, table i3dMappings)Arguments
table | components | components |
table | xmlFile | xml file object |
string | key | xml key |
string | customEnv | custom environment |
table | i3dMappings | i3dMappings |
boolean | success | success |
40 | function 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 |
70 | end |
loadPalletFromFilename
DescriptionDefinitionloadPalletFromFilename()Code
79 | function 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 |
112 | end |
new
DescriptionDefinitionnew()Code
22 | function 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 |
30 | end |
onFindExistingPallet
DescriptionDefinitiononFindExistingPallet()Code
235 | function 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 |
243 | end |
onFindPallet
DescriptionDefinitiononFindPallet()Code
247 | function 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 |
254 | end |
onFinishLoadingPallet
DescriptionDefinitiononFinishLoadingPallet()Code
225 | function 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) |
231 | end |
onSpawnSearchFinished
DescriptionDefinitiononSpawnSearchFinished()Code
206 | function 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 |
221 | end |
registerXMLPaths
DescriptionDefinitionregisterXMLPaths()Code
258 | function 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") |
264 | end |
spawnPallet
DescriptionSpawn and get pallet for given fillTypeIdDefinition
spawnPallet(int fillTypeId, function callback, table callbackTarget)Arguments
int | fillTypeId | fillTypeId to be supported by the pallet |
function | callback | callback function which will receive pallet vehicle |
table | callbackTarget | target for the callback function |
125 | function 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 |
140 | end |
update
DescriptionDefinitionupdate()Code
188 | function 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 |
202 | end |