added entity pushing(disabled by default)

This commit is contained in:
GameHerobrine 2024-01-07 22:49:46 +03:00
parent 5a059eab71
commit 3c89749125
6 changed files with 73 additions and 16 deletions

View file

@ -162,6 +162,9 @@ class EntityAPI{
}else{
$this->entities[$eid] = new Entity($level, $eid, $class, $type, $data);
}
$cX = (int)$this->entities[$eid]->x >> 4;
$cZ = (int)$this->entities[$eid]->z >> 4;
$level->entityListPositioned["$cX $cZ"][$eid] = $eid;
$level->entityList[$eid] = &$this->entities[$eid];
$this->server->handle("entity.add", $this->entities[$eid]);
return $this->entities[$eid];
@ -182,6 +185,12 @@ class EntityAPI{
public function remove($eid){
if(isset($this->entities[$eid])){
$this->entities[$eid]->closed = true;
if($this->entities[$eid]->level instanceof Level){
$cX = (int)$this->entities[$eid]->x >> 4;
$cZ = (int)$this->entities[$eid]->z >> 4;
$index = "$cX $cZ";
unset($this->entities[$eid]->level->entityListPositioned[$index][$eid]);
}
if($this->entities[$eid]->isPlayer()){
$pk = new RemovePlayerPacket;
$pk->eid = $eid;

View file

@ -110,8 +110,9 @@ class Player{
$this->blockUpdateQueue = new RakNetPacket(RakNetInfo::DATA_PACKET_0);
$this->blockUpdateQueue->data = [];
$this->server->schedule(1, [$this, "handlePacketQueues"], [], true);
$this->server->schedule(20 * 60, [$this, "clearQueue"], [], true);
$this->server->schedule(1, [$this, "handlePacketQueues"], [], true);
$this->evid[] = $this->server->event("server.close", [$this, "close"]);
console("[DEBUG] New Session started with " . $ip . ":" . $port . ". MTU " . $this->MTU . ", Client ID " . $this->clientID, true, true, 2);
}
@ -1365,6 +1366,7 @@ class Player{
}
public function entityTick(){
if($this->isSleeping) ++$this->sleepingTime;
$this->handlePacketQueues();
}
public function handleDataPacket(RakNetDataPacket $packet){
if($this->connected === false){
@ -1567,7 +1569,7 @@ class Player{
console("[INFO] " . FORMAT_AQUA . $this->username . FORMAT_RESET . "[/" . $this->ip . ":" . $this->port . "] logged in with entity id " . $this->eid . " at (" . $this->entity->level->getName() . ", " . round($this->entity->x, 2) . ", " . round($this->entity->y, 2) . ", " . round($this->entity->z, 2) . ")");
break;
return false; //return false so it will not be updated by tickerFunction
case ProtocolInfo::READY_PACKET:
if($this->loggedIn === false){
break;

View file

@ -89,10 +89,12 @@ class PocketMinecraftServer{
"16x16x16_chunk_sending" => false,
"experimental-mob-ai" => false,
"force-20-tps" => false,
"enable-mob-pushing" => Living::$entityPushing
]);
Player::$smallChunks = $this->extraprops->get("16x16x16_chunk_sending");
Living::$despawnMobs = $this->extraprops->get("despawn-mobs");
Living::$despawnTimer = $this->extraprops->get("mob-despawn-ticks");
Living::$entityPushing = $this->extraprops->get("enable-mob-pushing");
self::$FORCE_20_TPS = $this->extraprops->get("force-20-tps");
PocketMinecraftServer::$SAVE_PLAYER_DATA = $this->extraprops->get("save-player-data");
MobController::$ADVANCED = $this->extraprops->get("experimental-mob-ai");

View file

@ -1117,14 +1117,7 @@ class Entity extends Position
public function setPosition(Vector3 $pos, $yaw = false, $pitch = false)
{
if($pos instanceof Position and $pos->level instanceof Level and $this->level !== $pos->level){
$this->level = $pos->level;
$this->server->preparedSQL->entity->setLevel->reset();
$this->server->preparedSQL->entity->setLevel->clear();
$this->server->preparedSQL->entity->setLevel->bindValue(":level", $this->level->getName(), SQLITE3_TEXT);
$this->server->preparedSQL->entity->setLevel->bindValue(":eid", $this->eid, SQLITE3_INTEGER);
$this->server->preparedSQL->entity->setLevel->execute();
}
$this->x = $pos->x;
$this->y = $pos->y;
$this->z = $pos->z;

View file

@ -2,7 +2,7 @@
abstract class Living extends Entity implements Damageable, Pathfindable{
public static $despawnMobs, $despawnTimer, $entityPushing;
public static $despawnMobs, $despawnTimer, $entityPushing = false;
public $target, $ai;
public $pathFinder, $path = null, $currentIndex = 0, $currentNode, $pathFollower;
@ -42,7 +42,23 @@ abstract class Living extends Entity implements Damageable, Pathfindable{
return true;
}
public function collideHandler(){
$rd = ceil($this->radius);
$minChunkX = ((int)($this->x - $rd)) >> 4;
$minChunkZ = ((int)($this->z - $rd)) >> 4;
$maxChunkX = ((int)($this->x + $rd)) >> 4;
$maxChunkZ = ((int)($this->z + $rd)) >> 4;
//TODO also index by chunkY?
for($chunkX = $minChunkX; $chunkX <= $maxChunkX; ++$chunkX){
for($chunkZ = $minChunkZ; $chunkZ <= $maxChunkZ; ++$chunkZ){
$ind = "$chunkX $chunkZ";
foreach($this->level->entityListPositioned[$ind] ?? [] as $entid){
if($this->level->entityList[$entid] instanceof Entity){
$this->applyCollision($this->level->entityList[$entid]);
}
}
}
}
}
public function applyCollision(Entity $collided){
@ -83,9 +99,9 @@ abstract class Living extends Entity implements Damageable, Pathfindable{
$this->ai->mobController->rotateTick();
$this->ai->mobController->movementTick();
/*if(self::$entityPushing){
$this->collideHandler();
}*/
if(self::$entityPushing){
$this->collideHandler();
}
if($this->onGround){
//if(!$this->hasPath() && $this->pathFinder instanceof ITileNavigator){
// $this->path = $this->pathFinder->navigate(new PathTileXYZ($this->x, $this->y, $this->z, $this->level), new PathTileXYZ($this->x + mt_rand(-10, 10), $this->y + mt_rand(-1, 1), $this->z + mt_rand(-10, 10), $this->level), 10);

View file

@ -11,6 +11,7 @@ class Level{
*/
public $entityList;
public $entityListPositioned = [];
public $entitiesInLove = [];
public $tiles, $blockUpdates, $nextSave, $players = [], $level, $mobSpawner, $totalMobsAmount = 0;
@ -321,7 +322,6 @@ class Level{
}
public function onTick(PocketMinecraftServer $server, $currentTime){
//$ents = $server->api->entity->getAll($this);
if(!$this->stopTime) ++$this->time;
for($cX = 0; $cX < 16; ++$cX){
for($cZ = 0; $cZ < 16; ++$cZ){
@ -343,11 +343,16 @@ class Level{
$this->totalMobsAmount = 0;
$post = [];
foreach($this->entityList as $k => $e){
if(!($e instanceof Entity)){
unset($this->entityList[$k]);
unset($this->server->entities[$k]);
//TODO try to remove from $entityListPositioned?
continue;
}
$curChunkX = (int)$e->x >> 4;
$curChunkZ = (int)$e->z >> 4;
if($e->class === ENTITY_MOB && !$e->isPlayer()){
++$this->totalMobsAmount;
}
@ -355,6 +360,17 @@ class Level{
$e->update($currentTime);
if(!$e->isPlayer()) $post[] = $k;
}
if($e instanceof Entity){
$newChunkX = (int)$e->x >> 4;
$newChunkZ = (int)$e->z >> 4;
if($curChunkX != $newChunkX || $curChunkZ != $newChunkZ){
$index = "$curChunkX $curChunkZ"; //while creating index like $curChunkX << 32 | $curChunkZ is faster, placing it inside list is slow
$newIndex = "$newChunkX $newChunkZ";
unset($this->entityListPositioned[$index][$e->eid]);
$this->entityListPositioned[$newIndex][$e->eid] = $e->eid; //set to e->eid to avoid possible memory leaks
}
}
}
$this->checkSleep();
@ -387,6 +403,25 @@ class Level{
}
}
public function getEntitiesNearby(Entity $e, $radius = 5){
$minChunkX = ((int)($e->x - $radius)) >> 4;
$minChunkZ = ((int)($e->z - $radius)) >> 4;
$maxChunkX = ((int)($e->x + $radius)) >> 4;
$maxChunkZ = ((int)($e->z + $radius)) >> 4;
$ents = [];
//TODO also index by chunkY?
for($chunkX = $minChunkX; $chunkX <= $maxChunkX; ++$chunkX){
for($chunkZ = $minChunkZ; $chunkZ <= $maxChunkZ; ++$chunkZ){
$ind = "$chunkX $chunkZ";
foreach($this->entityListPositioned[$ind] ?? [] as $entid){
$ents[$entid] = $this->entityList[$entid];
}
}
}
unset($ents[$e->eid]);
return $ents;
}
public function addBlockToSendQueue($x, $y, $z, $id, $meta){
$this->queuedBlockUpdates["$x $y $z"] = [$x, $y, $z, $id, $meta];
}