PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
SpruceTree.php
1 <?php
2 
3 /*
4  *
5  * ____ _ _ __ __ _ __ __ ____
6  * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7  * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8  * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9  * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10  *
11  * This program is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation, either version 3 of the License, or
14  * (at your option) any later version.
15  *
16  * @author PocketMine Team
17  * @link http://www.pocketmine.net/
18  *
19  *
20 */
21 
22 namespace pocketmine\level\generator\object;
23 
27 
28 class SpruceTree extends Tree{
29  var $type = 1;
30  private $totalHeight = 8;
31  private $leavesBottomY = -1;
32  private $leavesMaxRadius = -1;
33 
34  public function canPlaceObject(ChunkManager $level, $x, $y, $z, Random $random){
35  $this->findRandomLeavesSize($random);
36  $checkRadius = 0;
37  for($yy = 0; $yy < $this->totalHeight + 2; ++$yy){
38  if($yy === $this->leavesBottomY){
39  $checkRadius = $this->leavesMaxRadius;
40  }
41  for($xx = -$checkRadius; $xx < ($checkRadius + 1); ++$xx){
42  for($zz = -$checkRadius; $zz < ($checkRadius + 1); ++$zz){
43  if(!isset($this->overridable[$level->getBlockIdAt($x + $xx, $y + $yy, $z + $zz)])){
44  return false;
45  }
46  }
47  }
48  }
49 
50  return true;
51  }
52 
53  private function findRandomLeavesSize(Random $random){
54  $this->totalHeight += $random->nextRange(-1, 2);
55  $this->leavesBottomY = (int) ($this->totalHeight - $random->nextRange(1, 2) - 3);
56  $this->leavesMaxRadius = 1 + $random->nextRange(0, 1);
57  }
58 
59  public function placeObject(ChunkManager $level, $x, $y, $z, Random $random){
60  if($this->leavesBottomY === -1 or $this->leavesMaxRadius === -1){
61  $this->findRandomLeavesSize($random);
62  }
63  $level->setBlockIdAt($x, $y - 1, $z, Block::DIRT);
64  $leavesRadius = 0;
65  for($yy = $this->totalHeight; $yy >= $this->leavesBottomY; --$yy){
66  for($xx = -$leavesRadius; $xx <= $leavesRadius; ++$xx){
67  for($zz = -$leavesRadius; $zz <= $leavesRadius; ++$zz){
68  if(abs($xx) != $leavesRadius or abs($zz) != $leavesRadius or $leavesRadius <= 0){
69  $level->setBlockIdAt($x + $xx, $y + $yy, $z + $zz, Block::LEAVES);
70  $level->setBlockDataAt($x + $xx, $y + $yy, $z + $zz, $this->type);
71  }
72  }
73  }
74  if($leavesRadius > 0 and $yy === ($y + $this->leavesBottomY + 1)){
75  --$leavesRadius;
76  }elseif($leavesRadius < $this->leavesMaxRadius){
77  ++$leavesRadius;
78  }
79  }
80  for($yy = 0; $yy < ($this->totalHeight - 1); ++$yy){
81  $level->setBlockIdAt($x, $y + $yy, $z, Block::TRUNK);
82  $level->setBlockDataAt($x, $y + $yy, $z, $this->type);
83  }
84  }
85 
86 
87 }
setBlockIdAt($x, $y, $z, $id)
setBlockDataAt($x, $y, $z, $data)
nextRange($start=0, $end=0x7fffffff)
Definition: Random.php:112