added breeding(experimental)

This commit is contained in:
GameHerobrine 2024-03-09 10:29:36 +03:00
parent b7ecbb7e8d
commit eff034e897
8 changed files with 278 additions and 1 deletions

View file

@ -4,6 +4,10 @@ abstract class Living extends Entity implements Pathfindable{
public static $despawnMobs, $despawnTimer, $entityPushing = false;
public $moveStrafing, $moveForward;
/**
* @var Entity $target
* @var EntityAI $ai
*/
public $target, $ai;
public $pathFinder, $path = null, $currentIndex = 0, $currentNode, $pathFollower;
public $ticksExisted = 0;

View file

@ -0,0 +1,77 @@
<?php
class TaskMate extends TaskBase
{
public function __construct($speed){
$this->speedMultiplier = $speed;
}
public $targetMateID;
public function onStart(EntityAI $ai)
{
$this->selfCounter = 60;
}
public function onEnd(EntityAI $ai)
{
$this->selfCounter = 0;
$this->targetMateID = null;
}
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->setLookPosition($movTo->x, $movTo->y + $movTo->getEyeHeight(), $movTo->z, 10, $ai->entity->getVerticalFaceSpeed());
$ai->mobController->setMovingTarget($movTo->x, $movTo->y, $movTo->z, $this->speedMultiplier);
--$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

@ -0,0 +1,90 @@
<?php
class TaskTempt extends TaskBase
{
public function __construct($speed){
$this->speedMultiplier = $speed;
}
public function onStart(EntityAI $ai)
{
$this->selfCounter = 1;
}
public function onEnd(EntityAI $ai)
{
}
public function onUpdate(EntityAI $ai)
{
if(!$this->isTargetValid($ai)){
$this->reset();
return false;
}
$target = $ai->entity->target;
$xdiff = ($target->x - $ai->entity->x);
$ydiff = ($target->y - $ai->entity->y);
$zdiff = ($target->z - $ai->entity->z);
$dist = $xdiff*$xdiff + $ydiff*$ydiff + $zdiff*$zdiff;
$ai->mobController->setLookPosition($target->x, $target->y + $target->getEyeHeight(), $target->z, 30, $ai->entity->getVerticalFaceSpeed());
if($dist >= 6.25){ //TODO update yaw even if dist is less
$ai->mobController->setMovingTarget($target->x, $target->y, $target->z, $this->speedMultiplier);
}
}
public function isTargetValid(EntityAI $ai){
$e = $ai->entity;
if($e->target instanceof Entity && !$e->target->closed && $e->isFood($e->target->player->getHeldItem()->id)){
$t = $e->target;
$xDiff = ($t->x - $e->x);
$yDiff = ($t->y - $e->y);
$zDiff = ($t->z - $e->z);
return ($xDiff*$xDiff + $yDiff*$yDiff + $zDiff*$zDiff) <= 36;
}
return false;
}
public function canBeExecuted(EntityAI $ai)
{
return !$ai->entity->inPanic && !$ai->isStarted("TaskMate") && $this->tryTargeting($ai);
}
public function tryTargeting(EntityAI $ai){
$e = $ai->entity;
if($e->target instanceof Entity){
$t = $e->target;
$xDiff = ($t->x - $e->x);
$yDiff = ($t->y - $e->y);
$zDiff = ($t->z - $e->z);
if(($xDiff*$xDiff + $yDiff*$yDiff + $zDiff*$zDiff) <= 36){
return true;
}
}
$bestTargetDistance = INF;
$closestTarget = null;
foreach($e->level->players as $p){
if($p->spawned && $e->isFood($p->getHeldItem()->id)){
$pt = $p->entity;
$xDiff = $pt->x - $e->x;
$yDiff = $pt->y - $e->y;
$zDiff = $pt->z - $e->z;
$d = ($xDiff*$xDiff + $yDiff*$yDiff + $zDiff*$zDiff);
if($d <= 36){
if($bestTargetDistance >= $d){
$closestTarget = $pt;
$bestTargetDistance = $d;
}
}
}
}
if($closestTarget != null){
$e->target = $closestTarget; //TODO dont save entity object ?
return true;
}
return false;
}
}

View file

@ -16,6 +16,9 @@ class Chicken extends Animal{
$this->ai->addTask(new TaskPanic(1.5));
$this->ai->addTask(new TaskLookAround());
$this->ai->addTask(new TaskSwimming());
$this->ai->addTask(new TaskTempt(1.0));
$this->ai->addTask(new TaskMate(1.0));
}
public function isFood($id){
return $id === PUMPKIN_SEEDS || $id === MELON_SEEDS || $id === BEETROOT_SEEDS || $id === WHEAT_SEEDS;

View file

@ -13,6 +13,8 @@ class Cow extends Animal{
$this->ai->addTask(new TaskPanic(1.5));
$this->ai->addTask(new TaskLookAround());
$this->ai->addTask(new TaskSwimming());
$this->ai->addTask(new TaskTempt(1.0));
$this->ai->addTask(new TaskMate(1.0));
}
public function isFood($id){

View file

@ -15,6 +15,10 @@ class Pig extends Animal implements Rideable{
$this->ai->addTask(new TaskPanic(1.5));
$this->ai->addTask(new TaskLookAround());
$this->ai->addTask(new TaskSwimming());
$this->ai->addTask(new TaskTempt(1.0));
$this->ai->addTask(new TaskMate(1.0));
}
/**
* @return boolean

View file

@ -19,6 +19,8 @@ class Sheep extends Animal{
$this->ai->addTask(new TaskEatTileGoal());
$this->ai->addTask(new TaskLookAround());
$this->ai->addTask(new TaskSwimming());
$this->ai->addTask(new TaskTempt(1.0));
$this->ai->addTask(new TaskMate(1.0));
}
public function createSaveData(){