S
Sanil
Guest
Hello fellow programmers, I was trying to create a pathfinding algorithm using breadth-first search and using enqueues and dequeues...... I had seen a post on a*(star) algorithm and I was adapting that to my algorithm(bad idea?) an ways here is the code I was trying to adapt it from...
and this is my own code .....
please mind that it is still incomplete..... this is the link to the original post can someone please help me understand a few thing like what does the connections, end , filterpath do in the original code and how can I adapt it to my case. I know it is a lot to ask but anyone's help will be extremely appreciated.... thank you for atleast reading the whole post in advance.....
[Edit]- I just realized that you cannot color text in a code block so you will have to search for the words yourself I am sorry
Code:
/*
getShortestPath(SourceNode, TargetNode): ds_list
Computes the shortest path between the two nodes.
Returns a ds_list containing all the nodes in the path (first and last are included)
If a path could not be found, noone is returned
*/
var Source, Target, OpenedList, ClosedList, PreviousNodes, TentativeDistances, CurrentNode, End, Connections;
var tentativeDistance, Score, heuristicValue, lastConnection, currentScore;
Source = argument[0];
Target = argument[1];
OpenedList = ds_priority_create(); //{Value: Node, Priority: Score}
ClosedList = ds_list_create();
PreviousNodes = ds_map_create();//{Key: Node, Value: PreviousNode}
TentativeDistances = ds_map_create(); //{Key: Node, Value: Distance}
CurrentNode = noone;
ds_priority_add(OpenedList, Source, 1);
ds_map_add(TentativeDistances, Source, 0);
while (ds_priority_size(OpenedList) > 0) {
CurrentNode = ds_priority_find_min(OpenedList);
if (CurrentNode == Target) {
break;
}
ds_priority_delete_min(OpenedList); //Remove current node
ds_list_add(ClosedList, CurrentNode);
Connections = __Nodes[CurrentNode, 1];
End = false;
lastConnection = ds_map_find_last(Connections);
for (var Neighbour = ds_map_find_first(Connections); End == false;
Neighbour = ds_map_find_next(Connections, Neighbour)) {
if (Neighbour == lastConnection) {
End = true;
}
if (ds_list_find_index(ClosedList, Neighbour) != -1) {
continue;
}
if (!filterPath(id, Neighbour)) {
continue;
}
tentativeDistance = ds_map_find_value(Connections, Neighbour) + ds_map_find_value(TentativeDistances, CurrentNode);
heuristicValue = getHeuristic(Neighbour, Target);
if (!ds_map_exists(TentativeDistances, Neighbour)) {
ds_map_add(TentativeDistances, Neighbour, tentativeDistance);
} else if (tentativeDistance < ds_map_find_value(TentativeDistances, Neighbour)) {
ds_map_replace(TentativeDistances, Neighbour, tentativeDistance);
}
currentScore = ds_priority_find_priority(OpenedList, Neighbour);
Score = ds_map_find_value(TentativeDistances, Neighbour) + heuristicValue + 1;
if (Score < currentScore || currentScore == 0 || is_undefined(currentScore)) {
if (currentScore == 0 || is_undefined(currentScore)) {
ds_priority_add(OpenedList, Neighbour, Score);
} else {
ds_priority_change_priority(OpenedList, Neighbour, Score);
}
if (!ds_map_exists(PreviousNodes, Neighbour)) {
ds_map_add(PreviousNodes, Neighbour, CurrentNode);
} else {
ds_map_replace(PreviousNodes, Neighbour, CurrentNode);
}
}
}
}
ds_priority_destroy(OpenedList);
ds_list_destroy(ClosedList);
ds_map_destroy(TentativeDistances);
if (CurrentNode != Target) {
ds_map_destroy(PreviousNodes);
return(noone);
}
var Path;
Path = ds_list_create();
while (true) {
ds_list_add(Path, CurrentNode);
if (CurrentNode == Source) {
reversedPath = ds_list_create();
for (i = ds_list_size(Path) - 1; i >=0; i-=1) {
ds_list_add(reversedPath, ds_list_find_value(Path, i));
}
ds_list_destroy(Path);
ds_map_destroy(PreviousNodes);
return(reversedPath);
}
CurrentNode = ds_map_find_value(PreviousNodes, CurrentNode);
}
Code:
//scr_length_first(startnode, endnode);\
//This script will initialise a length first
//path finding search using queues
//and enqueue and dequeue functions
var UnexploredQueue, ExploredList, StartNode, EndNode, CurrentNode, Connections, End;
StartNode = argument[0];//The initial node
EndNode = argument[1];//Thelast node
ExploredList = ds_list_create();//Creates a list
UnexploredQueue = ds_queue_create();//Creates a queue
CurrentNode = noone;//Sets the current node to nothing
ds_queue_enqueue(UnexploredQueue, StartNode);//Puts the initial node at the top of the queue
while ds_queue_size(UnexploredQueue) > 0
{
CurrentNode = ds_queue_head(UnexploredQueue);//Stores the top value of queue in a variable
if CurrentNode == EndNode//Checks if we have reached the target
{
break;//Ends the script if the condition is true
}
ds_queue_dequeue(UnexploredQueue);//Removes the topmost node of queue
ds_list_add(ExploredList, CurrentNode);//Adds the current(explored) node to the explored list
Connections = Nodes[CurrentNode, 1]
End = false;
}
[Edit]- I just realized that you cannot color text in a code block so you will have to search for the words yourself I am sorry