PocketMine-MP  1.4 - API 1.10.0
 All Classes Namespaces Functions Variables Pages
SmallTree.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 SmallTree extends Tree{
29  public $type = 0;
30  private $trunkHeight = 5;
31  private static $leavesHeight = 4; // All trees appear to be 4 tall
32  private static $leafRadii = [1, 1.41, 2.83, 2.24];
33 
34  private $addLeavesVines = false;
35  private $addLogVines = false;
36  private $addCocoaPlants = false;
37 
38  public function canPlaceObject(ChunkManager $level, $x, $y, $z, Random $random){
39  $radiusToCheck = 0;
40  for($yy = 0; $yy < $this->trunkHeight + 3; ++$yy){
41  if($yy == 1 or $yy === $this->trunkHeight){
42  ++$radiusToCheck;
43  }
44  for($xx = -$radiusToCheck; $xx < ($radiusToCheck + 1); ++$xx){
45  for($zz = -$radiusToCheck; $zz < ($radiusToCheck + 1); ++$zz){
46  if(!isset($this->overridable[$level->getBlockIdAt($x + $xx, $y + $yy, $z + $zz)])){
47  return false;
48  }
49  }
50  }
51  }
52 
53  return true;
54  }
55 
56  public function placeObject(ChunkManager $level, $x, $y, $z, Random $random){
57  // The base dirt block
58  $level->setBlockIdAt($x, $y, $z, Block::DIRT);
59 
60  // Adjust the tree trunk's height randomly
61  // plot [-14:11] int( x / 8 ) + 5
62  // - min=4 (all leaves are 4 tall, some trunk must show)
63  // - max=6 (top leaves are within ground-level whacking range
64  // on all small trees)
65  $heightPre = $random->nextRange(-14, 11);
66  $this->trunkHeight = intval($heightPre / 8) + 5;
67 
68  // Adjust the starting leaf density using the trunk height as a
69  // starting position (tall trees with skimpy leaves don't look
70  // too good)
71  $leafPre = $random->nextRange($this->trunkHeight, 10) / 20; // (TODO: seed may apply)
72 
73  // Now build the tree (from the top down)
74  $leaflevel = 0;
75  for($yy = ($this->trunkHeight + 1); $yy >= 0; --$yy){
76  if($leaflevel < self::$leavesHeight){
77  // The size is a slight variation on the trunkheight
78  $radius = self::$leafRadii[$leaflevel] + $leafPre;
79  $bRadius = 3;
80  for($xx = -$bRadius; $xx <= $bRadius; ++$xx){
81  for($zz = -$bRadius; $zz <= $bRadius; ++$zz){
82  if(sqrt($xx ** 2 + $zz ** 2) <= $radius){
83  $level->setBlockIdAt($x + $xx, $y + $yy, $z + $zz, Block::LEAVES);
84  $level->setBlockDataAt($x + $xx, $y + $yy, $z + $zz, $this->type);
85  }
86  }
87  }
88  $leaflevel++;
89  }
90 
91  // Place the trunk last
92  if($leaflevel > 1){
93  $level->setBlockIdAt($x, $y + $yy, $z, Block::TRUNK);
94  $level->setBlockDataAt($x, $y + $yy, $z, $this->type);
95  }
96  }
97  }
98 }
setBlockIdAt($x, $y, $z, $id)
setBlockDataAt($x, $y, $z, $data)
nextRange($start=0, $end=0x7fffffff)
Definition: Random.php:112