LUADOC - Farming Simulator 19

FarmStats

Description
Farm statistics
Functions

addHeroStat

Description
Definition
addHeroStat()
Code
348function FarmStats:addHeroStat(statName, delta)
349 if self.heroStats[statName] ~= nil then
350 if statName == "moneyEarned" then
351 -- Money earned updates too fast and causes Xbox event rate issues.
352 -- We queue the value and update in :update every 10 seconds
353 self.moneyEarnedHeroAccum = self.moneyEarnedHeroAccum + delta
354 else
355 self:addValueToHeroStat(statName, delta)
356 end
357 elseif statName == "fieldJobMissionCount" or statName == "transportMissionCount" then
358 self:addValueToHeroStat("completedMissions", delta)
359 end
360end

addStatistic

Description
Add a statistic to the statistic data table
Definition
addStatistic()

addValueToHeroStat

Description
Definition
addValueToHeroStat()
Code
240function FarmStats:addValueToHeroStat(name, value)
241 local heroStat = self.heroStats[name]
242
243 if self.heroStatsLoaded then
244 heroStat.value = heroStat.value + value
245 statsSet(heroStat.id, heroStat.value)
246 else
247 heroStat.accumValue = heroStat.accumValue + value
248 end
249end

archiveFinances

Description
Definition
archiveFinances()
Code
272function FarmStats:archiveFinances()
273 if g_currentMission:getIsServer() then
274 table.insert(self.financesHistory, self.finances)
275 self.finances = FinanceStats:new()
276
277 self.financesVersionCounter = self.financesVersionCounter + 1
278 if self.financesVersionCounter > 999999 then
279 self.financesVersionCounter = 0
280 end
281
282 self.financesHistoryVersionCounter = self.financesHistoryVersionCounter + 1
283 if self.financesHistoryVersionCounter > 127 then
284 self.financesHistoryVersionCounter = 0
285 end
286 end
287end

changeFinanceStats

Description
Definition
changeFinanceStats()
Code
253function FarmStats:changeFinanceStats(amount, statType)
254 if statType ~= nil then
255 if self.finances[statType] ~= nil then
256 self.finances[statType] = self.finances[statType] + amount
257
258 if g_currentMission:getIsServer() then
259 self.financesVersionCounter = self.financesVersionCounter + 1
260 if self.financesVersionCounter > 999999 then
261 self.financesVersionCounter = 0
262 end
263 end
264 else
265 print("Warning: Invalid finance statType '"..statType.."'")
266 end
267 end
268end

delete

Description
Definition
delete()
Code
71function FarmStats:delete()
72 g_currentMission:removeUpdateable(self)
73end

getCompletedFieldMissions

Description
Definition
getCompletedFieldMissions()

getCompletedFieldMissionsSession

Description
Definition
getCompletedFieldMissionsSession()

getCompletedMissions

Description
Definition
getCompletedMissions()

getCompletedMissionsSession

Description
Definition
getCompletedMissionsSession()

getCompletedTransportMissions

Description
Definition
getCompletedTransportMissions()

getCompletedTransportMissionsSession

Description
Definition
getCompletedTransportMissionsSession()

getSessionValue

Description
Definition
getSessionValue()
Code
374function FarmStats:getSessionValue(statName)
375 if self.statistics[statName] ~= nil then
376 return self.statistics[statName].session
377 end
378
379 return nil
380end

getStatisticData

Description
Turn all data into text to display in the finances menu
Definition
getStatisticData()
Return Values
tabledataData for the menu
Code
427function FarmStats:getStatisticData()
428 if not g_currentMission.missionDynamicInfo.isMultiplayer or not g_currentMission.missionDynamicInfo.isClient then
429 self:addStatistic("workedHectares", g_i18n:getAreaUnit(false), g_i18n:getArea(self:getSessionValue("workedHectares")), g_i18n:getArea(self:getTotalValue("workedHectares")), "%.2f")
430 self:addStatistic("cultivatedHectares", g_i18n:getAreaUnit(false), g_i18n:getArea(self:getSessionValue("cultivatedHectares")), g_i18n:getArea(self:getTotalValue("cultivatedHectares")), "%.2f")
431 self:addStatistic("plowedHectares", g_i18n:getAreaUnit(false), g_i18n:getArea(self:getSessionValue("plowedHectares")), g_i18n:getArea(self:getTotalValue("plowedHectares")), "%.2f")
432 self:addStatistic("sownHectares", g_i18n:getAreaUnit(false), g_i18n:getArea(self:getSessionValue("sownHectares")), g_i18n:getArea(self:getTotalValue("sownHectares")), "%.2f")
433 self:addStatistic("fertilizedHectares", g_i18n:getAreaUnit(false), g_i18n:getArea(self:getSessionValue("fertilizedHectares")), g_i18n:getArea(self:getTotalValue("fertilizedHectares")), "%.2f")
434 self:addStatistic("threshedHectares", g_i18n:getAreaUnit(false), g_i18n:getArea(self:getSessionValue("threshedHectares")), g_i18n:getArea(self:getTotalValue("threshedHectares")), "%.2f")
435
436 self:addStatistic("workedTime", nil, Utils.formatTime(self:getSessionValue("workedTime")), Utils.formatTime(self:getTotalValue("workedTime")), "%s")
437 self:addStatistic("cultivatedTime", nil, Utils.formatTime(self:getSessionValue("cultivatedTime")), Utils.formatTime(self:getTotalValue("cultivatedTime")), "%s")
438 self:addStatistic("plowedTime", nil, Utils.formatTime(self:getSessionValue("plowedTime")), Utils.formatTime(self:getTotalValue("plowedTime")), "%s")
439 self:addStatistic("sownTime", nil, Utils.formatTime(self:getSessionValue("sownTime")), Utils.formatTime(self:getTotalValue("sownTime")), "%s")
440 self:addStatistic("fertilizedTime", nil, Utils.formatTime(self:getSessionValue("fertilizedTime")), Utils.formatTime(self:getTotalValue("fertilizedTime")), "%s")
441 self:addStatistic("threshedTime", nil, Utils.formatTime(self:getSessionValue("threshedTime")), Utils.formatTime(self:getTotalValue("threshedTime")), "%s")
442
443 self:addStatistic("traveledDistance", g_i18n:getMeasuringUnit(), g_i18n:getDistance(self:getSessionValue("traveledDistance")), g_i18n:getDistance(self:getTotalValue("traveledDistance")), "%.2f")
444 self:addStatistic("fuelUsage", g_i18n:getText("unit_liter"), g_i18n:getFluid(self:getSessionValue("fuelUsage")), g_i18n:getFluid(self:getTotalValue("fuelUsage")), "%.2f")
445 self:addStatistic("seedUsage", g_i18n:getText("unit_liter"), g_i18n:getFluid(self:getSessionValue("seedUsage")), g_i18n:getFluid(self:getTotalValue("seedUsage")), "%.2f")
446 self:addStatistic("sprayUsage", g_i18n:getText("unit_liter"), g_i18n:getFluid(self:getSessionValue("sprayUsage")), g_i18n:getFluid(self:getTotalValue("sprayUsage")), "%.2f")
447 self:addStatistic("baleCount", nil, self:getSessionValue("baleCount"), self:getTotalValue("baleCount"), "%d")
448
449 self:addStatistic("plantedTreeCount", nil, self:getSessionValue("plantedTreeCount"), self:getTotalValue("plantedTreeCount"), "%d")
450 self:addStatistic("cutTreeCount", nil, self:getSessionValue("cutTreeCount"), self:getTotalValue("cutTreeCount"), "%d")
451
452 self:addStatistic("fieldJobMissionCount", nil, self:getSessionValue("fieldJobMissionCount"), self:getTotalValue("fieldJobMissionCount"), "%d")
453 self:addStatistic("transportMissionCount", nil, self:getSessionValue("transportMissionCount"), self:getTotalValue("transportMissionCount"), "%d")
454
455 self:addStatistic("playTime", nil, Utils.formatTime(self:getSessionValue("playTime")), Utils.formatTime(self:getTotalValue("playTime")), "%s")
456 self:addStatistic("workersHired", nil, self:getSessionValue("workersHired"), nil, "%s")
457
458 self:addStatistic("storageCapacity", nil, g_currentMission:getFarmSiloCapacity(), nil, "%s")
459 end
460
461 return Utils.getNoNil(self.statisticData, {})
462end

getTotalValue

Description
Definition
getTotalValue()
Code
364function FarmStats:getTotalValue(statName)
365 if self.statistics[statName] ~= nil then
366 return self.statistics[statName].total
367 end
368
369 return nil
370end

loadFromXMLFile

