From eff034e897e55711785f101c660cbc7a1b2a4dab Mon Sep 17 00:00:00 2001 From: GameHerobrine Date: Sat, 9 Mar 2024 10:29:36 +0300 Subject: [PATCH] added breeding(experimental) --- src/entity/Living.php | 4 ++ src/entity/ai/tasks/TaskMate.php | 77 ++++++++++++++++++++++++ src/entity/ai/tasks/TaskTempt.php | 90 ++++++++++++++++++++++++++++ src/entity/animal/Chicken.php | 3 + src/entity/animal/Cow.php | 2 + src/entity/animal/Pig.php | 4 ++ src/entity/animal/Sheep.php | 2 + src/world/Level.php | 97 ++++++++++++++++++++++++++++++- 8 files changed, 278 insertions(+), 1 deletion(-) create mode 100644 src/entity/ai/tasks/TaskMate.php create mode 100644 src/entity/ai/tasks/TaskTempt.php diff --git a/src/entity/Living.php b/src/entity/Living.php index f9ab776a..7f390c8b 100644 --- a/src/entity/Living.php +++ b/src/entity/Living.php @@ -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; diff --git a/src/entity/ai/tasks/TaskMate.php b/src/entity/ai/tasks/TaskMate.php new file mode 100644 index 00000000..28ebe40b --- /dev/null +++ b/src/entity/ai/tasks/TaskMate.php @@ -0,0 +1,77 @@ +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; + } +} + diff --git a/src/entity/ai/tasks/TaskTempt.php b/src/entity/ai/tasks/TaskTempt.php new file mode 100644 index 00000000..6d7d2b53 --- /dev/null +++ b/src/entity/ai/tasks/TaskTempt.php @@ -0,0 +1,90 @@ +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; + } +} diff --git a/src/entity/animal/Chicken.php b/src/entity/animal/Chicken.php index 278d5b1b..5cf752b6 100644 --- a/src/entity/animal/Chicken.php +++ b/src/entity/animal/Chicken.php @@ -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; diff --git a/src/entity/animal/Cow.php b/src/entity/animal/Cow.php index a3a9fb1c..564bf77f 100644 --- a/src/entity/animal/Cow.php +++ b/src/entity/animal/Cow.php @@ -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){ diff --git a/src/entity/animal/Pig.php b/src/entity/animal/Pig.php index 3f6ba532..5cee7186 100644 --- a/src/entity/animal/Pig.php +++ b/src/entity/animal/Pig.php @@ -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 diff --git a/src/entity/animal/Sheep.php b/src/entity/animal/Sheep.php index 0a9d2812..94bff624 100644 --- a/src/entity/animal/Sheep.php +++ b/src/entity/animal/Sheep.php @@ -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(){ diff --git a/src/world/Level.php b/src/world/Level.php index 58f43b5a..96a54cd4 100644 --- a/src/world/Level.php +++ b/src/world/Level.php @@ -82,7 +82,102 @@ class Level{ return true; } - + /* TODO neccessary for projectiles + public function rayTraceBlocks(Vector3 $start, Vector3 $end){ + + $par3 = false; //TODO move to params? + $par4 = true; + + $xStart = floor($start->x); + $yStart = floor($start->y); + $zStart = floor($start->z); + $xEnd = floor($end->x); + $yEnd = floor($end->y); + $zEnd = floor($end->z); + + [$startID, $startMeta] = $this->level->getBlock($xStart, $yStart, $zStart); + $block = StaticBlock::getBlock($startID); + //$block::updateShape($this, $xStart, $yStart, $zStart); //TODO better way to do it + $aabb = $block::getAABB($this, $xStart, $yStart, $zStart); + if($startID > 0 && $aabb != null){ //TODO also block::canColideCheck + $v14 = $block::clip($this, $xStart, $yStart, $zStart, $start, $end); + if($v14 != null) return $v14; + } + + for($i = 0; $i <= 200; ++$i){ + + if($xStart == $xEnd && $yStart == $yEnd && $zStart == $zEnd){ //also add checks for nan? + console("$start $end"); + return null; + } + + $v39 = $v40 = $v41 = true; + $v15 = $v17 = $v19 = 999.0; //nice mojang + + if($xEnd > $xStart) $v15 = $xStart + 1; + else if($xEnd < $xStart) $v15 = $xStart; + else $v39 = false; + + if($yEnd > $yStart) $v17 = $yStart + 1; + else if($yEnd < $yStart) $v17 = $yStart; + else $v40 = false; + + if($zEnd > $zStart) $v19 = $zStart + 1; + else if($zEnd < $zStart) $v19 = $zStart; + else $v41 = false; + + $v21 = $v23 = $v25 = 999.0; //nice mojang x2 + $v27 = $end->x - $start->x; + $v29 = $end->y - $start->y; + $v31 = $end->z - $start->z; + + if($v39) $v21 = ($v15 - $start->x) / $v27; + if($v40) $v23 = ($v17 - $start->y) / $v29; + if($v41) $v25 = ($v19 - $start->z) / $v31; + + $v33 = false; + if($v21 < $v23 && $v21 < $v25){ + $v42 = $xEnd > $xStart ? 4 : 5; + + $start->x = $v15; + $start->y += $v29 * $v21; + $start->z += $v31 * $v21; + }else if($v23 < $v25){ + $v42 = $yEnd > $yStart ? 0 : 1; + + $start->x += $v27 * $v23; + $start->y = $v17; + $start->z += $v31 * $v23; + }else{ + $v42 = $zEnd > $zStart ? 2 : 3; + + $start->x += $v27 * $v23; + $start->y += $v29 * $v21; + $start->z = $v19; + } + + $xStart = floor($start->x); + if($v42 == 5) --$xStart; + + $yStart = floor($start->y); + if($v42 == 1) --$yStart; + + $zStart = floor($start->z); + if($v42 == 3) --$zStart; + + [$blockID, $blockMeta] = $this->level->getBlock($xStart, $yStart, $zStart); + $block = StaticBlock::getBlock($blockID); + + $aabb = $block::getAABB($this, $xStart, $yStart, $zStart); + if($startID > 0 && $aabb != null){ //TODO also block::canColideCheck + $v38 = $block::clip($this, $xStart, $yStart, $zStart, $start, $end); + if($v38 != null) return $v38; + } + } + + return null; + } + */ //TODO mayPlace public function isLavaInBB($aabb){ $minX = floor($aabb->minX);