mobs will randomly walk around(no jumping)

This commit is contained in:
GameHerobrine 2022-10-20 23:20:26 +03:00
parent 8d454e5ec9
commit a479a9ec80
11 changed files with 375 additions and 392 deletions

View file

@ -10,45 +10,6 @@ class EntityAPI{
$this->entities = []; $this->entities = [];
$this->server = ServerAPI::request(); $this->server = ServerAPI::request();
$this->hp = [
10 => 4,
11 => 10,
12 => 10,
13 => 8,
32 => 20,
33 => 20,
34 => 20,
35 => 16,
36 => 20,
];
$this->mob = [
"chicken" => 10,
"cow" => 11,
"pig" => 12,
"sheep" => 13,
"zombie" => 32,
"creeper" => 33,
"skeleton" => 34,
"spider" => 35,
"pigman" => 36,
];
$this->mobName = [
10 => "Chicken",
11 => "Cow",
12 => "Pig",
13 => "Sheep",
32 => "Zombie",
33 => "Creeper",
34 => "Skeleton",
35 => "Spider",
36 => "Pigman",
];
$this->serverSpawnAnimals = $this->server->api->getProperty("spawn-animals"); $this->serverSpawnAnimals = $this->server->api->getProperty("spawn-animals");
$this->serverSpawnMobs = $this->server->api->getProperty("spawn-mobs"); $this->serverSpawnMobs = $this->server->api->getProperty("spawn-mobs");
} }
@ -62,6 +23,18 @@ class EntityAPI{
public function commandHandler($cmd, $args, $issuer, $alias){ public function commandHandler($cmd, $args, $issuer, $alias){
$mob = [
"chicken" => 10,
"cow" => 11,
"pig" => 12,
"sheep" => 13,
"zombie" => 32,
"creeper" => 33,
"skeleton" => 34,
"spider" => 35,
"pigman" => 36
];
$output = ""; $output = "";
switch($cmd){ switch($cmd){
case 'summon': case 'summon':
@ -75,93 +48,54 @@ class EntityAPI{
break; break;
} }
$type = $this->mob[strtolower($args[0])]; if(is_int($args[0])) $type = $args[0];
else $type = $mob[strtolower($args[0])];
if($type != (10 or 11 or 12 or 13 or 32 or 33 or 34 or 35 or 36)){ if($type != (10 or 11 or 12 or 13 or 32 or 33 or 34 or 35 or 36)){
$output .= "Unknown mob."; $output .= "Unknown mob.";
break; break;
} }
$mobName = ucfirst(array_flip($mob)[$type]);
if(count($args) == 1){//summon <mob> if(strtolower($args[1] === "baby") or strtolower($args[2] === "baby") and $type > 13){
$output .= "$mobName cannot be a baby!";
$spawnX = round($issuer->entity->x, 1, PHP_ROUND_HALF_UP);
$spawnY = round($issuer->entity->y, 1, PHP_ROUND_HALF_UP);
$spawnZ = round($issuer->entity->z, 1, PHP_ROUND_HALF_UP);
$spawnLevel = $issuer->entity->level;
$entityit = $this->add($spawnLevel, ENTITY_MOB, $type, [
"x" => $spawnX,
"y" => $spawnY,
"z" => $spawnZ,
"Health" => $this->hp[$type],
]);
$this->spawnToAll($entityit, $spawnLevel);
$output .= $this->mobName[$type] . " spawned in " . $spawnX . ", " . $spawnY . ", " . $spawnZ . ".";
break; break;
}elseif(is_numeric($args[1])){//summon <mob> [amount] }
$x = round($issuer->entity->x, 2, PHP_ROUND_HALF_UP);
$y = round($issuer->entity->y, 2, PHP_ROUND_HALF_UP);
$z = round($issuer->entity->z, 2, PHP_ROUND_HALF_UP);
$level = $issuer->entity->level;
$pos = new Position($x, $y, $z, $level);
if(count($args) === 1){//summon <mob>
$this->summon($pos, ENTITY_MOB, $type);
$output .= "$mobName spawned in $x, $y, $z.";
break;
}
elseif(is_numeric($args[1])){//summon <mob> [amount]
$amount = (int) $args[1]; $amount = (int) $args[1];
if($amount > 25){ if($amount > 25){
$output .= "Cannot spawn > 25 mobs"; $output .= "Cannot spawn > 25 mobs";
break; break;
} }
$isBaby = 0; if(isset($args[2]) and strtolower($args[2]) === 'baby'){//summon <mob> [amount] [baby]
if(isset($args[2]) and strtolower($args[2]) == 'baby'){//summon <mob> [amount] [baby]
if($type > 13){
$output .= "This mob cant be baby!";
break;
}
$isBaby = 1; $isBaby = 1;
} }
$spawnX = round($issuer->entity->x, 1, PHP_ROUND_HALF_UP);
$spawnY = round($issuer->entity->y, 1, PHP_ROUND_HALF_UP);
$spawnZ = round($issuer->entity->z, 1, PHP_ROUND_HALF_UP);
$spawnLevel = $issuer->entity->level;
for($cnt = $amount; $cnt > 0; --$cnt){ for($cnt = $amount; $cnt > 0; --$cnt){
$entityit = $this->add($spawnLevel, ENTITY_MOB, $type, [ $this->summon($pos, ENTITY_MOB, $type, $isBaby === 1 ? ["IsBaby" => 1] : []);
"x" => $spawnX,
"y" => $spawnY,
"z" => $spawnZ,
"Health" => $this->hp[$type],
"IsBaby" => $isBaby,
]);
$this->spawnToAll($entityit, $spawnLevel);
} }
if($type == 13 or $amount == 1) $output .= "$amount".($isBaby === 1 ? " Baby" : "")." $mobName".(($type !== 13 || $amount > 1) ? "s" : "")." spawned in $x, $y, $z.";
$plural = '';
else $plural = 's';
if($isBaby == 1)
$baby = "Baby ";
else $baby = '';
$output .= $amount . " " . $baby . $this->mobName[$type] . $plural . " spawned in " . $spawnX . ", " . $spawnY . ", " . $spawnZ . ".";
break; break;
}elseif(strtolower($args[1]) == 'baby'){//summon <mob> [baby] }
if($type > 13){ elseif(strtolower($args[1]) == 'baby'){//summon <mob> [baby]
$output .= "This mob cant be baby!"; $this->summon($pos, ENTITY_MOB, $type, ["IsBaby" => 1]);
break; $output .= "Baby $mobName spawned in $x, $y, $z.";
}else{
$spawnX = round($issuer->entity->x, 1, PHP_ROUND_HALF_UP);
$spawnY = round($issuer->entity->y, 1, PHP_ROUND_HALF_UP);
$spawnZ = round($issuer->entity->z, 1, PHP_ROUND_HALF_UP);
$spawnLevel = $issuer->entity->level;
$entityit = $this->add($spawnLevel, ENTITY_MOB, $type, [
"x" => $spawnX,
"y" => $spawnY,
"z" => $spawnZ,
"Health" => $this->hp[$type],
"IsBaby" => 1,
]);
$this->spawnToAll($entityit, $spawnLevel);
$output .= "Baby " . $this->mobName[$type] . " spawned in " . $spawnX . ", " . $spawnY . ", " . $spawnZ . ".";
break; break;
} }
break; break;
}
case 'despawn': case 'despawn':
$cnt = 0; $cnt = 0;
$l = $this->server->query("SELECT EID FROM entities WHERE class = " . ENTITY_MOB . ";"); $l = $this->server->query("SELECT EID FROM entities WHERE class = " . ENTITY_MOB . ";");
@ -174,12 +108,21 @@ class EntityAPI{
} }
} }
} }
$output .= $cnt . " mobs have been despawned!"; $output .= "$cnt mobs have been despawned!";
break; break;
} }
return $output; return $output;
} }
public function summon(Position $pos, $class, $type, Array $data = []){
$entity = $this->add($pos->level, $class, $type, [
"x" => $pos->x,
"y" => $pos->y,
"z" => $pos->z
] + $data);
$this->spawnToAll($entity, $pos->level);
}
public function add(Level $level, $class, $type = 0, $data = []){ public function add(Level $level, $class, $type = 0, $data = []){
$eid = $this->eCnt++; $eid = $this->eCnt++;
$efl = EntityRegistry::$entityList->getEntityFromTypeAndClass($type, $class); $efl = EntityRegistry::$entityList->getEntityFromTypeAndClass($type, $class);

View file

@ -9,6 +9,8 @@ abstract class Creature extends Living{
parent::__construct($level, $eid, $class, $type, $data); parent::__construct($level, $eid, $class, $type, $data);
$this->setHealth(isset($this->data["Health"]) ? $this->data["Health"]:1, "generic"); $this->setHealth(isset($this->data["Health"]) ? $this->data["Health"]:1, "generic");
//$this->setName((isset($mobs[$this->type]) ? $mobs[$this->type]:$this->type)); //$this->setName((isset($mobs[$this->type]) ? $mobs[$this->type]:$this->type));
$this->ai->addTask(new TaskLookAround());
$this->ai->addTask(new TaskRandomWalk());
} }

View file

@ -42,7 +42,7 @@ class Entity extends Position{
public $fallStart; public $fallStart;
private $tickCounter; private $tickCounter;
private $speedMeasure = array(0, 0, 0, 0, 0, 0, 0); private $speedMeasure = array(0, 0, 0, 0, 0, 0, 0);
protected $server; public $server;
private $isStatic; private $isStatic;
public $level; public $level;
public $isInMinecart = false; public $isInMinecart = false;
@ -94,6 +94,7 @@ class Entity extends Position{
$this->height = 0.98; $this->height = 0.98;
$this->moveTime = 0; $this->moveTime = 0;
$this->lookTime = 0; $this->lookTime = 0;
$this->onGround = false;
switch($this->class){ switch($this->class){
case ENTITY_PLAYER: case ENTITY_PLAYER:
$this->player = $this->data["player"]; $this->player = $this->data["player"];
@ -153,7 +154,7 @@ class Entity extends Position{
} }
public function isMoving(){ public function isMoving(){
return $this->moveTime > 0 || ($this->speedX > 0.01 || $this->speedX < -0.01) || ($this->speedY > 0.01 || $this->speedY < -0.01) || ($this->speedZ > 0.01 || $this->speedZ < -0.01); return ($this->speedX > 0.01 || $this->speedX < -0.01) || ($this->speedY > 0.01 || $this->speedY < -0.01) || ($this->speedZ > 0.01 || $this->speedZ < -0.01);
} }
public function setVelocity($vX, $vY = 0, $vZ = 0){ public function setVelocity($vX, $vY = 0, $vZ = 0){
if($vX instanceof Vector3){ if($vX instanceof Vector3){
@ -396,6 +397,13 @@ class Entity extends Position{
return $hasUpdate; return $hasUpdate;
} }
protected function checkGroundState($movX, $movY, $movZ, $dx, $dy, $dz){
//$this->isCollidedVertically = $movY != $dy;
//$this->isCollidedHorizontally = ($movX != $dx or $movZ != $dz);
//$this->isCollided = ($this->isCollidedHorizontally or $this->isCollidedVertically);
$this->onGround = ($movY != $dy and $movY < 0);
}
public function update(){ public function update(){
if($this->closed === true){ if($this->closed === true){
return false; return false;
@ -459,19 +467,18 @@ class Entity extends Position{
} }
if($this->class !== ENTITY_PLAYER){ if($this->class !== ENTITY_PLAYER){
$update = false; $update = false;
if((($this->class !== ENTITY_OBJECT and $this->type !== OBJECT_PRIMEDTNT) or $support === false)){ if(true){
$drag = 0.2; $drag = 0.2;
if($this->speedX < 0.01 && $this->speedX > -0.01){ if(Utils::in_range($this->speedX, -0.01, 0.01)){
$this->speedX = 0; $this->speedX = 0;
} }
if($this->speedZ < 0.01 && $this->speedZ > -0.01){ if(Utils::in_range($this->speedZ, -0.01, 0.01)){
$this->speedZ = 0; $this->speedZ = 0;
} }
if($this->speedY < 0.001 && $this->speedY > -0.001){ if(Utils::in_range($this->speedY, -0.001, 0.001)){
$this->speedY = 0; $this->speedY = 0;
} }
if(($this->class === ENTITY_MOB || $this->class === ENTITY_ITEM) && ($this->speedX != 0 || $this->speedY != 0 || $this->speedZ != 0)){
if($this->class === ENTITY_MOB || $this->class === ENTITY_ITEM){
$blocks = $this->level->getCubes($this->boundingBox->getOffsetBoundingBox($this->speedX, $this->speedY, $this->speedZ)); $blocks = $this->level->getCubes($this->boundingBox->getOffsetBoundingBox($this->speedX, $this->speedY, $this->speedZ));
foreach($blocks as $b){ foreach($blocks as $b){
$this->speedX = $b->calculateXOffset($this->boundingBox, $this->speedX); $this->speedX = $b->calculateXOffset($this->boundingBox, $this->speedX);
@ -991,18 +998,6 @@ class Entity extends Position{
if($health < $this->health){ if($health < $this->health){
$harm = true; $harm = true;
$dmg = $this->health - $health; $dmg = $this->health - $health;
if(is_numeric($cause) && ($entity = $this->server->api->entity->get($cause)) != false){
$d = $entity->x - $this->x;
for($d1 = $entity->z - $this->z; $d * $d + $d1 * $d1 < 0.0001; $d1 = (Utils::randomFloat() - Utils::randomFloat()) * 0.01)
{
$d = (Utils::randomFloat() - Utils::randomFloat()) * 0.01;
}
//attackedAtYaw = (float)((Math.atan2($d1, $d) * 180D) / 3.1415927410125732D) - rotationYaw;
$this->knockBack($d, $d1);
$this->sendMotion();
}
if($this->class === ENTITY_PLAYER and ($this->player instanceof Player)){ if($this->class === ENTITY_PLAYER and ($this->player instanceof Player)){
$points = 0; $points = 0;
$values = array( $values = array(
@ -1092,6 +1087,18 @@ class Entity extends Position{
}elseif($this->health > 0){ }elseif($this->health > 0){
$this->dead = false; $this->dead = false;
} }
if(is_numeric($cause) && ($entity = $this->server->api->entity->get($cause)) != false){
$d = $entity->x - $this->x;
for($d1 = $entity->z - $this->z; $d * $d + $d1 * $d1 < 0.0001; $d1 = (Utils::randomFloat() - Utils::randomFloat()) * 0.01)
{
$d = (Utils::randomFloat() - Utils::randomFloat()) * 0.01;
}
//attackedAtYaw = (float)((Math.atan2($d1, $d) * 180D) / 3.1415927410125732D) - rotationYaw;
$this->knockBack($d, $d1);
$this->sendMotion();
}
return true; return true;
} }
return false; return false;

View file

@ -5,7 +5,7 @@ class TaskLookAround extends TaskBase
private $finYaw = 0; private $finYaw = 0;
public function shouldBeExecuted(EntityAI $ai) public function shouldBeExecuted(EntityAI $ai)
{ {
return !$ai->entity->isMoving() && $ai->entity->lookTime <= 0 && mt_rand(0,30) == 1 && $ai->entity->idleTime <= 0; return !$ai->entity->isMoving() && $ai->entity->lookTime <= 0 && mt_rand(0,30) == 1 && $ai->entity->idleTime <= 0 && !($ai->lastTask instanceof TaskLookAround);
} }
public function wasExecuted(EntityAI $ai){ public function wasExecuted(EntityAI $ai){

View file

@ -2,25 +2,57 @@
class TaskRandomWalk extends TaskBase class TaskRandomWalk extends TaskBase
{ {
private $x, $z, $delay = 10, $newPos, $looking = false;
public function shouldBeExecuted(EntityAI $ai) public function shouldBeExecuted(EntityAI $ai)
{ {
return !$ai->entity->pathFound && $ai->entity->moveTime <= 0 && $ai->entity->lookTime <= 0 && !($ai->entity->target instanceof Entity) && mt_rand(0,3) == 1; return !$ai->entity->pathFound && $ai->entity->moveTime <= 0 && $ai->entity->lookTime <= 0 && $ai->entity->idleTime <= 0 && !($ai->entity->target instanceof Entity) && mt_rand(0,3) == 1;
} }
public function onUpdate(EntityAI $ai)
{
console("random walk");
}
public function wasExecuted(EntityAI $ai) public function wasExecuted(EntityAI $ai)
{ {
return $ai->entity->moveTime > 0; return $ai->entity->moveTime > 0;
} }
public function onStart(EntityAI $ai) public function onStart(EntityAI $ai)
{ {
$ai->entity->moveTime = 100; $this->x = mt_rand(-7, 7);
$this->z = mt_rand(-7, 7);
$this->newPos = $ai->entity->add($this->x, 0, $this->z);
$ai->entity->moveTime = 15 * Utils::distance($ai->entity, $this->newPos);
} }
public function onUpdate(EntityAI $ai)
{
if($this->delay > 0 && !$this->looking){
--$this->delay;
try{
return;
}finally{
$this->testLook($ai, ($this->x > 0 ? 0.1 : ($this->x < 0 ? -0.1 : 0)), ($this->z > 0 ? 0.1 : ($this->z < 0 ? -0.1 : 0)));
$pk = new RotateHeadPacket();
$pk->eid = $ai->entity->eid;
$pk->yaw = $ai->entity->yaw;
$ai->entity->server->api->player->broadcastPacket($ai->entity->level->players, $pk);
$this->looking = true;
}
}
if($ai->entity->moveTime <= 1){
$ai->entity->idleTime = mt_rand(10,100);
$this->looking = false;
}
if(!$ai->entity->isMoving()){
$ai->entity->addVelocity(($this->x > 0 ? 0.1 : ($this->x < 0 ? -0.1 : 0)), 0, ($this->z > 0 ? 0.1 : ($this->z < 0 ? -0.1 : 0)));
}
}
public function updateLook($ai){
$ai->entity->server->query("UPDATE entities SET pitch = ".$ai->entity->pitch.", yaw = ".$ai->entity->yaw." WHERE EID = ".$ai->entity->eid.";");
}
public function testLook($ai, $vX, $vZ){
$x = $this->newPos->x - $ai->entity->x;
$y = $this->newPos->y - $ai->entity->y;
$z = $this->newPos->z - $ai->entity->z;
$ai->entity->yaw = -atan2($vX, $vZ) * 180 / M_PI;
$ai->entity->pitch = $y == 0 ? 0 : rad2deg(-atan2($y, sqrt(pow($x, 2) + pow($z, 2))));
$ai->entity->server->query("UPDATE entities SET pitch = ".$ai->entity->pitch.", yaw = ".$ai->entity->yaw." WHERE EID = ".$ai->entity->eid.";");
}
} }

View file

@ -4,8 +4,7 @@ class Chicken extends Animal{
const TYPE = MOB_CHICKEN; const TYPE = MOB_CHICKEN;
function __construct(Level $level, $eid, $class, $type = 0, $data = array()){ function __construct(Level $level, $eid, $class, $type = 0, $data = array()){
parent::__construct($level, $eid, $class, $type, $data); parent::__construct($level, $eid, $class, $type, $data);
$server = ServerAPI::request(); $this->server->schedule(mt_rand(0,6000) + 6000, array($this, "dropAnEgg"));
$server->schedule(mt_rand(0,6000) + 6000, array($this, "dropAnEgg"));
$this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 4, "generic"); $this->setHealth(isset($this->data["Health"]) ? $this->data["Health"] : 4, "generic");
$this->setName('Chicken'); $this->setName('Chicken');
$this->setSize($this->isBaby() ? 0.2 : 0.4, $this->isBaby() ? 0.35 : 0.7); $this->setSize($this->isBaby() ? 0.2 : 0.4, $this->isBaby() ? 0.35 : 0.7);

View file

@ -8,7 +8,6 @@ class Pig extends Animal{
$this->server = ServerAPI::request(); $this->server = ServerAPI::request();
$this->setSize($this->isBaby() ? 0.45 : 0.9, $this->isBaby() ? 0.45 : 0.9); $this->setSize($this->isBaby() ? 0.45 : 0.9, $this->isBaby() ? 0.45 : 0.9);
$this->setName('Pig'); $this->setName('Pig');
$this->ai->addTask(new TaskLookAround());
$this->update(); $this->update();
} }
public function isFood($id){ public function isFood($id){

View file

@ -6,24 +6,6 @@ class Arrow extends Projectile{
parent::__construct($level, $eid, $class, $type, $data); parent::__construct($level, $eid, $class, $type, $data);
//$this->server->schedule(1210, array($this, "update")); //Despawn //$this->server->schedule(1210, array($this, "update")); //Despawn
} }
public function update(){
$now = microtime(true);
if(($now - $this->spawntime) >= 60){
$this->close();
return false;
}
$f3 = 0.99;
$f5 = 0.03;
$this->move(new Vector3($this->speedX, $this->speedY, $this->speedZ));
$this->speedX *= $f3;
$this->speedZ *= $f3;
$this->speedY -= $f5;
$this->server->api->handle("entity.move", $this);
$this->server->api->handle("entity.motion", $this);
$this->handleUpdate();
$this->server->schedule(1, array($this, "update"), false);
$this->lastUpdate = $now;
}
public function handleUpdate(){ public function handleUpdate(){
$pk = new MoveEntityPacket_PosRot; $pk = new MoveEntityPacket_PosRot;
$pk->eid = $this->eid; $pk->eid = $this->eid;

View file

@ -16,6 +16,10 @@ class Vector3{
$this->z = $z; $this->z = $z;
} }
public function toArray(){
return ["x" => $this->x, "y" => $this->y, "z" => $this->z];
}
public function getFloorX(){ public function getFloorX(){
return (int) $this->x; return (int) $this->x;
} }

View file

@ -535,12 +535,23 @@ class Utils{
public static function round($number){ public static function round($number){
return round($number, 0, PHP_ROUND_HALF_DOWN); return round($number, 0, PHP_ROUND_HALF_DOWN);
} }
public static function distance($pos1, $pos2){ public static function distance($pos1, $pos2){
if($pos1 instanceof Vector3){
$pos1 = $pos1->toArray();
}
if($pos2 instanceof Vector3){
$pos2 = $pos2->toArray();
}
return sqrt(pow($pos1["x"] - $pos2["x"], 2) + pow($pos1["y"] - $pos2["y"], 2) + pow($pos1["z"] - $pos2["z"], 2)); return sqrt(pow($pos1["x"] - $pos2["x"], 2) + pow($pos1["y"] - $pos2["y"], 2) + pow($pos1["z"] - $pos2["z"], 2));
} }
public static function angle3D($pos1, $pos2){ public static function angle3D($pos1, $pos2){
if($pos1 instanceof Vector3){
$pos1 = $pos1->toArray();
}
if($pos2 instanceof Vector3){
$pos2 = $pos2->toArray();
}
$X = $pos1["x"] - $pos2["x"]; $X = $pos1["x"] - $pos2["x"];
$Z = $pos1["z"] - $pos2["z"]; $Z = $pos1["z"] - $pos2["z"];
$dXZ = sqrt(pow($X, 2) + pow($Z, 2)); $dXZ = sqrt(pow($X, 2) + pow($Z, 2));

View file

@ -32,7 +32,10 @@ class Level{
public function close(){ public function close(){
$this->__destruct(); $this->__destruct();
} }
/**
* @param AxisAlignedBB $aABB
* @return AxisAlignedBB[]
*/
public function getCubes(AxisAlignedBB $aABB) { public function getCubes(AxisAlignedBB $aABB) {
$aABBs = []; $aABBs = [];
$x0 = floor($aABB->minX); $x0 = floor($aABB->minX);
@ -61,6 +64,7 @@ class Level{
return $aABBs; return $aABBs;
} }
public function __destruct(){ public function __destruct(){
if(isset($this->level)){ if(isset($this->level)){
$this->save(false, false); $this->save(false, false);