001package net.minecraft.entity.ai;
002
003import net.minecraft.entity.EntityCreature;
004import net.minecraft.util.MathHelper;
005import net.minecraft.util.Vec3;
006import net.minecraft.village.Village;
007import net.minecraft.village.VillageDoorInfo;
008
009public class EntityAIMoveIndoors extends EntityAIBase
010{
011    private EntityCreature entityObj;
012    private VillageDoorInfo doorInfo;
013    private int insidePosX = -1;
014    private int insidePosZ = -1;
015
016    public EntityAIMoveIndoors(EntityCreature par1EntityCreature)
017    {
018        this.entityObj = par1EntityCreature;
019        this.setMutexBits(1);
020    }
021
022    /**
023     * Returns whether the EntityAIBase should begin execution.
024     */
025    public boolean shouldExecute()
026    {
027        if ((!this.entityObj.worldObj.isDaytime() || this.entityObj.worldObj.isRaining()) && !this.entityObj.worldObj.provider.hasNoSky)
028        {
029            if (this.entityObj.getRNG().nextInt(50) != 0)
030            {
031                return false;
032            }
033            else if (this.insidePosX != -1 && this.entityObj.getDistanceSq((double)this.insidePosX, this.entityObj.posY, (double)this.insidePosZ) < 4.0D)
034            {
035                return false;
036            }
037            else
038            {
039                Village village = this.entityObj.worldObj.villageCollectionObj.findNearestVillage(MathHelper.floor_double(this.entityObj.posX), MathHelper.floor_double(this.entityObj.posY), MathHelper.floor_double(this.entityObj.posZ), 14);
040
041                if (village == null)
042                {
043                    return false;
044                }
045                else
046                {
047                    this.doorInfo = village.findNearestDoorUnrestricted(MathHelper.floor_double(this.entityObj.posX), MathHelper.floor_double(this.entityObj.posY), MathHelper.floor_double(this.entityObj.posZ));
048                    return this.doorInfo != null;
049                }
050            }
051        }
052        else
053        {
054            return false;
055        }
056    }
057
058    /**
059     * Returns whether an in-progress EntityAIBase should continue executing
060     */
061    public boolean continueExecuting()
062    {
063        return !this.entityObj.getNavigator().noPath();
064    }
065
066    /**
067     * Execute a one shot task or start executing a continuous task
068     */
069    public void startExecuting()
070    {
071        this.insidePosX = -1;
072
073        if (this.entityObj.getDistanceSq((double)this.doorInfo.getInsidePosX(), (double)this.doorInfo.posY, (double)this.doorInfo.getInsidePosZ()) > 256.0D)
074        {
075            Vec3 vec3 = RandomPositionGenerator.findRandomTargetBlockTowards(this.entityObj, 14, 3, this.entityObj.worldObj.getWorldVec3Pool().getVecFromPool((double)this.doorInfo.getInsidePosX() + 0.5D, (double)this.doorInfo.getInsidePosY(), (double)this.doorInfo.getInsidePosZ() + 0.5D));
076
077            if (vec3 != null)
078            {
079                this.entityObj.getNavigator().tryMoveToXYZ(vec3.xCoord, vec3.yCoord, vec3.zCoord, 0.3F);
080            }
081        }
082        else
083        {
084            this.entityObj.getNavigator().tryMoveToXYZ((double)this.doorInfo.getInsidePosX() + 0.5D, (double)this.doorInfo.getInsidePosY(), (double)this.doorInfo.getInsidePosZ() + 0.5D, 0.3F);
085        }
086    }
087
088    /**
089     * Resets the task
090     */
091    public void resetTask()
092    {
093        this.insidePosX = this.doorInfo.getInsidePosX();
094        this.insidePosZ = this.doorInfo.getInsidePosZ();
095        this.doorInfo = null;
096    }
097}