예제 #1
0
        public bool OutputItem(Thing thing)
        {
            if (boundStorageUnit?.CanReceiveIO ?? false)
            {
                return(GenPlace.TryPlaceThing(thing.SplitOff(thing.stackCount), Position, Map, ThingPlaceMode.Near,
                                              null, pos =>
                {
                    if (settings.AllowedToAccept(thing) && OutputSettings.SatisfiesMin(thing.stackCount))
                    {
                        if (pos == Position)
                        {
                            return true;
                        }
                    }
                    foreach (Thing t in Map.thingGrid.ThingsListAt(pos))
                    {
                        if (t is Building_StorageUnitIOPort)
                        {
                            return false;
                        }
                    }

                    return true;
                }));
            }

            return(false);
        }
예제 #2
0
 public void OutputItem(Thing thing)
 {
     if (this.boundStorageUnit?.CanReceiveIO ?? false)
     {
         Thing currentItem = Position.GetFirstItem(Map);
         if (currentItem == null)
         {
             if (this.settings.AllowedToAccept(thing) && OutputSettings.SatisfiesMin(thing.stackCount))
             {
                 GenPlace.TryPlaceThing(thing.SplitOff(thing.stackCount), this.Position, this.Map, ThingPlaceMode.Near, null,
                                        delegate(IntVec3 pos) {
                     // Can place here or anywhere near here not in another IOPort
                     if (pos == this.Position)
                     {
                         return(true);
                     }
                     foreach (Thing t in this.Map.thingGrid.ThingsListAt(pos))
                     {
                         if (t is Building_StorageUnitIOPort)
                         {
                             return(false);
                         }
                     }
                     return(true);
                 });
             }
             else
             {
                 GenPlace.TryPlaceThing(thing.SplitOff(thing.stackCount), this.Position, this.Map, ThingPlaceMode.Near, null,
                                        // anywhere but on a storage unit io port.
                                        delegate(IntVec3 pos) {
                     foreach (Thing t in this.Map.thingGrid.ThingsListAt(pos))
                     {
                         if (t is Building_StorageUnitIOPort)
                         {
                             return(false);
                         }
                     }
                     return(true);
                 });
             }
         }
         else
         {
             GenPlace.TryPlaceThing(thing.SplitOff(thing.stackCount), this.Position, this.Map, ThingPlaceMode.Near, null,
                                    // anywhere but on a storage unit io port.
                                    delegate(IntVec3 pos) {
                 foreach (Thing t in this.Map.thingGrid.ThingsListAt(pos))
                 {
                     if (t is Building_StorageUnitIOPort)
                     {
                         return(false);
                     }
                 }
                 return(true);
             });
         }
     }
 }
예제 #3
0
 protected IEnumerable <Thing> ItemsThatSatisfyMin(List <Thing> itemCandidates, Thing currentItem)
 {
     if (currentItem != null)
     {
         IEnumerable <Thing> stackableCandidates = itemCandidates.Where(t => currentItem.CanStackWith(t));
         return(OutputSettings.SatisfiesMin(stackableCandidates.Sum(t => t.stackCount) + currentItem.stackCount) ? stackableCandidates : Enumerable.Empty <Thing>());
     }
     return(itemCandidates
            .GroupBy(t => t.def)
            .FirstOrDefault(g => OutputSettings.SatisfiesMin(g.Sum(t => t.stackCount))) ?? Enumerable.Empty <Thing>());
 }
예제 #4
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));
                 }
             }
         }
     }
 }