Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Is Smooth AI Possible

Comments in 'Plugin Development' started by Primus, Oct 30, 2015.

  1. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    Tryed to make AI and it worked, but only one thing that movements are 'jerky'

    First tried update movement with packets
    PHP:
    $this->lastX $this->x;
    $this->lastY $this->y;
    $this->lastZ $this->z;

    $this->lastYaw $this->yaw;
    $this->lastPitch $this->pitch;

    $pk = new MovePlayerPacket();
    $pk->eid $this->id;
    $pk->$this->x;
    $pk->$this->$this->height 2// Else it jumps (teleports) up and falls down looks really ugly
    $pk->$this->z;
    $pk->yaw $this->yaw;
    $pk->pitch $this->pitch;
    $pk->bodyYaw $this->yaw;
    Then setMotion()
    PHP:
    $this->setMotion($this->x$this->y$this->z);
    And this bad BAD BAD (testing) code :D
    PHP:
    if(mt_rand(010) === mt_rand(010)){
    $this->setMotion($this->+= rand(-11) * self::$speed,
    $this->+= rand(-11) * self::$speed,
    $this->y);
    }
    Should I give up the idea of a smooth AI?
  2. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    And this is exactly why I hate making AI :D
    iksaku and Creeperface like this.
  3. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    So no? :D
  4. CrazedMiner
    Offline

    CrazedMiner Notable Member Plugin Developer

    Joined:
    Jan 31, 2015
    Posts:
    348
    Plugins:
    2
    Minecraft User:
    CrazedMinerYT
    You could try looking through how PC server software does it and port it to PHP :p
  5. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    I tried but every entity file i found is just an interface :(
  6. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
  7. hmmmm
    Offline

    hmmmm New Member Plugin Developer

    Joined:
    Jul 13, 2014
    Posts:
    31
    Plugins:
    6
    Minecraft User:
    Sky___Blue
    ifteam makes a.i system (。+・`ω・´)b,
    entitymanager is very smooth,
    and entitymanager is support api 1.13.0 + api 2.0.0
  8. iDirtPlayzMC
    Offline

    iDirtPlayzMC Active Member

    Joined:
    Apr 10, 2015
    Posts:
    264
    Minecraft User:
    iZenith_xPr
    PHP:
    Skip to content
    This repository 
    Search
    Pull requests
    Issues
    Gist
    @iDirtniverse
    You don’t have any verified emails. We recommend verifying at least one email.
    Email verification helps our support team verify ownership if you lose account access and allows you to receive all the notifications you ask for.
    Watch 5
      Star 20
      Fork 14 alejandroliu/bad-plugins
    Branch: master  bad-plugins/Mobsters/src/aliuly/mobsters/idiots/Zombie.php
    3bba524  on Jul 17
    @alejandroliu alejandroliu Mobsters-0.5.0
    1 contributor
    RawBlameHistory     273 lines (241 sloc)  7.48 KB
    <?php
    /*
    *
    *  ____            _        _   __  __ _                  __  __ ____
    * |  _ \ ___   ___| | _____| |_|  \/  (_)_ __   ___      |  \/  |  _ \
    * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
    * |  __/ (_) | (__|   <  __/ |_| |  | | | | | |  __/_____| |  | |  __/
    * |_|   \___/ \___|_|\_\___|\__|_|  |_|_|_| |_|\___|     |_|  |_|_|
    *
    * This program is free software: you can redistribute it and/or modify
    * it under the terms of the GNU Lesser General Public License as published by
    * the Free Software Foundation, either version 3 of the License, or
    * (at your option) any later version.
    *
    * @author PocketMine Team
    * @link http://www.pocketmine.net/
    *
    *
    */
    namespace aliuly\mobsters\idiots;
    use 
    pocketmine\event\entity\EntityDamageByEntityEvent;
    use 
    pocketmine\event\entity\EntityDamageEvent;
    use 
    pocketmine\item\Item as Item;
    use 
    pocketmine\network\protocol\AddEntityPacket;
    use 
    pocketmine\network\Network;
    use 
    pocketmine\network\protocol\MovePlayerPacket;
    use 
    pocketmine\network\protocol\MoveEntityPacket;
    use 
    pocketmine\math\AxisAlignedBB;
    use 
    pocketmine\Player;
    use 
    pocketmine\entity\Monster;
    use 
    pocketmine\entity\Entity;
    use 
    pocketmine\math\Vector3;
    class 
    Zombie extends Monster{
        const 
    NETWORK_ID 32;
        public static 
    $range 32;
        public static 
    $speed 0.2;
        public static 
    $jump 2.5;
        public static 
    $attack 1.5;
        public 
    $width 0.6;
        public 
    $length 0.6;
        public 
    $height 1.8;
        public 
    $stepHeight 0.5;
        public 
    $knockback 0;
        public function 
    getName(){
            return 
    "ZombieIdiot";
        }
        public function 
    spawnTo(Player $player){
            
    $pk = new AddEntityPacket();
            
    $pk->eid $this->getId();
            
    $pk->type self::NETWORK_ID;
            
    $pk->$this->x;
            
    $pk->$this->y;
            
    $pk->$this->z;
            
    $pk->speedX $this->motionX;
            
    $pk->speedY $this->motionY;
            
    $pk->speedZ $this->motionZ;
            
    $pk->yaw $this->yaw;
            
    $pk->pitch $this->pitch;
            
    $pk->metadata $this->dataProperties;
            
    $player->dataPacket($pk->setChannel(Network::CHANNEL_ENTITY_SPAWNING));
            
    parent::spawnTo($player);
        }
        
    /*
        public function getData(){ //TODO
            $flags = 0;
            $flags |= $this->fireTicks > 0 ? 1 : 0;
            //$flags |= ($this->crouched === true ? 0b10:0) << 1;
            //$flags |= ($this->inAction === true ? 0b10000:0);
            $d = [
                0 => ["type" => 0, "value" => $flags],
                1 => ["type" => 1, "value" => $this->airTicks],
                16 => ["type" => 0, "value" => 0],
                17 => ["type" => 6, "value" => [0, 0, 0]],
            ];
            return $d;
        }
    */
        
    public function getDrops(){
            
    $drops = [];
            
    $rnd mt_rand(0,1);
            if (
    $rnd) {
                
    $drops[] = Item::get(Item::FEATHER0$rnd);
            }
            if(
    $this->lastDamageCause instanceof EntityDamageByEntityEvent and $this->lastDamageCause->getEntity() instanceof Player){
                if(
    mt_rand(0199) < 5){
                    switch(
    mt_rand(02)){
                        case 
    0:
                            
    $drops[] = Item::get(Item::IRON_INGOT01);
                            break;
                        case 
    1:
                            
    $drops[] = Item::get(Item::CARROT01);
                            break;
                        case 
    2:
                            
    $drops[] = Item::get(Item::POTATO01);
                            break;
                    }
                }
            }
            return 
    $drops;
        }
        public function 
    zupdateMovement(){
            if(
    $this->!== $this->lastX or $this->!== $this->lastY or $this->!== $this->lastZ or $this->yaw !== $this->lastYaw or $this->pitch !== $this->lastPitch){
                
    $this->lastX $this->x;
                
    $this->lastY $this->y;
                
    $this->lastZ $this->z;
                
    $this->lastYaw $this->yaw;
                
    $this->lastPitch $this->pitch;
                
    $pk = new MovePlayerPacket();
                
    $pk->eid $this->id;
                
    $pk->$this->x;
                
    $pk->$this->y;
                
    $pk->$this->z;
                
    $pk->yaw $this->yaw;
                
    $pk->pitch $this->pitch;
                
    $pk->bodyYaw $this->yaw;
                foreach(
    $this->hasSpawned as $player){
                    
    $player->dataPacket($pk);
                }
            }
            if((
    $this->lastMotionX != $this->motionX or $this->lastMotionY != $this->motionY or $this->lastMotionZ != $this->motionZ)){
                
    $this->lastMotionX $this->motionX;
                
    $this->lastMotionY $this->motionY;
                
    $this->lastMotionZ $this->motionZ;
                foreach(
    $this->hasSpawned as $player){
                    
    $player->addEntityMotion($this->id$this->motionX$this->motionY$this->motionZ);
                }
            }
        }
        public function 
    getBoundingBox(){
            
    $this->boundingBox = new AxisAlignedBB(
                
    $x $this->$this->width 2,
                
    $y $this->$this->height $this->stepHeight,
                
    $z $this->$this->length 2,
                
    $x $this->width,
                
    $y $this->height $this->stepHeight,
                
    $z $this->length
            
    );
            return 
    $this->boundingBox;
        }
        private function 
    findTarget(){
            
    $lv $this->getLevel();
            
    $ps $lv->getPlayers();
            if(!
    count($ps)){
                return [
    null,null];
            }
            
    $target null;
            
    $dist null;
            foreach(
    $ps as $pl){
                if(
    $pl->isCreative()){
                    continue;
                }
                
    $cd $this->distance($pl);
                if((
    $cd self::$range)||($dist && $cd $dist)||$pl->getHealth()<1){
                    continue;
                }
                
    $dist $cd;
                
    $target $pl;
            }
            return [
    $target,$dist];
        }
        public function 
    onUpdate($currentTick){
            if (
    $this->knockback) {
                if (
    time() < $this->knockback) {
                    return  
    parent::onUpdate($currentTick);
                }
                
    $this->knockback 0;
            }
            
    $hasUpdate false;
            
    $this->timings->startTiming();
            
    // Handle flying objects...
            
    $tickDiff max(1$currentTick $this->lastUpdate);
            
    $bb = clone $this->getBoundingBox();
            
    $onGround count($this->level->getCollisionBlocks($bb->offset(0, -$this->gravity0))) > 0;
            if(!
    $onGround){
                
    // falling or jumping...
                
    $this->motionY -= $this->gravity;
                
    $this->+= $this->motionX $tickDiff;
                
    $this->+= $this->motionY $tickDiff;
                
    $this->+= $this->motionZ $tickDiff;
                
    //echo ("Falling...\n");
            
    }else{
                
    $this->motionX 0// No longer jumping/falling
                
    $this->motionY 0;
                
    $this->motionZ 0;
                if (
    $this->!= floor($this->y)) $this->floor($this->y);
                
    // Try to attack a player
                
    list($target,$dist) = $this->findTarget();
                if(
    $target !== null && $dist 0){
                    
    $dir $target->subtract($this);
                    
    $dir $dir->divide($dist);
                    
    $this->yaw rad2deg(atan2(-$dir->getX(),$dir->getZ()));
                    
    $this->pitch rad2deg(atan(-$dir->getY()));
                    if (
    $dist self::$attack) {
                        
    //
                        
    $x $dir->getX() * self::$speed;
                        
    $y 0;
                        
    $z $dir->getZ() * self::$speed;
                        
    $isJump count($this->level->getCollisionBlocks($bb->offset($x1.2$z))) <= 0;
                        if(
    count($this->level->getCollisionBlocks($bb->offset(00.1$z))) > 0){
                            if (
    $isJump) {
                                
    $y self::$jump;
                                
    $this->motionZ $z;
                            }
                            
    $z 0;
                        }
                        if(
    count($this->level->getCollisionBlocks($bb->offset($x0.10))) > 0){
                            if (
    $isJump) {
                                
    $y self::$jump;
                                
    $this->motionX $x;
                            }
                            
    $x 0;
                        }
                        
    //if ($y) echo "Jumping\n";
                        
    $ev = new \pocketmine\event\entity\EntityMotionEvent($this,new \pocketmine\math\Vector3($x,$y,$z));
                        
    $this->server->getPluginManager()->callEvent($ev);
                        if (
    $ev->isCancelled()) return false;
                        
    $this->+= $x;
                        
    $this->+= $y;
                        
    $this->+= $z;
                    }else {
                        
    $attack mt_rand(0,4);
                        if (
    $attack && $attack 0) {
                            
    $source = new EntityDamageByEntityEvent($this,$target,EntityDamageEvent::CAUSE_ENTITY_ATTACK,$attack);
                            
    $target->attack($attack,$source);
                        }
                    }
                }
            }
            
    $bb = clone $this->getBoundingBox();
            
    $onGround count($this->level->getCollisionBlocks($bb->offset(0, -$this->gravity0))) > 0;
            
    $this->onGround $onGround;
            
    $this->timings->stopTiming();
            
    $hasUpdate parent::onUpdate($currentTick) || $hasUpdate;
            return 
    $hasUpdate;
        }
        public function 
    knockBack(Entity $attacker$damage$x$z$base 2){
            echo 
    __METHOD__.",".__LINE__."\n"//##DEBUG
            
    parent::knockBack($attacker,$damage,$x,$z,$base);
            
    $this->knockback time() + 1;// Stunned for 1 second...
        
    }
    }
    Status API Training Shop Blog About Pricing
    © 2015 GitHub
    IncTerms Privacy Security Contact Help
    credit : ALIULY
    https://github.com/alejandroliu/bad...obsters/src/aliuly/mobsters/idiots/Zombie.php
  9. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    I did the same like he did with the packet
  10. Patrick jean noel
    Offline

    Patrick jean noel New Member

    Joined:
    Apr 26, 2014
    Posts:
    21
    Minecraft User:
    patarhd
    Once You download it... Do you put the phps in the plugin folder?!? Or somewhere else?like the mobs and such.... :p
  11. zombie_Power
    Offline

    zombie_Power Active Member

    Joined:
    Sep 7, 2015
    Posts:
    239
    Minecraft User:
    Zombie_Power
    download the zip folder... add it to pmt.mcpe.me . receive your .phar file and add it to your plugins folder
  12. Patrick jean noel
    Offline

    Patrick jean noel New Member

    Joined:
    Apr 26, 2014
    Posts:
    21
    Minecraft User:
    patarhd
    Ooo,makes sence,Lmao,but...where shall I get this pmt.mcpe.me thingy?!?sorry for this nubish act :p
  13. Patrick jean noel
    Offline

    Patrick jean noel New Member

    Joined:
    Apr 26, 2014
    Posts:
    21
    Minecraft User:
    patarhd
    Nvm...lmao TY!!!
  14. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    Next time find a person to ask on PM (Personal Message), so you wont go off-topic, just like i did now.

Share This Page

Advertisement