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
BeehiveSystem
Functions- addBeehive
- addBeehivePalletSpawner
- consoleCommandBeehiveDebug
- delete
- getBeehiveInfluenceFactorAt
- getBeehives
- getFarmBeehivePalletSpawner
- getFarmHasBeehive
- new
- onHourChanged
- removeBeehive
- removeBeehivePalletSpawner
- showNoSpawnerWarning
- update
- updateBeehivesOutput
- updateBeehivesState
- updateState
addBeehive
DescriptionDefinitionaddBeehive()Code
51 | function 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) |
67 | end |
addBeehivePalletSpawner
DescriptionDefinitionaddBeehivePalletSpawner()Code
199 | function BeehiveSystem:addBeehivePalletSpawner(beehivePalletSpawner) |
200 | table.addElement(self.beehivePalletSpawners, beehivePalletSpawner) |
201 | |
202 | self:updateBeehivesOutput(beehivePalletSpawner:getOwnerFarmId()) |
203 | end |
consoleCommandBeehiveDebug
DescriptionDefinitionconsoleCommandBeehiveDebug()Code
240 | function BeehiveSystem:consoleCommandBeehiveDebug() |
241 | BeehiveSystem.DEBUG_ENABLED = not BeehiveSystem.DEBUG_ENABLED |
242 | return "BeehiveSystem.DEBUG_ENABLED=" .. tostring(BeehiveSystem.DEBUG_ENABLED) |
243 | end |
delete
DescriptionDefinitiondelete()Code
45 | function BeehiveSystem:delete() |
46 | removeConsoleCommand("gsBeehiveDebug") |
47 | end |
getBeehiveInfluenceFactorAt
DescriptionReturns factor between 0..1 based on presence of beehives relative to given world position independent of farmDefinition
getBeehiveInfluenceFactorAt()Code
178 | function 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) |
195 | end |
getBeehives
DescriptionDefinitiongetBeehives()Code
171 | function BeehiveSystem:getBeehives() |
172 | return self.beehivesSortedRadius |
173 | end |
getFarmBeehivePalletSpawner
DescriptionDefinitiongetFarmBeehivePalletSpawner()Code
229 | function 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 |
236 | end |
getFarmHasBeehive
DescriptionDefinitiongetFarmHasBeehive()Code
159 | function 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 |
167 | end |
new
DescriptionDefinitionnew()Code
18 | function 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 |
41 | end |
onHourChanged
DescriptionDefinitiononHourChanged()Code
83 | function BeehiveSystem:onHourChanged() |
84 | self:updateBeehivesOutput() |
85 | self:updateBeehivesState() |
86 | end |
removeBeehive
DescriptionDefinitionremoveBeehive()Code
71 | function 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 |
79 | end |
removeBeehivePalletSpawner
DescriptionDefinitionremoveBeehivePalletSpawner()Code
207 | function BeehiveSystem:removeBeehivePalletSpawner(beehivePalletSpawner) |
208 | table.removeElement(self.beehivePalletSpawners, beehivePalletSpawner) |
209 | |
210 | self:showNoSpawnerWarning(beehivePalletSpawner) |
211 | end |
showNoSpawnerWarning
Descriptionshow notification if no spawner is left for the farmDefinition
showNoSpawnerWarning()Code
215 | function 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 |
225 | end |
update
DescriptionDefinitionupdate()Code
90 | function 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 |
107 | end |
updateBeehivesOutput
Descriptioncollects honey from all behives and spawns pallets at triggersDefinition
updateBeehivesOutput(farmId integer)Arguments
farmId | integer | optional filter for farm id |
112 | function 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 |
128 | end |
updateBeehivesState
Descriptionupdates beehive fx and production stateDefinition
updateBeehivesState()Code
149 | function BeehiveSystem:updateBeehivesState() |
150 | self:updateState() |
151 | |
152 | for i=1, #self.beehivesSortedRadius do |
153 | self.beehivesSortedRadius[i]:updateBeehiveState() |
154 | end |
155 | end |
updateState
Descriptionupdates internal state used by beehivesDefinition
updateState()Code
132 | function 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 |
145 | end |