diff --git a/AnimalFoodCalculator.lua b/AnimalFoodCalculator.lua index fa2a7a2..5bb7c78 100644 --- a/AnimalFoodCalculator.lua +++ b/AnimalFoodCalculator.lua @@ -2,6 +2,8 @@ AnimalFoodCalculator = {} AnimalFoodCalculator.name = g_currentModName AnimalFoodCalculator.modDir = g_currentModDirectory +AnimalFoodCalculator.useEas = false + function AnimalFoodCalculator:onListSelectionChanged(husbandry, cluster) local overallMonthly = 0 @@ -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) @@ -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) diff --git a/modDesc.xml b/modDesc.xml index dc933f2..ce439d3 100644 --- a/modDesc.xml +++ b/modDesc.xml @@ -1,7 +1,7 @@ BigFood2307 - 1.0.0.0 + 1.0.0.1 <en>Animal Food Calculator</en> @@ -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> @@ -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>