Description
Definition
loadFromXMLFile()
Code
137function FarmStats:loadFromXMLFile(xmlFile, key)
138 local farmKey = key
139 local key = key .. ".statistics"
140
141 self.statistics["traveledDistance"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".traveledDistance"), 0)
142
143 self.statistics["fuelUsage"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".fuelUsage"), 0)
144 self.statistics["seedUsage"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".seedUsage"), 0)
145 self.statistics["sprayUsage"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".sprayUsage"), 0)
146
147 self.statistics["workedHectares"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".workedHectares"), 0)
148 self.statistics["cultivatedHectares"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".cultivatedHectares"), 0)
149 self.statistics["sownHectares"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".sownHectares"), 0)
150 self.statistics["fertilizedHectares"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".fertilizedHectares"), 0)
151 self.statistics["sprayedHectares"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".sprayedHectares"), 0)
152 self.statistics["threshedHectares"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".threshedHectares"), 0)
153 self.statistics["weededHectares"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".weededHectares"), 0)
154 self.statistics["plowedHectares"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".plowedHectares"), 0)
155
156 self.statistics["workedTime"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".workedTime"), 0)
157 self.statistics["cultivatedTime"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".cultivatedTime"), 0)
158 self.statistics["sownTime"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".sownTime"), 0)
159 self.statistics["fertilizedTime"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".fertilizedTime"), 0)
160 self.statistics["threshedTime"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".threshedTime"), 0)
161 self.statistics["weededTime"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".weededTime"), 0)
162 self.statistics["sprayedTime"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".sprayedTime"), 0)
163 self.statistics["plowedTime"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".plowedTime"), 0)
164
165 self.statistics["baleCount"].total = Utils.getNoNil(getXMLInt(xmlFile, key .. ".baleCount"), 0)
166
167 self.statistics["breedCowsCount"].total = Utils.getNoNil(getXMLInt(xmlFile, key .. ".breedCowsCount"), 0)
168 self.statistics["breedSheepCount"].total = Utils.getNoNil(getXMLInt(xmlFile, key .. ".breedSheepCount"), 0)
169 self.statistics["breedPigsCount"].total = Utils.getNoNil(getXMLInt(xmlFile, key .. ".breedPigsCount"), 0)
170 self.statistics["breedChickenCount"].total = Utils.getNoNil(getXMLInt(xmlFile, key .. ".breedChickenCount"), 0)
171 self.statistics["breedHorsesCount"].total = Utils.getNoNil(getXMLInt(xmlFile, key .. ".breedHorsesCount"), 0)
172
173 self.statistics["fieldJobMissionCount"].total = Utils.getNoNil(getXMLInt(xmlFile, key .. ".fieldJobMissionCount"), 0)
174 self.statistics["fieldJobMissionByNPC"].total = Utils.getNoNil(getXMLInt(xmlFile, key .. ".fieldJobMissionByNPC"), 0)
175 self.statistics["transportMissionCount"].total = Utils.getNoNil(getXMLInt(xmlFile, key .. ".transportMissionCount"), 0)
176
177 self.statistics["plantedTreeCount"].total = Utils.getNoNil(getXMLInt(xmlFile, key .. ".plantedTreeCount"), 0)
178 self.statistics["cutTreeCount"].total = Utils.getNoNil(getXMLInt(xmlFile, key .. ".cutTreeCount"), 0)
179 self.statistics["woodTonsSold"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".woodTonsSold"), 0)
180 self.statistics["treeTypesCut"] = Utils.getNoNil(getXMLString(xmlFile, key .. ".treeTypesCut"), "000000")
181
182 self.statistics["revenue"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".revenue"), 0)
183 self.statistics["expenses"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".expenses"), 0)
184
185 self.statistics["playTime"].total = Utils.getNoNil(getXMLFloat(xmlFile, key .. ".playTime"), 0)
186
187 -- Finances
188 local i = 0
189 while true do
190 local financeKey = string.format("%s.finances.stats(%d)", farmKey, i)
191 local day = getXMLInt(xmlFile, financeKey .. "#day")
192 if day == nil then
193 break
194 end
195
196 local finances = FinanceStats:new()
197 finances:loadFromXMLFile(xmlFile, financeKey)
198
199 if day == 0 then
200 self.finances = finances
201 else
202 table.insert(self.financesHistory, finances)
203 end
204
205 i = i + 1
206 end
207end

merge

Description
Used to merge farms into each other when playing SP on a MP game
Definition
merge()

new

Description
Definition
new()
Code
30function FarmStats:new()
31 local self = {}
32 setmetatable(self, FarmStats_mt)
33
34 -- General statistics
35 self.statistics = {}
36 for _, statName in pairs(FarmStats.STAT_NAMES) do
37 self.statistics[statName] = { session = 0, total = 0 }
38 end
39
40 self.statistics.treeTypesCut = "000000"
41
42 -- Finances
43 self.finances = FinanceStats:new()
44 self.financesHistory = {} -- over time, the finances of past days is put in here
45
46 -- Major achievements
47 self.heroStats = {}
48 for _, heroStat in pairs(FarmStats.HERO_STAT_NAMES) do
49 self.heroStats[heroStat] = { id = nil, value = nil, accumValue = 0 }
50 end
51 self.heroStatsLoaded = false
52 self.moneyEarnedHeroAccum = 0
53 self.nextHeroAccumUpdate = 0
54
55 if g_currentMission:getIsServer() then
56 g_currentMission:addUpdateable(self)
57 end
58
59 -- this is increased whenever the finance stats are changed, so that other code can check if it has the latest state
60 self.financesVersionCounter = 0
61 self.financesHistoryVersionCounter = 0
62 self.financesHistoryVersionCounterLocal = 0
63
64 self.updatePlayTime = true
65
66 return self
67end

saveToXMLFile

Description
Definition
saveToXMLFile()
Code
77function FarmStats:saveToXMLFile(xmlFile, key)
78
79 setXMLFloat(xmlFile, key .. ".statistics.traveledDistance", self.statistics.traveledDistance.total)
80
81 setXMLFloat(xmlFile, key .. ".statistics.fuelUsage", self.statistics.fuelUsage.total)
82 setXMLFloat(xmlFile, key .. ".statistics.seedUsage", self.statistics.seedUsage.total)
83 setXMLFloat(xmlFile, key .. ".statistics.sprayUsage", self.statistics.sprayUsage.total)
84
85 setXMLFloat(xmlFile, key .. ".statistics.workedHectares", self.statistics.workedHectares.total)
86 setXMLFloat(xmlFile, key .. ".statistics.cultivatedHectares", self.statistics.cultivatedHectares.total)
87 setXMLFloat(xmlFile, key .. ".statistics.sownHectares", self.statistics.sownHectares.total)
88 setXMLFloat(xmlFile, key .. ".statistics.fertilizedHectares", self.statistics.fertilizedHectares.total)
89 setXMLFloat(xmlFile, key .. ".statistics.threshedHectares", self.statistics.threshedHectares.total)
90 setXMLFloat(xmlFile, key .. ".statistics.plowedHectares", self.statistics.plowedHectares.total)
91
92 setXMLFloat(xmlFile, key .. ".statistics.workedTime", self.statistics.workedTime.total)
93 setXMLFloat(xmlFile, key .. ".statistics.cultivatedTime", self.statistics.cultivatedTime.total)
94 setXMLFloat(xmlFile, key .. ".statistics.sownTime", self.statistics.sownTime.total)
95 setXMLFloat(xmlFile, key .. ".statistics.fertilizedTime", self.statistics.fertilizedTime.total)
96 setXMLFloat(xmlFile, key .. ".statistics.threshedTime", self.statistics.threshedTime.total)
97 setXMLFloat(xmlFile, key .. ".statistics.plowedTime", self.statistics.plowedTime.total)
98
99 setXMLInt(xmlFile, key .. ".statistics.baleCount", self.statistics.baleCount.total)
100
101 setXMLInt(xmlFile, key .. ".statistics.breedCowsCount", self.statistics.breedCowsCount.total)
102 setXMLInt(xmlFile, key .. ".statistics.breedSheepCount", self.statistics.breedSheepCount.total)
103 setXMLInt(xmlFile, key .. ".statistics.breedPigsCount", self.statistics.breedPigsCount.total)
104 setXMLInt(xmlFile, key .. ".statistics.breedChickenCount", self.statistics.breedChickenCount.total)
105 setXMLInt(xmlFile, key .. ".statistics.breedHorsesCount", self.statistics.breedHorsesCount.total)
106
107 setXMLInt(xmlFile, key .. ".statistics.fieldJobMissionCount", self.statistics.fieldJobMissionCount.total)
108 setXMLInt(xmlFile, key .. ".statistics.fieldJobMissionByNPC", self.statistics.fieldJobMissionByNPC.total)
109 setXMLInt(xmlFile, key .. ".statistics.transportMissionCount", self.statistics.transportMissionCount.total)
110
111 setXMLFloat(xmlFile, key .. ".statistics.revenue", self.statistics.revenue.total)
112 setXMLFloat(xmlFile, key .. ".statistics.expenses", self.statistics.expenses.total)
113 setXMLFloat(xmlFile, key .. ".statistics.playTime", self.statistics.playTime.total)
114
115 setXMLInt(xmlFile, key .. ".statistics.plantedTreeCount", self.statistics.plantedTreeCount.total)
116 setXMLInt(xmlFile, key .. ".statistics.cutTreeCount", self.statistics.cutTreeCount.total)
117 setXMLFloat(xmlFile, key .. ".statistics.woodTonsSold", self.statistics.woodTonsSold.total)
118 setXMLString(xmlFile, key .. ".statistics.treeTypesCut", self.statistics.treeTypesCut)
119
120 local toSave = { self.finances }
121 local numHistoricItems = #self.financesHistory
122 for n = 3, 0, -1 do
123 if numHistoricItems > n then
124 table.insert(toSave, self.financesHistory[numHistoricItems - n])
125 end
126 end
127
128 for i, finances in ipairs(toSave) do
129 local statsKey = string.format("%s.finances.stats(%d)", key, i-1)
130 setXMLInt(xmlFile, statsKey.."#day", i-1)
131 finances:saveToXMLFile(xmlFile, statsKey)
132 end
133end

