Update EntityAPI.php

This commit is contained in:
ArkQuark 2022-10-20 23:35:02 +03:00
parent 7ea63bdceb
commit 83b7bc4b39
11 changed files with 380 additions and 57 deletions

View file

@ -9,6 +9,8 @@ abstract class Creature extends Living{
parent::__construct($level, $eid, $class, $type, $data);
$this->setHealth(isset($this->data["Health"]) ? $this->data["Health"]:1, "generic");
//$this->setName((isset($mobs[$this->type]) ? $mobs[$this->type]:$this->type));
$this->ai->addTask(new TaskLookAround());
$this->ai->addTask(new TaskRandomWalk());
}

View file

@ -42,7 +42,7 @@ class Entity extends Position{
public $fallStart;
private $tickCounter;
private $speedMeasure = array(0, 0, 0, 0, 0, 0, 0);
protected $server;
public $server;
private $isStatic;
public $level;
public $isInMinecart = false;
@ -94,6 +94,7 @@ class Entity extends Position{
$this->height = 0.98;
$this->moveTime = 0;
$this->lookTime = 0;
$this->onGround = false;
switch($this->class){
case ENTITY_PLAYER:
$this->player = $this->data["player"];
@ -153,7 +154,7 @@ class Entity extends Position{
}
public function isMoving(){
return $this->moveTime > 0 || ($this->speedX > 0.01 || $this->speedX < -0.01) || ($this->speedY > 0.01 || $this->speedY < -0.01) || ($this->speedZ > 0.01 || $this->speedZ < -0.01);
return ($this->speedX > 0.01 || $this->speedX < -0.01) || ($this->speedY > 0.01 || $this->speedY < -0.01) || ($this->speedZ > 0.01 || $this->speedZ < -0.01);
}
public function setVelocity($vX, $vY = 0, $vZ = 0){
if($vX instanceof Vector3){
@ -395,7 +396,14 @@ class Entity extends Position{
}
return $hasUpdate;
}
protected function checkGroundState($movX, $movY, $movZ, $dx, $dy, $dz){
//$this->isCollidedVertically = $movY != $dy;
//$this->isCollidedHorizontally = ($movX != $dx or $movZ != $dz);
//$this->isCollided = ($this->isCollidedHorizontally or $this->isCollidedVertically);
$this->onGround = ($movY != $dy and $movY < 0);
}
public function update(){
if($this->closed === true){
return false;
@ -459,19 +467,18 @@ class Entity extends Position{
}
if($this->class !== ENTITY_PLAYER){
$update = false;
if((($this->class !== ENTITY_OBJECT and $this->type !== OBJECT_PRIMEDTNT) or $support === false)){
if(true){
$drag = 0.2;
if($this->speedX < 0.01 && $this->speedX > -0.01){
if(Utils::in_range($this->speedX, -0.01, 0.01)){
$this->speedX = 0;
}
if($this->speedZ < 0.01 && $this->speedZ > -0.01){
if(Utils::in_range($this->speedZ, -0.01, 0.01)){
$this->speedZ = 0;
}
if($this->speedY < 0.001 && $this->speedY > -0.001){
if(Utils::in_range($this->speedY, -0.001, 0.001)){
$this->speedY = 0;
}
if($this->class === ENTITY_MOB || $this->class === ENTITY_ITEM){
if(($this->class === ENTITY_MOB || $this->class === ENTITY_ITEM) && ($this->speedX != 0 || $this->speedY != 0 || $this->speedZ != 0)){
$blocks = $this->level->getCubes($this->boundingBox->getOffsetBoundingBox($this->speedX, $this->speedY, $this->speedZ));
foreach($blocks as $b){
$this->speedX = $b->calculateXOffset($this->boundingBox, $this->speedX);
@ -991,18 +998,6 @@ class Entity extends Position{
if($health < $this->health){
$harm = true;
$dmg = $this->health - $health;
if(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;
}
//attackedAtYaw = (float)((Math.atan2($d1, $d) * 180D) / 3.1415927410125732D) - rotationYaw;
$this->knockBack($d, $d1);
$this->sendMotion();
}
if($this->class === ENTITY_PLAYER and ($this->player instanceof Player)){
$points = 0;
$values = array(
@ -1092,6 +1087,18 @@ class Entity extends Position{
}elseif($this->health > 0){
$this->dead = false;
}
if(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;
}
//attackedAtYaw = (float)((Math.atan2($d1, $d) * 180D) / 3.1415927410125732D) - rotationYaw;
$this->knockBack($d, $d1);
$this->sendMotion();
}
return true;
}
return false;

View file

@ -5,7 +5,7 @@ class TaskLookAround extends TaskBase
private $finYaw = 0;
public function shouldBeExecuted(EntityAI $ai)
{
return !$ai->entity->isMoving() && $ai->entity->lookTime <= 0 && mt_rand(0,30) == 1 && $ai->entity->idleTime <= 0;
return !$ai->entity->isMoving() && $ai->entity->lookTime <= 0 && mt_rand(0,30) == 1 && $ai->entity->idleTime <= 0 && !($ai->lastTask instanceof TaskLookAround);
}
public function wasExecuted(EntityAI $ai){

View file

@ -2,25 +2,57 @@
class TaskRandomWalk extends TaskBase
{
private $x, $z, $delay = 10, $newPos, $looking = false;
public function shouldBeExecuted(EntityAI $ai)
{
return !$ai->entity->pathFound && $ai->entity->moveTime <= 0 && $ai->entity->lookTime <= 0 && !($ai->entity->target instanceof Entity) && mt_rand(0,3) == 1;
}
public function onUpdate(EntityAI $ai)
{
console("random walk");
return !$ai->entity->pathFound && $ai->entity->moveTime <= 0 && $ai->entity->lookTime <= 0 && $ai->entity->idleTime <= 0 && !($ai->entity->target instanceof Entity) && mt_rand(0,3) == 1;
}
public function wasExecuted(EntityAI $ai)
{
return $ai->entity->moveTime > 0;
}
public function onStart(EntityAI $ai)
{
$ai->entity->moveTime = 100;
$this->x = mt_rand(-7, 7);
$this->z = mt_rand(-7, 7);
$this->newPos = $ai->entity->add($this->x, 0, $this->z);
$ai->entity->moveTime = 15 * Utils::distance($ai->entity, $this->newPos);
}
public function onUpdate(EntityAI $ai)
{
if($this->delay > 0 && !$this->looking){
--$this->delay;
try{
return;
}finally{
$this->testLook($ai, ($this->x > 0 ? 0.1 : ($this->x < 0 ? -0.1 : 0)), ($this->z > 0 ? 0.1 : ($this->z < 0 ? -0.1 : 0)));
$pk = new RotateHeadPacket();
$pk->eid = $ai->entity->eid;
$pk->yaw = $ai->entity->yaw;
$ai->entity->server->api->player->broadcastPacket($ai->entity->level->players, $pk);
$this->looking = true;
}
}
if($ai->entity->moveTime <= 1){
$ai->entity->idleTime = mt_rand(10,100);
$this->looking = false;
}
if(!$ai->entity->isMoving()){
$ai->entity->addVelocity(($this->x > 0 ? 0.1 : ($this->x < 0 ? -0.1 : 0)), 0, ($this->z > 0 ? 0.1 : ($this->z < 0 ? -0.1 : 0)));
}
}
public function updateLook($ai){
$ai->entity->server->query("UPDATE entities SET pitch = ".$ai->entity->pitch.", yaw = ".$ai->entity->yaw." WHERE EID = ".$ai->entity->eid.";");
}
public function testLook($ai, $vX, $vZ){
$x = $this->newPos->x - $ai->entity->x;
$y = $this->newPos->y - $ai->entity->y;
$z = $this->newPos->z - $ai->entity->z;
$ai->entity->yaw = -atan2($vX, $vZ) * 180 / M_PI;
$ai->entity->pitch = $y == 0 ? 0 : rad2deg(-atan2($y, sqrt(pow($x, 2) + pow($z, 2))));
$ai->entity->server->query("UPDATE entities SET pitch = ".$ai->entity->pitch.", yaw = ".$ai->entity->yaw." WHERE EID = ".$ai->entity->eid.";");
}
}

View file

@ -4,8 +4,7 @@ class Chicken extends Animal{
const TYPE = MOB_CHICKEN;
function __construct(Level $level, $eid, $class, $type = 0, $data = array()){
parent::__construct($level, $eid, $class, $type, $data);
$server = ServerAPI::request();
$server->schedule(mt_rand(0,6000) + 6000, array($this, "dropAnEgg"));
$this->server->schedule(mt_rand(0,6000) + 6000, array($this, "dropAnEgg"));
$this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 4, "generic");
$this->setName('Chicken');
$this->setSize($this->isBaby() ? 0.2 : 0.4, $this->isBaby() ? 0.35 : 0.7);

View file

@ -8,7 +8,6 @@ class Pig extends Animal{
$this->server = ServerAPI::request();
$this->setSize($this->isBaby() ? 0.45 : 0.9, $this->isBaby() ? 0.45 : 0.9);
$this->setName('Pig');
$this->ai->addTask(new TaskLookAround());
$this->update();
}
public function isFood($id){

View file

@ -6,24 +6,6 @@ class Arrow extends Projectile{
parent::__construct($level, $eid, $class, $type, $data);
//$this->server->schedule(1210, array($this, "update")); //Despawn
}
public function update(){
$now = microtime(true);
if(($now - $this->spawntime) >= 60){
$this->close();
return false;
}
$f3 = 0.99;
$f5 = 0.03;
$this->move(new Vector3($this->speedX, $this->speedY, $this->speedZ));
$this->speedX *= $f3;
$this->speedZ *= $f3;
$this->speedY -= $f5;
$this->server->api->handle("entity.move", $this);
$this->server->api->handle("entity.motion", $this);
$this->handleUpdate();
$this->server->schedule(1, array($this, "update"), false);
$this->lastUpdate = $now;
}
public function handleUpdate(){
$pk = new MoveEntityPacket_PosRot;
$pk->eid = $this->eid;