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.
- int pathing[32,32];
- // 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(x=0; x<32; x++) {
- for(y=0; y<32; y++) {
- pathing[x,y] = 256;
- }
- }
- // set the element where the player is to 0.
- pathing[player.x, player.y] = 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(x=0; x<32; x++) {
- for(y=0; y<32; y++) {
- // 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[x,y] < 256) {
- if(pathing[x-1,y] == 256 && map[x-1,y] == floor) {pathing[x-1,y]=pathing[x,y]+1; doneflag=0;}
- if(pathing[x+1,y] == 256 && map[x+1,y] == floor) {pathing[x+1,y]=pathing[x,y]+1; doneflag=0;}
- if(pathing[x,y-1] == 256 && map[x,y-1] == floor) {pathing[x,y-1]=pathing[x,y]+1; doneflag=0;}
- if(pathing[x,y+1] == 256 && map[x,y+1] == floor) {pathing[x,y+1]=pathing[x,y]+1; doneflag=0;}
- }
- // close the double loop
- }
- }
- //close while
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement