Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
schwiti6190 committed Mar 26, 2022
1 parent 1de6bc7 commit aa9a873
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 96 deletions.
1 change: 0 additions & 1 deletion modDesc.xml
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ Funktionen:
<sourceFile filename="scripts/events/GlobalSettingsEvent.lua"/>
<sourceFile filename="scripts/events/VehicleSettingsEvent.lua"/>
<sourceFile filename="scripts/events/DebugChannelEvent.lua"/>
<sourceFile filename="scripts/events/SendCustomFieldsToServerEvent.lua"/>
</extraSourceFiles>

<specializations>
Expand Down
13 changes: 6 additions & 7 deletions scripts/ai/AIDriveStrategyFindBales.lua
Original file line number Diff line number Diff line change
Expand Up @@ -146,18 +146,17 @@ function AIDriveStrategyFindBales:setAllStaticParameters()
self.pathfinderFailureCount = 0
end

function AIDriveStrategyFindBales:setFieldPolygon(fieldPolygon)
self.fieldPolygon = fieldPolygon
end

-----------------------------------------------------------------------------------------------------------------------
--- Bale finding
-----------------------------------------------------------------------------------------------------------------------
---@param bale BaleToCollect
function AIDriveStrategyFindBales:isBaleOnField(bale)
if self.fieldId then
-- normal field mode
return bale:getFieldId() == self.fieldId
elseif self.customField then
local x, _, z = bale:getPosition()
return self.customField:isPointOnField(x, z)
end
local x, _, z = bale:getPosition()
return CpMathUtil.isPointInPolygon(self.fieldPolygon, x, z)
end

--- Find bales on field
Expand Down
14 changes: 13 additions & 1 deletion scripts/ai/jobs/CpAIJobBaleFinder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ function CpAIJobBaleFinder:validate(farmId)
if not isValid then
return isValid, errorMessage
end
isValid, errorMessage = self:validateFieldSetup(isValid, errorMessage)
isValid, errorMessage = self:validateFieldSetup(isValid, errorMessage)
self.baleFinderTask:setFieldPolygon(self.fieldPolygon)
return isValid, errorMessage
end

function CpAIJobBaleFinder:readStream(streamId, connection)
self.fieldPolygon = CustomField.readStreamVertices(streamId, connection)
self.baleFinderTask:setFieldPolygon(self.fieldPolygon)
CpAIJobBaleFinder:superClass().readStream(self, streamId, connection)
end

function CpAIJobBaleFinder:writeStream(streamId, connection)
CustomField.writeStreamVertices(self.fieldPolygon, streamId, connection)
CpAIJobBaleFinder:superClass().writeStream(self, streamId, connection)
end
7 changes: 6 additions & 1 deletion scripts/ai/tasks/CpAITaskBaleFinder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ local AITaskBaleFinderCp_mt = Class(CpAITaskBaleFinder, AITask)
function CpAITaskBaleFinder.new(isServer, job, customMt)
local self = AITask.new(isServer, job, customMt or AITaskBaleFinderCp_mt)
self.vehicle = nil
self.fieldPolygon = nil
return self
end

Expand All @@ -20,9 +21,13 @@ function CpAITaskBaleFinder:setVehicle(vehicle)
self.vehicle = vehicle
end

function CpAITaskBaleFinder:setFieldPolygon(fieldPolygon)
self.fieldPolygon = fieldPolygon
end

function CpAITaskBaleFinder:start()
if self.isServer then
self.vehicle:startCpBaleFinder()
self.vehicle:startCpBaleFinder(self.fieldPolygon)
end

CpAITaskBaleFinder:superClass().start(self)
Expand Down
3 changes: 1 addition & 2 deletions scripts/events/CpJoinEvent.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ end

--- Runs the event on the receiving end of the event.
function CpJoinEvent:run(connection)
--- Makes sure the custom fields are send to the server.
g_customFieldManager:sendToServer()

end

function CpJoinEvent.debug(str, ...)
Expand Down
45 changes: 0 additions & 45 deletions scripts/events/SendCustomFieldsToServerEvent.lua

This file was deleted.

22 changes: 22 additions & 0 deletions scripts/field/CustomField.lua
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,25 @@ function CustomField.createFromStream(streamId, connection)
CpUtil.debugFormat(CpDebug.DBG_COURSES,'Custom field with %d points loaded from stream.', #customField.vertices)
return customField
end

function CustomField.writeStreamVertices(vertices, streamId, connection)
streamWriteInt32(streamId, #vertices)
for _, point in pairs(vertices) do
streamWriteFloat32(streamId, point.x)
streamWriteFloat32(streamId, point.z)
end
end

function CustomField.readStreamVertices(streamId, connection)
local numVertices = streamReadInt32(streamId)
local vertices = {}
local p = {}
for i=1, numVertices do
p = {
x = streamReadFloat32(streamId),
z = streamReadFloat32(streamId)
}
table.insert(vertices, p)
end
return vertices
end
26 changes: 3 additions & 23 deletions scripts/field/CustomFieldManager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ function CustomFieldManager:onClickSaveDialog(clickOk, field)
field:getName())
fieldValid = true
table.insert(self.fields, field)
self:refresh()
self.fileSystem:refresh()
end
end
if not fieldValid then
Expand All @@ -141,7 +141,7 @@ function CustomFieldManager:onClickDeleteDialog(clickOk, fieldToDelete)
file:delete()
field:delete()
table.remove(self.fields, i)
self:refresh()
self.fileSystem:refresh()
else
CpUtil.debugFormat(CpDebug.DBG_COURSES, 'Custom field %s was found, but the file not.', fieldToDelete:getName())
end
Expand All @@ -162,7 +162,7 @@ function CustomFieldManager:onClickRenameDialog(newName,clickOk,fieldToRename)
CpUtil.debugFormat(CpDebug.DBG_COURSES, 'Renamed custom field from %s to %s.', fieldToRename:getName(),newName)
if file:rename(newName) then
fieldToRename:setName(newName)
self:refresh()
self.fileSystem:refresh()
return
end
end
Expand Down Expand Up @@ -226,26 +226,6 @@ function CustomFieldManager:refresh()
CpUtil.debugFormat(CpDebug.DBG_COURSES,"Added new hotspot %s from filesystem.", entry:getName())
table.insert(self.fields, CustomField.createFromXmlFile(entry))
end
if g_server == nil then
self:sendToServer()
end
end

--- Sends the custom field data to the server.
function CustomFieldManager:sendToServer()
SendCustomFieldsToServerEvent.sendEvent(self.fields)
end

--- Removes the custom field data, when the user disconnects.
function CustomFieldManager:onUserRemoved(user)
if user then
self.clientFields[user:getUniqueUserId()] = nil
end
end

--- Custom fields received by a user.
function CustomFieldManager:setFromClient(uniqueUserId, fields)
self.clientFields[uniqueUserId] = fields
end

-- for reload only:
Expand Down
23 changes: 15 additions & 8 deletions scripts/specializations/CpAIBaleFinder.lua
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,11 @@ function CpAIBaleFinder:startCpAtFirstWp(superFunc)
local spec = self.spec_cpAIBaleFinder
spec.cpJob:applyCurrentState(self, g_currentMission, g_currentMission.player.farmId, true)
spec.cpJob:setValues()
g_client:getServerConnection():sendEvent(AIJobStartRequestEvent.new(spec.cpJob, self:getOwnerFarmId()))
return true
local success = spec.cpJob:validate(false)
if success then
g_client:getServerConnection():sendEvent(AIJobStartRequestEvent.new(spec.cpJob, self:getOwnerFarmId()))
return true
end
end
else
return true
Expand All @@ -104,27 +107,30 @@ function CpAIBaleFinder:startCpAtLastWp(superFunc)
local spec = self.spec_cpAIBaleFinder
spec.cpJob:applyCurrentState(self, g_currentMission, g_currentMission.player.farmId, true)
spec.cpJob:setValues()
g_client:getServerConnection():sendEvent(AIJobStartRequestEvent.new(spec.cpJob, self:getOwnerFarmId()))
return true
local success = spec.cpJob:validate(false)
if success then
g_client:getServerConnection():sendEvent(AIJobStartRequestEvent.new(spec.cpJob, self:getOwnerFarmId()))
return true
end
end
else
return true
end
end

--- Custom version of AIFieldWorker:startFieldWorker()
function CpAIBaleFinder:startCpBaleFinder(jobParameters)
function CpAIBaleFinder:startCpBaleFinder(fieldPolygon)
--- Calls the giants startFieldWorker function.
self:startFieldWorker()
if self.isServer then
--- Replaces drive strategies.
CpAIBaleFinder.replaceDriveStrategies(self, jobParameters)
CpAIBaleFinder.replaceDriveStrategies(self, fieldPolygon)
end
end

-- We replace the Giants AIDriveStrategyStraight with our AIDriveStrategyFieldWorkCourse to take care of
-- field work.
function CpAIBaleFinder:replaceDriveStrategies(jobParameters)
function CpAIBaleFinder:replaceDriveStrategies(fieldPolygon)
CpUtil.infoVehicle(self, 'This is a CP field work job, start the CP AI driver, setting up drive strategies...')
local spec = self.spec_aiFieldWorker
if spec.driveStrategies ~= nil then
Expand All @@ -137,7 +143,8 @@ function CpAIBaleFinder:replaceDriveStrategies(jobParameters)
end
CpUtil.infoVehicle(self, 'Bale collect/wrap job, install CP drive strategy for it')
local cpDriveStrategy = AIDriveStrategyFindBales.new()
cpDriveStrategy:setAIVehicle(self, jobParameters)
cpDriveStrategy:setFieldPolygon(fieldPolygon)
cpDriveStrategy:setAIVehicle(self)
self.spec_cpAIFieldWorker.driveStrategy = cpDriveStrategy
--- TODO: Correctly implement this strategy.
local driveStrategyCollision = AIDriveStrategyCollision.new(cpDriveStrategy)
Expand Down
14 changes: 10 additions & 4 deletions scripts/specializations/CpAIFieldWorker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,11 @@ function CpAIFieldWorker:startCpAtFirstWp()
--- Applies the lane offset set in the hud, so ad can start with the correct lane offset.
spec.cpJobStartAtFirstWp:getCpJobParameters().laneOffset:setValue(spec.cpJob:getCpJobParameters().laneOffset:getValue())
spec.cpJobStartAtFirstWp:setValues()
g_client:getServerConnection():sendEvent(AIJobStartRequestEvent.new(spec.cpJobStartAtFirstWp, self:getOwnerFarmId()))
return true
local success = spec.cpJob:validate(false)
if success then
g_client:getServerConnection():sendEvent(AIJobStartRequestEvent.new(spec.cpJobStartAtFirstWp, self:getOwnerFarmId()))
return true
end
end
end

Expand All @@ -216,8 +219,11 @@ function CpAIFieldWorker:startCpAtLastWp()
if self:hasCpCourse() and self:getCanStartCpFieldWork() then
spec.cpJobStartAtLastWp:applyCurrentState(self, g_currentMission, g_currentMission.player.farmId, true)
spec.cpJobStartAtLastWp:setValues()
g_client:getServerConnection():sendEvent(AIJobStartRequestEvent.new(spec.cpJobStartAtLastWp, self:getOwnerFarmId()))
return true
local success = spec.cpJob:validate(false)
if success then
g_client:getServerConnection():sendEvent(AIJobStartRequestEvent.new(spec.cpJobStartAtLastWp, self:getOwnerFarmId()))
return true
end
end
end

Expand Down
10 changes: 6 additions & 4 deletions scripts/specializations/CpAIWorker.lua
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,15 @@ function CpAIWorker:startStopDriver()

job:applyCurrentState(self, g_currentMission, g_currentMission.player.farmId, true)
job:setValues()
-- local success = spec.cpJob:validate(false)
-- if success then
if true then
local success, message = job:validate(false)
if success then
g_client:getServerConnection():sendEvent(AIJobStartRequestEvent.new(job, self:getOwnerFarmId()))
CpUtil.infoVehicle(self, "Cp helper started.")
else
CpUtil.infoVehicle(self, "Job parameters not valid.")
CpUtil.infoVehicle(self, "Could not start CP helper: %s", tostring(message))
if message then
g_currentMission:showBlinkingWarning("CP: "..message, 5000)
end
end
else
CpUtil.infoVehicle(self, "Could not start CP helper, it needs a course when not collecting bales.")
Expand Down

0 comments on commit aa9a873

Please sign in to comment.