Commits

Eric Fredricksen committed c967d23

Move tree planting after chunk is generated (and bring it back into the world)

  • Participants
  • Parent commits 0c412c4

Comments (0)

Files changed (2)

 
   GAME.chunks[this.chunkx + ',' + this.chunkz] = this;
 
-  data.blocks;
-  if (!data.block) 
-    data.blocks = generateChunk(GAME.seed, this.chunkx, this.chunkz).blocks;
+  var blocks = data.blocks;
+  if (!blocks)
+    blocks = generateChunk(GAME.seed, this.chunkx, this.chunkz).blocks;
 
   for (var i = 0; i < NX * NY * NZ; ++i) {
     var c = coords(i);
     c = coords(this.chunkx + c.x, c.y, this.chunkz + c.z);
-    c.data = data.blocks[i];
+    c.data = blocks[i];
     this.blocks[i] = new Block(c, this);
     if (this.blocks[i].dirtyLight || this.blocks[i].dirtyGeometry)
       this.nDirty++;
   }
 
+  if (!data.blocks) {
+    // Chunk just generated. Plant some trees.
+    var margin = 3;
+    for (var n = 0; n < 2; ++n) {
+      var ix = margin + irand(NX - margin * 2);
+      var iz = margin + irand(NZ - margin * 2);
+      var b = topmost(this.chunkx + ix, this.chunkz + iz);
+      if (b && b.type.plantable)
+        buildTree(b.neighbor(FACE_TOP));
+    }
+  }
+
   for (var i in data.entities||{})
     new Entity(data.entities[i]);
 }
   }
 
   // Plant some plants
-  function plant(n, howOrWhat, margin) {
+  function plant(n, what, margin) {
     margin = margin || 0;
     while (n--) {
       var ix = margin + irand(NX - margin * 2);
       var t = top(ix, iz);
       if (t && t.iy < NY-1 && BLOCK_TYPES[t.b.type].plantable) {
         t = block(ix, t.iy+1, iz);
-        if (typeof howOrWhat === 'function')
-          howOrWhat(t);
-        else
-          t.type = howOrWhat;
+        t.type = what;
       }
     }
   }
   plant(4, 'flower');
-  //plant(2, buildTree, 3);
   plant(6, 'weeds');
   
   // Initial quick lighting update, some of which we can know accurately