    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 onUpdateTasks()
    031        {
    032            ArrayList var1 = new ArrayList();
    033            Iterator var2;
    034            EntityAITaskEntry var3;
    035
    036            if (this.field_75778_d++ % this.field_75779_e == 0)
    037            {
    038                var2 = this.taskEntries.iterator();
    039
    040                while (var2.hasNext())
    041                {
    042                    var3 = (EntityAITaskEntry)var2.next();
    043                    boolean var4 = this.executingTaskEntries.contains(var3);
    044
    045                    if (var4)
    046                    {
    047                        if (this.func_75775_b(var3) && this.func_75773_a(var3))
    048                        {
    049                            continue;
    050                        }
    051
    052                        var3.action.resetTask();
    053                        this.executingTaskEntries.remove(var3);
    054                    }
    055
    056                    if (this.func_75775_b(var3) && var3.action.shouldExecute())
    057                    {
    058                        var1.add(var3);
    059                        this.executingTaskEntries.add(var3);
    060                    }
    061                }
    062            }
    063            else
    064            {
    065                var2 = this.executingTaskEntries.iterator();
    066
    067                while (var2.hasNext())
    068                {
    069                    var3 = (EntityAITaskEntry)var2.next();
    070
    071                    if (!var3.action.continueExecuting())
    072                    {
    073                        var3.action.resetTask();
    074                        var2.remove();
    075                    }
    076                }
    077            }
    078
    079            this.theProfiler.startSection("goalStart");
    080            var2 = var1.iterator();
    081
    082            while (var2.hasNext())
    083            {
    084                var3 = (EntityAITaskEntry)var2.next();
    085                this.theProfiler.startSection(var3.action.getClass().getSimpleName());
    086                var3.action.startExecuting();
    087                this.theProfiler.endSection();
    088            }
    089
    090            this.theProfiler.endSection();
    091            this.theProfiler.startSection("goalTick");
    092            var2 = this.executingTaskEntries.iterator();
    093
    094            while (var2.hasNext())
    095            {
    096                var3 = (EntityAITaskEntry)var2.next();
    097                var3.action.updateTask();
    098            }
    099
    100            this.theProfiler.endSection();
    101        }
    102
    103        private boolean func_75773_a(EntityAITaskEntry par1EntityAITaskEntry)
    104        {
    105            this.theProfiler.startSection("canContinue");
    106            boolean var2 = par1EntityAITaskEntry.action.continueExecuting();
    107            this.theProfiler.endSection();
    108            return var2;
    109        }
    110
    111        private boolean func_75775_b(EntityAITaskEntry par1EntityAITaskEntry)
    112        {
    113            this.theProfiler.startSection("canUse");
    114            Iterator var2 = this.taskEntries.iterator();
    115
    116            while (var2.hasNext())
    117            {
    118                EntityAITaskEntry var3 = (EntityAITaskEntry)var2.next();
    119
    120                if (var3 != par1EntityAITaskEntry)
    121                {
    122                    if (par1EntityAITaskEntry.priority >= var3.priority)
    123                    {
    124                        if (this.executingTaskEntries.contains(var3) && !this.areTasksCompatible(par1EntityAITaskEntry, var3))
    125                        {
    126                            this.theProfiler.endSection();
    127                            return false;
    128                        }
    129                    }
    130                    else if (this.executingTaskEntries.contains(var3) && !var3.action.isContinuous())
    131                    {
    132                        this.theProfiler.endSection();
    133                        return false;
    134                    }
    135                }
    136            }
    137
    138            this.theProfiler.endSection();
    139            return true;
    140        }
    141
    142        /**
    143         * Returns whether two EntityAITaskEntries can be executed concurrently
    144         */
    145        private boolean areTasksCompatible(EntityAITaskEntry par1EntityAITaskEntry, EntityAITaskEntry par2EntityAITaskEntry)
    146        {
    147            return (par1EntityAITaskEntry.action.getMutexBits() & par2EntityAITaskEntry.action.getMutexBits()) == 0;
    148        }
    149    }