mirror of
https://github.com/oldminecraftcommunity/NostalgiaCore.git
synced 2026-11-22 20:49:43 +00:00
animal breeding added
This commit is contained in:
parent
426728342f
commit
ffd5fa738c
9 changed files with 135 additions and 36 deletions
|
|
@ -17,6 +17,7 @@ abstract class Animal extends Creature implements Ageable, Breedable{
|
||||||
|
|
||||||
$this->ai->addTask(new TaskTempt());
|
$this->ai->addTask(new TaskTempt());
|
||||||
$this->ai->addTask(new TaskPanic());
|
$this->ai->addTask(new TaskPanic());
|
||||||
|
$this->ai->addTask(new TaskMate());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function harm($dmg, $cause = "generic", $force = false){
|
public function harm($dmg, $cause = "generic", $force = false){
|
||||||
|
|
@ -31,11 +32,21 @@ abstract class Animal extends Creature implements Ageable, Breedable{
|
||||||
}
|
}
|
||||||
|
|
||||||
public function breed(){
|
public function breed(){
|
||||||
$c = $this->spawnChild();
|
if($this->server->dhandle("entity.animal.breed", ["parent" => $this]) !== false){
|
||||||
if($this->server->dhandle("entity.animal.breed", ["parent" => $this, "child" => $c]) !== false){
|
$c = $this->spawnChild();
|
||||||
$c->parent = $this;
|
$c->parent = $this; //TODO save eid to avoid memory leaks?
|
||||||
$this->server->api->entity->spawnToAll($c);
|
$this->server->api->entity->spawnToAll($c);
|
||||||
|
return $c;
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function resetInLove(){
|
||||||
|
$this->inLove = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function canMate($ent){
|
||||||
|
return $ent->eid != $this->eid && $this->type === $ent->type && $this->isInLove() && $ent->isInLove();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function update(){
|
public function update(){
|
||||||
|
|
@ -47,6 +58,11 @@ abstract class Animal extends Creature implements Ageable, Breedable{
|
||||||
}else{
|
}else{
|
||||||
$this->setAge($age);
|
$this->setAge($age);
|
||||||
}
|
}
|
||||||
|
if($this->isInLove() && !isset($this->level->entitiesInLove[$this->eid])){
|
||||||
|
$this->level->entitiesInLove[$this->eid] = true;
|
||||||
|
}else if(!$this->isInLove() && isset($this->level->entitiesInLove[$this->eid])){
|
||||||
|
unset($this->level->entitiesInLove[$this->eid]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getAge(){
|
public function getAge(){
|
||||||
|
|
@ -88,8 +104,10 @@ abstract class Animal extends Creature implements Ageable, Breedable{
|
||||||
public function interactWith(Entity $e, $action){
|
public function interactWith(Entity $e, $action){
|
||||||
if($e->isPlayer() && $action === InteractPacket::ACTION_HOLD){
|
if($e->isPlayer() && $action === InteractPacket::ACTION_HOLD){
|
||||||
$slot = $e->player->getHeldItem();
|
$slot = $e->player->getHeldItem();
|
||||||
if($this->isFood($slot->getID())){
|
if($this->isFood($slot->getID()) && $this->inLove <= 0 && !$this->isBaby()){ //TODO check vanilla
|
||||||
$e->player->removeItem($slot->getID(), $slot->getMetadata(), 1);
|
if(($e->player->gamemode & 0x01) === SURVIVAL){
|
||||||
|
$e->player->removeItem($slot->getID(), $slot->getMetadata(), 1);
|
||||||
|
}
|
||||||
$this->inLove = 600; //600 ticks, original mehod from mcpe
|
$this->inLove = 600; //600 ticks, original mehod from mcpe
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
@ -103,7 +121,7 @@ abstract class Animal extends Creature implements Ageable, Breedable{
|
||||||
--$this->inLove;
|
--$this->inLove;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getDrops(){
|
public function getDrops(){
|
||||||
if($this->isBaby()){
|
if($this->isBaby()){
|
||||||
return [];
|
return [];
|
||||||
|
|
|
||||||
|
|
@ -687,27 +687,7 @@ class Entity extends Position
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
if($this->knockbackTime > 0){
|
$this->counterUpdate();
|
||||||
--$this->knockbackTime;
|
|
||||||
}
|
|
||||||
if($this->delayBeforePickup > 0){
|
|
||||||
--$this->delayBeforePickup;
|
|
||||||
}
|
|
||||||
if($this->moveTime > 0){
|
|
||||||
--$this->moveTime;
|
|
||||||
}
|
|
||||||
if($this->lookTime > 0){
|
|
||||||
--$this->lookTime;
|
|
||||||
}
|
|
||||||
if($this->idleTime > 0){
|
|
||||||
--$this->idleTime;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if($this->inAction){
|
|
||||||
++$this->inActionCounter;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if($this->lastHeadYaw != $this->headYaw){
|
if($this->lastHeadYaw != $this->headYaw){
|
||||||
$this->sendHeadYaw();
|
$this->sendHeadYaw();
|
||||||
|
|
@ -1025,7 +1005,30 @@ class Entity extends Position
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function counterUpdate(){
|
||||||
|
if($this->knockbackTime > 0){
|
||||||
|
--$this->knockbackTime;
|
||||||
|
}
|
||||||
|
if($this->delayBeforePickup > 0){
|
||||||
|
--$this->delayBeforePickup;
|
||||||
|
}
|
||||||
|
if($this->moveTime > 0){
|
||||||
|
--$this->moveTime;
|
||||||
|
}
|
||||||
|
if($this->lookTime > 0){
|
||||||
|
--$this->lookTime;
|
||||||
|
}
|
||||||
|
if($this->idleTime > 0){
|
||||||
|
--$this->idleTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if($this->inAction){
|
||||||
|
++$this->inActionCounter;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public function close()
|
public function close()
|
||||||
{
|
{
|
||||||
if($this->closed === false){
|
if($this->closed === false){
|
||||||
|
|
|
||||||
|
|
@ -28,12 +28,16 @@ abstract class Living extends Entity implements Damageable, Pathfindable{
|
||||||
return $this->path != null;
|
return $this->path != null;
|
||||||
}
|
}
|
||||||
public function eatGrass(){}
|
public function eatGrass(){}
|
||||||
public function __destruct()
|
|
||||||
{
|
public function close(){
|
||||||
parent::__destruct();
|
parent::close();
|
||||||
unset($this->pathFollower->entity);
|
unset($this->pathFollower->entity);
|
||||||
unset($this->ai->entity);
|
unset($this->ai->entity);
|
||||||
|
unset($this->ai->mobController->entity);
|
||||||
|
unset($this->ai);
|
||||||
|
unset($this->parent);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function canBeShot(){
|
public function canBeShot(){
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ class TaskLookAround extends TaskBase
|
||||||
|
|
||||||
public function canBeExecuted(EntityAI $ai)
|
public function canBeExecuted(EntityAI $ai)
|
||||||
{
|
{
|
||||||
return !$ai->entity->inPanic && !$ai->entity->isMoving() && lcg_value() < 0.02 && !$ai->isStarted("TaskLookAtPlayer") && !$ai->isStarted("TaskTempt") && !$ai->entity->hasPath(); /*Vanilla value*/
|
return !$ai->entity->inPanic && !$ai->entity->isMoving() && lcg_value() < 0.02 && !$ai->isStarted("TaskLookAtPlayer") && !$ai->isStarted("TaskMate") && !$ai->isStarted("TaskTempt") && !$ai->entity->hasPath(); /*Vanilla value*/
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onUpdate(EntityAI $ai)
|
public function onUpdate(EntityAI $ai)
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ class TaskLookAtPlayer extends TaskBase{
|
||||||
public $target = false;
|
public $target = false;
|
||||||
private $state, $yaw, $pitch;
|
private $state, $yaw, $pitch;
|
||||||
public function canBeExecuted(EntityAI $ai){
|
public function canBeExecuted(EntityAI $ai){
|
||||||
return !$ai->entity->inPanic && lcg_value() < 0.02 && !$ai->entity->isMovingHorizontally() && !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskTempt") && !$ai->isStarted("TaskPanic") && !$ai->entity->hasPath();
|
return !$ai->entity->inPanic && lcg_value() < 0.02 && !$ai->entity->isMovingHorizontally() && !$ai->isStarted("TaskMate") && !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskTempt") && !$ai->isStarted("TaskPanic") && !$ai->entity->hasPath();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function findTarget($e, $r){
|
protected function findTarget($e, $r){
|
||||||
|
|
|
||||||
72
src/entity/ai/tasks/TaskMate.php
Normal file
72
src/entity/ai/tasks/TaskMate.php
Normal file
|
|
@ -0,0 +1,72 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
class TaskMate extends \TaskBase
|
||||||
|
{
|
||||||
|
public $targetMateID;
|
||||||
|
|
||||||
|
public function onStart(EntityAI $ai)
|
||||||
|
{
|
||||||
|
$this->selfCounter = 60;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onEnd(EntityAI $ai)
|
||||||
|
{
|
||||||
|
$this->selfCounter = 0;
|
||||||
|
$this->targetMateID = null;
|
||||||
|
$ai->entity->pitch = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onUpdate(EntityAI $ai)
|
||||||
|
{
|
||||||
|
$movTo = $ai->entity->level->entityList[$this->targetMateID] ?? false;
|
||||||
|
if(!($movTo instanceof Entity) || $movTo->dead || !$movTo->isInLove()){
|
||||||
|
$this->reset();
|
||||||
|
$this->onEnd($ai);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$ai->mobController->moveTo($movTo->x, $movTo->y, $movTo->z, false);
|
||||||
|
$ai->mobController->lookOn($movTo);
|
||||||
|
|
||||||
|
--$this->selfCounter;
|
||||||
|
if($this->selfCounter <= 0 && $ai->entity->distanceSquared($movTo) < 9){
|
||||||
|
$this->createBaby($ai, $movTo);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public function createBaby(EntityAI $ai, Entity $entpar2){
|
||||||
|
/**
|
||||||
|
* @var Animal $baby
|
||||||
|
*/
|
||||||
|
$baby = $ai->entity->breed();
|
||||||
|
//this.theAnimal.setGrowingAge(6000); TODO growing age?
|
||||||
|
//this.targetMate.setGrowingAge(6000);
|
||||||
|
|
||||||
|
$ai->entity->resetInLove();
|
||||||
|
$entpar2->resetInLove();
|
||||||
|
$baby->setAge(-24000);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function findMate(EntityAI $ai){
|
||||||
|
$distance = 8.0*8.0;
|
||||||
|
$minDist = $distance;; //TODO check bb intersection?
|
||||||
|
$matedEID = null;
|
||||||
|
|
||||||
|
foreach($ai->entity->level->entitiesInLove as $eid => $_){
|
||||||
|
$e = $ai->entity->level->entityList[$eid] ?? false;
|
||||||
|
if($e instanceof Entity){
|
||||||
|
$d = $ai->entity->distanceSquared($e);
|
||||||
|
if($ai->entity->canMate($e) && $d < $minDist){
|
||||||
|
$minDist = $d;
|
||||||
|
$matedEID = $e->eid;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $matedEID;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function canBeExecuted(EntityAI $ai)
|
||||||
|
{
|
||||||
|
return $ai->entity->isInLove() && ($this->targetMateID = $this->findMate($ai)) != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -33,8 +33,8 @@ class TaskRandomWalk extends TaskBase
|
||||||
{
|
{
|
||||||
if(($ai->entity instanceof Creeper && $ai->entity->isIgnited()) || $ai->entity->hasPath() || $ai->isStarted("TaskTempt") ||$ai->isStarted("TaskAttackPlayer")) {
|
if(($ai->entity instanceof Creeper && $ai->entity->isIgnited()) || $ai->entity->hasPath() || $ai->isStarted("TaskTempt") ||$ai->isStarted("TaskAttackPlayer")) {
|
||||||
return false;
|
return false;
|
||||||
}
|
} // i really need mutexBits
|
||||||
return !$ai->entity->inPanic && !$ai->isStarted("TaskEatTileGoal")&& !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskLookAtPlayer") && mt_rand(0, 120) == 0;
|
return !$ai->entity->inPanic && !$ai->isStarted("TaskMate") && !$ai->isStarted("TaskEatTileGoal") && !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskLookAtPlayer") && mt_rand(0, 120) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,7 @@ class TaskTempt extends TaskBase
|
||||||
|
|
||||||
public function canBeExecuted(EntityAI $ai)
|
public function canBeExecuted(EntityAI $ai)
|
||||||
{
|
{
|
||||||
if(!($ai->entity instanceof Breedable) || $ai->entity->inPanic){ //TODO Work with path
|
if(!($ai->entity instanceof Breedable) || $ai->entity->inPanic || $ai->isStarted("TaskMate")){ //TODO Work with path
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$target = $this->findTarget($ai->entity, 10);
|
$target = $this->findTarget($ai->entity, 10);
|
||||||
|
|
|
||||||
|
|
@ -11,6 +11,8 @@ class Level{
|
||||||
*/
|
*/
|
||||||
public $entityList;
|
public $entityList;
|
||||||
|
|
||||||
|
public $entitiesInLove = [];
|
||||||
|
|
||||||
public $tiles, $blockUpdates, $nextSave, $players = [], $level, $mobSpawner, $totalMobsAmount = 0;
|
public $tiles, $blockUpdates, $nextSave, $players = [], $level, $mobSpawner, $totalMobsAmount = 0;
|
||||||
private $time, $startCheck, $startTime, $server, $name, $usedChunks, $changedBlocks, $changedCount, $stopTime;
|
private $time, $startCheck, $startTime, $server, $name, $usedChunks, $changedBlocks, $changedCount, $stopTime;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue