GenisysPro  for Minecraft PE/Windows 10 v1.1.x
Feature-rich server software for Minecraft PE and Windows 10 Edition
BlockIterator Class Reference

Public Member Functions

 __construct (Level $level, Vector3 $start, Vector3 $direction, $yOffset=0, $maxDistance=0)
 
 next ()
 
 current ()
 
 rewind ()
 
 key ()
 
 valid ()
 

Detailed Description

This class performs ray tracing and iterates along blocks on a line

Constructor & Destructor Documentation

◆ __construct()

__construct ( Level  $level,
Vector3  $start,
Vector3  $direction,
  $yOffset = 0,
  $maxDistance = 0 
)

BlockIterator constructor.

Parameters
Level$level
Vector3$start
Vector3$direction
int$yOffset
int$maxDistance
68  {
69  $this->level = $level;
70  $this->maxDistance = (int) $maxDistance;
71  $this->blockQueue = new \SplFixedArray(3);
72 
73  $startClone = new Vector3($start->x, $start->y, $start->z);
74  $startClone->y += $yOffset;
75 
76  $this->currentDistance = 0;
77 
78  $mainDirection = 0;
79  $secondDirection = 0;
80  $thirdDirection = 0;
81 
82  $mainPosition = 0;
83  $secondPosition = 0;
84  $thirdPosition = 0;
85 
86  $pos = new Vector3($startClone->x, $startClone->y, $startClone->z);
87  $startBlock = $this->level->getBlock(new Vector3(floor($pos->x), floor($pos->y), floor($pos->z)));
88 
89  if($this->getXLength($direction) > $mainDirection){
90  $this->mainFace = $this->getXFace($direction);
91  $mainDirection = $this->getXLength($direction);
92  $mainPosition = $this->getXPosition($direction, $startClone, $startBlock);
93 
94  $this->secondFace = $this->getYFace($direction);
95  $secondDirection = $this->getYLength($direction);
96  $secondPosition = $this->getYPosition($direction, $startClone, $startBlock);
97 
98  $this->thirdFace = $this->getZFace($direction);
99  $thirdDirection = $this->getZLength($direction);
100  $thirdPosition = $this->getZPosition($direction, $startClone, $startBlock);
101  }
102  if($this->getYLength($direction) > $mainDirection){
103  $this->mainFace = $this->getYFace($direction);
104  $mainDirection = $this->getYLength($direction);
105  $mainPosition = $this->getYPosition($direction, $startClone, $startBlock);
106 
107  $this->secondFace = $this->getZFace($direction);
108  $secondDirection = $this->getZLength($direction);
109  $secondPosition = $this->getZPosition($direction, $startClone, $startBlock);
110 
111  $this->thirdFace = $this->getXFace($direction);
112  $thirdDirection = $this->getXLength($direction);
113  $thirdPosition = $this->getXPosition($direction, $startClone, $startBlock);
114  }
115  if($this->getZLength($direction) > $mainDirection){
116  $this->mainFace = $this->getZFace($direction);
117  $mainDirection = $this->getZLength($direction);
118  $mainPosition = $this->getZPosition($direction, $startClone, $startBlock);
119 
120  $this->secondFace = $this->getXFace($direction);
121  $secondDirection = $this->getXLength($direction);
122  $secondPosition = $this->getXPosition($direction, $startClone, $startBlock);
123 
124  $this->thirdFace = $this->getYFace($direction);
125  $thirdDirection = $this->getYLength($direction);
126  $thirdPosition = $this->getYPosition($direction, $startClone, $startBlock);
127  }
128 
129  $d = $mainPosition / $mainDirection;
130  $secondd = $secondPosition - $secondDirection * $d;
131  $thirdd = $thirdPosition - $thirdDirection * $d;
132 
133  $this->secondError = floor($secondd * self::$gridSize);
134  $this->secondStep = round($secondDirection / $mainDirection * self::$gridSize);
135  $this->thirdError = floor($thirdd * self::$gridSize);
136  $this->thirdStep = round($thirdDirection / $mainDirection * self::$gridSize);
137 
138  if($this->secondError + $this->secondStep <= 0){
139  $this->secondError = -$this->secondStep + 1;
140  }
141 
142  if($this->thirdError + $this->thirdStep <= 0){
143  $this->thirdError = -$this->thirdStep + 1;
144  }
145 
146  $lastBlock = $startBlock->getSide(Vector3::getOppositeSide($this->mainFace));
147 
148  if($this->secondError < 0){
149  $this->secondError += self::$gridSize;
150  $lastBlock = $lastBlock->getSide(Vector3::getOppositeSide($this->secondFace));
151  }
152 
153  if($this->thirdError < 0){
154  $this->thirdError += self::$gridSize;
155  $lastBlock = $lastBlock->getSide(Vector3::getOppositeSide($this->thirdFace));
156  }
157 
158  $this->secondError -= self::$gridSize;
159  $this->thirdError -= self::$gridSize;
160 
161  $this->blockQueue[0] = $lastBlock;
162 
163  $this->currentBlock = -1;
164 
165  $this->scan();
166 
167  $startBlockFound = false;
168 
169  for($cnt = $this->currentBlock; $cnt >= 0; --$cnt){
170  if($this->blockEquals($this->blockQueue[$cnt], $startBlock)){
171  $this->currentBlock = $cnt;
172  $startBlockFound = true;
173  break;
174  }
175  }
176 
177  if(!$startBlockFound){
178  throw new \InvalidStateException("Start block missed in BlockIterator");
179  }
180 
181  $this->maxDistanceInt = round($maxDistance / (sqrt($mainDirection ** 2 + $secondDirection ** 2 + $thirdDirection ** 2) / $mainDirection));
182  }

Member Function Documentation

◆ current()

current ( )
Returns
Block
Exceptions
307  {
308  if($this->currentBlockObject === null){
309  throw new \OutOfBoundsException;
310  }
311  return $this->currentBlockObject;
312  }

◆ key()

key ( )
Returns
int
321  {
322  return $this->currentBlock - 1;
323  }

◆ next()

next ( )
292  {
293  $this->scan();
294 
295  if($this->currentBlock <= -1){
296  throw new \OutOfBoundsException;
297  }else{
298  $this->currentBlockObject = $this->blockQueue[$this->currentBlock--];
299  }
300  }

◆ rewind()

rewind ( )
314  {
315  throw new \InvalidStateException("BlockIterator doesn't support rewind()");
316  }

◆ valid()

valid ( )
Returns
bool
328  {
329  $this->scan();
330  return $this->currentBlock !== -1;
331  }

The documentation for this class was generated from the following file: