public void RegisterStockpile(CompRTQuantumStockpile compStockpile)
 {
     if (!compStockpiles.Contains(compStockpile))
     {
         compStockpiles.Add(compStockpile);
     }
 }
        private void DefragStockpileCells()
        {
            CompRTQuantumStockpile sourceStockpile = compStockpiles[qsSourceIndex];
            CompRTQuantumStockpile targetStockpile = compStockpiles[qsTargetIndex];

            foreach (IntVec3 sourceCell in sourceStockpile.parent.OccupiedRect().Cells)
            {
                if (sparklesEnabled)
                {
                    sourceCell.ThrowSparkle(parent.Map);
                }
                List <Thing> sourceThingsWithChunks = sourceCell.GetItemList(parent.Map, true);
                foreach (Thing thing in sourceThingsWithChunks)
                {
                    if (thing.IsChunk())
                    {
                        QueueThing(thing);
                        return;
                    }
                }
                foreach (IntVec3 targetCell in targetStockpile.parent.OccupiedRect().Cells)
                {
                    List <Thing> targetThings = targetCell.GetItemList(parent.Map);
                    List <Thing> sourceThings = sourceCell.GetItemList(parent.Map);
                    Thing        targetThing  = (targetThings.Count == 0) ? (null) : (targetThings[0]);
                    Thing        sourceThing  = (sourceThings.Count == 0) ? (null) : (sourceThings[0]);
                    if (sourceThing != null && targetThings.Count < sourceThings.Count - 1 &&
                        targetCell.AllowedToAccept(parent.Map, sourceThing) &&
                        sourceThing.stackCount > 0)
                    {
                        sourceCell.ThrowDustPuff(parent.Map);
                        Thing thing = GenSpawn.Spawn(sourceThing.SplitOff(sourceThing.stackCount), targetCell, parent.Map);
                        targetCell.DropSound(parent.Map, thing.def);
                        SlotGroup slotGroup = targetCell.GetSlotGroup(parent.Map);
                        if (slotGroup != null && slotGroup.parent != null)
                        {
                            slotGroup.parent.Notify_ReceivedThing(thing);
                        }
                    }
                    else if (targetThing != null && sourceThings.Count < targetThings.Count - 1 &&
                             sourceCell.AllowedToAccept(parent.Map, targetThing) &&
                             targetThing.stackCount > 0)
                    {
                        targetCell.ThrowDustPuff(parent.Map);
                        Thing thing = GenSpawn.Spawn(targetThing.SplitOff(targetThing.stackCount), sourceCell, parent.Map);
                        sourceCell.DropSound(parent.Map, thing.def);
                        SlotGroup slotGroup = sourceCell.GetSlotGroup(parent.Map);
                        if (slotGroup != null && slotGroup.parent != null)
                        {
                            slotGroup.parent.Notify_ReceivedThing(thing);
                        }
                    }
                }
            }
        }
        private void SendItemsToRelay()
        {
            CompRTQuantumRelay compRelay = compRelays[qrIndex];

            if (compRelay != null)
            {
                bool itemSent = false;
                if (compStockpiles.Count != 0)
                {
                    CompRTQuantumStockpile compStockpile = compStockpiles.RandomElement();
                    foreach (IntVec3 stockpileCell in compStockpile.parent.OccupiedRect().Cells)
                    {
                        List <Thing> stockpileThings = stockpileCell.GetItemList();
                        for (int i = 0; i < stockpileThings.Count; i++)
                        {
                            Thing stockpileThing = stockpileThings[i];
                            if (stockpileCell.AllowedToAccept(stockpileThing) &&
                                compRelay.ReceiveThing(stockpileThing))
                            {
                                itemSent = true;
                                break;
                            }
                        }
                        if (itemSent)
                        {
                            break;
                        }
                    }
                }
                if (!itemSent && compChunkSilos.Count != 0)
                {
                    CompRTQuantumChunkSilo compChunkSilo = compChunkSilos.RandomElement();
                    foreach (IntVec3 chunkSiloCell in compChunkSilo.parent.OccupiedRect().Cells)
                    {
                        List <Thing> chunkSiloThings = chunkSiloCell.GetItemList(true);
                        for (int i = 0; i < chunkSiloThings.Count; i++)
                        {
                            Thing chunkSiloThing = chunkSiloThings[i];
                            if (chunkSiloCell.AllowedToAccept(chunkSiloThing) &&
                                compRelay.ReceiveThing(chunkSiloThing))
                            {
                                itemSent = true;
                                break;
                            }
                        }
                        if (itemSent)
                        {
                            break;
                        }
                    }
                }
            }
        }
        private void DefragStockpileStacks()
        {
            CompRTQuantumStockpile sourceStockpile = compStockpiles[qsSourceIndex];
            CompRTQuantumStockpile targetStockpile = compStockpiles[qsTargetIndex];

            foreach (IntVec3 targetCell in targetStockpile.parent.OccupiedRect().Cells)
            {
                if (sparklesEnabled)
                {
                    targetCell.ThrowSparkle();
                }
                foreach (IntVec3 sourceCell in sourceStockpile.parent.OccupiedRect().Cells)
                {
                    List <Thing> targetThings = targetCell.GetItemList();
                    for (int i = 0; i < targetThings.Count(); i++)
                    {
                        bool  itemWasMoved = false;
                        Thing targetThing  = targetThings[i];
                        if (targetThing != null && targetCell.AllowedToAccept(targetThing))
                        {
                            List <Thing> sourceThings = sourceCell.GetItemList();
                            for (int j = 0; j < sourceThings.Count(); j++)
                            {
                                Thing sourceThing = sourceThings[j];
                                if (targetThing.CanAbsorb(sourceThing) &&
                                    sourceThing.stackCount < sourceThing.def.stackLimit)
                                {
                                    int targetStackCount = targetThing.stackCount;
                                    if (targetThing.TryAbsorbStack(sourceThing, true))
                                    {
                                        sourceCell.ThrowDustPuff();
                                    }
                                    if (targetStackCount != targetThing.stackCount)
                                    {
                                        ForbidUtility.SetForbidden(targetThing, false, false);
                                        targetCell.DropSound(targetThing.def);
                                    }
                                    itemWasMoved = true;
                                    break;
                                }
                            }
                        }
                        if (itemWasMoved)
                        {
                            break;
                        }
                    }
                }
            }
        }
        private void SortOutComps()
        {
            if (!switchedToChunkSilo)
            {
                CompRTQuantumChunkSilo compChunkSilo = parent.TryGetComp <CompRTQuantumChunkSilo>();
                if (compChunkSilo != null)
                {
                    foreach (ThingComp comp in parent.AllComps)
                    {
                        if (comp is CompRTQuantumChunkSilo)
                        {
                            comp.PostDeSpawn();
                        }
                    }
                    parent.AllComps.RemoveAll(x => x is CompRTQuantumChunkSilo);
                    parent.def.comps.RemoveAll(x => x.compClass.ToString().Equals("RT_QuantumStorage.CompRTQuantumChunkSilo"));
                }
                if (parent.TryGetComp <CompRTQuantumStockpile>() == null)
                {
                    CompProperties_RTQuantumStockpile compPropsStockpile = new CompProperties_RTQuantumStockpile();
                    compPropsStockpile.compClass = typeof(CompRTQuantumStockpile);
                    List <CompProperties> componentsBackup = new List <CompProperties>();
                    foreach (CompProperties compProperties in parent.def.comps)
                    {
                        componentsBackup.Add(compProperties);
                    }
                    parent.def.comps.Clear();
                    parent.def.comps.Add(compPropsStockpile);
                    parent.InitializeComps();
                    foreach (CompProperties compProperties in componentsBackup)
                    {
                        parent.def.comps.Add(compProperties);
                    }

                    CompRTQuantumStockpile compStockpile = parent.TryGetComp <CompRTQuantumStockpile>();
                    compStockpile.PostSpawnSetup();
                    compStockpile.PostExposeData();
                }
            }
            else
            {
                CompRTQuantumStockpile compStockpile = parent.TryGetComp <CompRTQuantumStockpile>();
                if (compStockpile != null)
                {
                    foreach (ThingComp comp in parent.AllComps)
                    {
                        if (comp is CompRTQuantumStockpile)
                        {
                            comp.PostDeSpawn();
                        }
                    }
                    parent.AllComps.RemoveAll(x => x is CompRTQuantumStockpile);
                    parent.def.comps.RemoveAll(x => x.compClass.ToString().Equals("RT_QuantumStorage.CompRTQuantumStockpile"));
                }
                if (parent.TryGetComp <CompRTQuantumChunkSilo>() == null)
                {
                    CompProperties_RTQuantumChunkSilo compPropsChunkSilo = new CompProperties_RTQuantumChunkSilo();
                    compPropsChunkSilo.compClass = typeof(CompRTQuantumChunkSilo);
                    List <CompProperties> componentsBackup = new List <CompProperties>();
                    foreach (CompProperties compProperties in parent.def.comps)
                    {
                        componentsBackup.Add(compProperties);
                    }
                    parent.def.comps.Clear();
                    parent.def.comps.Add(compPropsChunkSilo);
                    parent.InitializeComps();
                    foreach (CompProperties compProperties in componentsBackup)
                    {
                        parent.def.comps.Add(compProperties);
                    }

                    CompRTQuantumChunkSilo compChunkSilo = parent.TryGetComp <CompRTQuantumChunkSilo>();
                    compChunkSilo.PostSpawnSetup();
                    compChunkSilo.PostExposeData();
                }
            }
        }
 public void DeRegisterStockpile(CompRTQuantumStockpile compStockpile)
 {
     compStockpiles.Remove(compStockpile);
 }
        private void QuantumWarehouseTick(int tickAmount)
        {
            if ((Find.TickManager.TicksGame + tickStagger) % tickAmount == 0)
            {
                if (compPowerTrader == null || compPowerTrader.PowerOn)
                {
                    if (parent.OccupiedRect().CenterCell.Priority() == StoragePriority.Unstored)
                    {
                        valid = false;
                    }
                    if (valid)
                    {
                        if (compStockpiles.Count > 1)
                        {
                            qsSourceIndex++;
                            if (qsSourceIndex >= compStockpiles.Count)
                            {
                                qsTargetIndex++;
                                qsSourceIndex = qsTargetIndex + 1;
                            }
                            if (qsTargetIndex >= compStockpiles.Count - 1)
                            {
                                compStockpiles.Shuffle();
                                qsTargetIndex = 0;
                                qsSourceIndex = 1;
                            }
                            DefragStockpileStacks();
                            DefragStockpileCells();
                        }
                        else if (compStockpiles.Count == 1)
                        {
                            CompRTQuantumStockpile compStockpile = compStockpiles.First();
                            if (sparklesEnabled)
                            {
                                foreach (IntVec3 stockpileCell in compStockpile.parent.OccupiedRect().Cells)
                                {
                                    stockpileCell.ThrowSparkle();
                                }
                            }
                            compStockpile.DefragStacks();
                        }

                        if (compChunkSilos.Count > 1)
                        {
                            qcsSourceIndex++;
                            if (qcsSourceIndex >= compChunkSilos.Count)
                            {
                                qcsTargetIndex++;
                                qcsSourceIndex = qcsTargetIndex + 1;
                            }
                            if (qcsTargetIndex >= compChunkSilos.Count - 1)
                            {
                                compChunkSilos.Shuffle();
                                qcsTargetIndex = 0;
                                qcsSourceIndex = 1;
                            }
                            DefragChunkSilos();
                        }
                        else if (compChunkSilos.Count == 1)
                        {
                            CompRTQuantumChunkSilo compChunkSilo = compChunkSilos.First();
                            foreach (IntVec3 chunkSiloCell in compChunkSilo.parent.OccupiedRect().Cells)
                            {
                                if (sparklesEnabled)
                                {
                                    chunkSiloCell.ThrowSparkle();
                                }
                                List <Thing> cellThings = chunkSiloCell.GetItemList();
                                if (cellThings.Count != 0)
                                {
                                    QueueThing(cellThings.RandomElement());
                                }
                            }
                        }

                        if (compRelays.Count != 0)
                        {
                            if (qrIndex >= compRelays.Count)
                            {
                                qrIndex = 0;
                            }
                            SendItemsToRelay();
                            qrIndex++;
                        }

                        if (buffer.Count != 0)
                        {
                            Thing thing = buffer.RandomElement();
                            ReceiveThing(thing);
                            buffer.Remove(thing);
                            if (buffer.Count != 0)
                            {
                                if (bufferTicker == bufferLifespan)
                                {
                                    bufferTicker = 0;
                                    buffer.Clear();
                                }
                                bufferTicker++;
                            }
                        }
                    }
                    else
                    {
                        CompRTQuantumWarehouse compWarehouse = parent.FindWarehouse();
                        valid = (compWarehouse != null && compWarehouse == this &&
                                 parent.OccupiedRect().CenterCell.Priority() != StoragePriority.Unstored);
                    }
                }
                else
                {
                    valid = false;
                }
            }
        }
 public void RegisterStockpile(CompRTQuantumStockpile compStockpile)
 {
     if (!compStockpiles.Contains(compStockpile))
     {
         compStockpiles.Add(compStockpile);
     }
 }
 public void DeRegisterStockpile(CompRTQuantumStockpile compStockpile)
 {
     compStockpiles.Remove(compStockpile);
 }