more entity optimizations

This commit is contained in:
GameHerobrine 2023-07-28 02:50:23 +03:00
parent 2b0ad36e14
commit 2d9b37aa9c
12 changed files with 44 additions and 62 deletions

View file

@ -60,9 +60,9 @@ abstract class Animal extends Creature implements Ageable, Breedable{
public function spawnChild()
{
return $this->server->api->entity->add($this->level, $this->class, $this->type, [
"x" => $this->x + Utils::randomFloat() * mt_rand(-1, 1),
"x" => $this->x + lcg_value() * mt_rand(-1, 1),
"y" => $this->y,
"z" => $this->z + Utils::randomFloat() * mt_rand(-1, 1),
"z" => $this->z + lcg_value() * mt_rand(-1, 1),
"IsBaby" => true,
"Age" => -24000,
]);

View file

@ -423,14 +423,13 @@ class Entity extends Position
for ($y = $startY; $y <= $endY; ++$y){
for ($x = $startX; $x <= $endX; ++$x){
for ($z = $startZ; $z <= $endZ; ++$z){
$pos = new Vector3($x, $y, $z);
$b = $this->level->level->getBlock($x, $y, $z);
$id = $b[0];
$meta = $b[1];
switch ($id) {
case WATER:
case STILL_WATER: // Drowing
if ($this->fire > 0 and $this->inBlock($pos)) {
if ($this->fire > 0 and $this->inBlockNonVector($x, $y, $z)) {
$this->fire = 0;
$this->updateMetadata();
}
@ -447,7 +446,7 @@ class Entity extends Position
break;
case LAVA: // Lava damage
case STILL_LAVA:
if ($this->inBlock($pos)) {
if ($this->inBlockNonVector($x, $y, $z)) {
$this->harm(5, "lava");
$this->fire = 300;
$this->updateMetadata();
@ -455,7 +454,7 @@ class Entity extends Position
}
break;
case FIRE: // Fire block damage
if ($this->inBlock($pos)) {
if ($this->inBlockNonVector($x, $y, $z)) {
$this->harm(1, "fire");
$this->fire = 300;
$this->updateMetadata();
@ -463,14 +462,15 @@ class Entity extends Position
}
break;
case CACTUS: // Cactus damage
if ($this->touchingBlock($pos)) {
if ($this->inBlockNonVector($x, $y, $z)) {
$this->harm(1, "cactus");
$hasUpdate = true;
}
break;
default:
if($this->inBlock(new Vector3($x, $y, $z), 0.7) and $y == $endY and !StaticBlock::getIsTransparent($id) and ($this->class === ENTITY_MOB or $this->class === ENTITY_PLAYER)){}
elseif($x == ($endX - 1) and $y == $endY and $z == ($endZ - 1)){
if($this->inBlockNonVector($x, $y, $z, 0.7) && $y == $endY and !StaticBlock::getIsTransparent($id) && ($this->class === ENTITY_MOB || $this->class === ENTITY_PLAYER)){
}elseif($x == ($endX - 1) and $y == $endY and $z == ($endZ - 1)){
$this->air = 200;
}
break;
@ -1123,7 +1123,10 @@ class Entity extends Position
$this->server->preparedSQL->entity->setPosition->bindValue(":eid", $this->eid, SQLITE3_INTEGER);
$this->server->preparedSQL->entity->setPosition->execute();
}
public function inBlockNonVector($x, $y, $z, $radius = 0.8)
{
return $y == ceil($this->y) || $y == (ceil($this->y)+1) && max(abs($x - ($this->x-0.5)), abs($z - ($this->z-0.5)));
}
public function inBlock(Vector3 $block, $radius = 0.8)
{
$me = new Vector3($this->x - 0.5, $this->y, $this->z - 0.5);
@ -1154,15 +1157,7 @@ class Entity extends Position
public function resetSpeed()
{
$this->speedMeasure = array(
0,
0,
0,
0,
0,
0,
0
);
$this->speedMeasure = array(0, 0, 0, 0, 0, 0, 0);
}
public function getSpeed()
@ -1256,8 +1251,8 @@ class Entity extends Position
if ($ret != false && $this->hasKnockback && is_numeric($cause) && ($entity = $this->server->api->entity->get($cause)) != false) {
$d = $entity->x - $this->x;
for ($d1 = $entity->z - $this->z; $d * $d + $d1 * $d1 < 0.0001; $d1 = (Utils::randomFloat() - Utils::randomFloat()) * 0.01) {
$d = (Utils::randomFloat() - Utils::randomFloat()) * 0.01;
for ($d1 = $entity->z - $this->z; $d * $d + $d1 * $d1 < 0.0001; $d1 = (lcg_value() - lcg_value()) * 0.01) {
$d = (lcg_value() - lcg_value()) * 0.01;
}
// attackedAtYaw = (float)((Math.atan2($d1, $d) * 180D) / 3.1415927410125732D) >
$this->knockBack($d, $d1);
@ -1469,9 +1464,9 @@ class Entity extends Position
$f1 = 0.4;
$this->speedX /= 2;
$this->speedZ /= 2;
$this->speedX -= ($d / (double) $f) * (double) $f1;
$this->speedX -= ($d / $f) * $f1;
$this->speedY += 0.40000000596046448;
$this->speedZ -= ($d1 / (double) $f) * (double) $f1;
$this->speedZ -= ($d1 / $f) * $f1;
if($this->speedY > 0.40000000596046448){
$this->speedY = 0.40000000596046448;
}

View file

@ -6,6 +6,7 @@ abstract class Living extends Entity implements Damageable, Pathfindable{
public $target, $ai;
public $pathFinder, $path = null, $currentIndex = 0, $currentNode, $pathFollower;
public $ticksExisted = 0;
public function __construct(Level $level, $eid, $class, $type = 0, $data = array()){
$this->target = false;
$this->ai = new EntityAI($this);
@ -15,7 +16,7 @@ abstract class Living extends Entity implements Damageable, Pathfindable{
$this->canBeAttacked = true;
$this->hasGravity = true;
$this->hasKnockback = true;
if(self::$despawnMobs) $this->server->schedule(self::$despawnTimer, [$this, "close"]); //900*20
//if(self::$despawnMobs) $this->server->schedule(self::$despawnTimer, [$this, "close"]); //900*20
}
public function fall(){
$dmg = floor($this->fallDistance - 3);
@ -62,6 +63,9 @@ abstract class Living extends Entity implements Damageable, Pathfindable{
}
public function update(){
if(self::$despawnMobs && ++$this->ticksExisted > self::$despawnTimer){
$this->close();
}
if(!$this->dead && Entity::$allowedAI && $this->idleTime <= 0) {
$this->ai->updateTasks();
}

View file

@ -16,7 +16,7 @@ class TaskLookAround extends TaskBase
public function canBeExecuted(EntityAI $ai)
{
return !$ai->entity->inPanic && !$ai->entity->isMoving() && Utils::randomFloat() < 0.02 && !$ai->isStarted("TaskLookAtPlayer") && !$ai->isStarted("TaskTempt") && !$ai->entity->hasPath(); /*Vanilla value*/
return !$ai->entity->inPanic && !$ai->entity->isMoving() && lcg_value() < 0.02 && !$ai->isStarted("TaskLookAtPlayer") && !$ai->isStarted("TaskTempt") && !$ai->entity->hasPath(); /*Vanilla value*/
}
public function onUpdate(EntityAI $ai)

View file

@ -4,7 +4,7 @@ class TaskLookAtPlayer extends TaskBase{
public $target = false;
private $state, $yaw, $pitch;
public function canBeExecuted(EntityAI $ai){
return !$ai->entity->inPanic && Utils::randomFloat() < 0.02 && !$ai->entity->isMovingHorizontally() && !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskTempt") && !$ai->entity->hasPath();
return !$ai->entity->inPanic && lcg_value() < 0.02 && !$ai->entity->isMovingHorizontally() && !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskTempt") && !$ai->entity->hasPath();
}
protected function findTarget($e, $r){

View file

@ -20,7 +20,7 @@ class TaskSwimming extends TaskBase
return;
}
if(Utils::randomFloat() < 0.8){ #1.5.2 method
if(lcg_value() < 0.8){ #1.5.2 method
$ai->mobController->setJumping(true);
}

View file

@ -12,11 +12,6 @@ class Sheep extends Animal{
$this->data["Color"] = isset($this->data["Color"]) ? $this->data["Color"] : $this->sheepColor();
$this->setSpeed(0.25);
$this->update();
/*if($this->isSheared() and Entity::$updateOnTick){ //if ai enabled
$this->server->schedule(mt_rand(60, 400), [$this, "eatGrass"], null, true); //unknown time
}elseif($this->isBaby() and Entity::$updateOnTick){
$this->server->schedule(mt_rand(400, 1000), [$this, "eatGrass"], null, true); //unknown time
}*/
$this->ai->addTask(new TaskEatTileGoal());
}
public function createSaveData(){
@ -27,17 +22,6 @@ class Sheep extends Animal{
}
public function eatGrass(){
/*$downBlock = $this->level->getBlock(new Vector3($this->x, $this->y-1, $this->z));
if($downBlock->getID() !== GRASS){
//console("eww i don't wanna eat this $downBlock");
return false;
}
$pk = new EntityEventPacket;
$pk->eid = $this->eid;
$pk->event = EntityEventPacket::ENTITY_ANIM_10;
$this->server->api->player->broadcastPacket($this->level->players, $pk);
$this->server->schedule(38, [$this, "setSheared"], false);
$this->level->setblock(new Position($this->x, $this->y-1, $this->z, $this->level), new DirtBlock());*/
$this->setSheared(0);
if($this->isBaby()){
$age = $this->getAge() + 1200;