--
-- Mod: FS19_RealLifeNumbers by Kaj-Aage Henneberg "Ki", ArmChairFarming
-- Script: RealNumbersContracts 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

--[[

	Added with version 1.1.0.0

	With this script the user can set up a farming contractor service and
	supplement the income from farming with high level contracting jobs using
	your own equipment.

	This is different from the concept of "missions", where a person will go around
	and do odd jobs for a farmer, using the farmers equipment.

	Contract (mission) earnings is split into the following components:

	Start fee
	Labour
	Vehicle use cost
	Farm service

	All parameters are set in RealLifeNumbersInitalization.lua

	Example output to log file:

	sow field                                =        21
	sow fieldArea                            =        1.2233 Ha
	sow SalaryPerHa                          =        5.0000 €/Ha
	sow Start fee                            =       20.0000 €
	sow Labor total                          =        6.1164 €
	sow vehicleUseCost                       =        6.1164 €
	sow farmService                          =       48.9310 €
	sow Total                                =      111.1638 €

	SalaryPerHa is calculated from the wage and the number of hectares worked per hour.

	For sowing RN.missionPayPerHour = 10 €/hour
	For sowing RN.missionHaPerHour = 2 ha/hour
	Hence for sowing, the fee becomes 10/2 = 5€/ha

	The user can define the wages and work capacity for each contract type
	in RealLifeNumbersInitalization.lua. With respect to wages, one should
	consider how much training is needed to do the job. Handling herbicide
	requires much more training than spraying dry fertilizer, hence the
	wages is set higher for spraying than for fertilizing.

	It is up to the user to define the conditions in contracts.

	PS. FS19 gives a fraction of the payback for sold fruits to the contractor.
	The mod does not change this, as it would make the information on contracts event
	even more confusing.

--]]

RealNumbersContracts = {};

function RealNumbersContracts:loadMap()
	if RN.runContractScript then
		print("\nFS19_RealLifeNumbers: RealNumbersContracts active, press lAlt-rc (if on host PC)\n");
	else
		print("\nFS19_RealLifeNumbers: RealNumbersContracts active, but not 100% multiplayer ready\n");
	end;
	MissionManager.CATEGORY_FIELD = 1.000000;
	MissionManager.AI_PRICE_MULTIPLIER = 1.000000;
	MissionManager.MAX_MISSIONS_PER_GENERATION = 4.000000;
	MissionManager.CATEGORY_GRASS_FIELD = 3.000000;
	MissionManager.MAX_TRIES_PER_GENERATION = 5.000000;
	MissionManager.MAX_MISSIONS = 25.000000;
	MissionManager.MISSION_GENERATION_INTERVAL = 14400000.000000;
	MissionManager.CATEGORY_TRANSPORT = 2.000000;
	MissionManager.MAX_TRANSPORT_MISSIONS = 2.000000;

	AbstractFieldMission.VEHICLE_USE_COST = 10.000000;
	AbstractFieldMission.classId = 68.000000;
	AbstractFieldMission.FIELD_SIZE_LARGE = 5.000000;
	AbstractFieldMission.className = 'AbstractFieldMission';
	AbstractFieldMission.REWARD_PER_HA = 100.000000;
	AbstractFieldMission.REIMBURSEMENT_FACTOR = 0.950000;
	AbstractFieldMission.FIELD_SIZE_MEDIUM = 1.500000;
end;


