Skip to content
This repository has been archived by the owner on May 23, 2023. It is now read-only.

Commit

Permalink
Merge pull request #7126 from Courseplay/issue-7051
Browse files Browse the repository at this point in the history
Diff fixes
  • Loading branch information
schwiti6190 authored Apr 23, 2021
2 parents 23f0ce5 + 47856cd commit fe97f76
Show file tree
Hide file tree
Showing 27 changed files with 198 additions and 67 deletions.
6 changes: 6 additions & 0 deletions AIDriver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,12 @@ function AIDriver:init(vehicle)
self.triggerHandler:enableFuelLoading()
end

---This function is called once on the first update tick,
---for post setup possibilities.
function AIDriver:postInit()

end

function AIDriver:updateLoadingText()
local fillableObject = self.triggerHandler.fillableObject
if fillableObject then
Expand Down
35 changes: 23 additions & 12 deletions ActionEventsLoader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ ActionEventsLoader.actionEventAttributes = {
{name = 'callbackState', getXmlFunction = getXMLInt},
{name = 'isDisabledCallbackFunc', getXmlFunction = getXMLString},
{name = 'text', getXmlFunction = getXMLString},
{name = 'textAlwaysVisible', getXmlFunction = getXMLBool},
}

---All the setting action events attributes
Expand All @@ -21,6 +22,7 @@ ActionEventsLoader.settingActionEventAttributes = {
{name = 'callbackState', getXmlFunction = getXMLInt},
{name = 'isDisabledCallbackFunc', getXmlFunction = getXMLString},
{name = 'text', getXmlFunction = getXMLString},
{name = 'textAlwaysVisible', getXmlFunction = getXMLBool},
}

---String to class reference, only for global classes.
Expand Down Expand Up @@ -172,18 +174,18 @@ function ActionEventsLoader.registerGlobalActionEvents()

local actionEventCallback = ActionEventsLoaderUtil.getActionEventCallback(actionEventData,class)
local actionEventText = ActionEventsLoaderUtil.getActionEventText(actionEventData)

local actionEventTextVisible = ActionEventsLoaderUtil.isActionEventTextVisible(actionEventData,vehicle)
local isDisabled = ActionEventsLoaderUtil.getActionEventIsDisabled(actionEventData,class,classParameter)

courseplay.debugFormat(courseplay.DBG_HUD,"Register action event: name= %s, text= %s, isDisabled= %s",
actionEventName, courseplay:loc(actionEventText), tostring(isDisabled))
courseplay.debugFormat(courseplay.DBG_HUD,"Register action event: name= %s, text= %s, isDisabled= %s, isVisible:%s",
actionEventName, courseplay:loc(actionEventText), tostring(isDisabled),tostring(actionEventTextVisible))

---Registers an action event.
local _, eventId = g_inputBinding:registerActionEvent(actionEventName, class, actionEventCallback, false, true, false, true,actionEventData.callbackState)
g_inputBinding:setActionEventTextPriority(eventId, GS_PRIO_HIGH)
g_inputBinding:setActionEventText(eventId, courseplay:loc(actionEventText))
g_inputBinding:setActionEventActive(eventId, not isDisabled)

g_inputBinding:setActionEventTextVisibility(eventId,actionEventTextVisible)

