Hello!
Thanks for all your comments.
@Tai
Ok, lets see what we have here.
There's only one syntax error in the code:
//LL_PFIND_PATH_NODE = (LL_PFIND_PATH_NODE *)path->first->data; // old
LL_PFIND_PATH_NODE *node= (LL_PFIND_PATH_NODE *)path->first->data; // new
This was not your fault, but a misstatement in the example from the documentation.
Sorry for that!
Your code compiles fine for me after correcting this (compiled with lite-C 1.7).
However, it will probably not quite do what you want it to do, as there are still a few semantic errors. Here we go:
1) ll_pfind_createPathNode() takes a VECTOR* as an argument, but node numbers are passed instead:
// insert the start position as first node
//node = ll_pfind_createPathNode(start_node); // old
node = ll_pfind_createPathNode(&my->x); // new
ll_list_push(path, (void *)node);
// insert the destination as last node
//node = ll_pfind_createPathNode(dest_node); // old
node = ll_pfind_createPathNode(&target_pos); // new
ll_list_add(path, (void *)node);
2) A pointer is passed instead of a VECTOR*:
//if (vec_dist(&my->x, &node) < 50) // old
if (vec_dist(&my->x, &node->pos) < 50) // new
3) The while loop misses a wait().
4) path should be set to NULL when the path has been traversed or else the condition 'if (path)' is still true, although the path was already freed:
else // path has been traversed completely
{
ll_list_destroy(path, NULL); // remove it
path = NULL; // new
}
This one wasn't that obvious and it isn't mentioned in the examples yet. I'll check if and how I can clarify this.
Last but not least also note that in each iteration of your while loop a path from the starting point to the destination point is created. In principle this isn't wrong, but it isn't necessary either and just produces overhead. If the destination point is immovable, it's entirely sufficient to calculate the path only once. Also mind, that all the created lists are never freed in your code (except for the case if a path is traversed completely), so this ultimately leads to a heap overflow.
You could shorten your while loop to the following code and do the rest outside of the loop (as an example):
while(1)
{
if (path) // path has been found
{
if (path->count > 0) // path is not empty yet
{
// get the next node of the path
node = (LL_PFIND_PATH_NODE *)path->first->data;
// move to the node
// [insert your movement code here]
// [the destination should be: node->pos]
// if the entity has reached the node
if (vec_dist(&my->x, &node->pos) < 50)
{
// remove it from the path
ll_list_remove(path, path->first, ll_pfind_destroyPathNode);
}
}
else // path has been traversed completely
{
ll_list_destroy(path, NULL); // remove it
path = NULL;
}
}
wait(1);
}
You might also have a look at "pathfinder.c" from the demo which uses the same approach.
Hope this helps! If not, you can tell me of course. : )
If you consider the documentation or the examples unclear, please tell me too, so I can improve them.