forked from bodzio528/FS22_CropRotation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCropRotationData.lua
More file actions
198 lines (155 loc) · 6.98 KB
/
CropRotationData.lua
File metadata and controls
198 lines (155 loc) · 6.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
--
-- FS22 - Crop Rotation mod
--
-- CropRotationData.lua
--
-- manage static data - crop categories and properties,
-- make contents of data/crops.xml accessible
CropRotationData = {
MOD_DIRECTORY = g_currentModDirectory
}
CropRotationData.BAD = 0
CropRotationData.NEUTRAL = 1
CropRotationData.GOOD = 2
local CropRotationData_mt = Class(CropRotationData)
function CropRotationData:new(crModule, customMt)
local self = setmetatable({}, customMt or CropRotationData_mt)
self.crModule = crModule
self.crModule.log:debug("CropRotationData:new() debug prints enabled - expect high amount of messages at startup.")
self.matrix = {}
return self
end
function CropRotationData:initialize()
self.crModule.log:debug("CropRotationData:initialize()")
self.fruitTypeManager = g_fruitTypeManager
end
function CropRotationData:delete()
self.crModule.log:debug("CropRotationData:delete() kthxbye!")
self.matrix = nil
end
function CropRotationData:loadFromSavegame(xmlFile)
end
----------------------------------------------------------------------
--- PUBLIC INTERFACE
----------------------------------------------------------------------
function CropRotationData:getRotationForecropValue(past, current)
if past == FruitType.UNKNOWN then
return CropRotationData.GOOD
end
if self.matrix[current] then
return self.matrix[current][past] or CropRotationData.NEUTRAL
end
return CropRotationData.NEUTRAL
end
----------------------------------------------------------------------
--- LOADING DATA FROM crops.xml
----------------------------------------------------------------------
function CropRotationData:loadFromXML(xmlFile, key, baseDirectory, cropsXmlFileName, mapFilename)
self.crModule.log:debug("CropRotationData:loadFromXML() path: " .. cropsXmlFileName)
if xmlFile then
local crops, overwrite = self:parseCropsXml(xmlFile, key, cropsXmlFileName)
if crops then
self:process(crops, overwrite, cropsXmlFileName)
end
end
-- optional: load crops.xml from different locations
end
--load data from file and build the necessary tables for crop rotation
function CropRotationData:parseCropsXml(xmlFile, cropsKey, cropsXmlFileName)
self.crModule.log:debug("CropRotationData:parseCropsXml()")
if not hasXMLProperty(xmlFile, cropsKey) then
self.crModule.log:error("CropRotationData:parseCropsXml(): loading XML element '" .. cropsKey .. "' failed: " .. cropsXmlFileName)
return
end
local crops = {}
local overwrite = Utils.getNoNil(getXMLBool(xmlFile, cropsKey .. "#overwrite"), false)
if overwrite then
self.crModule.log:debug("CropRotationData:parseCropsXml(): overwrite crops data with file " .. cropsXmlFileName)
end
local i = 0
while true do
local cropKey = string.format("%s.crop(%i)", cropsKey, i)
if not hasXMLProperty(xmlFile, cropKey) then
break
end
local cropName = getXMLString(xmlFile, cropKey .. "#name"):upper()
if cropName ~= nil then
local cropEnabled = Utils.getNoNil(getXMLBool(xmlFile, cropKey .. "#enabled"), true)
local cropReturnPeriod = Utils.getNoNil(getXMLInt(xmlFile, cropKey .. "#returnPeriod"), 2)
local goodForecrops = Utils.getNoNil(getXMLString(xmlFile, cropKey .. ".good"), ""):upper()
local badForecrops = Utils.getNoNil(getXMLString(xmlFile, cropKey .. ".bad"), ""):upper()
self.crModule.log:debug(string.format("CropRotationData:parseCropsXml(): reading crop %s forecrops: good: [%s] bad: [%s]", cropName, goodForecrops, badForecrops))
crops[cropName] = {
enabled = cropEnabled,
returnPeriod = cropReturnPeriod,
good = string.split(goodForecrops, " "),
bad = string.split(badForecrops, " ")
}
else
self.crModule.log:error("CropRotationData:parseCropsXml(): loading XML element '"..cropKey .. "#name' failed: " .. cropsXmlFileName)
return crops, overwrite
end
i = i + 1
end
return crops, overwrite
end
function CropRotationData:process(crops, overwrite, cropsXmlFileName)
self.crModule.log:debug("CropRotationData:process()")
if overwrite then
self.crModule.log:debug('CropRotationData:process(): overwrite crop rotation matrix by discarding entries created so far')
self.matrix = {}
end
-- write crop rotation data to fruitTypeManager and compose local matrix
for cropName, cropDesc in pairs(crops) do
local fruitType = self.fruitTypeManager:getFruitTypeByName(cropName)
if fruitType ~= nil then
fruitType.rotation = {}
fruitType.rotation.enabled = cropDesc.enabled
fruitType.rotation.returnPeriod = cropDesc.returnPeriod
self.matrix[fruitType.index] = {}
for _, name in pairs(cropDesc.good) do
local forecropType = self.fruitTypeManager:getFruitTypeByName(name)
if forecropType ~= nil then
self.matrix[fruitType.index][forecropType.index] = CropRotationData.GOOD
end
end
for _, name in pairs(cropDesc.bad) do
local forecropType = self.fruitTypeManager:getFruitTypeByName(name)
if forecropType ~= nil then
self.matrix[fruitType.index][forecropType.index] = CropRotationData.BAD
end
end
end
end
-- process fruits not mentioned in crops.xml
for k, fruitType in pairs(self.fruitTypeManager:getFruitTypes()) do
if crops[fruitType.name] == nil then
self.crModule.log:info(string.format("CropRotationData:process(): fruit (%i): '%s' not mentioned in file %s", fruitType.index, fruitType.name, cropsXmlFileName))
fruitType.rotation = {
enabled = false,
returnPeriod = 2
}
-- no good and no bad forecrops
self.matrix[fruitType.index] = {}
end
end
end
----------------------------------------------------------------------
-- DEBUG
----------------------------------------------------------------------
function CropRotationData:postLoad()
self.crModule.log:debug("CropRotationData:postLoad(): list fruits in rotation ...")
for fruitIndex, fruitType in pairs(self.fruitTypeManager:getFruitTypes()) do
if fruitType.rotation ~= nil then
if fruitType.rotation.enabled then
self.crModule.log:debug(string.format("ENABLED fruit(%d): %s RP=%d", fruitIndex, fruitType.name, fruitType.rotation.returnPeriod))
else
self.crModule.log:debug(string.format("DISABLED fruit(%d): %s", fruitIndex, fruitType.name))
end
else
local s = string.format("INVALID fruit(%d): %s", fruitIndex, fruitType.name)
self.crModule.log:warning(s)
end
end
self.crModule.log:debug("CropRotationData:postLoad(): ... done")
end