#define mapw 600
#define maph 600

#define ERROR -1
#define N 0
#define S 1
#define E 2
#define W 3
#define NE 4
#define SE 5
#define NW 6
#define SW 7



void pathfind(int x, int y, int dest_x, int dest_y){
  int path[20][15], i = 1, a, b, stop = 0, check;

  for(a=0; a < mapw; a++){
    for(b=0; b < maph; b++){
      path[a][b] = -1;
    }
  }

  path[dest_x][dest_y] = 0;

  while(stop == 0){
    check = 0;
    for(a=0; a < mapw; a++){
      for(b=0; b < maph; b++){
        if(path[a][b] == i-1){
          check = 1;
          if(b < maph-1 && path[a][b+1] == -1 && solids_array[a][b+1] == 0)   {path[a][b+1] = i;}
          if(b > 0      && path[x][b-1] == -1 && solids_array[a][b-1] == 0)   {path[a][b-1] = i;}
          if(a < mapw-1 && path[a+1][b] == -1 && solids_array[a+1][b] == 0)   {path[a+1][b] = i;}
          if(a > 0      && path[a-1][b] == -1 && solids_array[a-1][b] == 0)   {path[a-1][b] = i;}
          if(a < mapw-1 && b < maph-1 && path[a+1][b+1] == -1 && solids_array[a+1][b+1] == 0) {path[a+1][b+1] = i;}
          if(a > 0      && b < maph-1 && path[a-1][b+1] == -1 && solids_array[a-1][b+1] == 0) {path[a-1][b+1] = i;}
          if(a < mapw-1 && b > 0      && path[a+1][b-1] == -1 && solids_array[a+1][b-1] == 0) {path[a+1][b-1] = i;}
          if(a > 0      && b > 0      && path[a-1][b-1] == -1 && solids_array[a-1][b-1] == 0) {path[a-1][b-1] = i;}
        }
        if(path[x][y] > -1){stop = 1;}
      }
    }
    i++;
    if(check == 0){return ERROR; /*can't find route*/}
  }

  
  if(path[x][y] != 0){
    if(dest_x < x){
      if(dest_y < y)     {if(x > 0      && y > 0      && path[x-1][y-1] > -1 && path[x-1][y-1] < path[x][y] && solids_array[x-1][y-1] == 0 && solids_array[x-1][y] == 0 && solids_array[x][y-1] == 0) {return NW;}}
      else if(dest_y > y){if(x > 0      && y < maph-1 && path[x-1][y+1] > -1 && path[x-1][y+1] < path[x][y] && solids_array[x-1][y+1] == 0 && solids_array[x-1][y] == 0 && solids_array[x][y+1] == 0) {return NE;}}
      else               {if(x > 0                    && path[x-1][y] > -1   && path[x-1][y]   < path[x][y] && solids_array[x-1][y] == 0) {return N;}}
    }

    else if(dest_x > x){
      if(dest_y < y)     {if(x < mapw-1 && y > 0      && path[x+1][y-1] > -1 && path[x+1][y-1] < path[x][y] && solids_array[x+1][y-1] == 0 && solids_array[x+1][y] == 0 && solids_array[x][y-1] == 0) {return SW;}}
      else if(dest_y > y){if(x < mapw-1 && y < maph-1 && path[x+1][y+1] > -1 && path[x+1][y+1] < path[x][y] && solids_array[x+1][y+1] == 0 && solids_array[x+1][y] == 0 && solids_array[x][y+1] == 0) {return SE;}}
      else               {if(x < mapw-1               && path[x+1][y] > -1   && path[x+1][y]   < path[x][y] && solids_array[x+1][y] == 0) {return S;}}
    }

    else if(dest_y < y){if(y > 0                    && path[x][y-1] > -1   && path[x][y-1] < path[x][y] && solids_array[x][y-1] == 0) {return W;}}
    else if(dest_y > y){if(y < maph-1               && path[x][y+1] > -1   && path[x][y+1] < path[x][y] && solids_array[x][y+1] == 0) {return E;}}

    else{ //check all
      if(y < maph-1               && path[x][y+1] > -1   && path[x][y+1] < path[x][y] && solids_array[x][y+1] == 0)     {return E;}
      if(y > 0                    && path[x][y-1] > -1   && path[x][y-1] < path[x][y] && solids_array[x][y-1] == 0)     {return W;}
      if(x < mapw-1               && path[x+1][y] > -1   && path[x+1][y] < path[x][y] && solids_array[x+1][y] == 0)     {return S;}
      if(x > 0                    && path[x-1][y] > -1   && path[x-1][y] < path[x][y] && solids_array[x-1][y] == 0)     {return N;}
      if(x < mapw-1 && y < maph-1 && path[x+1][y+1] > -1 && path[x+1][y+1] < path[x][y] && solids_array[x+1][y+1] == 0 && solids_array[x+1][y] == 0 && solids_array[x][y+1] == 0) {return SE;}
      if(x > 0      && y < maph-1 && path[x-1][y+1] > -1 && path[x-1][y+1] < path[x][y] && solids_array[x-1][y+1] == 0 && solids_array[x-1][y] == 0 && solids_array[x][y+1] == 0) {return NE;}
      if(x < mapw-1 && y > 0      && path[x+1][y-1] > -1 && path[x+1][y-1] < path[x][y] && solids_array[x+1][y-1] == 0 && solids_array[x+1][y] == 0 && solids_array[x][y-1] == 0) {return SW;}
      if(x > 0      && y > 0      && path[x-1][y-1] > -1 && path[x-1][y-1] < path[x][y] && solids_array[x-1][y-1] == 0 && solids_array[x-1][y] == 0 && solids_array[x][y-1] == 0) {return NW;}
    }
  }

  return ERROR;
}
