mirror of
https://github.com/oldminecraftcommunity/NostalgiaCore.git
synced 2026-11-21 03:59:43 +00:00
rewrite of interact system, bugfixes, ai tweaks
This commit is contained in:
parent
f5222be9d2
commit
ac1a1b2449
62 changed files with 649 additions and 491 deletions
|
|
@ -6,13 +6,18 @@ 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 function isBaby(){
|
||||
if(!isset($this->data["IsBaby"])){
|
||||
$this->data["IsBaby"] = false;
|
||||
}
|
||||
return $this->data["IsBaby"];
|
||||
}
|
||||
|
||||
public function spawnChild(){
|
||||
//TODO
|
||||
//$c->parent = $this->eid;
|
||||
}
|
||||
public function getMetadata(){
|
||||
$d = parent::getMetadata();
|
||||
if(!isset($this->data["IsBaby"])){
|
||||
|
|
@ -21,6 +26,30 @@ abstract class Animal extends Creature implements Ageable, Breedable{
|
|||
$d[14]["value"] = $this->isBaby();
|
||||
return $d;
|
||||
}
|
||||
|
||||
public function isInLove(){
|
||||
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);
|
||||
}
|
||||
|
||||
public function counterUpdate(){
|
||||
parent::counterUpdate();
|
||||
if($this->isInLove()){
|
||||
--$this->inLove;
|
||||
}
|
||||
}
|
||||
|
||||
public function environmentUpdate(){
|
||||
if($this->server->api->getProperty("spawn-animals") !== true){
|
||||
$this->close();
|
||||
|
|
|
|||
|
|
@ -2,4 +2,8 @@
|
|||
|
||||
interface Breedable{
|
||||
public function isFood($id); //original name
|
||||
|
||||
public function spawnChild();
|
||||
|
||||
public function isInLove();
|
||||
}
|
||||
|
|
@ -8,6 +8,7 @@ class Entity extends Position{
|
|||
public $canBeAttacked;
|
||||
public $moveTime, $lookTime, $idleTime;
|
||||
public $needsUpdate;
|
||||
public $speedModifer;
|
||||
/**
|
||||
* @var AxisAlignedBB
|
||||
*/
|
||||
|
|
@ -40,12 +41,13 @@ class Entity extends Position{
|
|||
public $player;
|
||||
public $fallY;
|
||||
public $fallStart;
|
||||
private $state;
|
||||
private $tickCounter;
|
||||
private $speedMeasure = array(0, 0, 0, 0, 0, 0, 0);
|
||||
public $server;
|
||||
private $isStatic;
|
||||
public $level;
|
||||
public $isInMinecart = false;
|
||||
public $isRiding = false;
|
||||
public $lastUpdate;
|
||||
public $linkedEntity = null;
|
||||
public $check = true;
|
||||
|
|
@ -54,10 +56,12 @@ class Entity extends Position{
|
|||
public $random;
|
||||
public $radius;
|
||||
public $inAction = false;
|
||||
|
||||
function __construct(Level $level, $eid, $class, $type = 0, $data = array()){
|
||||
$this->random = new Random();
|
||||
$this->canBeAttacked = false;
|
||||
$this->level = $level;
|
||||
$this->speedModifer = 0.7;
|
||||
$this->fallY = false;
|
||||
$this->fallStart = false;
|
||||
$this->server = ServerAPI::request();
|
||||
|
|
@ -79,6 +83,7 @@ class Entity extends Position{
|
|||
$this->closed = false;
|
||||
$this->isStatic = false;
|
||||
$this->name = "";
|
||||
$this->state = $this->data["State"] = isset($this->data["State"]) ? $this->data["State"] : 0;
|
||||
$this->tickCounter = 0;
|
||||
$this->server->query("INSERT OR REPLACE INTO entities (EID, level, type, class, health, hasUpdate) VALUES (".$this->eid.", '".$this->level->getName()."', ".$this->type.", ".$this->class.", ".$this->health.", 0);");
|
||||
$this->x = isset($this->data["x"]) ? (float) $this->data["x"] : 0;
|
||||
|
|
@ -99,6 +104,7 @@ class Entity extends Position{
|
|||
case ENTITY_PLAYER:
|
||||
$this->player = $this->data["player"];
|
||||
$this->setHealth($this->health, "generic");
|
||||
$this->speedModifer = 1;
|
||||
$this->width = 1.2;
|
||||
$this->height = 1.9;
|
||||
$this->canBeAttacked = true;
|
||||
|
|
@ -341,19 +347,20 @@ class Entity extends Position{
|
|||
for($y = $startY; $y <= $endY; ++$y){
|
||||
for($x = $startX; $x <= $endX; ++$x){
|
||||
for($z = $startZ; $z <= $endZ; ++$z){
|
||||
$b = $this->level->getBlock(new Vector3($x, $y, $z));
|
||||
$pos = new Vector3($x, $y, $z);
|
||||
$b = $this->level->getBlock($pos);
|
||||
switch($b->getID()){
|
||||
case WATER:
|
||||
case STILL_WATER: //Drowing
|
||||
if($this->fire > 0 and $this->inBlock(new Vector3($x, $y, $z))){
|
||||
if($this->fire > 0 and $this->inBlock($pos)){
|
||||
$this->fire = 0;
|
||||
$this->updateMetadata();
|
||||
}
|
||||
if($this->air <= 0){
|
||||
$this->harm(2, "water");
|
||||
$hasUpdate = true;
|
||||
}elseif($x == ($endX - 1) and $y == $endY and $z == ($endZ - 1) and ($this->class === ENTITY_MOB or $this->class === ENTITY_PLAYER) and $waterDone === false){
|
||||
$this->air -= 10;
|
||||
}elseif($x == ($endX - 1) and $y == $endY and $z == ($endZ - 1) and ($this->class === ENTITY_MOB or $this->class === ENTITY_PLAYER) and !$waterDone){
|
||||
$this->air -= 1;
|
||||
$waterDone = true;
|
||||
$this->updateMetadata();
|
||||
$hasUpdate = true;
|
||||
|
|
@ -361,7 +368,7 @@ class Entity extends Position{
|
|||
break;
|
||||
case LAVA: //Lava damage
|
||||
case STILL_LAVA:
|
||||
if($this->inBlock(new Vector3($x, $y, $z))){
|
||||
if($this->inBlock($pos)){
|
||||
$this->harm(5, "lava");
|
||||
$this->fire = 300;
|
||||
$this->updateMetadata();
|
||||
|
|
@ -369,7 +376,7 @@ class Entity extends Position{
|
|||
}
|
||||
break;
|
||||
case FIRE: //Fire block damage
|
||||
if($this->inBlock(new Vector3($x, $y, $z))){
|
||||
if($this->inBlock($pos)){
|
||||
$this->harm(1, "fire");
|
||||
$this->fire = 300;
|
||||
$this->updateMetadata();
|
||||
|
|
@ -377,13 +384,13 @@ class Entity extends Position{
|
|||
}
|
||||
break;
|
||||
case CACTUS: //Cactus damage
|
||||
if($this->touchingBlock(new Vector3($x, $y, $z))){
|
||||
if($this->touchingBlock($pos)){
|
||||
$this->harm(1, "cactus");
|
||||
$hasUpdate = true;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
if($this->inBlock(new Vector3($x, $y, $z), 0.7) and $y == $endY and $b->isTransparent === false and ($this->class === ENTITY_MOB or $this->class === ENTITY_PLAYER)){
|
||||
if($this->inBlock($pos, 0.7) and $y == $endY and $b->isTransparent === false and ($this->class === ENTITY_MOB or $this->class === ENTITY_PLAYER)){
|
||||
$this->harm(1, "suffocation"); //Suffocation
|
||||
$hasUpdate = true;
|
||||
}elseif($x == ($endX - 1) and $y == $endY and $z == ($endZ - 1)){
|
||||
|
|
@ -397,13 +404,6 @@ 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;
|
||||
|
|
@ -525,32 +525,22 @@ class Entity extends Position{
|
|||
$this->y = $ny;
|
||||
$this->speedY -= $this->speedY * $drag;
|
||||
$update = true;
|
||||
}
|
||||
if($this->moveTime > 0){
|
||||
--$this->moveTime;
|
||||
}
|
||||
if($this->lookTime > 0){
|
||||
--$this->lookTime;
|
||||
}
|
||||
if($this->idleTime > 0){
|
||||
--$this->idleTime;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if($support === false){
|
||||
$this->speedY -= $this->class === ENTITY_FALLING ? 0.04 : 0.08; //TODO: replace with $gravity
|
||||
$this->speedY -= $this->class === ENTITY_FALLING ? 0.04 : 0.08; //TODO: replace with $gravity
|
||||
$update = true;
|
||||
}else{
|
||||
//$this->speedX = 0;
|
||||
//$this->speedY = 0;
|
||||
//$this->speedZ = 0;
|
||||
$this->server->api->handle("entity.move", $this);
|
||||
if(($this->class === ENTITY_OBJECT and $this->type !== OBJECT_PRIMEDTNT) or $this->speedY <= 0.1){
|
||||
$update = false;
|
||||
if(($this->class === ENTITY_OBJECT and $this->type !== OBJECT_PRIMEDTNT) or $this->speedY <= 0.1){
|
||||
$this->server->api->handle("entity.motion", $this);
|
||||
}
|
||||
$update = true;
|
||||
}
|
||||
|
||||
if($update === true){
|
||||
|
|
@ -564,7 +554,7 @@ class Entity extends Position{
|
|||
$this->fallY = $y;
|
||||
$this->fallStart = microtime(true);
|
||||
}elseif($this->class === ENTITY_PLAYER and ($this->fallStart + 5) < microtime(true)){
|
||||
if($this->server->api->getProperty("allow-flight") !== true and $this->server->handle("player.flying", $this->player) !== true and $this->isInMinecart === false){
|
||||
if($this->server->api->getProperty("allow-flight") !== true and $this->server->handle("player.flying", $this->player) !== true and $this->isRiding === false){
|
||||
$this->player->close("flying");
|
||||
return;
|
||||
}
|
||||
|
|
@ -595,9 +585,17 @@ class Entity extends Position{
|
|||
}
|
||||
}
|
||||
}
|
||||
if($this->moveTime > 0){
|
||||
--$this->moveTime;
|
||||
}
|
||||
if($this->lookTime > 0){
|
||||
--$this->lookTime;
|
||||
}
|
||||
if($this->idleTime > 0){
|
||||
--$this->idleTime;
|
||||
}
|
||||
|
||||
|
||||
if($this->class !== ENTITY_PLAYER){
|
||||
if($this->class !== ENTITY_PLAYER && $update){
|
||||
$this->updateMovement();
|
||||
}
|
||||
$this->needsUpdate = $hasUpdate;
|
||||
|
|
@ -649,7 +647,27 @@ class Entity extends Position{
|
|||
|
||||
$this->lastUpdate = $now;
|
||||
}
|
||||
|
||||
|
||||
public function interactWith(Entity $e, $action){
|
||||
if($this->class === ENTITY_PLAYER and ($this->server->api->getProperty("pvp") == false or $this->server->difficulty <= 0 or ($e->player->gamemode & 0x01) === 0x01)){
|
||||
return false;
|
||||
}
|
||||
$data["targetentity"] = $this;
|
||||
$data["entity"] = $e;
|
||||
if($this->server->handle("player.interact", $data) !== false && $action === InteractPacket::ACTION_ATTACK && $e->isPlayer()){
|
||||
$slot = $e->player->getHeldItem();
|
||||
$damage = $slot->getDamageAgainstOf($e);
|
||||
$this->harm($damage, $e->eid);
|
||||
if($slot->isTool() === true and ($e->player->gamemode & 0x01) === 0){
|
||||
if($slot->useOn($e) and $slot->getMetadata() >= $slot->getMaxDurability()){
|
||||
$e->player->removeItem($slot->getID(), $slot->getMetadata(), 1, true);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getDirection(){
|
||||
$rotation = ($this->yaw - 90) % 360;
|
||||
if($rotation < 0) {
|
||||
|
|
@ -682,7 +700,7 @@ class Entity extends Position{
|
|||
0 => ["type" => 0, "value" => $flags] --> DATA_FLAGS
|
||||
1 => ["type" => 1, "value" => $this->air] --> Entity Air
|
||||
14 => ["type" => 0, "value" => 1] --> IsBaby, value: 0 => false, 1 => true
|
||||
16 => ["type" => 0, "value" => 0] --> Fuse(TNT), Saddled(Pig)
|
||||
16 => ["type" => 0, "value" => 0] --> Fuse(TNT), Saddled(Pig), Creeper(29 ticks before explosion)
|
||||
17 => ["type" => 6, "value" => [0, 0, 0]] --> Bed Position <?>
|
||||
|
||||
|
||||
|
|
@ -704,11 +722,8 @@ class Entity extends Position{
|
|||
16 => ["type" => 0, "value" => 0],
|
||||
17 => ["type" => 6, "value" => [0, 0, 0]]
|
||||
];
|
||||
if($this->class === ENTITY_MOB){ //ladder fix 2.0
|
||||
//if($this->type === MOB_PIG){ /*Saddled Pig*/
|
||||
//$d[16]["value"] = 0;
|
||||
//}
|
||||
}elseif($this->class === ENTITY_PLAYER){
|
||||
$d[16]["value"] = $this->data["State"];
|
||||
if($this->class === ENTITY_PLAYER){
|
||||
if($this->player->isSleeping !== false){
|
||||
$d[16]["value"] = 2;
|
||||
$d[17]["value"] = [$this->player->isSleeping->x, $this->player->isSleeping->y, $this->player->isSleeping->z];
|
||||
|
|
@ -856,7 +871,7 @@ class Entity extends Position{
|
|||
public function updatePosition(){
|
||||
$this->server->query("UPDATE entities SET level = '".$this->level->getName()."', x = ".$this->x.", y = ".$this->y.", z = ".$this->z.", pitch = ".$this->pitch.", yaw = ".$this->yaw." WHERE EID = ".$this->eid.";");
|
||||
$this->sendMoveUpdate();
|
||||
$this->sendMotion();
|
||||
//$this->sendMotion();
|
||||
$this->updateAABB();
|
||||
}
|
||||
|
||||
|
|
@ -960,7 +975,17 @@ class Entity extends Position{
|
|||
public function harm($dmg, $cause = "generic", $force = false){
|
||||
return $this->setHealth(max(-128, $this->getHealth() - ((int) $dmg)), $cause, $force);
|
||||
}
|
||||
|
||||
|
||||
public function setState($v){
|
||||
$this->state = $v;
|
||||
$this->data["State"] = $v;
|
||||
$this->updateMetadata();
|
||||
}
|
||||
|
||||
public function getState(){
|
||||
return $this->state;
|
||||
}
|
||||
|
||||
public function heal($health, $cause = "generic"){
|
||||
return $this->setHealth(min(20, $this->getHealth() + ((int) $health)), $cause);
|
||||
}
|
||||
|
|
@ -974,6 +999,21 @@ class Entity extends Position{
|
|||
$this->server->api->player->broadcastPacket($this->level->players, $pk);
|
||||
}
|
||||
|
||||
public function linkEntity(Entity $e, $type){
|
||||
if($e->isPlayer()){
|
||||
$pk = new SetEntityLinkPacket();
|
||||
$pk->rider = $this->eid;
|
||||
$pk->riding = $type === SetEntityLinkPacket::TYPE_REMOVE ? 0 : $e->eid;
|
||||
$pk->type = $type;
|
||||
$e->player->dataPacket($pk);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function isPlayer(){
|
||||
return $this->player instanceof Player;
|
||||
}
|
||||
|
||||
public function sendMoveUpdate(){
|
||||
if($this->class === ENTITY_PLAYER){
|
||||
$this->player->teleport(new Vector3($this->x, $this->y, $this->z));
|
||||
|
|
@ -989,6 +1029,17 @@ class Entity extends Position{
|
|||
$this->server->api->player->broadcastPacket($this->level->players, $pk);
|
||||
}
|
||||
|
||||
public function moveEntityWithOffset($oX, $oY, $oZ){
|
||||
$oX = $oX === 0 ? $this->speedX : $this->getSpeedModifer() * $oX * $this->getSpeed();
|
||||
$oY = $oY === 0 ? $this->speedY : $this->getSpeedModifer() * $oY * $this->getSpeed();
|
||||
$oZ = $oZ === 0 ? $this->speedZ : $this->getSpeedModifer() * $oZ * $this->getSpeed();
|
||||
$this->setVelocity($oX, $oY, $oZ);
|
||||
}
|
||||
|
||||
public function getSpeedModifer(){
|
||||
return $this->speedModifer;
|
||||
}
|
||||
|
||||
public function setHealth($health, $cause = "generic", $force = false){
|
||||
if(!$this->canBeAttacked){
|
||||
return false;
|
||||
|
|
@ -1109,7 +1160,11 @@ class Entity extends Position{
|
|||
$this->height = $h;
|
||||
$this->radius = $w / 2;
|
||||
}
|
||||
|
||||
|
||||
public function setSpeed($s){
|
||||
$this->speed = $s;
|
||||
}
|
||||
|
||||
public function knockBack($d, $d1)
|
||||
{
|
||||
$f = sqrt($d * $d + $d1 * $d1);
|
||||
|
|
|
|||
7
src/entity/Pathfindable.php
Normal file
7
src/entity/Pathfindable.php
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
<?php
|
||||
/*
|
||||
Illegal features
|
||||
*/
|
||||
interface Pathfindable{
|
||||
function getPathFinder();
|
||||
}
|
||||
|
|
@ -1,5 +1,5 @@
|
|||
<?php
|
||||
|
||||
interface Rideable{
|
||||
|
||||
public function canRide($e);
|
||||
}
|
||||
13
src/entity/WalkingEntity.php
Normal file
13
src/entity/WalkingEntity.php
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
abstract class WalkingEntity extends Entity{
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param int $tickDiff
|
||||
*
|
||||
* @return null|Vector3
|
||||
*/
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -5,12 +5,12 @@ 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 && !($ai->lastTask instanceof TaskLookAround);
|
||||
return !$ai->entity->isMoving() && $ai->entity->lookTime <= 0 && mt_rand(0,30) == 1 && $ai->entity->idleTime <= 0;
|
||||
}
|
||||
|
||||
public function wasExecuted(EntityAI $ai){
|
||||
|
||||
return $ai->entity->lookTime > 0;
|
||||
return $ai->entity->lookTime > 0 && $ai->entity->moveTime <= 0;
|
||||
}
|
||||
|
||||
public function onStart(EntityAI $ai){
|
||||
|
|
@ -24,12 +24,12 @@ class TaskLookAround extends TaskBase
|
|||
//60 - 30 = 90deg rot
|
||||
//30 - 15 = -45deg rot
|
||||
if($ai->entity->lookTime === 1){
|
||||
$ai->entity->idleTime = mt_rand(40, 200);
|
||||
$ai->entity->idleTime = mt_rand(10, 40);
|
||||
}
|
||||
if(Utils::in_range($ai->entity->lookTime, 105, 120) || Utils::in_range($ai->entity->lookTime, 15, 30)){
|
||||
if(Utils::in_range($ai->entity->lookTime, 45, 60) || Utils::in_range($ai->entity->lookTime, 0, 15)){
|
||||
$ai->entity->yaw -= 3;
|
||||
}
|
||||
if(Utils::in_range($ai->entity->lookTime, 50, 80)){
|
||||
if(Utils::in_range($ai->entity->lookTime, 15, 45)){
|
||||
$ai->entity->yaw += 3;
|
||||
}
|
||||
|
||||
|
|
@ -44,4 +44,3 @@ class TaskLookAround extends TaskBase
|
|||
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -5,19 +5,22 @@ class TaskRandomWalk extends TaskBase
|
|||
private $x, $z, $delay = 10, $newPos, $looking = false;
|
||||
public function shouldBeExecuted(EntityAI $ai)
|
||||
{
|
||||
if($ai->entity instanceof Creeper && $ai->entity->isIgnited()){
|
||||
return false;
|
||||
}
|
||||
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;
|
||||
return $ai->entity->moveTime > 0 && ($this->x != 0 || $this->z != 0);
|
||||
}
|
||||
public function onStart(EntityAI $ai)
|
||||
{
|
||||
$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);
|
||||
$ai->entity->moveTime = floor(5 * Utils::distance($ai->entity, $this->newPos));
|
||||
}
|
||||
|
||||
public function onUpdate(EntityAI $ai)
|
||||
|
|
@ -36,12 +39,12 @@ class TaskRandomWalk extends TaskBase
|
|||
}
|
||||
}
|
||||
if($ai->entity->moveTime <= 1){
|
||||
$ai->entity->idleTime = mt_rand(10,100);
|
||||
$ai->entity->idleTime = mt_rand(10,40);
|
||||
$this->x = 0;
|
||||
$this->z = 0;
|
||||
$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)));
|
||||
}
|
||||
$ai->entity->moveEntityWithOffset(($this->x > 0 ? 1 : ($this->x < 0 ? -1 : 0)), 0, ($this->z > 0 ? 1 : ($this->z < 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.";");
|
||||
|
|
@ -50,9 +53,8 @@ class TaskRandomWalk extends TaskBase
|
|||
$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->yaw = -atan2($x, $z) * 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.";");
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -8,6 +8,7 @@ class Chicken extends Animal{
|
|||
$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);
|
||||
$this->setSpeed(0.25);
|
||||
$this->update();
|
||||
}
|
||||
public function isFood($id){
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ class Cow extends Animal{
|
|||
$this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 10, "generic");
|
||||
$this->setSize($this->isBaby() ? 0.45 : 0.9 , $this->isBaby() ? 0.7 : 1.4);
|
||||
$this->setName("Cow");
|
||||
$this->setSpeed(0.2);
|
||||
$this->update();
|
||||
}
|
||||
|
||||
|
|
@ -14,6 +15,18 @@ class Cow extends Animal{
|
|||
return $id === WHEAT;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
public function getDrops(){
|
||||
return $this->isBaby() ? parent::getDrops() : [
|
||||
[LEATHER, 0, mt_rand(0,2)],
|
||||
|
|
|
|||
|
|
@ -1,30 +1,68 @@
|
|||
<?php
|
||||
class Pig extends Animal implements Rideable{
|
||||
const TYPE = MOB_PIG;
|
||||
|
||||
public function __construct(Level $level, $eid, $class, $type = 0, $data = []){
|
||||
parent::__construct($level, $eid, $class, $type, $data);
|
||||
$this->setHealth(isset($this->data["Health"]) ? $this->data["Health"]:10, "generic");
|
||||
$this->server = ServerAPI::request();
|
||||
$this->setSize($this->isBaby() ? 0.45 : 0.9, $this->isBaby() ? 0.45 : 0.9);
|
||||
$this->setName("Pig");
|
||||
$this->setSpeed(0.25);
|
||||
$this->update();
|
||||
}
|
||||
|
||||
public function getMetadata(){
|
||||
$d = parent::getMetadata();
|
||||
if(!isset($this->data["Saddled"])){
|
||||
$this->data["Saddled"] = 0;
|
||||
}
|
||||
$d[16]["value"] = ((int)$this->data["Saddled"]);
|
||||
return $d;
|
||||
/**
|
||||
* @return boolean
|
||||
*/
|
||||
public function isSaddled(){
|
||||
return (boolean) $this->getState();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param boolean $value
|
||||
*/
|
||||
public function setSaddled($value = null){
|
||||
$this->setState($value === null ? !$this->getState() : $value);
|
||||
}
|
||||
|
||||
public function canRide($e){
|
||||
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 isFood($id){
|
||||
return $id === POTATO || $id === CARROT || $id === BEETROOT;
|
||||
}
|
||||
|
||||
public function getDrops(){
|
||||
return $this->isBaby() ? parent::getDrops() : ($this->data["Saddled"] ? [
|
||||
return $this->isBaby() ? parent::getDrops() : ($this->isSaddled() ? [
|
||||
[($this->fire > 0 ? COOKED_PORKCHOP : RAW_PORKCHOP), 0, mt_rand(0,2)],
|
||||
[SADDLE, 0, 1]
|
||||
] : [
|
||||
|
|
|
|||
|
|
@ -1,18 +1,34 @@
|
|||
<?php
|
||||
|
||||
class Sheep extends Animal{
|
||||
public $color;
|
||||
const TYPE = MOB_SHEEP;
|
||||
function __construct(Level $level, $eid, $class, $type = 0, $data = []){
|
||||
parent::__construct($level, $eid, $class, $type, $data);
|
||||
$this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 8, "generic");
|
||||
$this->setName("Sheep");
|
||||
$this->data["Color"] = isset($this->data["Color"]) ? $this->data["Color"] : $this->sheepColor();
|
||||
$this->setSize($this->isBaby() ? 0.45 : 0.9, $this->isBaby() ? 0.675 : 1.3);
|
||||
$this->setSpeed(0.25);
|
||||
$this->update();
|
||||
}
|
||||
|
||||
public function setSheared($v = null){
|
||||
$this->data["Sheared"] = $v === null ? !$this->isSheared() : $v;
|
||||
$this->updateMetadata();
|
||||
}
|
||||
|
||||
public function isSheared(){
|
||||
return $this->data["Sheared"];
|
||||
}
|
||||
|
||||
public function getColor(){
|
||||
return $this->data["Color"] & 16; //color === 16 -> color = 0, color === 17 -> color = 1 ...
|
||||
}
|
||||
|
||||
public function getDrops(){
|
||||
return $this->isBaby() ? parent::getDrops() : [
|
||||
[WOOL, $this->data["Color"] & 0x0F, 1],
|
||||
[WOOL, $this->getColor() & 0x0F, 1],
|
||||
];
|
||||
}
|
||||
|
||||
|
|
@ -20,40 +36,49 @@ class Sheep extends Animal{
|
|||
return $id === WHEAT;
|
||||
}
|
||||
|
||||
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($slot->getMetadata() >= $slot->getMaxDurability()){
|
||||
$this->removeItem($slot->getID(), $slot->getMetadata(), $slot->count, true);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return parent::interactWith($e, $action);
|
||||
}
|
||||
|
||||
public function getMetadata(){
|
||||
$d = parent::getMetadata();
|
||||
if(!isset($this->data["Sheared"])){
|
||||
$this->data["Sheared"] = 0;
|
||||
}
|
||||
if(!isset($this->data["Color"])){ //Make a new random color if it is not in data
|
||||
$this->data["Color"] = $this->sheepColor();
|
||||
}
|
||||
$d[16]["value"] = ($this->data["Sheared"] << 4) | ($this->data["Color"] & 0x0F); //dark manipulations are happening here...
|
||||
$d[16]["value"] = ($this->data["Sheared"] << 4) | ($this->getColor() & 0x0F);
|
||||
return $d;
|
||||
}
|
||||
|
||||
public function sheepColor(){
|
||||
$pink = 0.1558;
|
||||
$brown = 2.85;
|
||||
$lightgrayBlackGray = 14.25;
|
||||
$chance = Utils::randomFloat() * 100;
|
||||
switch($chance){
|
||||
case($chance <= $pink):
|
||||
$color = 6;
|
||||
break;
|
||||
case($chance > $pink and $chance <= ($brown+$pink)):
|
||||
$color = 12;
|
||||
break;
|
||||
case($chance > ($brown+$pink) and $chance <= ($lightgrayBlackGray+$brown+$pink)):
|
||||
$rand = mt_rand(1, 3);
|
||||
if($rand == 1) $color = 15;
|
||||
elseif($rand == 2) $color = 7;
|
||||
else $color = 8;
|
||||
break;
|
||||
default:
|
||||
$color = 0;
|
||||
break;
|
||||
public function sheepColor(){ //a method from 0.8.1
|
||||
$c = mt_rand(0,100);
|
||||
if($c <= 4){
|
||||
return 0xF; //white
|
||||
}
|
||||
return $color;
|
||||
if($c <= 9){
|
||||
return 0x7;
|
||||
}
|
||||
if($c <= 14){
|
||||
return 0x8;
|
||||
}
|
||||
if($c <= 17){
|
||||
return 0xC;
|
||||
}
|
||||
if(mt_rand(0, 500)){
|
||||
return 0x0;
|
||||
}
|
||||
return 0x0;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,40 +1,73 @@
|
|||
<?php
|
||||
class Creeper extends Monster{
|
||||
const TYPE = MOB_CREEPER;
|
||||
const EXPL_TIME = 30;
|
||||
public $timeUntilExplode;
|
||||
function __construct(Level $level, $eid, $class, $type = 0, $data = []){
|
||||
parent::__construct($level, $eid, $class, $type, $data);
|
||||
$this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 16, "generic");
|
||||
$this->setName("Creeper");
|
||||
$this->ignited = 0;
|
||||
$this->setSize(0.6, 1.7);
|
||||
$this->setSpeed(0.25);
|
||||
$this->update();
|
||||
if($this->data["Saddled"] === 1){
|
||||
$this->server->schedule(30, [$this, "updateFuse"], []); //unknown ticks
|
||||
}
|
||||
$this->timeUntilExplode = $this->isIgnited() ? self::EXPL_TIME : 0;
|
||||
}
|
||||
|
||||
public function getMetadata(){
|
||||
$d = parent::getMetadata();
|
||||
if(!isset($this->data["Saddled"])){
|
||||
$this->data["Saddled"] = 0;
|
||||
}
|
||||
$d[16]["value"] = ((int)$this->data["Saddled"]);
|
||||
return $d;
|
||||
|
||||
public function setIgnited($v = null){
|
||||
$this->setState($v === null ? !$this->getState() : $v);
|
||||
}
|
||||
|
||||
public function updateFuse(){
|
||||
if($this->closed === true){
|
||||
return false;
|
||||
}
|
||||
if($this->type === MOB_CREEPER){
|
||||
$this->updateMetadata();
|
||||
$this->close();
|
||||
$explosion = new Explosion($this, 3);
|
||||
$explosion->explode();
|
||||
}
|
||||
/**
|
||||
* @return boolean
|
||||
*/
|
||||
public function isIgnited(){
|
||||
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);
|
||||
}
|
||||
|
||||
public function ignite(){
|
||||
$this->setIgnited(1);
|
||||
$this->timeUntilExplode = self::EXPL_TIME;
|
||||
}
|
||||
|
||||
public function update(){
|
||||
if($this->timeUntilExplode === 1){
|
||||
$this->explode();
|
||||
}
|
||||
if($this->timeUntilExplode >= 0){
|
||||
--$this->timeUntilExplode;
|
||||
}
|
||||
parent::update();
|
||||
}
|
||||
|
||||
public function explode()
|
||||
{
|
||||
if($this->closed){
|
||||
return false;
|
||||
}
|
||||
$this->setIgnited(0);
|
||||
$explosion = new Explosion($this, 3);
|
||||
$explosion->explode();
|
||||
$this->close();
|
||||
}
|
||||
|
||||
public function getDrops(){
|
||||
return $this->data["Saddled"] === 1 ? parent::getDrops() : [
|
||||
return [
|
||||
[GUNPOWDER, 0, mt_rand(0,2)]
|
||||
];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ class PigZombie extends Monster{
|
|||
$this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 12, "generic");
|
||||
$this->setSize(0.3, 1.95);
|
||||
$this->setName("Pigman");
|
||||
$this->setSpeed(0.25);
|
||||
$this->update();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ class Skeleton extends Monster{
|
|||
$this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 10, "generic");
|
||||
$this->setName("Skeleton");
|
||||
$this->setSize(0.6, 1.99);
|
||||
$this->setSpeed(0.25);
|
||||
$this->update();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ class Spider extends Monster{
|
|||
$this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 8, "generic");
|
||||
$this->setName("Spider");
|
||||
$this->setSize(1.4, 0.9);
|
||||
$this->setSpeed(0.3);
|
||||
$this->update();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ class Zombie extends Monster{
|
|||
$this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 12, "generic");
|
||||
$this->setName("Zombie");
|
||||
$this->setSize(0.6, 1.85);
|
||||
$this->setSpeed(0.23);
|
||||
$this->update();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ class Minecart extends Vehicle{
|
|||
$this->x = isset($this->data["TileX"]) ? $this->data["TileX"]:$this->x;
|
||||
$this->y = isset($this->data["TileY"]) ? $this->data["TileY"]:$this->y;
|
||||
$this->z = isset($this->data["TileZ"]) ? $this->data["TileZ"]:$this->z;
|
||||
$this->setHealth(1, "generic");
|
||||
$this->setHealth(3, "generic");
|
||||
//$this->setName((isset($objects[$this->type]) ? $objects[$this->type]:$this->type));
|
||||
$this->width = 1;
|
||||
/*$this->moveVector[Entity::NORTH] = new Vector3(-1, 0, 0);
|
||||
|
|
@ -46,4 +46,25 @@ class Minecart extends Vehicle{
|
|||
$pk->speedZ = $this->speedZ;
|
||||
$player->dataPacket($pk);
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
public function canRide($e)
|
||||
{
|
||||
return !($this->linkedEntity instanceof Entity) && !$e->isRiding;
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue