Skip to content

Commit

Permalink
Merge pull request #1 from BigFood2307/feature/eas-update-2-2-0-0
Browse files Browse the repository at this point in the history
Added compatibility with new eas lactation cycles
  • Loading branch information
BigFood2307 authored Sep 6, 2023
2 parents 1673810 + 0b6bd05 commit dac84f9
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 15 deletions.
103 changes: 89 additions & 14 deletions AnimalFoodCalculator.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
AnimalFoodCalculator = {}
AnimalFoodCalculator.name = g_currentModName
AnimalFoodCalculator.modDir = g_currentModDirectory
AnimalFoodCalculator.useEas = false


function AnimalFoodCalculator:onListSelectionChanged(husbandry, cluster)
local overallMonthly = 0
Expand Down Expand Up @@ -43,34 +45,66 @@ function AnimalFoodCalculator:calcFoodPerCluster(cluster)
local subType = g_currentMission.animalSystem.subTypes[subTypeIdx]
local food = subType.input["food"]

local firstMonth = AnimalFoodCalculator:calcFoodByAge(food, age)
local firstMonth = AnimalFoodCalculator:calcFoodByAge(cluster, 0)
local wholeYear = firstMonth

for extraMonth = 1,11 do
wholeYear = wholeYear + AnimalFoodCalculator:calcFoodByAge(food, age+extraMonth)
wholeYear = wholeYear + AnimalFoodCalculator:calcFoodByAge(cluster, extraMonth)
end

return firstMonth*num, wholeYear*num
end

function AnimalFoodCalculator:calcFoodByAge(food, age)
if age >= food.maxTime then
return food.keyframes[food.numKeyframes][1]
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 idx = 1
local amount = food:get(age + extraMonths)
local factor = 1.0

while food.keyframes[idx]["time"] <= age do
idx = idx + 1
if AnimalFoodCalculator.useEas then
-- caluclate food factor for animals
factor = AnimalFoodCalculator:lactationFactorForAnimalType(cluster, extraMonths)
end

local factor = 1-((age - food.keyframes[idx-1]["time"])/(food.keyframes[idx]["time"] - food.keyframes[idx-1]["time"]))

return AnimalFoodCalculator:interpolateFood(food.keyframes[idx-1], food.keyframes[idx], factor, food.interpolatorDegree)
return amount * factor
end

function AnimalFoodCalculator:interpolateFood(kf1, kf2, factor, degree)
return (factor^degree)*kf1[1] + (1-(factor^degree))*kf2[1]
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)
Expand All @@ -81,5 +115,46 @@ function AnimalFoodCalculator:getText(key)
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)

14 changes: 13 additions & 1 deletion modDesc.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8" standalone="no" ?>
<modDesc descVersion="77">
<author>BigFood2307</author>
<version>1.0.0.0</version>
<version>1.0.0.1</version>

<title>
<en>Animal Food Calculator</en>
Expand All @@ -18,6 +18,13 @@ The yearly estimate includes the fact, that animals will eat more when grown up.
Supports MaizePlus and Enhanced Animal System.
Have fun feeding!
Change log:
*v1.0.0.1
- Improved compatibility with Enhanced Animal System v2.2.0.0.
- Lactation Cycles are now included when calculating food amounts.
- 1 Year amount is still just a (good) approximation, since insemination could fail.
]]>
</en>
<de>
Expand All @@ -29,6 +36,11 @@ Die jährliche Schätzung beinhaltet das Ansteigen der Futtermenge mit dem Alter
Funktioniert mit MaizePlus und Enhanced Animal System.
Viel Spaß beim Füttern!
*v1.0.0.1
- Vermesserte Kompatibilität mit Enhanced Animal System v2.2.0.0.
- Laktationszyklen werden nun beim Berechnen der Futtermenge berücksichtigt.
- 1 Jahres Vorhersage ist dennoch eine Annäherung, da die Besamung fehlschlagen könnte.
]]>
</de>
</description>
Expand Down

0 comments on commit dac84f9

Please sign in to comment.