--
-- Mod: FS19_RealLifeNumbers by Kaj-Aage Henneberg "Ki", ArmChairFarming
-- Script: RealNumbersAnimalCare by Kaj-Aage Henneberg "Ki", ArmChairFarming
-- e-mail: kaj-aage.henneberg@outlook.dk
-- Download: www.modhoster.com
-- All rights reserved.
-- Version 1.1.4.0
-- 30.11.2018 - 29.06.2019

-- Names for FillTypes

-- WHEAT BARLEY OAT CANOLA SUNFLOWER SOYBEAN MAIZE POTATO SUGARBEET OILSEEDRADISH
-- MANURE SEEDS FORAGE FORAGE_MIXING CHAFF WOODCHIPS SILAGE
-- STRAW GRASS_WINDROW DRYGRASS_WINDROW SUGARCANE FERTILIZER PIGFOOD LIME
-- WOOL TREESAPLINGS EGG
-- MILK FUEL WATER DIESEL DEF
-- LIQUIDFERTILIZER HERBICIDE
-- CHAFF GRASS_WINDROW
-- LIQUIDMANURE DIGESTATE
-- COW_TYPE_BRAHMAN_GREY COW_TYPE_BRAHMAN_LIGHT_BROWN COW_TYPE_BRAHMAN_WHITE COW_TYPE_BRAHMAN_BROWN
-- COW_TYPE_BROWN COW_TYPE_BROWN_WHITE COW_TYPE_BLACK COW_TYPE_BLACK_WHITE
-- SHEEP_TYPE_WHITE SHEEP_TYPE_BROWN SHEEP_TYPE_BLACK_WHITE SHEEP_TYPE_BLACK
-- PIG_TYPE_RED PIG_TYPE_WHITE PIG_TYPE_BLACK_WHITE PIG_TYPE_BLACK
-- CHICKEN_TYPE_BLACK CHICKEN_TYPE_WHITE CHICKEN_TYPE_BROWN CHICKEN_TYPE_ROOSTER


--[[

	Version 1.1
	
	No change.


Animal care

	Animal care involves:
		Food intake per animal per game day: water, food, straw
		Output per animal per gameday: Milk, wool, eggs, manuare, liquid manure, food spillage
		Breeding: Birthrate per game day per animal. The effective birth rate declines as the number
		of animals approach the maximum capacity of the pen.

		Animals are fictively divided into breedding animals and offspring, and offspring are again divided into younger
		and older growers. The total amount of milk, wool, and eggs is calculated based on the number of mature (breeding) animals.
		FS19 assumes all animals produce milk, wool, and eggs. Hence we need to downscale the yield per animal by dividing total yield
		per game day by the mean number of animals in a breeding cycle.

		There are in general four subtypes of each animal. The mod uses this to create different types of husbandries:

		Pigs:

			1. Standard: Mature and offspring live in the same pen, and all breed.
			2. Weaners: Mature and preweaners live inthe same pen, and all breed.
			3. Growers: Weaners are moved to the growers pen nd live there without breeding
			4. Finishers: Growers are moved to the finishers pen and live there without breeding.

		The features of an animal is associated with its subtype (red, white, black, black-white) and not with the pen. Hence
		when a weaner (white) is moved to the growers pen, it must change color from white to black-white. Istead of physically
		moving the animal, the weaners are simply sold at the weaners pen, and the growers are bought at the same priceat
		the growers pen. For this reason the transport fee is set to zero.

		If red/pnk igs are put in a pen, the husbandry is the standard style of FS19. Offspring are sold from the same pen
		as where they are born. In real life, this type of pig farming represents ery small scale pig breeding.

		For large scale pig breeding, you need 3 different pens, one for weaners, one for growers and one for finishers.
		Straw, water and feed consumption as well as waste production and buy and sell prices are all based on the range
		of weight of the animal taken from real-life weight charts.

		In breeding pens, breeding rate is downscaled in a similar fashion to compensate for the fact that all animals
		breed in FS19.

		For cows there are also 4 different pen types:

			1. Standard: Mature and offspring live in the same pen, and all breed and produce milk.
			2: Milkers:  Mature live in the same pen, and all breed and produce milk. Calves are instantly moved.
			3: 1st year calves: For each cow in the Milkers pen, a calf will be born in this pen. No milk is produced.
			4: 2nd year calves: In this pen heifers are grown to replaces milkers. F. ex. 1/5 of the number of milkers. No milk.

		For a small scale dairy farm, use the standard pen. For intensive dairy farming, 3 pens are needed.

		New born calves are removed from the dam 2-3 days after birth, whereas piglets stay several weeks before weaned.
		For this reason, the approch is different for calves. The mod does not breed the calves in the milkerspen but in
		the growers pen. There must therefore be ONE grower animal (black) in the growers pen at the start.

		For sheep there are 4 different pen types:

			1. Standard: Mature and offspring live in the same pen, and all breed and produce wool.
			2. Preweaned: Offspring stays in the pen for 4 months. Breeding and wool production is downscaled
			3. Growers: Preweaned ar moved to this pen at 4 months. No breeding or wool production
			4. Replacers: Growers are move to this pen at 8 months of age. No breeding or wool production.

		As for the other animals, sheep are not physically moved between pens, but sold and bought at the same price
		at the respective pens.

		For chicken there are 4 different pen types:

			1. Standard: Mature and offspring live in the same pen, and all breed and produce egg.
			2: Hatchers: These are 1 day old chicks, bought into the pen and stay there for 4 months.
			3. Layers: These are moved from the hatchers pen at 4 months. They lay egg, but do not breed.
			4. Broilers: These are 1 day old chicks bougth into the pen. They are sold to slaughter after 4 months. No eggs, no breeding.

		For a given animal the following are defined:
			number of breeding animals/growers
			litter size for breeding animals
			number of litters per year
			pen capacity
			accumulated intake of water and feed per year for breeding animals and for offsprings
			accumulated output of milk, egg, and wool per year for breeding animals
			accumulated output of manure for breeding animals and for offsprings

		From these numbers we calculate:
			The number of offsprings
			The final number of animals at the end of the breeding cycle
			Average number of animals in the pen in one breeding cycle
			The birth rate per day
			The feed and water amount per animal per day
			The milk, egg, wool amount per animal per day
			The total feed intake per year
			The total intake of feed components per year in liters and in number of bales
			The field size required to produce the consumed feed components

Game days

		The script operates with the concept of a year and a gameday. As default there are 12 gamedays in a year and a
		year is set to 360 days. This makes the default gameday correspond to 30 rel life days.
		The reason for this is that most real life parameters are annual values, such as milk yield, and number of litters per
		breeding animal.

Input and output

		Cows and sheep eat a lot more than would normally be given in a trough. Therefore it is possible to set the feed fraction
		to begiven in a trough (RN.cows.troughFeedFraction, RN.sheep.troughFeedFraction).
		This can be adjusted.

		Farm animals drink a lot more water than would normally be delivered in a bowser. Therefore it is assumed that most
		of the drinking water comes from an automatic system. The amount given with a water bowser (tank) is set to 10%
		(RN.*****.bowserWaterFraction = 0.1).

		Mean number of animals, birthrate, and time to first birth are calculated using a mathematical model that
		takes into account that the breeding is self-limiting, meaning that the birth rate will go toward zero as the number
		of animals approach the pen capacity.


Final comment

	If you want to reduce the effect of pen size on the reproduction rate, you should use a pen with a capacity much larger than
	your number of animals in the pen.

	The predicted numbers are only valid if you sell all the offspring at the end of the pen-stay durtion.

	Hopefully Seasons will soon be available for FS19. When this happens, this script will most likely interfere with the settings
	in Seasons. The first solution is to comment out the call to this script in the modDesc.xml file.
]]--


RealNumbersAnimalCare = {};


function myPrintTimeStamp(text1, text2, textWidth)
	local stringAppend1 = string.rep(" ",textWidth-string.len(text1));
	local stringAppend2 = string.rep(" ",8-string.len(text2));
	print(string.format("%s =  %s", text1..stringAppend1,stringAppend2..text2));
end;
function timeStamp(timeInSeconds)
	local hour = tostring(math.floor(timeInSeconds/3600));
	local minutes = tostring(math.floor((timeInSeconds - hour*3600)/60));
	local seconds = tostring(math.floor(timeInSeconds - hour*3600 - minutes*60));
	if #hour < 2 then hour = "0"..hour; end;
	if #minutes < 2 then minutes = "0"..minutes; end;
	if #seconds < 2 then seconds = "0"..seconds; end;

	return tostring(hour..":"..minutes..":"..seconds);
end;


function RealNumbersAnimalCare:loadMap(name)
    local textWidth = 40; 	-- text width in log file
    local iFillType = 0;
	local iFruitType = 0;
	local Animal= "Empty";
	local myAnimalType = {};
	local gameDays = {};
	local penCapacity = {};
	local rearingLitter = {};
	local breeders = {};
	local pig = {};
	local cow = {};
	local sheep = {};
	local chicken = {};
	local horse = {};
	local general = {};
	local crop = {};
	local bales = {};
	local idx = 0;
	local pigColor ={};
	pigColor = RN.pigs.color;


	local isDebug = RN.isDebug;  		-- Change to false when not debugging
	local isDebug1 = false; 			-- Change to false when not wanting fillTypes to be dumped to log file.
	local isDebug2 = false;  			-- Change to false when not wanting to dump tables to log file
	local isDebug3 = false;				-- Change to false when not wanting to dump file and names to log file

	local pen = {};

	local moddir = RN.moddir;

	AnimalHusbandry.GAME_LIMIT = 20;		-- Increase the number of animal pens from 10 to 20

	pen.pigPenNumber = 0;
	pen.cowPenNumber = 0;
	pen.sheepPenNumber = 0;
	pen.chickenPenNumber = 0;
	pen.horsePenNumber = 0;
	pen.pigMax = {};
	pen.cowMax = {};
	pen.sheepMax = {};
	pen.chickenMax = {};
	pen.horseMax = {};
	pen.pigText = {};
	pen.cowText = {};
	pen.sheepText = {};
	pen.chickenText = {};
	pen.horseText = {};

	print("\nFS19_RealLifeNumbers: RealNumbersAnimalCare active\n")

	local savegameDirectory = g_currentMission.missionInfo.savegameDirectory;	-- works only after savegame is present
	local itemsFile = g_currentMission.missionInfo.itemsXML;					-- works only after savegame is present
--	local baseDirectory = g_currentMission.missionInfo.baseDirectory;			-- works but often not set
	if isDebug3 then
		print("savegameDirectory : "..tostring(savegameDirectory));
		print("itemsFile : "..tostring(itemsFile));
		print("current moddir : "..tostring(RealLifeNumbersDirectory));
		print("default mod folder = "..moddir);
	end;


	gameDays.gameDaysPerYear 					= RN.gameDaysPerYear;  	-- Number of game days per year
	gameDays.realDaysPerGameDay 				= 360/gameDays.gameDaysPerYear;
	gameDays.pigRearingGameDays 				= gameDays.gameDaysPerYear/2;
	gameDays.cowRearingGameDays 				= gameDays.gameDaysPerYear;
	gameDays.sheepRearingGameDays 				= gameDays.gameDaysPerYear;
	gameDays.chickenRearingGameDays 			= gameDays.gameDaysPerYear/4;
	gameDays.horseRearingGameDays 				= gameDays.gameDaysPerYear;
	general.waterBowserFraction 				= RN.waterBowserFraction;
	general.gameCropYieldCorrection 			= RN.fieldWorkbaseYieldAdjustment;	-- scale factor to obtain base yield from bonus yield
	general.manureDensity 						= RN.commodityDensity.MANURE;
	general.liquidManureDensity 				= RN.commodityDensity.LIQUIDMANURE;
	general.feedSpillageFraction 				= RN.feedSpillageFraction;
	general.strawBaleVolume 					= RN.commodityVolume.ROUNDBALE_WHEAT;
	general.strawBaleDensity 					= RN.commodityDensity.ROUNDBALE_WHEAT ;
	general.freshGrassDryMatterFraction 		= 0.15;		-- dry matter mass fraction of grazzing grass
	general.grassBaleDensity 					= RN.commodityDensity.ROUNDBALE_GRASS;
	general.grassBaleVolume 					= RN.commodityVolume.ROUNDBALE_GRASS;
	general.dryGrassDryMatterFraction 			= 0.85;		-- dry matter mass fraction of hay
	general.hayBaleDensity 						= RN.commodityDensity.ROUNDBALE_DRYGRASS;
	general.hayBaleVolume 						= RN.commodityVolume.ROUNDBALE;
	general.silageBaleVolume					= RN.commodityVolume.ROUNDBALE;
	general.oatDensity							= RN.commodityDensity.OAT;
	general.milkDensity                         = RN.animalProductDensity[1];
	general.woolDensity                         = RN.animalProductDensity[2];
	general.eggDensity                          = RN.animalProductDensity[3];

	pig.foodDensity                             = RN.commodityDensity.PIGFOOD;	-- kg/L
	cow.foodDensity 							= RN.commodityDensity.FORAGE_MIXING; 	-- kg/L
	sheep.foodDensity                           = RN.commodityDensity.FORAGE; 	-- kg/L
	chicken.foodDensity                         = RN.commodityDensity.WHEAT;	-- kg/L
	cow.strawTMRFraction 						= 0.25;		-- Straw fraction of TMW mix
	cow.hayTMRFraction 							= 0.5;		-- Hay fraction of TMR mix
	cow.silageTMRFraction 						= 0.25;		-- Silage fraction of TMR mix

-- dirt
	cow.CleanDuration =     500;
	pig.CleanDuration =     500;
	sheep.CleanDuration =   500;
	chicken.CleanDuration = 500;
	horse.CleanDuration =   500;

