mirror of
https://github.com/oldminecraftcommunity/NostalgiaCore.git
synced 2026-11-22 20:49:43 +00:00
fixed bug with flying detection, added panic task
This commit is contained in:
parent
7c47c852e2
commit
d7561ed1ba
10 changed files with 70 additions and 17 deletions
|
|
@ -16,6 +16,13 @@ abstract class Animal extends Creature implements Ageable, Breedable{
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->ai->addTask(new TaskTempt());
|
$this->ai->addTask(new TaskTempt());
|
||||||
|
$this->ai->addTask(new TaskPanic());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function harm($dmg, $cause = "generic", $force = false){
|
||||||
|
$ret = parent::harm($dmg, $cause, $force);
|
||||||
|
$this->inPanic |= ($ret && is_numeric($cause));
|
||||||
|
return $ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function isBaby(){
|
public function isBaby(){
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@ abstract class Creature extends Living{
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getSpeedModifer(){
|
public function getSpeedModifer(){
|
||||||
return $this->speedModifer * ($this->inPanic ? 1.4 : 0.7);
|
return $this->speedModifer * ($this->inPanic ? 1.0 : 0.7);
|
||||||
}
|
}
|
||||||
public function getArmorValue(){
|
public function getArmorValue(){
|
||||||
return 2;
|
return 2;
|
||||||
|
|
|
||||||
|
|
@ -133,7 +133,7 @@ class Entity extends Position
|
||||||
$this->player = $this->data["player"];
|
$this->player = $this->data["player"];
|
||||||
$this->setHealth($this->health, "generic");
|
$this->setHealth($this->health, "generic");
|
||||||
$this->speedModifer = 1;
|
$this->speedModifer = 1;
|
||||||
$this->width = 0.6;
|
$this->width = 1.2;
|
||||||
$this->height = 1.8;
|
$this->height = 1.8;
|
||||||
$this->hasKnockback = true;
|
$this->hasKnockback = true;
|
||||||
$this->hasGravity = true;
|
$this->hasGravity = true;
|
||||||
|
|
@ -523,7 +523,7 @@ class Entity extends Position
|
||||||
for($x = $startX; $x <= $endX; ++ $x){
|
for($x = $startX; $x <= $endX; ++ $x){
|
||||||
$v = new Vector3($x, $y, $z);
|
$v = new Vector3($x, $y, $z);
|
||||||
$v1 = new Vector3($x, $yC, $z);
|
$v1 = new Vector3($x, $yC, $z);
|
||||||
if($this->isSupport($v, $this->radius)){
|
if($this->isSupport($v, $this->width)){
|
||||||
$b = $this->level->getBlock($v);
|
$b = $this->level->getBlock($v);
|
||||||
if($b->isSolid === true){
|
if($b->isSolid === true){
|
||||||
$support = true;
|
$support = true;
|
||||||
|
|
@ -532,7 +532,7 @@ class Entity extends Position
|
||||||
} elseif(($b instanceof LiquidBlock) or $b->getID() === COBWEB or $b->getID() === LADDER or $b->getID() === FENCE or $b->getID() === STONE_WALL or $b->getID() === IRON_BARS){
|
} elseif(($b instanceof LiquidBlock) or $b->getID() === COBWEB or $b->getID() === LADDER or $b->getID() === FENCE or $b->getID() === STONE_WALL or $b->getID() === IRON_BARS){
|
||||||
$isFlying = false;
|
$isFlying = false;
|
||||||
}
|
}
|
||||||
} elseif($this->isSupport($v1, $this->radius)){
|
} elseif($this->isSupport($v1, $this->width)){
|
||||||
$b = $this->level->getBlock($v1);
|
$b = $this->level->getBlock($v1);
|
||||||
if($b->isSolid === true){
|
if($b->isSolid === true){
|
||||||
$support = true;
|
$support = true;
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,6 @@ class EntityAI
|
||||||
console("[DEBUG] Adding new task...",true, true, 2);
|
console("[DEBUG] Adding new task...",true, true, 2);
|
||||||
$this->tasks[$task->__toString()] = $task;
|
$this->tasks[$task->__toString()] = $task;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function removeTask($name){
|
public function removeTask($name){
|
||||||
if(isset($this->tasks[$name])){
|
if(isset($this->tasks[$name])){
|
||||||
unset($this->tasks[$name]);
|
unset($this->tasks[$name]);
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@ class MobController
|
||||||
|
|
||||||
public function moveNonInstant($x, $y, $z){
|
public function moveNonInstant($x, $y, $z){
|
||||||
if($x == 0 && $y == 0 && $z == 0){
|
if($x == 0 && $y == 0 && $z == 0){
|
||||||
return false; //failed
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$ox = ($x > 0 ? 1 : ($x < 0 ? -1 : 0));
|
$ox = ($x > 0 ? 1 : ($x < 0 ? -1 : 0));
|
||||||
|
|
@ -58,7 +58,7 @@ class MobController
|
||||||
$tan = $dz == 0 ? ($dx < 0 ? 180 : 0) : (90 - rad2deg(atan($dx / $dz)));
|
$tan = $dz == 0 ? ($dx < 0 ? 180 : 0) : (90 - rad2deg(atan($dx / $dz)));
|
||||||
$thetaOffset = $dz < 0 ? 90 : 270;
|
$thetaOffset = $dz < 0 ? 90 : 270;
|
||||||
$calcYaw = ($thetaOffset + $tan);
|
$calcYaw = ($thetaOffset + $tan);
|
||||||
$this->entity->yaw = $this->entity->headYaw = $calcYaw;
|
$this->entity->headYaw = $this->entity->yaw = $calcYaw;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function lookOffset($x, $y, $z, $pitch = true){
|
public function lookOffset($x, $y, $z, $pitch = true){
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ class TaskLookAround extends TaskBase
|
||||||
|
|
||||||
public function canBeExecuted(EntityAI $ai)
|
public function canBeExecuted(EntityAI $ai)
|
||||||
{
|
{
|
||||||
return !$ai->entity->isMoving() && Utils::randomFloat() < 0.02 && !$ai->isStarted("TaskLookAtPlayer") && !$ai->isStarted("TaskTempt") && !$ai->entity->hasPath(); /*Vanilla value*/
|
return !$ai->entity->inPanic && !$ai->entity->isMoving() && Utils::randomFloat() < 0.02 && !$ai->isStarted("TaskLookAtPlayer") && !$ai->isStarted("TaskTempt") && !$ai->entity->hasPath(); /*Vanilla value*/
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onUpdate(EntityAI $ai)
|
public function onUpdate(EntityAI $ai)
|
||||||
|
|
|
||||||
|
|
@ -1,32 +1,38 @@
|
||||||
<?php
|
<?php
|
||||||
|
|
||||||
class TaskLookAtPlayer extends TaskBase{
|
class TaskLookAtPlayer extends TaskBase{
|
||||||
const S_LOOK = 0x1;
|
|
||||||
const S_STOPLOOK = 0x2;
|
|
||||||
public $target = false;
|
public $target = false;
|
||||||
private $state, $yaw, $pitch;
|
private $state, $yaw, $pitch;
|
||||||
public function canBeExecuted(EntityAI $ai){
|
public function canBeExecuted(EntityAI $ai){
|
||||||
return Utils::randomFloat() < 0.02 && !$ai->entity->isMovingHorizontally() && !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskTempt") && !$ai->entity->hasPath();
|
return !$ai->entity->inPanic && Utils::randomFloat() < 0.02 && !$ai->entity->isMovingHorizontally() && !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskTempt") && !$ai->entity->hasPath();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function findTarget($e, $r){
|
protected function findTarget($e, $r){
|
||||||
$svd = null;
|
$svd = null;
|
||||||
$svdDist = -1;
|
$svdDist = -1;
|
||||||
foreach($e->server->api->entity->getRadius($e, $r, ENTITY_PLAYER) as $p){
|
foreach($e->level->players as $p){
|
||||||
|
$p = $p->entity;
|
||||||
|
if(Utils::distance_noroot($e, $p) > $r*$r){
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if($svdDist === -1){
|
if($svdDist === -1){
|
||||||
$svdDist = Utils::manh_distance($e, $p);
|
$svdDist = Utils::distance_noroot($e, $p);
|
||||||
$svd = $p;
|
$svd = $p;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
if($svd != null && $svdDist === 0){
|
if($svd != null && $svdDist === 0){
|
||||||
$svd = $p;
|
$svd = $p;
|
||||||
}
|
}
|
||||||
|
if(($cd = Utils::distance_noroot($e, $p)) < $svdDist){
|
||||||
if(($cd = Utils::manh_distance($e, $p)) < $svdDist){
|
|
||||||
$svdDist = $cd;
|
$svdDist = $cd;
|
||||||
$svd = $p;
|
$svd = $p;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if($svd == null){
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
return $svd;
|
return $svd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
41
src/entity/ai/tasks/TaskPanic.php
Normal file
41
src/entity/ai/tasks/TaskPanic.php
Normal file
|
|
@ -0,0 +1,41 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
class TaskPanic extends TaskBase
|
||||||
|
{
|
||||||
|
protected $randX = 2, $randZ = 2;
|
||||||
|
public function onStart(EntityAI $ai)
|
||||||
|
{
|
||||||
|
$this->selfCounter = mt_rand(60, 120);
|
||||||
|
$this->regenerateRandXZ();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onEnd(EntityAI $ai)
|
||||||
|
{
|
||||||
|
$ai->entity->inPanic = false;
|
||||||
|
$this->reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function regenerateRandXZ(){
|
||||||
|
$this->randX = (mt_rand(0, 1) ? -1 : 1) * 2;
|
||||||
|
$this->randZ = (mt_rand(0, 1) ? -1 : 1) * 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function reset(){
|
||||||
|
$this->randX = $this->randZ = 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function onUpdate(EntityAI $ai)
|
||||||
|
{
|
||||||
|
--$this->selfCounter;
|
||||||
|
if($this->selfCounter % 20 === 0) $this->regenerateRandXZ();
|
||||||
|
$ai->mobController->moveNonInstant($this->randX, 0, $this->randZ);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function canBeExecuted(EntityAI $ai)
|
||||||
|
{
|
||||||
|
return $ai->entity instanceof Animal && $ai->entity->inPanic && $ai->entity->knockbackTime <= 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -34,7 +34,7 @@ class TaskRandomWalk extends TaskBase
|
||||||
if(($ai->entity instanceof Creeper && $ai->entity->isIgnited()) || $ai->entity->hasPath() || $ai->isStarted("TaskTempt")) {
|
if(($ai->entity instanceof Creeper && $ai->entity->isIgnited()) || $ai->entity->hasPath() || $ai->isStarted("TaskTempt")) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskLookAtPlayer") && mt_rand(0, 120) == 0;
|
return !$ai->entity->inPanic && !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskLookAtPlayer") && mt_rand(0, 120) == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -29,7 +29,7 @@ class TaskTempt extends TaskBase
|
||||||
|
|
||||||
public function canBeExecuted(EntityAI $ai)
|
public function canBeExecuted(EntityAI $ai)
|
||||||
{
|
{
|
||||||
if(!($ai->entity instanceof Breedable)){ //TODO Work with path
|
if(!($ai->entity instanceof Breedable) || $ai->entity->inPanic){ //TODO Work with path
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
$target = $this->findTarget($ai->entity, 10);
|
$target = $this->findTarget($ai->entity, 10);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue