Skip to content

Commit

Permalink
Changed CpObject to allow giants class overrides
Browse files Browse the repository at this point in the history
  • Loading branch information
schwiti6190 committed Dec 22, 2024
1 parent 3afefd9 commit 90e2d97
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 127 deletions.
1 change: 1 addition & 0 deletions Courseplay.lua
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ end
--- This function is called on loading a savegame.
---@param filename string
function Courseplay:loadMap(filename)
CpAIJob.registerJob(g_currentMission.aiJobTypeManager)
self.globalSettings = CpGlobalSettings()
self:registerXmlSchema()
--- Savegame infos here
Expand Down
34 changes: 22 additions & 12 deletions scripts/CpObject.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
]]

-- Class implementation stolen from http://lua-users.org/wiki/SimpleLuaClasses

---@class CpObject
function CpObject(base, init)
---@param base table|function|nil Optional base class or giants constructor function
---@param baseClassInit function|nil Optional giants constructor function for the base class
---@return table
function CpObject(base, baseClassInit)
local c = {} -- a new class instance
if not init and type(base) == 'function' then
init = base
base = nil
elseif type(base) == 'table' then
if type(base) == 'table' then
-- our new class is a shallow copy of the base class!
for i,v in pairs(base) do
for i, v in pairs(base) do
c[i] = v
end
c._base = base
Expand All @@ -38,8 +36,15 @@ function CpObject(base, init)
-- expose a constructor which can be called by <classname>(<args>)
local mt = {}
mt.__call = function(class_tbl, ...)
local obj = {}
setmetatable(obj, c)
local obj
if base and base.baseClassInit then
--- A custom init function from a giants class
obj = base.baseClassInit(..., c)
end
if obj == nil then
obj = {}
setmetatable(obj, c)
end
if class_tbl.init then
class_tbl.init(obj,...)
else
Expand All @@ -50,7 +55,7 @@ function CpObject(base, init)
end
return obj
end
c.init = init
c.baseClassInit = baseClassInit
c.is_a = function(self, klass)
local m = getmetatable(self)
while m do
Expand Down Expand Up @@ -83,7 +88,12 @@ function CpObject(base, init)
end
return str
end

c.new = function(...)
return c(...)
end
c.superClass = function ()
return c._base
end
setmetatable(c, mt)
return c
end
Expand Down
44 changes: 15 additions & 29 deletions scripts/ai/jobs/CpAIJob.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,13 @@
---@field groupedParameters table
---@field isServer boolean
---@field helperIndex number
CpAIJob = {
name = "",
jobName = "",
}
local AIJobCp_mt = Class(CpAIJob, AIJob)

function CpAIJob.new(isServer, customMt)
local self = AIJob.new(isServer, customMt or AIJobCp_mt)
CpAIJob = CpObject(AIJob, AIJob.new)
function CpAIJob:init(isServer)
self.isDirectStart = false
self.debugChannel = CpDebug.DBG_FIELDWORK

--- Small translation fix, needs to be removed once giants fixes it.
local ai = g_currentMission.aiJobTypeManager
ai:getJobTypeByIndex(ai:getJobTypeIndexByName(self.name)).title = g_i18n:getText(self.jobName)

self:setupJobParameters()
self:setupTasks(isServer)

return self
end
end

---@param task CpAITask
function CpAIJob:removeTask(task)
Expand Down Expand Up @@ -123,7 +110,7 @@ end

function CpAIJob:start(farmId)
self:onPreStart()
CpAIJob:superClass().start(self, farmId)
AIJob.start(self, farmId)

if self.isServer then
local vehicle = self.vehicleParameter:getVehicle()
Expand All @@ -135,7 +122,7 @@ end

