From 7187c658ca8205fe4c3cb9a160e347b4eb2b056c Mon Sep 17 00:00:00 2001 From: EmreTr1 Date: Thu, 21 Mar 2019 00:17:10 +0700 Subject: [PATCH] Some fixes for maps --- src/pocketmine/item/Map.php | 24 +-- src/pocketmine/maps/MapData.php | 59 +++++--- src/pocketmine/maps/MapInfo.php | 23 +-- src/pocketmine/maps/MapManager.php | 2 + src/pocketmine/maps/MapRenderer.php | 24 --- src/pocketmine/maps/renderer/MapRenderer.php | 39 +++++ .../{ => renderer}/VanillaMapRenderer.php | 140 ++++++++++-------- 7 files changed, 182 insertions(+), 129 deletions(-) delete mode 100644 src/pocketmine/maps/MapRenderer.php create mode 100644 src/pocketmine/maps/renderer/MapRenderer.php rename src/pocketmine/maps/{ => renderer}/VanillaMapRenderer.php (85%) diff --git a/src/pocketmine/item/Map.php b/src/pocketmine/item/Map.php index 8424d39b8..94c2baa43 100644 --- a/src/pocketmine/item/Map.php +++ b/src/pocketmine/item/Map.php @@ -24,6 +24,9 @@ use pocketmine\maps\MapData; use pocketmine\maps\MapManager; +use pocketmine\nbt\tag\ByteTag; +use pocketmine\nbt\tag\LongTag; +use pocketmine\nbt\tag\IntTag; use pocketmine\Player; class Map extends Item{ @@ -35,10 +38,6 @@ class Map extends Item{ public function __construct(int $meta = 0){ parent::__construct(self::FILLED_MAP, $meta, "Map"); - - if($this->isMapInit()){ - MapManager::loadMapData($this->getMapId()); - } } public function getMapData() : ?MapData{ @@ -46,11 +45,12 @@ public function getMapData() : ?MapData{ } public function onUpdate(Player $player) : void{ - if($data = $this->getMapData()){ - $data->renderMap($player); - $player->sendTip("messaaage"); + if($this->isMapInit()){ + if($data = $this->getMapData()){ + $data->renderMap($player); - $data->updateVisiblePlayers($player, $this); + $data->updateVisiblePlayers($player, $this); + } } } @@ -75,7 +75,7 @@ public function getMaxStackSize() : int{ } public function setMapId(int $mapId) : void{ - $this->getNamedTag()->setLong(self::TAG_MAP_UUID, $mapId); + $this->setNamedTagEntry(new LongTag(self::TAG_MAP_UUID, $mapId)); } public function getMapId() : int{ @@ -83,7 +83,7 @@ public function getMapId() : int{ } public function setMapNameIndex(int $nameIndex) : void{ - $this->getNamedTag()->setInt(self::TAG_MAP_NAME_INDEX, $nameIndex); + $this->setNamedTagEntry(new IntTag(self::TAG_MAP_NAME_INDEX, $nameIndex)); } public function getMapNameIndex() : int{ @@ -91,7 +91,7 @@ public function getMapNameIndex() : int{ } public function setMapDisplayPlayers(bool $value) : void{ - $this->getNamedTag()->setByte(self::TAG_MAP_DISPLAY_PLAYERS, intval($value)); + $this->setNamedTagEntry(new ByteTag(self::TAG_MAP_DISPLAY_PLAYERS, intval($value))); } public function isMapDisplayPlayers() : bool{ @@ -99,7 +99,7 @@ public function isMapDisplayPlayers() : bool{ } public function setMapInit(bool $value) : void{ - $this->getNamedTag()->setByte(self::TAG_MAP_IS_INIT, intval($value)); + $this->setNamedTagEntry(new ByteTag(self::TAG_MAP_IS_INIT, intval($value))); } public function isMapInit() : bool{ diff --git a/src/pocketmine/maps/MapData.php b/src/pocketmine/maps/MapData.php index 593a86fc9..afb4d0b61 100644 --- a/src/pocketmine/maps/MapData.php +++ b/src/pocketmine/maps/MapData.php @@ -1,28 +1,33 @@ id = $mapId; if(empty($renderers)){ $renderers[] = new VanillaMapRenderer(); } $this->renderers = $renderers; + + foreach($renderers as $renderer){ + $renderer->initialize($this); + } + + $transColor = new Color(0, 0, 0, 0); + for($y = 0; $y < 128; $y++){ + for($x = 0; $x < 128; $x++){ + $this->setColorAt($x, $y, clone $transColor); + } + } } /** @@ -176,8 +198,7 @@ public function updateMap(int $flags) : void{ $pk = $this->createDataPacket($flags); if($info->forceUpdate and !empty($pk->colors)){ $info->forceUpdate = false; - $player->sendPopup("xxxxx"); - $pk->cropTexture($info->minX, $info->minY, $info->maxX + 1 - $info->minX, $info->maxY + 1 - $info->minY); + //$pk->cropTexture($info->minX, $info->minY, $info->maxX + 1 - $info->minX, $info->maxY + 1 - $info->minY); } $player->sendDataPacket($pk); } @@ -283,6 +304,7 @@ public function setFullyExplored(bool $fullyExplored) : void{ public function getMapInfo(Player $player) : MapInfo{ if(!isset($this->playersMap[spl_object_hash($player)])){ $this->playersMap[spl_object_hash($player)] = new MapInfo($player); + $mo = new MapTrackedObject(); $mo->entityUniqueId = $player->getId(); $mo->type = MapTrackedObject::TYPE_PLAYER; @@ -327,15 +349,11 @@ public function updateVisiblePlayers(Player $player, Map $mapStack){ $this->trackedObjects[$player->getName()] = $mo; } - if(!$player->getInventory()->contains($mapStack)){ - unset($this->decorations[$player->getName()]); - } - if($mapStack->isMapDisplayPlayers()){ foreach($this->playersMap as $info){ $pi = $info->player; - if($pi->isOnline() and $pi->isAlive() and $pi->level->getDimension() === $this->dimension and $info->packetSendTimer++ % 5 === 0){ - if(!$mapStack->isOnItemFrame() and $pi->getInventory()->contains($mapStack)){ + if($pi->isOnline() and $pi->isAlive() and $pi->level->getDimension() === $this->dimension){ + if(!$mapStack->isOnItemFrame() and $info->packetSendTimer++ % 5 === 0){ $this->updateDecorations(MapDecoration::TYPE_PLAYER, $pi->level, $pi->getName(), $pi->getFloorX(), $pi->getFloorZ(), $pi->getYaw()); } }else{ @@ -416,7 +434,7 @@ public function updateDecorations(int $type, Level $worldIn, String $entityIdent $deco->rot = $b2; $deco->xOffset = $b0; $deco->yOffset = $b1; - $deco->color = $color ?? new Color(0, 0, 0); + $deco->color = $color ?? new Color(255, 255, 255); $deco->label = $entityIdentifier; $this->decorations[$entityIdentifier] = $deco; @@ -429,6 +447,7 @@ public function updateDecorations(int $type, Level $worldIn, String $entityIdent */ public function addRenderer(MapRenderer $renderer) : void{ $this->renderers[spl_object_id($renderer)] = $renderer; + $renderer->initialize($this); } /** diff --git a/src/pocketmine/maps/MapInfo.php b/src/pocketmine/maps/MapInfo.php index f1234d794..25efe75dd 100644 --- a/src/pocketmine/maps/MapInfo.php +++ b/src/pocketmine/maps/MapInfo.php @@ -1,22 +1,25 @@ getMapInfo($player); $info->textureCheckCounter++; + $info->textureCheckCounter %= 15; + $world = $player->level; $tempVector = new Vector3(); $changed = false; - for($k1 = max(0, $l - $j1 + 1); $k1 < min($l + $j1, 128); ++$k1){ - if(($k1 & 15) === ($info->textureCheckCounter & 15)){ - $d0 = 0.0; - for($l1 = max($i1 - $j1 - 1, 0); $l1 < min($i1 + $j1, 128); ++$l1){ - if($k1 >= 0 and $l1 >= -1 and $k1 < 128 and $l1 < 128){ - $i2 = $k1 - $l; - $j2 = $l1 - $i1; - $flag1 = $i2 * $i2 + $j2 * $j2 > ($j1 - 2) * ($j1 - 2); - $k2 = ($j / $i + $k1 - 64) * $i; - $l2 = ($k / $i + $l1 - 64) * $i; - if($world->isChunkInUse($k2 >> 4, $l2 >> 4)){ - $k3 = 0; - $d1 = 0.0; - $chunk = $world->getChunk($k2 >> 4, $l2 >> 4); - $h = $chunk->getHighestBlockAt($k2 & 15, $l2 & 15); - if($h >= 0){ - $block = $world->getBlock($tempVector->setComponents($k2, $h, $l2)); - if($block instanceof Water){ - $attempt = 0; - while($block->getSide(Vector3::SIDE_DOWN) instanceof Water and $h > 0 and $attempt++ < 15){ - $block = $block->getSide(Vector3::SIDE_DOWN); - $h--; - } + for($k1 = max(0, $l - $j1 + 1) + $info->textureCheckCounter; $k1 < min($l + $j1, 128); $k1 += 15){ + $d0 = 0.0; + for($l1 = max($i1 - $j1 - 1, 0); $l1 < min($i1 + $j1, 128); $l1++){ + if($k1 >= 0 and $l1 >= -1 and $k1 < 128 and $l1 < 128){ + $i2 = $k1 - $l; + $j2 = $l1 - $i1; + $flag1 = $i2 * $i2 + $j2 * $j2 > ($j1 - 2) * ($j1 - 2); + $k2 = ($j / $i + $k1 - 64) * $i; + $l2 = ($k / $i + $l1 - 64) * $i; + if($world->isChunkInUse($k2 >> 4, $l2 >> 4)){ + $k3 = 0; + $d1 = 0.0; + $chunk = $world->getChunk($k2 >> 4, $l2 >> 4); + $h = $chunk->getHeightMap($k2 & 15, $l2 & 15) - 1; + if($h >= 0){ + $block = $world->getBlock($tempVector->setComponents($k2, $h, $l2)); + /*if($block instanceof Water){ + $attempt = 0; + while($block->getSide(Vector3::SIDE_DOWN) instanceof Water and $h > 0 and $attempt++ < 15){ + $block = $block->getSide(Vector3::SIDE_DOWN); + $h--; } - $d1 += (int) $h / (int) ($i * $i); - $mapColor = self::getMapColorByBlock($block); - }else{ - $mapColor = new Color(0 ,0, 0); - } - $k3 = $k3 / ($i * $i); - $d2 = ($d1 - $d0) * 4.0 / (int) ($i + 4) + ((int) ($k1 + $l1 & 1) - 0.5) * 0.4; + }*/ + $d1 += (int) $h / (int) ($i * $i); + $mapColor = self::getMapColorByBlock($block); + }else{ + $mapColor = new Color(0, 0, 0); + } + $k3 = $k3 / ($i * $i); + $d2 = ($d1 - $d0) * 4.0 / (int) ($i + 4) + ((int) ($k1 + $l1 & 1) - 0.5) * 0.4; + $i5 = 1; + if($d2 > 0.6){ + $i5 = 2; + } + if($d2 < -0.6){ + $i5 = 0; + } + if($mapColor->getR() === 64 and $mapColor->getG() === 64 and $mapColor->getB() === 255){ // water color + $d2 = (int) $k3 * 0.1 + (int) ($k1 + $l1 & 1) * 0.2; $i5 = 1; - if($d2 > 0.6){ + if($d2 < 0.5){ $i5 = 2; } - if($d2 < -0.6){ + if($d2 > 0.9){ $i5 = 0; } - if($mapColor->getR() === 64 and $mapColor->getG() === 64 and $mapColor->getB() === 255){ // water color - $d2 = (int) $k3 * 0.1 + (int) ($k1 + $l1 & 1) * 0.2; - $i5 = 1; - if($d2 < 0.5){ - $i5 = 2; - } - if($d2 > 0.9){ - $i5 = 0; - } - } - $d0 = $d1; - if($l1 >= 0 and $i2 * $i2 + $j2 * $j2 < $j1 * $j1 and (!$flag1 || ($k1 + $l1 & 1) != 0)){ - $b0 = $mapData->getColorAt($k1, $l1)->toABGR(); - $b1 = self::colorizeMapColor($mapColor->toABGR(), $i5); - if($b0 !== $b1){ - $mapData->setColorAt($k1, $l1, Color::fromABGR($b1)); + } + $d0 = $d1; + if($l1 >= 0 and $i2 * $i2 + $j2 * $j2 < $j1 * $j1 and (!$flag1 || ($k1 + $l1 & 1) != 0)){ + $b0 = $mapData->getColorAt($k1, $l1)->toABGR(); + $b1 = self::colorizeMapColor($mapColor->toABGR(), $i5); + if($b0 !== $b1){ + $mapData->setColorAt($k1, $l1, Color::fromABGR($b1)); - $mapData->updateTextureAt($k1, $l1); - $changed = true; - } + $mapData->updateTextureAt($k1, $l1); + $changed = true; } } }