PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/misc/world/generator/object/tree/SpruceTreeObject.php

https://github.com/domanicYL/PocketMine-MP
PHP | 88 lines | 55 code | 9 blank | 24 comment | 14 complexity | e82dc534ad7327302c347ab7f71b147f MD5 | raw file
Possible License(s): LGPL-3.0
  1. <?php
  2. /*
  3. -
  4. / \
  5. / \
  6. / PocketMine \
  7. / MP \
  8. |\ @shoghicp /|
  9. |. \ / .|
  10. | .. \ / .. |
  11. | .. | .. |
  12. | .. | .. |
  13. \ | /
  14. \ | /
  15. \ | /
  16. \ | /
  17. This program is free software: you can redistribute it and/or modify
  18. it under the terms of the GNU Lesser General Public License as published by
  19. the Free Software Foundation, either version 3 of the License, or
  20. (at your option) any later version.
  21. */
  22. require_once("misc/world/generator/object/tree/TreeObject.php");
  23. class SpruceTreeObject extends TreeObject{
  24. var $type = 1;
  25. private $totalHeight = 8;
  26. private $leavesBottomY = -1;
  27. private $leavesMaxRadius = -1;
  28. public function canPlaceObject(LevelAPI $level, $x, $y, $z){
  29. $this->findRandomLeavesSize();
  30. $checkRadius = 0;
  31. for($yy = 0; $yy < $this->totalHeight + 2; ++$yy) {
  32. if($yy === $this->leavesBottomY) {
  33. $checkRadius = $this->leavesMaxRadius;
  34. }
  35. for($xx = -$checkRadius; $xx < ($checkRadius + 1); ++$xx){
  36. for($zz = -$checkRadius; $zz < ($checkRadius + 1); ++$zz){
  37. $block = $level->getBlock($x + $xx, $y + $yy, $z + $zz);
  38. if(!isset($this->overridable[$block[0]])){
  39. return false;
  40. }
  41. }
  42. }
  43. }
  44. return true;
  45. }
  46. private function findRandomLeavesSize(){
  47. $this->totalHeight += mt_rand(-1, 2);
  48. $this->leavesBottomY = (int) ($this->totalHeight - mt_rand(1,2) - 3);
  49. $this->leavesMaxRadius = 1 + mt_rand(0, 1);
  50. }
  51. public function placeObject(LevelAPI $level, $x, $y, $z){
  52. if($this->leavesBottomY === -1 or $this->leavesMaxRadius === -1) {
  53. $this->findRandomLeavesSize();
  54. }
  55. $level->setBlock($x, $y - 1, $z, 3, 0);
  56. $leavesRadius = 0;
  57. for($yy = $this->totalHeight; $yy >= $this->leavesBottomY; --$yy){
  58. for ($xx = -$leavesRadius; $xx < ($leavesRadius + 1); ++$xx) {
  59. for ($zz = -$leavesRadius; $zz < ($leavesRadius + 1); ++$zz) {
  60. if (abs($xx) != $leavesRadius or abs($zz) != $leavesRadius or $leavesRadius <= 0) {
  61. $level->setBlock($x + $xx, $y + $yy, $z + $zz, 18, $this->type);
  62. }
  63. }
  64. }
  65. if ($leavesRadius > 0 and $yy === ($y + $this->leavesBottomY + 1)) {
  66. --$leavesRadius;
  67. }elseif($leavesRadius < $this->leavesMaxRadius){
  68. ++$leavesRadius;
  69. }
  70. }
  71. for($yy = 0; $yy < ($this->totalHeight - 1); ++$yy){
  72. $level->setBlock($x, $y + $yy, $z, 17, $this->type);
  73. }
  74. }
  75. }