Skip to content

Commit

Permalink
fine tuning 1.2.6.5
Browse files Browse the repository at this point in the history
- Improve reward multiplier getReward()
- handle zombie (pallet, bigbag) vehicles when dismissing contracts
  • Loading branch information
Mmtrx committed Jan 19, 2023
1 parent 8bcac1d commit 6b6b4f1
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 13 deletions.
29 changes: 18 additions & 11 deletions betterContracts.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@
-- v1.2.6.2 16.12.2022 don't act onFarmlandStateChanged() before mission started. Smaller menu icon
-- v1.2.6.3 02.01.2023 onClickBuyFarmland, missionVehicles (userdefined) fixed
-- v1.2.6.4 17.01.2023 fix issue #88: onClickBuyFarmland() if discountMode off
-- v1.2.6.5 18.01.2023 add setting "toDeliver": harvest contract success factor
-- v1.2.6.5 18.01.2023 add setting "toDeliver": harvest contract success factor.
-- Improve reward multiplier getReward()
-- handle zombie (pallet, bigbag) vehicles when dismissing contracts
--=======================================================================================================
SC = {
FERTILIZER = 1, -- prices index
Expand Down Expand Up @@ -458,7 +460,7 @@ function BetterContracts:initialize()
Utility.appendedFunction(Farm,"readStream",farmRead)

-- to adjust contracts reward / vehicle lease values:
Utility.overwrittenFunction(AbstractFieldMission,"calculateReward",calcReward)
Utility.overwrittenFunction(AbstractFieldMission,"getReward",getReward)
Utility.overwrittenFunction(AbstractFieldMission,"calculateVehicleUseCost",calcLeaseCost)

-- adjust NPC activity for missions:
Expand All @@ -482,6 +484,9 @@ function BetterContracts:initialize()

-- to load own mission vehicles:
Utility.overwrittenFunction(MissionManager, "loadMissionVehicles", BetterContracts.loadMissionVehicles)
Utility.overwrittenFunction(AbstractFieldMission, "loadNextVehicleCallback", loadNextVehicle)
Utility.prependedFunction(AbstractFieldMission, "removeAccess", removeAccess)

-- flexible mission limit:
Utility.overwrittenFunction(MissionManager, "hasFarmReachedMissionLimit", hasFarmReachedMissionLimit)
-- fix AbstractMission:
Expand Down Expand Up @@ -539,8 +544,9 @@ function BetterContracts:onPostLoadMap(mapNode, mapFile)
addConsoleCommand("gsMissionHarvestField", "Harvest a field and print the liters", "consoleHarvestField", g_missionManager)
addConsoleCommand("gsMissionTestHarvests", "Run an expansive tests for harvest missions", "consoleHarvestTests", g_missionManager)
end
-- init HarvestMission.SUCCESS_FACTOR
-- init Harvest SUCCESS_FACTORs (std is harv = .93, bale = .9)
HarvestMission.SUCCESS_FACTOR = self.config.toDeliver
BaleMission.FILL_SUCCESS_FACTOR = self.config.toDeliver - 0.03

-- adjust max missions
local fieldsAmount = table.size(g_fieldManager.fields)
Expand Down Expand Up @@ -677,6 +683,12 @@ function BetterContracts:getFilltypePrice(m)
end
return m.sellPoint:getEffectiveFillTypePrice(m.fillType)
end
function BetterContracts:calcProfit(m, successFactor)
-- calculate brutto income as reward + value of kept harvest
local keep = math.floor(m.expectedLiters *(1 - successFactor))
local price = self:getFilltypePrice(m)
return keep, price, m:getReward() + keep * price
end
function BetterContracts:addMission(m)
-- add mission m to the corresponding BetterContracts list
local cont = {}
Expand Down Expand Up @@ -720,9 +732,7 @@ function BetterContracts:addMission(m)
end
end
if cat == SC.HARVEST then
local keep = math.floor(m.expectedLiters *(1 - HarvestMission.SUCCESS_FACTOR))
local price = self:getFilltypePrice(m)
local profit = rew + keep * price
local keep, price, profit = self:calcProfit(m, HarvestMission.SUCCESS_FACTOR)
cont = {
miss = m,
width = wid,
Expand Down Expand Up @@ -752,17 +762,14 @@ function BetterContracts:addMission(m)
}
table.insert(self.simple, cont)
elseif cat == SC.BALING then
local deliver = math.ceil(m.expectedLiters * BaleMission.FILL_SUCCESS_FACTOR)
local keep = math.floor(m.expectedLiters - deliver) --can be sold on your own
local price = self:getFilltypePrice(m)
local profit = rew + keep * price
local keep, price, profit = self:calcProfit(m, BaleMission.FILL_SUCCESS_FACTOR)
cont = {
miss = m,
width = wid,
height = hei,
worktime = dura * 3, -- dura is just the mow time, adjust for windrowing/ baling
ftype = self.ft[m.fillType].title,
deliver = deliver,
deliver = math.ceil(m.expectedLiters - keep),
keep = keep, --can be sold on your own
price = price * 1000,
profit = profit,
Expand Down
11 changes: 10 additions & 1 deletion scripts/gui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,8 @@ function populateCell(frCon, list, sect, index, cell)
profit:setVisible(false)
return
end
local id = frCon.sectionContracts[sect].contracts[index].mission.id
local m = frCon.sectionContracts[sect].contracts[index].mission
local id = m.id
local cont
if self.IdToCont[id] == nil or self.IdToCont[id][2] == nil then
debugPrint("populateCell(): empty IdToCont for id %s. sect/index: %s/%s",
Expand All @@ -386,6 +387,14 @@ function populateCell(frCon, list, sect, index, cell)
local showProf = false
if cat==SC.HARVEST or cat==SC.SPREAD or cat==SC.BALING then
-- only for harvest, spread, mow contracts
-- update total profit
if cat == SC.HARVEST then
_,_, prof = self:calcProfit(m, HarvestMission.SUCCESS_FACTOR)
elseif cat == SC.BALING then
_,_, prof = self:calcProfit(m, BaleMission.FILL_SUCCESS_FACTOR)
end
--todo: update profit spread mission

local reward = cell:getAttribute("reward")
local rewtext = reward:getText()
reward:setText(g_i18n:formatMoney(prof, 0, true, true))
Expand Down
30 changes: 30 additions & 0 deletions scripts/missionVehicles.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
-- v1.2.0.0 30.11.2021 (Mmtrx) adapt for FS22
-- v.1.2.4.2 19.09.2022 [ModHub] recognize FS22_DynamicMissionVehicles
-- v.1.2.6.2 19.12.2022 don't add dlc vehicles to a userdefined "overwrite" setup
-- v1.2.6.5 19.01.2023 handle zombie (pallet, bigbag) vehicles when dismissing contracts
--=======================================================================================================

---------------------- mission vehicle enhancement functions --------------------------------------------
Expand Down Expand Up @@ -261,3 +262,32 @@ function BetterContracts:loadExtraMissionVehicles_configurations(xmlFile, vehicl
end
return configurations
end

function loadNextVehicle(self, super, vehicle, vehicleLoadState, arguments)
-- overwritten AbstractFieldMission:loadNextVehicleCallback() to allow spawning pallets
if vehicle == nil then return end

self.lastVehicleIndexToLoad = arguments[1]
self.vehicleLoadWaitFrameCounter = 2
vehicle.activeMissionId = self.activeMissionId
if vehicle.addWearAmount ~= nil then
vehicle:addWearAmount(math.random() * 0.3 + 0.1)
end
vehicle:setOperatingTime(3600000 * (math.random() * 40 + 30))
table.insert(self.vehicles, vehicle)
end
function removeAccess(self)
-- prepend to AbstractFieldMission:removeAccess()
-- remove "zombie" pallets/ bigbags from list of leased vehicles
if not self.isServer then return end

local toDelete = {}
for _, vehicle in ipairs(self.vehicles) do
if vehicle.isDeleted then
table.insert(toDelete, vehicle)
end
end
for _, vehicle in ipairs(toDelete) do
table.removeElement(self.vehicles, vehicle)
end
end
4 changes: 3 additions & 1 deletion scripts/options.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
-- v1.2.5.0 31.10.2022 hard mode: active miss time out at midnght. Penalty for missn cancel
-- v1.2.6.0 30.11.2022 UI for all settings
-- v1.2.6.4 17.01.2023 fix issue #88: onClickBuyFarmland() if discountMode off
-- v1.2.6.5 18.01.2023 add setting "toDeliver": harvest contract success factor.
-- Improve reward multiplier getReward()
--=======================================================================================================

--------------------- lazyNPC ---------------------------------------------------------------------------
Expand Down Expand Up @@ -75,7 +77,7 @@ function NPCHarvest(self, superf, field, allowUpdates)
end

--------------------- reward / lease cost ---------------------------------------------------------------
function calcReward(self,superf)
function getReward(self,superf)
return BetterContracts.config.multReward * superf(self)
end
function calcLeaseCost(self,superf)
Expand Down
5 changes: 5 additions & 0 deletions scripts/settings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ BCSettingsBySubtitle = {
default = 3,
title = "bc_rewardMultiplier",
tooltip = "bc_rewardMultiplier_tooltip",
actionFunc = function(self,ix)
BetterContracts:refresh() -- to recalc contract rewards
end,
noTranslate = true
},
{name = "multLease",
Expand Down Expand Up @@ -53,6 +56,8 @@ BCSettingsBySubtitle = {
tooltip = "bc_toDeliver_tooltip",
actionFunc = function(self,ix)
HarvestMission.SUCCESS_FACTOR = self.values[ix]
BaleMission.FILL_SUCCESS_FACTOR = self.values[ix] - 0.03
BetterContracts:refresh() -- to recalc deliver/keep for harvest contr
end,
noTranslate = true
},
Expand Down

0 comments on commit 6b6b4f1

Please sign in to comment.