attackplayer task fix

This commit is contained in:
GameHerobrine 2024-01-31 07:14:13 +03:00
parent 0ca6becc6a
commit d0a262a3da
9 changed files with 100 additions and 262 deletions

View file

@ -96,7 +96,6 @@ require_once(FILE_PATH . "/src/astarnavigator/TileNavigator.php");
require_once(FILE_PATH . "/src/entity/ai/tasks/TaskBase.php");
require_once(FILE_PATH. "/src/entity/ai/tasks/TaskTempt.php");
require_once(FILE_PATH . "/src/entity/Rideable.php");
require_once(FILE_PATH . "/src/entity/Breedable.php");

View file

@ -104,7 +104,6 @@ class MobController
$v10 = (atan2($diffZ, $diffX) * 180 / M_PI) - 90;
$this->entity->yaw = self::limitAngle($this->entity->yaw, $v10, 30);
$this->entity->setAIMoveSpeed($this->speed * $this->entity->getSpeedModifer());
if($diffY > 0 && $diffX*$diffX + $diffZ*$diffZ < 1) $this->setJumping(true);
}
}

View file

@ -1,42 +1,111 @@
<?php
/**
* A Tempt but for Monsters
*/
class TaskAttackPlayer extends TaskTempt
class TaskAttackPlayer extends TaskBase
{
public $attackTime = 0;
public $moveSpeed;
public $rangeSquared;
public $attackCounter;
public function __construct($speed, $range){
$this->moveSpeed = $speed;
$this->rangeSquared = $range*$range;
}
public function onStart(EntityAI $ai)
{
$this->selfCounter = 1;
console("found!");
}
public function onUpdate(EntityAI $ai)
{
if(!($this->target instanceof Entity) || ($ai->entity instanceof Spider && $ai->entity->level->isDay()) || ($this->target instanceof Entity && !$this->target->isPlayer()) || (Utils::distance_noroot($this->target, $ai->entity) > 256) || $this->target->level->getName() != $ai->entity->level->getName()){
if(!$this->isTargetValid($ai)){
$this->reset();
return;
return false;
}
$ai->mobController->setMovingTarget($ai->entity->target->x, $ai->entity->target->y, $ai->entity->target->z, $this->moveSpeed);
$ai->mobController->setLookPosition($ai->entity->target->x, $ai->entity->target->y + 0.12, $ai->entity->target->z, 10, $ai->entity->getVerticalFaceSpeed());
//TODO fix $ai->mobController->moveTo($this->target->x - 0.5, floor($ai->entity->y), $this->target->z - 0.5, false);
$ai->mobController->lookOn($this->target);
$mult = $ai->entity instanceof Spider ? 1 : 2;
$radius = ($ai->entity->width * $mult)*($ai->entity->width * $mult);
if(Utils::distance_noroot($this->target, $ai->entity) <= $radius && $this->attackTime <= 0){
$ai->entity->attackEntity($this->target);
$this->attackTime = 20;
--$this->attackCounter;
$v1 = $ai->entity->width * $ai->entity->width * 4;
$e = $ai->entity;
$t = $e->target;
$xDiff = ($t->x - $e->x);
$yDiff = ($t->y - $e->y);
$zDiff = ($t->z - $e->z);
$dist = ($xDiff*$xDiff + $yDiff*$yDiff + $zDiff*$zDiff);
if($dist <= $v1){
if($this->attackCounter <= 0){
$this->attackCounter = 20;
$e->attackEntity($t);
}
}
--$this->attackTime;
}
public function canBeExecuted(EntityAI $ai)
{
$target = $this->findTarget($ai->entity, 16);
if(($ai->entity instanceof Spider && !$ai->entity->level->isDay()) || $target instanceof Entity && $target->class === ENTITY_PLAYER && $target->isPlayer()){
$this->target = $target; //TODO get rid of it
$ai->entity->target = $target;
return true;
public function isTargetValid(EntityAI $ai){
$e = $ai->entity; //TODO sometimes it keeps being valid even after getting invalid (/gm 1 -> /gm 0 switch)
if($e->target instanceof Entity){
$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) <= $this->rangeSquared;
}
return false;
}
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) <= $this->rangeSquared){
return true;
}
}
$bestTargetDistance = INF;
$closestTarget = null;
foreach($e->level->players as $p){
if($p->spawned){
$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 <= $this->rangeSquared){
if($bestTargetDistance >= $d){
$closestTarget = $pt;
$bestTargetDistance = $d;
}
}
}
}
if($closestTarget != null){
$e->target = $closestTarget; //TODO dont save entity object ?
return true;
}
return false;
}
public function canBeExecuted(EntityAI $ai)
{
return $this->tryTargeting($ai);
}
public function onEnd(EntityAI $ai)
{
}
}

View file

