I have a small pathfinding logic. On my PC it takes around 2000 milliseconds to search a 1000x1000 grid in BASIC with some obstacles, and the same grid and obstacles, 14 milliseconds with C++.
You can toggle show_search to 1 if you want to see it output graphic updates while it is searching.
It is not very well documented, but I can comment the code if you want to use it.
BASIC
// Project: PATHFINDER 2
// Created: 2017-12-18
// show all errors
SetErrorMode(2)
// set window properties
SetWindowTitle( "PATHFINDER 2" )
SetWindowSize( 1000, 1000, 0 )
SetWindowAllowResize( 1 ) // allow the user to resize the window
// set display properties
SetVirtualResolution( 1000, 1000 ) // doesn't have to match the window
SetOrientationAllowed( 1, 1, 1, 1 ) // allow both portrait and landscape on mobile devices
SetSyncRate( 0, 1 ) // 30fps instead of 60 to save battery
SetScissor( 0,0,0,0 ) // use the maximum available screen space, no black borders
UseNewDefaultFonts( 1 ) // since version 2.0.22 we can use nicer default fonts
start_x = 500
start_y = 500
destination_x = 50
destination_y = 950
render_image_memblock = CreateMemblock(4 + 4 + 4 + (1000 * 1000 * 4))
SetMemblockInt(render_image_memblock,0,1000)
SetMemblockInt(render_image_memblock,4,1000)
SetMemblockInt(render_image_memblock,8,32)
n = ((start_y) * 1000 * 4) + ((start_x) * 4) + 12
SetMemblockByte(render_image_memblock,n,255)
SetMemblockByte(render_image_memblock,n + 1,0)
SetMemblockByte(render_image_memblock,n + 2,0)
SetMemblockByte(render_image_memblock,n + 3,255)
n = ((destination_y) * 1000 * 4) + ((destination_x) * 4) + 12
SetMemblockByte(render_image_memblock,n,255)
SetMemblockByte(render_image_memblock,n + 1,255)
SetMemblockByte(render_image_memblock,n + 2,255)
SetMemblockByte(render_image_memblock,n + 3,255)
render_image = CreateImageFromMemblock(render_image_memblock)
DeleteMemblock(render_image_memblock)
render_sprite = CreateSprite(render_image)
draw_dot_memblock = CreateMemblock(4 + 4 + 4 + (1 * 1 * 4))
SetMemblockInt(draw_dot_memblock,0,1)
SetMemblockInt(draw_dot_memblock,4,1)
SetMemblockInt(draw_dot_memblock,8,32)
SetMemblockByte(draw_dot_memblock,12,255)
SetMemblockByte(draw_dot_memblock,12 + 1,255)
SetMemblockByte(draw_dot_memblock,12 + 2,255)
SetMemblockByte(draw_dot_memblock,12 + 3,255)
draw_dot_image = CreateImageFromMemblock(draw_dot_memblock)
DeleteMemblock(draw_dot_memblock)
draw_dot_sprite = CreateSprite(draw_dot_image)
sync()
dim obstacle_map_info[2]
obstacle_map_info[1] = 1000
obstacle_map_info[2] = 1000
dim calculated_path[obstacle_map_info[1] * obstacle_map_info[2],6]
dim obstacle_array_for_pathfinder[obstacle_map_info[1],obstacle_map_info[2]]
SetRenderToImage(render_image,0)
SetSpriteColor(draw_dot_sprite,255,0,0,255)
for x = 400 to 800
obstacle_array_for_pathfinder[x,700] = 1
SetSpritePosition(draw_dot_sprite,x,700)
DrawSprite(draw_dot_sprite)
next x
for x = 1 to 10
obstacle_array_for_pathfinder[x,400] = 1
SetSpritePosition(draw_dot_sprite,x,400)
DrawSprite(draw_dot_sprite)
next x
for x = 600 to 800
obstacle_array_for_pathfinder[x,200] = 1
SetSpritePosition(draw_dot_sprite,x,200)
DrawSprite(draw_dot_sprite)
next x
for x = 10 to 400
obstacle_array_for_pathfinder[x,200] = 1
SetSpritePosition(draw_dot_sprite,x,200)
DrawSprite(draw_dot_sprite)
next x
for y = 200 to 700
obstacle_array_for_pathfinder[800,y] = 1
SetSpritePosition(draw_dot_sprite,800,y)
DrawSprite(draw_dot_sprite)
next y
for y = 200 to 700
obstacle_array_for_pathfinder[400,y] = 1
SetSpritePosition(draw_dot_sprite,400,y)
DrawSprite(draw_dot_sprite)
next y
for y = 200 to 400
obstacle_array_for_pathfinder[10,y] = 1
SetSpritePosition(draw_dot_sprite,10,y)
DrawSprite(draw_dot_sprite)
next y
SetRenderToScreen()
UnDim pathfinder_paths[] `1st=X, 2nd=Y, 3rd=paths that already checked this tile
Dim pathfinder_paths[obstacle_map_info[1],obstacle_map_info[2]] `1st=X, 2nd=Y, stores current step
current_step = 1
pathfinder_paths[destination_x,destination_y] = current_step
pathfinder_status = 0
pathfinder_length = 1
max_possible_steps = obstacle_map_info[1] * obstacle_map_info[2]
dim scan_list[max_possible_steps,2]
scan_list[1,1] = destination_x
scan_list[1,2] = destination_y
current_scan_item = 1
current_scan_list_insertion_point = 2
show_search = 0
show_found_path = 1
last_current_step = current_step
SetSpriteColor(draw_dot_sprite,0,0,255,255)
print("Searching...")
sync()
search_start = GetMilliseconds()
while not current_scan_item = current_scan_list_insertion_point
x = scan_list[current_scan_item,1]
y = scan_list[current_scan_item,2]
inc current_scan_item
current_step = pathfinder_paths[x,y]
if not last_current_step = current_step and show_search
last_current_step = current_step
SetRenderToScreen()
sync()
SetRenderToImage(render_image,0)
endif
if x < obstacle_map_info[1]
if not obstacle_array_for_pathfinder[x + 1,y] = 1 and pathfinder_paths[x + 1,y] = 0
pathfinder_paths[x + 1,y] = current_step + 1
scan_list[current_scan_list_insertion_point,1] = x + 1
scan_list[current_scan_list_insertion_point,2] = y
inc current_scan_list_insertion_point
if show_search
SetSpritePosition(draw_dot_sprite,x + 1,y)
DrawSprite(draw_dot_sprite)
endif
if x + 1 = start_x and y = start_y
exit
endif
endif
endif
if y > 1
if not obstacle_array_for_pathfinder[x,y - 1] = 1 and pathfinder_paths[x,y - 1] = 0
pathfinder_paths[x,y - 1] = current_step + 1
scan_list[current_scan_list_insertion_point,1] = x
scan_list[current_scan_list_insertion_point,2] = y - 1
inc current_scan_list_insertion_point
if show_search
SetSpritePosition(draw_dot_sprite,x,y - 1)
DrawSprite(draw_dot_sprite)
endif
if x = start_x and y - 1 = start_y
exit
endif
endif
endif
if y < obstacle_map_info[2]
if not obstacle_array_for_pathfinder[x,y + 1] = 1 and pathfinder_paths[x,y + 1] = 0
pathfinder_paths[x,y + 1] = current_step + 1
scan_list[current_scan_list_insertion_point,1] = x
scan_list[current_scan_list_insertion_point,2] = y + 1
inc current_scan_list_insertion_point
if show_search
SetSpritePosition(draw_dot_sprite,x,y + 1)
DrawSprite(draw_dot_sprite)
endif
if x = start_x and y + 1 = start_y
exit
endif
endif
endif
if x > 1
if not obstacle_array_for_pathfinder[x - 1,y] = 1 and pathfinder_paths[x - 1,y] = 0
pathfinder_paths[x - 1,y] = current_step + 1
scan_list[current_scan_list_insertion_point,1] = x - 1
scan_list[current_scan_list_insertion_point,2] = y
inc current_scan_list_insertion_point
if show_search
SetSpritePosition(draw_dot_sprite,x - 1,y)
DrawSprite(draw_dot_sprite)
endif
if x - 1 = start_x and y = start_y
exit
endif
endif
endif
endwhile
SetRenderToScreen()
step_in_calculated_path = 0
inc step_in_calculated_path
current_step = pathfinder_paths[start_x,start_y]
calculated_path[step_in_calculated_path,1] = start_x
calculated_path[step_in_calculated_path,2] = start_y
previous_x = start_x
previous_y = start_y
SetSpriteColor(draw_dot_sprite,255,255,255,255)
if current_step > 1
while current_step > 1
direction_diagonally = 0
current_tile_x = previous_x `store originating tiles X
current_tile_y = previous_y `store originating tiles Y
current_step = pathfinder_paths[current_tile_x,current_tile_y] `set current step, willl change later in this loop if backtracking goes diagonally
`find out whether to go up or down, right or left, diagonally
if pathfinder_paths[current_tile_x - 1,current_tile_y] < pathfinder_paths[current_tile_x,current_tile_y] and pathfinder_paths[current_tile_x - 1,current_tile_y] > 0 and not obstacle_array_for_pathfinder[current_tile_x,current_tile_y] = 1 then direction_diagonally = 5 `go left
if pathfinder_paths[current_tile_x,current_tile_y + 1] < pathfinder_paths[current_tile_x,current_tile_y] and pathfinder_paths[current_tile_x,current_tile_y + 1] > 0 and not obstacle_array_for_pathfinder[current_tile_x,current_tile_y] = 1 then direction_diagonally = 6 `go down
if pathfinder_paths[current_tile_x,current_tile_y - 1] < pathfinder_paths[current_tile_x,current_tile_y] and pathfinder_paths[current_tile_x,current_tile_y - 1] > 0 and not obstacle_array_for_pathfinder[current_tile_x,current_tile_y] = 1 then direction_diagonally = 7 `go up
if pathfinder_paths[current_tile_x + 1,current_tile_y] < pathfinder_paths[current_tile_x,current_tile_y] and pathfinder_paths[current_tile_x + 1,current_tile_y] > 0 and not obstacle_array_for_pathfinder[current_tile_x,current_tile_y] = 1 then direction_diagonally = 8 `go right
if pathfinder_paths[current_tile_x - 1,current_tile_y - 1] < pathfinder_paths[current_tile_x,current_tile_y] and pathfinder_paths[current_tile_x - 1,current_tile_y - 1] > 0 and not obstacle_array_for_pathfinder[current_tile_x - 1,current_tile_y] = 1 and not obstacle_array_for_pathfinder[current_tile_x,current_tile_y - 1] = 1
direction_diagonally = 1 `go up left diagonally
endif
if pathfinder_paths[current_tile_x - 1,current_tile_y + 1] < pathfinder_paths[current_tile_x,current_tile_y] and pathfinder_paths[current_tile_x - 1,current_tile_y + 1] > 0 and not obstacle_array_for_pathfinder[current_tile_x - 1,current_tile_y] = 1 and not obstacle_array_for_pathfinder[current_tile_x,current_tile_y + 1] = 1
direction_diagonally = 2 `go down left diagonally
endif
if pathfinder_paths[current_tile_x + 1,current_tile_y - 1] < pathfinder_paths[current_tile_x,current_tile_y] and pathfinder_paths[current_tile_x + 1,current_tile_y - 1] > 0 and not obstacle_array_for_pathfinder[current_tile_x + 1,current_tile_y] = 1 and not obstacle_array_for_pathfinder[current_tile_x,current_tile_y - 1] = 1
direction_diagonally = 3 `go up right diagonally
endif
if pathfinder_paths[current_tile_x + 1,current_tile_y + 1] < pathfinder_paths[current_tile_x,current_tile_y] and pathfinder_paths[current_tile_x + 1,current_tile_y + 1] > 0 and not obstacle_array_for_pathfinder[current_tile_x + 1,current_tile_y] = 1 and not obstacle_array_for_pathfinder[current_tile_x,current_tile_y + 1] = 1
direction_diagonally = 4 `go down right diagonally
endif
if current_tile_x > 1 and direction_diagonally = 5 `go left
previous_x = current_tile_x - 1
previous_y = current_tile_y
endif
if current_tile_y < obstacle_map_info[2] and direction_diagonally = 6 `go down
previous_x = current_tile_x
previous_y = current_tile_y + 1
endif
if current_tile_y > 1 and direction_diagonally = 7 `go up
previous_x = current_tile_x
previous_y = current_tile_y - 1
endif
if current_tile_x < obstacle_map_info[1] and direction_diagonally = 8 `go right
previous_x = current_tile_x + 1
previous_y = current_tile_y
endif
if current_tile_x > 1 and current_tile_y > 1 and direction_diagonally = 1 `go up left diagonally
previous_x = current_tile_x - 1
previous_y = current_tile_y - 1
endif
if current_tile_x > 1 and current_tile_y < obstacle_map_info[2] and direction_diagonally = 2 `go down left diagonally
previous_x = current_tile_x - 1
previous_y = current_tile_y + 1
endif
if current_tile_x < obstacle_map_info[1] and current_tile_y > 1 and direction_diagonally = 3 `go up right diagonally
previous_x = current_tile_x + 1
previous_y = current_tile_y - 1
endif
if current_tile_x < obstacle_map_info[1] and current_tile_y < obstacle_map_info[2] and direction_diagonally = 4 `go down right diagonally
previous_x = current_tile_x + 1
previous_y = current_tile_y + 1
endif
inc step_in_calculated_path
calculated_path[step_in_calculated_path,1] = previous_x
calculated_path[step_in_calculated_path,2] = previous_y
calculated_path[step_in_calculated_path,3] = direction_diagonally
if show_found_path and show_search
SetRenderToImage(render_image,0)
SetSpritePosition(draw_dot_sprite,previous_x,previous_y)
DrawSprite(draw_dot_sprite)
SetRenderToScreen()
sync()
endif
if previous_x = destination_x and previous_y = destination_y
pathfinder_status = 1 //pathfinder completed succesfully
exit
endif
endwhile
endif
search_end = GetMilliseconds()
total_time_seconds = (search_end - search_start)
do
print( ScreenFPS() )
print(total_time_seconds)
sync()
loop
C++
// Includes
#include "template.h"
// Namespace
using namespace AGK;
int start_x{ 500 };
int start_y{ 500 };
int destination_x{ 50 };
int destination_y{ 950 };
int render_image_memblock;
int render_image;
int render_sprite;
int draw_dot_memblock;
int draw_dot_image;
int draw_dot_sprite;
int n;
int current_step;
int long obstacle_map_info[3] = { 0 , 1001 , 1001 };
//obstacle_map_info[1] = 1000;
//obstacle_map_info[2] = 1000;
int calculated_path[1000000][6];
bool obstacle_array_for_pathfinder[1001][1001]
{
{ 0 },
{ 0 }
};
int pathfinder_status;
int pathfinder_length;
int max_possible_steps;
int current_scan_item;
int current_scan_list_insertion_point;
int show_search;
int show_found_path;
int last_current_step;
int search_start;
int search_end;
int total_time_seconds;
int step_in_calculated_path;
int previous_x;
int previous_y;
int direction_diagonally;
int current_tile_x;
int current_tile_y;
int pathfinder_paths[1001][1001]
{
{ 0 },
{ 0 }
};
int scan_list[1000001][2]
{
{0},
{0}
};
app App;
void app::Begin(void)
{
// show all errors
agk::SetErrorMode(2);
// set window properties
agk::SetWindowTitle("PATHFINDER 2");
agk::SetWindowSize(1000, 1000, 0);
agk::SetWindowAllowResize(1); // allow the user to resize the window
// set display properties
agk::SetVirtualResolution(1000, 1000); // doesn't have to match the window
agk::SetOrientationAllowed(1, 1, 1, 1); // allow both portrait and landscape on mobile devices
agk::SetSyncRate(0, 1); // 30fps instead of 60 to save battery
agk::SetScissor(0, 0, 0, 0); // use the maximum available screen space, no black borders
agk::UseNewDefaultFonts(1); // since version 2.0.22 we can use nicer default fonts
render_image_memblock = agk::CreateMemblock(4 + 4 + 4 + (1000 * 1000 * 4));
agk::SetMemblockInt(render_image_memblock, 0, 1000);
agk::SetMemblockInt(render_image_memblock, 4, 1000);
agk::SetMemblockInt(render_image_memblock, 8, 32);
n = ((start_y) * 1000 * 4) + ((start_x) * 4) + 12;
agk::SetMemblockByte(render_image_memblock, n, 255);
agk::SetMemblockByte(render_image_memblock, n + 1, 0);
agk::SetMemblockByte(render_image_memblock, n + 2, 0);
agk::SetMemblockByte(render_image_memblock, n + 3, 255);
n = ((destination_y) * 1000 * 4) + ((destination_x) * 4) + 12;
agk::SetMemblockByte(render_image_memblock, n, 255);
agk::SetMemblockByte(render_image_memblock, n + 1, 255);
agk::SetMemblockByte(render_image_memblock, n + 2, 255);
agk::SetMemblockByte(render_image_memblock, n + 3, 255);
render_image = agk::CreateImageFromMemblock(render_image_memblock);
agk::DeleteMemblock(render_image_memblock);
render_sprite = agk::CreateSprite(render_image);
draw_dot_memblock = agk::CreateMemblock(4 + 4 + 4 + (1 * 1 * 4));
agk::SetMemblockInt(draw_dot_memblock, 0, 1);
agk::SetMemblockInt(draw_dot_memblock, 4, 1);
agk::SetMemblockInt(draw_dot_memblock, 8, 32);
agk::SetMemblockByte(draw_dot_memblock, 12, 255);
agk::SetMemblockByte(draw_dot_memblock, 12 + 1, 255);
agk::SetMemblockByte(draw_dot_memblock, 12 + 2, 255);
agk::SetMemblockByte(draw_dot_memblock, 12 + 3, 255);
draw_dot_image = agk::CreateImageFromMemblock(draw_dot_memblock);
agk::DeleteMemblock(draw_dot_memblock);
draw_dot_sprite = agk::CreateSprite(draw_dot_image);
agk::Sync();
agk::SetRenderToImage(render_image, 0);
agk::SetSpriteColor(draw_dot_sprite, 255, 0, 0, 255);
for (int x = 400; x < 801; x++)
{
obstacle_array_for_pathfinder[x][700] = 1;
agk::SetSpritePosition(draw_dot_sprite, x, 700.0);
agk::DrawSprite(draw_dot_sprite);
}
for (int x = 1; x < 11; x++)
{
obstacle_array_for_pathfinder[x][400] = 1;
agk::SetSpritePosition(draw_dot_sprite, x, 400);
agk::DrawSprite(draw_dot_sprite);
}
for (int x = 600; x < 801; x++)
{
obstacle_array_for_pathfinder[x][200] = 1;
agk::SetSpritePosition(draw_dot_sprite, x, 200);
agk::DrawSprite(draw_dot_sprite);
}
for (int x = 10; x < 401; x++)
{
obstacle_array_for_pathfinder[x][200] = 1;
agk::SetSpritePosition(draw_dot_sprite, x, 200);
agk::DrawSprite(draw_dot_sprite);
}
for (int y = 200; y < 701; y++)
{
obstacle_array_for_pathfinder[800][y] = 1;
agk::SetSpritePosition(draw_dot_sprite, 800, y);
agk::DrawSprite(draw_dot_sprite);
}
for (int y = 200; y < 701; y++)
{
obstacle_array_for_pathfinder[400][y] = 1;
agk::SetSpritePosition(draw_dot_sprite, 400, y);
agk::DrawSprite(draw_dot_sprite);
}
for (int y = 200; y < 400; y++)
{
obstacle_array_for_pathfinder[10][y] = 1;
agk::SetSpritePosition(draw_dot_sprite, 10, y);
agk::DrawSprite(draw_dot_sprite);
}
agk::SetRenderToScreen();
//int long pathfinder_paths[1000][1000];// `1st=X, 2nd = Y, stores current step
current_step = 1;
pathfinder_paths[destination_x][destination_y] = current_step;
pathfinder_status = 0;
pathfinder_length = 1;
max_possible_steps = 1000000;
scan_list[1][1] = destination_x;
scan_list[1][2] = destination_y;
current_scan_item = 1;
current_scan_list_insertion_point = 2;
show_search = 0;
show_found_path = 1;
last_current_step = current_step;
agk::SetSpriteColor(draw_dot_sprite, 0, 0, 255, 255);
agk::Print("Searching...");
agk::Sync();
search_start = agk::GetMilliseconds();
int x;
int y;
while (current_scan_item != current_scan_list_insertion_point)
{
x = scan_list[current_scan_item][1];
y = scan_list[current_scan_item][2];
current_scan_item++;
current_step = pathfinder_paths[x][y];
if ((last_current_step != current_step) && (show_search == 1))
{
last_current_step = current_step;
agk::SetRenderToScreen();
agk::Sync();
agk::SetRenderToImage(render_image, 0);
}
if (x < obstacle_map_info[1])
{
if ((obstacle_array_for_pathfinder[x + 1][y] != 1) && (pathfinder_paths[x + 1][y] == 0))
{
pathfinder_paths[x + 1][y] = current_step + 1;
scan_list[current_scan_list_insertion_point][1] = x + 1;
scan_list[current_scan_list_insertion_point][2] = y;
current_scan_list_insertion_point++;
if (show_search == 1)
{
agk::SetSpritePosition(draw_dot_sprite, x + 1, y);
agk::DrawSprite(draw_dot_sprite);
}
if ((x + 1 == start_x) && (y == start_y))
{
break;
}
}
}
if (y > 1)
{
if ((obstacle_array_for_pathfinder[x][y - 1] != 1) && (pathfinder_paths[x][y - 1] == 0))
{
pathfinder_paths[x][y - 1] = current_step + 1;
scan_list[current_scan_list_insertion_point][1] = x;
scan_list[current_scan_list_insertion_point][2] = y - 1;
current_scan_list_insertion_point++;
if (show_search == 1)
{
agk::SetSpritePosition(draw_dot_sprite, x, y - 1);
agk::DrawSprite(draw_dot_sprite);
}
if ((x == start_x) && (y - 1 == start_y))
{
break;
}
}
}
if (y < obstacle_map_info[2])
{
if ((obstacle_array_for_pathfinder[x][y + 1] != 1) && (pathfinder_paths[x][y + 1] == 0))
{
pathfinder_paths[x][y + 1] = current_step + 1;
scan_list[current_scan_list_insertion_point][1] = x;
scan_list[current_scan_list_insertion_point][2] = y + 1;
current_scan_list_insertion_point++;
if (show_search == 1)
{
agk::SetSpritePosition(draw_dot_sprite, x, y + 1);
agk::DrawSprite(draw_dot_sprite);
}
if ((x == start_x) && (y + 1 == start_y))
{
break;
}
}
}
if (x > 1)
{
if ((obstacle_array_for_pathfinder[x - 1][y] != 1) && (pathfinder_paths[x - 1][y] == 0))
{
pathfinder_paths[x - 1][y] = current_step + 1;
scan_list[current_scan_list_insertion_point][1] = x - 1;
scan_list[current_scan_list_insertion_point][2] = y;
current_scan_list_insertion_point++;
if (show_search == 1)
{
agk::SetSpritePosition(draw_dot_sprite, x - 1, y);
agk::DrawSprite(draw_dot_sprite);
}
if ((x - 1 == start_x) && (y == start_y))
{
break;
}
}
}
}
agk::SetRenderToScreen();
step_in_calculated_path = 0;
step_in_calculated_path++;
current_step = pathfinder_paths[start_x][start_y];
calculated_path[step_in_calculated_path][1] = start_x;
calculated_path[step_in_calculated_path][2] = start_y;
previous_x = start_x;
previous_y = start_y;
agk::SetSpriteColor(draw_dot_sprite, 255, 255, 255, 255);
if (current_step > 1)
{
while (current_step > 1)
{
direction_diagonally = 0;
current_tile_x = previous_x; //store originating tiles X
current_tile_y = previous_y; //store originating tiles Y
current_step = pathfinder_paths[current_tile_x][current_tile_y]; //set current step, willl change later in this loop if backtracking goes diagonally
//find out whether to go up or down, right or left, diagonally
if ((pathfinder_paths[current_tile_x - 1][current_tile_y] < pathfinder_paths[current_tile_x][current_tile_y]) && (pathfinder_paths[current_tile_x - 1][current_tile_y] > 0) && (obstacle_array_for_pathfinder[current_tile_x][current_tile_y] != 1))
direction_diagonally = 5; // go left
if ((pathfinder_paths[current_tile_x][current_tile_y + 1] < pathfinder_paths[current_tile_x][current_tile_y]) && (pathfinder_paths[current_tile_x][current_tile_y + 1] > 0) && (obstacle_array_for_pathfinder[current_tile_x][current_tile_y] != 1))
direction_diagonally = 6; // go down
if ((pathfinder_paths[current_tile_x][current_tile_y - 1] < pathfinder_paths[current_tile_x][current_tile_y]) && (pathfinder_paths[current_tile_x][current_tile_y - 1] > 0) && (obstacle_array_for_pathfinder[current_tile_x][current_tile_y] != 1))
direction_diagonally = 7; //go up
if ((pathfinder_paths[current_tile_x + 1][current_tile_y] < pathfinder_paths[current_tile_x][current_tile_y]) && (pathfinder_paths[current_tile_x + 1][current_tile_y] > 0) && (obstacle_array_for_pathfinder[current_tile_x][current_tile_y] != 1))
direction_diagonally = 8; //go right
if ((pathfinder_paths[current_tile_x - 1][current_tile_y - 1] < pathfinder_paths[current_tile_x][current_tile_y]) && (pathfinder_paths[current_tile_x - 1][current_tile_y - 1] > 0) && (obstacle_array_for_pathfinder[current_tile_x - 1][current_tile_y] != 1) && (obstacle_array_for_pathfinder[current_tile_x][current_tile_y - 1] != 1))
direction_diagonally = 1;; //go up left diagonally
if ((pathfinder_paths[current_tile_x - 1][current_tile_y + 1] < pathfinder_paths[current_tile_x][current_tile_y]) && (pathfinder_paths[current_tile_x - 1][current_tile_y + 1] > 0) && (obstacle_array_for_pathfinder[current_tile_x - 1][current_tile_y] != 1) && (obstacle_array_for_pathfinder[current_tile_x][current_tile_y + 1] != 1))
direction_diagonally = 2; //go down left diagonally
if ((pathfinder_paths[current_tile_x + 1][current_tile_y - 1] < pathfinder_paths[current_tile_x][current_tile_y]) && (pathfinder_paths[current_tile_x + 1][current_tile_y - 1] > 0) && (obstacle_array_for_pathfinder[current_tile_x + 1][current_tile_y] != 1) && (obstacle_array_for_pathfinder[current_tile_x][current_tile_y - 1] != 1))
direction_diagonally = 3; //go up right diagonally
if ((pathfinder_paths[current_tile_x + 1][current_tile_y + 1] < pathfinder_paths[current_tile_x][current_tile_y]) && (pathfinder_paths[current_tile_x + 1][current_tile_y + 1] > 0) && (obstacle_array_for_pathfinder[current_tile_x + 1][current_tile_y] != 1) && (obstacle_array_for_pathfinder[current_tile_x][current_tile_y + 1] != 1))
direction_diagonally = 4; //go down right diagonally
if ((current_tile_x > 1) && (direction_diagonally == 5)) //go left
{
previous_x = current_tile_x - 1;
previous_y = current_tile_y;
}
if ((current_tile_y < obstacle_map_info[2]) && (direction_diagonally == 6)) //go down
{
previous_x = current_tile_x;
previous_y = current_tile_y + 1;
}
if ((current_tile_y > 1) && (direction_diagonally == 7)) //go up
{
previous_x = current_tile_x;
previous_y = current_tile_y - 1;
}
if ((current_tile_x < obstacle_map_info[1]) && (direction_diagonally == 8)) //go right
{
previous_x = current_tile_x + 1;
previous_y = current_tile_y;
}
if ((current_tile_x > 1) && (current_tile_y > 1) && (direction_diagonally == 1)) //go up left diagonally
{
previous_x = current_tile_x - 1;
previous_y = current_tile_y - 1;
}
if ((current_tile_x > 1) && (current_tile_y < obstacle_map_info[2]) && (direction_diagonally == 2)) //go down left diagonally
{
previous_x = current_tile_x - 1;
previous_y = current_tile_y + 1;
}
if ((current_tile_x < obstacle_map_info[1]) && (current_tile_y > 1) && (direction_diagonally == 3)) //go up right diagonally
{
previous_x = current_tile_x + 1;
previous_y = current_tile_y - 1;
}
if ((current_tile_x < obstacle_map_info[1]) && (current_tile_y < obstacle_map_info[2]) && (direction_diagonally == 4)) //go down right diagonally
{
previous_x = current_tile_x + 1;
previous_y = current_tile_y + 1;
}
step_in_calculated_path++;
calculated_path[step_in_calculated_path][1] = previous_x;
calculated_path[step_in_calculated_path][2] = previous_y;
calculated_path[step_in_calculated_path][3] = direction_diagonally;
if ((show_found_path == 1) && (show_search == 1))
{
agk::SetRenderToImage(render_image, 0);
agk::SetSpritePosition(draw_dot_sprite, previous_x, previous_y);
agk::DrawSprite(draw_dot_sprite);
agk::SetRenderToScreen();
agk::Sync();
}
if ((previous_x == destination_x) && (previous_y == destination_y))
{
pathfinder_status = 1; //pathfinder completed succesfully
break;
}
}
}
search_end = agk::GetMilliseconds();
total_time_seconds = (search_end - search_start);
}
int app::Loop (void)
{
agk::Print( agk::ScreenFPS() );
agk::Print(total_time_seconds);
agk::Sync();
return 0; // return 1 to close app
}
void app::End (void)
{
}
13/0