Jump to content

Recommended Posts

Posted

I am creating a dinosaur mod where dinosaurs can guard a nest that they have already laid. The AI for laying a nest is working, but the guarding AI I coded doesn't work.

The guarding AI is supposed to accomplish 2 tasks:

a. Make sure the dinosaur can't move out of range from the nest

b. Attack any EntityLiving within the range specified

Currently, none of them are working. Here is my code:

public class EntityAIGuardNest extends EntityAIBase {

        private EntityDinosaurTameable dinosaur;

        World world;

        private double speed;
        private double distance;

        public EntityAIGuardNest(EntityDinosaurTameable dinosaur, double distance ,double speed){
            this.dinosaur = dinosaur;
            this.world = this.dinosaur.world;

            this.speed = speed;
            this.distance = distance;
        }

        @Override
        public boolean shouldExecute(){
            return this.dinosaur.nest != null && !this.dinosaur.isTamed() && this.dinosaur.nestBlockPos != null;
        }

        @Override
        public void updateTask(){
            if(MathHelper.sqrt(this.dinosaur.getDistanceSq(this.dinosaur.nestBlockPos)) > this.distance){
                System.out.println("test");
                this.dinosaur.getNavigator().tryMoveToXYZ(this.dinosaur.nestBlockPos.getX(), this.dinosaur.nestBlockPos.getY(), this.dinosaur.nestBlockPos.getZ(), this.speed);
            } else {
                System.out.println("test2");
                List<EntityLiving> list = this.dinosaur.world.<EntityLiving>getEntitiesWithinAABB(EntityLiving.class, this.dinosaur.nest.getCollisionBoundingBox(this.dinosaur.nest.getDefaultState(), this.dinosaur.world, nestBlockPos).grow(this.distance));
                for (EntityLiving attacker : list)
                {
                    if (!(attacker instanceof EntityEgg) && attacker.getDistanceSq(this.dinosaur.nestBlockPos) < this.distance)
                    {
                        this.dinosaur.setAttackTarget(attacker);
                    }
                }
            }
        }

        @Override
        public boolean shouldContinueExecuting()
        {
            return this.dinosaur.nest != null;
        }
    }

 

Posted

Can't say much without seeing the dinosaur entity code as well... But already can tell that dinosaur.setAttackTarget() part should be in the shouldExecute() function, and the for loop must be broken upon finding the target.

Posted (edited)

I added a break in the for loop, and I found out the problem. The code I have used to get the block's bounding box is not working, as I changed 

List<EntityLiving> list = this.dinosaur.world.<EntityLiving>getEntitiesWithinAABB(EntityLiving.class, this.dinosaur.nest.getCollisionBoundingBox(this.dinosaur.nest.getDefaultState(), this.dinosaur.world, nestBlockPos).grow(this.distance));

to:

                List<EntityLiving> list = this.dinosaur.world.<EntityLiving>getEntitiesWithinAABB(EntityLiving.class, this.dinosaur.getEntityBoundingBox().grow(this.distance));

And it began to attack other mobs. The problem is that I don't want it to attack other entities within a certain range of the dinosaur, I want it to attack certain entities within a certain range of the nest. So I need to know why the 1st snippet of code is not working

Edited by Geometrically

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.