Skip to content

Commit

Permalink
More debug outputs
Browse files Browse the repository at this point in the history
and move field functions from PathfinderUtil to
CpFieldUtil
  • Loading branch information
pvaiko committed Jan 1, 2022
1 parent 189326c commit 1c6ec20
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 34 deletions.
4 changes: 2 additions & 2 deletions scripts/DevHelper.lua
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ function DevHelper:update()

-- self.data.hasFruit, self.data.fruitValue, self.data.fruit = PathfinderUtil.hasFruit(self.data.x, self.data.z, 5, 3.6)

--self.data.landId = PathfinderUtil.getFieldIdAtWorldPosition(self.data.x, self.data.z)
--self.data.landId = CpFieldUtil.getFieldIdAtWorldPosition(self.data.x, self.data.z)
--self.data.owned = PathfinderUtil.isWorldPositionOwned(self.data.x, self.data.z)
self.data.farmlandId = g_farmlandManager:getFarmlandIdAtWorldPosition(self.data.x, self.data.z)
self.data.farmland = g_farmlandManager:getFarmlandAtWorldPosition(self.data.x, self.data.z)
Expand Down Expand Up @@ -146,7 +146,7 @@ function DevHelper:keyEvent(unicode, sym, modifier, isDown)
self:debug('Calculate')
self:startPathfinding()
elseif bitAND(modifier, Input.MOD_LCTRL) ~= 0 and isDown and sym == Input.KEY_comma then
self.fieldNumForPathfinding = PathfinderUtil.getFieldNumUnderNode(self.node)
self.fieldNumForPathfinding = CpFieldUtil.getFieldNumUnderNode(self.node)
self:debug('Set field %d for pathfinding', self.fieldNumForPathfinding)
elseif bitAND(modifier, Input.MOD_LALT) ~= 0 and isDown and sym == Input.KEY_space then
-- save vehicle position
Expand Down
6 changes: 3 additions & 3 deletions scripts/ai/AIDriveStrategyCombineCourse.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1384,8 +1384,8 @@ function AIDriveStrategyCombineCourse:findBestTrailer()
if SpecializationUtil.hasSpecialization(Attachable, vehicle.specializations) then
attacherVehicle = vehicle.spec_attachable:getAttacherVehicle()
end
local fieldNum = PathfinderUtil.getFieldNumUnderVehicle(vehicle)
local myFieldNum = PathfinderUtil.getFieldNumUnderVehicle(self.vehicle)
local fieldNum = CpFieldUtil.getFieldNumUnderVehicle(vehicle)
local myFieldNum = CpFieldUtil.getFieldNumUnderVehicle(self.vehicle)
local x, _, z = getWorldTranslation(vehicle.rootNode)
local closestDistance = self:getClosestDistanceToFieldEdge(x, z)
local lastSpeed = rootVehicle:getLastSpeed()
Expand Down Expand Up @@ -1484,7 +1484,7 @@ function AIDriveStrategyCombineCourse:startSelfUnload()
self.selfUnloadAlignCourse = Course.createFromNode(self.vehicle, targetNode,
offsetX, offsetZ + 1, offsetZ + 1 + alignLength, 1, false)
self.selfUnloadAlignCourse:print()
local fieldNum = PathfinderUtil.getFieldNumUnderVehicle(self.vehicle)
local fieldNum = CpFieldUtil.getFieldNumUnderVehicle(self.vehicle)
local done, path
-- require full accuracy from pathfinder as we must exactly line up with the trailer
self.pathfinder, done, path = PathfinderUtil.startPathfindingFromVehicleToNode(
Expand Down
11 changes: 11 additions & 0 deletions scripts/ai/AIDriveStrategyFieldWorkCourse.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ function AIDriveStrategyFieldWorkCourse:delete()
end

function AIDriveStrategyFieldWorkCourse:start(course, startIx)
self:showAllInfo('Starting field work')
if self.frontMarkerDistance < 0 then
self:debug('extend course by %.1f m to make sure we do not miss anything when the course ends',
-self.frontMarkerDistance)
Expand Down Expand Up @@ -626,3 +627,13 @@ function AIDriveStrategyFieldWorkCourse:handleRidgeMarkers(isAllowed)
end

end

function AIDriveStrategyFieldWorkCourse:showAllInfo(note)
self:debug('%s: work width %.1f, turning radius %.1f, front marker %.1f, back marker %.1f',
note, self.workWidth, self.turningRadius, self.frontMarkerDistance, self.backMarkerDistance)
self:debug(' - map: %s, field %s', g_currentMission.missionInfo.mapTitle,
CpFieldUtil.getFieldNumUnderVehicle(self.vehicle))
for _, implement in pairs(self.vehicle:getAttachedImplements()) do
self:debug(' - %s', CpUtil.getName(implement.object))
end
end
3 changes: 2 additions & 1 deletion scripts/ai/jobs/AIJobFieldWorkCp.lua
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ function AIJobFieldWorkCp:validate(farmId)
self.lastPositionX, self.lastPositionZ = tx, tz
self.hasValidPosition = true
end

local fieldNum = CpFieldUtil.getFieldIdAtWorldPosition(tx, tz)
CpUtil.info('Scanning field %d on %s', fieldNum, g_currentMission.missionInfo.mapTitle)
self.fieldPolygon = g_fieldScanner:findContour(tx, tz)
if not self.fieldPolygon then
self.hasValidPosition = false
Expand Down
13 changes: 13 additions & 0 deletions scripts/field/CpFieldUtil.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,19 @@ function CpFieldUtil.isOnField(x, z)
return isOnField
end

--- Which field this node is on.
---@param node table Giants engine node
---@return number 0 if not on any field, otherwise the number of field, see note on getFieldItAtWorldPosition()
function CpFieldUtil.getFieldNumUnderNode(node)
local x, _, z = getWorldTranslation(node)
return CpFieldUtil.getFieldIdAtWorldPosition(x, z)
end

--- Which field this node is on. See above for more info
function CpFieldUtil.getFieldNumUnderVehicle(vehicle)
return CpFieldUtil.getFieldNumUnderNode(vehicle.rootNode)
end

--- Returns the field ID (actually, land ID) for a position. The land is what you can buy in the game,
--- including the area around an actual field.
function CpFieldUtil.getFieldIdAtWorldPosition(posX, posZ)
Expand Down
30 changes: 2 additions & 28 deletions scripts/pathfinder/PathfinderUtil.lua
Original file line number Diff line number Diff line change
Expand Up @@ -160,32 +160,6 @@ function PathfinderUtil.isNodeOnField(node)
return PathfinderUtil.isPosOnField(x, y, z)
end

--- Which field this node is on.
---@param node table Giants engine node
---@return number 0 if not on any field, otherwise the number of field, see note on getFieldItAtWorldPosition()
function PathfinderUtil.getFieldNumUnderNode(node)
local x, _, z = getWorldTranslation(node)
return PathfinderUtil.getFieldIdAtWorldPosition(x, z)
end

--- Which field this node is on. See above for more info
function PathfinderUtil.getFieldNumUnderVehicle(vehicle)
return PathfinderUtil.getFieldNumUnderNode(vehicle.rootNode)
end

--- Returns the field ID (actually, land ID) for a position. The land is what you can buy in the game,
--- including the area around an actual field.
function PathfinderUtil.getFieldIdAtWorldPosition(posX, posZ)
local farmland = g_farmlandManager:getFarmlandAtWorldPosition(posX, posZ)
if farmland ~= nil then
local fieldMapping = g_fieldManager.farmlandIdFieldMapping[farmland.id]
if fieldMapping ~= nil and fieldMapping[1] ~= nil then
return fieldMapping[1].fieldId
end
end
return 0
end

--- Is the land at this position owned by me?
function PathfinderUtil.isWorldPositionOwned(posX, posZ)
local farmland = g_farmlandManager:getFarmlandAtWorldPosition(posX, posZ)
Expand Down Expand Up @@ -462,7 +436,7 @@ function PathfinderConstraints:getNodePenalty(node)
self.offFieldPenaltyNodeCount = self.offFieldPenaltyNodeCount + 1
node.offField = true
end
--local fieldId = PathfinderUtil.getFieldIdAtWorldPosition(node.x, -node.y)
--local fieldId = CpFieldUtil.getFieldIdAtWorldPosition(node.x, -node.y)
if not offField then
local hasFruit, fruitValue = PathfinderUtil.hasFruit(node.x, -node.y, 4, 4)
if hasFruit and fruitValue > self.maxFruitPercent then
Expand Down Expand Up @@ -694,7 +668,7 @@ function PathfinderUtil.findPathForTurn(vehicle, startOffset, goalReferenceNode,
pathfinder = HybridAStarWithAStarInTheMiddle(turnRadius * 3, 200, 10000)
end

local fieldNum = PathfinderUtil.getFieldNumUnderVehicle(vehicle)
local fieldNum = CpFieldUtil.getFieldNumUnderVehicle(vehicle)
local context = PathfinderUtil.Context(
vehicle,
vehiclesToIgnore)
Expand Down

0 comments on commit 1c6ec20

Please sign in to comment.