Jump to content

Mob Flocking AI...


Captain Hillman

Recommended Posts

I'm trying to implement a generic Flocking AI that I can use for any new creatures I add to my mod in the future. I've followed the flocking information found here (http://bit.ly/1kOI2ul) and implemented it with the code below. However, my creatures don't move at all (except for occasionally falling through the floor).

 

Anyone have any idea where I'm going wrong? If anyone's got any posts/tutorials on group-movement of creatures, that'd be handy too :)

 

FlockUtils.java

 

public class FlockUtils {

 

  public static Point2D.Double computeAlignment(Entity creature, int blockRadius) {

      Point2D.Double target = new Point2D.Double();

      int neighbours = 0;

     

      double x = creature.posX;

      double y = creature.posY;

      double z = creature.posZ;

     

      AxisAlignedBB boundingBox = AxisAlignedBB.getAABBPool().getAABB(x, y, z, x + 1, y + 1, z + 1);

      boundingBox = boundingBox.expand(blockRadius, blockRadius, blockRadius);

     

      List<Entity> entities = creature.worldObj.getEntitiesWithinAABB(Entity.class, boundingBox);

     

      for(Entity entity : entities) {

        if( !entity.equals(creature) && entity.getClass().equals(creature.getClass()) ) {

            target.x += entity.motionX;

            target.y += entity.motionZ;

            neighbours++;

        }

      }

     

      if(neighbours == 0) {

        return target;

      }

      target.x /= neighbours;

      target.y /= neighbours;

     

      double length = Math.sqrt((target.x * target.x) + (target.y * target.y));

      target.x /= length;

      target.y /= length;

      return target;

  }

 

  public static Point2D.Double computeCohesion(Entity creature, int blockRadius) {

      Point2D.Double target = new Point2D.Double();

      int neighbours = 0;

     

      double x = creature.posX;

      double y = creature.posY;

      double z = creature.posZ;

     

      AxisAlignedBB boundingBox = AxisAlignedBB.getAABBPool().getAABB(x, y, z, x + 1, y + 1, z + 1);

      boundingBox = boundingBox.expand(blockRadius, blockRadius, blockRadius);

     

      List<Entity> entities = creature.worldObj.getEntitiesWithinAABB(Entity.class, boundingBox);

     

      for(Entity entity : entities) {

        if( !entity.equals(creature) && entity.getClass().equals(creature.getClass()) ) {

            target.x += entity.posX;

            target.y += entity.posZ;

            neighbours++;

        }

      }

     

      if(neighbours == 0) {

        return target;

      }

      target.x /= neighbours;

      target.y /= neighbours;

     

      target = new Point2D.Double(target.x - creature.posX, target.y - creature.posZ);

     

      double length = Math.sqrt((target.x * target.x) + (target.y * target.y));

      target.x /= length;

      target.y /= length;

      return target;

  }

 

  public static Point2D.Double computeSeparation(Entity creature, int blockRadius) {

      Point2D.Double target = new Point2D.Double();

      int neighbours = 0;

     

      double x = creature.posX;

      double y = creature.posY;

      double z = creature.posZ;

     

      AxisAlignedBB boundingBox = AxisAlignedBB.getAABBPool().getAABB(x, y, z, x + 1, y + 1, z + 1);

      boundingBox = boundingBox.expand(blockRadius, blockRadius, blockRadius);

     

      List<Entity> entities = creature.worldObj.getEntitiesWithinAABB(Entity.class, boundingBox);

     

      for(Entity entity : entities) {

        if( !entity.equals(creature) && entity.getClass().equals(creature.getClass()) ) {

            target.x -= entity.posX;

            target.y -= entity.posZ;

            neighbours++;

        }

      }

     

      if(neighbours == 0) {

        return target;

      }

      target.x /= neighbours;

      target.y /= neighbours;

     

      target = new Point2D.Double((target.x - creature.posX) * -1, (target.y - creature.posZ) * -1);

     

      double length = Math.sqrt((target.x * target.x) + (target.y * target.y));

      target.x /= length;

      target.y /= length;

      return target;

  }

}

 

 

FlockAI.java

 

public class FlockAI extends EntityAIBase {

 

private EntityCreature entity;

    private double xPosition;

    private double yPosition;

    private double zPosition;

    private double speed;

 

    public FlockAI(EntityCreature par1EntityCreature, double par2) {

        this.entity = par1EntityCreature;

        this.speed = par2;

        this.setMutexBits(1);

    }

 

    @Override

    public boolean shouldExecute() {

        if(entity.getAge() >= 100) {

            return false;

           

        } else if (entity.getRNG().nextInt(120) != 0) {

            return false;

           

        } else {

            Vec3 vec3 = RandomPositionGenerator.findRandomTarget(entity, 10, 7);

 

            if(vec3 == null) {

                return false;

               

            } else {

                xPosition = vec3.xCoord;

                yPosition = vec3.yCoord;

                zPosition = vec3.zCoord;

                return true;

            }

        }

    }

 

    @Override

    public boolean continueExecuting() {

    return !this.entity.getNavigator().noPath();

    }

 

    @Override

    public void startExecuting() {

    entity.getNavigator().tryMoveToXYZ(xPosition, yPosition, zPosition, speed);

    }

   

    @Override

    public void updateTask() {

    if(!continueExecuting()) {

    return;

    }

    Point2D.Double alignment = FlockUtils.computeAlignment(entity, 5);

    Point2D.Double cohesion = FlockUtils.computeCohesion(entity, 5);

    Point2D.Double separation = FlockUtils.computeSeparation(entity, 5);

   

    entity.motionX += alignment.x + cohesion.x + separation.x;

    entity.motionZ += alignment.y + cohesion.y + separation.y;

   

    //Normalise it.

    double length = Math.sqrt((entity.motionX * entity.motionX) + (entity.motionZ * entity.motionZ));

    entity.motionX /= length;

    entity.motionZ /= length;

    }

}

 

Link to comment
Share on other sites

How did you register your AI to your entity? Can you show that code?  (You should be adding it to the tasks list and assign it an appropriate priority.)

 

I think you should take out the randomization until you have it working a bit more.  Your current code will only on average fire once every six seconds!

 

Also, if you're having this AI run together with other AI then you may need to look at how you set the mutexBits.  I recently figured out how those work.  Basically, you don't want two different movement AI active at the same time or each will be interfering with the other, and same with attacking, etc.  Swimming is compatible with some things but not others.  Anyway, see the last post on this thread about how to set the mutexBits: http://www.minecraftforge.net/forum/index.php?topic=18777.0.  In your case I think it mutexBits should be set to 1 (like other movement AI).

 

Lastly, you should put console System.out.println() statements in your code to confirm whether it is executing at all, and how.  I usually put in a statement in each method to at least indicate that the method was run with something like "running Flock AI startExecuting()" type statements.  And I would put such statements inside each if-else-if so you can trace which path is followed.  It is usually pretty clear once you see those what is happening.

 

Overall, for your AI to execute it needs to be in the task list, it needs to have higher priority (lower number) in that list than other movement AI tasks, and the shouldExecute() method needs to return true.  Also, you the continueExecuting() method needs to return true for as long as the AI is supposed to continue.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.