mirror of
https://github.com/oldminecraftcommunity/NostalgiaCore.git
synced 2026-11-24 21:49: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
|
|
@ -299,6 +299,13 @@ class Player{
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$pk = new SetEntityMotionPacket();
|
||||||
|
$pk->eid = 0;
|
||||||
|
$pk->speedX = 0;
|
||||||
|
$pk->speedY = 0;
|
||||||
|
$pk->speedZ = 0;
|
||||||
|
$this->dataPacket($pk);
|
||||||
|
|
||||||
$pk = new MovePlayerPacket;
|
$pk = new MovePlayerPacket;
|
||||||
$pk->eid = 0;
|
$pk->eid = 0;
|
||||||
$pk->x = $pos->x;
|
$pk->x = $pos->x;
|
||||||
|
|
|
||||||
|
|
@ -21,21 +21,29 @@ abstract class Animal extends Creature implements Ageable, Breedable{
|
||||||
$this->searchForClosestPlayers = true;
|
$this->searchForClosestPlayers = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function handlePrePlayerSearcher(){
|
public function isPlayerValid(Player $player){
|
||||||
parent::handlePrePlayerSearcher();
|
return $player->spawned && $this->isFood($player->getHeldItem()->id) && !$player->entity->dead;
|
||||||
|
|
||||||
if($this->closestPlayerThatCanFeedEID !== false && !isset($this->level->entityList[$this->closestPlayerThatCanFeedEID])){
|
|
||||||
$this->closestPlayerThatCanFeedEID = false;
|
|
||||||
$this->closestPlayerThatCanFeedDist = INF;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function handlePrePlayerSearcher(){
|
||||||
|
parent::handlePrePlayerSearcher();
|
||||||
|
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){
|
public function handlePlayerSearcher(Player $player, $dist){
|
||||||
parent::handlePlayerSearcher($player, $dist);
|
parent::handlePlayerSearcher($player, $dist);
|
||||||
|
|
||||||
if($this->closestPlayerThatCanFeedDist >= $dist){
|
if($this->closestPlayerThatCanFeedDist >= $dist){
|
||||||
if($player->spawned && $this->isFood($player->getHeldItem()->id)){
|
if($this->isPlayerValid($player)){
|
||||||
$this->closestPlayerThatCanFeedDist = $dist;
|
$this->closestPlayerThatCanFeedDist = $dist;
|
||||||
$this->closestPlayerThatCanFeedEID = $player->entity->eid;
|
$this->closestPlayerThatCanFeedEID = $player->entity->eid;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,11 +21,18 @@ abstract class Creature extends Living{
|
||||||
return parent::update($now);
|
return parent::update($now);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function handlePrePlayerSearcher(){
|
public function handlePrePlayerSearcher(){
|
||||||
parent::handlePrePlayerSearcher();
|
parent::handlePrePlayerSearcher();
|
||||||
if($this->closestPlayerEID !== false && !isset($this->level->entityList[$this->closestPlayerEID])){
|
if($this->closestPlayerEID !== false){
|
||||||
|
$player = $this->level->entityList[$this->closestPlayerEID] ?? false;
|
||||||
|
if($player === false){
|
||||||
$this->closestPlayerEID = false;
|
$this->closestPlayerEID = false;
|
||||||
$this->closestPlayerDist = INF;
|
$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
|
<?php
|
||||||
class Monster extends Creature{
|
class Monster extends Creature{
|
||||||
|
|
||||||
|
public $closestPlayerToAttackEID = false;
|
||||||
|
public $closestPlayerToAttackDist = INF;
|
||||||
public function __construct(Level $level, $eid, $class, $type = 0, $data = []){
|
public function __construct(Level $level, $eid, $class, $type = 0, $data = []){
|
||||||
parent::__construct($level, $eid, $class, $type, $data);
|
parent::__construct($level, $eid, $class, $type, $data);
|
||||||
//$this->ai->addTask(new TaskAttackPlayer());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getAttackDamage(){
|
public function getAttackDamage(){
|
||||||
|
|
@ -20,4 +22,34 @@ class Monster extends Creature{
|
||||||
return false;
|
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){
|
public function isTargetValid(EntityAI $ai){
|
||||||
$e = $ai->entity;
|
$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;
|
$t = $e->target;
|
||||||
$xDiff = ($t->x - $e->x);
|
$xDiff = ($t->x - $e->x);
|
||||||
$yDiff = ($t->y - $e->y);
|
$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){
|
if($closestTarget != null){
|
||||||
$e->target = $closestTarget; //TODO dont save entity object ?
|
$e->target = $closestTarget; //TODO dont save entity object ?
|
||||||
|
|
|
||||||
|
|
@ -73,7 +73,7 @@ class TaskRangedAttack extends \TaskBase
|
||||||
$diffY = ($target->boundingBox->minY + ($target->height / 3)) - $posY;
|
$diffY = ($target->boundingBox->minY + ($target->height / 3)) - $posY;
|
||||||
$diffZ = $target->z - $selfEntity->z;
|
$diffZ = $target->z - $selfEntity->z;
|
||||||
$v12 = sqrt($diffX * $diffX + $diffZ * $diffZ);
|
$v12 = sqrt($diffX * $diffX + $diffZ * $diffZ);
|
||||||
//if($v12 >= 1.0e-7){
|
if($v12 >= 0.0000001){
|
||||||
$yaw = ((atan2($diffZ, $diffX) * 180) / M_PI) - 90;
|
$yaw = ((atan2($diffZ, $diffX) * 180) / M_PI) - 90;
|
||||||
$pitch = -((atan2($diffY, $v12) * 180) / M_PI);
|
$pitch = -((atan2($diffY, $v12) * 180) / M_PI);
|
||||||
$v16 = $diffX / $v12;
|
$v16 = $diffX / $v12;
|
||||||
|
|
@ -86,13 +86,13 @@ class TaskRangedAttack extends \TaskBase
|
||||||
$v20 = $v12 * 0.2;
|
$v20 = $v12 * 0.2;
|
||||||
$arrow->shoot($diffX, $diffY + $v20, $diffZ, 1.6, 12);
|
$arrow->shoot($diffX, $diffY + $v20, $diffZ, 1.6, 12);
|
||||||
$this->server->api->entity->spawnToAll($arrow);
|
$this->server->api->entity->spawnToAll($arrow);
|
||||||
//}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public function isTargetValid(EntityAI $ai){
|
public function isTargetValid(EntityAI $ai){
|
||||||
$e = $ai->entity;
|
$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;
|
$t = $e->target;
|
||||||
$xDiff = ($t->x - $e->x);
|
$xDiff = ($t->x - $e->x);
|
||||||
$yDiff = ($t->y - $e->y);
|
$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){
|
if($closestTarget != null){
|
||||||
$e->target = $closestTarget; //TODO dont save entity object ?
|
$e->target = $closestTarget; //TODO dont save entity object ?
|
||||||
|
|
|
||||||
|
|
@ -584,7 +584,7 @@ class Level{
|
||||||
$e->handlePrePlayerSearcher();
|
$e->handlePrePlayerSearcher();
|
||||||
|
|
||||||
foreach($this->players as $player){
|
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);
|
$e->handlePlayerSearcher($player, $dist);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue