mirror of
https://github.com/oldminecraftcommunity/NostalgiaCore.git
synced 2026-11-20 11:39:43 +00:00
added breeding(experimental)
This commit is contained in:
parent
b7ecbb7e8d
commit
eff034e897
8 changed files with 278 additions and 1 deletions
|
|
@ -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;
|
||||
|
|
|
|||
77
src/entity/ai/tasks/TaskMate.php
Normal file
77
src/entity/ai/tasks/TaskMate.php
Normal 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;
|
||||
}
|
||||
}
|
||||
|
||||
90
src/entity/ai/tasks/TaskTempt.php
Normal file
90
src/entity/ai/tasks/TaskTempt.php
Normal 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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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){
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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(){
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue