Posted July 7, 20187 yr 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; } }
July 7, 20187 yr 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.
July 8, 20187 yr Author 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 July 8, 20187 yr 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.