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

Public Member Functions

 __construct (ChunkManager $level, Vector3 $from, Vector3 $to)
 
 next ()
 
 current ()
 
 rewind ()
 
 key ()
 
 valid ()
 

Detailed Description

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

Constructor & Destructor Documentation

◆ __construct()

__construct ( ChunkManager  $level,
Vector3  $from,
Vector3  $to 
)

VectorIterator constructor.

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

Member Function Documentation

◆ current()

current ( )
Returns
Block
Exceptions
311  {
312  if($this->currentBlockObject === null){
313  throw new \OutOfBoundsException;
314  }
315  return $this->currentBlockObject;
316  }

◆ key()

key ( )
Returns
int
325  {
326  return $this->currentBlock - 1;
327  }

◆ next()

next ( )
296  {
297  $this->scan();
298 
299  if($this->currentBlock <= -1){
300  throw new \OutOfBoundsException;
301  }else{
302  $this->currentBlockObject = $this->positionQueue[$this->currentBlock--];
303  }
304  }

◆ rewind()

rewind ( )
318  {
319  throw new \InvalidStateException("BlockIterator doesn't support rewind()");
320  }

◆ valid()

valid ( )
Returns
bool
332  {
333  $this->scan();
334  return $this->currentBlock !== -1;
335  }

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