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(){
return $this->speedModifer * ($this->inPanic ? 1.5 : 1.0);
return $this->speedModifer;
}
public function getArmorValue(){
return 2;

View file

@ -91,7 +91,7 @@ class Entity extends Position
$this->canBeAttacked = false;
$this->hasKnockback = false;
$this->level = $level;
$this->speedModifer = 0.7;
$this->speedModifer = 1;
$this->fallY = false;
$this->fallStart = false;
$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 $jumpMovementFactor = 0.02; //TODO may not be constant
public $aiMoveSpeed;
public function __construct(Level $level, $eid, $class, $type = 0, $data = array()){
$this->target = false;
@ -28,6 +30,17 @@ abstract class Living extends Entity implements Pathfindable{
$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(){
return $this->path != null;
}
@ -100,10 +113,10 @@ abstract class Living extends Entity implements Pathfindable{
if(!$this->dead && Entity::$allowedAI && $this->idleTime <= 0) {
$this->ai->updateTasks();
}
$this->ai->mobController->rotateTick();
$this->ai->mobController->movementTick();
$this->ai->mobController->rotateTick();
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->speedY) < self::MIN_POSSIBLE_SPEED) $this->speedY = 0;

View file

@ -11,7 +11,7 @@ class MobController
FIRE => true,
];
/**
* @var Entity
* @var Living
*/
public $entity;
@ -20,6 +20,10 @@ class MobController
protected $jumping;
protected $jumpTimeout;
public $moveToX, $moveToY, $moveToZ;
public $speed;
public $updateMove = false;
public function __construct($e){
$this->entity = $e;
}
@ -38,89 +42,66 @@ class MobController
return $this->finalHeadYaw === $this->entity->headYaw;
}
public function moveNonInstant($x, $y, $z, $camera = true){
if($x == 0 && $y == 0 && $z == 0){
return false;
}
$ox = $x <=> 1;
$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 setMovingOffset($x, $y, $z, $speed){
$this->moveToX = $this->entity->x + ($x);
$this->moveToY = $this->entity->y + ($y);
$this->moveToZ = $this->entity->z + ($z);
$this->speed = $speed;
$this->updateMove = true;
}
public function canJump(){
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(){
if($this->canJump()){
$this->jumpTimeout = 10;
$this->entity->speedY = 0.50;
$this->entity->moveForward = 0;
if($this->updateMove){
$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
$this->entity->lastHeadYaw = $this->entity->headYaw;
$w180 = Utils::wrapAngleTo180($this->finalHeadYaw - $this->entity->headYaw);
$w180min = min(abs($w180), 20)*Utils::getSign($w180);
$this->entity->headYaw = Utils::wrapAngleTo360($this->entity->headYaw + $w180min);
//$this->entity->lastHeadYaw = $this->entity->headYaw;
//$w180 = Utils::wrapAngleTo180($this->finalHeadYaw - $this->entity->headYaw);
//$w180min = min(abs($w180), 20)*Utils::getSign($w180);
//$this->entity->headYaw = Utils::wrapAngleTo360($this->entity->headYaw + $w180min);
}
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){

View file

@ -16,7 +16,7 @@ class TaskAttackPlayer extends TaskTempt
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);
$mult = $ai->entity instanceof Spider ? 1 : 2;
$radius = ($ai->entity->width * $mult)*($ai->entity->width * $mult);

View file

@ -21,7 +21,7 @@ class TaskDestroyServerPerformance extends TaskTempt
return;
}
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{
}

View file

@ -24,7 +24,7 @@ class TaskMate extends \TaskBase
$this->onEnd($ai);
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);
--$this->selfCounter;

View file

@ -28,7 +28,7 @@ class TaskPanic extends TaskBase
{
--$this->selfCounter;
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)

View file

@ -3,10 +3,15 @@
class TaskRandomWalk extends TaskBase
{
private $x, $z;
public $moveSpeed;
public function __construct($moveSpeed){
$this->moveSpeed = 0.25;
}
public function onStart(EntityAI $ai)
{
$this->x = mt_rand(-16, 16);
$this->z = mt_rand(-16, 16);
$this->x = mt_rand(-10, 10);
$this->z = mt_rand(-10, 10);
if($this->x === 0 && $this->z === 0){
$this->reset();
return false;
@ -26,15 +31,17 @@ class TaskRandomWalk extends TaskBase
return false; //TODO Better way: block movement
}
--$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)
{
if(($ai->entity instanceof Creeper && $ai->entity->isIgnited()) || $ai->entity->hasPath() || $ai->isStarted("TaskTempt") ||$ai->isStarted("TaskAttackPlayer")) {
return false;
} // 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;
return true;
//if(($ai->entity instanceof Creeper && $ai->entity->isIgnited()) || $ai->entity->hasPath() || $ai->isStarted("TaskTempt") ||$ai->isStarted("TaskAttackPlayer")) {
// return false;
//} // 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;
}
$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);
}

View file

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