new ai task, fixes of old ones, php8 fixes

This commit is contained in:
GameHerobrine 2022-12-24 22:42:23 +03:00
parent a907a7cf5e
commit 80bdddb817
13 changed files with 1581 additions and 1458 deletions

View file

@ -72,8 +72,8 @@ class EntityAPI{
}
elseif(is_numeric($args[1])){//summon <mob> [amount]
$amount = (int) $args[1];
if($amount > 25){
$output .= "Cannot spawn > 25 mobs";
if($amount > 100){
$output .= "Cannot spawn > 100 mobs";
break;
}
$isBaby = false;

View file

@ -92,7 +92,7 @@ class TimeAPI{
case "day":
case "sunset":
case "night":
if(substr($args[1], 0, 2) === "w:"){
if(count($args) > 1 && substr($args[1], 0, 2) === "w:"){
$levelName = preg_replace("/w:/", "", $args[1]);
$level = $this->server->api->level->get($levelName);
if($level instanceof Level){

View file

@ -14,6 +14,7 @@ abstract class Animal extends Creature implements Ageable, Breedable{
if(isset($this->data["IsBaby"]) && $this->data["IsBaby"] && $this->getAge() >= 0){
$this->setAge(-24000);
}
$this->ai->addTask(new TaskTempt());
}
public function isBaby(){

File diff suppressed because it is too large Load diff

View file

@ -16,7 +16,6 @@ abstract class Living extends Entity implements Damageable{
}
public function update(){
if($this->idleTime <= 0) {
$this->ai->updateTasks();
}

View file

@ -38,9 +38,13 @@ class EntityAI
* @return TaskBase | false
*/
public function getTask($id){
return $this->tasks[$id] ?: false; //i never saw this operator before
return isset($this->tasks[$id]) ? $this->tasks[$id] : false;
}
public function isStarted($id){
$task = @$this->getTask($id);
return $task instanceof TaskBase && $task->isStarted;
}
public function updateTasks(){
foreach($this->tasks as $t){

View file

@ -6,7 +6,7 @@ class TaskLookAround extends TaskBase
public function onStart(EntityAI $ai)
{
$this->selfCounter = 1;
$this->rotation = mt_rand(-90, 90);
$this->rotation = mt_rand(-120, 120);
}
public function onEnd(EntityAI $ai)
@ -16,7 +16,7 @@ class TaskLookAround extends TaskBase
public function canBeExecuted(EntityAI $ai)
{
return !$ai->entity->isMoving() && Utils::randomFloat() < 0.04;
return !$ai->entity->isMoving() && Utils::randomFloat() < 0.02 && !$ai->isStarted("TaskLookAtPlayer") && !$ai->isStarted("TaskTempt"); /*Vanilla value*/
}
public function onUpdate(EntityAI $ai)

View file

@ -6,16 +6,32 @@ class TaskLookAtPlayer extends TaskBase{
public $target = false;
private $state, $yaw, $pitch;
public function canBeExecuted(EntityAI $ai){
return !$ai->entity->isMoving() && !@$ai->getTask("TaskLookAround")->isStarted && mt_rand(0, 20) == 0;
return Utils::randomFloat() < 0.02 && !$ai->entity->isMoving() && !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskTempt");
}
protected function findTarget($e, $r){
$ents = $e->server->api->entity->getRadius($e, $r, ENTITY_PLAYER); //TODO sort by nearest and select nearest
return count($ents) <= 0 ? false : $ents[array_rand($ents)];
$svd = null;
$svdDist = -1;
foreach($e->server->api->entity->getRadius($e, $r, ENTITY_PLAYER) as $p){
if($svdDist === -1){
$svdDist = Utils::manh_distance($e, $p);
$svd = $p;
continue;
}
if($svd != null && $svdDist === 0){
$svd = $p;
}
if(($cd = Utils::manh_distance($e, $p)) < $svdDist){
$svdDist = $cd;
$svd = $p;
}
}
return $svd;
}
public function onStart(EntityAI $ai){
$this->target = $this->findTarget($ai->entity, 5);
$this->target = $this->findTarget($ai->entity, 6); //TODO max distance for different mobs
if(!($this->target instanceof Entity) || !$this->target->isPlayer()){
$this->reset();
return;
@ -26,7 +42,7 @@ class TaskLookAtPlayer extends TaskBase{
}
public function onUpdate(EntityAI $ai){
if(!($this->target instanceof Entity)){
if(!($this->target instanceof Entity) || Utils::distance($ai->entity, $this->target) > 6){ //TODO max distance for different mobs
$this->reset();
return;
}

View file

@ -17,7 +17,6 @@ class TaskRandomWalk extends TaskBase
public function onEnd(EntityAI $ai)
{
$this->x = $this->z = 0;
$ai->entity->idleTime = mt_rand(20, 120);
}
public function onUpdate(EntityAI $ai)
@ -34,7 +33,7 @@ class TaskRandomWalk extends TaskBase
if($ai->entity instanceof Creeper && $ai->entity->isIgnited()) {
return false;
}
return !@$ai->getTask("TaskLookAround")->isStarted && !@$ai->getTask("TaskLookAtPlayer")->isStarted && mt_rand(0, 40) == 0;
return !$ai->isStarted("TaskLookAround") && !$ai->isStarted("TaskLookAtPlayer") && mt_rand(0, 120) == 0;
}

View file

@ -0,0 +1,72 @@
<?php
class TaskTempt extends TaskBase
{
public $target = false;
public function onStart(EntityAI $ai)
{
$this->selfCounter = 1;
}
public function onEnd(EntityAI $ai)
{
unset($this->target);
$ai->entity->pitch = 0;
}
public function onUpdate(EntityAI $ai)
{
if($this->target instanceof Entity && $this->target->distanceSquared($ai->entity) > 100 || !$ai->entity->isFood($this->target->player->getHeldItem()->getID())){
$this->reset();
return;
}
//TODO following
$ai->mobController->lookOn($this->target);
$pk = new RotateHeadPacket(); //TODO headYaw auto update
$pk->eid = $ai->entity->eid;
$pk->yaw = $ai->entity->yaw;
$ai->entity->server->api->player->broadcastPacket($ai->entity->level->players, $pk);
}
public function canBeExecuted(EntityAI $ai)
{
if(!($ai->entity instanceof Breedable)){
return false;
}
$target = $this->findTarget($ai->entity, 10);
if($target instanceof Entity && $target->class === ENTITY_PLAYER && $target->isPlayer() && $ai->entity->isFood($target->player->getHeldItem()->getID())){
$this->target = $target;
return true;
}
return false;
}
protected function findTarget($e, $r){
$svd = null;
$svdDist = -1;
foreach($e->server->api->entity->getRadius($e, $r * 3, ENTITY_PLAYER) as $p){
if($svdDist === -1){
$svdDist = Utils::manh_distance($e, $p);
$svd = $p;
continue;
}
if($svd != null && $svdDist === 0){
$svd = $p;
}
if(($cd = Utils::manh_distance($e, $p)) < $svdDist){
$svdDist = $cd;
$svd = $p;
}
}
if($svd == null || Utils::distance_noroot($e, $svd) > ($r * $r)){
return null;
}
return $svd;
}
}

View file

@ -436,8 +436,15 @@ class PMFLevel extends PMF{
$aZ = $z - ($Z << 4);
$aY = $y - ($Y << 4);
#Need to fix. But idk how.
$b = ord($this->chunks[$index][$Y][(int) ($aY + ($aX << 5) + ($aZ << 9))]);
$m = ord($this->chunks[$index][$Y][(int) (($aY >> 1) + 16 + ($aX << 5) + ($aZ << 9))]);
if(is_array($this->chunks) && is_array($this->chunks[$index]) && is_string($this->chunks[$index][$Y])){ //PHP8 warn fix
$b = ord($this->chunks[$index][$Y][(int) ($aY + ($aX << 5) + ($aZ << 9))]);
$m = ord($this->chunks[$index][$Y][(int) (($aY >> 1) + 16 + ($aX << 5) + ($aZ << 9))]);
}else{
console(get_class($this->chunks[$index][$Y]));
$b = 0;
$m = 0;
}
if(($y & 1) === 0){
$m = $m & 0x0F;
}else{

View file

@ -56,6 +56,7 @@ class Utils{
public static function hasEmoji($s){
return preg_match(Utils::emojiRegex, $s);
}
public static function getCallableIdentifier(callable $variable){
if(is_array($variable)){
return sha1(strtolower(get_class($variable[0])) . "::" . strtolower($variable[1]));
@ -551,6 +552,26 @@ class Utils{
public static function round($number){
return round($number, 0, PHP_ROUND_HALF_DOWN);
}
/**
* manhattan distance, less accurate, but still useful sometimes
*/
public static function manh_distance(Vector3 $pos1, Vector3 $pos2){
return abs($pos2->x - $pos1->x) + abs($pos2->y - $pos1->y) + abs($pos2->z - $pos1->z);
}
/**
* Euclidian distance, but without square roots
*/
public static function distance_noroot(Vector3 $pos1, Vector3 $pos2){
return pow($pos2->x - $pos1->x, 2) + pow($pos2->z - $pos1->z, 2) + pow($pos2->z - $pos1->z, 2);
}
/**
*
* @param $pos1
* @param $pos2
* @return number
*/
public static function distance($pos1, $pos2){
if($pos1 instanceof Vector3){
$pos1 = $pos1->toArray();

View file

@ -20,6 +20,7 @@ class MobSpawner{
}
public function handle(){
return false;
if($this->countEntities() > self::MOB_LIMIT || count($this->level->players) <= 0){
return false; //not spawning
}