@ -1,89 +0,0 @@
<?php
class TaskDestroyServerPerformance extends TaskTempt
{
public $attackTime = 0;
public $server;
public function __construct(){
parent::__construct();
$this->server = ServerAPI::request();
}
public function onStart(EntityAI $ai)
{
$this->selfCounter = 1;
}
public function onUpdate(EntityAI $ai)
{
if(!($this->target instanceof Entity) || ($this->target instanceof Entity && !$this->target->isPlayer()) || (Utils::distance_noroot($this->target, $ai->entity) > 256) || $this->target->level->getName() != $ai->entity->level->getName()){
$this->reset();
return;
}
if(Utils::distance_noroot($this->target, $ai->entity) > 100){
//TODO fix $ai->mobController->moveTo($this->target->x, floor($ai->entity->y), $this->target->z, false);
}else{
}
$ai->mobController->lookOn($this->target);
if(--$this->attackTime <= 0){
$this->rangedAttack($ai->entity, $this->target);
$this->attackTime = 60;
}
}
/**
*
* @param Entity $selfEntity
* @param Entity $target
*/
public function rangedAttack($selfEntity, $target){
$d = [
"x" => $selfEntity->x,
"y" => $selfEntity->y + 1.6,
"z" => $selfEntity->z,
"yaw" => $selfEntity->yaw,
"pitch" => $selfEntity->pitch
];
/**
* @var Arrow $arrow
*/
$arrow = $this->server->api->entity->add($selfEntity->level, ENTITY_OBJECT, OBJECT_ARROW, $d);
$arrow->shotByEntity = true;
$arrow->shooterEID = $selfEntity->eid;
$posY = ($target->y + $target->getEyeHeight() - 0.1);
$diffX = $target->x - $selfEntity->x;
$diffY = ($target->boundingBox->minY + ($target->height / 3)) - $posY;
$diffZ = $target->z - $selfEntity->z;
$v12 = sqrt($diffX * $diffX + $diffZ * $diffZ);
if($v12 >= 1.0e-7){
$yaw = ((atan2($diffZ, $diffX) * 180) / M_PI) - 90;
$pitch = -((atan2($diffY, $v12) * 180) / M_PI);
$v16 = $diffX / $v12;
$v18 = $diffZ / $v12;
$arrow->x = $selfEntity->x + $v16;
$arrow->y = $posY;
$arrow->z = $selfEntity->z + $v18;
$arrow->yaw = $yaw;
$arrow->pitch = $pitch;
$arrow->updateAABB();
$v20 = $v12 * 0.2;
$arrow->shoot($diffX, $diffY + $v20, $diffZ, 1.6, 12);
$this->server->api->entity->spawnToAll($arrow);
}
}
public function canBeExecuted(EntityAI $ai)
{
$target = $this->findTarget($ai->entity, 16);
if(($ai->entity instanceof Spider && !$ai->entity->level->isDay()) || $target instanceof Entity && $target->class === ENTITY_PLAYER && $target->isPlayer()){
$this->target = $target; //TODO get rid of it
$ai->entity->target = $target;
return true;
}
return false;
}
}

View file

@ -1,72 +0,0 @@
<?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;
}
//TODO fix $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

@ -14,7 +14,7 @@ class TaskSwimming extends TaskBase
public function onUpdate(EntityAI $ai)
{
if(!$ai->entity->inWater || !$ai->entity->inLava){
if(!$ai->entity->inWater && !$ai->entity->inLava){
$this->reset();
return;
}

View file

@ -1,74 +0,0 @@
<?php
class TaskTempt extends TaskBase
{
public $target = false;
public function onStart(EntityAI $ai)
{
$this->selfCounter = 1;
}
public function onEnd(EntityAI $ai)
{
unset($this->target);
unset($ai->entity->target);
$ai->entity->pitch = 0;
}
public function onUpdate(EntityAI $ai)
{
if(!($this->target instanceof Entity) || ($this->target instanceof Entity && !$this->target->isPlayer()) || (Utils::distance_noroot($this->target, $ai->entity) > 100) || !$ai->entity->isFood($this->target->player->getHeldItem()->getID()) || $this->target->level->getName() != $ai->entity->level->getName()){
$this->reset();
return;
}
//TODO fix $ai->mobController->moveTo($this->target->x - 0.5, floor($ai->entity->y), $this->target->z - 0.5, false);
$ai->mobController->lookOn($this->target);
}
public function canBeExecuted(EntityAI $ai)
{
if(!($ai->entity instanceof Breedable) || $ai->entity->inPanic || $ai->isStarted("TaskMate")){
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;
$ai->entity->target = $target;
return true;
}
return false;
}
protected function findTarget($e, $r){
$svd = null;
$svdDist = -1;
foreach($e->level->players as $p){
$p = $p->entity;
if(Utils::distance_noroot($e, $p) > $r*$r){
continue;
}
if($svdDist === -1){
$svdDist = Utils::distance_noroot($e, $p);
$svd = $p;
continue;
}
if($svd != null && $svdDist === 0){
$svd = $p;
}
if(($cd = Utils::distance_noroot($e, $p)) < $svdDist){
$svdDist = $cd;
$svd = $p;
}
}
if($svd == null){
return null;
}
return $svd;
}
}

View file

@ -11,6 +11,7 @@ class Zombie extends Monster{
$this->ai->addTask(new TaskRandomWalk(0.25));
$this->ai->addTask(new TaskLookAround());
$this->ai->addTask(new TaskSwimming());
$this->ai->addTask(new TaskAttackPlayer(0.25, 16));
}
public function getArmorValue(){

View file

@ -14,7 +14,12 @@ class Level{
public $entityListPositioned = [];
public $entitiesInLove = [];
public $tiles, $blockUpdates, $nextSave, $players = [], $level, $mobSpawner, $totalMobsAmount = 0;
/**
* @var Player[]
*/
public $players = [];
public $tiles, $blockUpdates, $nextSave, $level, $mobSpawner, $totalMobsAmount = 0;
private $time, $startCheck, $startTime, $server, $name, $usedChunks, $changedBlocks, $changedCount, $stopTime;
public $randInt1, $randInt2;