public void RegisterChunkSilo(CompRTQuantumChunkSilo compChunkSilo)
 {
     if (!compChunkSilos.Contains(compChunkSilo))
     {
         compChunkSilos.Add(compChunkSilo);
     }
 }
        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;
                        }
                    }
                }
            }
        }
 public override IEnumerable <Command> CompGetGizmosExtra()
 {
     if (canSwitchModes)
     {
         Command_Action commandModeSwitch = new Command_Action();
         commandModeSwitch.groupKey = 676192;
         if (switchedToChunkSilo)
         {
             commandModeSwitch.defaultLabel = "CompRTDeviceModeSwitcher_StockpileModeLabel".Translate();
             commandModeSwitch.defaultDesc  = "CompRTDeviceModeSwitcher_StockpileModeDesc".Translate();
             commandModeSwitch.icon         = Resources.stockpileTexture;
         }
         else
         {
             commandModeSwitch.defaultLabel = "CompRTDeviceModeSwitcher_ChunkSiloModeLabel".Translate();
             commandModeSwitch.defaultDesc  = "CompRTDeviceModeSwitcher_ChunkSiloModeDesc".Translate();
             commandModeSwitch.icon         = Resources.chunkSiloTexture;
         }
         commandModeSwitch.action = () =>
         {
             CompFlickable compFlickable = parent.TryGetComp <CompFlickable>();
             if (compFlickable != null)
             {
                 compFlickable.ResetToOn();
                 compFlickable.DoFlick();
                 FlickUtility.UpdateFlickDesignation(parent);
             }
             if (switchedToChunkSilo)
             {
                 CompRTQuantumChunkSilo compChunkSilo = parent.TryGetComp <CompRTQuantumChunkSilo>();
                 if (compChunkSilo != null)
                 {
                     compChunkSilo.EmptyOut();
                 }
             }
             switchedToChunkSilo = !switchedToChunkSilo;
             SortOutComps();
         };
         yield return(commandModeSwitch);
     }
 }
        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 DeRegisterChunkSilo(CompRTQuantumChunkSilo compChunkSilo)
 {
     compChunkSilos.Remove(compChunkSilo);
 }
        private void DefragChunkSilos()
        {
            CompRTQuantumChunkSilo sourceChunkSilo = compChunkSilos[qcsSourceIndex];
            CompRTQuantumChunkSilo targetChunkSilo = compChunkSilos[qcsTargetIndex];

            if (sourceChunkSilo.ChunkTotalFast() > targetChunkSilo.ChunkTotalFast() + 1)
            {
                bool chunkMoved = false;
                foreach (IntVec3 sourceCell in sourceChunkSilo.parent.OccupiedRect().Cells)
                {
                    if (sparklesEnabled)
                    {
                        sourceCell.ThrowSparkle();
                    }
                    List <Thing> sourceThings = sourceCell.GetItemList(true);
                    foreach (Thing sourceThing in sourceThings)
                    {
                        if (sourceThing.IsChunk())
                        {
                            if (!chunkMoved)
                            {
                                targetChunkSilo.ReceiveThing(sourceThing);
                                chunkMoved = true;
                                break;
                            }
                        }
                        else
                        {
                            QueueThing(sourceThing);
                        }
                    }
                }
                if (sparklesEnabled)
                {
                    foreach (IntVec3 targetCell in targetChunkSilo.parent.OccupiedRect().Cells)
                    {
                        targetCell.ThrowSparkle();
                    }
                }
            }
            else
            {
                foreach (IntVec3 sourceCell in sourceChunkSilo.parent.OccupiedRect().Cells)
                {
                    if (sparklesEnabled)
                    {
                        sourceCell.ThrowSparkle();
                    }
                    List <Thing> sourceThings = sourceCell.GetItemList();
                    if (sourceThings.Count != 0)
                    {
                        QueueThing(sourceThings.RandomElement());
                    }
                }
                if (sparklesEnabled)
                {
                    foreach (IntVec3 targetCell in targetChunkSilo.parent.OccupiedRect().Cells)
                    {
                        targetCell.ThrowSparkle();
                    }
                }
            }
        }
        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 RegisterChunkSilo(CompRTQuantumChunkSilo compChunkSilo)
 {
     if (!compChunkSilos.Contains(compChunkSilo))
     {
         compChunkSilos.Add(compChunkSilo);
     }
 }
 public void DeRegisterChunkSilo(CompRTQuantumChunkSilo compChunkSilo)
 {
     compChunkSilos.Remove(compChunkSilo);
 }