Skip to content

Commit eb0eedf

Browse files
authored
Merge pull request #289 from Courseplay/diff-mp-fixes
Changed settings event calls
2 parents 9dfc680 + 31b8215 commit eb0eedf

9 files changed

Lines changed: 42 additions & 27 deletions

Courseplay.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ function Courseplay:loadMap(filename)
106106
self:load()
107107
self:setupGui()
108108
self:loadUserSettings()
109-
if g_currentMission.missionInfo.savegameDirectory ~= nil then
109+
if g_server ~= nil and g_currentMission.missionInfo.savegameDirectory ~= nil then
110110
local saveGamePath = g_currentMission.missionInfo.savegameDirectory .."/"
111111
local filePath = saveGamePath .. "Courseplay.xml"
112112
self.xmlFile = XMLFile.load("cpXml", filePath , self.xmlSchema)

scripts/ai/parameters/AIParameterBooleanSetting.lua

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,15 @@ function AIParameterBooleanSetting:loadFromXMLFile(xmlFile, key)
3434
local rawValue = xmlFile:getString(key .. "#currentValue")
3535
if rawValue ~= nil then
3636
self.loadedValue = rawValue
37-
self:setValue(rawValue == "true" or false)
37+
self:setValue(rawValue == "true" or false, true)
3838
else
3939
self:loadFromXMLFileLegacy(xmlFile, key)
4040
end
4141
end
4242

43-
function AIParameterBooleanSetting:resetToLoadedValue()
43+
function AIParameterBooleanSetting:resetToLoadedValue(noEventSend)
4444
if self.loadedValue ~= nil then
45-
self:setValue(self.loadedValue)
46-
self:raiseDirtyFlag()
45+
self:setValue(self.loadedValue, noEventSend)
4746
end
4847
end
4948

scripts/ai/parameters/AIParameterSetting.lua

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,12 @@ end
128128
--- Make sure the setting value gets synchronized by the class.
129129
function AIParameterSetting:raiseDirtyFlag()
130130
if not self:getIsUserSetting() then
131-
if self.class and self.class.raiseDirtyFlag then
131+
if self.class ~= nil and self.class.raiseDirtyFlag ~= nil then
132132
if self.vehicle ~= nil then
133+
self:debug("Raising dirty flag for vehicle", CpUtil.getName(self.vehicle))
133134
self.class.raiseDirtyFlag(self.vehicle, self)
134135
else
136+
self:debug("Raising dirty flag")
135137
self.class:raiseDirtyFlag(self)
136138
end
137139
end

scripts/ai/parameters/AIParameterSettingInterface.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,12 @@ function AIParameterSettingInterface:refresh()
103103
end
104104

105105
--- Sets a float value relative to the incremental.
106-
function AIParameterSettingInterface:setFloatValue(value, epsilon)
106+
function AIParameterSettingInterface:setFloatValue(value, epsilon, noEventSend)
107107

108108
end
109109

110110
--- Sets a value.
111-
function AIParameterSettingInterface:setValue(value)
111+
function AIParameterSettingInterface:setValue(value, noEventSend)
112112

113113
end
114114

scripts/ai/parameters/AIParameterSettingList.lua

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ function AIParameterSettingList:init(data, vehicle, class)
5454
end
5555
--- Lastly apply the default values here.
5656
if data.default ~=nil then
57-
AIParameterSettingList.setFloatValue(self, data.default)
57+
AIParameterSettingList.setFloatValue(self, data.default, nil, true)
5858
self:debug("set to default %s", data.default)
5959
end
6060
if data.defaultBool ~= nil then
61-
AIParameterSettingList.setValue(self, data.defaultBool)
61+
AIParameterSettingList.setValue(self, data.defaultBool, true)
6262
self:debug("set to default %s", tostring(data.defaultBool))
6363
end
6464

