animal breeding added

This commit is contained in:
GameHerobrine 2023-10-31 03:15:52 +03:00
parent 426728342f
commit ffd5fa738c
9 changed files with 135 additions and 36 deletions

View file

@ -17,6 +17,7 @@ abstract class Animal extends Creature implements Ageable, Breedable{
$this->ai->addTask(new TaskTempt());
$this->ai->addTask(new TaskPanic());
$this->ai->addTask(new TaskMate());
}
public function harm($dmg, $cause = "generic", $force = false){
@ -31,11 +32,21 @@ abstract class Animal extends Creature implements Ageable, Breedable{
}
public function breed(){
$c = $this->spawnChild();
if($this->server->dhandle("entity.animal.breed", ["parent" => $this, "child" => $c]) !== false){
$c->parent = $this;
if($this->server->dhandle("entity.animal.breed", ["parent" => $this]) !== false){
$c = $this->spawnChild();
$c->parent = $this; //TODO save eid to avoid memory leaks?
$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(){
@ -47,6 +58,11 @@ abstract class Animal extends Creature implements Ageable, Breedable{
}else{
$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(){
@ -88,8 +104,10 @@ abstract class Animal extends Creature implements Ageable, Breedable{
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);
if($this->isFood($slot->getID()) && $this->inLove <= 0 && !$this->isBaby()){ //TODO check vanilla
if(($e->player->gamemode & 0x01) === SURVIVAL){
$e->player->removeItem($slot->getID(), $slot->getMetadata(), 1);
}
$this->inLove = 600; //600 ticks, original mehod from mcpe
return true;
}
@ -103,7 +121,7 @@ abstract class Animal extends Creature implements Ageable, Breedable{
--$this->inLove;
}
}
public function getDrops(){
if($this->isBaby()){
return [];

View file

@ -687,27 +687,7 @@ class Entity extends Position
}
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;
}
$this->counterUpdate();
if($this->lastHeadYaw != $this->headYaw){
$this->sendHeadYaw();
@ -1025,7 +1005,30 @@ class Entity extends Position
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()
{
if($this->closed === false){

View file

@ -28,12 +28,16 @@ abstract class Living extends Entity implements Damageable, Pathfindable{
return $this->path != null;
}
public function eatGrass(){}
public function __destruct()
{
parent::__destruct();
public function close(){
parent::close();
unset($this->pathFollower->entity);
unset($this->ai->entity);
unset($this->ai->mobController->entity);
unset($this->ai);
unset($this->parent);
}
public function canBeShot(){
return true;
}

View file

@ -16,7 +16,7 @@ class TaskLookAround extends TaskBase
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)

View file

@ -4,7 +4,7 @@ class TaskLookAtPlayer extends TaskBase{
public $target = false;
private $state, $yaw, $pitch;
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){

View 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;
}
}

View file

@ -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")) {
return false;
}
return !$ai->entity->inPanic && !$ai->isStarted("TaskEatTileGoal")&& !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskLookAtPlayer") && mt_rand(0, 120) == 0;
} // i really need mutexBits
return !$ai->entity->inPanic && !$ai->isStarted("TaskMate") && !$ai->isStarted("TaskEatTileGoal") && !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskLookAtPlayer") && mt_rand(0, 120) == 0;
}

View file

@ -29,7 +29,7 @@ class TaskTempt extends TaskBase
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;
}
$target = $this->findTarget($ai->entity, 10);

View file

@ -11,6 +11,8 @@ class Level{
*/
public $entityList;
public $entitiesInLove = [];
public $tiles, $blockUpdates, $nextSave, $players = [], $level, $mobSpawner, $totalMobsAmount = 0;
private $time, $startCheck, $startTime, $server, $name, $usedChunks, $changedBlocks, $changedCount, $stopTime;