LUADOC - Farming Simulator 22

BeehiveSystem

Functions

addBeehive

Description
Definition
addBeehive()
Code
51function BeehiveSystem:addBeehive(beehiveToAdd)
52 if #self.beehivesSortedRadius == 0 then -- first beehive is added, subscribe to events
53 self:updateState()
54
55 g_messageCenter:subscribe(MessageType.HOUR_CHANGED, self.onHourChanged, self)
56 g_messageCenter:subscribe(MessageType.WEATHER_CHANGED, self.updateBeehivesState, self)
57 end
58
59 table.insert(self.beehivesSortedRadius, beehiveToAdd)
60
61 if self.mission.isMissionStarted then
62 self:showNoSpawnerWarning(beehiveToAdd)
63 end
64
65 -- make sure beehives with biggest radii are at the start of the list, so that iteration for crop yield bonus exits earlier
66 table.sort(self.beehivesSortedRadius, function (a, b) return a.spec_beehive.actionRadius > b.spec_beehive.actionRadius end)
67end

addBeehivePalletSpawner

Description
Definition
addBeehivePalletSpawner()
Code
199function BeehiveSystem:addBeehivePalletSpawner(beehivePalletSpawner)
200 table.addElement(self.beehivePalletSpawners, beehivePalletSpawner)
201
202 self:updateBeehivesOutput(beehivePalletSpawner:getOwnerFarmId())
203end

consoleCommandBeehiveDebug

Description
Definition
consoleCommandBeehiveDebug()
Code
240function BeehiveSystem:consoleCommandBeehiveDebug()
241 BeehiveSystem.DEBUG_ENABLED = not BeehiveSystem.DEBUG_ENABLED
242 return "BeehiveSystem.DEBUG_ENABLED=" .. tostring(BeehiveSystem.DEBUG_ENABLED)
243end

delete

Description
Definition
delete()
Code
45function BeehiveSystem:delete()
46 removeConsoleCommand("gsBeehiveDebug")
47end

getBeehiveInfluenceFactorAt

Description
Returns factor between 0..1 based on presence of beehives relative to given world position independent of farm
Definition
getBeehiveInfluenceFactorAt()
Code
178function BeehiveSystem:getBeehiveInfluenceFactorAt(wx, wz)
179 local beehiveInfluenceFactor = 0
180
181 for i=1, #self.beehivesSortedRadius do
182 local beehive = self.beehivesSortedRadius[i]
183
184 -- accumulate influence of all behives
185 beehiveInfluenceFactor = beehiveInfluenceFactor + beehive:getBeehiveInfluenceFactor(wx, wz)
186 if beehiveInfluenceFactor >= 1 then
187 break -- stop if factor reached max of 1
188 end
189 end
190 --#debug if BeehiveSystem.DEBUG_ENABLED then
191 --#debug local wy = getTerrainHeightAtWorldPos(g_currentMission.terrainRootNode, wx, 0, wz)
192 --#debug Utils.renderTextAtWorldPosition(wx, wy, wz, string.format("beehiveInfluenceFactor %.3f", math.min(beehiveInfluenceFactor, 1)), getCorrectTextSize(0.016), 0)
193 --#debug end
194 return math.min(beehiveInfluenceFactor, 1)
195end

getBeehives

Description
Definition
getBeehives()
Code
171function BeehiveSystem:getBeehives()
172 return self.beehivesSortedRadius
173end

getFarmBeehivePalletSpawner

Description
Definition
getFarmBeehivePalletSpawner()
Code
229function BeehiveSystem:getFarmBeehivePalletSpawner(farmId)
230 for _, beehivePalletSpawner in ipairs(self.beehivePalletSpawners) do
231 if beehivePalletSpawner:getOwnerFarmId() == farmId then
232 return beehivePalletSpawner
233 end
234 end
235 return nil
236end

getFarmHasBeehive

Description
Definition
getFarmHasBeehive()
Code
159function BeehiveSystem:getFarmHasBeehive(farmId)
160 for _, beehive in ipairs(self.beehivesSortedRadius) do
161 if beehive:getOwnerFarmId() == farmId then
162 return true
163 end
164 end
165
166 return false
167end

new

Description
Definition
new()
Code
18function BeehiveSystem.new(mission, customMt)
19 local self = setmetatable({}, customMt or BeehiveSystem_mt)
20
21 self.mission = mission
22
23 self.beehives = {}
24 self.beehivesSortedRadius = {}
25 self.beehivePalletSpawners = {}
26
27 self.isFxActive = false
28 self.isProductionActive = false
29
30 if self.mission:getIsServer() then
31 if g_addTestCommands then
32 addConsoleCommand("gsBeehiveDebug", "Toggles beehive debug mode", "consoleCommandBeehiveDebug", self)
33 end
34 end
35
36 self.updateCooldown = BeehiveSystem.COOLDOWN_DURATION
37 self.currentSpawnerUpdateIndex = 0
38
39 self.lastTimeNoSpawnerWarningDisplayed = 0
40 return self
41end