function CpAIJob:stop(aiMessage)
if not self.isServer then
CpAIJob:superClass().stop(self, aiMessage)
AIJob.stop(self, aiMessage)
return
end
local vehicle = self.vehicleParameter:getVehicle()
Expand All @@ -148,7 +135,7 @@ function CpAIJob:stop(aiMessage)
if driveStrategy then
driveStrategy:onFinished()
end
CpAIJob:superClass().stop(self, aiMessage)
AIJob.stop(self, aiMessage)
return
end
local releaseMessage, hasFinished, event, isOnlyShownOnPlayerStart =
Expand All @@ -161,7 +148,7 @@ function CpAIJob:stop(aiMessage)
--- TODO: Add check if passing to ad is active maybe?
vehicle:setCpInfoTextActive(releaseMessage)
end
CpAIJob:superClass().stop(self, aiMessage)
AIJob.stop(self, aiMessage)
if event then
SpecializationUtil.raiseEvent(vehicle, event)
end
Expand All @@ -173,7 +160,7 @@ end

--- Updates the parameter values.
function CpAIJob:applyCurrentState(vehicle, mission, farmId, isDirectStart)
CpAIJob:superClass().applyCurrentState(self, vehicle, mission, farmId, isDirectStart)
AIJob.applyCurrentState(self, vehicle, mission, farmId, isDirectStart)
self.vehicleParameter:setVehicle(vehicle)
if not self.cpJobParameters or not self.cpJobParameters.startPosition then
return
Expand Down Expand Up @@ -339,15 +326,15 @@ function CpAIJob:readStream(streamId, connection)
end

function CpAIJob:saveToXMLFile(xmlFile, key, usedModNames)
CpAIJob:superClass().saveToXMLFile(self, xmlFile, key, usedModNames)
AIJob.saveToXMLFile(self, xmlFile, key, usedModNames)
if self.cpJobParameters then
self.cpJobParameters:saveToXMLFile(xmlFile, key)
end
return true
end

function CpAIJob:loadFromXMLFile(xmlFile, key)
CpAIJob:superClass().loadFromXMLFile(self, xmlFile, key)
AIJob.loadFromXMLFile(self, xmlFile, key)
if self.cpJobParameters then
self.cpJobParameters:validateSettings()
self.cpJobParameters:loadFromXMLFile(xmlFile, key)
Expand Down Expand Up @@ -378,7 +365,7 @@ end
--- Applies the global wage modifier.
function CpAIJob:getPricePerMs()
local modifier = g_Courseplay.globalSettings:getSettings().wageModifier:getValue()/100
return CpAIJob:superClass().getPricePerMs(self) * modifier
return AIJob.getPricePerMs(self) * modifier
end

--- Fix for precision farming ...
Expand Down Expand Up @@ -439,13 +426,13 @@ end

function CpAIJob:showNotification(aiMessage)
if g_Courseplay.globalSettings.infoTextHudActive:getValue() == g_Courseplay.globalSettings.DISABLED then
CpAIJob:superClass().showNotification(self, aiMessage)
AIJob.showNotification(self, aiMessage)
return
end
local releaseMessage, hasFinished, event = g_infoTextManager:getInfoTextDataByAIMessage(aiMessage)
if not releaseMessage and not aiMessage:isa(AIMessageSuccessStoppedByUser) then
self:debug("No release message found, so we use the giants notification!")
CpAIJob:superClass().showNotification(self, aiMessage)
AIJob.showNotification(self, aiMessage)
return
end
local vehicle = self:getVehicle()
Expand Down Expand Up @@ -482,9 +469,9 @@ end
AIJobTypeManager.getJobTypeIndex = Utils.overwrittenFunction(AIJobTypeManager.getJobTypeIndex ,CpAIJob.getJobTypeIndex)

--- Registers additional jobs.
function CpAIJob.registerJob(AIJobTypeManager)
function CpAIJob.registerJob(aiJobTypeManager)
local function register(class)
AIJobTypeManager:registerJobType(class.name, class.jobName, class)
aiJobTypeManager:registerJobType(class.name, g_i18n:getText(class.jobName), class)
end
register(CpAIJobBaleFinder)
register(CpAIJobFieldWork)
Expand All @@ -493,5 +480,4 @@ function CpAIJob.registerJob(AIJobTypeManager)
register(CpAIJobBunkerSilo)
end

AIJobTypeManager.loadMapData = Utils.appendedFunction(AIJobTypeManager.loadMapData,CpAIJob.registerJob)

24 changes: 9 additions & 15 deletions scripts/ai/jobs/CpAIJobBaleFinder.lua
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
--- Bale finder job.
---@class CpAIJobBaleFinder : CpAIJobFieldWork
---@field selectedFieldPlot FieldPlot
CpAIJobBaleFinder = {
name = "BALE_FINDER_CP",
jobName = "CP_job_baleCollect",
minStartDistanceToField = 20,
}
local AIJobBaleFinderCp_mt = Class(CpAIJobBaleFinder, CpAIJob)


function CpAIJobBaleFinder.new(isServer, customMt)
local self = CpAIJob.new(isServer, customMt or AIJobBaleFinderCp_mt)
CpAIJobBaleFinder = CpObject(CpAIJob)
CpAIJobBaleFinder.name = "BALE_FINDER_CP"
CpAIJobBaleFinder.jobName = "CP_job_baleCollect"
CpAIJobBaleFinder.minStartDistanceToField = 20
function CpAIJobBaleFinder:init(isServer)
CpAIJob.init(self, isServer)
self.selectedFieldPlot = FieldPlot(true)
self.selectedFieldPlot:setVisible(false)
self.selectedFieldPlot:setBrightColor(true)

return self
self.selectedFieldPlot:setBrightColor()
end

function CpAIJobBaleFinder:setupTasks(isServer)
Expand All @@ -39,7 +33,7 @@ end


function CpAIJobBaleFinder:applyCurrentState(vehicle, mission, farmId, isDirectStart, isStartPositionInvalid)
CpAIJobBaleFinder:superClass().applyCurrentState(self, vehicle, mission, farmId, isDirectStart, isStartPositionInvalid)
CpAIJob.applyCurrentState(self, vehicle, mission, farmId, isDirectStart)
self.cpJobParameters:validateSettings()

self:copyFrom(vehicle:getCpBaleFinderJob())
Expand Down Expand Up @@ -116,7 +110,7 @@ end

--- Gets the additional task description shown.
function CpAIJobBaleFinder:getDescription()
local desc = CpAIJob:superClass().getDescription(self)
local desc = CpAIJob.getDescription(self)
local currentTask = self:getTaskByIndex(self.currentTaskIndex)
if currentTask == self.driveToTask then
desc = desc .. " - " .. g_i18n:getText("ai_taskDescriptionDriveToField")
Expand Down
20 changes: 7 additions & 13 deletions scripts/ai/jobs/CpAIJobBunkerSilo.lua
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
--- AI job for the silo driver.
---@class CpAIJobBunkerSilo : CpAIJob
CpAIJobBunkerSilo = {
name = "BUNKER_SILO_CP",
jobName = "CP_job_bunkerSilo",
}
local CpAIJobBunkerSilo_mt = Class(CpAIJobBunkerSilo, CpAIJob)

function CpAIJobBunkerSilo.new(isServer, customMt)
local self = CpAIJob.new(isServer, customMt or CpAIJobBunkerSilo_mt)

CpAIJobBunkerSilo = CpObject(CpAIJob)
CpAIJobBunkerSilo.name = "BUNKER_SILO_CP"
CpAIJobBunkerSilo.jobName = "CP_job_bunkerSilo"
function CpAIJobBunkerSilo:init(isServer)
CpAIJob.init(self, isServer)
self.hasValidPosition = nil
self.bunkerSilo = nil

return self
end

function CpAIJobBunkerSilo:setupTasks(isServer)
Expand Down Expand Up @@ -115,7 +109,7 @@ function CpAIJobBunkerSilo:draw(map, isOverviewMap)
end

function CpAIJobBunkerSilo:readStream(streamId, connection)
CpAIJobBunkerSilo:superClass().readStream(self, streamId, connection)
CpAIJob.readStream(self, streamId, connection)

local x, z = self.cpJobParameters.siloPosition:getPosition()
self.hasValidPosition, self.bunkerSilo = g_bunkerSiloManager:getBunkerSiloAtPosition(x, z)
Expand All @@ -124,7 +118,7 @@ end

