public void TrySetConnectedTeleporter(Building_Teleporter teleporter)
 {
     if (teleporter != null && teleporter != this)
     {
         this.connectedTeleporter = teleporter;
     }
 }
        private Building_Teleporter TryFindOtherTeleporter()
        {
            // Find all teleporter
            IEnumerable <Building_Teleporter> buildings = Find.ListerBuildings.AllBuildingsColonistOfClass <Building_Teleporter>();

            if (buildings == null || buildings.Count() == 0)
            {
                return(null);
            }

            // remove this teleporter
            IEnumerable <Building_Teleporter> buildings1 = buildings.Where(b => b != this);

            if (buildings1 == null || buildings1.Count() == 0)
            {
                return(null);
            }

            Building_Teleporter building = buildings.RandomElement();

            return(building);
        }
        // Do ticker work
        private void DoTickWork(int ticks)
        {
            // not off, not connected => try to find other teleporter
            if (ConnectedTeleporter == null)
            {
                TrySetConnectedTeleporter(TryFindOtherTeleporter());

                // nothing found, set to off
                if (ConnectedTeleporter == null)
                {
                    TrySwitchStateToOff();
                }
            }

            if (!PowerOk)
            {
                TrySwitchStateToOff();
                return;
            }

            // State: not sending => Do nothing more
            if (State != TeleporterState.send)
            {
                if (counterNextCheck != counterNextCheckMax)
                {
                    counterNextCheck = counterNextCheckMax;
                }

                return;
            }

            // Send items only after x ticks
            if (countdownTeleportingActive && countdownTeleporting > 0)
            {
                countdownTeleporting -= ticks;
                return;
            }

            // Check for new items only every x ticks
            if (!countdownTeleportingActive && counterNextCheck > 0)
            {
                counterNextCheck -= ticks;
                return;
            }
            else
            {
                counterNextCheck = counterNextCheckMax;
            }

            // State connected building: not receiving
            Building_Teleporter target = ConnectedTeleporter;

            if (target == null || target.State != TeleporterState.receive)
            {
                return;
            }

            // Check if there is an item on my slots, send if possible to target
            List <IntVec3> allCells    = AllSlotCellsList();
            List <IntVec3> targetCells = target.AllSlotCellsList();

            for (int i = 0; i < allCells.Count; i++)
            {
                IntVec3 sourceCell = allCells[i];
                IntVec3 targetCell = targetCells[i];


                // Check if resource is here and check if no resource is at target position
                if (Find.ThingGrid.CellContains(sourceCell, ThingCategory.Item))
                {
                    // Check if target cell is empty
                    if (Find.ThingGrid.CellContains(targetCell, ThingCategory.Item))
                    {
                        continue;
                    }

                    // Start sending countdown
                    if (!countdownTeleportingActive)
                    {
                        countdownTeleporting       = countdownTeleportingMax;
                        countdownTeleportingActive = true;
                        break;
                    }
                    else
                    {
                        // Send to target position
                        IEnumerable <Thing> sourceCellThings = Find.ThingGrid.ThingsAt(sourceCell);
                        foreach (Thing t in sourceCellThings)
                        {
                            if (t.def.category == ThingCategory.Item)
                            {
                                // Create identical item at target
                                Thing targetThing = ThingMaker.MakeThing(t.def, t.Stuff);
                                targetThing            = GenSpawn.Spawn(targetThing, targetCell);
                                targetThing.stackCount = t.stackCount;

                                // Destroy source
                                t.Destroy(DestroyMode.Vanish);
                                break;
                            }
                        }
                    }
                }
            }

            if (countdownTeleportingActive && countdownTeleporting <= 0)
            {
                countdownTeleportingActive = false;
            }
        }