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

Commit

Permalink
6.03.00045 Pathfinder fix for merged fields
Browse files Browse the repository at this point in the history
- check for field ownership, not the exact field number
when applying off-field penalty. This should be good
enough to keep the path off other's fields.

Fixes #6874 #6793
  • Loading branch information
pvaiko committed Feb 24, 2021
1 parent 6968a7c commit 8ecdec8
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 63 deletions.
62 changes: 1 addition & 61 deletions DevHelper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ function DevHelper:update()
self.data.isField, self.fieldArea, self.totalFieldArea = courseplay:isField(self.data.x, self.data.z, 3, 3)

self.data.landId = PathfinderUtil.getFieldIdAtWorldPosition(self.data.x, self.data.z)
self.data.owned = PathfinderUtil.isWorldPositionOwned(self.data.x, self.data.z)
self.data.fieldAreaPercent = 100 * self.fieldArea / self.totalFieldArea

self.data.collidingShapes = ''
Expand Down Expand Up @@ -149,8 +150,6 @@ end
-- Left-Ctrl + , (<) = mark current field as field for pathfinding
-- Left-Alt + Space = save current vehicle position
-- Left-Ctrl + Space = restore current vehicle position
-- Left-Alt + / = look for bales
-- Left-Ctrl + / = find path to next bale
function DevHelper:keyEvent(unicode, sym, modifier, isDown)
if not CpManager.isDeveloper then return end
if bitAND(modifier, Input.MOD_LALT) ~= 0 and isDown and sym == Input.KEY_comma then
Expand Down Expand Up @@ -186,10 +185,6 @@ function DevHelper:keyEvent(unicode, sym, modifier, isDown)
elseif bitAND(modifier, Input.MOD_LCTRL) ~= 0 and isDown and sym == Input.KEY_space then
-- restore vehicle position
DevHelper.restoreVehiclePosition(g_currentMission.controlledVehicle)
elseif bitAND(modifier, Input.MOD_LALT) ~= 0 and isDown and sym == Input.KEY_slash then
self:initBales()
elseif bitAND(modifier, Input.MOD_LCTRL) ~= 0 and isDown and sym == Input.KEY_slash then
self:findPathToNextBale()
end
end

Expand Down Expand Up @@ -284,61 +279,6 @@ function DevHelper:showFillNodes()
end
end

function DevHelper:findBales(fieldId)
self:debug('Finding bales on field %d...', fieldId or 0)
local bales = {}
for _, object in pairs(g_currentMission.nodeToObject) do
if object:isa(Bale) then
local bale = BaleToCollect(object)
if not fieldId or fieldId == 0 or bale:getFieldId() == fieldId then
bales[object.id] = bale
end
end
end
return bales
end

function DevHelper:initBales()
local allBales = self:findBales()
local closestBale, d = self:findClosestBale(allBales, self.node)
self:debug('Closest bale is at %.1f m, on field %d', d, closestBale:getFieldId())
self.bales = self:findBales(closestBale:getFieldId())
end

---@return BaleToCollect, number closest bale and its distance
function DevHelper:findClosestBale(bales, node)
local closestBale, minDistance = nil, math.huge
for _, bale in pairs(bales) do
local _, _, _, d = bale:getPositionFromNodeInfo(node)
if d < minDistance then
closestBale = bale
minDistance = d
end
end
return closestBale, minDistance
end

function DevHelper:findPathToNextBale()
if not self.bales then return end
local baleId
for id, bale in pairs(self.bales) do
-- first figure out the direction at the goal, as the pathfinder needs that.
-- for now, just use the direction from our location towards the bale
local xb, zb, yRot, d = bale:getPositionFromNodeInfo(self.node)

self.start = State3D(self.data.x, -self.data.z, courseGenerator.fromCpAngleDeg(self.data.yRotDeg))
self.goal = State3D(xb, -zb, courseGenerator.fromCpAngle(yRot))
local offset = Vector(0, 3.5)
self.goal:add(offset:rotate(self.goal.t))

self:startPathfinding()
baleId = id
break
end
-- remove bale from list
if baleId then self.bales[baleId] = nil end
end

function DevHelper:showVehicleSize()
local vehicle = g_currentMission.controlledVehicle
if not vehicle then return end
Expand Down
8 changes: 7 additions & 1 deletion course-generator/PathfinderUtil.lua
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,12 @@ function PathfinderUtil.getFieldIdAtWorldPosition(posX, posZ)
return 0
end

--- Is the land at this position owned by me?
function PathfinderUtil.isWorldPositionOwned(posX, posZ)
local farmland = g_farmlandManager:getFarmlandAtWorldPosition(posX, posZ)
return farmland and farmland.isOwned
end

--- Pathfinder context
---@class PathfinderUtil.Context
PathfinderUtil.Context = CpObject()
Expand Down Expand Up @@ -485,7 +491,7 @@ function PathfinderConstraints:getNodePenalty(node)
local offField = area / totalArea < minRequiredAreaRatio
if self.fieldNum ~= 0 and not offField then
-- if there's a preferred field and we are on a field
if self.fieldNum ~= PathfinderUtil.getFieldIdAtWorldPosition(node.x, -node.y) then
if not PathfinderUtil.isWorldPositionOwned(node.x, -node.y) then
-- the field we are on is not ours, more penalty!
offField = true
offFieldPenalty = self.offFieldPenalty * 1.2
Expand Down
2 changes: 1 addition & 1 deletion modDesc.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8" standalone="no" ?>
<modDesc descVersion="47">
<version>6.03.00044</version>
<version>6.03.00045</version>
<author><![CDATA[Courseplay.devTeam]]></author>
<title><!-- en=English de=German fr=French es=Spanish ru=Russian pl=Polish it=Italian br=Brazilian-Portuguese cs=Chinese(Simplified) ct=Chinese(Traditional) cz=Czech nl=Netherlands hu=Hungary jp=Japanese kr=Korean pt=Portuguese ro=Romanian tr=Turkish -->
<en>CoursePlay SIX</en>
Expand Down

0 comments on commit 8ecdec8

Please sign in to comment.