Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More flexible movement options #17

Closed
exectails opened this issue Jul 17, 2018 · 4 comments
Closed

More flexible movement options #17

exectails opened this issue Jul 17, 2018 · 4 comments

Comments

@exectails
Copy link

exectails commented Jul 17, 2018

I've been using this library for a small prototype I've been working on and it's working very well, kudos.

I've been thinking about the movement patterns though, and while it's nice to be able to specify that something can't move down for example, I found that what I need is to be able to tell the path finder not how it can move generally, but on a tile to tile basis. For example, I might have a tile that you can only walk over from north to south.

unbenannt-1

It seems like that's not currently possible. A way to do it might be to let Grid/PathFinder take a function that can be called to get the available movement options instead of an array of offsets. I feel like that would make the system a whole lot more flexible, as you get full control over the options.

grid.GetPath(tile1, tile2, GetMovementOptions);

private IEnumerable<Offset> GetMovementOptions(Position position, int dimX, int dimY)
{
	return MovementPatterns.LateralOnly.Where(
		m =>
		{
			var target = position + m;
			if (!(target.X >= 0 && target.X < dimX && target.Y >= 0 && target.Y < dimY))
				return false;

			if ((this.GetTile(position).NorthToSouth() || this.GetTile(target).NorthToSouth()) && m != new Offset(0, 1))
				return false;

			return true;
		}
	);
}

This would be a nice feature to be have in my opinion.

@exectails exectails changed the title More flexble movement options More flexible movement options Jul 24, 2018
@roy-t
Copy link
Owner

roy-t commented Aug 1, 2018

Hey exectails. I've given this some thought, and I think it will be extremely hard to implement without sacrificing a lot of performance. It would add a call to a delegate for every tile inspect. Which is very different from the list iteration we have now.

In the case where not all tiles are equal you could start looking at path finding algorithms for Directed Acyclic Graphs. (Which is also possible with A*). It might be nice to add a DAG path finding part to this library and a way to convert from DAGs to grids (with some meta info) and back.

@roy-t
Copy link
Owner

roy-t commented Aug 1, 2018

I've opened #18 to explorer this further. No ETA though :)

@exectails
Copy link
Author

I think it will be extremely hard to implement without sacrificing a lot of performance. It would add a call to a delegate for every tile inspect.

That would certainly not be desirable, though I wonder how much performance you would lose, have you done any benchmarks by any chance? My idea was that you wouldn't actually lose a lot/any performance because you replace the call to the current GetMovementOptions with the call to the delegate. Do you think the use of a static method gives a measurable advantage over a delegate?

@roy-t
Copy link
Owner

roy-t commented Dec 27, 2018

Sorry for the late reply. I've been pondering about this. But yes a call to a delegate is definitely slower because the compiler cannot think about what will happen until it runs the code. So optimizations like inlining are out of the question. Its also two jumps instead of one (from method to method vs, from method to delegate logic, to method in delegate).

@roy-t roy-t closed this as completed Dec 27, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants