test movement added

This commit is contained in:
GameHerobrine 2024-01-09 07:36:14 +03:00
parent 5161d087ba
commit 1ac7179a97
11 changed files with 91 additions and 87 deletions

View file

@ -24,7 +24,7 @@ abstract class Creature extends Living{
} }
public function getSpeedModifer(){ public function getSpeedModifer(){
return $this->speedModifer * ($this->inPanic ? 1.5 : 1.0); return $this->speedModifer;
} }
public function getArmorValue(){ public function getArmorValue(){
return 2; return 2;

View file

@ -91,7 +91,7 @@ class Entity extends Position
$this->canBeAttacked = false; $this->canBeAttacked = false;
$this->hasKnockback = false; $this->hasKnockback = false;
$this->level = $level; $this->level = $level;
$this->speedModifer = 0.7; $this->speedModifer = 1;
$this->fallY = false; $this->fallY = false;
$this->fallStart = false; $this->fallStart = false;
$this->server = ServerAPI::request(); $this->server = ServerAPI::request();

View file

@ -10,6 +10,8 @@ abstract class Living extends Entity implements Pathfindable{
public $landMovementFactor = 0.1; //TODO may not be constant public $landMovementFactor = 0.1; //TODO may not be constant
public $jumpMovementFactor = 0.02; //TODO may not be constant public $jumpMovementFactor = 0.02; //TODO may not be constant
public $aiMoveSpeed;
public function __construct(Level $level, $eid, $class, $type = 0, $data = array()){ public function __construct(Level $level, $eid, $class, $type = 0, $data = array()){
$this->target = false; $this->target = false;
@ -28,6 +30,17 @@ abstract class Living extends Entity implements Pathfindable{
$this->harm($dmg, "fall"); $this->harm($dmg, "fall");
} }
} }
public function getAIMoveSpeed(){
return $this->aiMoveSpeed;
}
public function setAIMoveSpeed($speed){
console("set spedd to $speed");
$this->aiMoveSpeed = $speed;
$this->moveForward = $speed;
}
public function hasPath(){ public function hasPath(){
return $this->path != null; return $this->path != null;
} }
@ -100,9 +113,9 @@ abstract class Living extends Entity implements Pathfindable{
if(!$this->dead && Entity::$allowedAI && $this->idleTime <= 0) { if(!$this->dead && Entity::$allowedAI && $this->idleTime <= 0) {
$this->ai->updateTasks(); $this->ai->updateTasks();
} }
$this->ai->mobController->rotateTick();
$this->ai->mobController->movementTick();
$this->ai->mobController->movementTick();
$this->ai->mobController->rotateTick();
if(abs($this->speedX) < self::MIN_POSSIBLE_SPEED) $this->speedX = 0; if(abs($this->speedX) < self::MIN_POSSIBLE_SPEED) $this->speedX = 0;
if(abs($this->speedZ) < self::MIN_POSSIBLE_SPEED) $this->speedZ = 0; if(abs($this->speedZ) < self::MIN_POSSIBLE_SPEED) $this->speedZ = 0;

View file

@ -11,7 +11,7 @@ class MobController
FIRE => true, FIRE => true,
]; ];
/** /**
* @var Entity * @var Living
*/ */
public $entity; public $entity;
@ -20,6 +20,10 @@ class MobController
protected $jumping; protected $jumping;
protected $jumpTimeout; protected $jumpTimeout;
public $moveToX, $moveToY, $moveToZ;
public $speed;
public $updateMove = false;
public function __construct($e){ public function __construct($e){
$this->entity = $e; $this->entity = $e;
} }
@ -38,89 +42,66 @@ class MobController
return $this->finalHeadYaw === $this->entity->headYaw; return $this->finalHeadYaw === $this->entity->headYaw;
} }
public function moveNonInstant($x, $y, $z, $camera = true){ public function setMovingOffset($x, $y, $z, $speed){
if($x == 0 && $y == 0 && $z == 0){ $this->moveToX = $this->entity->x + ($x);
return false; $this->moveToY = $this->entity->y + ($y);
} $this->moveToZ = $this->entity->z + ($z);
$this->speed = $speed;
$ox = $x <=> 1; $this->updateMove = true;
$oy = $y <=> 1;
$oz = $z <=> 1;
$xf = $this->entity->x + ($this->entity->getSpeedModifer() * $ox * $this->entity->getSpeed());
$zf = $this->entity->z + ($this->entity->getSpeedModifer() * $oz * $this->entity->getSpeed());
$a = $b = $c = $d = 0;
if(self::$AUTOJUMP){
$oy = $this->entity->onGround && (
StaticBlock::getIsSolid(($a = $this->entity->level->level->getBlockID(ceil($xf), (int)($this->entity->y), ceil($zf)))) &&
!StaticBlock::getIsSolid($this->entity->level->level->getBlockID(ceil($xf), (int)($this->entity->y) + 1, ceil($zf)))
||
StaticBlock::getIsSolid(($b = $this->entity->level->level->getBlockID(ceil($xf), (int)($this->entity->y), $zf - ($oz < 0)))) &&
!StaticBlock::getIsSolid($this->entity->level->level->getBlockID(ceil($xf), (int)($this->entity->y) + 1, $zf - ($oz < 0)))
||
StaticBlock::getIsSolid(($c = $this->entity->level->level->getBlockID($xf - ($ox < 0), (int)($this->entity->y), $zf - ($oz < 0)))) &&
!StaticBlock::getIsSolid($this->entity->level->level->getBlockID($xf - ($ox < 0), (int)($this->entity->y) + 1, $zf - ($oz < 0)))
||
StaticBlock::getIsSolid(($d = $this->entity->level->level->getBlockID($xf - ($ox < 0), (int)($this->entity->y), ceil($zf)))) &&
!StaticBlock::getIsSolid($this->entity->level->level->getBlockID($xf - ($ox < 0), (int)($this->entity->y) + 1, ceil($zf)))
);
}
while(self::$ADVANCED){
if($this->isDangerous($a) || $this->isDangerous($b) || $this->isDangerous($c) || $this->isDangerous($d)){
return false;
}
$id1 = $this->entity->level->level->getBlockID($xf, $this->entity->y + $oy - 1, $zf);
if($this->isDangerous($id1)) return false;
if(!StaticBlock::getIsSolid($id1)){
$id2 = $this->entity->level->level->getBlockID($xf, $this->entity->y - 2, $zf);
$id3 = $this->entity->level->level->getBlockID($xf, $this->entity->y - 3, $zf);
$id4 = $this->entity->level->level->getBlockID($xf, $this->entity->y - 4, $zf);
$s2 = StaticBlock::getIsSolid($id2);
$s3 = StaticBlock::getIsSolid($id3);
$s4 = StaticBlock::getIsSolid($id4);
if($this->isDangerous($id2)) return false;
if($s2) break; //i cant goto label which wasnt declared before
if($this->isDangerous($id3)) return false;
if($s3) break;
if($this->isDangerous($id4)) return false;
if($s4) break;
if(!($this->entity instanceof Chicken)){
return false;
}
}
break;
}
if($camera) $this->faceEntity($ox, $oy, $oz);
if($this->entity->knockbackTime <= 0 && $this->isRotationCompleted()){
$this->entity->moveEntityWithOffset($ox, $oy, $oz);
}
return true;
} }
public function canJump(){ public function canJump(){
return $this->isJumping() && $this->jumpTimeout <= 0 && $this->entity->onGround; return $this->isJumping() && $this->jumpTimeout <= 0 && $this->entity->onGround;
} }
public static function limitAngle($old, $newA, $limit){
$v4 = Utils::wrapAngleTo180($newA - $old);
if($v4 > $limit) $v4 = $limit;
if($v4 < -$limit) $v4 = -$limit;
return $old + $v4;
}
public function movementTick(){ public function movementTick(){
if($this->canJump()){ $this->entity->moveForward = 0;
$this->jumpTimeout = 10; if($this->updateMove){
$this->entity->speedY = 0.50; $this->updateMove = false;
$v1 = floor($this->entity->boundingBox->minY + 0.5);
$diffX = $this->moveToX - $this->entity->x;
$diffZ = $this->moveToZ - $this->entity->z;
$diffY = $this->moveToY - $v1;
$v8 = $diffX*$diffX + $diffY*$diffY + $diffZ*$diffZ;
if($v8 >= 2.500000277905201E-7){ //TODO convert notation
$v10 = (atan2($diffZ, $diffX) * 180 / M_PI) - 90;
console("$v10: ".$v10);
$this->entity->yaw = self::limitAngle($this->entity->yaw, $v10, 30);
console("yaw: {$this->entity->yaw}");
$this->entity->setAIMoveSpeed($this->speed * $this->entity->getSpeedModifer());
if($diffY > 0 && $diffX*$diffX + $diffZ*$diffZ < 1) $this->setJumping(true);
}
} }
if($this->jumpTimeout > 0) --$this->jumpTimeout; //TODO handle jumps somewhere
//if($this->canJump()){
// $this->jumpTimeout = 10;
// $this->entity->speedY = 0.50;
//}
//if($this->jumpTimeout > 0) --$this->jumpTimeout;
} }
public function rotateTick(){ //TODO handle more rotation public function rotateTick(){ //TODO handle more rotation
$this->entity->lastHeadYaw = $this->entity->headYaw; //$this->entity->lastHeadYaw = $this->entity->headYaw;
$w180 = Utils::wrapAngleTo180($this->finalHeadYaw - $this->entity->headYaw); //$w180 = Utils::wrapAngleTo180($this->finalHeadYaw - $this->entity->headYaw);
$w180min = min(abs($w180), 20)*Utils::getSign($w180); //$w180min = min(abs($w180), 20)*Utils::getSign($w180);
$this->entity->headYaw = Utils::wrapAngleTo360($this->entity->headYaw + $w180min); //$this->entity->headYaw = Utils::wrapAngleTo360($this->entity->headYaw + $w180min);
} }
public function moveTo($x, $y, $z, $camera = true){ public function moveTo($x, $y, $z, $camera = true){
return $this->moveNonInstant($x - floor($this->entity->x), $y - floor($this->entity->y), $z - floor($this->entity->z), $camera); //TODO fix return $this->moveNonInstant($x - floor($this->entity->x), $y - floor($this->entity->y), $z - floor($this->entity->z), $camera);
} }
public function faceEntity($x, $y, $z){ public function faceEntity($x, $y, $z){

View file

@ -16,7 +16,7 @@ class TaskAttackPlayer extends TaskTempt
return; return;
} }
$ai->mobController->moveTo($this->target->x - 0.5, floor($ai->entity->y), $this->target->z - 0.5, false); //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); $ai->mobController->lookOn($this->target);
$mult = $ai->entity instanceof Spider ? 1 : 2; $mult = $ai->entity instanceof Spider ? 1 : 2;
$radius = ($ai->entity->width * $mult)*($ai->entity->width * $mult); $radius = ($ai->entity->width * $mult)*($ai->entity->width * $mult);

View file

@ -21,7 +21,7 @@ class TaskDestroyServerPerformance extends TaskTempt
return; return;
} }
if(Utils::distance_noroot($this->target, $ai->entity) > 100){ if(Utils::distance_noroot($this->target, $ai->entity) > 100){
$ai->mobController->moveTo($this->target->x, floor($ai->entity->y), $this->target->z, false); //TODO fix $ai->mobController->moveTo($this->target->x, floor($ai->entity->y), $this->target->z, false);
}else{ }else{
} }

View file

@ -24,7 +24,7 @@ class TaskMate extends \TaskBase
$this->onEnd($ai); $this->onEnd($ai);
return; return;
} }
$ai->mobController->moveTo($movTo->x, $movTo->y, $movTo->z, false); //TODO fix $ai->mobController->moveTo($movTo->x, $movTo->y, $movTo->z, false);
$ai->mobController->lookOn($movTo); $ai->mobController->lookOn($movTo);
--$this->selfCounter; --$this->selfCounter;

View file

@ -28,7 +28,7 @@ class TaskPanic extends TaskBase
{ {
--$this->selfCounter; --$this->selfCounter;
if($this->selfCounter % 20 === 0) $this->regenerateRandXZ(); if($this->selfCounter % 20 === 0) $this->regenerateRandXZ();
$ai->mobController->moveNonInstant($this->randX, 0, $this->randZ); //TODO fix $ai->mobController->moveNonInstant($this->randX, 0, $this->randZ);
} }
public function canBeExecuted(EntityAI $ai) public function canBeExecuted(EntityAI $ai)

View file

@ -3,10 +3,15 @@
class TaskRandomWalk extends TaskBase class TaskRandomWalk extends TaskBase
{ {
private $x, $z; private $x, $z;
public $moveSpeed;
public function __construct($moveSpeed){
$this->moveSpeed = 0.25;
}
public function onStart(EntityAI $ai) public function onStart(EntityAI $ai)
{ {
$this->x = mt_rand(-16, 16); $this->x = mt_rand(-10, 10);
$this->z = mt_rand(-16, 16); $this->z = mt_rand(-10, 10);
if($this->x === 0 && $this->z === 0){ if($this->x === 0 && $this->z === 0){
$this->reset(); $this->reset();
return false; return false;
@ -26,15 +31,17 @@ class TaskRandomWalk extends TaskBase
return false; //TODO Better way: block movement return false; //TODO Better way: block movement
} }
--$this->selfCounter; --$this->selfCounter;
$ai->mobController->moveNonInstant($this->x, 0, $this->z); $ai->mobController->setMovingOffset($this->x, 0, $this->z, $this->moveSpeed);
//TODO fix $ai->mobController->moveNonInstant($this->x, 0, $this->z);
} }
public function canBeExecuted(EntityAI $ai) public function canBeExecuted(EntityAI $ai)
{ {
if(($ai->entity instanceof Creeper && $ai->entity->isIgnited()) || $ai->entity->hasPath() || $ai->isStarted("TaskTempt") ||$ai->isStarted("TaskAttackPlayer")) { return true;
return false; //if(($ai->entity instanceof Creeper && $ai->entity->isIgnited()) || $ai->entity->hasPath() || $ai->isStarted("TaskTempt") ||$ai->isStarted("TaskAttackPlayer")) {
} // i really need mutexBits // return false;
return !$ai->entity->inPanic && !$ai->isStarted("TaskMate") && !$ai->isStarted("TaskEatTileGoal") && !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskLookAtPlayer") && mt_rand(0, 120) == 0; //} // i really need mutexBits
//return !$ai->entity->inPanic && !$ai->isStarted("TaskMate") && !$ai->isStarted("TaskEatTileGoal") && !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskLookAtPlayer") && mt_rand(0, 120) == 0;
} }

View file

@ -23,7 +23,7 @@ class TaskTempt extends TaskBase
return; return;
} }
$ai->mobController->moveTo($this->target->x - 0.5, floor($ai->entity->y), $this->target->z - 0.5, false); //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); $ai->mobController->lookOn($this->target);
} }

View file

@ -9,6 +9,9 @@ class Pig extends Animal implements Rideable{
$this->server = ServerAPI::request(); $this->server = ServerAPI::request();
$this->setName("Pig"); $this->setName("Pig");
$this->setSpeed(0.25); $this->setSpeed(0.25);
$this->ai->addTask(new TaskRandomWalk(0.25));
} }
/** /**
* @return boolean * @return boolean