Example #1
0
        //precondition: lock is already taken
        public bool Defragment()
        {
            TEStorageUnit emptyUnit = null;

            foreach (TEAbstractStorageUnit abstractStorageUnit in GetStorageUnits())
            {
                if (!(abstractStorageUnit is TEStorageUnit))
                {
                    continue;
                }
                TEStorageUnit storageUnit = (TEStorageUnit)abstractStorageUnit;
                if (emptyUnit == null && storageUnit.IsEmpty && !storageUnit.Inactive)
                {
                    emptyUnit = storageUnit;
                }
                else if (emptyUnit != null && !storageUnit.IsEmpty && storageUnit.NumItems <= emptyUnit.Capacity)
                {
                    TEStorageUnit.SwapItems(emptyUnit, storageUnit);
                    NetHelper.SendRefreshNetworkItems(ID);
                    return(true);
                }
            }
            compactStage++;
            return(false);
        }
Example #2
0
        //precondition: lock is already taken
        public bool EmptyInactive()
        {
            TEStorageUnit inactiveUnit = null;

            foreach (TEAbstractStorageUnit abstractStorageUnit in GetStorageUnits())
            {
                if (!(abstractStorageUnit is TEStorageUnit))
                {
                    continue;
                }
                TEStorageUnit storageUnit = (TEStorageUnit)abstractStorageUnit;
                if (storageUnit.Inactive && !storageUnit.IsEmpty)
                {
                    inactiveUnit = storageUnit;
                }
            }
            if (inactiveUnit == null)
            {
                compactStage++;
                return(false);
            }
            foreach (TEAbstractStorageUnit abstractStorageUnit in GetStorageUnits())
            {
                if (!(abstractStorageUnit is TEStorageUnit) || abstractStorageUnit.Inactive)
                {
                    continue;
                }
                TEStorageUnit storageUnit = (TEStorageUnit)abstractStorageUnit;
                if (storageUnit.IsEmpty && inactiveUnit.NumItems <= storageUnit.Capacity)
                {
                    TEStorageUnit.SwapItems(inactiveUnit, storageUnit);
                    NetHelper.SendRefreshNetworkItems(ID);
                    return(true);
                }
            }
            bool hasChange = false;

            NetHelper.StartUpdateQueue();
            Item tryMove = inactiveUnit.WithdrawStack();

            foreach (TEAbstractStorageUnit abstractStorageUnit in GetStorageUnits())
            {
                if (!(abstractStorageUnit is TEStorageUnit) || abstractStorageUnit.Inactive)
                {
                    continue;
                }
                TEStorageUnit storageUnit = (TEStorageUnit)abstractStorageUnit;
                while (storageUnit.HasSpaceFor(tryMove, true) && !tryMove.IsAir)
                {
                    storageUnit.DepositItem(tryMove, true);
                    if (tryMove.IsAir && !inactiveUnit.IsEmpty)
                    {
                        tryMove = inactiveUnit.WithdrawStack();
                    }
                    hasChange = true;
                }
            }
            if (!tryMove.IsAir)
            {
                inactiveUnit.DepositItem(tryMove, true);
            }
            NetHelper.ProcessUpdateQueue();
            if (hasChange)
            {
                NetHelper.SendRefreshNetworkItems(ID);
            }
            else
            {
                compactStage++;
            }
            return(hasChange);
        }