Advertisement
Guest User

Desert Maze Generator

a guest
Feb 11th, 2014
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //set stage
  2. terrain = p5_generate_terrain(25,25);
  3.  
  4. //*********GENERATE TERRAIN*****************//
  5.  
  6. ///p5_generate_terrain(wide,height);
  7. //generates the desert map
  8.  
  9. //wide x height
  10. var wide = argument0;
  11. var height = argument1;
  12.  
  13. var t = 0; //var for holding the entire terrain information
  14.  
  15. //generation
  16. for(var i = 0;i<height;i++){
  17.     for(var j = 0;j<wide;j++){
  18.         t[i,j] = p5_get_square();
  19.     }
  20. }
  21.  
  22. //fix terrain depending on setting rules
  23. t = p5_terrain_check(t);
  24.  
  25. //generated terrain
  26. return t;
  27.  
  28. //*****************************************//
  29.  
  30. //**********RANDOM TERRAIN SELECTION******//
  31. ///p5_get_square();
  32.  
  33. //type of terrain rate
  34. var normal = 14;
  35. var dune = 10;
  36. var cactus = 10;
  37. var water = 7;
  38. var shadow = 15;
  39. var scavenge = 7;
  40. var abyss = 15;
  41. var patrol = 10;
  42. var camp = 5;
  43.  
  44. //set scale of success 0-15, 15-25, 25-34...
  45. dune+=normal;
  46. cactus+=dune;
  47. water+=cactus;
  48. shadow+=water;
  49. scavenge+=shadow;
  50. abyss+=scavenge;
  51. patrol+=abyss;
  52. camp+=patrol;
  53.  
  54. var type = 'normal';
  55.  
  56. //random terrain selection
  57. var square = irandom(camp);//select the higher value for the roll
  58. //normal
  59. if(square<normal){
  60.     type = 'normal';
  61. }else
  62. //dune
  63. if(square<dune){
  64.     type = 'dune';
  65. }else
  66. //cactus
  67. if(square<cactus){
  68.     type = 'cactus';
  69. }else
  70. //water
  71. if(square<water){
  72.     type = 'water';
  73. }else
  74. //shadow
  75. if(square<shadow){
  76.     type = 'shadow';
  77. }else
  78. //scavenge
  79. if(square<scavenge){
  80.     type = 'scavenge';
  81. }else
  82. //abyss
  83. if(square<abyss){
  84.     type = 'abyss';
  85. }else
  86. //patrol
  87. if(square<patrol){
  88.     type = 'patrol';
  89. }else
  90. //camp
  91. if(square<camp){
  92.     type = 'camp';
  93. }
  94.  
  95. //return terrain type (string)
  96. return type;
  97. //****************************************//
  98.  
  99. //**********FIXING THE MAP***************//
  100. ///p5_terrain_check(terrain);
  101.  
  102. var t = argument0;
  103. var valid = false;
  104. var h = array_height_2d(t); //height
  105. var w = array_length_2d(t,0); //width
  106.  
  107. //set starting point and goal for player
  108. switch(irandom(3)){ //4 possible quadrants
  109.     case 0:
  110.         t[floor(w/6),floor(h/6)] = 'start'; //top left - start
  111.         t[floor(w*5/6)-3+irandom(6),floor(h*5/6)-3+irandom(6)] = 'goal'; //bottom right - goal
  112.     break;
  113.     case 1:
  114.         t[floor(w*5/6),floor(h/6)] = 'start'; //top right - start
  115.         t[floor(w/6)-3+irandom(6),floor(h*5/6)-3+irandom(6)] = 'goal'; //bottom left - goal
  116.     break;
  117.     case 2:
  118.         t[floor(w/6),floor(h*5/6)] = 'start'; //bottom left - start
  119.         t[floor(w*5/6)-3+irandom(6),floor(h/6)-3+irandom(6)] = 'goal'; //top right - goal
  120.     break;
  121.     case 3:
  122.         t[floor(w*5/6),floor(h*5/6)] = 'start'; //bottom right - start
  123.         t[floor(w/6)-3+irandom(6),floor(w/6)-3+irandom(6)] = 'goal'; //top left - goal
  124.     break;
  125. }
  126.  
  127. //check if the generated terrain is valid, and if not, fix it.
  128. for(var i = 0;i<h;i++){
  129.     for(var j = 0;j<w;j++){
  130.         //read square type
  131.         switch(t[i,j]){
  132.             case 'start':
  133.                 //WE NEED TO GO DEEPER
  134.                 for(var k = i-1;k<=i+1;k++){
  135.                     for(var l = j-1;l<=j+1;l++){
  136.                         //clear all the surrounding terrain
  137.                         //of the starting point.
  138.                         //don't apply to self
  139.                         if(!(k==i and l==j)) t[k,l] = 'normal';
  140.                     }
  141.                 }
  142.             break;
  143.             case 'goal':
  144.                 //WE NEED TO GO DEEPER
  145.                 for(var k = i-1;k<=i+1;k++){
  146.                     for(var l = j-1;l<=j+1;l++){
  147.                         //count abyss
  148.                         var n = 0;
  149.                         if(t[k,l] == 'abyss') n++;
  150.                     }
  151.                 }
  152.                 //if there is too much abyss around, re-generate it
  153.                 if(n>7){
  154.                     for(var k = i-1;k<=i+1;k++){
  155.                         for(var l = j-1;l<=j+1;l++){
  156.                             t[k,l] = p5_get_square();
  157.                         }
  158.                     }
  159.                     //reset row search
  160.                     j=0;
  161.                 }
  162.             break;
  163.             case 'cactus':
  164.             case 'water':
  165.             case 'shadow':
  166.             case 'scavenge':
  167.                 //WE NEED TO GO DEEPER
  168.                 for(var k = i-1;k<=i+1;k++){
  169.                     for(var l = j-1;l<=j+1;l++){
  170.                         //if the terrain has one of the same type
  171.                         //in his 9 grid area, re-generate that square
  172.                         if(!(k==i and l==j) and k>0 and l>0 and k<array_height_2d(t) and l<array_length_2d(t,i)){ //don't check self
  173.                             if(t[i,j]==t[k,l]){
  174.                                 t[i,j] = p5_get_square();
  175.                                 //reset 9 grid search
  176.                                 k=i-1;
  177.                                 l=j-1;
  178.                             }
  179.                         }
  180.                     }
  181.                 }
  182.             break;
  183.             //don't let be more than 50% abyss in a row
  184.             case 'abyss':
  185.                 var n = 0;
  186.                 for(var k = 0;k<array_length_2d(t,i);k++) if(t[i,k]=='abyss') n++;
  187.                 if(array_length_2d(t,i)/n<2){
  188.                     t[i,j] = p5_get_square();
  189.                     //reset row search
  190.                     k=0;
  191.                 }
  192.             break;
  193.         }
  194.     }
  195. }
  196.    
  197. return t;
  198. //***************************************//
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement