Skip to content

Commit

Permalink
Merge pull request #31 from Courseplay/house-keeping
Browse files Browse the repository at this point in the history
House keeping
  • Loading branch information
Tensuko authored Dec 25, 2024
2 parents 2afe860 + f60358f commit ba724b1
Show file tree
Hide file tree
Showing 21 changed files with 458 additions and 1,381 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
11 changes: 5 additions & 6 deletions modDesc.xml
Original file line number Diff line number Diff line change
Expand Up @@ -265,15 +265,14 @@ Changelog 8.0.0.0:
<sourceFile filename="scripts/ai/tasks/CpAITaskSiloLoader.lua"/>

<sourceFile filename="scripts/gui/CpGuiUtil.lua"/>
<sourceFile filename="scripts/gui/CoursePlot.lua"/>
<sourceFile filename="scripts/gui/FieldPlot.lua"/>
<sourceFile filename="scripts/gui/BunkerSiloPlot.lua"/>
<sourceFile filename="scripts/gui/HeapPlot.lua"/>
<sourceFile filename="scripts/gui/UnloadingTriggerPlot.lua"/>
<sourceFile filename="scripts/gui/plots/CoursePlot.lua"/>
<sourceFile filename="scripts/gui/plots/FieldPlot.lua"/>
<sourceFile filename="scripts/gui/plots/BunkerSiloPlot.lua"/>
<sourceFile filename="scripts/gui/plots/HeapPlot.lua"/>
<sourceFile filename="scripts/gui/plots/UnloadingTriggerPlot.lua"/>
<sourceFile filename="scripts/gui/CpStatus.lua"/>
<sourceFile filename="scripts/gui/CustomFieldHotspot.lua"/>
<sourceFile filename="scripts/gui/CourseDisplay.lua"/>
<!-- <sourceFile filename="scripts/gui/CpGamePadHudScreen.lua"/> -->
<sourceFile filename="scripts/gui/elements/CpBinaryOptionElement.lua"/>
<sourceFile filename="scripts/gui/elements/CpOptionToggleElement.lua"/>
<sourceFile filename="scripts/gui/pages/CpCourseManagerFrame.lua"/>
Expand Down
36 changes: 24 additions & 12 deletions scripts/CpObject.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ 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)
---@generic K : CpObject
---@param base `K`|nil Optional base class
---@param baseClassInit function|nil Optional giants constructor function for the base class
---@return K
function CpObject(base, baseClassInit)
---@class CpObject
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 +38,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 +57,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 +90,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
115 changes: 24 additions & 91 deletions scripts/ai/AIMessages.lua
Original file line number Diff line number Diff line change
@@ -1,127 +1,60 @@
AIMessageErrorIsFull = {
name = "CP_ERROR_FULL"
}
local AIMessageErrorIsFull_mt = Class(AIMessageErrorIsFull, AIMessage)

function AIMessageErrorIsFull.new(customMt)
local self = AIMessage.new(customMt or AIMessageErrorIsFull_mt)

return self
end

---@class AIMessageErrorIsFull
AIMessageErrorIsFull = CpObject(AIMessage, AIMessage.new)
AIMessageErrorIsFull.name = "CP_ERROR_FULL"
function AIMessageErrorIsFull:getI18NText()
return g_i18n:getText("CP_ai_messageErrorIsFull")
end

AIMessageCpError = {
name = "CP_ERROR"
}
local AIMessageCpError_mt = Class(AIMessageCpError, AIMessage)

function AIMessageCpError.new(customMt)
local self = AIMessage.new(customMt or AIMessageCpError_mt)

return self
end

---@class AIMessageCpError
AIMessageCpError = CpObject(AIMessage, AIMessage.new)
AIMessageCpError.name = "CP_ERROR"
function AIMessageCpError:getI18NText()
return g_i18n:getText("CP_ai_messageError")
end

AIMessageCpErrorNoPathFound = {
name = "CP_ERROR_NO_PATH_FOUND"
}
local AIMessageCpErrorNoPathFound_mt = Class(AIMessageCpErrorNoPathFound, AIMessage)

function AIMessageCpErrorNoPathFound.new(customMt)
local self = AIMessage.new(customMt or AIMessageCpErrorNoPathFound_mt)

return self
end

---@class AIMessageCpErrorNoPathFound
AIMessageCpErrorNoPathFound = CpObject(AIMessage, AIMessage.new)
AIMessageCpErrorNoPathFound.name = "CP_ERROR_NO_PATH_FOUND"
function AIMessageCpErrorNoPathFound:getI18NText()
return g_i18n:getText("CP_ai_messageErrorNoPathFound")
end


AIMessageErrorWrongBaleWrapType = {
name = "CP_ERROR_WRONG_WRAP_TYPE"
}
local AIMessageErrorWrongBaleWrapType_mt = Class(AIMessageErrorWrongBaleWrapType, AIMessage)

function AIMessageErrorWrongBaleWrapType.new(customMt)
local self = AIMessage.new(customMt or AIMessageErrorWrongBaleWrapType_mt)

return self
end

---@class AIMessageErrorWrongBaleWrapType
AIMessageErrorWrongBaleWrapType = CpObject(AIMessage, AIMessage.new)
AIMessageErrorWrongBaleWrapType.name = "CP_ERROR_WRONG_WRAP_TYPE"
function AIMessageErrorWrongBaleWrapType:getI18NText()
return g_i18n:getText("CP_ai_messageErrorWrongBaleWrapType")
end

AIMessageErrorGroundUnloadNotSupported = {
name = "CP_ERROR_GROUND_UNLOAD_NOT_SUPPORTED"
}

local AIMessageErrorGroundUnloadNotSupported_mt = Class(AIMessageErrorGroundUnloadNotSupported, AIMessage)

function AIMessageErrorGroundUnloadNotSupported.new(customMt)
local self = AIMessage.new(customMt or AIMessageErrorGroundUnloadNotSupported_mt)

return self
end

---@class AIMessageErrorGroundUnloadNotSupported
AIMessageErrorGroundUnloadNotSupported = CpObject(AIMessage, AIMessage.new)
AIMessageErrorGroundUnloadNotSupported.name = "CP_ERROR_GROUND_UNLOAD_NOT_SUPPORTED"
function AIMessageErrorGroundUnloadNotSupported:getI18NText()
return g_i18n:getText("CP_ai_messageErrorGroundUnloadNotSupported")
end

AIMessageErrorCutterNotSupported = {
name = "CP_ERROR_CUTTER_NOT_SUPPORTED"
}
local AIMessageErrorCutterNotSupported_mt = Class(AIMessageErrorCutterNotSupported, AIMessage)

function AIMessageErrorCutterNotSupported.new(customMt)
local self = AIMessage.new(customMt or AIMessageErrorCutterNotSupported_mt)

return self
end

---@class AIMessageErrorCutterNotSupported
AIMessageErrorCutterNotSupported = CpObject(AIMessage, AIMessage.new)
AIMessageErrorCutterNotSupported.name = "CP_ERROR_CUTTER_NOT_SUPPORTED"
function AIMessageErrorCutterNotSupported:getI18NText()
return g_i18n:getText("CP_ai_messageErrorCutterNotSupported")
end

AIMessageErrorAutomaticCutterAttachNotActive = {
name = "CP_ERROR_AUTOMATIC_CUTTER_ATTACH_NOT_ACTIVE"
}
local AIMessageErrorAutomaticCutterAttachNotActive_mt = Class(AIMessageErrorAutomaticCutterAttachNotActive, AIMessage)

function AIMessageErrorAutomaticCutterAttachNotActive.new(customMt)
local self = AIMessage.new(customMt or AIMessageErrorAutomaticCutterAttachNotActive_mt)

return self
end

---@class AIMessageErrorAutomaticCutterAttachNotActive
AIMessageErrorAutomaticCutterAttachNotActive = CpObject(AIMessage, AIMessage.new)
AIMessageErrorAutomaticCutterAttachNotActive.name = "CP_ERROR_AUTOMATIC_CUTTER_ATTACH_NOT_ACTIVE"
function AIMessageErrorAutomaticCutterAttachNotActive:getI18NText()
return g_i18n:getText("CP_ai_messageErrorAutomaticCutterAttachNotActive")
end

AIMessageErrorWrongMissionFruitType = {
name = "CP_ERROR_WRONG_MISSION_FRUIT_TYPE"
}
local AIMessageErrorWrongMissionFruitType_mt = Class(AIMessageErrorWrongMissionFruitType, AIMessage)

function AIMessageErrorWrongMissionFruitType.new(customMt)
local self = AIMessage.new(customMt or AIMessageErrorWrongMissionFruitType_mt)

return self
end

---@class AIMessageErrorWrongMissionFruitType
AIMessageErrorWrongMissionFruitType = CpObject(AIMessage, AIMessage.new)
AIMessageErrorWrongMissionFruitType.name = "CP_ERROR_WRONG_MISSION_FRUIT_TYPE"
function AIMessageErrorWrongMissionFruitType:getI18NText()
return g_i18n:getText("CP_ai_messageErrorWrongMissionFruitType")
end

CpAIMessages = {}

function CpAIMessages.register()
local function register(messageClass)
g_currentMission.aiMessageManager:registerMessage(messageClass.name, messageClass)
Expand Down
2 changes: 1 addition & 1 deletion scripts/ai/controllers/LevelerController.lua
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ function LevelerController.actionEventAttacherJointControl(object, superFunc, ..
local rootVehicle = object:getRootVehicle()
if rootVehicle and rootVehicle.getJob then
local job = rootVehicle:getJob()
if job and job:isa(CpAIJobBunkerSilo) then
if job and job.is_a and job:is_a(CpAIJobBunkerSilo) then
return
end
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 @@ -481,9 +468,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 @@ -492,4 +479,3 @@ function CpAIJob.registerJob(AIJobTypeManager)
register(CpAIJobBunkerSilo)
end

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

0 comments on commit ba724b1

Please sign in to comment.