Skip to content

Commit

Permalink
Fix #214 (Shapes may have an additional block)
Browse files Browse the repository at this point in the history
i.e. diameter 17 placed on 24:24 would go from 16:16 to 32:32, right into the next chunk
TODO: i noticed that some calculations are off (i.e. Sphere total count)
  • Loading branch information
inxomnyaa committed Dec 4, 2020
1 parent a7963af commit e3ef7b2
Show file tree
Hide file tree
Showing 9 changed files with 184 additions and 185 deletions.
4 changes: 2 additions & 2 deletions src/xenialdan/MagicWE2/selection/shape/Cone.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ public function getLayer($manager, int $flags = API::FLAG_BASE): Generator
public function getTouchedChunks($manager): array
{//TODO optimize to remove "corner" chunks
$this->validateChunkManager($manager);
$maxX = $this->getMaxVec3()->x >> 4;
$maxX = ($this->getMaxVec3()->x + 1) >> 4;
$minX = $this->getMinVec3()->x >> 4;
$maxZ = $this->getMaxVec3()->z >> 4;
$maxZ = ($this->getMaxVec3()->z + 1) >> 4;
$minZ = $this->getMinVec3()->z >> 4;
$touchedChunks = [];
for ($x = $minX; $x <= $maxX; $x++) {
Expand Down
26 changes: 13 additions & 13 deletions src/xenialdan/MagicWE2/selection/shape/Cube.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,21 +81,21 @@ public function getLayer($manager, int $flags = API::FLAG_BASE): Generator
*/
public function getTouchedChunks($manager): array
{
$this->validateChunkManager($manager);
$maxX = $this->getMaxVec3()->x >> 4;
$minX = $this->getMinVec3()->x >> 4;
$maxZ = $this->getMaxVec3()->z >> 4;
$minZ = $this->getMinVec3()->z >> 4;
$touchedChunks = [];
for ($x = $minX; $x <= $maxX; $x++) {
for ($z = $minZ; $z <= $maxZ; $z++) {
$chunk = $manager->getChunk($x, $z);
if ($chunk === null) {
continue;
}
$this->validateChunkManager($manager);
$maxX = ($this->getMaxVec3()->x + 1) >> 4;
$minX = $this->getMinVec3()->x >> 4;
$maxZ = ($this->getMaxVec3()->z + 1) >> 4;
$minZ = $this->getMinVec3()->z >> 4;
$touchedChunks = [];
for ($x = $minX; $x <= $maxX; $x++) {
for ($z = $minZ; $z <= $maxZ; $z++) {
$chunk = $manager->getChunk($x, $z);
if ($chunk === null) {
continue;
}
print "Touched Chunk at: $x:$z" . PHP_EOL;
$touchedChunks[World::chunkHash($x, $z)] = FastChunkSerializer::serialize($chunk);
}
}
}
print "Touched chunks count: " . count($touchedChunks) . PHP_EOL;
return $touchedChunks;
Expand Down
28 changes: 14 additions & 14 deletions src/xenialdan/MagicWE2/selection/shape/Cuboid.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,21 +103,21 @@ public function getLayer($manager, int $flags = API::FLAG_BASE): Generator
*/
public function getTouchedChunks($manager): array
{
$this->validateChunkManager($manager);
$maxX = $this->getMaxVec3()->x >> 4;
$minX = $this->getMinVec3()->x >> 4;
$maxZ = $this->getMaxVec3()->z >> 4;
$minZ = $this->getMinVec3()->z >> 4;
$touchedChunks = [];
for ($x = $minX; $x <= $maxX; $x++) {
for ($z = $minZ; $z <= $maxZ; $z++) {
$chunk = $manager->getChunk($x, $z);
if ($chunk === null) {
continue;
}
$this->validateChunkManager($manager);
$maxX = ($this->getMaxVec3()->x + 1) >> 4;
$minX = $this->getMinVec3()->x >> 4;
$maxZ = ($this->getMaxVec3()->z + 1) >> 4;
$minZ = $this->getMinVec3()->z >> 4;
$touchedChunks = [];
for ($x = $minX; $x <= $maxX; $x++) {
for ($z = $minZ; $z <= $maxZ; $z++) {
$chunk = $manager->getChunk($x, $z);
if ($chunk === null) {
continue;
}
$touchedChunks[World::chunkHash($x, $z)] = FastChunkSerializer::serialize($chunk);
}
}
}
}
return $touchedChunks;
}

Expand Down
131 changes: 65 additions & 66 deletions src/xenialdan/MagicWE2/selection/shape/Custom.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,24 @@ public function __construct(Vector3 $pasteVector, array $positions)
{
$this->pasteVector = $pasteVector;
$this->positions = $positions;
}
}

/**
/**
* Returns the blocks by their actual position
* @param World|AsyncChunkManager $manager The world or AsyncChunkManager
* @param Block[] $filterblocks If not empty, applying a filter on the block list
* @param int $flags
* @return Generator|Block[]
* @throws Exception
*/
public function getBlocks($manager, array $filterblocks = [], int $flags = API::FLAG_BASE): Generator
{
$this->validateChunkManager($manager);
foreach ($this->positions as $position) {
public function getBlocks($manager, array $filterblocks = [], int $flags = API::FLAG_BASE): Generator
{
$this->validateChunkManager($manager);
foreach ($this->positions as $position) {
//TODO filterblocks
yield API::setComponents($manager->getBlockAt($position->getFloorX(), $position->getFloorY(), $position->getFloorZ()), (int)$position->x, (int)$position->y, (int)$position->z);
}
}
}

/**
* Returns a flat layer of all included x z positions in selection
Expand All @@ -53,69 +53,68 @@ public function getBlocks($manager, array $filterblocks = [], int $flags = API::
* @return Generator|Vector2[]
* @throws Exception
*/
public function getLayer($manager, int $flags = API::FLAG_BASE): Generator
{
$this->validateChunkManager($manager);
/* Mapping: $walked[$hash]=true */
$walked = [];
foreach ($this->positions as $position) {
$hash = World::chunkHash($position->getFloorX(), $position->getFloorZ());
if (isset($walked[$hash])) continue;
$walked[$hash] = true;
yield new Vector2($position->x, $position->z);
}
}
public function getLayer($manager, int $flags = API::FLAG_BASE): Generator
{
$this->validateChunkManager($manager);
/* Mapping: $walked[$hash]=true */
$walked = [];
foreach ($this->positions as $position) {
$hash = World::chunkHash($position->getFloorX(), $position->getFloorZ());
if (isset($walked[$hash])) continue;
$walked[$hash] = true;
yield new Vector2($position->x, $position->z);
}
}

/**
* @param World|AsyncChunkManager $manager
* @return string[] fastSerialized chunks
* @throws Exception
*/
public function getTouchedChunks($manager): array
{
$this->validateChunkManager($manager);
$touchedChunks = [];
foreach ($this->getLayer($manager) as $vector2) {
$x = $vector2->getFloorX() >> 4;
$z = $vector2->getFloorY() >> 4;
$chunk = $manager->getChunk($x, $z);
if ($chunk === null) {
continue;
}
/**
* @param World|AsyncChunkManager $manager
* @return string[] fastSerialized chunks
* @throws Exception
*/
public function getTouchedChunks($manager): array
{
$this->validateChunkManager($manager);
$touchedChunks = [];
foreach ($this->getLayer($manager) as $vector2) {
$x = $vector2->getFloorX() >> 4;
$z = $vector2->getFloorY() >> 4;
if (isset($touchedChunks[World::chunkHash($x, $z)]) || ($chunk = $manager->getChunk($x, $z)) === null) {
continue;
}
print "Touched Chunk at: $x:$z" . PHP_EOL;
$touchedChunks[World::chunkHash($x, $z)] = FastChunkSerializer::serialize($chunk);
}
print "Touched chunks count: " . count($touchedChunks) . PHP_EOL;
return $touchedChunks;
}
}
print "Touched chunks count: " . count($touchedChunks) . PHP_EOL;
return $touchedChunks;
}

public function getAABB(): AxisAlignedBB
{
$minX = $maxX = $minY = $maxY = $minZ = $maxZ = null;
foreach ($this->positions as $position) {
if (is_null($minX)) {
$minX = $maxX = $position->x;
$minY = $maxY = $position->y;
$minZ = $maxZ = $position->z;
continue;
}
$minX = min($minX, $position->x);
$minY = min($minY, $position->y);
$minZ = min($minZ, $position->z);
$maxX = max($maxX, $position->x);
$maxY = max($maxY, $position->y);
$maxZ = max($maxZ, $position->z);
}
return new AxisAlignedBB($minX, $minY, $minZ, $maxX, $maxY, $maxZ);
}
public function getAABB(): AxisAlignedBB
{
$minX = $maxX = $minY = $maxY = $minZ = $maxZ = null;
foreach ($this->positions as $position) {
if (is_null($minX)) {
$minX = $maxX = $position->x;
$minY = $maxY = $position->y;
$minZ = $maxZ = $position->z;
continue;
}
$minX = min($minX, $position->x);
$minY = min($minY, $position->y);
$minZ = min($minZ, $position->z);
$maxX = max($maxX, $position->x);
$maxY = max($maxY, $position->y);
$maxZ = max($maxZ, $position->z);
}
return new AxisAlignedBB($minX, $minY, $minZ, $maxX, $maxY, $maxZ);
}

public function getTotalCount(): int
{
return count($this->positions);
}
public function getTotalCount(): int
{
return count($this->positions);
}

public static function getName(): string
{
return "Custom";
}
public static function getName(): string
{
return "Custom";
}
}
26 changes: 13 additions & 13 deletions src/xenialdan/MagicWE2/selection/shape/Cylinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,21 +98,21 @@ public function getLayer($manager, int $flags = API::FLAG_BASE): Generator
*/
public function getTouchedChunks($manager): array
{//TODO optimize to remove "corner" chunks
$this->validateChunkManager($manager);
$maxX = $this->getMaxVec3()->x >> 4;
$minX = $this->getMinVec3()->x >> 4;
$maxZ = $this->getMaxVec3()->z >> 4;
$minZ = $this->getMinVec3()->z >> 4;
$touchedChunks = [];
for ($x = $minX; $x <= $maxX; $x++) {
for ($z = $minZ; $z <= $maxZ; $z++) {
$chunk = $manager->getChunk($x, $z);
if ($chunk === null) {
continue;
}
$this->validateChunkManager($manager);
$maxX = ($this->getMaxVec3()->x + 1) >> 4;
$minX = $this->getMinVec3()->x >> 4;
$maxZ = ($this->getMaxVec3()->z + 1) >> 4;
$minZ = $this->getMinVec3()->z >> 4;
$touchedChunks = [];
for ($x = $minX; $x <= $maxX; $x++) {
for ($z = $minZ; $z <= $maxZ; $z++) {
$chunk = $manager->getChunk($x, $z);
if ($chunk === null) {
continue;
}
print "Touched Chunk at: $x:$z" . PHP_EOL;
$touchedChunks[World::chunkHash($x, $z)] = FastChunkSerializer::serialize($chunk);
}
}
}
print "Touched chunks count: " . count($touchedChunks) . PHP_EOL;
return $touchedChunks;
Expand Down
26 changes: 13 additions & 13 deletions src/xenialdan/MagicWE2/selection/shape/Ellipsoid.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,21 +126,21 @@ public function getLayer($manager, int $flags = API::FLAG_BASE): Generator
*/
public function getTouchedChunks($manager): array
{//TODO optimize to remove "corner" chunks
$this->validateChunkManager($manager);
$maxX = $this->getMaxVec3()->x >> 4;
$minX = $this->getMinVec3()->x >> 4;
$maxZ = $this->getMaxVec3()->z >> 4;
$minZ = $this->getMinVec3()->z >> 4;
$touchedChunks = [];
for ($x = $minX - 1; $x <= $maxX + 1; $x++) {
for ($z = $minZ - 1; $z <= $maxZ + 1; $z++) {
$chunk = $manager->getChunk($x, $z);
if ($chunk === null) {
continue;
}
$this->validateChunkManager($manager);
$maxX = ($this->getMaxVec3()->x + 1) >> 4;
$minX = $this->getMinVec3()->x >> 4;
$maxZ = ($this->getMaxVec3()->z + 1) >> 4;
$minZ = $this->getMinVec3()->z >> 4;
$touchedChunks = [];
for ($x = $minX - 1; $x <= $maxX + 1; $x++) {
for ($z = $minZ - 1; $z <= $maxZ + 1; $z++) {
$chunk = $manager->getChunk($x, $z);
if ($chunk === null) {
continue;
}
print "Touched Chunk at: $x:$z" . PHP_EOL;
$touchedChunks[World::chunkHash($x, $z)] = FastChunkSerializer::serialize($chunk);
}
}
}
print "Touched chunks count: " . count($touchedChunks) . PHP_EOL;
return $touchedChunks;
Expand Down
26 changes: 13 additions & 13 deletions src/xenialdan/MagicWE2/selection/shape/Pyramid.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,21 +114,21 @@ public function getLayer($manager, int $flags = API::FLAG_BASE): Generator
*/
public function getTouchedChunks($manager): array
{//TODO optimize to remove "corner" chunks
$this->validateChunkManager($manager);
$maxX = $this->getMaxVec3()->x >> 4;
$minX = $this->getMinVec3()->x >> 4;
$maxZ = $this->getMaxVec3()->z >> 4;
$minZ = $this->getMinVec3()->z >> 4;
$touchedChunks = [];
for ($x = $minX; $x <= $maxX; $x++) {
for ($z = $minZ; $z <= $maxZ; $z++) {
$chunk = $manager->getChunk($x, $z);
if ($chunk === null) {
continue;
}
$this->validateChunkManager($manager);
$maxX = ($this->getMaxVec3()->x + 1) >> 4;
$minX = $this->getMinVec3()->x >> 4;
$maxZ = ($this->getMaxVec3()->z + 1) >> 4;
$minZ = $this->getMinVec3()->z >> 4;
$touchedChunks = [];
for ($x = $minX; $x <= $maxX; $x++) {
for ($z = $minZ; $z <= $maxZ; $z++) {
$chunk = $manager->getChunk($x, $z);
if ($chunk === null) {
continue;
}
print "Touched Chunk at: $x:$z" . PHP_EOL;
$touchedChunks[World::chunkHash($x, $z)] = FastChunkSerializer::serialize($chunk);
}
}
}
print "Touched chunks count: " . count($touchedChunks) . PHP_EOL;
return $touchedChunks;
Expand Down
Loading

0 comments on commit e3ef7b2

Please sign in to comment.