Skip to content

Commit

Permalink
fix "hardlimit"
Browse files Browse the repository at this point in the history
- reset of max jobs/farm at month end was not done on clients
  • Loading branch information
Mmtrx committed Jan 4, 2024
1 parent f0bd342 commit 49a2b0b
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 27 deletions.
1 change: 1 addition & 0 deletions betterContracts.lua
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
-- v1.2.8.6 12.10.2023 add bcPrintVehicles console command. Fix farmlandManagerSaveToXMLFile() (#169)
-- v1.2.8.7 02.12.2023 npc should not work before noon of first day in month (#187)
-- new setting "hardLimit": limit jobs per farm and month (#168)
-- new setting "multRewardMow" for baling contracts (#199)
--=======================================================================================================
SC = {
FERTILIZER = 1, -- prices index
Expand Down
67 changes: 40 additions & 27 deletions scripts/options.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
-- v1.2.8.3 10.10.2023 force plow after root crop harvest. Insta-ferment separate setting (#158)
-- v1.2.8.7 02.12.2023 npc should not work before noon of first day in month (#187)
-- new setting "hardLimit": limit jobs per farm and month (#168)
-- new setting "multRewardMow" for baling contracts (#199)
--=======================================================================================================

--------------------- lazyNPC ---------------------------------------------------------------------------
Expand Down Expand Up @@ -111,8 +112,8 @@ function farmWrite(self, streamId)
if self.isSpectator then return end

-- write jobsLeft for current month
self.jobsLeft = self.jobsLeft or BetterContracts.config.hardLimit
streamWriteUInt8(streamId, self.jobsLeft) -- # of jobs left to accept this month
self.stats.jobsLeft = self.stats.jobsLeft or BetterContracts.config.hardLimit
streamWriteUInt8(streamId, self.stats.jobsLeft) -- # of jobs left to accept this month

-- write stats.npcJobs when MP syncing a farm
local count = 0
Expand All @@ -135,7 +136,7 @@ function farmRead(self, streamId)
if self.isSpectator then return end

-- read jobsLeft for current month
self.jobsLeft = streamReadUInt8(streamId)
self.stats.jobsLeft = streamReadUInt8(streamId)

-- read npcJobs[npcIndex] for a farm
if self.stats.npcJobs == nil then
Expand Down Expand Up @@ -200,7 +201,10 @@ function saveToXML(self, xmlFile, key)
xmlFile:setInt(npcKey .. "#count", npc or 0)
end)
end
xmlFile:setInt(key..".jobsLeft", self.jobsLeft or BetterContracts.config.hardLimit)
local bc = BetterContracts
if bc.config.hardMode and bc.config.hardLimit > -1 then
xmlFile:setInt(key..".jobsLeft", self.jobsLeft, BetterContracts.config.hardLimit)
end
end
function loadFromXML(self, xmlFile, key)
-- appended to FarmStats:loadFromXMLFile()
Expand Down Expand Up @@ -352,39 +356,48 @@ function startContract(frCon, superf, wantsLease)
return
end
end
if self.config.hardLimit > -1 then
-- check available monthly jobs limit
farm.jobsLeft = farm.jobsLeft or BetterContracts.config.hardLimit
if farm.jobsLeft == 0 then
g_gui:showInfoDialog({
visible = true,
text = g_i18n:getText("bc_monthlyLimit"),
dialogType = DialogElement.TYPE_INFO
})
return
else
farm.jobsLeft = farm.jobsLeft -1
if farm.stats.jobsLeft == -1 then -- hardLimit was set during this game
farm.stats.jobsLeft = self.config.hardLimit
end
if farm.stats.jobsLeft == 0 then
g_gui:showInfoDialog({
visible = true,
text = g_i18n:getText("bc_monthlyLimit"),
dialogType = DialogElement.TYPE_INFO
})
return
else
farm.stats.jobsLeft = farm.stats.jobsLeft -1
end
end
end
superf(frCon, wantsLease)
end
function BetterContracts:resetJobsLeft()
-- recalc jobs left per farm
for _,farm in pairs(g_farmManager:getFarms()) do
if farm.farmId ~= FarmManager.SPECTATOR_FARM_ID then
local mlist = table.ifilter(g_missionManager:getMissionsList(farm.farmId), function(m)
return m.status ~= AbstractMission.STATUS_STOPPED
end)
local count = table.size(mlist)
farm.stats.jobsLeft = math.max(self.config.hardLimit - count, 0)
debugPrint("*BC: farm %s has %d jobs. Limit set to %d", farm.name, count, farm.stats.jobsLeft)
end
end
end
function BetterContracts:onPeriodChanged()
if g_server == nil then return end
self.NPCAllowWork = false -- prevent any NPC field work at start of month

-- reset monthly limit for all farms:
if self.config.hardLimit > -1 then
for _,farm in pairs(g_farmManager:getFarms()) do
if farm.farmId ~= FarmManager.SPECTATOR_FARM_ID then
local mlist = table.ifilter(g_missionManager:getMissionsList(farm.farmId), function(m)
return m.status ~= AbstractMission.STATUS_STOPPED
end)
local count = table.size(mlist)
farm.jobsLeft = math.max(self.config.hardLimit - count, 0)
debugPrint("*BC: farm %s has %d jobs. Limit set to %d", farm.name, count, farm.jobsLeft)
end
end
self:resetJobsLeft()
end

if g_server == nil then return end

self.NPCAllowWork = false -- prevent any NPC field work at start of month

-- hard mode: cancel any active field missions
if self.config.hardMode and self.config.hardExpire == SC.MONTH then
for _, m in ipairs(g_missionManager:getActiveMissions()) do
Expand Down
11 changes: 11 additions & 0 deletions scripts/settings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
-- v1.2.7.9 03.05.2023 more values discPerJob, discMaxJobs
-- v1.2.8.3 10.10.2023 force plow after root crop harvest. Insta-ferment separate setting (#158)
-- v1.2.8.7 03.12.2023 new setting "hardLimit": limit jobs per farm and month (#168)
-- new setting "multRewardMow" for baling contracts (#199)
--=======================================================================================================
local function lazyNPCDisabled()
return not BetterContracts.config.lazyNPC
Expand Down Expand Up @@ -210,6 +211,16 @@ BCSettingsBySubtitle = {
title = "bc_hardLimit",
tooltip = "bc_hardLimit_tooltip",
isDisabledFunc = hardDisabled,
actionFunc = function(self,ix)
-- set stats.jobsLeft for all farms
if ix == 1 then -- reset to no limit
for _, farm in pairs(g_farmManager:getFarms()) do
farm.stats.jobsLeft = -1
end
else
BetterContracts:resetJobsLeft()
end
end,
noTranslate = true
},
},
Expand Down

0 comments on commit 49a2b0b

Please sign in to comment.