-- Litter size

	pig.rearingLitter =      13;
	cow.rearingLitter =       1;
	sheep.rearingLitter =  1.25;
	chicken.rearingLitter =   7;

	pig.penCapacity = {};
	pig.startNumber = {};
	pig.numberOfOffspring = {};
	pig.growers = {};
	pig.endNumber = {};
	pig.birthRatePerDay = {};
	pig.meanNumber = {};
	pig.timeToFirstBirth = {};
	pig.gamedaysInPen = {};
	pig.matureStrawPerDay = {};
	pig.growerStrawPerDay = {};
	pig.strawPerMatureAnimalInPen = {};
	pig.strawPerGrowerAnimalInPen = {};
	pig.strawMatureTotalPerPenPeriod = {};
	pig.strawGrowerTotalPerPenPeriod = {};
	pig.totalStrawPerPenPeriod = {};
	pig.totalStrawVolumePerYear = {};
	pig.strawPerAnimalPerGameday = {};
	pig.beddingBales = {};
	pig.neededBeddingWheatFieldPerYear = {};
	pig.neededBeddingBarleyFieldPerYear = {};
	pig.neededBeddingOatFieldPerYear = {};
	pig.matureWaterPerDay = {};
	pig.growerWaterPerDay = {};
	pig.waterPerMatureAnimalInPen = {};
	pig.waterPerGrowerAnimalInPen = {};
	pig.waterMatureTotalPerPenPeriod = {};
	pig.waterGrowerTotalPerPenPeriod = {};
	pig.totalWaterPerPenPeriod = {};
	pig.totalWaterVolumePerYear = {};
	pig.totalBowserWaterPerYear = {};
	pig.waterPerAnimalPerGameday = {};
	pig.matureFeedPerDay = {};
	pig.growerFeedPerDay = {};
	pig.feedPerMatureAnimalInPen = {};
	pig.feedPerGrowerAnimalInPen = {};
	pig.feedMatureTotalPerPenPeriod = {};
	pig.feedGrowerTotalPerPenPeriod = {};
	pig.totalFeedPerPenPeriod = {};
	pig.totalFeedVolumePerYear = {};
	pig.totalTroughFeedPerYear = {};
	pig.feedPerAnimalPerGameday = {};
	pig.totalFoodGrainPerYear = {};
	pig.totalFoodMaizePerYear = {};
	pig.totalFoodProteinPerYear = {};
	pig.totalFoodRootCropPerYear = {};
	pig.neededWheatHa = {};
	pig.neededBarleyHa = {};
	pig.neededMaizeHa = {};
	pig.neededCanolaHa = {};
	pig.neededSunflowerHa = {};
	pig.neededSoybeanHa = {};
	pig.neededPotatoHa = {};
	pig.neededSugarbeetHa = {};
	pig.matureManurePerDay = {};
	pig.growerManurePerDay = {};
	pig.manurePerMatureAnimalInPen = {};
	pig.manurePerGrowerAnimalInPen = {};
	pig.manureMatureTotalPerPenPeriod = {};
	pig.manureGrowerTotalPerPenPeriod = {};
	pig.totalManurePerPenPeriod = {};
	pig.totalManureVolumePerYear = {};
	pig.manurePerAnimalPerGameday = {};
	pig.matureSlurryPerDay = {};
	pig.growerSlurryPerDay = {};
	pig.slurryPerMatureAnimalInPen = {};
	pig.slurryPerGrowerAnimalInPen = {};
	pig.slurryMatureTotalPerPenPeriod = {};
	pig.slurryGrowerTotalPerPenPeriod = {};
	pig.totalSlurryPerPenPeriod = {};
	pig.totalSlurryVolumePerYear = {};
	pig.slurryPerAnimalPerGameday = {};
	pig.feedSpillagePerDay = {};

	cow.penCapacity = {};
	cow.startNumber = {};
	cow.numberOfOffspring = {};
	cow.growers = {};
	cow.endNumber = {};
	cow.birthRatePerDay = {};
	cow.meanNumber = {};
	cow.timeToFirstBirth = {};
	cow.gamedaysInPen = {};
	cow.matureStrawPerDay = {};
	cow.growerStrawPerDay = {};
	cow.strawPerMatureAnimalInPen = {};
	cow.strawPerGrowerAnimalInPen = {};
	cow.strawMatureTotalPerPenPeriod = {};
	cow.strawGrowerTotalPerPenPeriod = {};
	cow.totalStrawPerPenPeriod = {};
	cow.totalStrawVolumePerYear = {};
	cow.strawPerAnimalPerGameday = {};
	cow.beddingBales = {};
	cow.neededBeddingWheatFieldPerYear = {};
	cow.neededBeddingBarleyFieldPerYear = {};
	cow.neededBeddingOatFieldPerYear = {};
	cow.matureWaterPerDay = {};
	cow.growerWaterPerDay = {};
	cow.waterPerMatureAnimalInPen = {};
	cow.waterPerGrowerAnimalInPen = {};
	cow.waterMatureTotalPerPenPeriod = {};
	cow.waterGrowerTotalPerPenPeriod = {};
	cow.totalWaterPerPenPeriod = {};
	cow.totalWaterVolumePerYear = {};
	cow.totalBowserWaterPerYear = {};
	cow.waterPerAnimalPerGameday = {};
	cow.matureFeedPerDay = {};
	cow.growerFeedPerDay = {};
	cow.feedPerMatureAnimalInPen = {};
	cow.feedPerGrowerAnimalInPen = {};
	cow.feedMatureTotalPerPenPeriod = {};
	cow.feedGrowerTotalPerPenPeriod = {};
	cow.totalFeedPerPenPeriod = {};
	cow.totalFeedVolumePerYear = {};
	cow.totalTroughFeedPerYear = {};
	cow.feedPerAnimalPerGameday = {};
	cow.strawFoodPerYear = {};
	cow.hayFoodPerYear = {};
	cow.silageFoodPerYear = {};
	cow.neededWheatHa = {};
	cow.neededBarleyHa = {};
	cow.neededOatHa = {};
	cow.strawBalesPerYear = {};
	cow.hayBalesPerYear = {};
	cow.silageBalesPerYear = {};
	cow.hayFeedHa = {};
	cow.silageFeedHa = {};
	cow.matureManurePerDay = {};
	cow.growerManurePerDay = {};
	cow.manurePerMatureAnimalInPen = {};
	cow.manurePerGrowerAnimalInPen = {};
	cow.manureMatureTotalPerPenPeriod = {};
	cow.manureGrowerTotalPerPenPeriod = {};
	cow.totalManurePerPenPeriod = {};
	cow.totalManureVolumePerYear = {};
	cow.manurePerAnimalPerGameday = {};
	cow.matureSlurryPerDay = {};
	cow.growerSlurryPerDay = {};
	cow.slurryPerMatureAnimalInPen = {};
	cow.slurryPerGrowerAnimalInPen = {};
	cow.slurryMatureTotalPerPenPeriod = {};
	cow.slurryGrowerTotalPerPenPeriod = {};
	cow.totalSlurryPerPenPeriod = {};
	cow.totalSlurryVolumePerYear = {};
	cow.slurryPerAnimalPerGameday = {};
	cow.matureMilkPerDay = {};
	cow.milkPerMatureAnimalInPen = {};
	cow.totalMilkPerPenPeriod = {};
	cow.totalMilkVolumePerYear = {};
	cow.milkPerAnimalPerGameday = {};
	cow.feedSpillagePerDay = {};

	sheep.penCapacity = {};
	sheep.startNumber = {};
	sheep.numberOfOffspring = {};
	sheep.growers = {};
	sheep.endNumber = {};
	sheep.birthRatePerDay = {};
	sheep.meanNumber = {};
	sheep.timeToFirstBirth = {};
	sheep.gamedaysInPen = {};
	sheep.matureWaterPerDay = {};
	sheep.growerWaterPerDay = {};
	sheep.waterPerMatureAnimalInPen = {};
	sheep.waterPerGrowerAnimalInPen = {};
	sheep.waterMatureTotalPerPenPeriod = {};
	sheep.waterGrowerTotalPerPenPeriod = {};
	sheep.totalWaterPerPenPeriod = {};
	sheep.totalWaterVolumePerYear = {};
	sheep.totalBowserWaterPerYear = {};
	sheep.waterPerAnimalPerGameday = {};
	sheep.matureFeedPerDay = {};
	sheep.growerFeedPerDay = {};
	sheep.feedPerMatureAnimalInPen = {};
	sheep.feedPerGrowerAnimalInPen = {};
	sheep.feedMatureTotalPerPenPeriod = {};
	sheep.feedGrowerTotalPerPenPeriod = {};
	sheep.totalFeedPerPenPeriod = {};
	sheep.totalFeedVolumePerYear = {};
	sheep.totalTroughFeedPerYear = {};
	sheep.feedPerAnimalPerGameday = {};
	sheep.hayFeedPerYear = {};
	sheep.grassFeedPerYear = {};
	sheep.hayBalesPerYear = {};
	sheep.grassBalesPerYear = {};
	sheep.hayFeedHa = {};
	sheep.grassFeedHa = {};
	sheep.matureWoolPerDay = {};
	sheep.growerWoolPerDay = {};
	sheep.woolPerMatureAnimalInPen = {};
	sheep.woolPerGrowerAnimalInPen = {};
	sheep.woolMatureTotalPerPenPeriod = {};
	sheep.woolGrowerTotalPerPenPeriod = {};
	sheep.totalWoolPerPenPeriod = {};
	sheep.totalWoolVolumePerYear = {};
	sheep.woolPalletsPerYear = {};
	sheep.woolPalletsPerGameday = {};
	sheep.feedSpillagePerDay = {};

-- some parameters do not apply to chicken at this moment and are not used

	chicken.penCapacity = {};
	chicken.startNumber = {};
	chicken.numberOfOffspring = {};
	chicken.growers = {};
	chicken.endNumber = {};
	chicken.birthRatePerDay = {};
	chicken.meanNumber = {};
	chicken.timeToFirstBirth = {};
	chicken.gamedaysInPen = {};
	chicken.matureStrawPerDay = {};
	chicken.growerStrawPerDay = {};
	chicken.strawPerMatureAnimalInPen = {};
	chicken.strawPerGrowerAnimalInPen = {};
	chicken.strawMatureTotalPerPenPeriod = {};
	chicken.strawGrowerTotalPerPenPeriod = {};
	chicken.totalStrawPerPenPeriod = {};
	chicken.totalStrawVolumePerYear = {};
	chicken.strawPerAnimalPerGameday = {};
	chicken.beddingBales = {};
	chicken.neededBeddingWheatFieldPerYear = {};
	chicken.neededBeddingBarleyFieldPerYear = {};
	chicken.neededBeddingOatFieldPerYear = {};
	chicken.matureWaterPerDay = {};
	chicken.growerWaterPerDay = {};
	chicken.waterPerMatureAnimalInPen = {};
	chicken.waterPerGrowerAnimalInPen = {};
	chicken.waterMatureTotalPerPenPeriod = {};
	chicken.waterGrowerTotalPerPenPeriod = {};
	chicken.totalWaterPerPenPeriod = {};
	chicken.totalWaterVolumePerYear = {};
	chicken.totalBowserWaterPerYear = {};
	chicken.waterPerAnimalPerGameday = {};
	chicken.matureFeedPerDay = {};
	chicken.growerFeedPerDay = {};
	chicken.feedPerMatureAnimalInPen = {};
	chicken.feedPerGrowerAnimalInPen = {};
	chicken.feedMatureTotalPerPenPeriod = {};
	chicken.feedGrowerTotalPerPenPeriod = {};
	chicken.totalFeedPerPenPeriod = {};
	chicken.totalFeedVolumePerYear = {};
	chicken.totalTroughFeedPerYear = {};
	chicken.feedPerAnimalPerGameday = {};
	chicken.totalFoodGrainPerYear = {};
	chicken.totalFoodMaizePerYear = {};
	chicken.totalFoodProteinPerYear = {};
	chicken.totalFoodRootCropPerYear = {};
	chicken.neededWheatHa = {};
	chicken.neededBarleyHa = {};
	chicken.neededOatHa = {};
	chicken.neededCanolaHa = {};
	chicken.neededSunflowerHa = {};
	chicken.neededSoybeanHa = {};
	chicken.neededPotatoHa = {};
	chicken.neededSugarbeetHa = {};
	chicken.matureManurePerDay = {};
	chicken.growerManurePerDay = {};
	chicken.manurePerMatureAnimalInPen = {};
	chicken.manurePerGrowerAnimalInPen = {};
	chicken.manureMatureTotalPerPenPeriod = {};
	chicken.manureGrowerTotalPerPenPeriod = {};
	chicken.totalManurePerPenPeriod = {};
	chicken.totalManureVolumePerYear = {};
	chicken.manurePerAnimalPerGameday = {};
	chicken.matureSlurryPerDay = {};
	chicken.growerSlurryPerDay = {};
	chicken.slurryPerMatureAnimalInPen = {};
	chicken.slurryPerGrowerAnimalInPen = {};
	chicken.slurryMatureTotalPerPenPeriod = {};
	chicken.slurryGrowerTotalPerPenPeriod = {};
	chicken.totalSlurryPerPenPeriod = {};
	chicken.totalSlurryVolumePerYear = {};
	chicken.slurryPerAnimalPerGameday = {};
	chicken.matureEggsPerDay = {};
	chicken.growerEggsPerDay = {};
	chicken.eggsPerMatureAnimalInPen = {};
	chicken.eggsPerGrowerAnimalInPen = {};
	chicken.eggsMatureTotalPerPenPeriod = {};
	chicken.eggsGrowerTotalPerPenPeriod = {};
	chicken.totalEggsPerPenPeriod = {};
	chicken.totalEggVolumePerYear = {};
	chicken.eggPalletsPerYear = {};
	chicken.eggPalletsPerGameday = {};
	chicken.feedSpillagePerDay = {};

	horse.penCapacity = {};

-- Pen capacity

-- default values for pen sizes
-- these values are used to predict birthrate and feed intake
-- if no pens are yet placed on the map.

	pig.gamedaysInPen = RN.pigs.gamedaysInPen;

	for k = 1,4 do
		pig.penCapacity[k] = RN.pigs.penCapacity[k];
		cow.penCapacity[k] = RN.cows.penCapacity[k];
		sheep.penCapacity[k] = RN.sheep.penCapacity[k];
		chicken.penCapacity[k] = RN.chicken.penCapacity[k];
		horse.penCapacity[k] = RN.horses.penCapacity[k];
	end;


-- obtain actual pen capacity from XML files in savegameDirectory
-- if pens are already placed on the map, birth rates and feed consumption
-- is predicted from the acutal pen sizes. This will only work properly if
-- the husbandries have already been saved to items.xml in the savegame folder.
-- This implies that the user must save and the game, and then reenter the game
-- after having bought an animal pen.

-- load items.XML

	local useXMLfile = true;

	if useXMLfile and (savegameDirectory ~= nil) then
		local xmlFile = loadXMLFile("itemsXML", itemsFile);

		local i = 0;
		while true do
			local husbandryName = string.format("items.item(%d)", i);
			if not hasXMLProperty(xmlFile, husbandryName) then
				if isDebug2 then print("Does not have XMLproperty") end;
				break;
			end;

			pen.name = getXMLString(xmlFile, husbandryName .. "#className");
			if pen.name == nil then
				if isDebug2 then print("pen.name is nil, i="..tostring(i), true) end;
				break;
			end;

-- 			if item is AnimalHusbandy read mod name and filename.
-- 			in-game animal sheds do not have a mod name. Here the file name is used.
-- 			for modded animal sheds the path is reconstructed by prepending the defult mod directory



			if tostring(pen.name) == "AnimalHusbandry" then

				pen.modname = getXMLString(xmlFile, husbandryName .. "#modName");
				pen.filename = getXMLString(xmlFile, husbandryName .. "#filename");
				if pen.filename == nil then
					if isDebug2 then print("pen.filename is nil") end;
					break;
				else
					if isDebug2 then print("pen.filename = "..pen.filename) end;
				end;

				if pen.modname ~= nil then
					if isDebug2 then print("modName found: "..pen.modname) end;
					pen.modtab = g_modManager:getModByName(pen.modname);
-- 					DebugUtil.printTableRecursively(pen.modtab,"_",0,3);
					pen.filename = string.gsub(pen.filename, "%$moddir%$", moddir);
					if isDebug2 then print("mod name based pen.filename = "..pen.filename) end;
				else
					if isDebug2 then print("using filename in stead of mod name") end;
				end;

-- load pen XML file

				local penFile = loadXMLFile("penXML", pen.filename);
				local husbandry = "placeable.husbandry";
				if not hasXMLProperty(penFile, husbandry) then
					if isDebug2 then print(penFile.." do not have XMLproperty") end;
					break;
				end;

-- read animal type

				local husbandryModule = "placeable.husbandry.modules.module.config";
				pen.type = getXMLString(penFile,husbandryModule .. "#type");
				if pen.type ~=nil then
						if isDebug2 then print("pen.type : "..pen.type) end;
					else
						if isDebug2 then print("pen.type = nil") end;
				end;

-- read pen capacity and increment number of pen types

				if string.upper(pen.type) == "PIG" then
					pen.pigPenNumber = pen.pigPenNumber + 1;
					local k = pen.pigPenNumber;
					pen.pigMax[k] = tonumber(getXMLString(penFile,husbandryModule .. "#maxNumAnimals"));
					pen.pigText[k] = string.format("Pig shed number %d has a capacity of", pen.pigPenNumber);
					if pen.pigMax[k] ~=nil then
						if isDebug2 then myPrint(pen.pigText[k], pen.pigMax[k], textWidth," ") end;
					else
						if isDebug2 then print("pen.pigMax = nil") end;
					end;

				elseif string.upper(pen.type) == "COW" then
					pen.cowPenNumber = pen.cowPenNumber + 1;
					local k = pen.cowPenNumber;
					pen.cowMax[k] = tonumber(getXMLInt(penFile,husbandryModule .. "#maxNumAnimals"));
					pen.cowText[k] = string.format("Cow shed number %d has a capacity of", pen.cowPenNumber);
					if pen.cowMax[k] ~=nil then
						if isDebug2 then myPrint(pen.cowText[k], pen.cowMax[k], textWidth," ") end;
					else
						if isDebug2 then print("pen.cowMax = nil") end;
					end;

				elseif string.upper(pen.type) == "SHEEP" then
					pen.sheepPenNumber = pen.sheepPenNumber + 1;
					local k = pen.sheepPenNumber;
					pen.sheepMax[k] = tonumber(getXMLString(penFile,husbandryModule .. "#maxNumAnimals"));
					pen.sheepText[k] = string.format("Sheep shed number %d has a capacity of", pen.sheepPenNumber);
					if pen.sheepMax[k] ~=nil then
						if isDebug2 then myPrint(pen.sheepText[k], pen.sheepMax[k], textWidth," ") end;
					else
						if isDebug2 then print("pen.sheepMax = nil") end;
					end;

				elseif string.upper(pen.type) == "CHICKEN" then
					pen.chickenPenNumber = pen.chickenPenNumber + 1;
					local k = pen.chickenPenNumber;
					pen.chickenMax[k] = tonumber(getXMLString(penFile,husbandryModule .. "#maxNumAnimals"));
					pen.chickenText[k] = string.format("Chicken shed number %d has a capacity of", pen.chickenPenNumber);
					if pen.chickenMax[k] ~=nil then
						if isDebug2 then myPrint(pen.chickenText[k], pen.chickenMax[k], textWidth," ") end;
					else
						if isDebug2 then print("pen.chickenMax = nil") end;
					end;

				elseif string.upper(pen.type) == "HORSE" then
					pen.horsePenNumber = pen.horsePenNumber + 1;
					local k = pen.horsePenNumber;
					pen.horseMax[k] = tonumber(getXMLString(penFile,husbandryModule .. "#maxNumAnimals"));
					pen.horseText[k] = string.format("Horse shed number %d has a capacity of", pen.horsePenNumber);
					if pen.horseMax[k] ~=nil then
						if isDebug2 then myPrint(pen.horseText[k], pen.horseMax[k], textWidth," ") end;
					else
						if isDebug2 then print("pen.horseMax = nil") end;
					end;
				end;
			end;
			i=i+1;
		end;

		local b = table.sort(pen.pigMax);	-- sorts pen capacities in-place
		b = table.sort(pen.cowMax);
		b = table.sort(pen.sheepMax);
		b = table.sort(pen.chickenMax);
		b = table.sort(pen.horseMax);


		if pen.pigPenNumber == 1 then
			pig.penCapacity[1] = pen.pigMax[1]; -- standard
			pig.penCapacity[2] = pen.pigMax[1]; -- sows + piglets
			pig.penCapacity[3] = pen.pigMax[1]; -- growers
			pig.penCapacity[4] = pen.pigMax[1];	-- finishers
		end;
		if pen.pigPenNumber ==  2 then
			pig.penCapacity[1] = pen.pigMax[2]; -- standard
			pig.penCapacity[2] = pen.pigMax[2]; -- sows + piglets
			pig.penCapacity[3] = pen.pigMax[2]; -- growers
			pig.penCapacity[4] = pen.pigMax[2];	-- finishers
		end;
		if pen.pigPenNumber ==  3 then
			pig.penCapacity[1] = pen.pigMax[2]; -- standard
			pig.penCapacity[2] = pen.pigMax[2]; -- sows + piglets
			pig.penCapacity[3] = pen.pigMax[3]; -- growers
			pig.penCapacity[4] = pen.pigMax[3];	-- finishers
		end;
		if pen.pigPenNumber ==  4 then
			pig.penCapacity[1] = pen.pigMax[2]; -- standard
			pig.penCapacity[2] = pen.pigMax[4]; -- sows + piglets
			pig.penCapacity[3] = pen.pigMax[3]; -- growers
			pig.penCapacity[4] = pen.pigMax[3];	-- finishers
		end;

		if pen.cowPenNumber == 1 then
			cow.penCapacity[1] = pen.cowMax[1];	-- standard
			cow.penCapacity[2] = pen.cowMax[1];	-- milkers
			cow.penCapacity[3] = pen.cowMax[1];	-- 1st year heifers
			cow.penCapacity[4] = pen.cowMax[1];	-- 2nd year heifers
		end;
		if pen.cowPenNumber ==  2 then
			cow.penCapacity[1] = pen.cowMax[2];	-- standard
			cow.penCapacity[2] = pen.cowMax[2];	-- milkers
			cow.penCapacity[3] = pen.cowMax[2];	-- 1st year heifers
			cow.penCapacity[4] = pen.cowMax[1];	-- 2nd year heifers
		end;
		if pen.cowPenNumber ==  3 then
			cow.penCapacity[1] = pen.cowMax[3];	-- standard
			cow.penCapacity[2] = pen.cowMax[3];	-- milkers
			cow.penCapacity[3] = pen.cowMax[2];	-- 1st year heifers
			cow.penCapacity[4] = pen.cowMax[2];	-- 2nd year heifers
		end;
		if pen.cowPenNumber ==  4 then
			cow.penCapacity[1] = pen.cowMax[4];	-- standard
			cow.penCapacity[2] = pen.cowMax[4];	-- milkers
			cow.penCapacity[3] = pen.cowMax[3];	-- 1st year heifers
			cow.penCapacity[4] = pen.cowMax[1];	-- 2nd year heifers
		end;

		if pen.sheepPenNumber == 1 then
			sheep.penCapacity[1] = pen.sheepMax[1];	-- standard
			sheep.penCapacity[2] = pen.sheepMax[1];	-- ewe+lamb
			sheep.penCapacity[3] = pen.sheepMax[1];	-- growers
			sheep.penCapacity[4] = pen.sheepMax[1];	-- replacers
		end;
		if pen.sheepPenNumber ==  2 then
			sheep.penCapacity[1] = pen.sheepMax[2];	-- standard
			sheep.penCapacity[2] = pen.sheepMax[2];	-- ewe+lamb
			sheep.penCapacity[3] = pen.sheepMax[1];	-- growers
			sheep.penCapacity[4] = pen.sheepMax[1];	-- replacers
		end;
		if pen.sheepPenNumber ==  3 then
			sheep.penCapacity[1] = pen.sheepMax[3];	-- standard
			sheep.penCapacity[2] = pen.sheepMax[3];	-- ewe+lamb
			sheep.penCapacity[3] = pen.sheepMax[2];	-- growers
			sheep.penCapacity[4] = pen.sheepMax[1];	-- replacers
		end;
		if pen.sheepPenNumber ==  4 then
			sheep.penCapacity[1] = pen.sheepMax[4];	-- standard
			sheep.penCapacity[2] = pen.sheepMax[4];	-- ewe+lamb
			sheep.penCapacity[3] = pen.sheepMax[3];	-- growers
			sheep.penCapacity[4] = pen.sheepMax[2];	-- replacers
		end;

		if pen.chickenPenNumber == 1 then
			chicken.penCapacity[1] = pen.chickenMax[1];	-- standard
			chicken.penCapacity[2] = pen.chickenMax[1];	-- hatched
			chicken.penCapacity[3] = pen.chickenMax[1];	-- layers
			chicken.penCapacity[4] = pen.chickenMax[1];	-- broilers
		end;
		if pen.chickenPenNumber ==  2 then
			chicken.penCapacity[1] = pen.chickenMax[2];	-- standard
			chicken.penCapacity[2] = pen.chickenMax[2];	-- hatched
			chicken.penCapacity[3] = pen.chickenMax[2];	-- layers
			chicken.penCapacity[4] = pen.chickenMax[2];	-- broilers
		end;
		if pen.chickenPenNumber ==  3 then
			chicken.penCapacity[1] = pen.chickenMax[3];	-- standard
			chicken.penCapacity[2] = pen.chickenMax[3];	-- hatched
			chicken.penCapacity[3] = pen.chickenMax[3];	-- layers
			chicken.penCapacity[4] = pen.chickenMax[2];	-- broilers
		end;
		if pen.chickenPenNumber ==  4 then
			chicken.penCapacity[1] = pen.chickenMax[4];	-- standard
			chicken.penCapacity[2] = pen.chickenMax[4];	-- hatched
			chicken.penCapacity[3] = pen.chickenMax[4];	-- layers
			chicken.penCapacity[4] = pen.chickenMax[4];	-- broilers
		end;


		if pen.horsePenNumber == 1 then
			horse.penCapacity[1] = pen.horseMax[1];
			horse.penCapacity[2] = pen.horseMax[1];	
			horse.penCapacity[3] = pen.horseMax[1];
			horse.penCapacity[4] = pen.horseMax[1];
		end;
		if pen.horsePenNumber ==  2 then
			horse.penCapacity[1] = pen.horseMax[2];
			horse.penCapacity[2] = pen.horseMax[2];
			horse.penCapacity[3] = pen.horseMax[2];
			horse.penCapacity[4] = pen.horseMax[2];	
		end;
		if pen.horsePenNumber ==  3 then
			horse.penCapacity[1] = pen.horseMax[1];
			horse.penCapacity[2] = pen.horseMax[2];
			horse.penCapacity[3] = pen.horseMax[3];
			horse.penCapacity[4] = pen.horseMax[3];
		end;
		if pen.horsePenNumber ==  4 then
			horse.penCapacity[1] = pen.horseMax[1];
			horse.penCapacity[2] = pen.horseMax[2];
			horse.penCapacity[3] = pen.horseMax[3];
			horse.penCapacity[4] = pen.horseMax[4];
		end;
	
	
	
-- print information in order

		if isDebug then
			print("\n------------Farms on the map--------------------------\n");
			if pen.pigPenNumber > 0 then
				for m = 1, pen.pigPenNumber do
					myPrint(pen.pigText[m], pen.pigMax[m], textWidth,"animals")
				end;
			else
					print("No pig pen on farm");
			end;
			if pen.cowPenNumber > 0 then
				for m = 1, pen.cowPenNumber do
					myPrint(pen.cowText[m], pen.cowMax[m], textWidth,"animals")
				end;
			else
					print("No cow pen on farm");
			end;
			if pen.sheepPenNumber > 0 then
				for m = 1, pen.sheepPenNumber do
					myPrint(pen.sheepText[m], pen.sheepMax[m], textWidth,"animals")
				end;
			else
					print("No sheep pen on farm");
			end;
			if pen.chickenPenNumber > 0 then
				for m = 1, pen.chickenPenNumber do
					myPrint(pen.chickenText[m], pen.chickenMax[m], textWidth,"animals")
				end;
			else
					print("No chicken pen on farm");
			end;
			if pen.horsePenNumber > 0 then
				for m = 1, pen.horsePenNumber do
					myPrint(pen.horseText[m], pen.horseMax[m], textWidth,"animals")
				end;
			else
					print("No horse pen on farm");
			end;
		end;
	end;

-- Fetch yield values for straw, hay and silage to calculate needed field areas to provide the feed

-- 1 cutting

	iFillType = g_fillTypeManager:getFillTypeIndexByName('WHEAT');
	iFruitType = g_fruitTypeManager:getFruitTypeIndexByFillTypeIndex(iFillType);
	crop.wheatStrawYieldPerHa = 10000 * g_currentMission.fruitTypeManager.fruitTypes[iFruitType].windrowLiterPerSqm/general.gameCropYieldCorrection;
	iFillType = g_fillTypeManager:getFillTypeIndexByName('BARLEY');
	iFruitType = g_fruitTypeManager:getFruitTypeIndexByFillTypeIndex(iFillType);
	crop.barleyStrawYieldPerHa = 10000 * g_currentMission.fruitTypeManager.fruitTypes[iFruitType].windrowLiterPerSqm/general.gameCropYieldCorrection;
	iFillType = g_fillTypeManager:getFillTypeIndexByName('OAT');
	iFruitType = g_fruitTypeManager:getFruitTypeIndexByFillTypeIndex(iFillType);
	crop.oatStrawYieldPerHa = 10000 * g_currentMission.fruitTypeManager.fruitTypes[iFruitType].windrowLiterPerSqm/general.gameCropYieldCorrection;

-- multiple cuttings

	crop.grassYieldPerHa = RN.annualGrassCuttings * RN.windrowYieldLitersPerHa["GRASS"];
	crop.hayYieldPerHa = RN.annualGrassCuttings * RN.windrowYieldLitersPerHa["DRYGRASS"];
	crop.silageYieldPerHa  = crop.grassYieldPerHa;

-- Fetch yield values for crop to calculate needed field areas to provide the feed


	crop.wheatYieldPerHa = RN.cropBonusYieldLitersPerHa["WHEAT"]
	crop.barleyYieldPerHa = RN.cropBonusYieldLitersPerHa["BARLEY"]
	crop.oatYieldPerHa = RN.cropBonusYieldLitersPerHa["OAT"]
	crop.maizeYieldPerHa = RN.cropBonusYieldLitersPerHa["MAIZE"]
	crop.canolaYieldPerHa = RN.cropBonusYieldLitersPerHa["CANOLA"]
	crop.sunflowerYieldPerHa = RN.cropBonusYieldLitersPerHa["SUNFLOWER"]
	crop.soybeanYieldPerHa = RN.cropBonusYieldLitersPerHa["SOYBEAN"]
	crop.potatoYieldPerHa = RN.cropBonusYieldLitersPerHa["POTATO"]
	crop.sugarbeetYieldPerHa = RN.cropBonusYieldLitersPerHa["SUGARBEET"]



if isDebug then print("\n--------------------PIGS------------------------------\n") end;


-- pig pen type 1 to 4

	for k = 1,4 do
		if RN.pigs.growers[k] >= 1 then		--non-breeding pen
			pig.growers[k] = math.min(RN.pigs.growers[k],pig.penCapacity[k]);
			pig.endNumber[k] = pig.growers[k];
			pig.startNumber[k] = 0;
		else	-- breeding pen
			pig.startNumber[k] = RN.pigs.birthingAnimals[k];
			pig.numberOfOffspring[k] = pig.startNumber[k] * RN.pigs.litterSize[k] * RN.pigs.gamedaysInPen[k]/RN.pigs.reeringGamedays[k];
			pig.endNumber[k] = pig.startNumber[k] + pig.numberOfOffspring[k];
			pig.growers[k] = 0;
			if pig.endNumber[k] >= pig.penCapacity[k] then
				pig.startNumber[k] = math.floor(pig.penCapacity[k]/ (1 + RN.pigs.litterSize[k]));
				pig.numberOfOffspring[k] = pig.startNumber[k] * RN.pigs.litterSize[k];
				pig.endNumber[k] = pig.startNumber[k] + pig.numberOfOffspring[k];
			end;
		end;
		if (pig.startNumber[k] >= 1) and (pig.startNumber[k] < pig.endNumber[k]) then
			pig.x = (pig.endNumber[k]/pig.startNumber[k])*(pig.penCapacity[k]-pig.startNumber[k])/(pig.penCapacity[k]-pig.endNumber[k]);
			pig.birthRatePerDay[k] = (1/RN.pigs.gamedaysInPen[k])*math.log(pig.x);
			pig.y = 1 - (pig.startNumber[k]/pig.penCapacity[k])*(1 - math.exp(pig.birthRatePerDay[k]*RN.pigs.gamedaysInPen[k]));
			pig.meanNumber[k] = (pig.penCapacity[k]/(pig.birthRatePerDay[k]*RN.pigs.gamedaysInPen[k]))*math.log(pig.y);
			pig.z = (1+pig.startNumber[k])*(pig.penCapacity[k] - pig.startNumber[k])/(pig.startNumber[k]*(pig.penCapacity[k]-pig.startNumber[k]-1));
			pig.timeToFirstBirth[k] =24*60*60* math.log(pig.z)/pig.birthRatePerDay[k]; -- in seconds
		else
			pig.birthRatePerDay[k] = 0;
			pig.meanNumber[k] = pig.endNumber[k];
			pig.timeToFirstBirth[k] = 0;
		end;
	end;

-- pig pens

    for k = 1,4 do
		pig.matureStrawPerDay[k] = RN.pigs.matureStrawPerDay[k] / general.strawBaleDensity;
		pig.growerStrawPerDay[k] = RN.pigs.growerStrawPerDay[k] / general.strawBaleDensity;
		pig.strawPerMatureAnimalInPen[k] = pig.matureStrawPerDay[k] * RN.pigs.gamedaysInPen[k] * gameDays.realDaysPerGameDay;
		pig.strawPerGrowerAnimalInPen[k] = pig.growerStrawPerDay[k] * RN.pigs.gamedaysInPen[k] * gameDays.realDaysPerGameDay;
		pig.strawMatureTotalPerPenPeriod[k] = pig.startNumber[k] * pig.strawPerMatureAnimalInPen[k];
		pig.strawGrowerTotalPerPenPeriod[k] = (pig.endNumber[k]-pig.startNumber[k]) * pig.strawPerGrowerAnimalInPen[k];
		pig.totalStrawPerPenPeriod[k] = pig.strawMatureTotalPerPenPeriod[k] + pig.strawGrowerTotalPerPenPeriod[k];
		pig.totalStrawVolumePerYear[k] = (12/RN.pigs.gamedaysInPen[k]) * pig.totalStrawPerPenPeriod[k];
		pig.strawPerAnimalPerGameday[k] = pig.totalStrawPerPenPeriod[k]/(RN.pigs.gamedaysInPen[k] * pig.meanNumber[k]);
		pig.beddingBales[k] = tonumber(math.ceil(pig.totalStrawVolumePerYear[k]/general.strawBaleVolume));
		pig.neededBeddingWheatFieldPerYear[k] = pig.totalStrawVolumePerYear[k] / crop.wheatStrawYieldPerHa;
		pig.neededBeddingBarleyFieldPerYear[k] = pig.totalStrawVolumePerYear[k] / crop.barleyStrawYieldPerHa;
		pig.neededBeddingOatFieldPerYear[k] = pig.totalStrawVolumePerYear[k] / crop.oatStrawYieldPerHa;

		pig.matureWaterPerDay[k] = RN.pigs.matureWaterPerDay[k];
		pig.growerWaterPerDay[k] = RN.pigs.growerWaterPerDay[k];
		pig.waterPerMatureAnimalInPen[k] = pig.matureWaterPerDay[k] * RN.pigs.gamedaysInPen[k] * gameDays.realDaysPerGameDay;
		pig.waterPerGrowerAnimalInPen[k] = pig.growerWaterPerDay[k] * RN.pigs.gamedaysInPen[k] * gameDays.realDaysPerGameDay;
		pig.waterMatureTotalPerPenPeriod[k] = pig.startNumber[k] * pig.waterPerMatureAnimalInPen[k];
		pig.waterGrowerTotalPerPenPeriod[k] = (pig.endNumber[k]-pig.startNumber[k]) * pig.waterPerGrowerAnimalInPen[k];
		pig.totalWaterPerPenPeriod[k] = pig.waterMatureTotalPerPenPeriod[k] + pig.waterGrowerTotalPerPenPeriod[k];
		pig.totalWaterVolumePerYear[k] = (12/RN.pigs.gamedaysInPen[k]) * pig.totalWaterPerPenPeriod[k];
		pig.totalBowserWaterPerYear[k] = RN.pigs.bowserWaterFraction[k] * pig.totalWaterVolumePerYear[k]
		pig.waterPerAnimalPerGameday[k] = RN.pigs.bowserWaterFraction[k] * pig.totalWaterPerPenPeriod[k]/(RN.pigs.gamedaysInPen[k] * pig.meanNumber[k]);

		pig.matureFeedPerDay[k] = RN.pigs.matureFeedPerDay[k] / pig.foodDensity;
		pig.growerFeedPerDay[k] = RN.pigs.growerFeedPerDay[k] / pig.foodDensity;
		pig.feedPerMatureAnimalInPen[k] = pig.matureFeedPerDay[k] * RN.pigs.gamedaysInPen[k] * gameDays.realDaysPerGameDay;
		pig.feedPerGrowerAnimalInPen[k] = pig.growerFeedPerDay[k] * RN.pigs.gamedaysInPen[k] * gameDays.realDaysPerGameDay;
		pig.feedMatureTotalPerPenPeriod[k] = pig.startNumber[k] * pig.feedPerMatureAnimalInPen[k];
		pig.feedGrowerTotalPerPenPeriod[k] = (pig.endNumber[k]-pig.startNumber[k]) * pig.feedPerGrowerAnimalInPen[k];
		pig.totalFeedPerPenPeriod[k] = pig.feedMatureTotalPerPenPeriod[k] + pig.feedGrowerTotalPerPenPeriod[k];
		pig.totalFeedVolumePerYear[k] = (12/RN.pigs.gamedaysInPen[k]) * pig.totalFeedPerPenPeriod[k];
		pig.totalTroughFeedPerYear[k] = RN.pigs.troughFeedFraction[k] * pig.totalFeedVolumePerYear[k];
		pig.feedPerAnimalPerGameday[k] = RN.pigs.troughFeedFraction[k] * pig.totalFeedPerPenPeriod[k]/(RN.pigs.gamedaysInPen[k] * pig.meanNumber[k]);

		pig.totalFoodGrainPerYear[k] = 0.25 * pig.totalTroughFeedPerYear[k];
		pig.totalFoodMaizePerYear[k] = 0.5 * pig.totalTroughFeedPerYear[k];
		pig.totalFoodProteinPerYear[k] = 0.2* pig.totalTroughFeedPerYear[k];
		pig.totalFoodRootCropPerYear[k] = 0.05* pig.totalTroughFeedPerYear[k];

		pig.neededWheatHa[k] = pig.totalFoodGrainPerYear[k] / crop.wheatYieldPerHa;
		pig.neededBarleyHa[k] = pig.totalFoodGrainPerYear[k] / crop.barleyYieldPerHa;
		pig.neededMaizeHa[k] = pig.totalFoodMaizePerYear[k] / crop.maizeYieldPerHa;
		pig.neededCanolaHa[k] = pig.totalFoodProteinPerYear[k] / crop.canolaYieldPerHa ;
		pig.neededSunflowerHa[k] = pig.totalFoodProteinPerYear[k] / crop.sunflowerYieldPerHa;
		pig.neededSoybeanHa[k] = pig.totalFoodProteinPerYear[k] / crop.soybeanYieldPerHa;
		pig.neededPotatoHa[k] = pig.totalFoodRootCropPerYear[k] / crop.potatoYieldPerHa;
		pig.neededSugarbeetHa[k] = pig.totalFoodRootCropPerYear[k] / crop.sugarbeetYieldPerHa;

		pig.matureManurePerDay[k] = RN.pigs.matureManurePerDay[k] / general.manureDensity;
		pig.growerManurePerDay[k] = RN.pigs.growerManurePerDay[k] / general.manureDensity;
		pig.manurePerMatureAnimalInPen[k] = pig.matureManurePerDay[k] * RN.pigs.gamedaysInPen[k] * gameDays.realDaysPerGameDay;
		pig.manurePerGrowerAnimalInPen[k] = pig.growerManurePerDay[k] * RN.pigs.gamedaysInPen[k] * gameDays.realDaysPerGameDay;
		pig.manureMatureTotalPerPenPeriod[k] = pig.startNumber[k] * pig.manurePerMatureAnimalInPen[k];
		pig.manureGrowerTotalPerPenPeriod[k] = (pig.endNumber[k]-pig.startNumber[k]) * pig.manurePerGrowerAnimalInPen[k];
		pig.totalManurePerPenPeriod[k] = pig.manureMatureTotalPerPenPeriod[k] + pig.manureGrowerTotalPerPenPeriod[k];
		pig.totalManureVolumePerYear[k] = (12/RN.pigs.gamedaysInPen[k]) * pig.totalManurePerPenPeriod[k];
		pig.manurePerAnimalPerGameday[k] = pig.totalManurePerPenPeriod[k]/(RN.pigs.gamedaysInPen[k] * pig.meanNumber[k]);

		pig.matureSlurryPerDay[k] = RN.pigs.matureSlurryPerWater[k] * RN.pigs.matureWaterPerDay[k];
		pig.growerSlurryPerDay[k] = RN.pigs.growerSlurryPerWater[k] * RN.pigs.growerWaterPerDay[k];
		pig.slurryPerMatureAnimalInPen[k] = pig.matureSlurryPerDay[k] * RN.pigs.gamedaysInPen[k] * gameDays.realDaysPerGameDay;
		pig.slurryPerGrowerAnimalInPen[k] = pig.growerSlurryPerDay[k] * RN.pigs.gamedaysInPen[k] * gameDays.realDaysPerGameDay;
		pig.slurryMatureTotalPerPenPeriod[k] = pig.startNumber[k] * pig.slurryPerMatureAnimalInPen[k];
		pig.slurryGrowerTotalPerPenPeriod[k] = (pig.endNumber[k]-pig.startNumber[k]) * pig.slurryPerGrowerAnimalInPen[k];
		pig.totalSlurryPerPenPeriod[k] = pig.slurryMatureTotalPerPenPeriod[k] + pig.slurryGrowerTotalPerPenPeriod[k];
		pig.totalSlurryVolumePerYear[k] = (12/RN.pigs.gamedaysInPen[k]) * pig.totalSlurryPerPenPeriod[k];
		pig.slurryPerAnimalPerGameday[k] = pig.totalSlurryPerPenPeriod[k]/(RN.pigs.gamedaysInPen[k] * pig.meanNumber[k]);

		pig.feedSpillagePerDay[k] = RN.feedSpillageFraction * pig.feedPerAnimalPerGameday[k];

		if isDebug then

			print("pen : "..k);
			print(" ");
			print("Pen type : "..RN.pigs.type[k]);
			print("Animal color : "..RN.pigs.color[k]);
			print("Animal fillType : "..RN.pigs.fillType[k]);
			print(" ");
			myPrint("Pig start number",pig.startNumber[k],textWidth," ");
			myPrint("Pig end number",pig.endNumber[k],textWidth," ");
			myPrint("Pig pen capacity",pig.penCapacity[k],textWidth," ");
			myPrintFloat("Pig BirthRatePerDay",pig.birthRatePerDay[k],textWidth," ");
			myPrintFloat("Pig mean number",pig.meanNumber[k],textWidth," ");
			myPrintTimeStamp("Pig time to first birth",timeStamp(pig.timeToFirstBirth[k]).." ",textWidth);
			print(" ");

			myPrint("Total pig bedding straw per year", pig.totalStrawVolumePerYear[k], textWidth," Liter");
			myPrint("Total pig bedding straw bales per year", pig.beddingBales[k], textWidth," round bales");
			myPrintFloat("Wheat field for pig bedding straw", pig.neededBeddingWheatFieldPerYear[k],textWidth," Ha");
			myPrintFloat("Barley field for pig bedding straw", pig.neededBeddingBarleyFieldPerYear[k],textWidth," Ha");
			myPrintFloat("Oat field for pig bedding straw", pig.neededBeddingOatFieldPerYear[k],textWidth," Ha");
			print(" ");

			myPrint("Total pig water per year", pig.totalWaterVolumePerYear[k], textWidth," Liter");
			myPrintFloat("Bowser fraction of drinking water", RN.pigs.bowserWaterFraction[k],textWidth," ");
			myPrint("Total pig bowser water per year", pig.totalBowserWaterPerYear[k], textWidth," Liter");
			print(" ");

			myPrint("Trough food per year", pig.totalTroughFeedPerYear[k], textWidth, " Liter");
			myPrint("Grain food per year", pig.totalFoodGrainPerYear[k], textWidth, " Liter");
			myPrint("Maize food per year", pig.totalFoodMaizePerYear[k], textWidth," Liter");
			myPrint("Protein food per year", pig.totalFoodProteinPerYear[k], textWidth," Liter");
			myPrint("Root crop food per year", pig.totalFoodRootCropPerYear[k], textWidth," Liter");
			myPrintFloat("Needed wheat field size", pig.neededWheatHa[k], textWidth," Ha or");
			myPrintFloat("Needed barley field size", pig.neededBarleyHa[k], textWidth," Ha");
			myPrintFloat("Needed maize field size", pig.neededMaizeHa[k], textWidth," Ha");
			myPrintFloat("Needed canola field size", pig.neededCanolaHa[k], textWidth," Ha or");
			myPrintFloat("Needed sunflower field size", pig.neededSunflowerHa[k], textWidth," Ha or");
			myPrintFloat("Needed soybean field size", pig.neededSoybeanHa[k], textWidth," Ha");
			myPrintFloat("Needed potato field size", pig.neededPotatoHa[k], textWidth," Ha or");
			myPrintFloat("Needed sugarbeet field size", pig.neededSugarbeetHa[k], textWidth," Ha");
			print(" ");
			myPrintFloat("Pig straw per pig per game day", pig.strawPerAnimalPerGameday[k], textWidth, " Liter");
			myPrintFloat("Pig water per pig per game day", pig.waterPerAnimalPerGameday[k], textWidth, " Liter");
			myPrintFloat("Pig feed per pig per game day", pig.feedPerAnimalPerGameday[k], textWidth, " Liter");
			print(" ");

			myPrint("Total pig manure per year",pig.totalManureVolumePerYear[k], textWidth," Liter");
			myPrint("Total pig liquid manure per year", pig.totalSlurryVolumePerYear[k], textWidth," Liter");
			myPrintFloat("Pig food spillage per game day", pig.feedSpillagePerDay[k], textWidth," Liter\n");
		end;

-- set game parameters

        iFillType = RN.pigs.fillType[k];
		myAnimalType = g_animalManager:getAnimalByFillType(iFillType);
		myAnimalType.input.strawPerDay = pig.strawPerAnimalPerGameday[k];					-- straw
		myAnimalType.input.waterPerDay = pig.waterPerAnimalPerGameday[k];					-- water
		myAnimalType.input.foodPerDay = pig.feedPerAnimalPerGameday[k];						-- feed
		myAnimalType.output.manurePerDay = pig.manurePerAnimalPerGameday[k];				-- manure
		myAnimalType.output.liquidManurePerDay = pig.slurryPerAnimalPerGameday[k];			-- liquid manure
		myAnimalType.output.foodSpillagePerDay = pig.feedSpillagePerDay[k];					-- feed spillage
		myAnimalType.breeding.birthRatePerDay = pig.birthRatePerDay[k];						-- birth rate
		myAnimalType.dirt.cleanDuration = pig.CleanDuration;								-- clean duration, not clear what that is, default used
		if isDebug1 then myPrint(RN.pigs.color[k].." fillTypeIndex",iFillType, textWidth, " \n") end;
    end;


if isDebug then print("\n--------------------COWS------------------------------\n") end;


-- cow pen type 1 to 4

	for k = 1,4 do
		if RN.cows.growers[k] >= 1 then		--non-breeding pen
			cow.growers[k] = math.min(RN.cows.growers[k],cow.penCapacity[k]);
			cow.endNumber[k] = cow.growers[k];
			cow.startNumber[k] = 0;
		else	-- breeding pen
			cow.startNumber[k] = RN.cows.birthingAnimals[k];
			cow.numberOfOffspring[k] = cow.startNumber[k] * RN.cows.litterSize[k] * RN.cows.gamedaysInPen[k]/RN.cows.reeringGamedays[k];
			cow.endNumber[k] = cow.startNumber[k] + cow.numberOfOffspring[k];
			cow.growers[k] = 0;
			if cow.endNumber[k] >= cow.penCapacity[k] then
				cow.startNumber[k] = math.floor(cow.penCapacity[k]/ (1 + RN.cows.litterSize[k]));
				cow.numberOfOffspring[k] = cow.startNumber[k] * RN.cows.litterSize[k];
				cow.endNumber[k] = cow.startNumber[k] + cow.numberOfOffspring[k];
			end;
		end;
		if (cow.startNumber[k] >= 1) and (cow.startNumber[k] < cow.endNumber[k]) then
			cow.x = (cow.endNumber[k]/cow.startNumber[k])*(cow.penCapacity[k]-cow.startNumber[k])/(cow.penCapacity[k]-cow.endNumber[k]);
			cow.birthRatePerDay[k] = (1/RN.cows.gamedaysInPen[k])*math.log(cow.x);
			cow.y = 1 - (cow.startNumber[k]/cow.penCapacity[k])*(1 - math.exp(cow.birthRatePerDay[k]*RN.cows.gamedaysInPen[k]));
			cow.meanNumber[k] = (cow.penCapacity[k]/(cow.birthRatePerDay[k]*RN.cows.gamedaysInPen[k]))*math.log(cow.y);
			cow.z = (1+cow.startNumber[k])*(cow.penCapacity[k] - cow.startNumber[k])/(cow.startNumber[k]*(cow.penCapacity[k]-cow.startNumber[k]-1));
			cow.timeToFirstBirth[k] =24*60*60* math.log(cow.z)/cow.birthRatePerDay[k]; -- in seconds
		else
			cow.birthRatePerDay[k] = 0;
			cow.meanNumber[k] = cow.endNumber[k];
			cow.timeToFirstBirth[k] = 0;
		end;
	end;

    for k = 1,4 do
		cow.matureStrawPerDay[k] = RN.cows.matureStrawPerDay[k] / general.strawBaleDensity;
		cow.growerStrawPerDay[k] = RN.cows.growerStrawPerDay[k] / general.strawBaleDensity;
		cow.strawPerMatureAnimalInPen[k] = cow.matureStrawPerDay[k] * RN.cows.gamedaysInPen[k] * gameDays.realDaysPerGameDay;
		cow.strawPerGrowerAnimalInPen[k] = cow.growerStrawPerDay[k] * RN.cows.gamedaysInPen[k] * gameDays.realDaysPerGameDay;
		cow.strawMatureTotalPerPenPeriod[k] = cow.startNumber[k] * cow.strawPerMatureAnimalInPen[k];
		cow.strawGrowerTotalPerPenPeriod[k] = (cow.endNumber[k]-cow.startNumber[k]) * cow.strawPerGrowerAnimalInPen[k];
		cow.totalStrawPerPenPeriod[k] = cow.strawMatureTotalPerPenPeriod[k] + cow.strawGrowerTotalPerPenPeriod[k];
		cow.totalStrawVolumePerYear[k] = (12/RN.cows.gamedaysInPen[k]) * cow.totalStrawPerPenPeriod[k];
		cow.strawPerAnimalPerGameday[k] = cow.totalStrawPerPenPeriod[k]/(RN.cows.gamedaysInPen[k] * cow.meanNumber[k]);
		cow.beddingBales[k] = tonumber(math.ceil(cow.totalStrawVolumePerYear[k]/general.strawBaleVolume));
		cow.neededBeddingWheatFieldPerYear[k] = cow.totalStrawVolumePerYear[k] / crop.wheatStrawYieldPerHa;
		cow.neededBeddingBarleyFieldPerYear[k] = cow.totalStrawVolumePerYear[k] / crop.barleyStrawYieldPerHa;
		cow.neededBeddingOatFieldPerYear[k] = cow.totalStrawVolumePerYear[k] / crop.oatStrawYieldPerHa;

		cow.matureWaterPerDay[k] = RN.cows.matureWaterPerDay[k];
		cow.growerWaterPerDay[k] = RN.cows.growerWaterPerDay[k];
		cow.waterPerMatureAnimalInPen[k] = cow.matureWaterPerDay[k] * RN.cows.gamedaysInPen[k] * gameDays.realDaysPerGameDay;
		cow.waterPerGrowerAnimalInPen[k] = cow.growerWaterPerDay[k] * RN.cows.gamedaysInPen[k] * gameDays.realDaysPerGameDay;
		cow.waterMatureTotalPerPenPeriod[k] = cow.startNumber[k] * cow.waterPerMatureAnimalInPen[k];
		cow.waterGrowerTotalPerPenPeriod[k] = (cow.endNumber[k]-cow.startNumber[k]) * cow.waterPerGrowerAnimalInPen[k];
		cow.totalWaterPerPenPeriod[k] = cow.waterMatureTotalPerPenPeriod[k] + cow.waterGrowerTotalPerPenPeriod[k];
		cow.totalWaterVolumePerYear[k] = (12/RN.cows.gamedaysInPen[k]) * cow.totalWaterPerPenPeriod[k];
		cow.totalBowserWaterPerYear[k] = RN.cows.bowserWaterFraction[k] * cow.totalWaterVolumePerYear[k]
		cow.waterPerAnimalPerGameday[k] = RN.cows.bowserWaterFraction[k] * cow.totalWaterPerPenPeriod[k]/(RN.cows.gamedaysInPen[k] * cow.meanNumber[k]);

		cow.matureFeedPerDay[k] = RN.cows.matureFeedPerDay[k] / cow.foodDensity;
		cow.growerFeedPerDay[k] = RN.cows.growerFeedPerDay[k] / cow.foodDensity;
		cow.feedPerMatureAnimalInPen[k] = cow.matureFeedPerDay[k] * RN.cows.gamedaysInPen[k] * gameDays.realDaysPerGameDay;
		cow.feedPerGrowerAnimalInPen[k] = cow.growerFeedPerDay[k] * RN.cows.gamedaysInPen[k] * gameDays.realDaysPerGameDay;
		cow.feedMatureTotalPerPenPeriod[k] = cow.startNumber[k] * cow.feedPerMatureAnimalInPen[k];
		cow.feedGrowerTotalPerPenPeriod[k] = (cow.endNumber[k]-cow.startNumber[k]) * cow.feedPerGrowerAnimalInPen[k];
		cow.totalFeedPerPenPeriod[k] = cow.feedMatureTotalPerPenPeriod[k] + cow.feedGrowerTotalPerPenPeriod[k];
		cow.totalFeedVolumePerYear[k] = (12/RN.cows.gamedaysInPen[k]) * cow.totalFeedPerPenPeriod[k];
		cow.totalTroughFeedPerYear[k] = RN.cows.troughFeedFraction[k] * cow.totalFeedVolumePerYear[k];
		cow.feedPerAnimalPerGameday[k] = RN.cows.troughFeedFraction[k] * cow.totalFeedPerPenPeriod[k]/(RN.cows.gamedaysInPen[k] * cow.meanNumber[k]);

		cow.strawFoodPerYear[k] = cow.strawTMRFraction * cow.totalTroughFeedPerYear[k];
		cow.hayFoodPerYear[k] = cow.hayTMRFraction * cow.totalTroughFeedPerYear[k];
		cow.silageFoodPerYear[k] = cow.silageTMRFraction * cow.totalTroughFeedPerYear[k];
		cow.neededWheatHa[k] = cow.strawFoodPerYear[k] / crop.wheatStrawYieldPerHa;
		cow.neededBarleyHa[k] = cow.strawFoodPerYear[k] / crop.barleyStrawYieldPerHa;
		cow.neededOatHa[k] = cow.strawFoodPerYear[k] / crop.oatStrawYieldPerHa;
		cow.strawBalesPerYear[k] = tonumber(math.ceil(cow.strawFoodPerYear[k]/general.strawBaleVolume));
		cow.hayBalesPerYear[k] = tonumber(math.ceil(cow.hayFoodPerYear[k]/general.hayBaleVolume));
		cow.silageBalesPerYear[k] = tonumber(math.ceil(cow.silageFoodPerYear[k]/general.silageBaleVolume));
		cow.hayFeedHa[k] = cow.hayFoodPerYear[k] / crop.hayYieldPerHa;
		cow.silageFeedHa[k] = cow.silageFoodPerYear[k] / crop.silageYieldPerHa;

		cow.matureManurePerDay[k] = RN.cows.matureManurePerDay[k] / general.manureDensity;
		cow.growerManurePerDay[k] = RN.cows.growerManurePerDay[k] / general.manureDensity;
		cow.manurePerMatureAnimalInPen[k] = cow.matureManurePerDay[k] * RN.cows.gamedaysInPen[k] * gameDays.realDaysPerGameDay;
		cow.manurePerGrowerAnimalInPen[k] = cow.growerManurePerDay[k] * RN.cows.gamedaysInPen[k] * gameDays.realDaysPerGameDay;
		cow.manureMatureTotalPerPenPeriod[k] = cow.startNumber[k] * cow.manurePerMatureAnimalInPen[k];
		cow.manureGrowerTotalPerPenPeriod[k] = (cow.endNumber[k]-cow.startNumber[k]) * cow.manurePerGrowerAnimalInPen[k];
		cow.totalManurePerPenPeriod[k] = cow.manureMatureTotalPerPenPeriod[k] + cow.manureGrowerTotalPerPenPeriod[k];
		cow.totalManureVolumePerYear[k] = (12/RN.cows.gamedaysInPen[k]) * cow.totalManurePerPenPeriod[k];
		cow.manurePerAnimalPerGameday[k] = cow.totalManurePerPenPeriod[k]/(RN.cows.gamedaysInPen[k] * cow.meanNumber[k]);

		cow.matureSlurryPerDay[k] = RN.cows.matureSlurryPerWater[k] * RN.cows.matureWaterPerDay[k];
		cow.growerSlurryPerDay[k] = RN.cows.growerSlurryPerWater[k] * RN.cows.growerWaterPerDay[k];
		cow.slurryPerMatureAnimalInPen[k] = cow.matureSlurryPerDay[k] * RN.cows.gamedaysInPen[k] * gameDays.realDaysPerGameDay;
		cow.slurryPerGrowerAnimalInPen[k] = cow.growerSlurryPerDay[k] * RN.cows.gamedaysInPen[k] * gameDays.realDaysPerGameDay;
		cow.slurryMatureTotalPerPenPeriod[k] = cow.startNumber[k] * cow.slurryPerMatureAnimalInPen[k];
		cow.slurryGrowerTotalPerPenPeriod[k] = (cow.endNumber[k]-cow.startNumber[k]) * cow.slurryPerGrowerAnimalInPen[k];
		cow.totalSlurryPerPenPeriod[k] = cow.slurryMatureTotalPerPenPeriod[k] + cow.slurryGrowerTotalPerPenPeriod[k];
		cow.totalSlurryVolumePerYear[k] = (12/RN.cows.gamedaysInPen[k]) * cow.totalSlurryPerPenPeriod[k];
		cow.slurryPerAnimalPerGameday[k] = cow.totalSlurryPerPenPeriod[k]/(RN.cows.gamedaysInPen[k] * cow.meanNumber[k]);

		cow.matureMilkPerDay[k] = RN.cows.milkPerYear[k] / (360 * general.milkDensity);
		cow.milkPerMatureAnimalInPen[k] = cow.matureMilkPerDay[k] * RN.cows.gamedaysInPen[k] * gameDays.realDaysPerGameDay;
		cow.totalMilkPerPenPeriod[k] = cow.startNumber[k] * cow.milkPerMatureAnimalInPen[k];
		cow.totalMilkVolumePerYear[k] = (12/RN.cows.gamedaysInPen[k]) * cow.totalMilkPerPenPeriod[k];
		cow.milkPerAnimalPerGameday[k] = cow.totalMilkPerPenPeriod[k]/(RN.cows.gamedaysInPen[k] * cow.meanNumber[k]);

		cow.feedSpillagePerDay[k] = RN.feedSpillageFraction * cow.feedPerAnimalPerGameday[k];

		if isDebug then

			print("pen : "..k);
			print(" ");
			print("Pen type        : "..RN.cows.type[k]);
			print("Animal color    : "..RN.cows.color1[k]);
			print("Animal fillType : "..RN.cows.fillType1[k]);
			print("Animal color    : "..RN.cows.color2[k]);
			print("Animal fillType : "..RN.cows.fillType2[k]);
			print(" ");
			myPrint("cow start number",cow.startNumber[k],textWidth," ");
			myPrint("cow end number",cow.endNumber[k],textWidth," ");
			myPrint("cow pen capacity",cow.penCapacity[k],textWidth," ");
			myPrintFloat("cow BirthRatePerDay",cow.birthRatePerDay[k],textWidth," ");
			myPrintFloat("cow mean number",cow.meanNumber[k],textWidth," ");
			myPrintTimeStamp("cow time to first birth",timeStamp(cow.timeToFirstBirth[k]).." ",textWidth);
			print(" ");

			myPrint("Total cow bedding straw per year", cow.totalStrawVolumePerYear[k], textWidth," Liter");
			myPrint("Total cow bedding straw bales per year", cow.beddingBales[k], textWidth," round bales");
			myPrintFloat("Wheat field for cow bedding straw", cow.neededBeddingWheatFieldPerYear[k],textWidth," Ha");
			myPrintFloat("Barley field for cow bedding straw", cow.neededBeddingBarleyFieldPerYear[k],textWidth," Ha");
			myPrintFloat("Oat field for cow bedding straw", cow.neededBeddingOatFieldPerYear[k],textWidth," Ha");
			print(" ");

			myPrint("Total cow water per year", cow.totalWaterVolumePerYear[k], textWidth," Liter");
			myPrintFloat("Bowser fraction of drinking water", RN.cows.bowserWaterFraction[k],textWidth," ");
			myPrint("Total cow bowser water per year", cow.totalBowserWaterPerYear[k], textWidth," Liter");
			print(" ");
			myPrint("Trough food per year", cow.totalTroughFeedPerYear[k], textWidth, " Liter");
			myPrintFloat("Straw fraction of total mixed ration", cow.strawTMRFraction, textWidth, " ");
			myPrintFloat("Hay fraction of total mixed ration", cow.hayTMRFraction, textWidth, " ");
			myPrintFloat("Silage fraction of total mixed ration", cow.silageTMRFraction, textWidth, " ");
			myPrint("Total cow straw feed per year", cow.strawFoodPerYear[k], textWidth," Liter");
			myPrint("Total cow hay feed per year", cow.hayFoodPerYear[k], textWidth," Liter");
			myPrint("Total cow silage feed per year", cow.silageFoodPerYear[k], textWidth," Liter");
			myPrint("Total cow straw bale feed per year", cow.strawBalesPerYear[k], textWidth," round bales");
			myPrint("Total cow hay bale feed per year", cow.hayBalesPerYear[k], textWidth," round bales");
			myPrint("Total cow silage bale feed per year", cow.silageBalesPerYear[k], textWidth," round bales");
			print(" ");
			myPrintFloat("Wheat field for cow straw feed", cow.neededWheatHa[k],textWidth," Ha or");
			myPrintFloat("Barley field for cow straw feed", cow.neededBarleyHa[k],textWidth," Ha or");
			myPrintFloat("Oat field for cow straw feed", cow.neededOatHa[k],textWidth," Ha");
			myPrintFloat("Hay field for cow hay feed", cow.hayFeedHa[k],textWidth," Ha");
			myPrintFloat("Grass field for cow silage feed", cow.silageFeedHa[k],textWidth," Ha");
			print(" ");
			myPrint("Total milk per year",cow.totalMilkVolumePerYear[k], textWidth," Liter");
			myPrint("Total cow manure per year",cow.totalManureVolumePerYear[k], textWidth," Liter");
			myPrint("Total cow liquid manure per year", cow.totalSlurryVolumePerYear[k], textWidth," Liter");
			myPrintFloat("cow food spillage per game day", cow.feedSpillagePerDay[k], textWidth," Liter");
			print(" ");
			myPrintFloat("cow straw per cow per game day", cow.strawPerAnimalPerGameday[k], textWidth, " Liter");
			myPrintFloat("cow water per cow per game day", cow.waterPerAnimalPerGameday[k], textWidth, " Liter");
			myPrintFloat("cow feed per cow per game day", cow.feedPerAnimalPerGameday[k], textWidth, " Liter\n");
		end;

-- set game parameters

        iFillType = RN.cows.fillType1[k];
		myAnimalType = g_animalManager:getAnimalByFillType(iFillType);
		myAnimalType.input.strawPerDay = cow.strawPerAnimalPerGameday[k];					-- straw
		myAnimalType.input.waterPerDay = cow.waterPerAnimalPerGameday[k];					-- water
		myAnimalType.input.foodPerDay = cow.feedPerAnimalPerGameday[k];						-- feed
		myAnimalType.output.milkPerDay = cow.milkPerAnimalPerGameday[k];					-- milk
		myAnimalType.output.manurePerDay = cow.manurePerAnimalPerGameday[k];				-- manure
		myAnimalType.output.liquidManurePerDay = cow.slurryPerAnimalPerGameday[k];			-- liquid manure
		myAnimalType.output.foodSpillagePerDay = cow.feedSpillagePerDay[k];					-- feed spillage
		myAnimalType.breeding.birthRatePerDay = cow.birthRatePerDay[k];						-- birth rate
		myAnimalType.dirt.cleanDuration = cow.CleanDuration;								-- clean duration, not clear what that is, default used

-- Brahman

		iFillType = RN.cows.fillType2[k];													-- Brahman cows
		myAnimalType = g_animalManager:getAnimalByFillType(iFillType);
		myAnimalType.input.strawPerDay = cow.strawPerAnimalPerGameday[k];					-- straw
		myAnimalType.input.waterPerDay = cow.waterPerAnimalPerGameday[k];					-- water
		myAnimalType.input.foodPerDay = cow.feedPerAnimalPerGameday[k];						-- feed
		myAnimalType.output.milkPerDay = cow.milkPerAnimalPerGameday[k];					-- milk
		myAnimalType.output.manurePerDay = cow.manurePerAnimalPerGameday[k];				-- manure
		myAnimalType.output.liquidManurePerDay = cow.slurryPerAnimalPerGameday[k];			-- liquid manure
		myAnimalType.output.foodSpillagePerDay = cow.feedSpillagePerDay[k];					-- feed spillage
		myAnimalType.breeding.birthRatePerDay = cow.birthRatePerDay[k];						-- birth rate
		myAnimalType.dirt.cleanDuration = cow.CleanDuration;								-- clean duration, not clear what that is, default used
		if isDebug1 then myPrint(RN.cows.color1[k].." fillTypeIndex",iFillType, textWidth, " \n") end;
		if isDebug1 then myPrint(RN.cows.color2[k].." fillTypeIndex",iFillType, textWidth, " \n") end;
    end;

if isDebug then print("\n--------------------SHEEP------------------------------\n") end;

-- sheep pen type 1 to 4

	for k = 1,4 do
		if RN.sheep.growers[k] >= 1 then		--non-breeding pen
			sheep.growers[k] = math.min(RN.sheep.growers[k],sheep.penCapacity[k]);
			sheep.endNumber[k] = sheep.growers[k];
			sheep.startNumber[k] = 0;
		else	-- breeding pen
			sheep.startNumber[k] = RN.sheep.birthingAnimals[k];
			sheep.numberOfOffspring[k] = sheep.startNumber[k] * RN.sheep.litterSize[k] * RN.sheep.gamedaysInPen[k]/RN.sheep.reeringGamedays[k];
			sheep.endNumber[k] = sheep.startNumber[k] + sheep.numberOfOffspring[k];
			sheep.growers[k] = 0;
			if sheep.endNumber[k] >= sheep.penCapacity[k] then
				sheep.startNumber[k] = math.floor(sheep.penCapacity[k]/ (1 + RN.sheep.litterSize[k]));
				sheep.numberOfOffspring[k] = sheep.startNumber[k] * RN.sheep.litterSize[k];
				sheep.endNumber[k] = sheep.startNumber[k] + sheep.numberOfOffspring[k];
			end;
		end;

		if (sheep.startNumber[k] >= 1) and (sheep.startNumber[k] < sheep.endNumber[k]) then
			sheep.x = (sheep.endNumber[k]/sheep.startNumber[k])*(sheep.penCapacity[k]-sheep.startNumber[k])/(sheep.penCapacity[k]-sheep.endNumber[k]);
			sheep.birthRatePerDay[k] = (1/RN.sheep.gamedaysInPen[k])*math.log(sheep.x);
			sheep.y = 1 - (sheep.startNumber[k]/sheep.penCapacity[k])*(1 - math.exp(sheep.birthRatePerDay[k]*RN.sheep.gamedaysInPen[k]));
			sheep.meanNumber[k] = (sheep.penCapacity[k]/(sheep.birthRatePerDay[k]*RN.sheep.gamedaysInPen[k]))*math.log(sheep.y);
			sheep.z = (1+sheep.startNumber[k])*(sheep.penCapacity[k] - sheep.startNumber[k])/(sheep.startNumber[k]*(sheep.penCapacity[k]-sheep.startNumber[k]-1));
			sheep.timeToFirstBirth[k] =24*60*60* math.log(sheep.z)/sheep.birthRatePerDay[k]; -- in seconds
		else
			sheep.birthRatePerDay[k] = 0;
			sheep.meanNumber[k] = sheep.endNumber[k];
			sheep.timeToFirstBirth[k] = 0;
		end;
	end;

    for k = 1,4 do

		sheep.matureWaterPerDay[k] = RN.sheep.matureWaterPerDay[k];
		sheep.growerWaterPerDay[k] = RN.sheep.growerWaterPerDay[k];
		sheep.waterPerMatureAnimalInPen[k] = sheep.matureWaterPerDay[k] * RN.sheep.gamedaysInPen[k] * gameDays.realDaysPerGameDay;
		sheep.waterPerGrowerAnimalInPen[k] = sheep.growerWaterPerDay[k] * RN.sheep.gamedaysInPen[k] * gameDays.realDaysPerGameDay;
		sheep.waterMatureTotalPerPenPeriod[k] = sheep.startNumber[k] * sheep.waterPerMatureAnimalInPen[k];
		sheep.waterGrowerTotalPerPenPeriod[k] = (sheep.endNumber[k]-sheep.startNumber[k]) * sheep.waterPerGrowerAnimalInPen[k];
		sheep.totalWaterPerPenPeriod[k] = sheep.waterMatureTotalPerPenPeriod[k] + sheep.waterGrowerTotalPerPenPeriod[k];
		sheep.totalWaterVolumePerYear[k] = (12/RN.sheep.gamedaysInPen[k]) * sheep.totalWaterPerPenPeriod[k];
		sheep.totalBowserWaterPerYear[k] = RN.sheep.bowserWaterFraction[k] * sheep.totalWaterVolumePerYear[k]
		sheep.waterPerAnimalPerGameday[k] = RN.sheep.bowserWaterFraction[k] * sheep.totalWaterPerPenPeriod[k]/(RN.sheep.gamedaysInPen[k] * sheep.meanNumber[k]);

		sheep.matureFeedPerDay[k] = RN.sheep.matureFeedPerDay[k] / sheep.foodDensity;
		sheep.growerFeedPerDay[k] = RN.sheep.growerFeedPerDay[k] / sheep.foodDensity;
		sheep.feedPerMatureAnimalInPen[k] = sheep.matureFeedPerDay[k] * RN.sheep.gamedaysInPen[k] * gameDays.realDaysPerGameDay;
		sheep.feedPerGrowerAnimalInPen[k] = sheep.growerFeedPerDay[k] * RN.sheep.gamedaysInPen[k] * gameDays.realDaysPerGameDay;
		sheep.feedMatureTotalPerPenPeriod[k] = sheep.startNumber[k] * sheep.feedPerMatureAnimalInPen[k];
		sheep.feedGrowerTotalPerPenPeriod[k] = (sheep.endNumber[k]-sheep.startNumber[k]) * sheep.feedPerGrowerAnimalInPen[k];
		sheep.totalFeedPerPenPeriod[k] = sheep.feedMatureTotalPerPenPeriod[k] + sheep.feedGrowerTotalPerPenPeriod[k];
		sheep.totalFeedVolumePerYear[k] = (12/RN.sheep.gamedaysInPen[k]) * sheep.totalFeedPerPenPeriod[k];
		sheep.totalTroughFeedPerYear[k] = RN.sheep.troughFeedFraction[k] * sheep.totalFeedVolumePerYear[k];
		sheep.feedPerAnimalPerGameday[k] = RN.sheep.troughFeedFraction[k] * sheep.totalFeedPerPenPeriod[k]/(RN.sheep.gamedaysInPen[k] * sheep.meanNumber[k]);

		sheep.hayFeedPerYear[k] = (1 - RN.sheep.grassFeedFraction[k]) * sheep.totalTroughFeedPerYear[k];
		sheep.grassFeedPerYear[k] = RN.sheep.grassFeedFraction[k] * sheep.totalTroughFeedPerYear[k];
		sheep.hayBalesPerYear[k] = tonumber(math.ceil(sheep.hayFeedPerYear[k]/general.hayBaleVolume));
		sheep.grassBalesPerYear[k] = tonumber(math.ceil(sheep.grassFeedPerYear[k]/general.grassBaleVolume));
		sheep.hayFeedHa[k] = sheep.hayFeedPerYear[k] / crop.hayYieldPerHa;
		sheep.grassFeedHa[k] = sheep.grassFeedPerYear[k] / crop.grassYieldPerHa;

		sheep.matureWoolPerDay[k] = RN.sheep.matureWoolPerYear[k] / (360 * general.woolDensity);
		sheep.growerWoolPerDay[k] = RN.sheep.growerWoolPerYear[k] / (360 * general.woolDensity);
		sheep.woolPerMatureAnimalInPen[k] = sheep.matureWoolPerDay[k] * RN.sheep.gamedaysInPen[k] * gameDays.realDaysPerGameDay;
		sheep.woolPerGrowerAnimalInPen[k] = sheep.growerWoolPerDay[k] * RN.sheep.gamedaysInPen[k] * gameDays.realDaysPerGameDay;
		sheep.woolMatureTotalPerPenPeriod[k] = sheep.startNumber[k] * sheep.woolPerMatureAnimalInPen[k];
		sheep.woolGrowerTotalPerPenPeriod[k] = (sheep.endNumber[k]-sheep.startNumber[k]) * sheep.woolPerGrowerAnimalInPen[k];
		sheep.totalWoolPerPenPeriod[k] = sheep.woolMatureTotalPerPenPeriod[k] + sheep.woolGrowerTotalPerPenPeriod[k];
		sheep.totalWoolVolumePerYear[k] = (12/RN.sheep.gamedaysInPen[k]) * sheep.totalWoolPerPenPeriod[k];
		sheep.woolPalletsPerYear[k] = sheep.totalWoolVolumePerYear[k] / RN.sheep.woolVolumePerPallet;
		sheep.woolPalletsPerGameday[k] = sheep.totalWoolPerPenPeriod[k]/(RN.sheep.gamedaysInPen[k] * sheep.meanNumber[k] * RN.sheep.woolVolumePerPallet);
		sheep.feedSpillagePerDay[k] = RN.feedSpillageFraction * sheep.feedPerAnimalPerGameday[k];

		if isDebug then

			print("pen : "..k);
			print(" ");
			print("Pen type        : "..RN.sheep.type[k]);
			print("Animal color    : "..RN.sheep.color[k]);
			print("Animal fillType : "..RN.sheep.fillType[k]);
			print(" ");
			myPrint("sheep start number",sheep.startNumber[k],textWidth," ");
			myPrint("sheep end number",sheep.endNumber[k],textWidth," ");
			myPrint("sheep pen capacity",sheep.penCapacity[k],textWidth," ");
			myPrintFloat("sheep BirthRatePerDay",sheep.birthRatePerDay[k],textWidth," ");
			myPrintFloat("sheep mean number",sheep.meanNumber[k],textWidth," ");
			myPrintTimeStamp("sheep time to first birth",timeStamp(sheep.timeToFirstBirth[k]).." ",textWidth);
			print(" ");
			myPrint("Total sheep water per year", sheep.totalWaterVolumePerYear[k], textWidth," Liter");
			myPrintFloat("Bowser fraction of drinking water", RN.sheep.bowserWaterFraction[k],textWidth," ");
			myPrint("Total sheep bowser water per year", sheep.totalBowserWaterPerYear[k], textWidth," Liter");
			print(" ");
			myPrint("Trough food per year", sheep.totalTroughFeedPerYear[k], textWidth, " Liter");
			myPrintFloat("Hay fraction of trough feed", 1- RN.sheep.grassFeedFraction[k], textWidth, " ");
			myPrintFloat("Grass fraction of trough feed", RN.sheep.grassFeedFraction[k], textWidth, " ");
			myPrint("Total sheep trough hay feed per year", sheep.hayFeedPerYear[k], textWidth," Liter");
			myPrint("Total sheep trough grass feed per year", sheep.grassFeedPerYear[k], textWidth," Liter");
			myPrint("Total sheep hay bales per year", sheep.hayBalesPerYear[k], textWidth," round bales");
			myPrint("Total sheep grass bales per year", sheep.grassBalesPerYear[k], textWidth," round bales");
			myPrintFloat("Hay field for sheep hay feed", sheep.hayFeedHa[k],textWidth," Ha");
			myPrintFloat("Grass field for sheep grass feed", sheep.grassFeedHa[k],textWidth," Ha");
			myPrint("Total wool per year",sheep.totalWoolVolumePerYear[k], textWidth," Liter");
			myPrintFloat("Total wool pallets per year",sheep.woolPalletsPerYear[k], textWidth," ");
			myPrintFloat("sheep water per sheep per game day", sheep.waterPerAnimalPerGameday[k], textWidth, " Liter");
			myPrintFloat("sheep feed per sheep per game day", sheep.feedPerAnimalPerGameday[k], textWidth, " Liter");
			myPrintFloat("wool pallet per sheep per game day", sheep.woolPalletsPerGameday[k], textWidth, " Liter");
			myPrintFloat("sheep food spillage per game day", sheep.feedSpillagePerDay[k], textWidth," Liter");
			print(" ");
		end;

-- set game parameters

        iFillType = RN.sheep.fillType[k];
		myAnimalType = g_animalManager:getAnimalByFillType(iFillType);
		myAnimalType.input.waterPerDay = sheep.waterPerAnimalPerGameday[k];						-- water
		myAnimalType.input.foodPerDay = sheep.feedPerAnimalPerGameday[k];						-- feed
		myAnimalType.output.palletsPerDay = sheep.woolPalletsPerGameday[k];						-- wool
		myAnimalType.output.foodSpillagePerDay = sheep.feedSpillagePerDay[k];					-- feed spillage
		myAnimalType.breeding.birthRatePerDay = sheep.birthRatePerDay[k];						-- birth rate
		myAnimalType.dirt.cleanDuration = sheep.CleanDuration;									-- clean duration, not clear what that is, default used

		if isDebug1 then myPrint(RN.sheep.color[k].." fillTypeIndex",iFillType, textWidth, " \n") end;
    end;


if isDebug then print("\n--------------------CHICKEN------------------------------\n") end;

-- chicken pen type 1 to 4

	for k = 1,4 do
		if RN.chicken.growers[k] >= 1 then		--non-breeding pen
			chicken.growers[k] = math.min(RN.chicken.growers[k],chicken.penCapacity[k]);
			chicken.endNumber[k] = chicken.growers[k];
			chicken.startNumber[k] = 0;
		else	-- breeding pen
			chicken.startNumber[k] = RN.chicken.birthingAnimals[k];
			chicken.numberOfOffspring[k] = chicken.startNumber[k] * RN.chicken.litterSize[k] * RN.chicken.gamedaysInPen[k]/RN.chicken.reeringGamedays[k];
			chicken.endNumber[k] = chicken.startNumber[k] + chicken.numberOfOffspring[k];
			chicken.growers[k] = 0;
			if chicken.endNumber[k] >= chicken.penCapacity[k] then
				chicken.startNumber[k] = math.floor(chicken.penCapacity[k]/ (1 + RN.chicken.litterSize[k]));
				chicken.numberOfOffspring[k] = chicken.startNumber[k] * RN.chicken.litterSize[k];
				chicken.endNumber[k] = chicken.startNumber[k] + chicken.numberOfOffspring[k];
			end;
		end;


		if (chicken.startNumber[k] >= 1) and (chicken.startNumber[k] < chicken.endNumber[k]) then		-- breeding pen
			chicken.x = (chicken.endNumber[k]/chicken.startNumber[k])*(chicken.penCapacity[k]-chicken.startNumber[k])/(chicken.penCapacity[k]-chicken.endNumber[k]);
			chicken.birthRatePerDay[k] = (1/RN.chicken.gamedaysInPen[k])*math.log(chicken.x);
			chicken.y = 1 - (chicken.startNumber[k]/chicken.penCapacity[k])*(1 - math.exp(chicken.birthRatePerDay[k]*RN.chicken.gamedaysInPen[k]));
			chicken.meanNumber[k] = (chicken.penCapacity[k]/(chicken.birthRatePerDay[k]*RN.chicken.gamedaysInPen[k]))*math.log(chicken.y);
			chicken.z = (1+chicken.startNumber[k])*(chicken.penCapacity[k] - chicken.startNumber[k])/(chicken.startNumber[k]*(chicken.penCapacity[k]-chicken.startNumber[k]-1));
			chicken.timeToFirstBirth[k] =24*60*60* math.log(chicken.z)/chicken.birthRatePerDay[k]; -- in seconds
		else		-- non-breeding pen
			chicken.birthRatePerDay[k] = 0;
			chicken.meanNumber[k] = chicken.endNumber[k];
			chicken.timeToFirstBirth[k] = 0;
		end;
	end;

    for k = 1,4 do

		chicken.matureFeedPerDay[k] = RN.chicken.matureFeedPerDay[k] / chicken.foodDensity;
		chicken.growerFeedPerDay[k] = RN.chicken.growerFeedPerDay[k] / chicken.foodDensity;
		chicken.feedPerMatureAnimalInPen[k] = chicken.matureFeedPerDay[k] * RN.chicken.gamedaysInPen[k] * gameDays.realDaysPerGameDay;
		chicken.feedPerGrowerAnimalInPen[k] = chicken.growerFeedPerDay[k] * RN.chicken.gamedaysInPen[k] * gameDays.realDaysPerGameDay;
		chicken.feedMatureTotalPerPenPeriod[k] = chicken.startNumber[k] * chicken.feedPerMatureAnimalInPen[k];
		chicken.feedGrowerTotalPerPenPeriod[k] = (chicken.endNumber[k]-chicken.startNumber[k]) * chicken.feedPerGrowerAnimalInPen[k];
		chicken.totalFeedPerPenPeriod[k] = chicken.feedMatureTotalPerPenPeriod[k] + chicken.feedGrowerTotalPerPenPeriod[k];
		chicken.totalFeedVolumePerYear[k] = (12/RN.chicken.gamedaysInPen[k]) * chicken.totalFeedPerPenPeriod[k];
		chicken.feedPerAnimalPerGameday[k] =  chicken.totalFeedPerPenPeriod[k]/(RN.chicken.gamedaysInPen[k] * chicken.meanNumber[k]);

		chicken.neededWheatHa[k] = chicken.totalFeedVolumePerYear[k] / crop.wheatYieldPerHa;
		chicken.neededBarleyHa[k] = chicken.totalFeedVolumePerYear[k] / crop.barleyYieldPerHa;
		chicken.neededOatHa[k] = chicken.totalFeedVolumePerYear[k] / crop.oatYieldPerHa;		-- may not be used

		chicken.matureEggsPerDay[k] = RN.chicken.LiterPerEgg * RN.chicken.matureEggsPerYear[k] / 360;
		chicken.growerEggsPerDay[k] = RN.chicken.LiterPerEgg * RN.chicken.growerEggsPerYear[k] / 360;
		chicken.eggsPerMatureAnimalInPen[k] = chicken.matureEggsPerDay[k] * RN.chicken.gamedaysInPen[k] * gameDays.realDaysPerGameDay;
		chicken.eggsPerGrowerAnimalInPen[k] = chicken.growerEggsPerDay[k] * RN.chicken.gamedaysInPen[k] * gameDays.realDaysPerGameDay;
		chicken.eggsMatureTotalPerPenPeriod[k] = chicken.startNumber[k] * chicken.eggsPerMatureAnimalInPen[k];
		chicken.eggsGrowerTotalPerPenPeriod[k] = (chicken.endNumber[k]-chicken.startNumber[k]) * chicken.eggsPerGrowerAnimalInPen[k];
		chicken.totalEggsPerPenPeriod[k] = chicken.eggsMatureTotalPerPenPeriod[k] + chicken.eggsGrowerTotalPerPenPeriod[k];
		chicken.totalEggVolumePerYear[k] = (12/RN.chicken.gamedaysInPen[k]) * chicken.totalEggsPerPenPeriod[k];
		chicken.eggPalletsPerYear[k] = chicken.totalEggVolumePerYear[k] / RN.chicken.eggVolumePerPallet;
		chicken.eggPalletsPerGameday[k] = chicken.totalEggsPerPenPeriod[k]/(RN.chicken.gamedaysInPen[k] * chicken.meanNumber[k] * RN.chicken.eggVolumePerPallet);
		chicken.feedSpillagePerDay[k] = RN.feedSpillageFraction * chicken.feedPerAnimalPerGameday[k];

		if isDebug then

			print("pen : "..k);
			print(" ");
			print("Pen type        : "..RN.chicken.type[k]);
			print("Animal color    : "..RN.chicken.color[k]);
			print("Animal fillType : "..RN.chicken.fillType[k]);
			print(" ");
			myPrint("chicken start number",chicken.startNumber[k],textWidth," ");
			myPrint("chicken end number",chicken.endNumber[k],textWidth," ");
			myPrint("chicken pen capacity",chicken.penCapacity[k],textWidth," ");
			myPrintFloat("chicken BirthRatePerDay",chicken.birthRatePerDay[k],textWidth," ");
			myPrintFloat("chicken mean number",chicken.meanNumber[k],textWidth," ");
			myPrintTimeStamp("chicken time to first birth",timeStamp(chicken.timeToFirstBirth[k]).." ",textWidth);
			print(" ");
			myPrint("Trough food per year", chicken.totalFeedVolumePerYear[k], textWidth, " Liter");
			myPrintFloat("Wheat field for chicken feed", chicken.neededWheatHa[k],textWidth," Ha");
			myPrintFloat("Barley field for chicken feed", chicken.neededBarleyHa[k],textWidth," Ha");
			myPrint("Total egg volume per year",chicken.totalEggVolumePerYear[k], textWidth," Liter");
			myPrint("Total egg pallets per year",chicken.eggPalletsPerYear[k], textWidth," ");
			print(" ");
			myPrintFloat("chicken feed per chicken per game day", chicken.feedPerAnimalPerGameday[k], textWidth, " Liter");
			myPrintFloat("egg pallets per chicken per game day", chicken.eggPalletsPerGameday[k], textWidth, " ");
			myPrintFloat("chicken food spillage per game day", chicken.feedSpillagePerDay[k], textWidth," Liter\n");
			print(" ");
		end;

-- set game parameters


        iFillType = RN.chicken.fillType[k];
		myAnimalType = g_animalManager:getAnimalByFillType(iFillType);
		myAnimalType.input.foodPerDay = chicken.feedPerAnimalPerGameday[k];						-- feed
		myAnimalType.output.palletsPerDay = chicken.eggPalletsPerGameday[k];					-- wool
		myAnimalType.output.foodSpillagePerDay = chicken.feedSpillagePerDay[k];					-- feed spillage
		myAnimalType.breeding.birthRatePerDay = chicken.birthRatePerDay[k];						-- birth rate
		myAnimalType.dirt.cleanDuration = chicken.CleanDuration;								-- clean duration, not clear what that is, default used

		if isDebug1 then myPrint(RN.chicken.color[k].." fillTypeIndex",iFillType, textWidth, " \n") end;
    end;

-- horses

	horse.number = math.min(RN.horses.numberOfMatureAnimals,horse.penCapacity[4]);

	RN.horses.beddingStrawPerHorsePerMonth = RN.horses.beddingStrawIntensity * RN.commodityVolume.ROUNDBALE_WHEAT;
	RN.horses.beddingStrawPerHorsePerDay = RN.horses.beddingStrawPerHorsePerMonth / 30;
	RN.horses.waterPerHorsePerRealDay = RN.horses.matureWeight * RN.horses.waterIntakePerKg;
	RN.horses.feedMassPerHorsePerRealDay = RN.horses.matureWeight * RN.horses.feedPerHorsePerDayPerKg;	
	
	horse.beddingStrawPerHorsePerGameDay = gameDays.realDaysPerGameDay * RN.horses.beddingStrawPerHorsePerDay;
	horse.beddingStrawPerHorsePerYear = gameDays.gameDaysPerYear * horse.beddingStrawPerHorsePerGameDay;
	horse.beddingStrawPerYear = horse.number * horse.beddingStrawPerHorsePerYear;
	horse.neededBeddingWheatFieldPerYear = horse.beddingStrawPerYear / crop.wheatStrawYieldPerHa;
	horse.neededBeddingBarleyFieldPerYear = horse.beddingStrawPerYear / crop.barleyStrawYieldPerHa;
	horse.neededBeddingOatFieldPerYear = horse.beddingStrawPerYear / crop.oatStrawYieldPerHa;
	horse.beddingBalesPerYear = tonumber(math.ceil(horse.beddingStrawPerYear/general.strawBaleVolume));

	horse.waterPerHorsePerGameDay = gameDays.realDaysPerGameDay * RN.horses.waterPerHorsePerRealDay;
	horse.waterPerHorsePerYear = gameDays.gameDaysPerYear * horse.waterPerHorsePerGameDay;
	horse.waterPerYear = horse.number * horse.waterPerHorsePerYear;

	horse.feedVolumePerHorsePerRealDay = RN.horses.feedMassPerHorsePerRealDay / (0.4 * general.hayBaleDensity + 0.6 * general.oatDensity);
	horse.feedVolumePerHorsePerGameDay = gameDays.gameDaysPerYear * horse.feedVolumePerHorsePerRealDay;
	horse.feedVolumePerHorsePerYear = gameDays.gameDaysPerYear * horse.feedVolumePerHorsePerGameDay
	horse.feedVolumePerYear = horse.number * horse.feedVolumePerHorsePerYear;

	
	
	
	
	horse.feedOatPerYear = 0.6 * horse.feedVolumePerYear;		-- Oat is 60% of food in FS19
	horse.feedHayPerYear = 0.4 * horse.feedVolumePerYear;		-- Hay is 40% of food in FS19
	horse.balesPerYear = tonumber(math.ceil(horse.feedHayPerYear/general.hayBaleVolume));
	horse.neededOatHa = horse.feedOatPerYear / crop.oatYieldPerHa;
	horse.neededHayHa = horse.feedHayPerYear / crop.hayYieldPerHa;

	horse.foodSpillagePerDay = RN.feedSpillageFraction;

	if isDebug then
		print("\n--------------------HORSES------------------------------\n");
		myPrint("Horse start number", horse.number,textWidth," ");
		myPrint("Total horse water per year", horse.waterPerYear,textWidth," Liter");

		myPrint("Horse bedding intensity ( 0...100)", 100*RN.horses.beddingStrawIntensity, textWidth, " %");
		myPrint("Total horse bedding straw per year", horse.beddingStrawPerYear, textWidth, " Liter");
		myPrint("Total horse bedding straw bales per year", horse.beddingBalesPerYear, textWidth," round bales");
		myPrintFloat("Wheat field for horse bedding straw", horse.neededBeddingWheatFieldPerYear,textWidth," Ha or");
		myPrintFloat("Barley field for horse bedding straw", horse.neededBeddingBarleyFieldPerYear,textWidth," Ha or");
		myPrintFloat("Oat field for horse bedding straw", horse.neededBeddingOatFieldPerYear,textWidth," Ha");

		myPrint("Total horse feed per year", horse.feedVolumePerYear, textWidth, " Liter");
		myPrint("Horse oat feed per year", horse.feedOatPerYear, textWidth, " Liter");
		myPrint("Horse hay feed per year", horse.feedHayPerYear, textWidth, " Liter");
		myPrint("Total horse hay bales per year", horse.balesPerYear, textWidth," round bales");
		myPrintFloat("Oat field for horse oat feed", horse.neededOatHa,textWidth," Ha");
		myPrintFloat("Hay field for horse hay feed", horse.neededHayHa,textWidth," Ha");
		myPrintFloat("Bedding straw per horse per game day", horse.beddingStrawPerHorsePerGameDay, textWidth, " Liter");
		myPrintFloat("Horse water per horse per game day", horse.waterPerHorsePerGameDay, textWidth, " Liter");
		myPrintFloat("Horse feed per horse per game day", horse.feedVolumePerHorsePerGameDay, textWidth, " Liter\n");
	end;

	for k = 1,8 do
		iFillType = RN.horses.fillType[k];
		myAnimalType = g_animalManager:getAnimalByFillType(iFillType);
		myAnimalType.input.strawPerDay = horse.beddingStrawPerHorsePerGameDay;
		myAnimalType.input.waterPerDay = horse.waterPerHorsePerGameDay;
		myAnimalType.input.foodPerDay = horse.feedVolumePerHorsePerGameDay;
		myAnimalType.output.manurePerDay = 0;
		myAnimalType.output.liquidManurePerDay = 0;
		myAnimalType.output.milkPerDay = 0;
		myAnimalType.output.foodSpillagePerDay = horse.foodSpillagePerDay;
		myAnimalType.breeding.birthRatePerDay = 0;
		myAnimalType.dirt.cleanDuration = horse.CleanDuration;
	end;

end;

function RealNumbersAnimalCare:keyEvent(unicode, sym, modifier, isDown)
end;
function RealNumbersAnimalCare:update(dt)
end;
function RealNumbersAnimalCare:draw()
end;
function RealNumbersAnimalCare:deleteMap()
end;
function RealNumbersAnimalCare:mouseEvent(posX, posY, isDown, isUp, button)
end;

addModEventListener(RealNumbersAnimalCare);