onHourChanged

Description
Definition
onHourChanged()
Code
83function BeehiveSystem:onHourChanged()
84 self:updateBeehivesOutput()
85 self:updateBeehivesState()
86end

removeBeehive

Description
Definition
removeBeehive()
Code
71function BeehiveSystem:removeBeehive(beehive)
72 table.removeElement(self.beehivesSortedRadius, beehive)
73
74 if #self.beehivesSortedRadius == 0 then
75 -- last beehive was removed, unsubscribe from events
76 g_messageCenter:unsubscribe(MessageType.HOUR_CHANGED, self)
77 g_messageCenter:unsubscribe(MessageType.WEATHER_CHANGED, self)
78 end
79end

removeBeehivePalletSpawner

Description
Definition
removeBeehivePalletSpawner()
Code
207function BeehiveSystem:removeBeehivePalletSpawner(beehivePalletSpawner)
208 table.removeElement(self.beehivePalletSpawners, beehivePalletSpawner)
209
210 self:showNoSpawnerWarning(beehivePalletSpawner)
211end

showNoSpawnerWarning

Description
show notification if no spawner is left for the farm
Definition
showNoSpawnerWarning()
Code
215function BeehiveSystem:showNoSpawnerWarning(placeable)
216 if self.mission:getIsClient() and (g_time - self.lastTimeNoSpawnerWarningDisplayed) > 5000 then
217 local placeableFarmId = placeable:getOwnerFarmId()
218 local farmId = self.mission:getFarmId()
219 if self:getFarmHasBeehive(farmId) and farmId == placeableFarmId and self:getFarmBeehivePalletSpawner(farmId) == nil then
220 local text = g_i18n:getText("ingameNotification_noPalletLocationAvailable") .. string.format(" (%s)", g_i18n:getText("category_beeHives"))
221 self.mission:addIngameNotification(FSBaseMission.INGAME_NOTIFICATION_CRITICAL, text)
222 self.lastTimeNoSpawnerWarningDisplayed = g_time
223 end
224 end
225end

update

Description
Definition
update()
Code
90function BeehiveSystem:update()
91 if #self.beehivePalletSpawners == 0 or not self.mission:getIsServer() then
92 return
93 end
94
95 if self.updateCooldown <= 0 then
96 local beehiveSpawner = self.beehivePalletSpawners[self.currentSpawnerUpdateIndex]
97 if beehiveSpawner ~= nil then
98 beehiveSpawner:updatePallets()
99 self.currentSpawnerUpdateIndex = self.currentSpawnerUpdateIndex + 1
100 else
101 self.currentSpawnerUpdateIndex = 1
102 self.updateCooldown = BeehiveSystem.COOLDOWN_DURATION
103 end
104 else
105 self.updateCooldown = self.updateCooldown - 1
106 end
107end

updateBeehivesOutput

Description
collects honey from all behives and spawns pallets at triggers
Definition
updateBeehivesOutput(farmId integer)
Arguments
farmIdintegeroptional filter for farm id
Code
112function BeehiveSystem:updateBeehivesOutput(farmId)
113 if self.mission:getIsServer() then
114 for i=1, #self.beehivesSortedRadius do
115 local beehive = self.beehivesSortedRadius[i]
116 local beehiveOwner = beehive:getOwnerFarmId()
117 if farmId == nil or (farmId == beehiveOwner) then -- optional filter
118 local palletSpawner = self:getFarmBeehivePalletSpawner(beehiveOwner)
119 if palletSpawner ~= nil then
120 local honeyAmount = beehive:getHoneyAmountToSpawn()
121 if honeyAmount > 0 then
122 palletSpawner:addFillLevel(honeyAmount) -- accumulate fillLevel of all beehives first, update spawners once at the end
123 end
124 end
125 end
126 end
127 end
128end

updateBeehivesState

Description
updates beehive fx and production state
Definition
updateBeehivesState()
Code
149function BeehiveSystem:updateBeehivesState()
150 self:updateState()
151
152 for i=1, #self.beehivesSortedRadius do
153 self.beehivesSortedRadius[i]:updateBeehiveState()
154 end
155end

updateState

Description
updates internal state used by beehives
Definition
updateState()
Code
132function BeehiveSystem:updateState()
133 local environment = g_currentMission.environment
134 self.isFxActive = true
135 self.isProductionActive = true
136
137 if not environment.isSunOn then
138 self.isFxActive = false
139 elseif environment.currentSeason == Environment.SEASON.WINTER then
140 self.isFxActive = false
141 self.isProductionActive = false -- honey production is continuous unless it's winter
142 elseif environment.weather:getIsRaining() then
143 self.isFxActive = false
144 end
145end