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