Commits

Eric Fredricksen committed 4e17c61

Refactored/encapsulated/rationalized plantings

Comments (0)

Files changed (1)

     var x = xi + this.chunkx;
     for (var zi = 0; zi < NZ; ++zi) {
       var z = zi + this.chunkz;
-      if (2 * noise(x/10,9938,z/10) + noise(x/1,9938,z/1) < -0.4) {
+      if (2 * noise(x/10,9938,z/10) + noise(x/1,9938,z/1) < -0.5) {
         var t = topmost(x, z);
         if (t && t.type.plantable)
           t.neighbor(FACE_TOP).type = BLOCK_TYPES.soybeans;
     }
   }
 
-  // Plant some flowers
-  for (var n = 0; n < 4; ++n) {
-    var x = this.chunkx + 
-      Math.round(Math.abs(noise(this.chunkx, this.chunkz, n)) * NX);
-    var z = this.chunkz + 
-      Math.round(Math.abs(noise(this.chunkx, this.chunkz, n + 23.4)) * NZ);
-    var t = topmost(x, z);
-    if (t && t.type.plantable)
-      t.neighbor(FACE_TOP).type = BLOCK_TYPES.flower;
-  }
-
-  // Plant some trees
-  for (var n = 0; n < 1; ++n) {
-    var x = this.chunkx + 3 +
-      Math.round(Math.abs(noise(this.chunkx, this.chunkz, n+981.1)) * (NX-6));
-    var z = this.chunkz + 3 +
-      Math.round(Math.abs(noise(this.chunkx, this.chunkz, n+123.4)) *(NZ-6));
-    var t = topmost(x, z);
-    if (t && t.type.plantable)
-      buildTree(t.neighbor(FACE_TOP));
-  }
-
-  // Plant some weeds
-  for (var n = 0; n < 6; ++n) {
-    var x = this.chunkx + 
-      Math.round(Math.abs(noise(this.chunkx, this.chunkz, n)) * NX);
-    var z = this.chunkz + 
-      Math.round(Math.abs(noise(this.chunkx, this.chunkz, n + 23.4)) * NZ);
-    var t = topmost(x, z);
-    if (t && t.type.plantable)
-      t.neighbor(FACE_TOP).type = BLOCK_TYPES.weeds;
+  // Plant some plants
+  var chunk = this;
+  function plant(n, howOrWhat, margin) {
+    margin = margin || 0;
+    while (n--) {
+      var x = chunk.chunkx + margin + irand(NX - margin * 2);
+      var z = chunk.chunkz + margin + irand(NZ - margin * 2);
+      var t = topmost(x, z);
+      if (t && t.type.plantable) {
+        t = t.neighbor(FACE_TOP);
+        if (typeof howOrWhat === 'function')
+          howOrWhat(t);
+        else
+          t.type = BLOCK_TYPES[howOrWhat];
+      }
+    }
   }
-
+  plant(4, 'flower');
+  plant(2, buildTree, 3);
+  plant(6, 'weeds');
+  
   // Initial quick lighting update, some of which we can know accurately
   this.nDirty = 0;
   for (var x = 0; x < NX; ++x) {
 
 function buildTree(base) {
   var r0 = 0.6, r1 = 0.2;
-  var h = Math.random() * 5 + 5;
+  var h = Math.random() * 7 + 3;
   var below;
   for (var i = 0; i < h && !base.outofbounds; ++i) {
     base.placeBlock(BLOCK_TYPES.log);
     indices: [],
   };
 
-  var cx0 = noise(b.x, 234.567 +  b.y   /10, b.z) % (0.5 - b.radius0) + 0.5;
-  var cx1 = noise(b.x, 234.567 + (b.y+1)/10, b.z) % (0.5 - b.radius1) + 0.5;
-  var cz0 = noise(b.x, 123.456 +  b.y   /10, b.z) % (0.5 - b.radius0) + 0.5;
-  var cz1 = noise(b.x, 123.456 + (b.y+1)/10, b.z) % (0.5 - b.radius1) + 0.5;
+  var SW = 15;
+  var cx0 = noise(b.x, 234.567 +  b.y   /SW, b.z) % (0.5 - b.radius0) + 0.5;
+  var cx1 = noise(b.x, 234.567 + (b.y+1)/SW, b.z) % (0.5 - b.radius1) + 0.5;
+  var cz0 = noise(b.x, 123.456 +  b.y   /SW, b.z) % (0.5 - b.radius0) + 0.5;
+  var cz1 = noise(b.x, 123.456 + (b.y+1)/SW, b.z) % (0.5 - b.radius1) + 0.5;
 
   function sq(cx, y, cz, r) {
     return [[cx - r, y, cz - r],
   return Math.floor(Math.random() * (max + 1));
 }
 
+function irand(n) {
+  return Math.floor(Math.random() * n);
+}
+
 var _AUDIO_CONTEXT;
 function Sound(sound) {
   var that = this;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.