--- Gets the additional task description shown.
function CpAIJobBunkerSilo:getDescription()
local desc = CpAIJob:superClass().getDescription(self)
local desc = CpAIJob.getDescription(self)
local currentTask = self:getTaskByIndex(self.currentTaskIndex)
if currentTask == self.driveToTask then
desc = desc .. " - " .. g_i18n:getText("ai_taskDescriptionDriveToField")
Expand Down
36 changes: 13 additions & 23 deletions scripts/ai/jobs/CpAIJobCombineUnloader.lua
Original file line number Diff line number Diff line change
@@ -1,36 +1,26 @@
--- Combine unloader job.
---@class CpAIJobCombineUnloader : CpAIJob
CpAIJobCombineUnloader = {
name = "COMBINE_UNLOADER_CP",
jobName = "CP_job_combineUnload",
minStartDistanceToField = 20,
minFieldUnloadDistanceToField = 20,
maxHeapLength = 150
}

local AIJobCombineUnloaderCp_mt = Class(CpAIJobCombineUnloader, CpAIJob)

function CpAIJobCombineUnloader.new(isServer, customMt)
local self = CpAIJob.new(isServer, customMt or AIJobCombineUnloaderCp_mt)

CpAIJobCombineUnloader = CpObject(CpAIJob)
CpAIJobCombineUnloader.name = "COMBINE_UNLOADER_CP"
CpAIJobCombineUnloader.jobName = "CP_job_combineUnload"
CpAIJobCombineUnloader.minStartDistanceToField = 20
CpAIJobCombineUnloader.minFieldUnloadDistanceToField = 20
CpAIJobCombineUnloader.maxHeapLength = 150
function CpAIJobCombineUnloader:init(isServer)
CpAIJob.init(self, isServer)
self.lastPositionX, self.lastPositionZ = math.huge, math.huge

self.selectedFieldPlot = FieldPlot(true)
self.selectedFieldPlot:setVisible(false)
self.selectedFieldPlot:setBrightColor(true)

self.heapPlot = HeapPlot()
self.heapPlot:setVisible(false)
self.heapNode = CpUtil.createNode("siloNode", 0, 0, 0, nil)

--- Giants unload
self.dischargeNodeInfos = {}

return self
end

function CpAIJobCombineUnloader:delete()
CpAICombineUnloader:superClass().delete(self)
CpAIJob.delete(self)
CpUtil.destroyNode(self.heapNode)
end

Expand Down Expand Up @@ -301,7 +291,7 @@ function CpAIJobCombineUnloader:getNextTaskIndex(isSkipTask)
end

function CpAIJobCombineUnloader:canContinueWork()
local canContinueWork, errorMessage = CpAIJobCombineUnloader:superClass().canContinueWork(self)
local canContinueWork, errorMessage = CpAIJob.canContinueWork(self)
if not canContinueWork then
return canContinueWork, errorMessage
end
Expand Down Expand Up @@ -340,15 +330,15 @@ function CpAIJobCombineUnloader:startTask(task)
dischargeNodeInfo.dirty = true
end
end
CpAIJobCombineUnloader:superClass().startTask(self, task)
CpAIJob.startTask(self, task)
end

--- Starting index for giants unload:
--- - Close or on the field, we make sure cp pathfinder is always involved.
--- - Else if the trailer is full and we are far away from the field, then let giants drive to unload directly.
---@return number
function CpAIJobCombineUnloader:getStartTaskIndex()
local startTask = CpAIJobCombineUnloader:superClass().getStartTaskIndex(self)
local startTask = CpAIJob.getStartTaskIndex(self)
if not self.cpJobParameters.useGiantsUnload:getValue() then
return startTask
end
Expand Down Expand Up @@ -387,7 +377,7 @@ end
--- TODO: Add the missing description once the task system is better implemented.
---@return unknown
function CpAIJobCombineUnloader:getDescription()
local desc = CpAIJob:superClass().getDescription(self)
local desc = CpAIJob.getDescription(self)
local currentTask = self:getTaskByIndex(self.currentTaskIndex)
if currentTask == self.driveToTask then
desc = desc .. " - " .. g_i18n:getText("ai_taskDescriptionDriveToField")
Expand Down
Loading

0 comments on commit 90e2d97

Please sign in to comment.