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 #7553 from Courseplay/WorkWidthChanges
Browse files Browse the repository at this point in the history
A few code improvements
  • Loading branch information
schwiti6190 authored Oct 23, 2021
2 parents 4e86337 + 7ae1ce8 commit 9ef9477
Show file tree
Hide file tree
Showing 43 changed files with 1,029 additions and 725 deletions.
8 changes: 5 additions & 3 deletions AITurn.lua
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,8 @@ end
---@return boolean, number True if there's enough space to make a forward turn on the field. Also return the
---distance to reverse in order to be able to just make the turn on the field
function AITurn.canTurnOnField(turnContext, vehicle)
local spaceNeededOnFieldForTurn = AIDriverUtil.getTurningRadius(vehicle) + vehicle.cp.workWidth / 2
local workWidth = vehicle.cp.courseGeneratorSettings.workWidth:get()
local spaceNeededOnFieldForTurn = AIDriverUtil.getTurningRadius(vehicle) + workWidth / 2
local distanceToFieldEdge = turnContext:getDistanceToFieldEdge(turnContext.vehicleAtTurnStartNode)
courseplay.debugVehicle(AITurn.debugChannel, vehicle, 'Space needed to turn on field %.1f m', spaceNeededOnFieldForTurn)
if distanceToFieldEdge then
Expand Down Expand Up @@ -635,9 +636,10 @@ function CombinePocketHeadlandTurn:generatePocketHeadlandTurn(turnContext)
local turnDiameter = self.vehicle.cp.settings.turnDiameter:get()
local turnRadius = turnDiameter / 2
-- this is how far we have to cut into the next headland (the position where the header will be after the turn)
local offset = math.min(turnRadius + turnContext.frontMarkerDistance, self.vehicle.cp.workWidth)
local workWidth = vehicle.cp.courseGeneratorSettings.workWidth:get()
local offset = math.min(turnRadius + turnContext.frontMarkerDistance, workWidth)
local corner = turnContext:createCorner(self.vehicle, turnRadius)
local d = -self.vehicle.cp.workWidth / 2 + turnContext.frontMarkerDistance
local d = -workWidth / 2 + turnContext.frontMarkerDistance
local wp = corner:getPointAtDistanceFromCornerStart(d + 2)
wp.speed = self.vehicle.cp.speeds.turn * 0.75
table.insert(cornerWaypoints, wp)
Expand Down
3 changes: 2 additions & 1 deletion BalerAIDriver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ function BalerAIDriver:startTurn(ix)
if self.isCombine then
self:debug('This vehicle is also a harvester, check check for special headland turns.')
self:setMarkers()
self.turnContext = TurnContext(self.course, ix, self.aiDriverData, self.vehicle.cp.workWidth,

self.turnContext = TurnContext(self.course, ix, self.aiDriverData, self:getWorkWidth(),
self.frontMarkerDistance, self.backMarkerDistance,
self:getTurnEndSideOffset(), self:getTurnEndForwardOffset())

Expand Down
2 changes: 1 addition & 1 deletion BunkerSiloAIDriver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ function BunkerSiloAIDriver:isHeapSearchAllowed()
end

function BunkerSiloAIDriver:getWorkWidth()
return self.vehicle.cp.workWidth
return self.courseGeneratorSettings.workWidth:get()
end

--- If true then the drive into silo course is reverse and
Expand Down
19 changes: 8 additions & 11 deletions CombineAIDriver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function CombineAIDriver:init(vehicle)
self:checkMarkers()

-- distance to keep to the right (>0) or left (<0) when pulling back to make room for the tractor
self.pullBackRightSideOffset = math.abs(self.pipeOffsetX) - self.vehicle.cp.workWidth / 2 + 5
self.pullBackRightSideOffset = math.abs(self.pipeOffsetX) - self:getWorkWidth() / 2 + 5
self.pullBackRightSideOffset = self.pipeOnLeftSide and self.pullBackRightSideOffset or -self.pullBackRightSideOffset
-- should be at pullBackRightSideOffset to the right or left at pullBackDistanceStart
self.pullBackDistanceStart = self.settings.turnDiameter:get() --* 0.7
Expand Down Expand Up @@ -200,7 +200,7 @@ function CombineAIDriver:start(startingPoint)
-- we work with the traffic conflict detector and the proximity sensors instead
self:disableCollisionDetection()
self:fixMaxRotationLimit()
local total, pipeInFruit = self.fieldworkCourse:setPipeInFruitMap(self.pipeOffsetX, self.vehicle.cp.workWidth)
local total, pipeInFruit = self.fieldworkCourse:setPipeInFruitMap(self.pipeOffsetX, self:getWorkWidth())
local ix = self.fieldworkCourse:getStartingWaypointIx(AIDriverUtil.getDirectionNode(self.vehicle), startingPoint)
self:shouldStrawSwathBeOn(ix)
self.fillLevelFullPercentage = self.normalFillLevelFullPercentage
Expand Down Expand Up @@ -580,9 +580,10 @@ function CombineAIDriver:checkFruit()
else
self.fruitLeft, self.fruitRight = AIVehicleUtil.getValidityOfTurnDirections(self.vehicle)
end
local x, _, z = localToWorld(self:getDirectionNode(), self.vehicle.cp.workWidth, 0, 0)
local workWidth = self:getWorkWidth()
local x, _, z = localToWorld(self:getDirectionNode(), workWidth, 0, 0)
self.fieldOnLeft = courseplay:isField(x, z, 1, 1)
x, _, z = localToWorld(self:getDirectionNode(), -self.vehicle.cp.workWidth, 0, 0)
x, _, z = localToWorld(self:getDirectionNode(), -workWidth, 0, 0)
self.fieldOnRight = courseplay:isField(x, z, 1, 1)
self:debug('Fruit left: %.2f right %.2f, field on left %s, right %s',
self.fruitLeft, self.fruitRight, tostring(self.fieldOnLeft), tostring(self.fieldOnRight))
Expand Down Expand Up @@ -904,7 +905,7 @@ end
--- from node.
function CombineAIDriver:getAreaToAvoid()
if self:isWaitingForUnloadAfterPulledBack() then
local xOffset = self.vehicle.cp.workWidth / 2
local xOffset = self:getWorkWidth() / 2
local zOffset = 0
local length = self.pullBackDistanceEnd
local width = self.pullBackRightSideOffset
Expand Down Expand Up @@ -1028,7 +1029,7 @@ function CombineAIDriver:startTurn(ix)
self:debug('Starting a combine turn.')

self:setMarkers()
self.turnContext = TurnContext(self.course, ix, self.aiDriverData, self.vehicle.cp.workWidth,
self.turnContext = TurnContext(self.course, ix, self.aiDriverData, self:getWorkWidth(),
self.frontMarkerDistance, self.backMarkerDistance,
self:getTurnEndSideOffset(), self:getTurnEndForwardOffset())

Expand Down Expand Up @@ -1085,10 +1086,6 @@ function CombineAIDriver:getFieldworkCourse()
return self.fieldworkCourse
end

function CombineAIDriver:getWorkWidth()
return self.vehicle.cp.workWidth
end

function CombineAIDriver:isChopper()
return self.combine:getFillUnitCapacity(self.combine.fillUnitIndex) > 10000000
end
Expand Down Expand Up @@ -1759,7 +1756,7 @@ end
function CombineAIDriver:addForwardProximitySensor()
self:setFrontMarkerNode(self.vehicle)
self.forwardLookingProximitySensorPack = WideForwardLookingProximitySensorPack(
self.vehicle, self.ppc, self:getFrontMarkerNode(self.vehicle), self.proximitySensorRange, 1, self.vehicle.cp.workWidth)
self.vehicle, self.ppc, self:getFrontMarkerNode(self.vehicle), self.proximitySensorRange, 1, self:getWorkWidth())
end

--- Check the vehicle in the proximity sensor's range. If it is player driven, don't slow them down when hitting this
Expand Down
2 changes: 1 addition & 1 deletion CombineUnloadAIDriver.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1909,7 +1909,7 @@ function CombineUnloadAIDriver:startChopperTurn(ix)
self:setNewOnFieldState(self.states.HANDLE_CHOPPER_HEADLAND_TURN)
else
self.turnContext = TurnContext(self.followCourse, ix, self.aiDriverData,
self.combineToUnload.cp.workWidth, self.frontMarkerDistance, self.backMarkerDistance, 0, 0)
self.combineToUnload.cp.driver:getWorkWidth(), self.frontMarkerDistance, self.backMarkerDistance, 0, 0)
local finishingRowCourse = self.turnContext:createFinishingRowCourse(self.vehicle)
self:startCourse(finishingRowCourse, 1)
self:setNewOnFieldState(self.states.HANDLE_CHOPPER_180_TURN)
Expand Down
4 changes: 2 additions & 2 deletions CombineUnloadManager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ end

function CombineUnloadManager:getPipeOffset(combine)
if self:getIsChopper(combine) then
return (combine.cp.workWidth / 2) + 3
return (combine.cp.driver:getWorkWidth() / 2) + 3
elseif self:getIsCombine(combine) then
local pipeOffsetX, _ = combine.cp.driver:getPipeOffset()
return pipeOffsetX
Expand Down Expand Up @@ -531,7 +531,7 @@ function CombineUnloadManager:getOnFieldSituation(combine)
local rightDirX,_,rightDirZ = localDirectionToWorld(node, -1, 0, 0);
--set measurements of the box to check
local boxWidth = 3;
local boxLength = 6 + combine.cp.workWidth/2;
local boxLength = 6 + combine.cp.driver:getWorkWidth()/2;
--to get the box centered divide the measurements by 2
local boxWidthCenter = boxWidth/2

Expand Down
1 change: 1 addition & 0 deletions CpManager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ function CpManager:deleteMap()

-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- delete fields data and overlays
---@class courseplay.fields.fieldData
courseplay.fields.fieldData = {};
courseplay.fields.curFieldScanIndex = 0;
courseplay.fields.allFieldsScanned = false;
Expand Down
110 changes: 110 additions & 0 deletions Events/CustomFieldEvent.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
--- This event is used to synchronize custom field,
--- either from the server to a newly joined player
--- or after a custom field was created to all user and server.
---
CustomFieldEvent = {}
local CustomFieldEvent_mt = Class(CustomFieldEvent, Event)

InitEventClass(CustomFieldEvent, "CustomFieldEvent")

function CustomFieldEvent:emptyNew()
local self = Event:new(CustomFieldEvent_mt)
self.className = "CustomFieldEvent"
return self
end

--- Creates a new Event
---@param field table
function CustomFieldEvent:new(field)
self.field = field
self.debug("CustomFieldEvent:new()")
return self
end

--- Reads the serialized data on the receiving end of the event.
function CustomFieldEvent:readStream(streamId, connection) -- wird aufgerufen wenn mich ein Event erreicht
self.field = CustomFieldEvent.readField(streamId)
self.debug("CustomFieldEvent:readStream()")
self.debug("Field name: %s, numPoints = %s ",tostring(self.field.name), tostring(self.field.numPoints))

self:run(connection);
end

--- Writes the serialized data from the sender.
function CustomFieldEvent:writeStream(streamId, connection) -- Wird aufgrufen wenn ich ein event verschicke (merke: reihenfolge der Daten muss mit der bei readStream uebereinstimmen
self.debug("CustomFieldEvent:writeStream()")
self.debug("Field name: %s, numPoints = %s ",tostring(self.field.name), tostring(self.field.numPoints))
CustomFieldEvent.writeField(self.field,streamId)
end

--- Runs the event on the receiving end of the event.
function CustomFieldEvent:run(connection) -- wir fuehren das empfangene event aus
self.debug("CustomFieldEvent:run()")
CpFieldUtil.saveFieldFromNetwork(self.field)
--- If the receiver was the client make sure every clients gets also updated.
if not connection:getIsServer() then
self.debug("Send CustomFieldEvent to clients")
g_server:broadcastEvent(CustomFieldEvent:new(self.field), nil, connection)
end
end

--- Sends an event to sync a custom created field.
function CustomFieldEvent.sendEvent(field)
--- Only sync custom fields
if field.isCustom then
if g_server ~= nil then
CustomFieldEvent.debug("Send CustomFieldEvent to clients")
CustomFieldEvent.debug("Field name: %s",tostring(field.name))
g_server:broadcastEvent(CustomFieldEvent:new(field))
else
CustomFieldEvent.debug("Send CustomFieldEvent to server")
CustomFieldEvent.debug("Field name: %s",tostring(field.name))
g_client:getServerConnection():sendEvent(CustomFieldEvent:new(field))
end;
end
end

function CustomFieldEvent.debug(...)
courseplay.debugFormat(courseplay.DBG_MULTIPLAYER,...)
end

--- Writes a single custom field.
function CustomFieldEvent.writeField(field,streamId)
local numPoints = field.numPoints or #field.points
streamDebugWriteString(streamId, field.name)
streamDebugWriteInt32(streamId, numPoints)
streamDebugWriteInt32(streamId, field.fieldNum)
streamDebugWriteInt32(streamId, field.dimensions.minX)
streamDebugWriteInt32(streamId, field.dimensions.maxX)
streamDebugWriteInt32(streamId, field.dimensions.minZ)
streamDebugWriteInt32(streamId, field.dimensions.maxZ)
for p = 1, numPoints do
streamDebugWriteFloat32(streamId, field.points[p].cx)
streamDebugWriteFloat32(streamId, field.points[p].cy)
streamDebugWriteFloat32(streamId, field.points[p].cz)
end

end

--- Reads a single custom field.
function CustomFieldEvent.readField(streamId)
local field = {
dimensions = {},
points = {},
isCustom = true
}
field.name = streamDebugReadString(streamId)
field.numPoints = streamDebugReadInt32(streamId)
field.fieldNum = streamDebugReadInt32(streamId)
field.dimensions.minX = streamDebugReadInt32(streamId)
field.dimensions.maxX = streamDebugReadInt32(streamId)
field.dimensions.minZ = streamDebugReadInt32(streamId)
field.dimensions.maxZ = streamDebugReadInt32(streamId)
for p = 1, field.numPoints do
field.points[p] = {}
field.points[p].cx = streamDebugReadFloat32(streamId)
field.points[p].cy = streamDebugReadFloat32(streamId)
field.points[p].cz = streamDebugReadFloat32(streamId)
end
return field
end
Loading

0 comments on commit 9ef9477

Please sign in to comment.