mirror of
https://github.com/oldminecraftcommunity/NostalgiaCore.git
synced 2026-11-21 12:09:43 +00:00
Player::teleport will set motion to 0, fixed possible divby0 when skeletonm shooting the player, fixed entities not untargetting dead players, fixed wrong distance calculations
This commit is contained in:
parent
2cd42fb605
commit
81aa575749
7 changed files with 84 additions and 30 deletions
|
|
@ -298,7 +298,14 @@ class Player{
|
|||
$this->forceMovement = $pos;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$pk = new SetEntityMotionPacket();
|
||||
$pk->eid = 0;
|
||||
$pk->speedX = 0;
|
||||
$pk->speedY = 0;
|
||||
$pk->speedZ = 0;
|
||||
$this->dataPacket($pk);
|
||||
|
||||
$pk = new MovePlayerPacket;
|
||||
$pk->eid = 0;
|
||||
$pk->x = $pos->x;
|
||||
|
|
|
|||
|
|
@ -21,21 +21,29 @@ abstract class Animal extends Creature implements Ageable, Breedable{
|
|||
$this->searchForClosestPlayers = true;
|
||||
}
|
||||
|
||||
public function isPlayerValid(Player $player){
|
||||
return $player->spawned && $this->isFood($player->getHeldItem()->id) && !$player->entity->dead;
|
||||
}
|
||||
|
||||
public function handlePrePlayerSearcher(){
|
||||
parent::handlePrePlayerSearcher();
|
||||
|
||||
if($this->closestPlayerThatCanFeedEID !== false && !isset($this->level->entityList[$this->closestPlayerThatCanFeedEID])){
|
||||
$this->closestPlayerThatCanFeedEID = false;
|
||||
$this->closestPlayerThatCanFeedDist = INF;
|
||||
if($this->closestPlayerThatCanFeedEID !== false){
|
||||
$player = $this->level->entityList[$this->closestPlayerThatCanFeedEID] ?? false;
|
||||
if($player === false || !$this->isPlayerValid($player->player)){
|
||||
$this->closestPlayerThatCanFeedEID = false;
|
||||
$this->closestPlayerThatCanFeedDist = INF;
|
||||
}else{
|
||||
$dist = ($this->x - $player->x)*($this->x - $player->x) + ($this->y - $player->y)*($this->y - $player->y) + ($this->z - $player->z)*($this->z - $player->z);
|
||||
$this->closestPlayerThatCanFeedDist = $dist;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function handlePlayerSearcher(Player $player, $dist){
|
||||
parent::handlePlayerSearcher($player, $dist);
|
||||
|
||||
if($this->closestPlayerThatCanFeedDist >= $dist){
|
||||
if($player->spawned && $this->isFood($player->getHeldItem()->id)){
|
||||
if($this->isPlayerValid($player)){
|
||||
$this->closestPlayerThatCanFeedDist = $dist;
|
||||
$this->closestPlayerThatCanFeedEID = $player->entity->eid;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,11 +21,18 @@ abstract class Creature extends Living{
|
|||
return parent::update($now);
|
||||
}
|
||||
|
||||
|
||||
public function handlePrePlayerSearcher(){
|
||||
parent::handlePrePlayerSearcher();
|
||||
if($this->closestPlayerEID !== false && !isset($this->level->entityList[$this->closestPlayerEID])){
|
||||
$this->closestPlayerEID = false;
|
||||
$this->closestPlayerDist = INF;
|
||||
if($this->closestPlayerEID !== false){
|
||||
$player = $this->level->entityList[$this->closestPlayerEID] ?? false;
|
||||
if($player === false){
|
||||
$this->closestPlayerEID = false;
|
||||
$this->closestPlayerDist = INF;
|
||||
}else{
|
||||
$dist = ($this->x - $player->x)*($this->x - $player->x) + ($this->y - $player->y)*($this->y - $player->y) + ($this->z - $player->z)*($this->z - $player->z);
|
||||
$this->closestPlayerDist = $dist;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,8 +1,10 @@
|
|||
<?php
|
||||
class Monster extends Creature{
|
||||
|
||||
public $closestPlayerToAttackEID = false;
|
||||
public $closestPlayerToAttackDist = INF;
|
||||
public function __construct(Level $level, $eid, $class, $type = 0, $data = []){
|
||||
parent::__construct($level, $eid, $class, $type, $data);
|
||||
//$this->ai->addTask(new TaskAttackPlayer());
|
||||
}
|
||||
|
||||
public function getAttackDamage(){
|
||||
|
|
@ -20,4 +22,34 @@ class Monster extends Creature{
|
|||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public function isPlayerValid(Player $player){
|
||||
return $player->spawned && !$player->entity->dead;
|
||||
}
|
||||
|
||||
public function handlePrePlayerSearcher(){
|
||||
parent::handlePrePlayerSearcher();
|
||||
if($this->closestPlayerToAttackEID !== false){
|
||||
$player = $this->level->entityList[$this->closestPlayerToAttackEID] ?? false;
|
||||
if($player === false || !$this->isPlayerValid($player->player)){
|
||||
$this->closestPlayerToAttackEID = false;
|
||||
$this->closestPlayerToAttackDist = INF;
|
||||
}else{
|
||||
$dist = ($this->x - $player->x)*($this->x - $player->x) + ($this->y - $player->y)*($this->y - $player->y) + ($this->z - $player->z)*($this->z - $player->z);
|
||||
$this->closestPlayerToAttackDist = $dist;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function handlePlayerSearcher(Player $player, $dist){
|
||||
parent::handlePlayerSearcher($player, $dist);
|
||||
|
||||
if($this->closestPlayerToAttackDist >= $dist){
|
||||
if($this->isPlayerValid($player)){
|
||||
$this->closestPlayerToAttackDist = $dist;
|
||||
$this->closestPlayerToAttackEID = $player->entity->eid;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -50,7 +50,7 @@ class TaskAttackPlayer extends TaskBase
|
|||
|
||||
public function isTargetValid(EntityAI $ai){
|
||||
$e = $ai->entity;
|
||||
if($e->target instanceof Entity && !$e->target->closed){
|
||||
if($e->target instanceof Entity && !$e->target->closed && !$e->target->dead){
|
||||
$t = $e->target;
|
||||
$xDiff = ($t->x - $e->x);
|
||||
$yDiff = ($t->y - $e->y);
|
||||
|
|
@ -72,7 +72,7 @@ class TaskAttackPlayer extends TaskBase
|
|||
}
|
||||
}
|
||||
|
||||
$closestTarget = $e->closestPlayerDist <= $this->rangeSquared ? $e->level->entityList[$e->closestPlayerEID] : null;
|
||||
$closestTarget = $e->closestPlayerToAttackDist <= $this->rangeSquared ? $e->level->entityList[$e->closestPlayerToAttackEID] : null;
|
||||
|
||||
if($closestTarget != null){
|
||||
$e->target = $closestTarget; //TODO dont save entity object ?
|
||||
|
|
|
|||
|
|
@ -73,26 +73,26 @@ class TaskRangedAttack extends \TaskBase
|
|||
$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;
|
||||
$v20 = $v12 * 0.2;
|
||||
$arrow->shoot($diffX, $diffY + $v20, $diffZ, 1.6, 12);
|
||||
$this->server->api->entity->spawnToAll($arrow);
|
||||
//}
|
||||
if($v12 >= 0.0000001){
|
||||
$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;
|
||||
$v20 = $v12 * 0.2;
|
||||
$arrow->shoot($diffX, $diffY + $v20, $diffZ, 1.6, 12);
|
||||
$this->server->api->entity->spawnToAll($arrow);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function isTargetValid(EntityAI $ai){
|
||||
$e = $ai->entity;
|
||||
if($e->target instanceof Entity && !$e->target->closed){
|
||||
if($e->target instanceof Entity && !$e->target->closed && !$e->target->dead){
|
||||
$t = $e->target;
|
||||
$xDiff = ($t->x - $e->x);
|
||||
$yDiff = ($t->y - $e->y);
|
||||
|
|
@ -114,7 +114,7 @@ class TaskRangedAttack extends \TaskBase
|
|||
}
|
||||
}
|
||||
|
||||
$closestTarget = $e->closestPlayerDist <= $this->rangeSquared ? $e->level->entityList[$e->closestPlayerEID] : null;
|
||||
$closestTarget = $e->closestPlayerToAttackDist <= $this->rangeSquared ? $e->level->entityList[$e->closestPlayerToAttackEID] : null;
|
||||
|
||||
if($closestTarget != null){
|
||||
$e->target = $closestTarget; //TODO dont save entity object ?
|
||||
|
|
|
|||
|
|
@ -584,7 +584,7 @@ class Level{
|
|||
$e->handlePrePlayerSearcher();
|
||||
|
||||
foreach($this->players as $player){
|
||||
$dist = ($e->x - $player->entity->x)*($e->x - $player->entity->x) + ($e->y - $player->entity->y) + ($e->z - $player->entity->z);
|
||||
$dist = ($e->x - $player->entity->x)*($e->x - $player->entity->x) + ($e->y - $player->entity->y)*($e->y - $player->entity->y) + ($e->z - $player->entity->z)*($e->z - $player->entity->z);
|
||||
$e->handlePlayerSearcher($player, $dist);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue