Skip to content

MergeCorridorStartMoved does not resize the path #90

@TomKuhn

Description

@TomKuhn

I noticed that in the original c++, it extends or truncates the path, by returning the new size. The c# version does not do this, I have fixed as follows;

                //adjust beginning of buffer to include the visited
		int req = visited.Count - furthestVisited;
		int orig = Math.Min(furthestPath + 1, path.Count);
		int size = Math.Max(0, path.Count - orig);
		if (req + size > path.Count)
		{
			path.SetSize(req + size);

			// Copy in reverse order
			for (int i = size-1; i >= 0; i--)
				path[req + i] = path[orig + i];
		}
		else
		{
			for (int i = 0; i < size; i++)
				path[req + i] = path[orig + i];
		}

		//store visited
		for (int i = 0; i < req; i++)
			path[i] = visited[(visited.Count - 1) - i];

		path.SetSize(req + size);

		return req + size;

And added the SetSize method to Path:

public void SetSize(int size)
{
if (size > polys.Count)
{
polys.AddRange(new NavPolyId[size - polys.Count]);
}
else if (size < polys.Count)
{
polys.RemoveRange(size, polys.Count - size);
}
}

hope someone finds this useful!

When the path was not truncated, junk was left at the end of the array, causing the logic that slows the agent down at the end of the path didn't work and the agent continually overshot the destination. When the path was not extended, then it threw IndexOutOfRangeException trying to copy the buffer up. Note you have to copy in reverse when extending, so we don't copy over our own buffer (check memcpy from the original supports overlapping buffers)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions