diff --git a/scripts/CpGlobalSettings.lua b/scripts/CpGlobalSettings.lua index f7b8ff2b9..b08c48201 100644 --- a/scripts/CpGlobalSettings.lua +++ b/scripts/CpGlobalSettings.lua @@ -8,6 +8,8 @@ function CpGlobalSettings:init() g_messageCenter:subscribe(MessageType.SETTING_CHANGED[GameSettings.SETTING.USE_MILES], self.onUnitChanged, self) g_messageCenter:subscribe(MessageType.SETTING_CHANGED[GameSettings.SETTING.USE_ACRE], self.onUnitChanged, self) g_messageCenter:subscribe(MessageType.CP_DISTANCE_UNIT_CHANGED, self.onUnitChanged, self) + + self:registerConsoleCommands() end function CpGlobalSettings:registerXmlSchema(schema,baseKey) @@ -105,4 +107,27 @@ end function CpGlobalSettings:debug(str,...) CpUtil.debugFormat(CpDebug.DBG_HUD,"Global settings: "..str,...) -end \ No newline at end of file +end + +--------------------------------------------- +--- Console Commands +--------------------------------------------- + +function CpGlobalSettings:registerConsoleCommands() + g_devHelper.consoleCommands:registerConsoleCommand("cpSettingsPrintGlobal", + "Prints the global settings or a given setting", + "consoleCommandPrintSetting", self) +end + +function CpGlobalSettings:consoleCommandPrintSetting(name) + if name == nil then + CpUtil.info("%d Global settings printed", tostring(self.settings)) + return + end + local num = tonumber(name) + if num then + CpUtil.info(tostring(self.settings[num])) + return + end + CpUtil.info(tostring(self[name])) +end diff --git a/scripts/CpSettingsUtil.lua b/scripts/CpSettingsUtil.lua index 9c4815955..24d9639bc 100644 --- a/scripts/CpSettingsUtil.lua +++ b/scripts/CpSettingsUtil.lua @@ -137,10 +137,17 @@ end ---@param class table class to save the data ---@param filePath string function CpSettingsUtil.loadSettingsFromSetup(class, filePath) - local xmlFile = XMLFile.load("settingSetupXml", filePath, CpSettingsUtil.setupXmlSchema) - class.settings = {} + local xmlFile = XMLFile.load("settingSetupXml", filePath, CpSettingsUtil.setupXmlSchema) + class.settings = {} + setmetatable(class.settings, { __tostring = function(self) + --- Adds tostring function to print all settings + for i, s in ipairs(self) do + CpUtil.info("%2d: %s", i, tostring(s)) + end + return tostring(#self) + end}) class.settingsBySubTitle = {} - local uniqueID = 0 + local uniqueID = 0 local autoUpdateGui = xmlFile:getValue("Settings#autoUpdateGui") local setupKey = xmlFile:getValue("Settings#prefixText") local pageTitle = xmlFile:getValue("Settings#title") @@ -256,7 +263,9 @@ end ---@param class table ---@param settings table function CpSettingsUtil.cloneSettingsTable(class, settings, ...) + local mt = getmetatable(settings) class.settings = {} + setmetatable(class.settings, mt) for _, setting in ipairs(settings) do local settingClone = setting:clone(...) table.insert(class.settings, settingClone) diff --git a/scripts/CpUtil.lua b/scripts/CpUtil.lua index 210c062bd..84dc7dca8 100644 --- a/scripts/CpUtil.lua +++ b/scripts/CpUtil.lua @@ -109,30 +109,6 @@ function CpUtil.getVariable(variableName) return f and f() or nil end -function CpUtil.internalPrint(printFunc, infoString, ...) - CpUtil.try( - function (...) - printFunc(string.format('%s %s', infoString, string.format(...))) - end, - ...) -end - --- convenience debug function that expects string.format() arguments, --- CpUtil.debugVehicle( CpDebug.DBG_TURN, "fill level is %.1f, mode = %d", fillLevel, mode ) ----@param channel number -function CpUtil.debugFormat(channel, ...) - if CpDebug and CpDebug:isChannelActive(channel) then - local updateLoopIndex = g_updateLoopIndex and g_updateLoopIndex or 0 - local timestamp = getDate( ":%S") - channel = channel or 0 - CpUtil.try( - function (...) - print(string.format('%s [dbg%d lp%d] %s', timestamp, channel, updateLoopIndex, string.format(...))) - end, - ...) - end -end - --- (Safely) get the name of a vehicle or implement. ---@param object table vehicle or implement function CpUtil.getName(object) @@ -149,6 +125,35 @@ function CpUtil.getName(object) return object.getName and object:getName() .. '/' .. helperName or 'Unknown' end +function CpUtil.internalPrint(tag, ...) + CpUtil.try( + function (...) + local updateLoopIndex = g_updateLoopIndex and g_updateLoopIndex or 0 + local timestamp = getDate( ":%S") + local infoString = string.format('%s [%s lp%d]', timestamp, tag or "---", updateLoopIndex) + print(string.format('%s %s', infoString, string.format(...))) + end, ...) +end + +function CpUtil.internalPrintVehicle(vehicle, tag, str, ...) + str = string.format("%s: %s", CpUtil.getName(vehicle), str) + CpUtil.internalPrint(tag, str, ...) +end + +function CpUtil.internalPrintImplement(implement, tag, str, ...) + str = string.format("%s: %s", CpUtil.getName(implement), str) + CpUtil.internalPrintVehicle(implement.rootVehicle, tag, str, ...) +end + +-- convenience debug function that expects string.format() arguments, +-- CpUtil.debugVehicle( CpDebug.DBG_TURN, "fill level is %.1f, mode = %d", fillLevel, mode ) +---@param channel number +function CpUtil.debugFormat(channel, ...) + if CpDebug and CpDebug:isChannelActive(channel) then + CpUtil.internalPrint("dbg"..channel, ...) + end +end + -- convenience debug function to show the vehicle name and expects string.format() arguments, -- CpUtil.debugVehicle( CpDebug.DBG_TURN, vehicle, "fill level is %.1f, mode = %d", fillLevel, mode ) ---@param channel number @@ -157,14 +162,7 @@ function CpUtil.debugVehicle(channel, vehicle, ...) local rootVehicle = vehicle and vehicle.rootVehicle local active = rootVehicle == nil or rootVehicle.getCpSettings == nil or CpUtil.isVehicleDebugActive(rootVehicle) if CpDebug and active and CpDebug:isChannelActive(channel) then - local updateLoopIndex = g_updateLoopIndex and g_updateLoopIndex or 0 - local timestamp = getDate( ":%S") - channel = channel or 0 - CpUtil.try( - function (...) - print(string.format('%s [dbg%d lp%d] %s: %s', timestamp, channel, updateLoopIndex, CpUtil.getName(vehicle), string.format( ... ))) - end, - ...) + CpUtil.internalPrintVehicle(vehicle, "dbg"..channel, ...) end end @@ -183,45 +181,31 @@ function CpUtil.isVehicleDebugActive(vehicle) end function CpUtil.info(...) - local updateLoopIndex = g_updateLoopIndex and g_updateLoopIndex or 0 - local timestamp = getDate( ":%S") - CpUtil.try( - function (...) - print(string.format('%s [info lp%d] %s', timestamp, updateLoopIndex, string.format(...))) - end, - ...) + CpUtil.internalPrint("info", ...) end function CpUtil.infoVehicle(vehicle, ...) - local updateLoopIndex = g_updateLoopIndex and g_updateLoopIndex or 0 - local timestamp = getDate( ":%S") - CpUtil.try( - function (...) - print(string.format('%s [info lp%d] %s: %s', timestamp, updateLoopIndex, CpUtil.getName(vehicle), string.format( ... ))) - end, - ...) + CpUtil.internalPrintVehicle(vehicle, "info", ...) end function CpUtil.infoImplement(implement, ...) - local updateLoopIndex = g_updateLoopIndex and g_updateLoopIndex or 0 - local timestamp = getDate( ":%S") - CpUtil.try( - function (...) - local rootVehicle = implement.rootVehicle or implement - print(string.format('%s [info lp%d] %s(%s): %s', timestamp, updateLoopIndex, CpUtil.getName(rootVehicle), CpUtil.getName(implement), string.format( ... ))) - end, - ...) + CpUtil.internalPrintImplement(implement, "info", ...) end --- The same as CpUtil.info(), but also uses printCallstack() function CpUtil.error(...) printCallstack() - local updateLoopIndex = g_updateLoopIndex and g_updateLoopIndex or 0 - local timestamp = getDate( ":%S") - if printError == nil then - printError = print - end - CpUtil.internalPrint(printError, string.format('%s [error lp%d]', timestamp, updateLoopIndex), ...) + CpUtil.internalPrint("error", ...) +end + +function CpUtil.errorVehicle(vehicle, ...) + printCallstack() + CpUtil.internalPrintVehicle( vehicle, "error", ...) +end + +function CpUtil.errorImplement(implement, ...) + printCallstack() + CpUtil.internalPrintImplement(implement, "error", ...) end --- Create a node at x, z, direction according to yRotation. diff --git a/scripts/ai/jobs/CpAIJob.lua b/scripts/ai/jobs/CpAIJob.lua index 24c193ab8..9d36dfb35 100644 --- a/scripts/ai/jobs/CpAIJob.lua +++ b/scripts/ai/jobs/CpAIJob.lua @@ -1,6 +1,7 @@ --- Basic cp job. --- Every cp job should be derived from this job. ---@class CpAIJob : AIJob +---@field jobTypeIndex number CpAIJob = { name = "", jobName = "", @@ -9,7 +10,13 @@ CpAIJob = { local AIJobCp_mt = Class(CpAIJob, AIJob) function CpAIJob.new(isServer, customMt) - local self = AIJob.new(isServer, customMt or AIJobCp_mt) + local mt = customMt or AIJobCp_mt + mt.__tostring = function(_self) + CpUtil.info("Job: %s", g_currentMission.aiJobTypeManager:getJobTypeByIndex(_self.jobTypeIndex).name) + return string.format("%d Job parameters", tostring(_self.cpJobParameters)) + end + + local self = AIJob.new(isServer, mt) self.isDirectStart = false self:setupTasks(isServer) diff --git a/scripts/ai/jobs/CpJobParameters.lua b/scripts/ai/jobs/CpJobParameters.lua index e734b4719..58e4bf271 100644 --- a/scripts/ai/jobs/CpJobParameters.lua +++ b/scripts/ai/jobs/CpJobParameters.lua @@ -139,9 +139,7 @@ end function CpJobParameters:__tostring() - for i, setting in ipairs(self.settings) do - CpUtil.info("%s", tostring(setting)) - end + return tostring(self.settings) end --- Are the setting values roughly equal. diff --git a/scripts/ai/parameters/AIParameterSetting.lua b/scripts/ai/parameters/AIParameterSetting.lua index a38e4167c..d7bd08909 100644 --- a/scripts/ai/parameters/AIParameterSetting.lua +++ b/scripts/ai/parameters/AIParameterSetting.lua @@ -147,6 +147,24 @@ function AIParameterSetting:debug(str, ...) end end +function AIParameterSetting:info(str, ...) + local name = string.format("%s: ", self.name) + if self.vehicle == nil then + CpUtil.info(name..str, ...) + else + CpUtil.infoVehicle(self.vehicle, name..str, ...) + end +end + +function AIParameterSetting:error(str, ...) + local name = string.format("%s: ", self.name) + if self.vehicle == nil then + CpUtil.error(name..str, ...) + else + CpUtil.errorVehicle(self.vehicle, name..str, ...) + end +end + function AIParameterSetting:getDebugString() return tostring(self) end \ No newline at end of file diff --git a/scripts/ai/parameters/AIParameterSettingList.lua b/scripts/ai/parameters/AIParameterSettingList.lua index 13bf97c00..edeed12ef 100644 --- a/scripts/ai/parameters/AIParameterSettingList.lua +++ b/scripts/ai/parameters/AIParameterSettingList.lua @@ -313,8 +313,7 @@ end function AIParameterSettingList:getClosestIxFromSetup(ix) local value = self.data.values[ix] if value == nil then - self:debug("Setting value bugged, ix: %s", tostring(ix)) - printCallstack() + self:error("Setting value bugged, ix: %s", tostring(ix)) return 1 end -- find the value requested diff --git a/scripts/specializations/CpAIWorker.lua b/scripts/specializations/CpAIWorker.lua index 1b95ca492..1215df1ac 100644 --- a/scripts/specializations/CpAIWorker.lua +++ b/scripts/specializations/CpAIWorker.lua @@ -538,7 +538,11 @@ end function CpAIWorker.registerConsoleCommands() g_devHelper.consoleCommands:registerConsoleCommand("cpVehicleOnWorkStartTest", - "Raise the field work start event.", "consoleCommandRaiseWorkStart", CpAIWorker) + "Raise the field work start event.", + "consoleCommandRaiseWorkStart", CpAIWorker) + g_devHelper.consoleCommands:registerConsoleCommand("cpSettingsPrintJob", + "Prints the current job and job parameters", + "consoleCommandPrintCurrentSelectedJob", CpAIWorker) --- TODO: Adding functions to execute the lowering, raising and fieldwork end events. end @@ -563,3 +567,16 @@ function CpAIWorker:consoleCommandRaiseWorkStart() c:delete() end end + +function CpAIWorker:consoleCommandPrintCurrentSelectedJob() + local vehicle = g_currentMission.controlledVehicle + if not vehicle or vehicle.getCpStartableJob == nil then + CpUtil.info("Not entered a valid vehicle!") + return + end + local job = vehicle:getCpStartableJob() + if not job then + CpUtil.infoVehicle(vehicle, "No valid job found!") + end + CpUtil.infoVehicle(vehicle, tostring(job)) +end \ No newline at end of file diff --git a/scripts/specializations/CpCourseGeneratorSettings.lua b/scripts/specializations/CpCourseGeneratorSettings.lua index 486fc7dff..49999f2cc 100644 --- a/scripts/specializations/CpCourseGeneratorSettings.lua +++ b/scripts/specializations/CpCourseGeneratorSettings.lua @@ -23,6 +23,8 @@ function CpCourseGeneratorSettings.initSpecialization() CpSettingsUtil.registerXmlSchema(schema, "vehicles.vehicle(?)"..CpCourseGeneratorSettings.KEY..CpCourseGeneratorSettings.VINE_SETTINGS_KEY.."(?)") CpCourseGeneratorSettings.loadSettingsSetup() + + CpCourseGeneratorSettings.registerConsoleCommands() end @@ -234,4 +236,37 @@ function CpCourseGeneratorSettings:generateWorkWidthSettingValuesAndTexts(settin table.insert(texts, i) end return values, texts -end \ No newline at end of file +end + +--------------------------------------------- +--- Console Commands +--------------------------------------------- + +function CpCourseGeneratorSettings.registerConsoleCommands() + g_devHelper.consoleCommands:registerConsoleCommand("cpSettingsPrintGenerator", + "Prints the course generator settings or a given setting", + "consoleCommandPrintSetting", CpCourseGeneratorSettings) +end + +function CpCourseGeneratorSettings:consoleCommandPrintSetting(name) + local vehicle = g_currentMission.controlledVehicle + if not vehicle then + CpUtil.info("Not entered a valid vehicle!") + return + end + local spec = vehicle.spec_cpCourseGeneratorSettings + if not spec then + CpUtil.infoVehicle(vehicle, "has no course generator settings!") + return + end + if name == nil then + CpUtil.infoVehicle(vehicle,"%d Course generator settings printed", tostring(spec.settings)) + return + end + local num = tonumber(name) + if num then + CpUtil.infoVehicle(vehicle, tostring(spec.settings[num])) + return + end + CpUtil.infoVehicle(vehicle, tostring(spec[name])) +end diff --git a/scripts/specializations/CpVehicleSettings.lua b/scripts/specializations/CpVehicleSettings.lua index c17a577f8..9dbaa0475 100644 --- a/scripts/specializations/CpVehicleSettings.lua +++ b/scripts/specializations/CpVehicleSettings.lua @@ -26,6 +26,8 @@ function CpVehicleSettings.initSpecialization() "vehicles.vehicle(?)" .. CpVehicleSettings.KEY .. CpVehicleSettings.USER_KEY .. "(?)") CpVehicleSettings.loadSettingsSetup() + + CpVehicleSettings.registerConsoleCommands() end @@ -414,3 +416,36 @@ function CpVehicleSettings:generateSpeedSettingValuesAndTexts(setting, lastValue end return values, texts, math.min(lastValue, maxSpeed) end + +--------------------------------------------- +--- Console Commands +--------------------------------------------- + +function CpVehicleSettings.registerConsoleCommands() + g_devHelper.consoleCommands:registerConsoleCommand("cpSettingsPrintVehicle", + "Prints the vehicle settings or a given setting", + "consoleCommandPrintSetting", CpVehicleSettings) +end + +function CpVehicleSettings:consoleCommandPrintSetting(name) + local vehicle = g_currentMission.controlledVehicle + if not vehicle then + CpUtil.info("Not entered a valid vehicle!") + return + end + local spec = vehicle.spec_cpVehicleSettings + if not spec then + CpUtil.infoVehicle(vehicle, "has no vehicle settings!") + return + end + if name == nil then + CpUtil.infoVehicle(vehicle,"%d Vehicle settings printed", tostring(spec.settings)) + return + end + local num = tonumber(name) + if num then + CpUtil.infoVehicle(vehicle, tostring(spec.settings[num])) + return + end + CpUtil.infoVehicle(vehicle, tostring(spec[name])) +end