project uses tabs instead of spaces(as in CONTRIBUTING.md)

This commit is contained in:
GameHerobrine 2022-12-26 07:01:28 +03:00
parent f00318b6ed
commit 62571f26cd
115 changed files with 2291 additions and 2364 deletions

View file

@ -5,58 +5,58 @@ move methods
*/
abstract class Animal extends Creature implements Ageable, Breedable{
public $parent;
public $inLove; //do NOT add it into metadata, it doesnt send it to player
public $age;
public function __construct(Level $level, $eid, $class, $type = 0, $data = []){
parent::__construct($level, $eid, $class, $type, $data);
$this->setAge(isset($data["Age"]) ? $data["Age"] : 0);
if(isset($this->data["IsBaby"]) && $this->data["IsBaby"] && $this->getAge() >= 0){
$this->setAge(-24000);
}
$this->ai->addTask(new TaskTempt());
}
public $parent;
public $inLove; //do NOT add it into metadata, it doesnt send it to player
public $age;
public function __construct(Level $level, $eid, $class, $type = 0, $data = []){
parent::__construct($level, $eid, $class, $type, $data);
$this->setAge(isset($data["Age"]) ? $data["Age"] : 0);
if(isset($this->data["IsBaby"]) && $this->data["IsBaby"] && $this->getAge() >= 0){
$this->setAge(-24000);
}
$this->ai->addTask(new TaskTempt());
}
public function isBaby(){
return $this->getAge() < 0;
}
public function breed(){
$c = $this->spawnChild();
if($this->server->dhandle("entity.animal.breed", ["parent" => $this, "child" => $c]) !== false){
$c->parent = &$this;
$this->server->api->entity->spawnToAll($c);
}
$c = $this->spawnChild();
if($this->server->dhandle("entity.animal.breed", ["parent" => $this, "child" => $c]) !== false){
$c->parent = &$this;
$this->server->api->entity->spawnToAll($c);
}
}
public function update(){
parent::update();
$age = $this->getAge() + 1; //100 - fast. debug, 1 - normal
if($age >= 0 && $this->isBaby()){
$this->setAge($age);
$this->updateMetadata();
}else{
$this->setAge($age);
}
parent::update();
$age = $this->getAge() + 1; //100 - fast. debug, 1 - normal
if($age >= 0 && $this->isBaby()){
$this->setAge($age);
$this->updateMetadata();
}else{
$this->setAge($age);
}
}
public function getAge(){
return $this->age;
return $this->age;
}
public function setAge($i){
$this->age = $i;
$this->age = $i;
}
public function spawnChild()
{
return $this->server->api->entity->add($this->level, $this->class, $this->type, [
"x" => $this->x + Utils::randomFloat() * mt_rand(-1, 1),
"y" => $this->y,
"z" => $this->z + Utils::randomFloat() * mt_rand(-1, 1),
"IsBaby" => true,
"Age" => -24000,
]);
return $this->server->api->entity->add($this->level, $this->class, $this->type, [
"x" => $this->x + Utils::randomFloat() * mt_rand(-1, 1),
"y" => $this->y,
"z" => $this->z + Utils::randomFloat() * mt_rand(-1, 1),
"IsBaby" => true,
"Age" => -24000,
]);
}
public function getMetadata(){
@ -66,33 +66,33 @@ abstract class Animal extends Creature implements Ageable, Breedable{
}
public function createSaveData(){
$data = parent::createSaveData();
$data["IsBaby"] = $this->isBaby();
$data["Age"] = $this->getAge();
return $data;
$data = parent::createSaveData();
$data["IsBaby"] = $this->isBaby();
$data["Age"] = $this->getAge();
return $data;
}
public function isInLove(){
return $this->inLove > 0;
return $this->inLove > 0;
}
public function interactWith(Entity $e, $action){
if($e->isPlayer() && $action === InteractPacket::ACTION_HOLD){
$slot = $e->player->getHeldItem();
if($this->isFood($slot->getID())){
$e->player->removeItem($slot->getID(), $slot->getMetadata(), 1);
$this->inLove = 600; //600 ticks, original mehod from mcpe
return true;
}
}
parent::interactWith($e, $action);
if($e->isPlayer() && $action === InteractPacket::ACTION_HOLD){
$slot = $e->player->getHeldItem();
if($this->isFood($slot->getID())){
$e->player->removeItem($slot->getID(), $slot->getMetadata(), 1);
$this->inLove = 600; //600 ticks, original mehod from mcpe
return true;
}
}
parent::interactWith($e, $action);
}
public function counterUpdate(){
parent::counterUpdate();
if($this->isInLove()){
--$this->inLove;
}
parent::counterUpdate();
if($this->isInLove()){
--$this->inLove;
}
}
public function getDrops(){

View file

@ -25,30 +25,30 @@ abstract class Creature extends Living{
}
public function update(){
if($this->onGround){
/*if($this->path === null && $this->pathFinder instanceof ITileNavigator){ // mt_rand(0, 120) === 0){
$this->path = $this->pathFinder->navigate(new PathTileXYZ($this->x, $this->y, $this->z, $this->level), new PathTileXYZ($this->x + mt_rand(-10, 10), $this->y, $this->z + mt_rand(-10, 10), $this->level));
}
if($this->path != null && (is_array($this->path) && count($this->path) <= 0 || $this->currentIndex >= count($this->path))){
$this->path = null;
$this->currentIndex = 0;
$this->currentNode = false;
}elseif($this->path != null && ($this->currentNode == false || $this->ai->mobController->moveTo($this->currentNode->x, $this->currentNode->y, $this->currentNode->z) === false)){
$this->currentNode = $this->path[$this->currentIndex++];
}*/
}
parent::update();
if($this->onGround){
/*if($this->path === null && $this->pathFinder instanceof ITileNavigator){ // mt_rand(0, 120) === 0){
$this->path = $this->pathFinder->navigate(new PathTileXYZ($this->x, $this->y, $this->z, $this->level), new PathTileXYZ($this->x + mt_rand(-10, 10), $this->y, $this->z + mt_rand(-10, 10), $this->level));
}
if($this->path != null && (is_array($this->path) && count($this->path) <= 0 || $this->currentIndex >= count($this->path))){
$this->path = null;
$this->currentIndex = 0;
$this->currentNode = false;
}elseif($this->path != null && ($this->currentNode == false || $this->ai->mobController->moveTo($this->currentNode->x, $this->currentNode->y, $this->currentNode->z) === false)){
$this->currentNode = $this->path[$this->currentIndex++];
}*/
}
parent::update();
}
public function createSaveData(){
$data = parent::createSaveData();
$data["State"] = @$this->getState();
return $data;
$data = parent::createSaveData();
$data["State"] = @$this->getState();
return $data;
}
public function getSpeedModifer(){
return $this->speedModifer * ($this->inPanic ? 1.4 : 0.7);
return $this->speedModifer * ($this->inPanic ? 1.4 : 0.7);
}
public function spawn($player){

View file

@ -69,7 +69,7 @@ class Entity extends Position
public $radius;
public $inAction = false;
public $hasKnockback;
public $onGround;
function __construct(Level $level, $eid, $class, $type = 0, $data = array())
@ -624,7 +624,7 @@ class Entity extends Position
break;
}
if($b != false && $b->boundingBox->intersectsWith($aABB) && $b->isSolid && $this->isMoving()){
$this->speedX = $b->boundingBox->calculateXOffset($this->boundingBox, $this->speedX);
$this->speedX = $b->boundingBox->calculateXOffset($this->boundingBox, $this->speedX);
$this->speedZ = $b->boundingBox->calculateZOffset($this->boundingBox, $this->speedZ);
$lastY = $this->speedY;
$this->speedY = $b->boundingBox->calculateYOffset($this->boundingBox, $this->speedY);
@ -638,34 +638,34 @@ class Entity extends Position
}
if(!$support){
for($z = $startZ; $z <= $endZ; ++ $z){
for($x = $startX; $x <= $endX; ++ $x){
$v = new Vector3($x, $y, $z);
$v1 = new Vector3($x, $yC, $z);
if($this->isSupport($v, $this->width)){
$b = $this->level->getBlock($v);
if($b->isSolid === true){
$support = true;
$isFlying = false;
break;
} elseif(($b instanceof LiquidBlock) or $b->getID() === COBWEB or $b->getID() === LADDER or $b->getID() === FENCE or $b->getID() === STONE_WALL or $b->getID() === IRON_BARS){
$isFlying = false;
}
} elseif($this->isSupport($v1, $this->width)){
$b = $this->level->getBlock($v1);
if($b->isSolid === true){
$support = true;
$isFlying = false;
break;
} elseif(($b instanceof LiquidBlock) or $b->getID() === COBWEB or $b->getID() === LADDER or $b->getID() === FENCE or $b->getID() === STONE_WALL or $b->getID() === IRON_BARS){
$isFlying = false;
}
}
}
if($support === true){
break;
}
}
for($z = $startZ; $z <= $endZ; ++ $z){
for($x = $startX; $x <= $endX; ++ $x){
$v = new Vector3($x, $y, $z);
$v1 = new Vector3($x, $yC, $z);
if($this->isSupport($v, $this->width)){
$b = $this->level->getBlock($v);
if($b->isSolid === true){
$support = true;
$isFlying = false;
break;
} elseif(($b instanceof LiquidBlock) or $b->getID() === COBWEB or $b->getID() === LADDER or $b->getID() === FENCE or $b->getID() === STONE_WALL or $b->getID() === IRON_BARS){
$isFlying = false;
}
} elseif($this->isSupport($v1, $this->width)){
$b = $this->level->getBlock($v1);
if($b->isSolid === true){
$support = true;
$isFlying = false;
break;
} elseif(($b instanceof LiquidBlock) or $b->getID() === COBWEB or $b->getID() === LADDER or $b->getID() === FENCE or $b->getID() === STONE_WALL or $b->getID() === IRON_BARS){
$isFlying = false;
}
}
}
if($support === true){
break;
}
}
}
if($this->speedX != 0){
$this->x += $this->speedX;

View file

@ -1,21 +1,21 @@
<?php
abstract class Living extends Entity implements Damageable{
public $target, $ai;
public function __construct(Level $level, $eid, $class, $type = 0, $data = array()){
$this->target = false;
$this->ai = new EntityAI($this);
parent::__construct($level, $eid, $class, $type, $data);
$this->canBeAttacked = true;
$this->hasGravity = true;
$this->hasKnockback = true;
}
public function update(){
if($this->idleTime <= 0) {
$this->ai->updateTasks();
}
parent::update();
}
public $target, $ai;
public function __construct(Level $level, $eid, $class, $type = 0, $data = array()){
$this->target = false;
$this->ai = new EntityAI($this);
parent::__construct($level, $eid, $class, $type, $data);
$this->canBeAttacked = true;
$this->hasGravity = true;
$this->hasKnockback = true;
}
public function update(){
if($this->idleTime <= 0) {
$this->ai->updateTasks();
}
parent::update();
}
}

View file

@ -1,13 +1,13 @@
<?php
abstract class WalkingEntity extends Entity{
/**
* @param int $tickDiff
*
* @return null|Vector3
*/
/**
* @param int $tickDiff
*
* @return null|Vector3
*/
}

View file

@ -2,74 +2,74 @@
class EntityAI
{
/**
* @var MobController
*/
public $mobController;
/**
* @var Living
*/
public $entity;
/**
* @var TaskBase[]
*/
protected $tasks;
public $lastTask;
public $counter = 0;
public function __construct($entity){
$this->entity = $entity;
$this->tasks = [];
$this->mobController = new MobController($entity);
}
/**
* Add a task for entity
* @param TaskBase $task
*/
public function addTask(TaskBase $task){
console("[DEBUG] Adding new task...",true, true, 2);
$this->tasks[$task->__toString()] = $task;
}
/**
*
* @param mixed $id classname
* @return TaskBase | false
*/
public function getTask($id){
return isset($this->tasks[$id]) ? $this->tasks[$id] : false;
}
public function isStarted($id){
$task = @$this->getTask($id);
return $task instanceof TaskBase && $task->isStarted;
}
/**
* @var MobController
*/
public $mobController;
/**
* @var Living
*/
public $entity;
/**
* @var TaskBase[]
*/
protected $tasks;
public $lastTask;
public $counter = 0;
public function __construct($entity){
$this->entity = $entity;
$this->tasks = [];
$this->mobController = new MobController($entity);
}
/**
* Add a task for entity
* @param TaskBase $task
*/
public function addTask(TaskBase $task){
console("[DEBUG] Adding new task...",true, true, 2);
$this->tasks[$task->__toString()] = $task;
}
/**
*
* @param mixed $id classname
* @return TaskBase | false
*/
public function getTask($id){
return isset($this->tasks[$id]) ? $this->tasks[$id] : false;
}
public function isStarted($id){
$task = @$this->getTask($id);
return $task instanceof TaskBase && $task->isStarted;
}
public function updateTasks(){
if($this->counter % 3 != 0){
return;
}
foreach($this->tasks as $t){
if(!$t->isStarted && $t->canBeExecuted($this)){
$t->isStarted = true;
$t->onStart($this);
}
if($t->isStarted){
$t->onUpdate($this);
if($t->selfCounter <= 0){
$t->isStarted = false;
$t->onEnd($this);
}
}
}
}
public function __destruct(){
unset($this->entity);
}
public function updateTasks(){
if($this->counter % 3 != 0){
return;
}
foreach($this->tasks as $t){
if(!$t->isStarted && $t->canBeExecuted($this)){
$t->isStarted = true;
$t->onStart($this);
}
if($t->isStarted){
$t->onUpdate($this);
if($t->selfCounter <= 0){
$t->isStarted = false;
$t->onEnd($this);
}
}
}
}
public function __destruct(){
unset($this->entity);
}
}

View file

@ -2,70 +2,70 @@
class MobController
{
/**
* @var Entity
*/
public $entity;
public function __construct($e){
$this->entity = $e;
}
public function moveNonInstant($x, $y, $z){
if($x == 0 && $y == 0 && $z == 0){
return false; //failed
}
$ox = ($x > 0 ? 1 : ($x < 0 ? -1 : 0));
$oy = ($y > 0 ? 1 : ($y < 0 ? -1 : 0));
$oz = ($z > 0 ? 1 : ($z < 0 ? -1 : 0));
$this->entity->moveEntityWithOffset($ox, $oy, $oz);
$this->faceEntity($this->entity->add($ox, $oy, $oz));
$pk = new RotateHeadPacket(); //TODO headYaw auto update
$pk->eid = $this->entity->eid;
$pk->yaw = $this->entity->yaw;
$this->entity->server->api->player->broadcastPacket($this->entity->level->players, $pk);
return true;
}
public function moveTo($x, $y, $z){
return $this->moveNonInstant($x - floor($this->entity->x), $y - floor($this->entity->y), $z - floor($this->entity->z));
}
public function faceEntity(Vector3 $v){
$d = $v->subtract($this->entity)->normalize();
$dx = $d->x;
$dz = $d->z;
$tan = $dz == 0 ? 0 : (90 - rad2deg($dx / $dz));
$thetaOffset = $dz < 0 ? 90 : 270;
$this->entity->yaw = $this->entity->headYaw = ($thetaOffset + $tan);
}
public function lookOffset($x, $y, $z, $pitch = true){
$tan = $z == 0 ? 0 : (90 - rad2deg(atan($x / $z))); /*arctan(infinity) = pi/2 = (90deg) - 90 = 0*/
$thetaOffset = $z < 0 ? 90 : 270;
$calcYaw = $tan + $thetaOffset;
$this->entity->yaw = $calcYaw;
if($pitch){
$diff = sqrt($x * $x + $z * $z);
$calcPitch = rad2deg(atan($y / $diff));
$this->entity->pitch = $this->entity->type === MOB_CHICKEN ? -$calcPitch : $calcPitch;
}
$this->entity->server->query("UPDATE entities SET pitch = ".$this->entity->pitch.", yaw = ".$this->entity->yaw." WHERE EID = ".$this->entity->eid.";");
return true;
}
public function lookOn($x, $y = 0, $z = 0, $pitch = true){
if($x instanceof Vector3){
return $this->lookOn($x->x, $x->y + $x->getEyeHeight(), $x->z, $pitch);
}
return $this->lookOffset($x - $this->entity->x, ($this->entity->y + $this->entity->height) - $y, $z - $this->entity->z, $pitch);
}
public function __destruct(){
unset($this->entity);
}
/**
* @var Entity
*/
public $entity;
public function __construct($e){
$this->entity = $e;
}
public function moveNonInstant($x, $y, $z){
if($x == 0 && $y == 0 && $z == 0){
return false; //failed
}
$ox = ($x > 0 ? 1 : ($x < 0 ? -1 : 0));
$oy = ($y > 0 ? 1 : ($y < 0 ? -1 : 0));
$oz = ($z > 0 ? 1 : ($z < 0 ? -1 : 0));
$this->entity->moveEntityWithOffset($ox, $oy, $oz);
$this->faceEntity($this->entity->add($ox, $oy, $oz));
$pk = new RotateHeadPacket(); //TODO headYaw auto update
$pk->eid = $this->entity->eid;
$pk->yaw = $this->entity->yaw;
$this->entity->server->api->player->broadcastPacket($this->entity->level->players, $pk);
return true;
}
public function moveTo($x, $y, $z){
return $this->moveNonInstant($x - floor($this->entity->x), $y - floor($this->entity->y), $z - floor($this->entity->z));
}
public function faceEntity(Vector3 $v){
$d = $v->subtract($this->entity)->normalize();
$dx = $d->x;
$dz = $d->z;
$tan = $dz == 0 ? 0 : (90 - rad2deg($dx / $dz));
$thetaOffset = $dz < 0 ? 90 : 270;
$this->entity->yaw = $this->entity->headYaw = ($thetaOffset + $tan);
}
public function lookOffset($x, $y, $z, $pitch = true){
$tan = $z == 0 ? 0 : (90 - rad2deg(atan($x / $z))); /*arctan(infinity) = pi/2 = (90deg) - 90 = 0*/
$thetaOffset = $z < 0 ? 90 : 270;
$calcYaw = $tan + $thetaOffset;
$this->entity->yaw = $calcYaw;
if($pitch){
$diff = sqrt($x * $x + $z * $z);
$calcPitch = rad2deg(atan($y / $diff));
$this->entity->pitch = $this->entity->type === MOB_CHICKEN ? -$calcPitch : $calcPitch;
}
$this->entity->server->query("UPDATE entities SET pitch = ".$this->entity->pitch.", yaw = ".$this->entity->yaw." WHERE EID = ".$this->entity->eid.";");
return true;
}
public function lookOn($x, $y = 0, $z = 0, $pitch = true){
if($x instanceof Vector3){
return $this->lookOn($x->x, $x->y + $x->getEyeHeight(), $x->z, $pitch);
}
return $this->lookOffset($x - $this->entity->x, ($this->entity->y + $this->entity->height) - $y, $z - $this->entity->z, $pitch);
}
public function __destruct(){
unset($this->entity);
}
}

View file

@ -1,73 +0,0 @@
<?php
class PathNavigator
{
const PATH_COMPLETED = 0x1;
const PATH_CONTINUE = 0x2;
const PATH_CANT_CONTINUE = 0x3;
public $entity;
/**
* @var Node
*/
private $getTo;
public $currentPointIndex;
public function __construct(Creature &$entity){
$this->entity = $entity;
$this->currentPointIndex = 0;
}
public function reset(){
$this->currentPointIndex = 0;
}
/**
* @param Node[] $path
*/
public function followPath($path){ //TODO remove local var
if(!$this->canContinue()){
console($this->getTo);
console($this->entity);
$diffX = $this->getTo->x - $this->entity->x;
$diffY = 0; //todo ?
$diffZ = $this->getTo->z - $this->entity->z;
$this->entity->moveEntityWithOffset(Utils::getSign($diffX), Utils::getSign($diffY), Utils::getSign($diffZ));
return self::PATH_CANT_CONTINUE;
}
$index = $this->currentPointIndex++;
if($index >= count($path)){
$this->onPathEnd();
console("COMPLETED");
return self::PATH_COMPLETED;
}
$node = $path[$index];
$this->getTo = $node;
$diffX = $node->x - $this->entity->x;
$diffY = 0; //todo ?
$diffZ = $node->z - $this->entity->z;
$this->entity->moveEntityWithOffset(Utils::getSign($diffX), Utils::getSign($diffY), Utils::getSign($diffZ));
return self::PATH_CONTINUE;
}
public function canContinue(){
return empty($this->getTo) || (floor($this->entity->x) == floor($this->getTo->x) && floor($this->entity->z) == floor($this->getTo->z));
}
public function onPathEnd(){
$this->currentPointIndex = 0;
unset($this->getTo);
}
public function testLook($pos){
$x = $pos->x - $this->entity->x;
$y = $pos->y - $this->entity->y;
$z = $pos->z - $this->entity->z;
$this->entity->yaw = -atan2($this->entity->speedX, $this->entity->speedZ) * 180 / M_PI;
$this->entity->pitch = $y == 0 ? 0 : rad2deg(-atan2($y, sqrt(pow($x, 2) + pow($z, 2))));
$this->entity->server->query("UPDATE entities SET pitch = ".$this->entity->pitch.", yaw = ".$this->entity->yaw." WHERE EID = ".$this->entity->eid.";");
}
}

View file

@ -2,46 +2,46 @@
abstract class TaskBase
{
public $isStarted = false, $selfCounter = 0;
/**
* Executed when entity starts task. It is recommended to set counter here.
* @param EntityAI $ai
*/
abstract function onStart(EntityAI $ai);
/**
* Every tick until counter will be not equal to 0. It is recommended to update value of counter here.
* @param EntityAI $ai
*/
abstract function onUpdate(EntityAI $ai);
/**
* On task end.
* @param EntityAI $ai
*/
abstract function onEnd(EntityAI $ai);
/**
* Can start the task
* @param EntityAI $ai
*/
abstract function canBeExecuted(EntityAI $ai);
/**
* @return number ticks
*/
function getIdleTimeAfterEnd(){
return 0;
}
function reset(){
$this->isStarted = false;
$this->selfCounter = 0;
}
final function __toString(){
return get_class($this);
}
public $isStarted = false, $selfCounter = 0;
/**
* Executed when entity starts task. It is recommended to set counter here.
* @param EntityAI $ai
*/
abstract function onStart(EntityAI $ai);
/**
* Every tick until counter will be not equal to 0. It is recommended to update value of counter here.
* @param EntityAI $ai
*/
abstract function onUpdate(EntityAI $ai);
/**
* On task end.
* @param EntityAI $ai
*/
abstract function onEnd(EntityAI $ai);
/**
* Can start the task
* @param EntityAI $ai
*/
abstract function canBeExecuted(EntityAI $ai);
/**
* @return number ticks
*/
function getIdleTimeAfterEnd(){
return 0;
}
function reset(){
$this->isStarted = false;
$this->selfCounter = 0;
}
final function __toString(){
return get_class($this);
}
}

View file

@ -2,35 +2,35 @@
class TaskLookAround extends TaskBase
{
private $rotation;
public function onStart(EntityAI $ai)
{
$this->selfCounter = 1;
$this->rotation = mt_rand(-120, 120);
}
private $rotation;
public function onStart(EntityAI $ai)
{
$this->selfCounter = 1;
$this->rotation = mt_rand(-120, 120);
}
public function onEnd(EntityAI $ai)
{
$ai->entity->idleTime = mt_rand(20, 40);
}
public function onEnd(EntityAI $ai)
{
$ai->entity->idleTime = mt_rand(20, 40);
}
public function canBeExecuted(EntityAI $ai)
{
return !$ai->entity->isMoving() && Utils::randomFloat() < 0.02 && !$ai->isStarted("TaskLookAtPlayer") && !$ai->isStarted("TaskTempt"); /*Vanilla value*/
}
public function canBeExecuted(EntityAI $ai)
{
return !$ai->entity->isMoving() && Utils::randomFloat() < 0.02 && !$ai->isStarted("TaskLookAtPlayer") && !$ai->isStarted("TaskTempt"); /*Vanilla value*/
}
public function onUpdate(EntityAI $ai)
{
if($this->rotation === 0){
$this->selfCounter = 0;
}
$v = min(Utils::getSign($this->rotation) * 10, $this->rotation);
$ai->entity->yaw += $v;
$pk = new RotateHeadPacket(); //TODO headYaw auto update
$pk->eid = $ai->entity->eid;
$pk->yaw = $ai->entity->yaw;
$ai->entity->server->api->player->broadcastPacket($ai->entity->level->players, $pk);
$this->rotation -= $v;
}
public function onUpdate(EntityAI $ai)
{
if($this->rotation === 0){
$this->selfCounter = 0;
}
$v = min(Utils::getSign($this->rotation) * 10, $this->rotation);
$ai->entity->yaw += $v;
$pk = new RotateHeadPacket(); //TODO headYaw auto update
$pk->eid = $ai->entity->eid;
$pk->yaw = $ai->entity->yaw;
$ai->entity->server->api->player->broadcastPacket($ai->entity->level->players, $pk);
$this->rotation -= $v;
}
}

View file

@ -2,39 +2,39 @@
class TaskRandomWalk extends TaskBase
{
private $x, $z;
public function onStart(EntityAI $ai)
{
$this->x = mt_rand(-10, 10);
$this->z = mt_rand(-10, 10);
if($this->x === 0 && $this->z === 0){
$this->reset();
return false;
}
$this->selfCounter = floor(5 * Utils::distance($ai->entity, $ai->entity->add($this->x, 0, $this->z)));
}
private $x, $z;
public function onStart(EntityAI $ai)
{
$this->x = mt_rand(-10, 10);
$this->z = mt_rand(-10, 10);
if($this->x === 0 && $this->z === 0){
$this->reset();
return false;
}
$this->selfCounter = floor(5 * Utils::distance($ai->entity, $ai->entity->add($this->x, 0, $this->z)));
}
public function onEnd(EntityAI $ai)
{
$this->x = $this->z = 0;
}
public function onEnd(EntityAI $ai)
{
$this->x = $this->z = 0;
}
public function onUpdate(EntityAI $ai)
{
if($ai->entity instanceof Creeper && $ai->entity->isIgnited()) {
return false; //TODO Better way: block movement
}
--$this->selfCounter;
$ai->mobController->moveNonInstant($this->x, 0, $this->z);
}
public function onUpdate(EntityAI $ai)
{
if($ai->entity instanceof Creeper && $ai->entity->isIgnited()) {
return false; //TODO Better way: block movement
}
--$this->selfCounter;
$ai->mobController->moveNonInstant($this->x, 0, $this->z);
}
public function canBeExecuted(EntityAI $ai)
{
if($ai->entity instanceof Creeper && $ai->entity->isIgnited()) {
return false;
}
return !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskLookAtPlayer") && mt_rand(0, 120) == 0;
}
public function canBeExecuted(EntityAI $ai)
{
if($ai->entity instanceof Creeper && $ai->entity->isIgnited()) {
return false;
}
return !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskLookAtPlayer") && mt_rand(0, 120) == 0;
}
}

View file

@ -4,7 +4,7 @@ class Chicken extends Animal{
const TYPE = MOB_CHICKEN;
public $timeUntilEgg;
function __construct(Level $level, $eid, $class, $type = 0, $data = []){
$this->timeUntilEgg = mt_rand(0,6000) + 6000;
$this->timeUntilEgg = mt_rand(0,6000) + 6000;
parent::__construct($level, $eid, $class, $type, $data);
$this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 4, "generic");
$this->setName('Chicken');
@ -17,11 +17,11 @@ class Chicken extends Animal{
}
public function update(){
parent::update();
if($this->timeUntilEgg-- <= 0 && !$this->isBaby()){
$this->dropAnEgg();
$this->timeUntilEgg = mt_rand(0,6000) + 6000;
}
parent::update();
if($this->timeUntilEgg-- <= 0 && !$this->isBaby()){
$this->dropAnEgg();
$this->timeUntilEgg = mt_rand(0,6000) + 6000;
}
}
public function dropAnEgg(){

View file

@ -16,15 +16,15 @@ class Cow extends Animal{
}
public function interactWith(Entity $e, $action){
if($e->isPlayer() && $action === InteractPacket::ACTION_HOLD){
$slot = $e->player->getHeldItem();
if($slot->getID() === BUCKET && $slot->getMetadata() === 0){
$e->player->removeItem($slot->getID(), $slot->getMetadata(), 1, true); //remove only 1 bucket
$e->player->addItem(BUCKET, 1, 1, true);
return true;
}
}
parent::interactWith($e, $action);
if($e->isPlayer() && $action === InteractPacket::ACTION_HOLD){
$slot = $e->player->getHeldItem();
if($slot->getID() === BUCKET && $slot->getMetadata() === 0){
$e->player->removeItem($slot->getID(), $slot->getMetadata(), 1, true); //remove only 1 bucket
$e->player->addItem(BUCKET, 1, 1, true);
return true;
}
}
parent::interactWith($e, $action);
}
public function getDrops(){

View file

@ -15,47 +15,47 @@ class Pig extends Animal implements Rideable{
* @return boolean
*/
public function isSaddled(){
return (boolean) $this->getState();
return (boolean) $this->getState();
}
/**
* @param boolean $value
*/
public function setSaddled($value = null){
$this->setState($value === null ? !$this->getState() : $value);
$this->setState($value === null ? !$this->getState() : $value);
}
public function canRide($e){
return $this->isSaddled() && !($this->linkedEntity instanceof Entity);
return $this->isSaddled() && !($this->linkedEntity instanceof Entity);
}
public function interactWith(Entity $e, $action)
{
if($e->isPlayer() && $action === InteractPacket::ACTION_HOLD){
$slot = $e->player->getHeldItem();
if($this->canRide($e)){
$this->linkedEntity = $e;
$e->isRiding = true;
$this->linkEntity($e, SetEntityLinkPacket::TYPE_RIDE);
return true;
}
if($e->isRiding && $this->linkedEntity->eid = $e->eid){
$this->linkEntity($e, SetEntityLinkPacket::TYPE_REMOVE);
$e->isRiding = false;
$this->linkedEntity = 0;
return true;
}
if($slot->getID() === SADDLE){
if(!$this->isSaddled()){
$e->player->removeItem($slot->getID(), 0, 1);
$this->setSaddled(1);
}
return true; //avoid further interactions
}
}
return parent::interactWith($e, $action);
}
public function interactWith(Entity $e, $action)
{
if($e->isPlayer() && $action === InteractPacket::ACTION_HOLD){
$slot = $e->player->getHeldItem();
if($this->canRide($e)){
$this->linkedEntity = $e;
$e->isRiding = true;
$this->linkEntity($e, SetEntityLinkPacket::TYPE_RIDE);
return true;
}
if($e->isRiding && $this->linkedEntity->eid = $e->eid){
$this->linkEntity($e, SetEntityLinkPacket::TYPE_REMOVE);
$e->isRiding = false;
$this->linkedEntity = 0;
return true;
}
if($slot->getID() === SADDLE){
if(!$this->isSaddled()){
$e->player->removeItem($slot->getID(), 0, 1);
$this->setSaddled(1);
}
return true; //avoid further interactions
}
}
return parent::interactWith($e, $action);
}
public function isFood($id){
return $id === POTATO || $id === CARROT || $id === BEETROOT;

View file

@ -1,7 +1,7 @@
<?php
class Sheep extends Animal{
public $color;
public $color;
const TYPE = MOB_SHEEP;
function __construct(Level $level, $eid, $class, $type = 0, $data = []){
parent::__construct($level, $eid, $class, $type, $data);
@ -20,10 +20,10 @@ class Sheep extends Animal{
}
public function createSaveData(){
$data = parent::createSaveData();
$data["Color"] = @$this->data["Color"];
$data["Sheared"] = @$this->data["Sheared"];
return $data;
$data = parent::createSaveData();
$data["Color"] = @$this->data["Color"];
$data["Sheared"] = @$this->data["Sheared"];
return $data;
}
public function eatGrass(){
@ -41,16 +41,16 @@ class Sheep extends Animal{
}
public function setSheared($v = null){
$this->data["Sheared"] = $v === null ? !$this->isSheared() : $v;
$this->updateMetadata();
$this->data["Sheared"] = $v === null ? !$this->isSheared() : $v;
$this->updateMetadata();
}
public function isSheared(){
return $this->data["Sheared"];
return $this->data["Sheared"];
}
public function getColor(){
return $this->data["Color"] & 16; //color === 16 -> color = 0, color === 17 -> color = 1 ...
return $this->data["Color"] & 16; //color === 16 -> color = 0, color === 17 -> color = 1 ...
}
public function getDrops(){
@ -64,21 +64,21 @@ class Sheep extends Animal{
}
public function interactWith(Entity $e, $action){
if($e->isPlayer() && $action === InteractPacket::ACTION_HOLD){
$slot = $e->player->getHeldItem();
if($slot->getID() === SHEARS){
if(!$this->isSheared()){
$this->setSheared(1);
$this->server->api->entity->drop($this, BlockAPI::getItem(WOOL, $this->getColor(), mt_rand(1, 3)));
if($e->isPlayer() && $action === InteractPacket::ACTION_HOLD){
$slot = $e->player->getHeldItem();
if($slot->getID() === SHEARS){
if(!$this->isSheared()){
$this->setSheared(1);
$this->server->api->entity->drop($this, BlockAPI::getItem(WOOL, $this->getColor(), mt_rand(1, 3)));
//$this->server->schedule(20, [$this, "eatGrass"]);
if($slot->getMetadata() >= $slot->getMaxDurability()){
$this->removeItem($slot->getID(), $slot->getMetadata(), $slot->count, true);
}
}
return true;
}
}
return parent::interactWith($e, $action);
if($slot->getMetadata() >= $slot->getMaxDurability()){
$this->removeItem($slot->getID(), $slot->getMetadata(), $slot->count, true);
}
}
return true;
}
}
return parent::interactWith($e, $action);
}
public function getMetadata(){
@ -93,19 +93,19 @@ class Sheep extends Animal{
public function sheepColor(){ //a method from 0.8.1
$c = mt_rand(0,100);
if($c <= 4){
return 0xF; //white
return 0xF; //white
}
if($c <= 9){
return 0x7;
return 0x7;
}
if($c <= 14){
return 0x8;
return 0x8;
}
if($c <= 17){
return 0xC;
return 0xC;
}
if(mt_rand(0, 500)){
return 0x0;
return 0x0;
}
return 0x0;
}

View file

@ -16,28 +16,28 @@ class Creeper extends Monster{
public function setIgnited($v = null){
$this->setState($v === null ? !$this->getState() : $v);
$this->setState($v === null ? !$this->getState() : $v);
}
/**
* @return boolean
*/
public function isIgnited(){
return (boolean)$this->getState();
return (boolean)$this->getState();
}
public function interactWith(Entity $e, $action){
if($e->isPlayer() && $action === InteractPacket::ACTION_HOLD){
$slot = $e->player->getHeldItem();
if($slot->getID() === FLINT_AND_STEEL && !$this->isIgnited()){
if($slot->useOn($e) and $slot->getMetadata() >= $slot->getMaxDurability()){
$e->player->removeItem($slot->getID(), $slot->getMetadata(), $slot->count, true);
}
$this->ignite();
return true;
}
}
return parent::interactWith($e, $action);
if($e->isPlayer() && $action === InteractPacket::ACTION_HOLD){
$slot = $e->player->getHeldItem();
if($slot->getID() === FLINT_AND_STEEL && !$this->isIgnited()){
if($slot->useOn($e) and $slot->getMetadata() >= $slot->getMaxDurability()){
$e->player->removeItem($slot->getID(), $slot->getMetadata(), $slot->count, true);
}
$this->ignite();
return true;
}
}
return parent::interactWith($e, $action);
}
public function ignite(){
@ -55,16 +55,16 @@ class Creeper extends Monster{
parent::update();
}
public function explode()
{
if($this->closed){
return false;
}
$this->setIgnited(0);
$explosion = new Explosion($this, 3);
$explosion->explode();
$this->close();
}
public function explode()
{
if($this->closed){
return false;
}
$this->setIgnited(0);
$explosion = new Explosion($this, 3);
$explosion->explode();
$this->close();
}
public function getDrops(){
return [

View file

@ -11,28 +11,28 @@ class Skeleton extends Monster{
}
public function updateBurning(){
if($this->fire > 0 or $this->server->api->time->getPhase($this->level->getTime()) != "day"){
return false;
}
for($y = $this->y; $y < 129; $y++){
$block = $this->level->getBlockWithoutVector($this->x, $y, $this->z);
if($block->isSolid){
return false;
}
}
if($block->getID() === AIR){
$this->fire = 160; //Value from 0.8.1
$this->updateMetadata();
return true;
}else{
return false;
}
if($this->fire > 0 or $this->server->api->time->getPhase($this->level->getTime()) != "day"){
return false;
}
for($y = $this->y; $y < 129; $y++){
$block = $this->level->getBlockWithoutVector($this->x, $y, $this->z);
if($block->isSolid){
return false;
}
}
if($block->getID() === AIR){
$this->fire = 160; //Value from 0.8.1
$this->updateMetadata();
return true;
}else{
return false;
}
}
public function update(){
$this->updateBurning();
parent::update();
$this->updateBurning();
parent::update();
}
public function getDrops(){

View file

@ -11,28 +11,28 @@ class Zombie extends Monster{
}
public function updateBurning(){
if($this->fire > 0 or $this->server->api->time->getPhase($this->level->getTime()) != "day"){
return false;
}
for($y = $this->y; $y < 129; $y++){
$block = $this->level->getBlockWithoutVector($this->x, $y, $this->z);
if($block->isSolid){
return false;
}
}
if($block->getID() === AIR){
$this->fire = 160; //Value from 0.8.1
$this->updateMetadata();
return true;
}else{
return false;
}
if($this->fire > 0 or $this->server->api->time->getPhase($this->level->getTime()) != "day"){
return false;
}
for($y = $this->y; $y < 129; $y++){
$block = $this->level->getBlockWithoutVector($this->x, $y, $this->z);
if($block->isSolid){
return false;
}
}
if($block->getID() === AIR){
$this->fire = 160; //Value from 0.8.1
$this->updateMetadata();
return true;
}else{
return false;
}
}
public function update(){
$this->updateBurning();
parent::update();
$this->updateBurning();
parent::update();
}
public function getDrops(){

View file

@ -20,23 +20,23 @@ class Arrow extends Projectile{
public function shoot($d, $d1, $d2, $f, $f1){ //original name from 0.8.1 IDA decompilation, var names are taken from b1.7.3
$f2 = sqrt($d * $d + $d1 * $d1 + $d2 * $d2);
$d /= $f2;
$d1 /= $f2;
$d2 /= $f2;
$d += $this->random->nextGaussian() * 0.0075 * $f1; //0.0074999998323619366 replaced with 0.0075
$d1 += $this->random->nextGaussian() * 0.0075 * $f1;
$d2 += $this->random->nextGaussian() * 0.0075 * $f1;
$d *= $f;
$d1 *= $f;
$d2 *= $f;
$this->speedX = $d;
$this->speedY = $d1;
$this->speedZ = $d2;
$f3 = sqrt($d * $d + $d2 * $d2);
$this->yaw = (atan2($d, $d2) * 180) / M_PI;
$this->pitch = (atan2($d1, $f3) * 180) / M_PI;
$d /= $f2;
$d1 /= $f2;
$d2 /= $f2;
$d += $this->random->nextGaussian() * 0.0075 * $f1; //0.0074999998323619366 replaced with 0.0075
$d1 += $this->random->nextGaussian() * 0.0075 * $f1;
$d2 += $this->random->nextGaussian() * 0.0075 * $f1;
$d *= $f;
$d1 *= $f;
$d2 *= $f;
$this->speedX = $d;
$this->speedY = $d1;
$this->speedZ = $d2;
$f3 = sqrt($d * $d + $d2 * $d2);
$this->yaw = (atan2($d, $d2) * 180) / M_PI;
$this->pitch = (atan2($d1, $f3) * 180) / M_PI;
$this->update();
//TODO i guess? $ticksInGround = 0;
//TODO i guess? $ticksInGround = 0;
}
public function spawn($player){

View file

@ -49,24 +49,24 @@ class Minecart extends Vehicle{
}
public function interactWith(Entity $e, $action){
if($action === InteractPacket::ACTION_HOLD && $e->isPlayer() && $this->canRide($e)){
$this->linkedEntity = $e;
$e->isRiding = true;
$this->linkEntity($e, SetEntityLinkPacket::TYPE_RIDE);
return true;
}
if($e->isPlayer() && $action === InteractPacket::ACTION_HOLD){
$this->linkEntity($e, SetEntityLinkPacket::TYPE_REMOVE);
$this->linkedEntity = 0;
$e->isRiding = false;
return true;
}
parent::interactWith($e, $action);
if($action === InteractPacket::ACTION_HOLD && $e->isPlayer() && $this->canRide($e)){
$this->linkedEntity = $e;
$e->isRiding = true;
$this->linkEntity($e, SetEntityLinkPacket::TYPE_RIDE);
return true;
}
if($e->isPlayer() && $action === InteractPacket::ACTION_HOLD){
$this->linkEntity($e, SetEntityLinkPacket::TYPE_REMOVE);
$this->linkedEntity = 0;
$e->isRiding = false;
return true;
}
parent::interactWith($e, $action);
}
public function canRide($e)
{
return !($this->linkedEntity instanceof Entity) && !$e->isRiding;
}
public function canRide($e)
{
return !($this->linkedEntity instanceof Entity) && !$e->isRiding;
}
}

View file

@ -20,11 +20,11 @@ class Painting extends Hanging{
}
public function createSaveData(){
$data = parent::createSaveData();
$data["Motive"] = $this->data["Motive"];
return $data;
$data = parent::createSaveData();
$data["Motive"] = $this->data["Motive"];
return $data;
}
public function spawn($player){

View file

@ -9,10 +9,10 @@ class TripodCamera extends Entity{
}
public function interactWith(Entity $e, $action){
if($e->isPlayer() and $action === InteractPacket::ACTION_HOLD){
if($e->isPlayer() and $action === InteractPacket::ACTION_HOLD){
//todo
$this->server->schedule(40, [$this, "close"]);
}
parent::interactWith($e, $action);
}
parent::interactWith($e, $action);
}
}