@@ -273,7 +273,7 @@ function AIParameterSettingList:loadFromXMLFile(xmlFile, key)
273273
self:debug("loaded value: %.2f", value)
274274
self.loadedValue = value
275275
--- Applies a small epsilon, as otherwise floating point problems might happen.
276-
self:setFloatValue(value, 0.001)
276+
self:setFloatValue(value, 0.001, true)
277277
else
278278
self:loadFromXMLFileLegacy(xmlFile, key)
279279
end
@@ -388,11 +388,15 @@ end
388388
---@param value number
389389
---@param epsilon number|nil optional
390390
---@return boolean value is not valid and could not be set.
391-
function AIParameterSettingList:setFloatValue(value, epsilon)
392-
return setValueInternal(self, value, function(a, b)
391+
function AIParameterSettingList:setFloatValue(value, epsilon, noEventSend)
392+
local failed = setValueInternal(self, value, function(a, b)
393393
local epsilon = epsilon or self.data.incremental or 0.1
394394
if a == nil or b == nil then return false end
395395
return a > b - epsilon / 2 and a <= b + epsilon / 2 end)
396+
if not failed and not noEventSend then
397+
self:raiseDirtyFlag()
398+
end
399+
return failed
396400
end
397401

398402
--- Gets the closest value ix and absolute difference, relative to the value searched for.
@@ -416,16 +420,21 @@ end
416420

417421
--- Sets a value.
418422
---@param value number
423+
---@param noEventSend boolean|nil
419424
---@return boolean value is not valid and could not be set.
420-
function AIParameterSettingList:setValue(value)
421-
return setValueInternal(self, value, function(a, b) return a == b end)
425+
function AIParameterSettingList:setValue(value, noEventSend)
426+
local failed = setValueInternal(self, value, function(a, b) return a == b end)
427+
if not failed and not noEventSend then
428+
self:raiseDirtyFlag()
429+
end
430+
return failed
422431
end
423432

424433
function AIParameterSettingList:setDefault(noEventSend)
425434
local current = self.current
426435
--- If the setting has a function to set the default value, then call it.
427436
if self:hasCallback(self.data.setDefaultFunc) then
428-
self:getCallback(self.data.setDefaultFunc)
437+
self:getCallback(self.data.setDefaultFunc, noEventSend)
429438
self:debug("set to default by extern function.")
430439
return
431440
end
@@ -437,9 +446,9 @@ function AIParameterSettingList:setDefault(noEventSend)
437446
local value = g_vehicleConfigurations:get(object, configName)
438447
if value then
439448
if tonumber(value) then
440-
self:setFloatValue(value)
449+
self:setFloatValue(value, noEventSend)
441450
else
442-
self:setValue(value)
451+
self:setValue(value, noEventSend)
443452
end
444453
self:debug("set to default: %s from vehicle configuration: (%s|%s)", value, CpUtil.getName(object), configName)
445454
return
@@ -449,12 +458,12 @@ function AIParameterSettingList:setDefault(noEventSend)
449458
end
450459
--- If default values were setup use these.
451460
if self.data.default ~=nil then
452-
AIParameterSettingList.setFloatValue(self, self.data.default)
461+
AIParameterSettingList.setFloatValue(self, self.data.default, noEventSend)
453462
self:debug("set to default %s", self.data.default)
454463
return
455464
end
456465
if self.data.defaultBool ~= nil then
457-
AIParameterSettingList.setValue(self, self.data.defaultBool)
466+
AIParameterSettingList.setValue(self, self.data.defaultBool, noEventSend)
458467
self:debug("set to default %s", tostring(self.data.defaultBool))
459468
return
460469
end
@@ -524,9 +533,9 @@ end
524533
--- Copy the value to another setting.
525534
function AIParameterSettingList:copy(setting)
526535
if self.data.incremental and self.data.incremental ~= 1 then
527-
self:setFloatValue(setting.values[setting.current])
536+
self:setFloatValue(setting.values[setting.current], nil, true)
528537
else
529-
self:setValue(setting.values[setting.current])
538+
self:setValue(setting.values[setting.current], true)
530539
end
531540
end
532541

scripts/ai/tasks/CpAITaskFieldWork.lua

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ function CpAITaskFieldWork:start()
6666
spec.isActive = true
6767
self.vehicle:raiseAIEvent("onAIFieldWorkerStart", "onAIImplementStart")
6868
if self.isServer then
69-
spec.isActive = true
70-
spec:updateAIFieldWorkerImplementData()
69+
self.vehicle:updateAIFieldWorkerImplementData()
7170
if self.vehicle:getAINeedsTrafficCollisionBox() and (AIFieldWorker.TRAFFIC_COLLISION ~= nil and
7271
(AIFieldWorker.TRAFFIC_COLLISION ~= 0 and spec.aiTrafficCollision == nil)) then
7372

scripts/events/VehicleSettingsEvent.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,16 @@ function VehicleSettingsEvent:run(connection, setting)
4545
end
4646

4747
function VehicleSettingsEvent.sendEvent(vehicle, setting)
48+
VehicleSettingsEvent.debug(vehicle, "sendEvent, server: %s", tostring(g_server ~= nil))
4849
if g_server ~= nil then
49-
VehicleSettingsEvent.debug(vehicle, "sendEvent")
5050
g_server:broadcastEvent(VehicleSettingsEvent.new(vehicle, setting), nil, nil, vehicle)
5151
else
5252
g_client:getServerConnection():sendEvent(VehicleSettingsEvent.new(vehicle, setting))
5353
end
5454
end
5555

5656
function VehicleSettingsEvent.debug(vehicle, str, ...)
57-
CpUtil.debugVehicle(CpDebug.DBG_MULTIPLAYER, vehicle, "VehicleSettingsEvent: "..str, ...)
57+
CpUtil.debugVehicle(CpDebug.DBG_MULTIPLAYER, vehicle, "VehicleSettingsEvent: " .. str, ...)
5858
end
5959

6060
--- Sends the changed setting value to the server,

scripts/specializations/CpAIWorker.lua

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,9 @@ function CpAIWorker:onRegisterActionEvents(isActiveForInput, isActiveForInputIgn
147147
end)
148148

149149
addActionEvent(self, InputAction.CP_GENERATE_COURSE, function (self)
150-
CourseGeneratorInterface.generateDefaultCourse()
150+
if self:getCanStartCpFieldWork() then
151+
CourseGeneratorInterface.generateDefaultCourse()
152+
end
151153
end)
152154
addActionEvent(self, InputAction.CP_CHANGE_SELECTED_JOB, function (self)
153155
local currentJobSetting = self:cpGetHudSelectedJobSetting()
@@ -238,6 +240,9 @@ function CpAIWorker:updateActionEvents()
238240

239241
actionEvent = spec.actionEvents[InputAction.CP_CLEAR_COURSE]
240242
g_inputBinding:setActionEventActive(actionEvent.actionEventId, self:hasCpCourse())
243+
244+
actionEvent = spec.actionEvents[InputAction.CP_GENERATE_COURSE]
245+
g_inputBinding:setActionEventActive(actionEvent.actionEventId, self:getCanStartCpFieldWork())
241246
end
242247
end
243248

scripts/specializations/CpVehicleSettings.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ end
104104
function CpVehicleSettings:onUpdate()
105105
local spec = self.spec_cpVehicleSettings
106106
if not spec.finishedFirstUpdate then
107+
CpVehicleSettings.validateSettings(self)
107108
--- TODO: Maybe consider a more generic approach in the future.
108109
spec.toolOffsetX:resetToLoadedValue()
109110
spec.bunkerSiloWorkWidth:resetToLoadedValue()

0 commit comments

Comments
 (0)