Dijkstra and Wed paths

Posted By: MadJack

Dijkstra and Wed paths - 06/19/08 08:27

As I need it for my project, I am developping a pathfinder solution using the paths created and edited in WED.
This pathfinder will use DIJKSTRA algo.
The first step is to inialize a matrix with the nodes links and the distances.
Here is an example of code to do that :
Code:
/***************************************************************************************

Basic Lite-C script for Pathfinding

Will use DIJKSTRA Algo
Nodes and edges are edited in WED using Path Objects (Add->Path and Vertex Move mode)

Version 0.01
****************************************************************************************/

#include <acknex.h>
#include <default.c>


int mat[1][1];		// Matrix where mat[i][j]>0 if there is a link between i and j
						// mat[i][j] = Distance between node number i and node number j
int nbNodes;		// How many nodes in the path ?

///////////////////////////////////////////////////////////////////////
// An Entity is attached to a path, first we need to init the matrix 			
//
// Example :
//
// STRING* myPath = "path_000";
// ...
// action myAction()
// {
//		...
// 	initMat(me, myPath);
//		...
//	}	
///////////////////////////////////////////////////////////////////////

function initMat(ENTITY* entPtr, STRING* entPath) 
{
	int i,j, numNextNode;
	int nodeOK=0, edgeOK=0, nbEdges=0;
	
	VECTOR posNode;	// needed for path_getnode function
	var edgeSkill[3];
	
	nbNodes = 0;
	
	nbNodes = path_set(entPtr, entPath); 					// path_set returns the number of nodes in the path (not in the manual !)
	
	realloc(mat, sizeof(int)*(nbNodes+1)*(nbNodes+1));	// (re)Set the matrix to the requested dimension
	
	for (i=1;i<=nbNodes; i++);									// All unexplored nodes should be set to zero
	for (j=1;j<=nbNodes; j++);
	mat[i][j]=0;
	
	
	i=1;
	nodeOK = path_getnode (entPtr,i,posNode.x,NULL);	// returns a positive value if node number i exist
	
	while(nodeOK>0)
	{
		j=1;
		edgeOK = path_getedge (entPtr,i,j,edgeSkill);	// returns a positive value if edge number j exist
																		// edgeSkill[0]  = distance
																		// edgeSkill[1] = edge weight
																		// edgeSkill[1] = edge skill
		
		while(edgeOK>0)
		{
			nbEdges++;
			numNextNode = path_nextnode(entPtr, i, j);	// edge number j exist, this function returns the number of the destination node
			mat[i][numNextNode]=(int)edgeSkill[0];			// distance is stored in mat[i][numNextNode]
			
			// You want to check each edge ? -> Uncomment next line :
			//printf("Mat[%1d][%2d] = %3d",i,numNextNode,mat[i][numNextNode]);
			j++;
			edgeOK = path_getedge (entPtr,i,j,edgeSkill);
		}
		i++;
		nodeOK = path_getnode (entPtr,i,posNode.x,NULL);	
	}
	
	// You want to check number of edges ? -> Uncomment next line :
	// printf("Nb edges = %1d",nbEdges);
}

// NB : if you set a bi-directionnal sens between two points such as 1<->2, it will give 2 edges


Sorry, the page setup is almost lost between "code" tags...
More to come...
Posted By: NITRO777

Re: Dijkstra and Wed paths - 06/20/08 21:31

Sounds like its going to be pretty cool. One thing though, if you could try to make it easy for people to implement in their own games it would be nice because a lot of people dont understand the D algoritm, and a lot of people dont really understand matrices.
Posted By: MadJack

Re: Dijkstra and Wed paths - 06/21/08 13:58

I'll try to do my best smile
To day, DJ algo using paths is working !
So, there is no doubt, I'll give you ASAP something to play with.

BTW:
sometimes a matrix is nothing more than two dimensional array wink
you don't need to understand what is DJ algo to use it, at the end, we'll have some new path functions like this one :

I am here, I want to go there, what is my next point ?

I go on working...
Posted By: Quad

Re: Dijkstra and Wed paths - 06/21/08 14:34

this is gonna be awesome(t least for me) if system works fast.
Posted By: NITRO777

Re: Dijkstra and Wed paths - 06/21/08 15:14

Quote:
I am here, I want to go there, what is my next point ?
I am sure many people would want this kind of functionality, including myself. If it works good and is simple you could even get paid for it. I would pay for a simple functionality which could simply be dropped in a game with little complications.
Posted By: MadJack

Re: Dijkstra and Wed paths - 06/25/08 21:12

Well, here is a beta demo (V 0.05):

TestPath

Just unzip TestPath in your work folder.
Main file is DJ_Test.c
I made this demo as simple as possible, but if you look at the level in wed, you can check that it is working.
In this version, only one Entity can use the DJ functions for a given path.

I have to work on memory management now (queues instead of arrays) but I am not sure to get it in Lite-C. It might be easier do make a DLL...

Thanks for your encouragements blush
If this work help you, it is free !
Posted By: Quad

Re: Dijkstra and Wed paths - 06/25/08 22:12

nice work so far.
Posted By: NITRO777

Re: Dijkstra and Wed paths - 06/26/08 16:28

Quote:
If this work help you, it is free !
Wow! That is unbelievable. Thank you very much! I dont know why more people are not excited about this code, probably they havent seen it yet. Maybe when you finish with it you should put it on "user contributions" or "Aum resources", just so your work doesnt just dissappear unnoticed.
Posted By: NITRO777

Re: Dijkstra and Wed paths - 06/26/08 16:38

edit:its pretty cool that you can simply set the nodes down, change directions of the arrows if you want,,it looks awesome so far, I need to get more spare time to really look deeply into it later.
Posted By: NITRO777

Re: Dijkstra and Wed paths - 07/14/08 14:28

@MAdJack
Where would you put a simple state chooser like:

If (state=hungry)
PtDep =1;
PtDest = 12;
If (state=thirsty)
PtDep =current;
PtDest = 14;

etc.

etc.


I am not so much concerned about how to write it as I am wondering WHERE in the code you could put it in order for the action to read it and the ai to go somewhere based on time or a particular state.

Any help appreciated
Posted By: MadJack

Re: Dijkstra and Wed paths - 07/14/08 16:53

It's hard to say ! It depends on your project.
I have to do it for my own project, and it it's not so easy.
So far I guess that the main action will follow some steps like :

  • Evaluate my state of mind
  • Take a decision
  • If decision need a new displacement :
    - see where I am (PtDep)
    - say where I want to go (PtDest)
  • If I am moving to PtDest : follow the path untill PtDest
  • If I am where I want : do something
    and so on...

Meanwhile, you'll have to analyse the world to change the feelings of your character, enable avoidance...
Might be very complicated. crazy
This is the reason why I have called my project "RUGod" laugh
Posted By: NITRO777

Re: Dijkstra and Wed paths - 07/14/08 18:01

:DIts nice to know that I am not the only one who is having some difficulty, IM looking forward to seeing what you can do with your project.

Thanks again smile
Posted By: MadJack

Re: Dijkstra and Wed paths - 07/14/08 18:09

I am building a small level using my new DLL.
I think it will be ready within a week.
Posted By: MadJack

Re: Dijkstra and Wed paths - 07/16/08 18:51

May be less tha n a week...

Here is a beta demo level using wed paths and Dijkstra algo.

Demo MDJK

There is nothing to do. Just look at it (like a screensaver).
Camera will follow the chief. The chief try to reach the yellow mark. When he get it, the mark goes to another node.
N.B.: Due to very short avoidance script, sometimes guard might fall down ...
Posted By: NITRO777

Re: Dijkstra and Wed paths - 07/16/08 20:56

Wow, thats awesome, how did you do that avoidance? Are you going to release that code also??
Posted By: Quad

Re: Dijkstra and Wed paths - 07/16/08 21:46

really cool.

i some dll there? all open source lite-c or you will release a DLL plugin?
Posted By: MadJack

Re: Dijkstra and Wed paths - 07/17/08 09:44

Merci !

Before releasing my code, I want to clean it and to add some comments. So far, it's not clear enough...

Here is a top wiew of the level :



What is interesting is that we have here 12 guards using the same path at the same time. Path has about 65 nodes. You can see that path calculation is very fast.

Avoidance is very short code, you 'll see that soon (1 or 2 days). For tests purpose, I let a bloc on the way (bottom left quarter) Funny to see how the guards avoid it !
Posted By: NITRO777

Re: Dijkstra and Wed paths - 07/17/08 12:29

Quote:
What is interesting is that we have here 12 guards using the same path at the same time.
Wow! I have been trying to do this also but have only barely succeeded, for all my efforts I have only been able to fix one guard per action, this is much better.

I am looking forward to seeing this code.

A big 'merci' to you also!
Posted By: Machinery_Frank

Re: Dijkstra and Wed paths - 07/17/08 14:09

This works very well! Congratulation!
Posted By: MadJack

Re: Dijkstra and Wed paths - 07/17/08 21:32

Thanks,

So, here is my full folder with DLL, sources and published folder.
Released "as it" crazy

Full Demo V0.6 (8 Mo)

It is still a beta version of the DLL. Now I'd like to doublecheck the memory management and add other functions.

Have fun ! cool
Posted By: MadJack

Re: Dijkstra and Wed paths - 07/18/08 16:16

I have upgraded today to V7.10 and you know what ?
I can't join two nodes anymore... (ctrl + click)

I can't believe it !
Just at the moment I want to release my DLL cry

I hope it is my mistake.
Posted By: pegamode

Re: Dijkstra and Wed paths - 07/20/08 07:52

Hi MadJack ... I hope you solved your problem.

I included your really nice work into my point&click adventure (very early progress state) and you have done a very good job.

Currently I'm playing around with some code to let the player walk to points between the path nodes ... did you do this already and give me some hints how to do this best?

Best regards,
Pegamode.
Posted By: MadJack

Re: Dijkstra and Wed paths - 07/20/08 10:41

Hi Pegamode,

My aim is to set a region surrounding each point (a sphere which radius could be stored in node skill1).
Within this region, entity could move directly to any point.
For example, if you have a simple room, idea is to set a node xxx in the middle, set the radius to cover the room, and while entity is within the node sphere, all the points of the room might be reached directly.
Between two nodes, you are in fact following one edge. What could be done is to use edge skill to store "edge width", imagine something like a cylinder.
Node sphere or edge cylinder represent the space where entity can move freely.

In my DLL, I have added a function that returns the skills of the edge you are following.

You can download DLL here : MDJK_A7 DLL
Posted By: Anonymous

Re: Dijkstra and Wed paths - 07/20/08 11:09

man könnte auch mit c_trace prüfen ob der Punkt von der Entity aus sichtbar ist.
Wenn ja dann geht sie direkt zum Punkt, so könnte es dann auch in eckigen Räumen besser funktionieren also mit einem runden Radius.
Posted By: pegamode

Re: Dijkstra and Wed paths - 07/20/08 13:55

In my special case I don't want to let the moving entity leave the path. It should always remain onto the line between the nodes, but my problem is as follows:

When clicking left mouse button I scan from the mouse position for the nearest path in range. This is set as destination. That works fine so far.

But I have to set a lot of path nodes in WED so that the distance between the point I really clicked at and the chosen path I set as destination is not so big.
I'd like to get rid of setting such a lot of nodes and just break down also that world points into one dimension so that the point I clicked at lies onto the line between the two nodes.


Posted By: MadJack

Re: Dijkstra and Wed paths - 07/20/08 17:32

Oh, I think I understand.
Ideally, you need something like
  • find the nearest edge of the path
  • go to this edge
  • stop when you are in front of the mouse click

So far we don't have a function to get the nearest edge, you have to develop something approaching, for example :
  • find the nearest node of the path
  • explore all the edges of this node
  • mesure angle between mouse click and edge
  • use edge with the smallest angle
  • follow this edge and stop at the minimum distance of mouse click



Might be a good function to add in the DLL...

Posted By: pegamode

Re: Dijkstra and Wed paths - 07/20/08 17:42

Would be great if you could add such a function to your DLL :-)
Posted By: MadJack

Re: Dijkstra and Wed paths - 07/20/08 17:54

Would be great if Conitec could debug path editor and path functions.
They have cut my legs !
Posted By: pegamode

Re: Dijkstra and Wed paths - 08/21/08 21:23

Hi MadJack,

I've got a problem when using your dll with a model created by ent_create instead of placing it in WED.

The engine crashes when calling the following line:

player.skill99 = path_MDJK_init(player, _chr (myPath));

After creating the entity I added a wait and I use path_set(player, myPath); to set the path to the player.

Any ideas ???

Regards,
Pegamode
Posted By: MadJack

Re: Dijkstra and Wed paths - 08/22/08 07:56

Right now, I don't know, I did not try to use the DLL after ent_create, I'll do it this week-end. (I have sent you a PM)
© 2024 lite-C Forums