Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 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.
- h = 24
- w = 24
- int pathing[h,w];
- // create a doneflag to record when a search pass didn't do anthing. initialise to zero to make a first pass.
- int doneflag = 0;
- // set all elements to a large value, like 256.
- for(xx=0; xx<w; xx++) {
- for(yy=0; yy<h; yy++) {
- pathing[xx,yy] = 256;
- }
- }
- // set the element where the player is to 0.
- pathing[player.x/8, player.y/8] = 0;
- // loop while there are still passes to do.
- while (doneflag == 0) {
- // set a doneflag to 1 to say we are done.
- doneflag = 1;
- // start a double loop through x and y.
- for(xx=0; xx<w; xx++) {
- for(yy=0; yy<h; yy++) {
- // 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.
- if(pathing[xx,yy] < 256) {
- if(pathing[xx-1,yy] == 256 && ds_grid_get(global.mainGrid,xx,yy) = 0) {pathing[xx-1,yy]=pathing[xx,yy]+1; doneflag=0;}
- 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;}
- 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;}
- 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;}
- }
- // close the double loop
- }
- }
- //close while
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement