    001    package net.minecraft.src;
    002
    003    import java.util.ArrayList;
    004    import java.util.Iterator;
    005    import java.util.List;
    006
    007    public class EntityAITasks
    008    {
    009        /** A list of EntityAITaskEntrys in EntityAITasks. */
    010        public List taskEntries = new ArrayList();
    011
    012        /** A list of EntityAITaskEntrys that are currently being executed. */
    013        private List executingTaskEntries = new ArrayList();
    014
    015        /** Instance of Profiler. */
    016        private final Profiler theProfiler;
    017        private int field_75778_d = 0;
    018        private int field_75779_e = 3;
    019
    020        public EntityAITasks(Profiler par1Profiler)
    021        {
    022            this.theProfiler = par1Profiler;
    023        }
    024
    025        public void addTask(int par1, EntityAIBase par2EntityAIBase)
    026        {
    027            this.taskEntries.add(new EntityAITaskEntry(this, par1, par2EntityAIBase));
    028        }
    029
    030        public void func_85156_a(EntityAIBase par1EntityAIBase)
    031        {
    032            Iterator var2 = this.taskEntries.iterator();
    033
    034            while (var2.hasNext())
    035            {
    036                EntityAITaskEntry var3 = (EntityAITaskEntry)var2.next();
    037                EntityAIBase var4 = var3.action;
    038
    039                if (var4 == par1EntityAIBase)
    040                {
    041                    if (this.executingTaskEntries.contains(var3))
    042                    {
    043                        var4.resetTask();
    044                        this.executingTaskEntries.remove(var3);
    045                    }
    046
    047                    var2.remove();
    048                }
    049            }
    050        }
    051
    052        public void onUpdateTasks()
    053        {
    054            ArrayList var1 = new ArrayList();
    055            Iterator var2;
    056            EntityAITaskEntry var3;
    057
    058            if (this.field_75778_d++ % this.field_75779_e == 0)
    059            {
    060                var2 = this.taskEntries.iterator();
    061
    062                while (var2.hasNext())
    063                {
    064                    var3 = (EntityAITaskEntry)var2.next();
    065                    boolean var4 = this.executingTaskEntries.contains(var3);
    066
    067                    if (var4)
    068                    {
    069                        if (this.func_75775_b(var3) && this.func_75773_a(var3))
    070                        {
    071                            continue;
    072                        }
    073
    074                        var3.action.resetTask();
    075                        this.executingTaskEntries.remove(var3);
    076                    }
    077
    078                    if (this.func_75775_b(var3) && var3.action.shouldExecute())
    079                    {
    080                        var1.add(var3);
    081                        this.executingTaskEntries.add(var3);
    082                    }
    083                }
    084            }
    085            else
    086            {
    087                var2 = this.executingTaskEntries.iterator();
    088
    089                while (var2.hasNext())
    090                {
    091                    var3 = (EntityAITaskEntry)var2.next();
    092
    093                    if (!var3.action.continueExecuting())
    094                    {
    095                        var3.action.resetTask();
    096                        var2.remove();
    097                    }
    098                }
    099            }
    100
    101            this.theProfiler.startSection("goalStart");
    102            var2 = var1.iterator();
    103
    104            while (var2.hasNext())
    105            {
    106                var3 = (EntityAITaskEntry)var2.next();
    107                this.theProfiler.startSection(var3.action.getClass().getSimpleName());
    108                var3.action.startExecuting();
    109                this.theProfiler.endSection();
    110            }
    111
    112            this.theProfiler.endSection();
    113            this.theProfiler.startSection("goalTick");
    114            var2 = this.executingTaskEntries.iterator();
    115
    116            while (var2.hasNext())
    117            {
    118                var3 = (EntityAITaskEntry)var2.next();
    119                var3.action.updateTask();
    120            }
    121
    122            this.theProfiler.endSection();
    123        }
    124
    125        private boolean func_75773_a(EntityAITaskEntry par1EntityAITaskEntry)
    126        {
    127            this.theProfiler.startSection("canContinue");
    128            boolean var2 = par1EntityAITaskEntry.action.continueExecuting();
    129            this.theProfiler.endSection();
    130            return var2;
    131        }
    132
    133        private boolean func_75775_b(EntityAITaskEntry par1EntityAITaskEntry)
    134        {
    135            this.theProfiler.startSection("canUse");
    136            Iterator var2 = this.taskEntries.iterator();
    137
    138            while (var2.hasNext())
    139            {
    140                EntityAITaskEntry var3 = (EntityAITaskEntry)var2.next();
    141
    142                if (var3 != par1EntityAITaskEntry)
    143                {
    144                    if (par1EntityAITaskEntry.priority >= var3.priority)
    145                    {
    146                        if (this.executingTaskEntries.contains(var3) && !this.areTasksCompatible(par1EntityAITaskEntry, var3))
    147                        {
    148                            this.theProfiler.endSection();
    149                            return false;
    150                        }
    151                    }
    152                    else if (this.executingTaskEntries.contains(var3) && !var3.action.isContinuous())
    153                    {
    154                        this.theProfiler.endSection();
    155                        return false;
    156                    }
    157                }
    158            }
    159
    160            this.theProfiler.endSection();
    161            return true;
    162        }
    163
    164        /**
    165         * Returns whether two EntityAITaskEntries can be executed concurrently
    166         */
    167        private boolean areTasksCompatible(EntityAITaskEntry par1EntityAITaskEntry, EntityAITaskEntry par2EntityAITaskEntry)
    168        {
    169            return (par1EntityAITaskEntry.action.getMutexBits() & par2EntityAITaskEntry.action.getMutexBits()) == 0;
    170        }
    171    }