diff --git a/src/PocketMinecraftServer.php b/src/PocketMinecraftServer.php index 68903385..4d6a5917 100644 --- a/src/PocketMinecraftServer.php +++ b/src/PocketMinecraftServer.php @@ -595,7 +595,7 @@ class PocketMinecraftServer{ unset($this->tickMeasure[key($this->tickMeasure)]); ++$this->ticks; foreach($this->api->level->levels as $l){ - $l->onTick($this); + $l->onTick($this, $time); } return $this->tickerFunction($time); } diff --git a/src/entity/Animal.php b/src/entity/Animal.php index 63e2eb6c..4a59a725 100644 --- a/src/entity/Animal.php +++ b/src/entity/Animal.php @@ -46,8 +46,8 @@ abstract class Animal extends Creature implements Ageable, Breedable{ return $ent->eid != $this->eid && $this->type === $ent->type && $this->isInLove() && $ent->isInLove(); } - public function update(){ - parent::update(); + public function update($now){ + parent::update($now); $age = $this->getAge() + 1; //100 - fast. debug, 1 - normal if($age >= 0 && $this->isBaby()){ $this->setAge($age); diff --git a/src/entity/Entity.php b/src/entity/Entity.php index 3c412b6d..911ccb00 100644 --- a/src/entity/Entity.php +++ b/src/entity/Entity.php @@ -318,10 +318,12 @@ class Entity extends Position } } - public function environmentUpdate() + public function environmentUpdate($time) { $hasUpdate = $this->class === ENTITY_MOB; // force true for mobs - $time = microtime(true); + + $tickDiff = ($time - $this->lastUpdate) / 0.05; + if($this->class === ENTITY_PLAYER and ($this->player instanceof Player) and $this->player->spawned === true and $this->player->blocked !== true && ! $this->dead){ $myBB = $this->boundingBox->grow(1, 0.5, 1); foreach($this->server->api->entity->getRadius($this, 2, ENTITY_ITEM) as $item){ @@ -419,9 +421,11 @@ class Entity extends Position $f = LiquidBlock::getPercentAir($this->level->level->getBlockDamage($x, $y, $z)) - 0.1111111; $f1 = ($y + 1) - $f; if($d < $f1){ - --$this->air; - if($this->air <= 0){ + $this->air -= 1*$tickDiff; + if($this->isPlayer())console($this->air.":".$tickDiff.":".($time - $this->lastUpdate)); + if($this->air <= -20){ $this->harm(2, "water"); + $this->air = 0; //harm every 1 second } }else{ $this->air = $this->maxAir; @@ -491,18 +495,17 @@ class Entity extends Position return $this->y < -1.6; } - public function update(){ + public function update($now){ if($this->closed === true){ return false; } - $now = microtime(true); if($this->check === false){ $this->lastUpdate = $now; return; } - $hasUpdate = $this->environmentUpdate(); + $hasUpdate = $this->environmentUpdate($now); if($this->closed === true){ return false; @@ -906,7 +909,7 @@ class Entity extends Position ], 1 => [ "type" => 1, - "value" => $this->air + "value" => (int)$this->air ], 14 => [ "type" => 0, diff --git a/src/entity/Living.php b/src/entity/Living.php index 15438f94..955f3a6a 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -66,14 +66,14 @@ abstract class Living extends Entity implements Damageable, Pathfindable{ } } - public function update(){ + public function update($now){ if(self::$despawnMobs && ++$this->ticksExisted > self::$despawnTimer){ $this->close(); return; } if($this->closed) return; - parent::update(); + parent::update($now); } public function updateEntityMovement(){ diff --git a/src/entity/animal/Chicken.php b/src/entity/animal/Chicken.php index 599ceb53..7d60201a 100644 --- a/src/entity/animal/Chicken.php +++ b/src/entity/animal/Chicken.php @@ -10,7 +10,6 @@ class Chicken extends Animal{ $this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 4, "generic"); $this->setName('Chicken'); $this->setSpeed(0.25); - $this->update(); } public function isFood($id){ return $id === PUMPKIN_SEEDS || $id === MELON_SEEDS || $id === BEETROOT_SEEDS || $id === WHEAT_SEEDS; @@ -18,8 +17,8 @@ class Chicken extends Animal{ public function fall(){} //chickens have no fall dmg? - public function update(){ - parent::update(); + public function update($now){ + parent::update($now); if($this->timeUntilEgg-- <= 0 && !$this->isBaby()){ $this->dropAnEgg(); $this->timeUntilEgg = mt_rand(0,6000) + 6000; diff --git a/src/entity/animal/Cow.php b/src/entity/animal/Cow.php index cc98e5d7..814251ac 100644 --- a/src/entity/animal/Cow.php +++ b/src/entity/animal/Cow.php @@ -8,7 +8,6 @@ class Cow extends Animal{ $this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 10, "generic"); $this->setName("Cow"); $this->setSpeed(0.2); - $this->update(); } public function isFood($id){ diff --git a/src/entity/animal/Pig.php b/src/entity/animal/Pig.php index 3bf30e3a..d0fe432b 100644 --- a/src/entity/animal/Pig.php +++ b/src/entity/animal/Pig.php @@ -9,7 +9,6 @@ class Pig extends Animal implements Rideable{ $this->server = ServerAPI::request(); $this->setName("Pig"); $this->setSpeed(0.25); - $this->update(); } /** * @return boolean diff --git a/src/entity/animal/Sheep.php b/src/entity/animal/Sheep.php index 6f58cfcd..f1957bf7 100644 --- a/src/entity/animal/Sheep.php +++ b/src/entity/animal/Sheep.php @@ -3,6 +3,7 @@ class Sheep extends Animal{ public $color; const TYPE = MOB_SHEEP; + function __construct(Level $level, $eid, $class, $type = 0, $data = []){ $this->setSize($this->isBaby() ? 0.45 : 0.9, $this->isBaby() ? 0.675 : 1.3); parent::__construct($level, $eid, $class, $type, $data); @@ -11,9 +12,9 @@ class Sheep extends Animal{ $this->data["Sheared"] = isset($this->data["Sheared"]) ? $this->data["Sheared"] : 0; $this->data["Color"] = isset($this->data["Color"]) ? $this->data["Color"] : $this->sheepColor(); $this->setSpeed(0.25); - $this->update(); $this->ai->addTask(new TaskEatTileGoal()); } + public function createSaveData(){ $data = parent::createSaveData(); $data["Color"] = @$this->data["Color"]; diff --git a/src/entity/monster/Creeper.php b/src/entity/monster/Creeper.php index 9e40e7ba..c1c5c38d 100644 --- a/src/entity/monster/Creeper.php +++ b/src/entity/monster/Creeper.php @@ -10,7 +10,6 @@ class Creeper extends Monster{ $this->setName("Creeper"); $this->ignited = 0; $this->setSpeed(0.25); - $this->update(); $this->timeUntilExplode = $this->isIgnited() ? self::EXPL_TIME : 0; } @@ -58,8 +57,8 @@ class Creeper extends Monster{ } } - public function update(){ - if($this->isIgnited() && $this->target instanceof Entity && Utils::distance_noroot($this->target, $this) > 49){ + public function update($now){ + if($this->isIgnited() && $this->target instanceof Entity && Utils::distance_noroot($this->target, $this) > 49){ //TODO move somewhere else $this->setIgnited(-1); //broken in vanilla too $this->timeUntilExplode = 0; } @@ -69,7 +68,7 @@ class Creeper extends Monster{ if($this->timeUntilExplode >= 0){ --$this->timeUntilExplode; } - parent::update(); + parent::update($now); } public function explode() diff --git a/src/entity/monster/PigZombie.php b/src/entity/monster/PigZombie.php index 44724f67..bfc5371c 100644 --- a/src/entity/monster/PigZombie.php +++ b/src/entity/monster/PigZombie.php @@ -7,7 +7,6 @@ class PigZombie extends Monster{ $this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 12, "generic"); $this->setName("Pigman"); $this->setSpeed(0.25); - $this->update(); } public function getAttackDamage(){ return 5; diff --git a/src/entity/monster/Skeleton.php b/src/entity/monster/Skeleton.php index 087fbc17..23a23d13 100644 --- a/src/entity/monster/Skeleton.php +++ b/src/entity/monster/Skeleton.php @@ -9,7 +9,6 @@ class Skeleton extends Monster{ $this->ai->removeTask("TaskAttackPlayer"); //$this->ai->addTask(new TaskDestroyServerPerformance()); $this->setSpeed(0.25); - $this->update(); } public function getAttackDamage(){ @@ -36,9 +35,9 @@ class Skeleton extends Monster{ } } - public function update(){ + public function update($now){ $this->updateBurning(); - parent::update(); + parent::update($now); } public function getDrops(){ diff --git a/src/entity/monster/Spider.php b/src/entity/monster/Spider.php index b3f41f60..2c078b92 100644 --- a/src/entity/monster/Spider.php +++ b/src/entity/monster/Spider.php @@ -7,7 +7,6 @@ class Spider extends Monster{ $this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 8, "generic"); $this->setName("Spider"); $this->setSpeed(0.3); - $this->update(); } public function getDrops(){ diff --git a/src/entity/monster/Zombie.php b/src/entity/monster/Zombie.php index 9add4347..f34e1bbf 100644 --- a/src/entity/monster/Zombie.php +++ b/src/entity/monster/Zombie.php @@ -7,7 +7,6 @@ class Zombie extends Monster{ $this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 12, "generic"); $this->setName("Zombie"); $this->setSpeed(0.23); - $this->update(); } public function getArmorValue(){ @@ -38,9 +37,9 @@ class Zombie extends Monster{ return 4; //TODO vanillafy(zombies might be able to hold items) } - public function update(){ + public function update($now){ $this->updateBurning(); - parent::update(); + parent::update($now); } public function getDrops(){ diff --git a/src/entity/object/Arrow.php b/src/entity/object/Arrow.php index 22ecc09d..f78ae790 100644 --- a/src/entity/object/Arrow.php +++ b/src/entity/object/Arrow.php @@ -65,7 +65,7 @@ class Arrow extends Projectile{ $p->directDataPacket(clone $pk); } } - public function update(){ + public function update($now){ $this->needsUpdate = false; //TODO reenable return; //parent::update(); diff --git a/src/utils/Utils.php b/src/utils/Utils.php index f81256d1..7c7247ff 100644 --- a/src/utils/Utils.php +++ b/src/utils/Utils.php @@ -447,7 +447,7 @@ class Utils{ } public static function readTriad($str){ - return @unpack("N", "\x00" . $str)[1]; + return strlen($str) < 3 ? false : @unpack("N", "\x00$str")[1]; } public static function writeDataArray($data){ @@ -582,16 +582,13 @@ class Utils{ } public static function writeBool($b){ - return Utils::writeByte($b === true ? 1 : 0); + return Utils::writeByte($b ? 1 : 0); } public static function readInt($str){ if(strlen($str) <= 0) return; - if(PHP_INT_SIZE === 8){ - return @unpack("N", $str)[1] << 32 >> 32; - }else{ - return @unpack("N", $str)[1]; - } + + return @unpack("N", $str)[1] << 32 >> 32; //php has no signed long unpack } public static function writeInt($value){ @@ -602,12 +599,12 @@ class Utils{ } public static function readFloat($str){ - list(, $value) = ENDIANNESS === BIG_ENDIAN ? @unpack("f", $str) : @unpack("f", strrev($str)); + list(, $value) = unpack("G", $str); return $value; } public static function writeFloat($value){ - return ENDIANNESS === BIG_ENDIAN ? pack("f", $value) : strrev(pack("f", $value)); + return pack("G", $value); } public static function printFloat($value){ @@ -615,60 +612,37 @@ class Utils{ } public static function readDouble($str){ - list(, $value) = ENDIANNESS === BIG_ENDIAN ? @unpack("d", $str) : @unpack("d", strrev($str)); + list(, $value) = @unpack("E", $str); return $value; } public static function writeDouble($value){ - return ENDIANNESS === BIG_ENDIAN ? pack("d", $value) : strrev(pack("d", $value)); + return pack("E", $value); } public static function readLDouble($str){ - list(, $value) = ENDIANNESS === BIG_ENDIAN ? @unpack("d", strrev($str)) : @unpack("d", $str); + list(, $value) = @unpack("e", $str); return $value; } public static function writeLDouble($value){ - return ENDIANNESS === BIG_ENDIAN ? strrev(pack("d", $value)) : pack("d", $value); + return pack("e", $value); } public static function readLLong($str){ - return Utils::readLong(strrev($str)); + return unpack("P", $str)[1]; } public static function readLong($x, $signed = true){ - $value = "0"; - if($signed === true){ - $negative = ((ord($x[0]) & 0x80) === 0x80); - if($negative){ - $x = ~$x; - } - }else{ - $negative = false; - } - - for($i = 0; $i < 8; $i += 4){ - $value = bcmul($value, "4294967296", 0); //4294967296 == 2^32 - $value = bcadd($value, 0x1000000 * ord($x[$i] ?? 0) + ((ord($x[$i + 1] ?? 0) << 16) | (ord($x[$i + 2] ?? 0) << 8) | ord($x[$i + 3] ?? 0)), 0); - } - return ($negative ? "-$value" : $value); + return unpack("J", $x)[1]; //signed is useless since number cant be more than 2^63-1 in php } public static function writeLLong($value){ - return strrev(Utils::writeLong($value)); + return pack("P", $value); } public static function writeLong($value){ - return - chr(($value & 0xff00000000000000) >> 56). - chr(($value & 0x00ff000000000000) >> 48). - chr(($value & 0x0000ff0000000000) >> 40). - chr(($value & 0x000000ff00000000) >> 32). - chr(($value & 0x00000000ff000000) >> 24). - chr(($value & 0x0000000000ff0000) >> 16). - chr(($value & 0x000000000000ff00) >> 8). - chr(($value & 0x00000000000000ff)) - ; + return pack("J", $value); } } diff --git a/src/world/Level.php b/src/world/Level.php index b68f23ef..10107d19 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -336,7 +336,7 @@ class Level{ } } - public function onTick(PocketMinecraftServer $server){ + public function onTick(PocketMinecraftServer $server, $currentTime){ //$ents = $server->api->entity->getAll($this); if(!$this->stopTime) ++$this->time; for($cX = 0; $cX < 16; ++$cX){ @@ -368,7 +368,7 @@ class Level{ ++$this->totalMobsAmount; } if($e->isPlayer() || $e->needsUpdate){ - $e->update(); + $e->update($currentTime); if(!$e->isPlayer()) $post[] = $k; } } @@ -417,7 +417,7 @@ class Level{ } $block->position($pos); if($direct === true){ - $this->addBlockToSendQueue($pos->x, $pos->y, $pos->z, $pos->id, $pos->meta); + $this->addBlockToSendQueue($pos->x, $pos->y, $pos->z, $block->id, $block->meta); }else{ $i = ($pos->x >> 4) . ":" . ($pos->y >> 4) . ":" . ($pos->z >> 4); if(!isset($this->changedBlocks[$i])){ diff --git a/src/world/generator/SuperflatGenerator.php b/src/world/generator/SuperflatGenerator.php index 587b90a8..30f8f207 100644 --- a/src/world/generator/SuperflatGenerator.php +++ b/src/world/generator/SuperflatGenerator.php @@ -132,7 +132,7 @@ class SuperflatGenerator implements LevelGenerator{ $end = 128 + $spawn[0]; for($x = $start; $x <= $end; ++$x){ for($z = $start; $z <= $end; ++$z){ - if(floor(sqrt(pow($x - 128, 2) + pow($z - 128, 2))) <= $spawn[0]){ + if(floor(sqrt(($x - 128)*($x - 128)+ ($z - 128)*($z - 128))) <= $spawn[0]){ $this->level->setBlockRaw(new Vector3($x, $this->floorLevel - 1, $z), $spawn[1], null); } }