mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-07-06 10:01:53 +00:00
Added Squids with basic AI
This commit is contained in:
parent
02ba9ffc16
commit
69f841a00c
@ -39,6 +39,7 @@ use pocketmine\entity\Human;
|
|||||||
use pocketmine\entity\Item as DroppedItem;
|
use pocketmine\entity\Item as DroppedItem;
|
||||||
use pocketmine\entity\PrimedTNT;
|
use pocketmine\entity\PrimedTNT;
|
||||||
use pocketmine\entity\Snowball;
|
use pocketmine\entity\Snowball;
|
||||||
|
use pocketmine\entity\Squid;
|
||||||
use pocketmine\entity\Villager;
|
use pocketmine\entity\Villager;
|
||||||
use pocketmine\entity\Zombie;
|
use pocketmine\entity\Zombie;
|
||||||
use pocketmine\event\HandlerList;
|
use pocketmine\event\HandlerList;
|
||||||
@ -2415,6 +2416,7 @@ class Server{
|
|||||||
Entity::registerEntity(Snowball::class);
|
Entity::registerEntity(Snowball::class);
|
||||||
Entity::registerEntity(Villager::class);
|
Entity::registerEntity(Villager::class);
|
||||||
Entity::registerEntity(Zombie::class);
|
Entity::registerEntity(Zombie::class);
|
||||||
|
Entity::registerEntity(Squid::class);
|
||||||
|
|
||||||
Entity::registerEntity(Human::class, true);
|
Entity::registerEntity(Human::class, true);
|
||||||
}
|
}
|
||||||
|
@ -25,6 +25,7 @@
|
|||||||
namespace pocketmine\block;
|
namespace pocketmine\block;
|
||||||
|
|
||||||
use pocketmine\entity\Entity;
|
use pocketmine\entity\Entity;
|
||||||
|
use pocketmine\entity\Squid;
|
||||||
use pocketmine\entity\Villager;
|
use pocketmine\entity\Villager;
|
||||||
use pocketmine\entity\Zombie;
|
use pocketmine\entity\Zombie;
|
||||||
use pocketmine\item\Item;
|
use pocketmine\item\Item;
|
||||||
@ -494,7 +495,7 @@ class Block extends Position implements Metadatable{
|
|||||||
//TODO: Slime
|
//TODO: Slime
|
||||||
[Item::SPAWN_EGG, Zombie::NETWORK_ID],
|
[Item::SPAWN_EGG, Zombie::NETWORK_ID],
|
||||||
//TODO: PigZombie
|
//TODO: PigZombie
|
||||||
//TODO: Replace with Entity constants
|
[Item::SPAWN_EGG, Squid::NETWORK_ID],
|
||||||
|
|
||||||
|
|
||||||
//Seeds
|
//Seeds
|
||||||
|
@ -23,5 +23,9 @@ namespace pocketmine\entity;
|
|||||||
|
|
||||||
|
|
||||||
interface Ageable{
|
interface Ageable{
|
||||||
|
const DATA_AGEABLE_FLAGS = 14;
|
||||||
|
|
||||||
|
const DATA_FLAG_BABY = 0;
|
||||||
|
|
||||||
|
public function isBaby();
|
||||||
}
|
}
|
@ -24,4 +24,14 @@ namespace pocketmine\entity;
|
|||||||
|
|
||||||
abstract class Animal extends Creature implements Ageable{
|
abstract class Animal extends Creature implements Ageable{
|
||||||
|
|
||||||
|
public function initEntity(){
|
||||||
|
parent::initEntity();
|
||||||
|
if($this->getDataProperty(self::DATA_AGEABLE_FLAGS) === null){
|
||||||
|
$this->setDataProperty(self::DATA_AGEABLE_FLAGS, self::DATA_TYPE_BYTE, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isBaby(){
|
||||||
|
return $this->getDataFlag(self::DATA_AGEABLE_FLAGS, self::DATA_FLAG_BABY);
|
||||||
|
}
|
||||||
}
|
}
|
@ -196,12 +196,35 @@ abstract class Entity extends Location implements Metadatable{
|
|||||||
/** @var \pocketmine\event\TimingsHandler */
|
/** @var \pocketmine\event\TimingsHandler */
|
||||||
protected $timings;
|
protected $timings;
|
||||||
|
|
||||||
|
protected $MASK = 0;
|
||||||
|
|
||||||
|
|
||||||
public function __construct(FullChunk $chunk, Compound $nbt){
|
public function __construct(FullChunk $chunk, Compound $nbt){
|
||||||
if($chunk === null or $chunk->getProvider() === null){
|
if($chunk === null or $chunk->getProvider() === null){
|
||||||
throw new ChunkException("Invalid garbage Chunk given to Entity");
|
throw new ChunkException("Invalid garbage Chunk given to Entity");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$PF = 0x200;
|
||||||
|
if($this instanceof Living){
|
||||||
|
$this->MASK |= 0x100 | $PF;
|
||||||
|
}
|
||||||
|
if($this instanceof Ageable){
|
||||||
|
$this->MASK |= 0x400;
|
||||||
|
}
|
||||||
|
if($this instanceof Monster){
|
||||||
|
$this->MASK |= 0x800;
|
||||||
|
}
|
||||||
|
if($this instanceof Animal){
|
||||||
|
$this->MASK |= 0x1000;
|
||||||
|
}
|
||||||
|
if($this instanceof WaterAnimal){
|
||||||
|
$this->MASK |= 0x2000;
|
||||||
|
}
|
||||||
|
if($this instanceof Tameable){
|
||||||
|
$this->MASK |= 0x4000;
|
||||||
|
}
|
||||||
|
//AMBIENT = 0x100 | 0x8000;
|
||||||
|
|
||||||
$this->timings = Timings::getEntityTimings($this);
|
$this->timings = Timings::getEntityTimings($this);
|
||||||
|
|
||||||
if($this->eyeHeight === null){
|
if($this->eyeHeight === null){
|
||||||
|
@ -167,6 +167,9 @@ abstract class Living extends Entity implements Damageable{
|
|||||||
}
|
}
|
||||||
|
|
||||||
if($this->dead !== true and !$this->hasEffect(Effect::WATER_BREATHING) and $this->isInsideOfWater()){
|
if($this->dead !== true and !$this->hasEffect(Effect::WATER_BREATHING) and $this->isInsideOfWater()){
|
||||||
|
if($this instanceof WaterAnimal){
|
||||||
|
$this->setDataProperty(self::DATA_AIR, self::DATA_TYPE_SHORT, 300);
|
||||||
|
}else{
|
||||||
$hasUpdate = true;
|
$hasUpdate = true;
|
||||||
$airTicks = $this->getDataProperty(self::DATA_AIR) - $tickDiff;
|
$airTicks = $this->getDataProperty(self::DATA_AIR) - $tickDiff;
|
||||||
if($airTicks <= -20){
|
if($airTicks <= -20){
|
||||||
@ -176,9 +179,22 @@ abstract class Living extends Entity implements Damageable{
|
|||||||
$this->attack($ev->getFinalDamage(), $ev);
|
$this->attack($ev->getFinalDamage(), $ev);
|
||||||
}
|
}
|
||||||
$this->setDataProperty(self::DATA_AIR, self::DATA_TYPE_SHORT, $airTicks);
|
$this->setDataProperty(self::DATA_AIR, self::DATA_TYPE_SHORT, $airTicks);
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
if($this instanceof WaterAnimal){
|
||||||
|
$hasUpdate = true;
|
||||||
|
$airTicks = $this->getDataProperty(self::DATA_AIR) - $tickDiff;
|
||||||
|
if($airTicks <= -20){
|
||||||
|
$airTicks = 0;
|
||||||
|
|
||||||
|
$ev = new EntityDamageEvent($this, EntityDamageEvent::CAUSE_SUFFOCATION, 2);
|
||||||
|
$this->attack($ev->getFinalDamage(), $ev);
|
||||||
|
}
|
||||||
|
$this->setDataProperty(self::DATA_AIR, self::DATA_TYPE_SHORT, $airTicks);
|
||||||
}else{
|
}else{
|
||||||
$this->setDataProperty(self::DATA_AIR, self::DATA_TYPE_SHORT, 300);
|
$this->setDataProperty(self::DATA_AIR, self::DATA_TYPE_SHORT, 300);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if($this->attackTime > 0){
|
if($this->attackTime > 0){
|
||||||
$this->attackTime -= $tickDiff;
|
$this->attackTime -= $tickDiff;
|
||||||
|
167
src/pocketmine/entity/Squid.php
Normal file
167
src/pocketmine/entity/Squid.php
Normal file
@ -0,0 +1,167 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
* ____ _ _ __ __ _ __ __ ____
|
||||||
|
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||||
|
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||||
|
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||||
|
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* @author PocketMine Team
|
||||||
|
* @link http://www.pocketmine.net/
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace pocketmine\entity;
|
||||||
|
|
||||||
|
|
||||||
|
use pocketmine\event\entity\EntityDamageByEntityEvent;
|
||||||
|
use pocketmine\event\entity\EntityDamageEvent;
|
||||||
|
use pocketmine\item\Item as ItemItem;
|
||||||
|
use pocketmine\math\Vector3;
|
||||||
|
use pocketmine\network\protocol\AddEntityPacket;
|
||||||
|
use pocketmine\network\protocol\EntityEventPacket;
|
||||||
|
use pocketmine\Player;
|
||||||
|
use pocketmine\Server;
|
||||||
|
|
||||||
|
class Squid extends WaterAnimal implements Ageable{
|
||||||
|
const NETWORK_ID = 17;
|
||||||
|
|
||||||
|
public $width = 0.95;
|
||||||
|
public $length = 0.95;
|
||||||
|
public $height = 0.95;
|
||||||
|
|
||||||
|
/** @var Vector3 */
|
||||||
|
public $swimDirection = null;
|
||||||
|
public $swimSpeed = 0.1;
|
||||||
|
|
||||||
|
public function initEntity(){
|
||||||
|
$this->setMaxHealth(5);
|
||||||
|
parent::initEntity();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getName(){
|
||||||
|
return "Squid";
|
||||||
|
}
|
||||||
|
|
||||||
|
public function attack($damage, EntityDamageEvent $source){
|
||||||
|
parent::attack($damage, $source);
|
||||||
|
if($source->isCancelled()){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if($source instanceof EntityDamageByEntityEvent){
|
||||||
|
$this->swimSpeed = mt_rand(150, 350) / 2000;
|
||||||
|
$e = $source->getDamager();
|
||||||
|
$this->swimDirection = (new Vector3($this->x - $e->x, $this->y - $e->y, $this->z - $e->z))->normalize();
|
||||||
|
|
||||||
|
$pk = new EntityEventPacket();
|
||||||
|
$pk->eid = $this->getId();
|
||||||
|
$pk->event = 15;
|
||||||
|
Server::broadcastPacket($this->hasSpawned, $pk);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private function generateRandomDirection(){
|
||||||
|
return new Vector3(mt_rand(-1000, 1000) / 1000, mt_rand(-500, 500) / 1000, mt_rand(-1000, 1000) / 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function onUpdate($currentTick){
|
||||||
|
if($this->closed !== false){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if($currentTick % 20 === 0 and mt_rand(0, 100) < 5){
|
||||||
|
$this->swimDirection = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->lastUpdate = $currentTick;
|
||||||
|
|
||||||
|
$this->timings->startTiming();
|
||||||
|
|
||||||
|
$hasUpdate = parent::onUpdate($currentTick);
|
||||||
|
|
||||||
|
if(!$this->dead){
|
||||||
|
|
||||||
|
if($this->y > 62 and $this->swimDirection !== null){
|
||||||
|
$this->swimDirection->y = -0.5;
|
||||||
|
}
|
||||||
|
|
||||||
|
$inWater = $this->isInsideOfWater();
|
||||||
|
if(!$inWater){
|
||||||
|
$this->motionY -= $this->gravity;
|
||||||
|
$this->swimDirection = null;
|
||||||
|
}elseif($this->swimDirection !== null){
|
||||||
|
if($this->motionX ** 2 + $this->motionY ** 2 + $this->motionZ ** 2 <= $this->swimDirection->lengthSquared()){
|
||||||
|
$this->motionX = $this->swimDirection->x * $this->swimSpeed;
|
||||||
|
$this->motionY = $this->swimDirection->y * $this->swimSpeed;
|
||||||
|
$this->motionZ = $this->swimDirection->z * $this->swimSpeed;
|
||||||
|
}
|
||||||
|
}else{
|
||||||
|
$this->swimDirection = $this->generateRandomDirection();
|
||||||
|
$this->swimSpeed = mt_rand(50, 100) / 2000;
|
||||||
|
}
|
||||||
|
|
||||||
|
$expectedPos = new Vector3($this->x + $this->motionX, $this->y + $this->motionY, $this->z + $this->motionZ);
|
||||||
|
|
||||||
|
$this->move($this->motionX, $this->motionY, $this->motionZ);
|
||||||
|
|
||||||
|
if($expectedPos->distanceSquared($this) > 0){
|
||||||
|
$this->swimDirection = $this->generateRandomDirection();
|
||||||
|
$this->swimSpeed = mt_rand(50, 100) / 2000;
|
||||||
|
}
|
||||||
|
|
||||||
|
$friction = 1 - $this->drag;
|
||||||
|
|
||||||
|
$this->motionX *= $friction;
|
||||||
|
$this->motionY *= 1 - $this->drag;
|
||||||
|
$this->motionZ *= $friction;
|
||||||
|
|
||||||
|
$f = sqrt(($this->motionX ** 2) + ($this->motionZ ** 2));
|
||||||
|
$this->yaw = (-atan2($this->motionX, $this->motionZ) * 180 / M_PI);
|
||||||
|
$this->pitch = (-atan2($f, $this->motionY) * 180 / M_PI);
|
||||||
|
|
||||||
|
if($this->onGround){
|
||||||
|
$this->motionY *= -0.5;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->timings->stopTiming();
|
||||||
|
|
||||||
|
return $hasUpdate or !$this->onGround or $this->motionX != 0 or $this->motionY != 0 or $this->motionZ != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function spawnTo(Player $player){
|
||||||
|
$pk = new AddEntityPacket();
|
||||||
|
$pk->eid = $this->getId();
|
||||||
|
$pk->type = Squid::NETWORK_ID | $this->MASK;
|
||||||
|
$pk->x = $this->x;
|
||||||
|
$pk->y = $this->y;
|
||||||
|
$pk->z = $this->z;
|
||||||
|
$pk->speedX = $this->motionX;
|
||||||
|
$pk->speedY = $this->motionY;
|
||||||
|
$pk->speedZ = $this->motionZ;
|
||||||
|
$pk->yaw = $this->yaw;
|
||||||
|
$pk->pitch = $this->pitch;
|
||||||
|
$pk->metadata = $this->dataProperties;
|
||||||
|
$player->dataPacket($pk);
|
||||||
|
|
||||||
|
parent::spawnTo($player);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getDrops(){
|
||||||
|
return [
|
||||||
|
ItemItem::get(ItemItem::DYE, 0, mt_rand(1, 3))
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
@ -54,7 +54,7 @@ class Villager extends Creature implements NPC, Ageable{
|
|||||||
public function spawnTo(Player $player){
|
public function spawnTo(Player $player){
|
||||||
$pk = new AddEntityPacket();
|
$pk = new AddEntityPacket();
|
||||||
$pk->eid = $this->getId();
|
$pk->eid = $this->getId();
|
||||||
$pk->type = Villager::NETWORK_ID;
|
$pk->type = Villager::NETWORK_ID | $this->MASK;
|
||||||
$pk->x = $this->x;
|
$pk->x = $this->x;
|
||||||
$pk->y = $this->y;
|
$pk->y = $this->y;
|
||||||
$pk->z = $this->z;
|
$pk->z = $this->z;
|
||||||
@ -81,4 +81,8 @@ class Villager extends Creature implements NPC, Ageable{
|
|||||||
public function getProfession(){
|
public function getProfession(){
|
||||||
return $this->namedtag["Profession"];
|
return $this->namedtag["Profession"];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function isBaby(){
|
||||||
|
return $this->getDataFlag(self::DATA_AGEABLE_FLAGS, self::DATA_FLAG_BABY);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
35
src/pocketmine/entity/WaterAnimal.php
Normal file
35
src/pocketmine/entity/WaterAnimal.php
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
*
|
||||||
|
* ____ _ _ __ __ _ __ __ ____
|
||||||
|
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||||
|
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||||
|
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||||
|
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Lesser General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* @author PocketMine Team
|
||||||
|
* @link http://www.pocketmine.net/
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace pocketmine\entity;
|
||||||
|
|
||||||
|
abstract class WaterAnimal extends Creature implements Ageable{
|
||||||
|
public function initEntity(){
|
||||||
|
parent::initEntity();
|
||||||
|
if($this->getDataProperty(self::DATA_AGEABLE_FLAGS) === null){
|
||||||
|
$this->setDataProperty(self::DATA_AGEABLE_FLAGS, self::DATA_TYPE_BYTE, 0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isBaby(){
|
||||||
|
return $this->getDataFlag(self::DATA_AGEABLE_FLAGS, self::DATA_FLAG_BABY);
|
||||||
|
}
|
||||||
|
}
|
@ -41,7 +41,7 @@ class Zombie extends Monster{
|
|||||||
public function spawnTo(Player $player){
|
public function spawnTo(Player $player){
|
||||||
$pk = new AddEntityPacket();
|
$pk = new AddEntityPacket();
|
||||||
$pk->eid = $this->getId();
|
$pk->eid = $this->getId();
|
||||||
$pk->type = Zombie::NETWORK_ID;
|
$pk->type = Zombie::NETWORK_ID | $this->MASK;
|
||||||
$pk->x = $this->x;
|
$pk->x = $this->x;
|
||||||
$pk->y = $this->y;
|
$pk->y = $this->y;
|
||||||
$pk->z = $this->z;
|
$pk->z = $this->z;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user