-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAnimalFoodCalculator.lua
160 lines (125 loc) · 5.85 KB
/
AnimalFoodCalculator.lua
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
AnimalFoodCalculator = {}
AnimalFoodCalculator.name = g_currentModName
AnimalFoodCalculator.modDir = g_currentModDirectory
AnimalFoodCalculator.useEas = false
function AnimalFoodCalculator:onListSelectionChanged(husbandry, cluster)
local overallMonthly = 0
local overallYearly = 0
for _, cluster in pairs(self.selectedHusbandry:getClusters()) do
local monthly, yearly = AnimalFoodCalculator:calcFoodPerCluster(cluster)
overallMonthly = overallMonthly + monthly
overallYearly = overallYearly + yearly
end
local daysPerMonth = g_currentMission.environment.daysPerPeriod
local firstRun = (self.dailyFoodLabel == nil)
if firstRun then
self.dailyFoodLabel = self.foodHeader:clone()
self.dailyFoodLabel.textAutoWidth = false
self.yearlyFoodLabel = self.foodHeader:clone()
self.yearlyFoodLabel.textAutoWidth = false
end
if firstRun then
self.requirementsLayout:addElement(self.dailyFoodLabel)
self.requirementsLayout:addElement(self.yearlyFoodLabel)
end
self.dailyFoodLabel:setText(AnimalFoodCalculator:getText("AFC_DailyLabel") .. ": " .. g_i18n:formatVolume(overallMonthly/daysPerMonth, 0))
self.yearlyFoodLabel:setText(AnimalFoodCalculator:getText("AFC_YearlyLabel") .. ": " .. g_i18n:formatVolume(overallYearly, 0))
self.requirementsLayout:invalidateLayout()
end
function AnimalFoodCalculator:calcFoodPerCluster(cluster)
local age = cluster.age
local num = cluster.numAnimals
local subTypeIdx = cluster.subTypeIndex
local subType = g_currentMission.animalSystem.subTypes[subTypeIdx]
local food = subType.input["food"]
local firstMonth = AnimalFoodCalculator:calcFoodByAge(cluster, 0)
local wholeYear = firstMonth
for extraMonth = 1,11 do
wholeYear = wholeYear + AnimalFoodCalculator:calcFoodByAge(cluster, extraMonth)
end
return firstMonth*num, wholeYear*num
end
function AnimalFoodCalculator:calcFoodByAge(cluster, extraMonths)
local age = cluster.age
local subTypeIdx = cluster.subTypeIndex
local subType = g_currentMission.animalSystem.subTypes[subTypeIdx]
local food = subType.input["food"]
local amount = food:get(age + extraMonths)
local factor = 1.0
if AnimalFoodCalculator.useEas then
-- caluclate food factor for animals
factor = AnimalFoodCalculator:lactationFactorForAnimalType(cluster, extraMonths)
end
return amount * factor
end
function AnimalFoodCalculator:lactationFactorForAnimalType(cluster, extraMonths)
local subTypeIdx = cluster.subTypeIndex
local subType = g_currentMission.animalSystem.subTypes[subTypeIdx]
local animalType = g_currentMission.animalSystem:getTypeByIndex(subType.typeIndex)
local animalTypeIndex = animalType.typeIndex
local foodFactor = 1.0
local values = {}
if animalTypeIndex == AnimalType.PIG then
values = AnimalFoodCalculator.EASSettings.PigFoodFactor
elseif animalTypeIndex == AnimalType.COW then
values = AnimalFoodCalculator.EASSettings.CowFoodFactor
elseif animalTypeIndex == AnimalType.HORSE then
values = AnimalFoodCalculator.EASSettings.HorseFoodFactor
elseif animalTypeIndex == AnimalType.SHEEP then
values = AnimalFoodCalculator.EASSettings.SheepFoodFactor
elseif animalTypeIndex == AnimalType.CHICKEN then
values = AnimalFoodCalculator.EASSettings.ChickenFoodFactor
end
if cluster:getCanReproduce() then
local reproductiveMonths = extraMonths - (subType.reproductionMinAgeMonth - cluster.age)
local actualReproduction = cluster.reproduction + (100/subType.reproductionDurationMonth)*reproductiveMonths
local actualHadABirth = cluster.hadABirth or actualReproduction >= 100
-- assumes perfect, instant insemination
local actualMonthsSinceLastBirth = (cluster.monthsSinceLastBirth + reproductiveMonths)%subType.reproductionDurationMonth
if actualHadABirth and actualMonthsSinceLastBirth < #values then
foodFactor = values[actualMonthsSinceLastBirth + 1]
end
end
return foodFactor
end
function AnimalFoodCalculator:getText(key)
local result = g_i18n.modEnvironments[AnimalFoodCalculator.name].texts[key]
if result == nil then
return g_i18n:getText(key)
end
return result
end
function AnimalFoodCalculator:loadedMission()
if g_modIsLoaded["FS22_EnhancedAnimalSystem"] then
AnimalFoodCalculator:loadEASXml()
end
end
function AnimalFoodCalculator:loadEASXml()
local path = AnimalFoodCalculator.modDir .. "../FS22_EnhancedAnimalSystem/xml/eas_settings.xml"
local key = "EnhancedAnimalSystem.Settings"
local xmlFileId = loadXMLFile("EAS_Utils", path)
-- check for actually existing settings file
if xmlFileId == nil then
return
end
-- make sure the installed version actually has those settings
if getXMLString(xmlFileId, key.."#PigFoodFactor") == nil then
return
end
AnimalFoodCalculator.useEas = true
AnimalFoodCalculator.EASSettings = {}
AnimalFoodCalculator.EASSettings.PigFoodFactor = AnimalFoodCalculator:sliceByKomma(getXMLString(xmlFileId, key.."#PigFoodFactor"))
AnimalFoodCalculator.EASSettings.CowFoodFactor = AnimalFoodCalculator:sliceByKomma(getXMLString(xmlFileId, key.."#CowFoodFactor"))
AnimalFoodCalculator.EASSettings.HorseFoodFactor = AnimalFoodCalculator:sliceByKomma(getXMLString(xmlFileId, key.."#HorseFoodFactor"))
AnimalFoodCalculator.EASSettings.SheepFoodFactor = AnimalFoodCalculator:sliceByKomma(getXMLString(xmlFileId, key.."#SheepFoodFactor"))
AnimalFoodCalculator.EASSettings.ChickenFoodFactor = AnimalFoodCalculator:sliceByKomma(getXMLString(xmlFileId, key.."#ChickenFoodFactor"))
end
function AnimalFoodCalculator:sliceByKomma(text)
local values = {}
for value in text:gmatch('[^,%s]+') do
table.insert(values, value)
end
return values
end
InGameMenuAnimalsFrame.onListSelectionChanged = Utils.appendedFunction(InGameMenuAnimalsFrame.onListSelectionChanged, AnimalFoodCalculator.onListSelectionChanged)
Mission00.loadMission00Finished = Utils.appendedFunction(Mission00.loadMission00Finished, AnimalFoodCalculator.loadedMission)