new ai task, fixes of old ones, php8 fixes

This commit is contained in:
GameHerobrine 2022-12-24 22:42:23 +03:00
parent a907a7cf5e
commit 80bdddb817
13 changed files with 1581 additions and 1458 deletions

View file

@ -14,6 +14,7 @@ abstract class Animal extends Creature implements Ageable, Breedable{
if(isset($this->data["IsBaby"]) && $this->data["IsBaby"] && $this->getAge() >= 0){
$this->setAge(-24000);
}
$this->ai->addTask(new TaskTempt());
}
public function isBaby(){

File diff suppressed because it is too large Load diff

View file

@ -16,7 +16,6 @@ abstract class Living extends Entity implements Damageable{
}
public function update(){
if($this->idleTime <= 0) {
$this->ai->updateTasks();
}

View file

@ -38,9 +38,13 @@ class EntityAI
* @return TaskBase | false
*/
public function getTask($id){
return $this->tasks[$id] ?: false; //i never saw this operator before
return isset($this->tasks[$id]) ? $this->tasks[$id] : false;
}
public function isStarted($id){
$task = @$this->getTask($id);
return $task instanceof TaskBase && $task->isStarted;
}
public function updateTasks(){
foreach($this->tasks as $t){

View file

@ -6,7 +6,7 @@ class TaskLookAround extends TaskBase
public function onStart(EntityAI $ai)
{
$this->selfCounter = 1;
$this->rotation = mt_rand(-90, 90);
$this->rotation = mt_rand(-120, 120);
}
public function onEnd(EntityAI $ai)
@ -16,7 +16,7 @@ class TaskLookAround extends TaskBase
public function canBeExecuted(EntityAI $ai)
{
return !$ai->entity->isMoving() && Utils::randomFloat() < 0.04;
return !$ai->entity->isMoving() && Utils::randomFloat() < 0.02 && !$ai->isStarted("TaskLookAtPlayer") && !$ai->isStarted("TaskTempt"); /*Vanilla value*/
}
public function onUpdate(EntityAI $ai)

View file

@ -6,16 +6,32 @@ class TaskLookAtPlayer extends TaskBase{
public $target = false;
private $state, $yaw, $pitch;
public function canBeExecuted(EntityAI $ai){
return !$ai->entity->isMoving() && !@$ai->getTask("TaskLookAround")->isStarted && mt_rand(0, 20) == 0;
return Utils::randomFloat() < 0.02 && !$ai->entity->isMoving() && !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskTempt");
}
protected function findTarget($e, $r){
$ents = $e->server->api->entity->getRadius($e, $r, ENTITY_PLAYER); //TODO sort by nearest and select nearest
return count($ents) <= 0 ? false : $ents[array_rand($ents)];
$svd = null;
$svdDist = -1;
foreach($e->server->api->entity->getRadius($e, $r, ENTITY_PLAYER) as $p){
if($svdDist === -1){
$svdDist = Utils::manh_distance($e, $p);
$svd = $p;
continue;
}
if($svd != null && $svdDist === 0){
$svd = $p;
}
if(($cd = Utils::manh_distance($e, $p)) < $svdDist){
$svdDist = $cd;
$svd = $p;
}
}
return $svd;
}
public function onStart(EntityAI $ai){
$this->target = $this->findTarget($ai->entity, 5);
$this->target = $this->findTarget($ai->entity, 6); //TODO max distance for different mobs
if(!($this->target instanceof Entity) || !$this->target->isPlayer()){
$this->reset();
return;
@ -26,7 +42,7 @@ class TaskLookAtPlayer extends TaskBase{
}
public function onUpdate(EntityAI $ai){
if(!($this->target instanceof Entity)){
if(!($this->target instanceof Entity) || Utils::distance($ai->entity, $this->target) > 6){ //TODO max distance for different mobs
$this->reset();
return;
}

View file

@ -17,7 +17,6 @@ class TaskRandomWalk extends TaskBase
public function onEnd(EntityAI $ai)
{
$this->x = $this->z = 0;
$ai->entity->idleTime = mt_rand(20, 120);
}
public function onUpdate(EntityAI $ai)
@ -34,7 +33,7 @@ class TaskRandomWalk extends TaskBase
if($ai->entity instanceof Creeper && $ai->entity->isIgnited()) {
return false;
}
return !@$ai->getTask("TaskLookAround")->isStarted && !@$ai->getTask("TaskLookAtPlayer")->isStarted && mt_rand(0, 40) == 0;
return !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskLookAtPlayer") && mt_rand(0, 120) == 0;
}

View file

@ -0,0 +1,72 @@
<?php
class TaskTempt extends TaskBase
{
public $target = false;
public function onStart(EntityAI $ai)
{
$this->selfCounter = 1;
}
public function onEnd(EntityAI $ai)
{
unset($this->target);
$ai->entity->pitch = 0;
}
public function onUpdate(EntityAI $ai)
{
if($this->target instanceof Entity && $this->target->distanceSquared($ai->entity) > 100 || !$ai->entity->isFood($this->target->player->getHeldItem()->getID())){
$this->reset();
return;
}
//TODO following
$ai->mobController->lookOn($this->target);
$pk = new RotateHeadPacket(); //TODO headYaw auto update
$pk->eid = $ai->entity->eid;
$pk->yaw = $ai->entity->yaw;
$ai->entity->server->api->player->broadcastPacket($ai->entity->level->players, $pk);
}
public function canBeExecuted(EntityAI $ai)
{
if(!($ai->entity instanceof Breedable)){
return false;
}
$target = $this->findTarget($ai->entity, 10);
if($target instanceof Entity && $target->class === ENTITY_PLAYER && $target->isPlayer() && $ai->entity->isFood($target->player->getHeldItem()->getID())){
$this->target = $target;
return true;
}
return false;
}
protected function findTarget($e, $r){
$svd = null;
$svdDist = -1;
foreach($e->server->api->entity->getRadius($e, $r * 3, ENTITY_PLAYER) as $p){
if($svdDist === -1){
$svdDist = Utils::manh_distance($e, $p);
$svd = $p;
continue;
}
if($svd != null && $svdDist === 0){
$svd = $p;
}
if(($cd = Utils::manh_distance($e, $p)) < $svdDist){
$svdDist = $cd;
$svd = $p;
}
}
if($svd == null || Utils::distance_noroot($e, $svd) > ($r * $r)){
return null;
}
return $svd;
}
}