Pathfinder `new2DGrid` initializer creates wrong diagonal connections

[EDIT: This originally-documented issue is not actually an issue, just a mistake on my part! Please scroll down to see the updated, real issue.]

Hi all! Back again with a pathfinder report. (I hope I'm not the only one using this library :slight_smile: )

I've noticed that with the new2DGrid initializer (which is very convenient by the way), diagonal connections are not added automatically, even if the diagonal boolean is passed in as true.

The expected behavior is that if I pass in allowDiagonals as true, then connections between diagonal nodes would be made automatically.

A small example of this behavior:

--- Pathfinder graph in the form of:
--- [ 1  0 ]
--- [ 0  1 ]
local graph = playdate.pathfinder.graph.new2DGrid(2, 2, true, { 1, 0, 0, 1 })

for _, node in pairs(graph:allNodes()) do
  if #node:connectedNodes() > 1 then
    print("Node has connections: " .. node.x .. " " .. node.y)
  else
    print("Node has no connections: " .. node.x .. " " .. node.y)
  end
end

You'd expect from the above example to have nodes (1, 1) and (2, 2) to be connected to each other.

However, the output is the following:

Node has no connections: 1.0 1.0
Node has no connections: 2.0 1.0
Node has no connections: 1.0 2.0
Node has no connections: 2.0 2.0

Hopefully this helps and is actually a bug. I'll have to build a work-around in the meantime but I know that doing this in the C-library will be a lot more efficient :slight_smile:

Ok. Big stupid here.

If you correct if #node:connectedNodes() > 1 to if #node:connectedNodes() > 0, (checks for a non-zero number of connections) then the test runs fine.

Apologies!

[DEFINITELY A BUG]

Ok, update. So the previous thing was totally my fault. But there is definitely something wrong with the way diagonals are being made in the graph initializer.

Here's a reproducible example:

-- [ 0 0 0 0 ]
-- [ 0 1 0 0 ]
-- [ 1 0 1 0 ]
-- [ 0 0 0 1 ]

local graph = playdate.pathfinder.graph.new2DGrid(4, 4, true, { 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 1 })

for _, node in pairs(graph:allNodes()) do
  if #node:connectedNodes() > 0 then
    print("Connected node: " .. node.x .. " " .. node.y .. " # connections: " .. #node:connectedNodes())
  else
    print("Node has no connections: " .. node.x .. " " .. node.y)
  end
end

I would expect the following result:

  • 4 nodes have connections.
  • 2 nodes have 1 connection (the x=1 and x=4 nodes), and 2 nodes have 2 connections (the middle ones)

This is the result:

Node has no connections: 1.0 1.0
Node has no connections: 2.0 1.0
Connected node: 3.0 1.0 # connections: 1
Node has no connections: 4.0 1.0
Node has no connections: 1.0 2.0
Connected node: 2.0 2.0 # connections: 3
Node has no connections: 3.0 2.0
Connected node: 4.0 2.0 # connections: 1
Connected node: 1.0 3.0 # connections: 1
Node has no connections: 2.0 3.0
Connected node: 3.0 3.0 # connections: 3
Node has no connections: 4.0 3.0
Node has no connections: 1.0 4.0
Node has no connections: 2.0 4.0
Node has no connections: 3.0 4.0
Connected node: 4.0 4.0 # connections: 1

Observations:

  • Side nodes (1, 3) and (4, 4) have the correct amount of connections (1).
  • Middle nodes (2, 2) and (3, 3) have an extra connection (3 instead of 2)
  • Extra 2 nodes have 1 connection (3, 1) and (4, 2) instead of zero.

Funnily enough, thanks to my work previously, I can very easily see this in my game :slight_smile:

You can see there's an extra "top-right" point for EVERY node, and the only time where that doesn't take effect is when there is already a node in that position or if the node is on the top or right edge of the graph.

Thankfully, if I turn off allowDiagonals then I no longer have such an issue, and I've already built the code that runs diagonal connections for me thanks to the previous non-issue :stuck_out_tongue:

2 Likes

This one goes in the bug report Hall of Fame. You showed me exactly where to find the bug, all I had to do was follow your directions and there it was, just a simple typo causing it to always add the top-right connection. :person_bowing:

Thank you for doing like 98% of the debugging here!

2 Likes

I appreciate it! I was a QA tester in my past life and the devs did love me for my documentation efforts :wink:

1 Like