update

Description
Definition
update()
Code
211function FarmStats:update(dt)
212 if GS_PLATFORM_TYPE == GS_PLATFORM_TYPE_XBOXONE or GS_PLATFORM_TYPE == GS_PLATFORM_TYPE_GGP then
213 -- Check if the stats are now available. If so, update from the queue and empty said queue.
214 if not self.heroStatsLoaded and areStatsAvailable() then
215 self.heroStatsLoaded = true
216 for heroStatName, heroStat in pairs(self.heroStats) do
217 heroStat.id = statsGetIndex(heroStatName)
218 heroStat.value = statsGet(heroStat.id)
219 if heroStat.accumValue ~= 0 then
220 heroStat.value = heroStat.value + heroStat.accumValue
221 statsSet(heroStat.id, heroStat.value)
222 heroStat.accumValue = 0
223 end
224 end
225 end
226
227 -- Update qeued money earned stat to prevent event-rate limit hit
228 if g_time >= self.nextHeroAccumUpdate and self.moneyEarnedHeroAccum > 0 then
229 self:addValueToHeroStat("moneyEarned", self.moneyEarnedHeroAccum)
230 self.moneyEarnedHeroAccum = 0
231 self.nextHeroAccumUpdate = g_time + 10 * 1000
232 end
233 end
234
235 self:updateStats("playTime", dt / (1000 * 60), self.updatePlayTime)
236end

updateFieldJobsDone

Description
Definition
updateFieldJobsDone()
Code
412function FarmStats:updateFieldJobsDone(npcIndex)
413 self:updateStats("fieldJobMissionCount", 1)
414 local npcValue = 2 ^ npcIndex
415 self.statistics["fieldJobMissionByNPC"].total = bitOR(self.statistics["fieldJobMissionByNPC"].total, npcValue)
416end

updateStats

Description
Definition
updateStats()
Code
327function FarmStats:updateStats(statName, delta, ignoreHeroStats)
328 if delta == nil then
329 printCallstack()
330 end
331
332 if self.statistics[statName] ~= nil then
333 self.statistics[statName].session = self.statistics[statName].session + delta
334 if self.statistics[statName].total ~= nil then
335 self.statistics[statName].total = self.statistics[statName].total + delta
336 end
337 else
338 print("Error: Invalid statistic '"..statName.."'")
339 end
340
341 if ignoreHeroStats == nil or not ignoreHeroStats then
342 self:addHeroStat(statName, delta)
343 end
344end

updateTransportJobsDone

Description
Definition
updateTransportJobsDone()
Code
420function FarmStats:updateTransportJobsDone()
421 self:updateStats("transportMissionCount", 1)
422end

updateTreeTypesCut

Description
achievement-specific stat
Definition
updateTreeTypesCut()
Code
385function FarmStats:updateTreeTypesCut(splitTypeName)
386 local trees = {"oak", "birch", "maple", {"spruce", "pine"}, "poplar", "ash"}
387
388 for i, treeName in ipairs(trees) do
389 local treeMatch = false
390 if type(treeName) == "table" then
391 for _, subTreeName in pairs(treeName) do
392 if splitTypeName == subTreeName then
393 treeMatch = true
394 end
395 end
396 else
397 if splitTypeName == treeName then
398 treeMatch = true
399 end
400 end
401
402 -- Set to 1 if tree matches
403 if treeMatch then
404 local stats = self.statistics
405 stats.treeTypesCut = string.sub(stats.treeTypesCut, 1, i - 1) .. "1" .. string.sub(stats.treeTypesCut, i + 1, string.len(stats.treeTypesCut))
406 end
407 end
408end