Performance improvements in blocks and Entities

This commit is contained in:
Shoghi Cervantes 2014-08-24 14:08:17 +02:00
parent 84ce5f1c73
commit 01ebe74974
9 changed files with 145 additions and 147 deletions

View File

@ -687,7 +687,7 @@ class Player extends Human implements CommandSender, InventoryHolder, IPlayer{
$chunkZ = $Z + $centerZ;
$index = Level::chunkHash($chunkX, $chunkZ);
if(!isset($this->usedChunks[$index])){
if($this->getLevel()->isChunkPopulated($chunkX, $chunkZ)){
if($this->level->isChunkPopulated($chunkX, $chunkZ)){
$newOrder[$index] = $distance;
}else{
$generateQueue->insert([$chunkX, $chunkZ], $distance);

View File

@ -527,142 +527,142 @@ abstract class Block extends Position implements Metadatable{
public static function init(){
if(count(self::$list) === 0){
self::$list = array(
self::AIR => new Air(),
self::STONE => new Stone(),
self::GRASS => new Grass(),
self::DIRT => new Dirt(),
self::COBBLESTONE => new Cobblestone(),
self::PLANKS => new Planks(),
self::SAPLING => new Sapling(),
self::BEDROCK => new Bedrock(),
self::WATER => new Water(),
self::STILL_WATER => new StillWater(),
self::LAVA => new Lava(),
self::STILL_LAVA => new StillLava(),
self::SAND => new Sand(),
self::GRAVEL => new Gravel(),
self::GOLD_ORE => new GoldOre(),
self::IRON_ORE => new IronOre(),
self::COAL_ORE => new CoalOre(),
self::WOOD => new Wood(),
self::LEAVES => new Leaves(),
self::SPONGE => new Sponge(),
self::GLASS => new Glass(),
self::LAPIS_ORE => new LapisOre(),
self::LAPIS_BLOCK => new Lapis(),
self::SANDSTONE => new Sandstone(),
self::BED_BLOCK => new Bed(),
self::COBWEB => new Cobweb(),
self::TALL_GRASS => new TallGrass(),
self::DEAD_BUSH => new DeadBush(),
self::WOOL => new Wool(),
self::DANDELION => new Dandelion(),
self::POPPY => new CyanFlower(),
self::BROWN_MUSHROOM => new BrownMushroom(),
self::RED_MUSHROOM => new RedMushroom(),
self::GOLD_BLOCK => new Gold(),
self::IRON_BLOCK => new Iron(),
self::DOUBLE_SLAB => new DoubleSlab(),
self::SLAB => new Slab(),
self::BRICKS_BLOCK => new Bricks(),
self::TNT => new TNT(),
self::BOOKSHELF => new Bookshelf(),
self::MOSS_STONE => new MossStone(),
self::OBSIDIAN => new Obsidian(),
self::TORCH => new Torch(),
self::FIRE => new Fire(),
self::MONSTER_SPAWNER => new MonsterSpawner(),
self::WOOD_STAIRS => new WoodStairs(),
self::CHEST => new Chest(),
self::AIR => Air::class,
self::STONE => Stone::class,
self::GRASS => Grass::class,
self::DIRT => Dirt::class,
self::COBBLESTONE => Cobblestone::class,
self::PLANKS => Planks::class,
self::SAPLING => Sapling::class,
self::BEDROCK => Bedrock::class,
self::WATER => Water::class,
self::STILL_WATER => StillWater::class,
self::LAVA => Lava::class,
self::STILL_LAVA => StillLava::class,
self::SAND => Sand::class,
self::GRAVEL => Gravel::class,
self::GOLD_ORE => GoldOre::class,
self::IRON_ORE => IronOre::class,
self::COAL_ORE => CoalOre::class,
self::WOOD => Wood::class,
self::LEAVES => Leaves::class,
self::SPONGE => Sponge::class,
self::GLASS => Glass::class,
self::LAPIS_ORE => LapisOre::class,
self::LAPIS_BLOCK => Lapis::class,
self::SANDSTONE => Sandstone::class,
self::BED_BLOCK => Bed::class,
self::COBWEB => Cobweb::class,
self::TALL_GRASS => TallGrass::class,
self::DEAD_BUSH => DeadBush::class,
self::WOOL => Wool::class,
self::DANDELION => Dandelion::class,
self::POPPY => CyanFlower::class,
self::BROWN_MUSHROOM => BrownMushroom::class,
self::RED_MUSHROOM => RedMushroom::class,
self::GOLD_BLOCK => Gold::class,
self::IRON_BLOCK => Iron::class,
self::DOUBLE_SLAB => DoubleSlab::class,
self::SLAB => Slab::class,
self::BRICKS_BLOCK => Bricks::class,
self::TNT => TNT::class,
self::BOOKSHELF => Bookshelf::class,
self::MOSS_STONE => MossStone::class,
self::OBSIDIAN => Obsidian::class,
self::TORCH => Torch::class,
self::FIRE => Fire::class,
self::MONSTER_SPAWNER => MonsterSpawner::class,
self::WOOD_STAIRS => WoodStairs::class,
self::CHEST => Chest::class,
self::DIAMOND_ORE => new DiamondOre(),
self::DIAMOND_BLOCK => new Diamond(),
self::WORKBENCH => new Workbench(),
self::WHEAT_BLOCK => new Wheat(),
self::FARMLAND => new Farmland(),
self::FURNACE => new Furnace(),
self::BURNING_FURNACE => new BurningFurnace(),
self::SIGN_POST => new SignPost(),
self::WOOD_DOOR_BLOCK => new WoodDoor(),
self::LADDER => new Ladder(),
self::DIAMOND_ORE => DiamondOre::class,
self::DIAMOND_BLOCK => Diamond::class,
self::WORKBENCH => Workbench::class,
self::WHEAT_BLOCK => Wheat::class,
self::FARMLAND => Farmland::class,
self::FURNACE => Furnace::class,
self::BURNING_FURNACE => BurningFurnace::class,
self::SIGN_POST => SignPost::class,
self::WOOD_DOOR_BLOCK => WoodDoor::class,
self::LADDER => Ladder::class,
self::COBBLESTONE_STAIRS => new CobblestoneStairs(),
self::WALL_SIGN => new WallSign(),
self::COBBLESTONE_STAIRS => CobblestoneStairs::class,
self::WALL_SIGN => WallSign::class,
self::IRON_DOOR_BLOCK => new IronDoor(),
self::REDSTONE_ORE => new RedstoneOre(),
self::GLOWING_REDSTONE_ORE => new GlowingRedstoneOre(),
self::IRON_DOOR_BLOCK => IronDoor::class,
self::REDSTONE_ORE => RedstoneOre::class,
self::GLOWING_REDSTONE_ORE => GlowingRedstoneOre::class,
self::SNOW_LAYER => new SnowLayer(),
self::ICE => new Ice(),
self::SNOW_BLOCK => new Snow(),
self::CACTUS => new Cactus(),
self::CLAY_BLOCK => new Clay(),
self::SUGARCANE_BLOCK => new Sugarcane(),
self::SNOW_LAYER => SnowLayer::class,
self::ICE => Ice::class,
self::SNOW_BLOCK => Snow::class,
self::CACTUS => Cactus::class,
self::CLAY_BLOCK => Clay::class,
self::SUGARCANE_BLOCK => Sugarcane::class,
self::FENCE => new Fence(),
self::PUMPKIN => new Pumpkin(),
self::NETHERRACK => new Netherrack(),
self::SOUL_SAND => new SoulSand(),
self::GLOWSTONE_BLOCK => new Glowstone(),
self::FENCE => Fence::class,
self::PUMPKIN => Pumpkin::class,
self::NETHERRACK => Netherrack::class,
self::SOUL_SAND => SoulSand::class,
self::GLOWSTONE_BLOCK => Glowstone::class,
self::LIT_PUMPKIN => new LitPumpkin(),
self::CAKE_BLOCK => new Cake(),
self::LIT_PUMPKIN => LitPumpkin::class,
self::CAKE_BLOCK => Cake::class,
self::TRAPDOOR => new Trapdoor(),
self::TRAPDOOR => Trapdoor::class,
self::STONE_BRICKS => new StoneBricks(),
self::STONE_BRICKS => StoneBricks::class,
self::IRON_BARS => new IronBars(),
self::GLASS_PANE => new GlassPane(),
self::MELON_BLOCK => new Melon(),
self::PUMPKIN_STEM => new PumpkinStem(),
self::MELON_STEM => new MelonStem(),
self::IRON_BARS => IronBars::class,
self::GLASS_PANE => GlassPane::class,
self::MELON_BLOCK => Melon::class,
self::PUMPKIN_STEM => PumpkinStem::class,
self::MELON_STEM => MelonStem::class,
self::FENCE_GATE => new FenceGate(),
self::BRICK_STAIRS => new BrickStairs(),
self::STONE_BRICK_STAIRS => new StoneBrickStairs(),
self::FENCE_GATE => FenceGate::class,
self::BRICK_STAIRS => BrickStairs::class,
self::STONE_BRICK_STAIRS => StoneBrickStairs::class,
self::MYCELIUM => new Mycelium(),
self::NETHER_BRICKS => new NetherBrick(),
self::MYCELIUM => Mycelium::class,
self::NETHER_BRICKS => NetherBrick::class,
self::NETHER_BRICKS_STAIRS => new NetherBrickStairs(),
self::NETHER_BRICKS_STAIRS => NetherBrickStairs::class,
self::END_PORTAL => new EndPortal(),
self::END_STONE => new EndStone(),
self::SANDSTONE_STAIRS => new SandstoneStairs(),
self::EMERALD_ORE => new EmeraldOre(),
self::END_PORTAL => EndPortal::class,
self::END_STONE => EndStone::class,
self::SANDSTONE_STAIRS => SandstoneStairs::class,
self::EMERALD_ORE => EmeraldOre::class,
self::EMERALD_BLOCK => new Emerald(),
self::SPRUCE_WOOD_STAIRS => new SpruceWoodStairs(),
self::BIRCH_WOOD_STAIRS => new BirchWoodStairs(),
self::JUNGLE_WOOD_STAIRS => new JungleWoodStairs(),
self::STONE_WALL => new StoneWall(),
self::EMERALD_BLOCK => Emerald::class,
self::SPRUCE_WOOD_STAIRS => SpruceWoodStairs::class,
self::BIRCH_WOOD_STAIRS => BirchWoodStairs::class,
self::JUNGLE_WOOD_STAIRS => JungleWoodStairs::class,
self::STONE_WALL => StoneWall::class,
self::CARROT_BLOCK => new Carrot(),
self::POTATO_BLOCK => new Potato(),
self::CARROT_BLOCK => Carrot::class,
self::POTATO_BLOCK => Potato::class,
self::QUARTZ_BLOCK => new Quartz(),
self::QUARTZ_STAIRS => new QuartzStairs(),
self::DOUBLE_WOOD_SLAB => new DoubleWoodSlab(),
self::WOOD_SLAB => new WoodSlab(),
self::STAINED_CLAY => new StainedClay(),
self::QUARTZ_BLOCK => Quartz::class,
self::QUARTZ_STAIRS => QuartzStairs::class,
self::DOUBLE_WOOD_SLAB => DoubleWoodSlab::class,
self::WOOD_SLAB => WoodSlab::class,
self::STAINED_CLAY => StainedClay::class,
self::LEAVES2 => new Leaves2(),
self::WOOD2 => new Wood2(),
self::ACACIA_WOOD_STAIRS => new AcaciaWoodStairs(),
self::DARK_OAK_WOOD_STAIRS => new DarkOakWoodStairs(),
self::LEAVES2 => Leaves2::class,
self::WOOD2 => Wood2::class,
self::ACACIA_WOOD_STAIRS => AcaciaWoodStairs::class,
self::DARK_OAK_WOOD_STAIRS => DarkOakWoodStairs::class,
self::HAY_BALE => new HayBale(),
self::CARPET => new Carpet(),
self::HARDENED_CLAY => new HardenedClay(),
self::COAL_BLOCK => new Coal(),
self::HAY_BALE => HayBale::class,
self::CARPET => Carpet::class,
self::HARDENED_CLAY => HardenedClay::class,
self::COAL_BLOCK => Coal::class,
self::PODZOL => new Podzol(),
self::BEETROOT_BLOCK => new Beetroot(),
self::STONECUTTER => new Stonecutter(),
self::GLOWING_OBSIDIAN => new GlowingObsidian(),
self::NETHER_REACTOR => new NetherReactor(),
self::PODZOL => Podzol::class,
self::BEETROOT_BLOCK => Beetroot::class,
self::STONECUTTER => Stonecutter::class,
self::GLOWING_OBSIDIAN => GlowingObsidian::class,
self::NETHER_REACTOR => NetherReactor::class,
);
}
}
@ -676,11 +676,12 @@ abstract class Block extends Position implements Metadatable{
*/
public static function get($id, $meta = 0, Position $pos = null){
if(isset(self::$list[$id])){
$block = clone self::$list[$id];
$block->setDamage($meta);
$block = self::$list[$id];
$block = new $block($meta);
}else{
$block = new Generic($id, $meta);
}
if($pos instanceof Position){
$block->position($pos);
}

View File

@ -1055,7 +1055,7 @@ abstract class Entity extends Position implements Metadatable{
$level->removeEntity($this);
}
$this->despawnFromAll();
$this->level->release();
unset($this->level);
}
}

View File

@ -449,7 +449,7 @@ class Item{
self::BOW => new Bow(),
);
foreach(Block::$list as $id => $class){
self::$list[$id] = new ItemBlock($class);
self::$list[$id] = new ItemBlock(new $class);
}
}

View File

@ -676,7 +676,6 @@ class Level implements ChunkManager, Metadatable{
}
}
//TODO: fix this
foreach($this->getCollidingEntities($bb->expand(0.25, 0.25, 0.25), $entity) as $ent){
$collides[] = clone $ent->boundingBox;
}
@ -1368,7 +1367,8 @@ class Level implements ChunkManager, Metadatable{
* @return bool
*/
public function isChunkGenerated($x, $z){
return $this->provider->isChunkGenerated($x, $z);
$chunk = $this->getChunkAt($x, $z);
return $chunk instanceof FullChunk ? $chunk->isGenerated() : false;
}
/**
@ -1378,7 +1378,8 @@ class Level implements ChunkManager, Metadatable{
* @return bool
*/
public function isChunkPopulated($x, $z){
return $this->provider->isChunkPopulated($x, $z);
$chunk = $this->getChunkAt($x, $z);
return $chunk instanceof FullChunk ? $chunk->isPopulated() : false;
}
/**

View File

@ -25,7 +25,7 @@ use pocketmine\math\Vector3 as Vector3;
class Position extends Vector3{
/** @var \WeakRef<Level> */
/** @var Level */
public $level = null;
/**
@ -35,32 +35,26 @@ class Position extends Vector3{
* @param Level $level
* @param bool $strong
*/
public function __construct($x = 0, $y = 0, $z = 0, Level $level, $strong = false){
public function __construct($x = 0, $y = 0, $z = 0, Level $level = null, $strong = false){
$this->x = $x;
$this->y = $y;
$this->z = $z;
$this->level = new \WeakRef($level);
if($strong === true){
$this->level->acquire();
}
$this->level = $level;
}
public static function fromObject(Vector3 $pos, Level $level, $strong = false){
return new Position($pos->x, $pos->y, $pos->z, $level, $strong);
public static function fromObject(Vector3 $pos, Level $level = null, $strong = false){
return new Position($pos->x, $pos->y, $pos->z, $level);
}
/**
* @return Level
*/
public function getLevel(){
return $this->level->get();
return $this->level;
}
public function setLevel(Level $level, $strong = false){
$this->level = new \WeakRef($level);
if($strong === true){
$this->level->acquire();
}
$this->level = $level;
}
/**
@ -69,27 +63,31 @@ class Position extends Vector3{
* @return bool
*/
public function isValid(){
return isset($this->level) and $this->level->valid();
return isset($this->level) and $this->level instanceof Level;
}
/**
* Marks the level reference as strong so it won't be collected
* by the garbage collector.
*
* @deprecated
*
* @return bool
*/
public function setStrong(){
return $this->level->acquire();
return false;
}
/**
* Marks the level reference as weak so it won't have effect against
* the garbage collector decision.
*
* @deprecated
*
* @return bool
*/
public function setWeak(){
return $this->level->release();
return false;
}
/**
@ -107,7 +105,7 @@ class Position extends Vector3{
throw new \RuntimeException("Undefined Level reference");
}
return Position::fromObject(parent::getSide($side, $step), $this->getLevel());
return Position::fromObject(parent::getSide($side, $step), $this->level);
}
/**

View File

@ -51,7 +51,7 @@ abstract class BaseChunk extends BaseFullChunk implements Chunk{
/** @var int[256] */
protected $biomeColors;
/** @var \WeakRef<LevelProvider> */
/** @var LevelProvider */
protected $level;
protected $x;

View File

@ -117,7 +117,7 @@ abstract class Tile extends Position{
if(($level = $this->getLevel()) instanceof Level){
$level->removeTile($this);
}
$this->level->release();
unset($this->level);
}
}

View File

@ -228,9 +228,7 @@ class Utils{
PHP_SAPI,
(string) PHP_INT_MAX . "." . PHP_INT_SIZE,
serialize($_SERVER),
serialize(get_defined_constants()),
get_current_user(),
serialize(ini_get_all()),
(string) memory_get_usage() . "." . memory_get_peak_usage(),
php_uname(),
phpversion(),
@ -266,7 +264,7 @@ class Utils{
$strongEntropyValues = array(
is_array($startEntropy) ? hash("sha512", $startEntropy[($rounds + $drop) % count($startEntropy)], true) : hash("sha512", $startEntropy, true), //Get a random index of the startEntropy, or just read it
file_exists("/dev/urandom") ? fread(fopen("/dev/urandom", "rb"), 64) : str_repeat("\x00", 64),
(function_exists("openssl_random_pseudo_bytes") and version_compare(PHP_VERSION, "5.3.4", ">=")) ? openssl_random_pseudo_bytes(64) : str_repeat("\x00", 64),
function_exists("openssl_random_pseudo_bytes") ? openssl_random_pseudo_bytes(64) : str_repeat("\x00", 64),
function_exists("mcrypt_create_iv") ? mcrypt_create_iv(64, MCRYPT_DEV_URANDOM) : str_repeat("\x00", 64),
$value,
);