Thedavedude

Manyland Structure Generator Test

Dec 8th, 2021 (edited)
1,197
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. INTRODUCTION
  3. A script that places the ENTIRE GODDAMN BEE MOVIE! Original code is by me, Dave Dood, but I am fine if players copy it for their own projects.
  4. Hopefully this will encourage players to tinker around with area generators. They are very fun!
  5. If you ever want help coding, I can give you some examples! Come find me or any other player who is adept at coding sometime!
  6. The code below was what I used to make this video: reddit.com/r/Manyland/comments/qmw6is/yep_all_of_it/
  7.  
  8. HOW IT WORKS
  9. I recommend reading up on this right here to get a general understanding for generators, first: manyland.com/info-generator
  10.  
  11. As for how this one in particular works, it uses the coordinates of the blocks it wants to place at to determine which letter from "script" it wants to place.
  12. For instance, at the very center of the world, the generator will have the coordinates, (0, 0), so it will try placing the very first letter in the script, which is the "A" in "According." If you move one block to the right of the center, at (1, 0), the script will try placing the 2nd letter, which will be "c" from "According", and so on.
  13.  
  14. Additionally, the text will NOT be placed if it is too close to the center of the area.
  15. This way, I can set it so the bee movie script appears once you fly far enough away from spawn.
  16. Scroll all the way down if you want to learn how to change the radius where the text doesn't appear!
  17.  
  18. I have also added some comments to help explain what everything does (they begin with //)
  19. */
  20.  
  21.  
  22. var testStructure =`
  23. h..................
  24. hh.............r...
  25. .hh...r.rr....rr...
  26. .hhhhhrrrrrrrrrr...
  27. ..hhhh..rr...rr....
  28. `
  29.  
  30. var blockIDs = {
  31.     'h': '5ee30642f7ffed344a76f350',
  32.     'r': '5ee2f99e413fc934436cc903'
  33. }
  34.  
  35. //Now for the juicy part! This is where the code actually determines how the letters are placed.
  36. //Pretty much all generate functions will start this way.
  37. function generate(data) {
  38.     var placements = [];
  39.     var minSpacing = 10;
  40.     var maxSpacing = 20;
  41.     var Xspacing = 0;
  42.     var Yspacing = 0;
  43.  //When we want to place characters, first we check if data.sectors is defined by doing "if (data.sectors){ ... }"
  44.  //The code in the generator help page doesn't do this, but if you leave it out, the generator can crash sometimes.
  45.  if (data.sectors){
  46.     //Manyland stores the blocks on screen in sectors (similar to chunks in Minecraft) as far as I can tell.
  47.     //This is how the generator accesses each of these sectors for placing blocks in.
  48.     for (var i = 0; i < data.sectors.length; i++) {
  49.         var sector = data.sectors[i];
  50.         log("Sector is:", sector);
  51.         var seed = "x1:" + sector.x1 + "y1:" + sector.y1;
  52.         var randGen = getRandGen(seed);
  53.         Xspacing = randInt(minSpacing, maxSpacing, randGen());
  54.         Yspacing = randInt(minSpacing, maxSpacing, randGen());
  55.         log("Xspacing is: ", Xspacing, "Yspacing is: ", Yspacing);
  56.         //To get every block in the sector, we do two "for" loops to get all blocks along the x and y axis.
  57.         for (var x = sector.x1; x <= sector.x2; x++) {
  58.             for (var y = sector.y1; y <= sector.y2; y++) {
  59.                 if (((y % Yspacing)==0) && ((x % Xspacing)==0) ){
  60.                     var structurePlacements = addStructure(x, y);
  61.                     structurePlacements.forEach(block => {
  62.                         placements.push(block);
  63.                         //log(block);
  64.                     });
  65.                 }//end of if should place structure
  66.                 Xspacing = randInt(minSpacing, maxSpacing, randGen());
  67.                 Yspacing = randInt(minSpacing, maxSpacing, randGen());
  68.             }//end of for y axis
  69.         }//end of for x axis
  70.     }//end of for each data sector
  71.     //Most generator functions will end with the following line.
  72.     return {placements: placements};
  73.   }//end of if data sectors is defined
  74. }//end of generator function
  75.  
  76. //Here's an additional function I made below to help me calculate distance, used to determine if something is inside the no-place circle.
  77. /*Referenced from: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sqrt*/
  78. function calcHypotenuse(a, b) {
  79.   return (Math.sqrt((a * a) + (b * b)));
  80. }
  81.  
  82. function randInt(min, max, randValue){
  83.     //Code referenced from: www.w3schools.com/js/js_random.asp
  84.     return Math.floor(randValue * (max-min)) + min;
  85. }
  86.  
  87. function getRandGen(seed){
  88.     //Code referenced from: www.w3schools.com/js/js_random.asp
  89.     //return Math.floor(Math.random() * (max-min)) + min;
  90.     var randSeed = xmur3(seed);
  91.     return mulberry32(randSeed());
  92. }
  93.  
  94. function addStructure(x, y){
  95.     var returnStruct = [];
  96.     var i = 0;
  97.     var j = 0;
  98.     for (var c = 0; c < testStructure.length; c++){
  99.         if (testStructure[c] == "\n"){
  100.             j++;
  101.             i = 0;
  102.         }//new line found
  103.         i++;
  104.         returnStruct.push({id: blockIDs[testStructure[c]], x: (x + i), y: (y + j), rotation: 0});
  105.     }
  106.     return returnStruct;
  107. }
  108.  
  109. function xmur3(str) {
  110.     for(var i = 0, h = 1779033703 ^ str.length; i < str.length; i++)
  111.         h = Math.imul(h ^ str.charCodeAt(i), 3432918353),
  112.         h = h << 13 | h >>> 19;
  113.     return function() {
  114.         h = Math.imul(h ^ h >>> 16, 2246822507);
  115.         h = Math.imul(h ^ h >>> 13, 3266489909);
  116.         return (h ^= h >>> 16) >>> 0;
  117.     }
  118. }
  119.  
  120. function mulberry32(a) {
  121.     return function() {
  122.       var t = a += 0x6D2B79F5;
  123.       t = Math.imul(t ^ t >>> 15, t | 1);
  124.       t ^= t + Math.imul(t ^ t >>> 7, t | 61);
  125.       return ((t ^ t >>> 14) >>> 0) / 4294967296;
  126.     }
  127. }
  128.  
RAW Paste Data