function RealNumbersContracts:keyEvent(unicode, sym, modifier, isDown)
	local textWidth = 40;
	local kmFee = 0;
	local objectFee = 0;
	local isDebug = RN.isDebug;
	local currency = "€";
	local fruitTypeIndex = 0;
	local fruitName = "";
	local fruitType = {};
	local isHost = true;

	local gameMoneyUnit = g_i18n.moneyUnit;
	if gameMoneyUnit == 1 then
        currency = "€";
	elseif gameMoneyUnit == 2 then
        currency = "$";
	elseif gameMoneyUnit == 3 then
        currency = "£";
	end

	local savegameDirectory = g_currentMission.missionInfo.savegameDirectory;	-- works except when a new map is opened the first time
	if (savegameDirectory == nil) and (RN.multiPlayer) then isHost = false; end;	-- is this PC multiplayer host (true) or client (false)?


	if RN.runContractScript then		-- only run if set in RealNumbersInitialization

		if Input.isKeyPressed(Input.KEY_lalt) and Input.isKeyPressed(Input.KEY_r) and Input.isKeyPressed(Input.KEY_c) then

			if isDebug	then
				print("\nlAlt-rc pressed\n");
				print("General fees for contracts");
				print(" ");
			end;

			for k,missionType in pairs(g_missionManager.missionTypes) do
				if missionType ~= nil then
					local name = missionType.name;
					if name == "transport" then
						myPrint(name.." fee per object ", RN.objectFee, textWidth, currency.."/object");
						myPrint(name.." default duration ", RN.transportHour, textWidth, " hour(s)");
						myPrint(name.." vehicle use cost per hour ", RN.vehicleUseCostPerHour[k], textWidth, currency.."/hour");
						myPrint(name.." salary per hour ", RN.missionPayPerHour[k], textWidth, currency.."/hour");
						myPrint(name.." start fee ", RN.transportStart, textWidth, currency.."/hour\n");
					else
						myPrint(name.." farmServicePerHa ", RN.farmServicePerHa[k], textWidth, currency.."/Ha");
						myPrint(name.." hectares per hour ", RN.missionHaPerHour[k], textWidth, "Ha/hour");
						myPrint(name.." vehicle use cost per hour ", RN.vehicleUseCostPerHour[k], textWidth, currency.."/hour");
						myPrint(name.." salary per hour ", RN.missionPayPerHour[k], textWidth, currency.."/hour");
						myPrint(name.." start fee ", RN.missionStart, textWidth, currency.."/hour\n");
					end;
				end;
			end;

			print(" ");
			if isDebug	then
				print("Contracts for available jobs");
				print("  ");
				print("An empty list indicates that either no contracts are defined");
				print("or that you are not the host of a multiplayer game");
				print("  ");
			end;

--			print_r(RN.missions);
	
			if isHost then
			for k,mission in pairs(g_missionManager.missions) do
				if mission == nil then
					print(" g_missionManager.missions = nil ");
				else
					if mission.type == nil then
						print(" g_missionManager.missions.type = nil ");
					else
						local missiontype = mission.type;
						local name = missiontype.name;
						local numObjects = mission.numObjects;
						local payment = mission.reward;
						if name == "transport" then
							local vehicleUseCost = RN.transportHour * RN.vehicleUseCostPerHour[9];
							local objectFeeTotal = numObjects * RN.objectFee;
							local salary = RN.transportHour * RN.missionPayPerHour[9];
							local totalTransportFee = RN.transportStart + vehicleUseCost + objectFeeTotal + salary;
							myPrint(name.." number of objects ", numObjects,textWidth," ");
							myPrintFloat(name.." fee per object ", RN.objectFee, textWidth, currency.."/object");
							myPrintFloat(name.." start fee ", RN.transportStart, textWidth, currency);
							myPrintFloat(name.." object fee ", objectFeeTotal, textWidth, currency);
							myPrintFloat(name.." vehicleUse fee ", vehicleUseCost, textWidth, currency);
							myPrintFloat(name.." salary ", salary, textWidth, currency);
							myPrintFloat(name.." total ", payment, textWidth, currency.."\n");

						else
							if mission.field == nil then
								print(" g_missionManager.missions.field = nil ");
							else
								local missionfield = mission.field;
								local fieldArea = missionfield.fieldArea;
								local fieldId = missionfield.fieldId;


								if mission.fruitType ~= nil then
									fruitTypeIndex = mission.fruitType;
								elseif missionfield.fruitType ~= nil then
									fruitTypeIndex = missionfield.fruitType;
								end;
								fruitType = g_fruitTypeManager:getFruitTypeByIndex(fruitTypeIndex);
								fruitName = fruitType.name;

								myPrint(name.." field ", fieldId, textWidth," ");
								if RN.fruits[fruitName] == nil then
									print(" multifruit type not defined in RealLifeNumbers");
								end;
								myPrint(name.." crop: "..fruitName, fruitTypeIndex, textWidth," ");
								myPrintFloat(name.." field area ", fieldArea, textWidth,"Ha");
								if mission.sprayFactor ~= nil then
									myPrint(name.." fertilizer state", mission.sprayFactor*100, textWidth,"%");
								else
									print("mission.sprayFactor not defined, can happen in multiplayer mode");
								end;
								if mission.limeFactor ~= nil then
									myPrint(name.." lime state", mission.limeFactor*100, textWidth,"%");
								else
									print("mission.limeFactor not defined, can happen in multiplayer mode");
								end;
								if mission.weedFactor ~= nil then
									myPrint(name.." herbicide state", mission.weedFactor*100, textWidth,"%");
								else
									print("mission.weedFactor not defined, can happen in multiplayer mode");
								end;
								if mission.fieldPlowFactor ~= nil then
									myPrint(name.." plow state", mission.fieldPlowFactor*100, textWidth,"%");
								else
									print("mission.fieldPlowFactor not defined, can happen in multiplayer mode");
								end;
								myPrint(name.." yield factor", mission.yieldFactor*100, textWidth,"%");
								myPrintFloat(name.." salary per Ha ", mission.rewardPerHa, textWidth, currency.."/Ha");
								myPrintFloat(name.." start fee ", RN.missionStart, textWidth, currency);
								myPrintFloat(name.." labor total ", fieldArea * mission.rewardPerHa, textWidth, currency);
								myPrintFloat(name.." vehicle use cost ", mission.vehicleUseCost, textWidth, currency);
								myPrintFloat(name.." farmService ", mission.serviceCost, textWidth, currency);
								if name == "sow" then
									myPrintFloat(name.." seed cost ", mission.seedCost, textWidth, currency);
								end;
								if name == "spray" then myPrintFloat(name.." spray cost ", mission.sprayCost, textWidth, currency); end;
								if name == "fertilize" then myPrintFloat(name.." fertilizer cost ", mission.fertilizerCost, textWidth, currency); end;
								myPrintFloat(name.." total ", mission.reward, textWidth, currency.." ");
								if name == "mow_bale" then
									print(name.." deliver to "..mission.sellPoint.stationName);
								end;
								if name == "harvest" then
									print(name.." deliver to "..mission.sellPoint.stationName);
									if (RN.fruits[fruitName] ~= nil) then
										myPrintFloat(name.." crop price/ha ", RN.cropPricePerHa[fruitName], textWidth, currency.."/Ha");
										myPrintFloat(name.." max yield/ha ", RN.cropBonusYieldLitersPerHa[fruitName], textWidth, "L/Ha");
										myPrintFloat(name.." max crop price ", fieldArea * RN.cropPricePerHa[fruitName], textWidth, currency);
										myPrintFloat(name.." max yield ", fieldArea * RN.cropBonusYieldLitersPerHa[fruitName], textWidth, "L");
										myPrintFloat(name.." expected yield ", mission.expectedLiters, textWidth, "L");
										myPrintFloat(name.." standard crop price/L ", RN.cropPricePerLiter[fruitName], textWidth, currency.."/L");
										myPrintFloat(name.." expected crop price ", mission.cropPrice, textWidth, currency);
									end;
								end;
								if name == "sow" then
									myPrintFloat(name.." seed liter/ha ", mission.seedRatePerHa, textWidth, "L/Ha");
									myPrintFloat(name.." seed cost/Liter ", mission.seedCostPerLiter, textWidth, currency.."/L");
									myPrintFloat(name.." seed cost/ha ", mission.seedCostPerHa, textWidth, currency.."/Ha");
								end;
								if name == "spray" then
									myPrintFloat(name.." spray liter/ha ", mission.sprayVolume, textWidth, "L/Ha");
									myPrintFloat(name.." spray cost/Liter ", mission.sprayPrice, textWidth, currency.."/L");
									myPrintFloat(name.." spray cost/ha ", mission.sprayPricePerHa, textWidth, currency.."/Ha");
								end;
								if name == "fertilize" then
									myPrintFloat(name.." fertilizer liter/ha ", mission.fertilizerVolume, textWidth, "L/Ha");
									myPrintFloat(name.." fertilizer cost/Liter ", mission.fertilizerPrice, textWidth, currency.."/L");
									myPrintFloat(name.." fertilizer cost/ha ", mission.fertilizerPricePerHa, textWidth, currency.."/Ha");
								end;
								print(" ");
							end;
						end;
					end;
				end;
			end;
			end;
		end;

--		DebugUtil.printTableRecursively(g_missionManager.missions,"_",0,3);
	end;

end;

function RealNumbersContracts:update(dt)

		local fruitTypeIndex = 0;
		local fruitType = {};
		local fruitName = " ";
		local missionfield = {};
		local fieldArea = 0;
		local fieldId = 0;
		local isHost = true;

		local savegameDirectory = g_currentMission.missionInfo.savegameDirectory;	-- works except when a new map is opened the first time
		if (savegameDirectory == nil) and (RN.multiPlayer) then isHost = false; end;	-- is this PC multiplayer host (true) or client (false)?

		for k,missionType in pairs(g_missionManager.missionTypes) do
			if missionType ~= nil then
				local name = missionType.name;
				if name == "mow_bale" then
					missionType.class.REWARD_PER_HA_HAY = RN.missionPayPerHour[1]/RN.missionHaPerHour[1];
					missionType.class.REWARD_PER_HA_SILAGE  = RN.missionPayPerHour[1]/RN.missionHaPerHour[1];
					BaleMission.REWARD_PER_HA_HAY = RN.missionPayPerHour[1]/RN.missionHaPerHour[1];
					BaleMission.REWARD_PER_HA_SILAGE = RN.missionPayPerHour[1]/RN.missionHaPerHour[1];
				end;
				if name == "plow" then
					missionType.class.REWARD_PER_HA = RN.missionPayPerHour[2]/RN.missionHaPerHour[2];
					PlowMission.REWARD_PER_HA = RN.missionPayPerHour[2]/RN.missionHaPerHour[2];
				end;
				if name == "cultivate" then
					missionType.class.REWARD_PER_HA = RN.missionPayPerHour[3]/RN.missionHaPerHour[3];
					CultivateMission.REWARD_PER_HA = RN.missionPayPerHour[3]/RN.missionHaPerHour[3];
				end;
				if name == "sow" then
					missionType.class.REWARD_PER_HA = RN.missionPayPerHour[4]/RN.missionHaPerHour[4];
					SowMission.REWARD_PER_HA = RN.missionPayPerHour[4]/RN.missionHaPerHour[4];
				end;
				if name == "harvest" then
					HarvestMission.REWARD_PER_HA_SMALL = RN.missionPayPerHour[5]/RN.missionHaPerHour[5];
					missionType.class.REWARD_PER_HA_SMALL  = RN.missionPayPerHour[5]/RN.missionHaPerHour[5];
					HarvestMission.REWARD_PER_HA_WIDE = RN.missionPayPerHour[5]/RN.missionHaPerHour[5];
					missionType.class.REWARD_PER_HA_WIDE = RN.missionPayPerHour[5]/RN.missionHaPerHour[5];
				end;
				if name == "weed" then
					missionType.class.REWARD_PER_HA = RN.missionPayPerHour[6]/RN.missionHaPerHour[6];
					WeedMission.REWARD_PER_HA = RN.missionPayPerHour[6]/RN.missionHaPerHour[6];
				end;
				if name == "spray" then
					missionType.class.REWARD_PER_HA = RN.missionPayPerHour[7]/RN.missionHaPerHour[7];
					missionType.class.REIMBURSEMENT_PER_HA  = RN.sprayCostHERBICIDE;
					SprayMission.REWARD_PER_HA = RN.missionPayPerHour[7]/RN.missionHaPerHour[7];
					SprayMission.REIMBURSEMENT_PER_HA = RN.sprayCostHERBICIDE;
				end;
				if name == "fertilize" then
					missionType.class.REWARD_PER_HA = RN.missionPayPerHour[8]/RN.missionHaPerHour[8];
					missionType.class.REIMBURSEMENT_PER_HA  = RN.sprayCostPerHa["WHEAT"]["FERTILIZER"];
					FertilizeMission.REWARD_PER_HA = RN.missionPayPerHour[8]/RN.missionHaPerHour[8];
					FertilizeMission.REIMBURSEMENT_PER_HA = RN.sprayCostPerHa["WHEAT"]["FERTILIZER"];
				end;
				if name == "transport" then
					missionType.class.REWARD_PER_METER  = RN.missionPayPerHour[9]/30000;  --Hour rate divided by mean velocity (meters per hour)
					missionType.class.REWARD_PER_OBJECT = RN.objectFee;
					TransportMission.REWARD_PER_METER = RN.missionPayPerHour[9]/30000;
					TransportMission.REWARD_PER_OBJECT = RN.objectFee;
				end;
			end;
		end;
	if isHost then			-- only run if this PC is host
		for k,mission in pairs(g_missionManager.missions) do
			fruitTypeIndex = 0;
			fruitName = " ";
			local yieldFactor = 0;
			if mission ~= nil then
				RN.missions[k] = {};

				local name = mission.type.name;
				RN.missions[k]["name"] = name;
				if name == "transport" then
					local numObjects = mission.numObjects;
					local vehicleUseCost = RN.transportHour * RN.vehicleUseCostPerHour[9];
					local objectFeeTotal = numObjects * RN.objectFee;
					local salary = RN.transportHour * RN.missionPayPerHour[9];
					local totalTransportFee = RN.transportStart + vehicleUseCost + objectFeeTotal + salary;
					mission.reward = totalTransportFee;
					RN.missions[k]["reward"] = totalTransportFee;
					RN.missions[k]["numObjects"] = numObjects;
				else


					if mission.field == nil then
							print(" g_missionManager.missions.field = nil ");
					else
						missionfield = mission.field;
						fieldArea = missionfield.fieldArea;
						fieldId = missionfield.fieldId;
						if missionfield.fruitType == nil then
							fruitTypeIndex = nil;
							fruitName = nil;
						else
							fruitTypeIndex = missionfield.fruitType;
							fruitType = g_fruitTypeManager:getFruitTypeByIndex(fruitTypeIndex);
							fruitName = fruitType.name;
						end;
						RN.missions[k]["fruitTypeIndex"] = fruitTypeIndex;
						RN.missions[k]["fieldId"] = fieldId;
						RN.missions[k]["fieldArea"] = fieldArea;
						RN.missions[k]["fruitName"] = fruitName;
						if mission.sprayFactor ~= nil then
							yieldFactor = yieldFactor + 2 * mission.sprayFactor * 0.25;		-- fertilizing state
						end;
						if mission.limeFactor ~= nil then
							yieldFactor = yieldFactor + mission.limeFactor * 0.15;			-- lim state
						end;
						if mission.weedFactor ~= nil then
							yieldFactor = yieldFactor + mission.weedFactor * 0.20;			-- weed state
						end;
						if mission.weedFactor ~= nil then
							yieldFactor = yieldFactor + mission.fieldPlowFactor * 0.15;		-- plow state
						end;
					end;
					mission.yieldFactor = 1 + yieldFactor;
					RN.missions[k]["sprayFactor"] = mission.sprayFactor;
					RN.missions[k]["limeFactor"] = mission.limeFactor;
					RN.missions[k]["weedFactor"] = mission.weedFactor;
					RN.missions[k]["fieldPlowFactor"] = mission.fieldPlowFactor;
					RN.missions[k]["yieldFactor"] = mission.yieldFactor;

					if name == "mow_bale" then
						mission.rewardPerHa = RN.missionPayPerHour[1]/RN.missionHaPerHour[1];
						mission.vehicleUseCost = fieldArea * RN.vehicleUseCostPerHour[1]/RN.missionHaPerHour[1];
						mission.serviceCost = fieldArea * RN.farmServicePerHa[1];
						mission.reward = RN.missionStart + mission.serviceCost + mission.vehicleUseCost + fieldArea * mission.rewardPerHa;
						RN.missions[k]["rewardPerHa"] = mission.rewardPerHa;
						RN.missions[k]["vehicleUseCost"] = mission.vehicleUseCost;
						RN.missions[k]["serviceCost"] = mission.serviceCost;
						RN.missions[k]["reward"] = mission.reward;
						RN.missions[k]["sellPoint"] = mission.sellPoint.stationName;
					end;
					if name == "plow" then
						mission.rewardPerHa = RN.missionPayPerHour[2]/RN.missionHaPerHour[2];
						mission.vehicleUseCost = fieldArea * RN.vehicleUseCostPerHour[2]/RN.missionHaPerHour[2];
						mission.serviceCost = fieldArea * RN.farmServicePerHa[2];
						mission.reward = RN.missionStart + mission.serviceCost + mission.vehicleUseCost + fieldArea * mission.rewardPerHa;
						RN.missions[k]["rewardPerHa"] = mission.rewardPerHa;
						RN.missions[k]["vehicleUseCost"] = mission.vehicleUseCost;
						RN.missions[k]["serviceCost"] = mission.serviceCost;
						RN.missions[k]["reward"] = mission.reward;
					end;
					if name == "cultivate" then
						mission.rewardPerHa = RN.missionPayPerHour[3]/RN.missionHaPerHour[3];
						mission.vehicleUseCost = fieldArea * RN.vehicleUseCostPerHour[3]/RN.missionHaPerHour[3];
						mission.serviceCost = fieldArea * RN.farmServicePerHa[3];
						mission.reward = RN.missionStart + mission.serviceCost + mission.vehicleUseCost + fieldArea * mission.rewardPerHa;
						RN.missions[k]["rewardPerHa"] = mission.rewardPerHa;
						RN.missions[k]["vehicleUseCost"] = mission.vehicleUseCost;
						RN.missions[k]["serviceCost"] = mission.serviceCost;
						RN.missions[k]["reward"] = mission.reward;
					end;
					if name == "sow" then
						if mission.fruitType == nil then
							fruitTypeIndex = missionfield.fruitType;
						else
							fruitTypeIndex = mission.fruitType;
						end;
						fruitType = g_fruitTypeManager:getFruitTypeByIndex(fruitTypeIndex);
						fruitName = fruitType.name;
						mission.rewardPerHa = RN.missionPayPerHour[4]/RN.missionHaPerHour[4];
						mission.vehicleUseCost = fieldArea * RN.vehicleUseCostPerHour[4]/RN.missionHaPerHour[4];
						mission.serviceCost = fieldArea * RN.farmServicePerHa[4];
						if (RN.fruits[fruitName] ~= nil) then	-- defined fruits
							mission.reimbursementPerHa = RN.seedCostPerHa[fruitName];
							mission.seedCost = fieldArea * RN.seedCostPerHa[fruitName];		-- add reimbursement to reward
							mission.seedRatePerHa = RN.seedVolumeRatePerHa[fruitName];
							mission.seedCostPerLiter = RN.seedCostPerLiter[fruitName];
							mission.seedCostPerHa = RN.seedCostPerHa[fruitName];
						else									-- for undefined fruits use WHEAT parameters
							mission.reimbursementPerHa = RN.seedCostPerHa["WHEAT"];
							mission.seedCost = fieldArea * RN.seedCostPerHa["WHEAT"];
							mission.seedRatePerHa = RN.seedVolumeRatePerHa["WHEAT"];
							mission.seedCostPerLiter = RN.seedCostPerLiter["WHEAT"];
							mission.seedCostPerHa = RN.seedCostPerHa["WHEAT"];
						end
						mission.reward = RN.missionStart + mission.serviceCost + mission.vehicleUseCost + fieldArea * mission.rewardPerHa + mission.seedCost;
						mission.fieldSpraySet  = true;
						RN.missions[k]["rewardPerHa"] = mission.rewardPerHa;
						RN.missions[k]["vehicleUseCost"] = mission.vehicleUseCost;
						RN.missions[k]["serviceCost"] = mission.serviceCost;
						RN.missions[k]["reimbursementPerHa"] = mission.reimbursementPerHa;
						RN.missions[k]["seedCost"] = mission.seedCost;
						RN.missions[k]["seedRatePerHa"] = mission.seedRatePerHa;
						RN.missions[k]["seedCostPerLiter"] = mission.seedCostPerLiter;
						RN.missions[k]["seedCostPerHa"] = mission.seedCostPerHa;
						RN.missions[k]["reward"] = mission.reward;
					end;
					if name == "harvest" then
						if mission.fruitType == nil then
							fruitTypeIndex = missionfield.fruitType;
						else
							fruitTypeIndex = mission.fruitType;
						end;
						fruitType = g_fruitTypeManager:getFruitTypeByIndex(fruitTypeIndex);
						fruitName = fruitType.name;
						mission.rewardPerHa  = RN.missionPayPerHour[5]/RN.missionHaPerHour[5];
						mission.vehicleUseCost = fieldArea * RN.vehicleUseCostPerHour[5]/RN.missionHaPerHour[5];
						mission.serviceCost = fieldArea * RN.farmServicePerHa[5];
						if (RN.fruits[fruitName] ~= nil) then	-- defined fruits
							mission.expectedLiters = fieldArea * mission.yieldFactor * 0.5 * RN.cropBonusYieldLitersPerHa[fruitName];
							mission.cropPrice = RN.cropPricePerLiter[fruitName] * mission.expectedLiters;
						else
							mission.expectedLiters = 0;
							mission.cropPrice = 0;
						end;
						mission.reward = RN.missionStart + mission.serviceCost + mission.vehicleUseCost + fieldArea * mission.rewardPerHa;
						RN.missions[k]["rewardPerHa"] = mission.rewardPerHa;
						RN.missions[k]["vehicleUseCost"] = mission.vehicleUseCost;
						RN.missions[k]["serviceCost"] = mission.serviceCost;
						RN.missions[k]["expectedLiters"] = mission.expectedLiters;
						RN.missions[k]["cropPrice"] = mission.cropPrice;
						RN.missions[k]["reward"] = mission.reward;
						RN.missions[k]["sellPoint"] = mission.sellPoint.stationName;
					end;
					if name == "weed" then
						mission.rewardPerHa = RN.missionPayPerHour[6]/RN.missionHaPerHour[6];
						mission.vehicleUseCost = fieldArea * RN.vehicleUseCostPerHour[6]/RN.missionHaPerHour[6];
						mission.serviceCost = fieldArea * RN.farmServicePerHa[6];
						mission.reward = RN.missionStart + mission.serviceCost + mission.vehicleUseCost + fieldArea * mission.rewardPerHa;
						RN.missions[k]["rewardPerHa"] = mission.rewardPerHa;
						RN.missions[k]["vehicleUseCost"] = mission.vehicleUseCost;
						RN.missions[k]["serviceCost"] = mission.serviceCost;
						RN.missions[k]["reward"] = mission.reward;
					end;
					if name == "spray" then		-- Herbicide
						mission.rewardPerHa = RN.missionPayPerHour[7]/RN.missionHaPerHour[7];
						mission.vehicleUseCost = fieldArea * RN.vehicleUseCostPerHour[7]/RN.missionHaPerHour[7];
						mission.serviceCost = fieldArea * RN.farmServicePerHa[7];
						mission.reimbursementPerHa  = RN.sprayCostHERBICIDE;
						mission.sprayCost = fieldArea * RN.sprayCostHERBICIDE; 					-- add reimbursement to reward
						mission.reward = RN.missionStart + mission.serviceCost + mission.vehicleUseCost + fieldArea * mission.rewardPerHa + mission.sprayCost;
						mission.fieldSpraySet  = true;
						SprayMission.REIMBURSEMENT_PER_HA = RN.sprayCostHERBICIDE;
						mission.sprayVolume = RN.sprayLitersPerHa["HERBICIDE"];
						mission.sprayPrice = RN2.pricePerLiter["HERBICIDE"];
						mission.sprayPricePerHa = RN.sprayLitersPerHa.HERBICIDE * RN2.pricePerLiter["HERBICIDE"];
						RN.missions[k]["rewardPerHa"] = mission.rewardPerHa;
						RN.missions[k]["vehicleUseCost"] = mission.vehicleUseCost;
						RN.missions[k]["serviceCost"] = mission.serviceCost;
						RN.missions[k]["reward"] = mission.reward;
						RN.missions[k]["reimbursementPerHa"] = mission.reimbursementPerHa;
						RN.missions[k]["sprayCost"] = mission.sprayCost;
						RN.missions[k]["sprayVolume"] = mission.sprayVolume;
						RN.missions[k]["sprayPrice"] = mission.sprayPrice;
						RN.missions[k]["sprayPricePerHa"] = mission.sprayPricePerHa;
						
					end;
					if name == "fertilize" then
						local sprayCostPerHa = 0;
						local sprayVolume = 0;
						mission.rewardPerHa = RN.missionPayPerHour[8]/RN.missionHaPerHour[8];
						mission.vehicleUseCost = fieldArea * RN.vehicleUseCostPerHour[8]/RN.missionHaPerHour[8];
						mission.serviceCost = fieldArea * RN.farmServicePerHa[8];
						if fruitName == nil then
							sprayCostPerHa = RN.sprayCostPerHa["WHEAT"]["FERTILIZER"];
							sprayVolume = 1000 * RN.mean1000LSprayRate["WHEAT"]["FERTILIZER"]
						else
							sprayCostPerHa = RN.sprayCostPerHa[fruitName]["FERTILIZER"];
							sprayVolume = 1000 * RN.mean1000LSprayRate[fruitName]["FERTILIZER"]
						end;
						mission.reimbursementPerHa  = sprayCostPerHa;
						mission.fertilizerCost = fieldArea * sprayCostPerHa; 		-- add reimbursement to reward
						mission.reward = RN.missionStart + mission.serviceCost + mission.vehicleUseCost + fieldArea * mission.rewardPerHa + mission.fertilizerCost;
						mission.fieldSpraySet  = true;
						FertilizeMission.REIMBURSEMENT_PER_HA = sprayCostPerHa;
						mission.fertilizerVolume = sprayVolume;
						mission.fertilizerPrice = RN2.pricePerLiter["FERTILIZER"];
						mission.fertilizerPricePerHa = sprayVolume * RN2.pricePerLiter["FERTILIZER"];
						RN.missions[k]["rewardPerHa"] = mission.rewardPerHa;
						RN.missions[k]["vehicleUseCost"] = mission.vehicleUseCost;
						RN.missions[k]["serviceCost"] = mission.serviceCost;
						RN.missions[k]["reward"] = mission.reward;
						RN.missions[k]["reimbursementPerHa"] = mission.reimbursementPerHa;
						RN.missions[k]["fertilizerCost"] = mission.fertilizerCost;
						RN.missions[k]["fertilizerVolume"] = mission.fertilizerVolume;
						RN.missions[k]["fertilizerPrice"] = mission.fertilizerPrice;
						RN.missions[k]["fertilizerPricePerHa"] = mission.fertilizerPricePerHa;
					end;					
				end;
			end;
		end;
	end;
end;
function RealNumbersContracts:draw()
end;
function RealNumbersContracts:deleteMap()
end;
function RealNumbersContracts:mouseEvent(posX, posY, isDown, isUp, button)
end;

addModEventListener(RealNumbersContracts);
