Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[SOLVED] [1.10.2] How to tell if a block can be "occupied" by an entity

Featured Replies

Posted

Edit: I updated the title to get closer to what I'm actually asking, which is how to tell if a block can be "occupied" by a living entity. See the second paragraph for more detail.

 

I'm creating a new entity AI that causes mobs to flee from any light source when it's night. I used EntityAIFleeSun as the base, and it's working decently. What I can't figure out is how to prevent it from picking destinations that are underground. EntityAIFleeSun simply picks 10 random blocks within 10 blocks n/w/s/e and 3 blocks up/down, then checks the light level of that block and makes sure it can't see the sky. But there doesn't seem to be anything to prevent it from picking blocks underground, and since solid blocks always have a light level of 0 it seems this would be a problem. I'm not sure why it works.

 

My AI that's based on EntityAIFleeSun sends my mobs running around insanely trying to reach blocks that are underground, because they always have a light level of 0. I must be missing something obvious. I was thinking I could solve it by checking if the block is "transparent" or otherwise able to be occupied by a living entity, but I'm not sure how to determine that. There are a million methods in the block class that all seem identical (isNormalCube, isFullCube, isOpaqueCube, isFullyOpaque, etc.). I also considered the method World#getTopSolidOrLiquidBlock, but that gets the top block from the height map, which wouldn't work when mobs are in caves or under trees. What am I missing? Any ideas?

 

My AI class:

Spoiler

 


public class PrimalEntityAIFleeFirelight extends EntityAIFleeSun
{

	private final World world;
	private final EntityCreature theCreature;
	private final double movementSpeed;
	private double shelterX;
	private double shelterY;
	private double shelterZ;

	public PrimalEntityAIFleeFirelight(EntityCreature theCreatureIn, double movementSpeedIn)
	{
		super(theCreatureIn, movementSpeedIn);
		this.world = theCreatureIn.world;
		this.theCreature = theCreatureIn;
		this.movementSpeed = movementSpeedIn;
		this.setMutexBits(1);
	}

	@Override
	public boolean shouldExecute()
	{
		if (!this.theCreature.getNavigator().noPath()) return false;
		if (this.world.isDaytime())
		{
			return false; // If it's daytime, don't flee
		}
		else if (PrimalAIUtil.isInDark(this.theCreature))
		{
			return false; // If it's night and entity is in the dark, don't flee
		}
		else
		{
			Vec3d vec3d = this.findPossibleShelter();

			if (vec3d == null)
			{
				return false;
			}
			else
			{
				this.shelterX = vec3d.xCoord;
				this.shelterY = vec3d.yCoord;
				this.shelterZ = vec3d.zCoord;
				return true;
			}
		}
	}

	@Override
	public boolean continueExecuting()
	{
		return !this.theCreature.getNavigator().noPath();
	}

	@Override
	public void startExecuting()
	{
		this.theCreature.getNavigator().tryMoveToXYZ(this.shelterX, this.shelterY, this.shelterZ, this.movementSpeed);
		world.playSound(null, new BlockPos(this.shelterX, this.shelterY, this.shelterZ), SoundEvents.ENTITY_WOLF_HURT, SoundCategory.HOSTILE, 1.0F, 1.0F);
	}

	@Nullable
	private Vec3d findPossibleShelter()
	{
		Random random = this.theCreature.getRNG();
		BlockPos entityPos = new BlockPos(this.theCreature.posX, this.theCreature.getEntityBoundingBox().minY, this.theCreature.posZ);

		for (int i = 2; i < 22; ++i)
		{
			int y = (int) Math.ceil((double) i / 3);
			double xCoord = random.nextInt(i) - i / 2;
			double yCoord = random.nextInt(y) - (y / 2);
			double zCoord = random.nextInt(i) - i / 2;

			BlockPos possibleShelter = entityPos.add(xCoord, yCoord, zCoord);

			if (world.getLightFromNeighbors(possibleShelter) <= 5)
			{
				return new Vec3d((double) possibleShelter.getX(), (double) possibleShelter.getY(), (double) possibleShelter.getZ());
			}
		}

		return null;
	}

}

public class PrimalAIUtil
{

	public static float DARKNESS_THRESHOLD = 0.1F;

	public static boolean isInDark(Entity entity)
	{
        float f = entity.getBrightness(1.0F);
        
        return f <= DARKNESS_THRESHOLD;
		
	}
}

 

 

 

 

Edited by Daeruin
Changed title to be more accurate

  • Author

I finally figured this out. I have edited the title of the post to reflect the question I actually should have been asking.

 

All I needed to do was check if the block had a collision bounding box (using world.getBlockState.getCollisionBoundingBox). Blocks with a bounding box aren't legitimate targets for an entity to move to, so if it has a bounding box, I check the blocks above it until I find a block with no bounding box. Then I check the light level, and if it's dark enough, I tell the entity to move there.

 

I also implemented a boolean toggle in the AI task that defaults to false. I turn it to true as soon as I find a legitimate block to move to. In the AI task's shouldExecute method, I first check if the toggle is true—if so, the entity is already moving somewhere, so we don't need to try to flee again. As soon as the entity has no path (i.e., they reached their destination), I set the toggle back to false.

 

New AI task code:

Spoiler

public class PrimalEntityAIFleeFirelight extends EntityAIFleeSun
{

	private final World world;
	private final EntityCreature theCreature;
	private final double movementSpeed;
	private double shelterX;
	private double shelterY;
	private double shelterZ;
	private boolean foundShelter;

	public PrimalEntityAIFleeFirelight(EntityCreature theCreatureIn, double movementSpeedIn)
	{
		super(theCreatureIn, movementSpeedIn);
		this.world = theCreatureIn.world;
		this.theCreature = theCreatureIn;
		this.movementSpeed = movementSpeedIn;
		this.setMutexBits(1);
	}

	@Override
	public boolean shouldExecute()
	{
		if (foundShelter)
		{
			return false;
		}
		else if (!PrimalAIUtil.worldIsFullyDark(this.theCreature.world)) // If it's daytime, don't flee
		{
			return false;
		}
		else if (PrimalAIUtil.isInDark(this.theCreature)) // It's night. If entity is in dark, don't flee
		{
			return false;
		}
		else // It's night, and entity is not in the dark - TRY TO FLEE
		{
			System.out.println("Need to find shelter");
			Vec3d vec3d = this.findPossibleShelter();

			if (vec3d == null) // Couldn't find suitable shelter - don't flee after all
			{
				return false;
			}
			else // Found suitable shelter - FLEE!
			{
				this.shelterX = vec3d.xCoord;
				this.shelterY = vec3d.yCoord;
				this.shelterZ = vec3d.zCoord;
				this.foundShelter = true;
				return true;
			}
		}
	}

	@Override
	public boolean continueExecuting()
	{
		if (this.theCreature.getNavigator().noPath())
		{
			this.foundShelter = false;
			return false;
		}
		else return true;
	}

	@Override
	public void startExecuting()
	{
		this.theCreature.getNavigator().tryMoveToXYZ(this.shelterX, this.shelterY, this.shelterZ, this.movementSpeed);

		BlockPos blockpos = new BlockPos(this.theCreature.posX, this.theCreature.getEntityBoundingBox().minY, this.theCreature.posZ);
		World world = this.theCreature.world;
		int i = world.getLightFromNeighbors(blockpos);

		if (i >= 5)
			world.playSound(null, new BlockPos(this.shelterX, this.shelterY, this.shelterZ), SoundEvents.ENTITY_WOLF_HURT, SoundCategory.HOSTILE, 1.0F, 1.0F);
	}

	@Nullable
	private Vec3d findPossibleShelter()
	{
		Random random = this.theCreature.getRNG();
		BlockPos entityPos = new BlockPos(this.theCreature.posX, this.theCreature.getEntityBoundingBox().minY, this.theCreature.posZ);

		for (int i = 2; i < 22; ++i)
		{
			int y = (int) Math.ceil((double) i / 3);
			double xCoord = random.nextInt(i) - i / 2;
			double yCoord = random.nextInt(y) - (y / 2);
			double zCoord = random.nextInt(i) - i / 2;

			BlockPos possibleShelter = entityPos.add(xCoord, yCoord, zCoord);
			AxisAlignedBB collisionBox = world.getBlockState(possibleShelter).getCollisionBoundingBox(world, possibleShelter); 

			if (collisionBox != null) // If block has a collision box
			{
				for (int j = 1; j < 4; j++) // Check up to three blocks above possible shelter
				{
					BlockPos posToCheck = possibleShelter.add(0, j, 0);
					collisionBox = world.getBlockState(posToCheck).getCollisionBoundingBox(world, posToCheck);

					if (collisionBox == null) // If block doesn't have collision box
					{
						possibleShelter = posToCheck; // Choose this block
						break;
					}
				}
			}

			if (world.getLightFromNeighbors(possibleShelter) <= 4)
			{
				return new Vec3d((double) possibleShelter.getX(), (double) possibleShelter.getY(), (double) possibleShelter.getZ());
			}
		}

		return null;
	}

}

 

 

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.