52 | function HusbandryModulePallets:load(xmlFile, configKey, rootNode, owner, baseDirectory) |
53 | local result = HusbandryModulePallets:superClass().load(self, xmlFile, configKey, rootNode, owner, baseDirectory) |
54 | |
55 | if result ~= true then |
56 | return false |
57 | end |
58 | if not hasXMLProperty(xmlFile, configKey) then |
59 | return false |
60 | end |
61 | |
62 | self.palletSpawnerStartNode = I3DUtil.indexToObject(rootNode, getXMLString(xmlFile, configKey .. "#startNode")) |
63 | self.palletSpawnerWidthNode = I3DUtil.indexToObject(rootNode, getXMLString(xmlFile, configKey .. "#widthNode")) |
64 | self.palletSpawnerHeightNode = I3DUtil.indexToObject(rootNode, getXMLString(xmlFile, configKey .. "#heightNode")) |
65 | |
66 | local xw, yw, zw = getTranslation(self.palletSpawnerWidthNode) |
67 | if xw == 0.0 or math.abs(yw) > 0.001 or math.abs(zw) > 0.001 then |
68 | g_logManager:devWarning(string.format("Warning: width node of husbandry module pallets has incorrect parameters x(%.3f), y(%.3f), z(%.3f).", xw, yw, zw)) |
69 | return false |
70 | end |
71 | local xh, yh, zh = getTranslation(self.palletSpawnerHeightNode) |
72 | if math.abs(xh) > 0.001 or math.abs(yh) > 0.001 or zh == 0.0 then |
73 | g_logManager:devWarning(string.format("Warning: height node of husbandry module pallets has incorrect parameters x(%.3f), y(%.3f), z(%.3f).", xh, yh, zh)) |
74 | return false |
75 | end |
76 | |
77 | self.palletSpawnerNode = self.palletSpawnerStartNode |
78 | self.palletSpawnerAreaSizeX = xw |
79 | self.palletSpawnerAreaSizeZ = zh |
80 | |
81 | local fillTypeStr = getXMLString(xmlFile, configKey .. "#fillType") |
82 | self.palletFillTypeIndex = g_fillTypeManager:getFillTypeIndexByName(fillTypeStr) |
83 | self.palletConfigFilename = Utils.getNoNil(getXMLString(xmlFile, configKey .. "#filename"), "") |
84 | self.palletConfigFilename = Utils.getFilename(self.palletConfigFilename, self.baseDirectory) |
85 | self.palletFillUnitIndex = getXMLFloat(xmlFile, configKey .. "#fillUnitIndex") |
86 | self.sizeWidth, self.sizeLength, _, _ = StoreItemUtil.getSizeValues(self.palletConfigFilename, "vehicle", 0, {}) |
87 | |
88 | -- In the backend, use the fill levels synchronization for MP sync |
89 | self.fillLevels[self.palletFillTypeIndex] = 0.0 |
90 | |
91 | self.palletSpawnerFillDelta = 0 |
92 | self.numObjectsInPalletSpawnerTrigger = 0 |
93 | |
94 | return self.palletSpawnerNode ~= nil and self.palletFillTypeIndex ~= nil and self.palletConfigFilename ~= "" and self.palletFillUnitIndex ~= nil |
95 | end |
100 | function HusbandryModulePallets:onIntervalUpdate(dayToInterval) |
101 | HusbandryModulePallets:superClass().onIntervalUpdate(self, dayToInterval) |
102 | local totalNumAnimals = self.owner:getNumOfAnimals() |
103 | |
104 | if self.singleAnimalUsagePerDay > 0 and totalNumAnimals > 0 then |
105 | local productivity = self.owner:getGlobalProductionFactor() |
106 | local fillDelta = productivity * totalNumAnimals * self.singleAnimalUsagePerDay * dayToInterval |
107 | |
108 | self.palletSpawnerFillDelta = self.palletSpawnerFillDelta + fillDelta |
109 | |
110 | if productivity > 0 and self.palletSpawnerFillDelta > 0 then |
111 | -- check if last pallet is still valid |
112 | if self.currentPallet ~= nil then |
113 | if self.currentPallet:getFillUnitFreeCapacity(self.palletFillUnitIndex) < 0.001 then |
114 | self.currentPallet = nil |
115 | end |
116 | end |
117 | |
118 | if self.currentPallet ~= nil then |
119 | if not entityExists(self.currentPallet.rootNode) then |
120 | self.currentPallet = nil |
121 | else |
122 | local x, _, z = localToLocal(self.currentPallet.rootNode, self.palletSpawnerNode, 0,0,0) |
123 | if x < 0 or z < 0 or x > self.palletSpawnerAreaSizeX or z > self.palletSpawnerAreaSizeZ then |
124 | self.currentPallet = nil |
125 | end |
126 | end |
127 | end |
128 | |
129 | -- check if there is a pallet which can be filled |
130 | if self.currentPallet == nil then |
131 | self.availablePallet = nil |
132 | local x,y,z = localToWorld(self.palletSpawnerNode, 0.5 * self.palletSpawnerAreaSizeX, 0, 0.5 * self.palletSpawnerAreaSizeZ) |
133 | local rx,ry,rz = getWorldRotation(self.palletSpawnerNode) |
134 | local nbShapesOverlap = overlapBox(x, y - 5, z, rx, ry, rz, 0.5 * self.palletSpawnerAreaSizeX, 10, 0.5 * self.palletSpawnerAreaSizeZ, "palletSpawnerCollisionTestCallback", self, nil, true, false, true) |
135 | if self.availablePallet ~= nil then |
136 | self.currentPallet = self.availablePallet |
137 | end |
138 | end |
139 | |
140 | if self.currentPallet == nil then |
141 | local rx, ry, rz = getWorldRotation(self.palletSpawnerNode) |
142 | local x, y, z = getWorldTranslation(self.palletSpawnerNode) |
143 | local canCreatePallet = false |
144 | |
145 | local widthHalf = self.sizeWidth * 0.5 |
146 | local heightHalf = self.sizeLength * 0.5 |
147 | |
148 | for dx = widthHalf, self.palletSpawnerAreaSizeX - widthHalf, widthHalf do |
149 | for dz = heightHalf, self.palletSpawnerAreaSizeZ - heightHalf, widthHalf do |
150 | x, y, z = localToWorld(self.palletSpawnerNode, dx, 0, dz) |
151 | self.palletSpawnerCollisionObjectId = 0 |
152 | local nbShapesOverlap = overlapBox(x, y - 5, z, rx, ry, rz, widthHalf, 10.0, heightHalf, "palletSpawnerCollisionTestCallback", self, nil, true, false, true) |
153 | if self.palletSpawnerCollisionObjectId == 0 then |
154 | canCreatePallet = true |
155 | break |
156 | end |
157 | end |
158 | if canCreatePallet then |
159 | break |
160 | end |
161 | end |
162 | |
163 | if canCreatePallet and self.palletSpawnerFillDelta > HusbandryModulePallets.fillLevelThresholdForDeletion then |
164 | self.currentPallet = g_currentMission:loadVehicle(self.palletConfigFilename, x, nil, z, 1.2, ry, true, 0, Vehicle.PROPERTY_STATE_OWNED, self.owner:getOwnerFarmId(), nil, nil) |
165 | end |
166 | end |
167 | |
168 | if self.currentPallet ~= nil then |
169 | self.palletSpawnerFillDelta = self.currentPallet:addFillUnitFillLevel(self.owner:getOwnerFarmId(), self.palletFillUnitIndex, self.palletSpawnerFillDelta, self.palletFillTypeIndex, ToolType.UNDEFINED) |
170 | |
171 | -- Set the fill level for syncing with clients |
172 | self:setFillLevel(self.palletFillUnitIndex, self:getCurrentPalletFillLevel()) |
173 | elseif self.palletSpawnerFillDelta > HusbandryModulePallets.fillLevelThresholdForDeletion then |
174 | self:showSpawnerBlockedWarning() |
175 | end |
176 | end |
177 | end |
178 | end |