Sorceress

Untitled

Sep 14th, 2015
249
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // start by creating an array the same size as the map (or a field in the map udt) . I'm assuming here the map is 32x32.
  2.  
  3. int pathing[32,32];
  4.  
  5.  
  6. // create a doneflag to record when a search pass didn't do anthing. initialise to zero to make a first pass.
  7. int doneflag = 0;
  8.  
  9. // set all elements to a large value, like 256.
  10. for(x=0; x<32; x++) {
  11. for(y=0; y<32; y++) {
  12. pathing[x,y] = 256;
  13. }
  14. }
  15.  
  16. // set the element where the player is to 0.
  17. pathing[player.x, player.y] = 0;
  18.  
  19. // loop while there are still passes to do.
  20. while (doneflag == 0)
  21.  
  22. // set a doneflag to 1 to say we are done.
  23. doneflag = 1;
  24.  
  25. // start a double loop through x and y.
  26. for(x=0; x<32; x++) {
  27. for(y=0; y<32; y++) {
  28.  
  29.  
  30. // if you find an element with value < 256 then look at four neighbours. if any of those is a floor tile, AND has value 256 then set it equal to value+1.
  31.  
  32. if(pathing[x,y] < 256) {
  33. if(pathing[x-1,y] == 256 && map[x-1,y] == floor) {pathing[x-1,y]=pathing[x,y]+1; doneflag=0;}
  34. if(pathing[x+1,y] == 256 && map[x+1,y] == floor) {pathing[x+1,y]=pathing[x,y]+1; doneflag=0;}
  35. if(pathing[x,y-1] == 256 && map[x,y-1] == floor) {pathing[x,y-1]=pathing[x,y]+1; doneflag=0;}
  36. if(pathing[x,y+1] == 256 && map[x,y+1] == floor) {pathing[x,y+1]=pathing[x,y]+1; doneflag=0;}
  37. }
  38.  
  39. // close the double loop
  40. }
  41. }
  42.  
  43. //close while
  44. }
RAW Paste Data