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

A* custom heuristic function #60

Open
MichaelMackus opened this issue May 5, 2020 · 8 comments
Open

A* custom heuristic function #60

MichaelMackus opened this issue May 5, 2020 · 8 comments
Assignees

Comments

@MichaelMackus
Copy link
Contributor

It would be helpful to add a parameter to the pathfinding for a custom heuristic function. I'm not too familiar with the internals of the TCOD pathfinding, but looking into it it looks like you're just using straight euclidian distance for the heuristic. Perhaps adding a function argument could help simplify this, I was thinking something like:

TCOD_path_t TCOD_path_new_using_map(TCOD_map_t map, float diagonalCost, TCOD_path_func_t heuristicFunc)

This could also further simplify things by eliminating the need to maintain a separate set of functions for the Dijkstra algorithm (could just use the NULL case for heuristicFunc here). Just an idea, I've implemented something similar and it seems to work pretty well.

@HexDecimal HexDecimal self-assigned this May 5, 2020
@HexDecimal
Copy link
Collaborator

HexDecimal commented May 5, 2020

I've been in the middle of a rewrite of the pathfinder for a long time now. I haven't been happy with what I've done so far, and my plans have been ambitious.

I want to remove the 2D limit, allow a custom heuristic function, and a custom graph function.

The C++ templates in the src/libtcod/pathfinding directory do what I want, but I need to somehow port these to C99.

@HexDecimal
Copy link
Collaborator

Thinking back on this I should have just made the C++ templates more accessible and not worried about what was available in C.

It was really easy to set up a new pathfinder in C++. The hard part was that I need to figure out how to handle the inputs and outputs of the pathfinding computation data, since there's no standard multi dimensional array type in C++.

@MichaelMackus
Copy link
Contributor Author

MichaelMackus commented Aug 12, 2021 via email

@MichaelMackus
Copy link
Contributor Author

MichaelMackus commented Aug 12, 2021 via email

@HexDecimal
Copy link
Collaborator

The main dependency I used was a priority queue I believe.

This is the main part. It's trivial to implement a pathfinder after you have access to a good priority queue. I have one in libtcod but the interface is too weird and I don't want to lock it in by making it public.

I know in my haskell roguelike I pass in a function to the A* algorithm which returns all neighboring points for a given point.

I'll need to do this for C sometime. I've previously done it with C++ templates but it might be easy enough to make a simple C callback work in a similar way. I'm still missing a good C container for the array of distances/path-costs.

@HexDecimal
Copy link
Collaborator

Simple things like "what is the size of an index" are not so easy to resolve in C. This is one of the things which made the internal heap queue turn out weird.

@MichaelMackus
Copy link
Contributor Author

MichaelMackus commented Aug 17, 2021 via email

@HexDecimal
Copy link
Collaborator

HexDecimal commented Aug 17, 2021

Thanks for sharing your own examples.

A malloc on each queue push isn't good for performance. It harms threading and memory locality.

It's tricky to figure out what I'd even want to do. I really want something that's flexible.

Maybe if I redid the C pathfinder I could make one that takes a C callback to define the graph. I don't think I'll do this until I've implemented something similar in C++, or maybe I can try to backport the complicated Python-tcod pathfinder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants