LUADOC - Farming Simulator 19

FieldManager

Description
This class handles all functionality for AI fields and the NPCs handling them.
Parent
AbstractManager
Functions

delete

Description
Deletes field manager
Definition
delete()
Code
212function FieldManager:delete()
213end

initDataStructures

Description
Initialize data structures
Definition
initDataStructures()
Code
41function FieldManager:initDataStructures()
42 self.fields = {}
43 self.farmlandIdFieldMapping = {}
44 self.fieldStatusParametersToSet = nil
45 self.currentFieldPartitionIndex = nil
46end

loadMapData

Description
Load data on map load
Definition
loadMapData()
Return Values
booleantrueif loading was successful else false
Code
51function FieldManager:loadMapData(xmlFile)
52 FieldManager:superClass().loadMapData(self)
53
54 g_currentMission:addUpdateable(self)
55
56 self.setFieldPartitionModifier = DensityMapModifier:new(g_currentMission.terrainDetailId)
57 self.detailModifier = DensityMapModifier:new(g_currentMission.terrainDetailId)
58
59 local weedType = g_fruitTypeManager:getWeedFruitType()
60 if weedType ~= nil then
61 local ids = g_currentMission.fruits[weedType.index]
62 local weed = weedType.weed
63
64 self.weedModifier = DensityMapModifier:new(ids.id, weedType.startStateChannel, weedType.numStateChannels)
65 end
66
67 -- create list of valid/available fruit types
68 self.availableFruitTypeIndices = {}
69 self.minFieldGrowthStateTime = math.huge
70 for _, fruitType in ipairs(g_fruitTypeManager:getFruitTypes()) do
71 if fruitType.useForFieldJob and fruitType.allowsSeeding and fruitType.needsSeeding then
72 if g_currentMission.fruits[fruitType.index] ~= nil then
73 table.insert(self.availableFruitTypeIndices, fruitType.index)
74 self.minFieldGrowthStateTime = math.min(self.minFieldGrowthStateTime, fruitType.growthStateTime)
75 end
76 end
77 end
78 self.fruitTypesCount = table.getn(self.availableFruitTypeIndices)
79
80 self.fieldIndexToCheck = table.getn(self.fields)
81
82 -- Connect farmlands to fields first. We need the farmlands to skip overriding owned fields (in order to have working starter fields)
83 g_deferredLoadingManager:addSubtask(function()
84 for _, field in ipairs(self.fields) do
85 local posX, posZ = field:getCenterOfFieldWorldPosition()
86 local farmland = g_farmlandManager:getFarmlandAtWorldPosition(posX, posZ)
87
88 field:setFarmland(farmland)
89
90 if self.farmlandIdFieldMapping[farmland.id] == nil then
91 self.farmlandIdFieldMapping[farmland.id] = {}
92 end
93
94 table.insert(self.farmlandIdFieldMapping[farmland.id], field)
95 end
96 end)
97
98 -- New save game
99 if not g_currentMission.missionInfo.isValid and g_server ~= nil then
100 g_deferredLoadingManager:addSubtask(function()
101 local index = 1
102
103 for _, field in pairs(self.fields) do
104 if field:getIsAIActive() and field.fieldMissionAllowed and not field.farmland.isOwned then
105 local fruitIndex = self.availableFruitTypeIndices[index]
106 if field.fieldGrassMission then
107 fruitIndex = FruitType.GRASS
108 end
109
110 local fruitDesc = g_fruitTypeManager:getFruitTypeByIndex(fruitIndex)
111 local fieldState = FieldManager.FIELDSTATE_GROWING
112
113 local growthState
114 if fruitDesc.minHarvestingGrowthState == 0 and fruitDesc.maxHarvestingGrowthState == 0 and fruitDesc.cutState == 0 then
115 growthState = 2
116 else
117 if fruitDesc.minPreparingGrowthState ~= -1 then
118 growthState = math.random(1, fruitDesc.maxPreparingGrowthState + 1)
119 else
120 growthState = math.random(1, fruitDesc.maxHarvestingGrowthState + 1)
121 end
122 end
123
124 -- Force grass up
125 if fruitIndex == FruitType.GRASS and growthState == 1 then
126 growthState = 2
127 end
128
129 local weedValue = 0
130 if fruitDesc.plantsWeed then
131 weedValue = 1
132 end
133
134 local plowState = 0
135 if not g_currentMission.missionInfo.plowingRequiredEnabled then
136 plowState = g_currentMission.plowCounterMaxValue
137 else
138 plowState = math.random(0, g_currentMission.plowCounterMaxValue)
139 end
140
141 local sprayLevel = math.random(0, g_currentMission.sprayLevelMaxValue)
142 local limeState = math.random(0, g_currentMission.limeCounterMaxValue)
143
144 for i = 1, table.getn(field.maxFieldStatusPartitions) do
145 self:setFieldPartitionStatus(field, field.maxFieldStatusPartitions, i, fruitIndex, fieldState, growthState, sprayLevel, false, plowState, weedValue, limeState)
146 end
147
148 index = index + 1
149 if index > self.fruitTypesCount then
150 index = 1
151 end
152 end
153 end
154 end)
155 elseif g_server ~= nil then
156 -- get current state of fields
157 for _, field in pairs(self.fields) do
158 g_deferredLoadingManager:addSubtask(function()
159 self:findFieldFruit(field)
160 end)
161 end
162 end
163
164 g_deferredLoadingManager:addSubtask(function()
165 self:findFieldSizes()
166 end)
167
168 g_deferredLoadingManager:addSubtask(function()
169 g_farmlandManager:addStateChangeListener(self)
170
171 if g_currentMission:getIsServer() then
172 if g_addCheatCommands then
173 addConsoleCommand("gsSetFieldFruit", "Sets a given fruit to field", "consoleCommandSetFieldFruit", self)
174 addConsoleCommand("gsSetFieldFruitAll", "Sets a given fruit to all fields", "consoleCommandSetFieldFruitAll", self)
175 addConsoleCommand("gsSetFieldGround", "Sets a given fruit to field", "consoleCommandSetFieldGround", self)
176 addConsoleCommand("gsSetFieldGroundAll", "Sets a given fruit to allfield", "consoleCommandSetFieldGroundAll", self)
177 end
178 end
179 end)
180
181 g_messageCenter:subscribe(MessageType.FARM_PROPERTY_CHANGED, self.farmPropertyChanged, self)
182end

new

Description
Creating manager
Definition
new()
Return Values
tableinstanceinstance of object
Code
33function FieldManager:new(customMt)
34 local self = AbstractManager:new(customMt or FieldManager_mt)
35
36 return self
37end

unloadMapData

Description
Unload data on mission delete
Definition
unloadMapData()
Code
186function FieldManager:unloadMapData()
187 g_currentMission:removeUpdateable(self)
188 g_farmlandManager:removeStateChangeListener(self)
189
190 for _, field in pairs(self.fields) do
191 field:delete()
192 end
193 self.fields = {}
194
195 self.setFieldPartitionModifier = nil
196 self.detailModifier = nil
197 self.weedModifier = nil
198
199 g_messageCenter:unsubscribeAll(self)
200
201 removeConsoleCommand("gsSetFieldFruit")
202 removeConsoleCommand("gsSetFieldFruitAll")
203 removeConsoleCommand("gsSetFieldGround")
204 removeConsoleCommand("gsSetFieldGroundAll")
205
206
207 FieldManager:superClass().unloadMapData(self)
208end