    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                this.theProfiler.startSection(var3.action.getClass().getSimpleName());
    098                var3.action.updateTask();
    099                this.theProfiler.endSection();
    100            }
    101
    102            this.theProfiler.endSection();
    103        }
    104
    105        private boolean func_75773_a(EntityAITaskEntry par1EntityAITaskEntry)
    106        {
    107            this.theProfiler.startSection("canContinue");
    108            boolean var2 = par1EntityAITaskEntry.action.continueExecuting();
    109            this.theProfiler.endSection();
    110            return var2;
    111        }
    112
    113        private boolean func_75775_b(EntityAITaskEntry par1EntityAITaskEntry)
    114        {
    115            this.theProfiler.startSection("canUse");
    116            Iterator var2 = this.taskEntries.iterator();
    117
    118            while (var2.hasNext())
    119            {
    120                EntityAITaskEntry var3 = (EntityAITaskEntry)var2.next();
    121
    122                if (var3 != par1EntityAITaskEntry)
    123                {
    124                    if (par1EntityAITaskEntry.priority >= var3.priority)
    125                    {
    126                        if (this.executingTaskEntries.contains(var3) && !this.areTasksCompatible(par1EntityAITaskEntry, var3))
    127                        {
    128                            this.theProfiler.endSection();
    129                            return false;
    130                        }
    131                    }
    132                    else if (this.executingTaskEntries.contains(var3) && !var3.action.isContinuous())
    133                    {
    134                        this.theProfiler.endSection();
    135                        return false;
    136                    }
    137                }
    138            }
    139
    140            this.theProfiler.endSection();
    141            return true;
    142        }
    143
    144        /**
    145         * Returns whether two EntityAITaskEntries can be executed concurrently
    146         */
    147        private boolean areTasksCompatible(EntityAITaskEntry par1EntityAITaskEntry, EntityAITaskEntry par2EntityAITaskEntry)
    148        {
    149            return (par1EntityAITaskEntry.action.getMutexBits() & par2EntityAITaskEntry.action.getMutexBits()) == 0;
    150        }
    151    }