View difference between Paste ID: pkbB8Vrz and VrAF9kdb
SHOW: | | - or go back to the newest paste.
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.
1+
2
w = 24
3
range = 256
4
5
int pathing[h,w]; 
6
int doneflag = 0;
7-
// create a doneflag to record when a search pass didn't do anthing. initialise to zero to make a first pass.
7+
8
for(xx=0; xx<w; xx++) {
9
  for(yy=0; yy<h; yy++) {
10-
// set all elements to a large value, like 256.
10+
    pathing[xx,yy] = range;
11
  }
12
}
13-
    pathing[xx,yy] = 256;
13+
14
pathing[player.x/8, player.y/8] = 0;
15
 
16
while (doneflag == 0) {
17-
// set the element where the player is to 0.
17+
18
doneflag = 1;
19
 
20-
// loop while there are still passes to do.
20+
21
  for(yy=0; yy<h; yy++) {
22
 
23-
// set a doneflag to 1 to say we are done.
23+
     if(pathing[xx,yy] < range) {
24
       if(pathing[xx-1,yy] == range && ds_grid_get(global.mainGrid,xx-1,yy) == 0) {pathing[xx-1,yy]=pathing[xx,yy]+1; doneflag=0;}
25
       if(pathing[xx+1,yy] == range && ds_grid_get(global.mainGrid,xx+1,yy) == 0) {pathing[xx+1,yy]=pathing[xx,yy]+1; doneflag=0;}
26-
// start a double loop through x and y.
26+
       if(pathing[xx,yy-1] == range && ds_grid_get(global.mainGrid,xx,yy-1) == 0) {pathing[xx,yy-1]=pathing[xx,yy]+1; doneflag=0;}
27
       if(pathing[xx,yy+1] == range && ds_grid_get(global.mainGrid,xx,yy+1) == 0) {pathing[xx,yy+1]=pathing[xx,yy]+1; doneflag=0;}
28
        } 
29
    }
30
}
31-
// 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
for(xx=w-1; xx>=0; xx--) {
33-
     if(pathing[xx,yy] < 256) {
33+
  for(yy=h-1; yy>=0; yy--) {
34-
       if(pathing[xx-1,yy] == 256 && ds_grid_get(global.mainGrid,xx,yy) = 0)  {pathing[xx-1,yy]=pathing[xx,yy]+1; doneflag=0;}
34+
35-
       if(pathing[xx+1,yy] == 256 && ds_grid_get(global.mainGrid,xx+1,yy) = 0) {pathing[xx+1,yy]=pathing[xx,yy]+1; doneflag=0;}
35+
     if(pathing[xx,yy] < range) {
36-
       if(pathing[xx,yy-1] == 256 && ds_grid_get(global.mainGrid,xx,yy-1) = 0) {pathing[xx,yy-1]=pathing[xx,yy]+1; doneflag=0;}
36+
       if(pathing[xx-1,yy] == range && ds_grid_get(global.mainGrid,xx-1,yy) == 0) {pathing[xx-1,yy]=pathing[xx,yy]+1; doneflag=0;}
37-
       if(pathing[xx,yy+1] == 256 && ds_grid_get(global.mainGrid,xx,yy+1) = 0) {pathing[xx,yy+1]=pathing[xx,yy]+1; doneflag=0;}
37+
       if(pathing[xx+1,yy] == range && ds_grid_get(global.mainGrid,xx+1,yy) == 0) {pathing[xx+1,yy]=pathing[xx,yy]+1; doneflag=0;}
38
       if(pathing[xx,yy-1] == range && ds_grid_get(global.mainGrid,xx,yy-1) == 0) {pathing[xx,yy-1]=pathing[xx,yy]+1; doneflag=0;}
39-
// close the double loop
39+
       if(pathing[xx,yy+1] == range && ds_grid_get(global.mainGrid,xx,yy+1) == 0) {pathing[xx,yy+1]=pathing[xx,yy]+1; doneflag=0;}
40
        } 
41
    }
42
}
43-
//close while
43+
44
}