public void maintainConnection()
        {
            //MyAPIGateway.Utilities.ShowNotification ("MAINTAIN", 1000, MyFontEnum.White);

            if (m_target != null)
            {
                if (m_target.functionalBlock == null)
                {
                    //MyAPIGateway.Utilities.ShowNotification ("TARGET REMOVED", 1000, MyFontEnum.White);
                    m_target = null;
                }
            }

            if (m_target != null)
            {
                //MyAPIGateway.Utilities.ShowNotification ("GOT TARGET", 1000, MyFontEnum.White);

                if (m_targetIdOld != m_target.id)
                {
                    m_target = null;
                }
                else
                {
                    m_targetIdOld = m_target.id;
                }
            }

            if (m_target == null)
            {
                //MyAPIGateway.Utilities.ShowNotification ("LF TARGET", 1000, MyFontEnum.White);
                foreach (var transmitter in TransmissionManager.opticalTransmitters)
                {
                    //MyAPIGateway.Utilities.ShowNotification ("CHECK " + transmitter.Value.id + " " + m_targetId, 1000, MyFontEnum.White);
                    if (transmitter.Value.id == m_targetId)
                    {
                        if (!transmitter.Value.sender)
                        {
                            if (transmitter.Value.functionalBlock.Enabled)
                            {
                                //MyAPIGateway.Utilities.ShowNotification ("GOT TARGET", 1000, MyFontEnum.White);
                                m_target      = transmitter.Value;
                                m_targetIdOld = m_target.id;
                            }
                        }
                    }

                    //MyAPIGateway.Utilities.ShowNotification ("COUNT: " + TransmissionManager.opticalTransmitters.Count, 1667);
                }
            }


            if (m_target != null)
            {
                //MyAPIGateway.Utilities.ShowNotification ("RECEIVER: " + m_target.functionalBlock.EntityId, 1000, MyFontEnum.White);

                m_targetVisible = false;
                Vector3D thisPosition   = m_functionalBlock.GetPosition();
                Vector3D targetPosition = m_target.functionalBlock.GetPosition();

                if (m_transmittedPower != m_oldTransmittedPower)
                {
                    m_currentRequiredInput = m_transmittedPower;

                    m_resourceSink.SetRequiredInputByType(m_electricityDefinition, m_transmittedPower);
                    //MyAPIGateway.Utilities.ShowNotification ("COUNT: " + TransmissionManager.opticalTransmitters.Count, 3000, MyFontEnum.Red);
                    m_oldTransmittedPower = m_transmittedPower;
                }

                if (m_target.enabled)
                {
                    double distance = Vector3D.DistanceSquared(thisPosition, targetPosition);

                    //MyAPIGateway.Utilities.ShowNotification ("RANGE: " + distance + ":" + m_maxRangeSquared, 1000, MyFontEnum.White);

                    if (distance < m_maxRangeSquared)
                    {
                        Vector3D direction = targetPosition - thisPosition;
                        direction.Normalize();

                        //MyAPIGateway.Utilities.ShowNotification ("VEC: " + thisPosition + ":" + targetPosition + ":" + direction * 20, 1000, MyFontEnum.White);

                        if (!MyAPIGateway.Entities.IsRaycastBlocked(thisPosition + direction * m_rayOffset, targetPosition - direction * m_target.rayOffset))
                        {
                            m_targetVisible = true;

                            //MyAPIGateway.Utilities.ShowNotification ("" + m_target.functionalBlock.CubeGrid.EntityId + " VISIBLE", 1000);

                            return;
                        }
                        else
                        {
                            m_targetVisible = false;
                            //MyAPIGateway.Utilities.ShowNotification ("BLOCKED", 1000, MyFontEnum.Red);
                        }
                    }
                }
            }

            m_targetVisible = false;

            if (m_currentRequiredInput != 0)
            {
                m_resourceSink.SetRequiredInputByType(m_electricityDefinition, 0);
                m_oldTransmittedPower  = 0;
                m_currentRequiredInput = 0;
            }
        }
        public override void UpdateBeforeSimulation()
        {
            base.UpdateBeforeSimulation();
            //MyAPIGateway.Utilities.ShowNotification ("YEP: " + m_subtypeName, 17);

            if (m_functionalBlock.IsFunctional)
            {
                if (m_functionalBlock.Enabled)
                {
                    m_info.enabled = true;
                }
                else
                {
                    m_info.enabled = false;
                }
            }
            else
            {
                m_info.enabled = false;
            }

            if (!m_sender)
            {
                if (m_currentRequiredInput != 0)
                {
                    m_resourceSink.SetRequiredInputByType(m_electricityDefinition, 0);
                    m_oldTransmittedPower  = 0;
                    m_currentRequiredInput = 0;
                }

                m_target = null;
            }
            else if (m_functionalBlock.Enabled && m_functionalBlock.IsFunctional)
            {
                if (m_target != null)
                {
                    if (m_target.functionalBlock == null)
                    {
                        m_target = null;
                    }
                    else
                    {
                        if (!m_target.functionalBlock.Enabled)
                        {
                            m_target = null;
                        }
                    }
                }

                if (m_ticks % 100 == 0)
                {
                    if (m_sender)
                    {
                        maintainConnection();
                    }

                    m_targetIdOld = m_targetId;
                }

                if (m_target != null)
                {
                    if (m_targetVisible)
                    {
                        m_currentOutput = m_resourceSink.CurrentInputByType(m_electricityDefinition);

                        m_powerToTransfer = m_currentOutput * m_powerMultiplicator;

                        var transmitterComponent = m_target.functionalBlock.GameLogic.GetAs <OpticalPowerTransmitter>();



                        if ((transmitterComponent.m_receivingPower + m_powerToTransfer) > m_target.strength)
                        {
                            m_powerToTransfer = m_target.strength - transmitterComponent.m_receivingPower;
                        }

                        transmitterComponent.m_receivingPower += m_powerToTransfer;
                        transmitterComponent.m_senders++;
                        //MyAPIGateway.Utilities.ShowNotification ("ADD: " + m_target.functionalBlock.CubeGrid.EntityId + ":" + powerToTransfer, 17, MyFontEnum.DarkBlue);
                        TransmissionManager.totalPowerPerGrid [m_target.functionalBlock.CubeGrid.EntityId] = TransmissionManager.totalPowerPerGrid [m_target.functionalBlock.CubeGrid.EntityId] + m_powerToTransfer;
                    }
                }
            }
            else
            {
                m_target = null;

                if (m_currentRequiredInput != 0)
                {
                    m_resourceSink.SetRequiredInputByType(m_electricityDefinition, 0);
                    m_oldTransmittedPower  = 0;
                    m_currentRequiredInput = 0;
                }
            }

            if (m_ticks % 100 == 0)
            {
                m_terminalBlock.RefreshCustomInfo();
            }

            if (m_sender)
            {
                m_saveInfo.ChannelTarget = m_targetId;
            }
            else
            {
                m_saveInfo.ChannelTarget = m_id;
            }

            m_saveInfo.Sender = m_sender;
            m_saveInfo.Power  = m_transmittedPower;

            m_receivingPower = 0f;
            m_senders        = 0;



            m_ticks++;
        }