예제 #1
0
 protected void RefreshOutput() //
 {
     if (powerComp.PowerOn)
     {
         Thing currentItem          = Position.GetFirstItem(Map);
         bool  storageSlotAvailable = currentItem == null || (settings.AllowedToAccept(currentItem) &&
                                                              OutputSettings.SatisfiesMax(currentItem.stackCount, currentItem.def.stackLimit));
         if (boundStorageUnit != null && boundStorageUnit.CanReceiveIO)
         {
             if (storageSlotAvailable && OutputSettings.min <= OutputSettings.max)
             {
                 List <Thing> itemCandidates = new List <Thing>(from Thing t in boundStorageUnit.StoredItems where settings.AllowedToAccept(t) select t); // ToList very important - evaluates enumerable
                 if (ItemsThatSatisfyMin(itemCandidates, currentItem).Any())
                 {
                     foreach (Thing item in itemCandidates)
                     {
                         if (currentItem != null)
                         {
                             if (currentItem.CanStackWith(item))
                             {
                                 int count = Math.Min(item.stackCount, OutputSettings.CountNeededToReachMax(currentItem.stackCount, currentItem.def.stackLimit));
                                 if (count > 0)
                                 {
                                     currentItem.TryAbsorbStack(item.SplitOff(count), true);
                                 }
                             }
                         }
                         else
                         {
                             int count = OutputSettings.CountNeededToReachMax(0, item.stackCount);
                             if (count > 0)
                             {
                                 currentItem = GenSpawn.Spawn(item.SplitOff(count), Position, Map);
                             }
                         }
                         if (currentItem != null && !OutputSettings.SatisfiesMax(currentItem.stackCount, currentItem.def.stackLimit))
                         {
                             break;
                         }
                     }
                 }
             }
             //Transfre a item back if it is either too few or disallowed
             if (currentItem != null && (!settings.AllowedToAccept(currentItem) || !OutputSettings.SatisfiesMin(currentItem.stackCount)) && boundStorageUnit.settings.AllowedToAccept(currentItem))
             {
                 boundStorageUnit.RegisterNewItem(currentItem);
             }
             //Transfer the diffrence back if it is too much
             if (currentItem != null && (!OutputSettings.SatisfiesMax(currentItem.stackCount, currentItem.def.stackLimit) && boundStorageUnit.settings.AllowedToAccept(currentItem)))
             {
                 int splitCount = -OutputSettings.CountNeededToReachMax(currentItem.stackCount, currentItem.def.stackLimit);
                 if (splitCount > 0)
                 {
                     boundStorageUnit.RegisterNewItem(currentItem.SplitOff(splitCount));
                 }
             }
         }
     }
 }