ActionEventsLoader.globalActionEventNameToID[actionEventName] = eventId
end
Expand All @@ -208,18 +210,19 @@ function ActionEventsLoader.registerActionEvent(actionEventData,vehicle,isSettin
end
local actionEventCallback = ActionEventsLoaderUtil.getActionEventCallback(actionEventData,class)
local actionEventText = ActionEventsLoaderUtil.getActionEventText(actionEventData)

local actionEventTextVisible = ActionEventsLoaderUtil.isActionEventTextVisible(actionEventData,vehicle)
local isDisabled = ActionEventsLoaderUtil.getActionEventIsDisabled(actionEventData,class,classParameter)

courseplay.debugVehicle(courseplay.DBG_HUD,vehicle,"Register action event: name= %s, text= %s, isDisabled= %s",
actionEventName, courseplay:loc(actionEventText), tostring(isDisabled))
courseplay.debugVehicle(courseplay.DBG_HUD,vehicle,"Register action event: name= %s, text= %s, isDisabled= %s, isVisible:%s",
actionEventName, courseplay:loc(actionEventText), tostring(isDisabled),tostring(actionEventTextVisible))

---Registers an action event into the vehicle.
local _, eventId = vehicle:addActionEvent(vehicle.cpActionEvents,actionEventName, classParameter, actionEventCallback, true, false, false, true,actionEventData.callbackState)
g_inputBinding:setActionEventTextPriority(eventId, GS_PRIO_HIGH)
---Display text in the F1 window in the top left.
g_inputBinding:setActionEventText(eventId, courseplay:loc(actionEventText))
g_inputBinding:setActionEventActive(eventId, not isDisabled)
g_inputBinding:setActionEventTextVisibility(eventId,actionEventTextVisible)
---addActionEvent( action event table, action event name, action event class,
--- callback function, trigger key up, trigger key down, trigger always,
--- is active, callback state, action event icon)
Expand All @@ -236,6 +239,7 @@ function ActionEventsLoader.updateActionEvents(actionEvents,actionEventsNameToID
for _,actionEventData in ipairs(actionEvents) do
local actionEventName = ActionEventsLoaderUtil.getActionEventName(actionEventData)
local actionEventText = ActionEventsLoaderUtil.getActionEventText(actionEventData)
local actionEventTextVisible = ActionEventsLoaderUtil.isActionEventTextVisible(actionEventData,vehicle)
local actionEvent = InputAction[actionEventName] and actionEventsNameToID[InputAction[actionEventName]]
if actionEvent then
local class,classParameter
Expand All @@ -250,14 +254,15 @@ function ActionEventsLoader.updateActionEvents(actionEvents,actionEventsNameToID
local isDisabled = ActionEventsLoaderUtil.getActionEventIsDisabled(actionEventData,class,classParameter)

if vehicle then
courseplay.debugVehicle(courseplay.DBG_HUD,vehicle,"Update action event: name= %s, text= %s, isDisabled= %s",
actionEventName, courseplay:loc(actionEventText), tostring(isDisabled))
courseplay.debugVehicle(courseplay.DBG_HUD,vehicle,"Update action event: name= %s, text= %s, isDisabled= %s, isVisible:%s",
actionEventName, courseplay:loc(actionEventText), tostring(isDisabled),tostring(actionEventTextVisible))
else
courseplay.debugFormat(courseplay.DBG_HUD,"Update action event: name= %s, text= %s, isDisabled= %s",
actionEventName, courseplay:loc(actionEventText), tostring(isDisabled))
courseplay.debugFormat(courseplay.DBG_HUD,"Update action event: name= %s, text= %s, isDisabled= %s, isVisible:%s",
actionEventName, courseplay:loc(actionEventText), tostring(isDisabled),tostring(actionEventTextVisible))
end
---Enable/disable the action event
g_inputBinding:setActionEventActive(actionEvent.actionEventId, not isDisabled)
g_inputBinding:setActionEventTextVisibility(actionEvent.actionEventId,actionEventTextVisible)
end
end
end
Expand Down Expand Up @@ -323,6 +328,12 @@ function ActionEventsLoaderUtil.getActionEventText(actionEventData)
return actionEventData.text or ActionEventsLoader.textPrefix..actionEventData.name
end

---Needs the action event text to be visible?
---@param table actionEventData from the config xml
function ActionEventsLoaderUtil.isActionEventTextVisible(actionEventData)
return courseplay.globalSettings.showActionEventsTexts:get() or actionEventData.textAlwaysVisible
end

---Is the action event disabled ?
---Defaults to class.isDisabled , if the is disabled callback function is not defined or it is always enabled
---@param table actionEventData from the config xml
Expand Down
64 changes: 33 additions & 31 deletions CpManager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ function CpManager:loadMap(name)
self.cpCoursesFolderPath = ("%s%s/%s"):format(getUserProfileAppPath(),"CoursePlay_Courses", g_currentMission.missionInfo.mapId);
self.cpCourseManagerXmlFilePath = self.cpCoursesFolderPath .. "/courseManager.xml";
self.cpCourseStorageXmlFileTemplate = "courseStorage%04d.xml";
self.cpDebugPrintXmlFilePath = string.format("%s%s",getUserProfileAppPath(),"courseplayDebugPrint.xml")
self.cpDebugPrintXmlFolderPath = string.format("%s%s",getUserProfileAppPath(),"courseplayDebugPrint")
self.cpDebugPrintXmlFilePathDefault = string.format("%s/%s",self.cpDebugPrintXmlFolderPath,"courseplayDebugPrint.xml")
createFolder(self.cpDebugPrintXmlFolderPath)

-- we need to create CoursePlay_Courses folder before we can create any new folders inside it.
createFolder(("%sCoursePlay_Courses"):format(getUserProfileAppPath()));
createFolder(self.cpCoursesFolderPath);

-- Add / at end of path, so we dont save that in the courseManager.xml (Needs to be done after folder creation!)
self.cpCoursesFolderPath = self.cpCoursesFolderPath .. "/";
end
Expand Down Expand Up @@ -104,6 +106,7 @@ function CpManager:loadMap(name)
addConsoleCommand('cpStopAll', 'Stop all Courseplayers', 'devStopAll', self);
addConsoleCommand( 'cpSaveAllFields', 'Save all fields', 'devSaveAllFields', self )
addConsoleCommand( 'print', 'Print a variable', 'printVariable', self )
addConsoleCommand( 'printGlobalCpVariable', 'Print a global cp variable', 'printGlobalCpVariable', self )
addConsoleCommand( 'printVehicleVariable', 'Print g_currentMission.controlledVehicle.variable', 'printVehicleVariable', self )
addConsoleCommand( 'printDriverVariable', 'Print g_currentMission.controlledVehicle.cp.driver.variable', 'printDriverVariable', self )
addConsoleCommand( 'printSettingVariable', 'Print g_currentMission.controlledVehicle.cp.settings.variable', 'printSettingVariable', self )
Expand Down Expand Up @@ -550,13 +553,14 @@ function CpManager:devSaveAllFields()
return( 'All fields saved' )
end

--- Print a global variable
-- @param string variableName name of the variable, can be multiple levels
-- @param int depth maximum depth, 1 by default
-- @param int printToXML and printToXML>0 => printing variable to xmlFile
function CpManager:printVariable(variableName, maxDepth,printToXML, printShortVersion)
---Prints a variable to the console or a xmlFile.
---@param string variableName name of the variable, can be multiple levels
---@param int depth maximum depth, 1 by default
---@param int printToXML: should the variable be printed to an xml file ? (optional)
---@param int printToSeparateXmlFiles: should the variable be printed to an xml file named after the variable ? (optional)
function CpManager:printVariable(variableName, maxDepth,printToXML, printToSeparateXmlFiles)
if printToXML and tonumber(printToXML) and tonumber(printToXML)>0 then
HelperUtil.printVariableToXML(variableName, maxDepth)
HelperUtil.printVariableToXML(variableName, maxDepth,printToSeparateXmlFiles)
return
end
print(string.format('%s - %s', tostring(variableName), tostring(maxDepth)))
Expand All @@ -566,17 +570,11 @@ function CpManager:printVariable(variableName, maxDepth,printToXML, printShortVe
if value then
print(string.format('Printing %s (%s), depth %d', variableName, valueType, depth))
if valueType == 'table' then
if not printShortVersion then
DebugUtil.printTableRecursively(value, ' ', 1, depth)
local mt = getmetatable(value)
if mt and type(mt) == 'table' then
print('-- metatable -->')
DebugUtil.printTableRecursively(mt, ' ', 1, depth)
end
else
--courseplay:printMeThisTable(table,level,maxlevel,upperPath)
courseplay.alreadyPrinted = {}
courseplay:printMeThisTable(value,0,depth,variableName)
DebugUtil.printTableRecursively(value, ' ', 1, depth)
local mt = getmetatable(value)
if mt and type(mt) == 'table' then
print('-- metatable -->')
DebugUtil.printTableRecursively(mt, ' ', 1, depth)
end
else
print(variableName .. ': ' .. tostring(value))
Expand All @@ -591,33 +589,37 @@ end

--- Print the variable in the selected vehicle's namespace
-- You can omit the dot for data members but if you want to call a function, you must start the variable name with a colon
function CpManager:printVehicleVariable(variableName, maxDepth, printToXML)
self:printVariableInternal( 'g_currentMission.controlledVehicle', variableName, maxDepth, printToXML)
function CpManager:printVehicleVariable(variableName, maxDepth, printToXML,printToSeparateXmlFiles)
self:printVariableInternal( 'g_currentMission.controlledVehicle', variableName, maxDepth, printToXML,printToSeparateXmlFiles)
end

function CpManager:printDriverVariable(variableName, maxDepth, printToXML,printToSeparateXmlFiles)
self:printVariableInternal( 'g_currentMission.controlledVehicle.cp.driver', variableName, maxDepth, printToXML,printToSeparateXmlFiles)
end

function CpManager:printDriverVariable(variableName, maxDepth, printToXML)
self:printVariableInternal( 'g_currentMission.controlledVehicle.cp.driver', variableName, maxDepth, printToXML)
function CpManager:printSettingVariable(variableName, maxDepth, printToXML,printToSeparateXmlFiles)
self:printVariableInternal( 'g_currentMission.controlledVehicle.cp.settings', variableName, maxDepth, printToXML,printToSeparateXmlFiles)
end

function CpManager:printSettingVariable(variableName, maxDepth, printToXML)
self:printVariableInternal( 'g_currentMission.controlledVehicle.cp.settings', variableName, maxDepth, printToXML)
function CpManager:printCourseGeneratorSettingVariable(variableName, maxDepth, printToXML,printToSeparateXmlFiles)
self:printVariableInternal( 'g_currentMission.controlledVehicle.cp.courseGeneratorSettings', variableName, maxDepth, printToXML,printToSeparateXmlFiles)
end

function CpManager:printCourseGeneratorSettingVariable(variableName, maxDepth, printToXML)
self:printVariableInternal( 'g_currentMission.controlledVehicle.cp.courseGeneratorSettings', variableName, maxDepth, printToXML)
function CpManager:printGlobalSettingVariable(variableName, maxDepth, printToXML,printToSeparateXmlFiles)
self:printVariableInternal( 'courseplay.courseplay.globalSettings', variableName, maxDepth, printToXML,printToSeparateXmlFiles)
end

function CpManager:printGlobalSettingVariable(variableName, maxDepth, printToXML)
self:printVariableInternal( 'courseplay.courseplay.globalSettings', variableName, maxDepth, printToXML)
function CpManager:printGlobalCpVariable(variableName, maxDepth, printToXML,printToSeparateXmlFiles)
self:printVariableInternal( 'courseplay', variableName, maxDepth, printToXML,printToSeparateXmlFiles)
end


function CpManager:printVariableInternal(prefix, variableName, maxDepth,printToXML)
function CpManager:printVariableInternal(prefix, variableName, maxDepth,printToXML,printToSeparateXmlFiles)
if not StringUtil.startsWith(variableName, ':') and not StringUtil.startsWith(variableName, '.') then
-- allow to omit the . at the beginning of the variable name.
prefix = prefix .. '.'
end
self:printVariable(prefix .. variableName, maxDepth,printToXML)
self:printVariable(prefix .. variableName, maxDepth,printToXML,printToSeparateXmlFiles)
end


Expand Down
19 changes: 19 additions & 0 deletions GlobalSettings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,25 @@ function ShowMiniHudSetting:init()
self:set(false)
end

---@class ShowActionEventTextsSetting : BooleanSetting
ShowActionEventTextsSetting = CpObject(BooleanSetting)
function ShowActionEventTextsSetting:init()
BooleanSetting.init(self, 'showActionEventsTexts', 'COURSEPLAY_SHOW_ACTION_EVENTS_TEXTS',
'COURSEPLAY_SHOW_ACTION_EVENTS_TEXTS_TOOLTIP', nil)
-- set default while we are transitioning from the the old setting to this new one
self:set(true)
end

---On setting change, make sure the current entered vehicle gets updated.
function ShowActionEventTextsSetting:onChange()
if g_currentMission then
local vehicle = g_currentMission.controlledVehicle
if vehicle ~= nil then
ActionEventsLoader.updateAllActionEvents(vehicle)
end
end
end


---@class AutoRepairSetting : SettingList
AutoRepairSetting = CpObject(SettingList)
Expand Down
26 changes: 24 additions & 2 deletions OverloaderAIDriver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ OverloaderAIDriver.myStates = {
OVERLOADING = {},
}

OverloaderAIDriver.MIN_SPEED_UNLOAD_COURSE = 10


function OverloaderAIDriver:init(vehicle)
--there seems to be a bug, where "vehicle" is not always set once start is pressed
CombineUnloadAIDriver.init(self, vehicle)
Expand All @@ -37,6 +40,13 @@ function OverloaderAIDriver:init(vehicle)
self.nearOverloadPoint = false
end

function OverloaderAIDriver:postInit()
---Refresh the Hud content here,as otherwise the moveable pipe is not
---detected the first time after loading a savegame.
self:setHudContent()
CombineUnloadAIDriver.postInit(self)
end

function OverloaderAIDriver:findPipeAndTrailer()
local implementWithPipe = AIDriverUtil.getImplementWithSpecialization(self.vehicle, Pipe)
if implementWithPipe then
Expand Down Expand Up @@ -145,13 +155,13 @@ end
function OverloaderAIDriver:onWaypointChange(ix)
-- this is called when the next wp changes, that is well before we get there
-- save it in a variable to avoid the relatively expensive hasWaitPointWithinDistance to be called too often
self.nearOverloadPoint = self.course:hasWaitPointWithinDistance(ix, 30)
self.nearOverloadPoint,self.closestOverloadPointIx = self.course:hasWaitPointWithinDistance(ix, 30)
CombineUnloadAIDriver.onWaypointChange(self, ix)
end

function OverloaderAIDriver:onWaypointPassed(ix)
-- just in case...
self.nearOverloadPoint = self.course:hasWaitPointWithinDistance(ix, 30)
self.nearOverloadPoint, self.closestOverloadPointIx = self.course:hasWaitPointWithinDistance(ix, 30)
if self.course:isWaitAt(ix) then
if self:isTrailerEmpty() then
self:debug('Wait point reached but my trailer is empty, continuing')
Expand All @@ -164,6 +174,18 @@ function OverloaderAIDriver:onWaypointPassed(ix)
end
end

function OverloaderAIDriver:getSpeed()

local defaultSpeed = CombineUnloadAIDriver.getSpeed(self)

if self.unloadCourseState == self.states.ENROUTE and self.closestOverloadPointIx then
local distToWaitPoint = self.course:getDistanceBetweenVehicleAndWaypoint(self.vehicle,self.closestOverloadPointIx)
return MathUtil.clamp(distToWaitPoint, self.MIN_SPEED_UNLOAD_COURSE, defaultSpeed)
else
return defaultSpeed
end
end

function OverloaderAIDriver:isTrailerEmpty()
if self.trailer and self.trailer.getFillUnits then
for _, fillUnit in pairs(self.trailer:getFillUnits()) do
Expand Down
10 changes: 10 additions & 0 deletions base.lua
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,16 @@ function courseplay:onUpdate(dt)
self.cp.infoText = nil
end

if self.cp.postInitDone == nil then
if self.cp.driver then
---Post init function, as not all giants variables are
---set correctly at the first courseplay:setAIDriver() call.
self.cp.driver:postInit()
self.cp.postInitDone = true
end
end


if self.cp.drawCourseMode == courseplay.COURSE_2D_DISPLAY_DBGONLY or self.cp.drawCourseMode == courseplay.COURSE_2D_DISPLAY_BOTH then
courseplay:drawWaypointsLines(self);
end;
Expand Down
Loading

0 comments on commit fe97f76

Please sign in to comment.