public IMyCubeGrid GetTarget()
        {
            //var turretBase = Entity as Sandbox.ModAPI.IMyLargeTurretBase;
            //var fixedWeapon = Entity as Sandbox.ModAPI.IMyUserControllableGun;

            //if (turretBase != null)
            //{
            //    target = turretBase.Target;
            //}

            try {
                MyEntitySubpart subpart1 = cubeBlock.GetSubpart("GatlingTurretBase1");
                MyEntitySubpart subpart2 = subpart1.GetSubpart("GatlingTurretBase2");

                if (cubeBlock == null || cubeBlock.CubeGrid == null || subpart1 == null || subpart2 == null || subpart1.WorldMatrix == null || subpart2.WorldMatrix == null)
                {
                    return(null);
                }

                var from = subpart2.WorldMatrix.Translation + subpart2.WorldMatrix.Forward * 0.3d;
                var to   = subpart2.WorldMatrix.Translation + subpart2.WorldMatrix.Forward * 800d;

                LineD ray = new LineD(from, to);
                List <MyLineSegmentOverlapResult <MyEntity> > result = new List <MyLineSegmentOverlapResult <MyEntity> >();
                MyGamePruningStructure.GetTopmostEntitiesOverlappingRay(ref ray, result, MyEntityQueryType.Both);

                foreach (var resultItem in result)
                {
                    if (resultItem.Element == null)
                    {
                        continue;
                    }

                    if (resultItem.Element.EntityId != cubeBlock.CubeGrid.EntityId)
                    {
                        if (resultItem.Element is IMyCubeGrid)
                        {
                            return(resultItem.Element as IMyCubeGrid);
                        }
                    }
                }
            }
            catch (KeyNotFoundException)
            {
            }
            return(null);
        }
        public override void UpdateBeforeSimulation()
        {
            int chargesInInventory = (int)m_inventory.GetItemAmount(chargeDefinitionIds [damageUpgrades]);

            IMyCubeBlock cube = Entity as IMyCubeBlock;

            long currentShootTime = ((MyObjectBuilder_InteriorTurret)cube.GetObjectBuilderCubeBlock()).GunBase.LastShootTime;


            if (currentHeat > 0f)
            {
                if ((ticks - lastShootTimeTicks) > beamWeaponInfo.heatDissipationDelay)
                {
                    currentHeat -= beamWeaponInfo.heatDissipationPerTick;

                    if (currentHeat <= 0f)
                    {
                        currentHeat = 0f;

                        overheated = false;
                    }
                }
            }

            //MyAPIGateway.Utilities.ShowNotification("TIME: " + currentShootTime + " :: " + lastShootTime, 17, MyFontEnum.Red);


            if (currentShootTime != lastShootTime)
            {
                // test

                hitBool = false;

                MyEntitySubpart subpart1 = cubeBlock.GetSubpart("InteriorTurretBase1");
                MyEntitySubpart subpart2 = subpart1.GetSubpart("InteriorTurretBase2");;
                //MyAPIGateway.Utilities.ShowNotification("Dif: " + (currentShootTime - lastShootTime), 17, MyFontEnum.Blue);

                from = subpart2.WorldMatrix.Translation + subpart2.WorldMatrix.Forward * 1.25d;
                to   = subpart2.WorldMatrix.Translation + subpart2.WorldMatrix.Forward * 3000d;

                LineD testRay = new LineD(from, to);

                List <MyLineSegmentOverlapResult <MyEntity> > result = new List <MyLineSegmentOverlapResult <MyEntity> >();

                MyGamePruningStructure.GetAllEntitiesInRay(ref testRay, result);


                foreach (var resultItem in result)
                {
                    IMyCubeGrid grid = resultItem.Element as IMyCubeGrid;

                    IMyDestroyableObject destroyableEntity = resultItem.Element as IMyDestroyableObject;

                    if (grid != null)
                    {
                        IMySlimBlock slimblock;

                        double hitd;

                        Vector3D?resultVec = grid.GetLineIntersectionExactAll(ref testRay, out hitd, out slimblock);

                        if (resultVec != null)
                        {
                            hitBool = true;

                            toTarget = from + subpart2.WorldMatrix.Forward * hitd;

                            if (!MyAPIGateway.Session.CreativeMode)
                            {
                                slimblock.DoDamage(beamWeaponInfo.damage * (currentHeat / beamWeaponInfo.maxHeat + 0.2f), MyStringHash.GetOrCompute("Laser"), false, default(MyHitInfo), cubeBlock.EntityId);
                            }
                            else
                            {
                                slimblock.DoDamage(beamWeaponInfo.damage * 1.2f, MyStringHash.GetOrCompute("Laser"), false, default(MyHitInfo), cubeBlock.EntityId);
                            }
                            //MyAPIGateway.Utilities.ShowNotification("" + s.BlockDefinition.Id.SubtypeId + " ::: " + resultItem.Distance, 17);
                        }
                    }
                    if (destroyableEntity != null)
                    {
                        IMyEntity ent  = (IMyEntity)destroyableEntity;
                        double    hitd = (from - ent.WorldMatrix.Translation).Length();

                        toTarget = from + subpart2.WorldMatrix.Forward * hitd;

                        hitBool = true;

                        if (!MyAPIGateway.Session.CreativeMode)
                        {
                            destroyableEntity.DoDamage(beamWeaponInfo.damage * (currentHeat / beamWeaponInfo.maxHeat + 0.2f), MyStringHash.GetOrCompute("Laser"), false, default(MyHitInfo), cubeBlock.EntityId);
                        }
                        else
                        {
                            destroyableEntity.DoDamage(beamWeaponInfo.damage * 1.2f, MyStringHash.GetOrCompute("Laser"), false, default(MyHitInfo), cubeBlock.EntityId);
                        }
                    }
                }


                // test

                lastShootTime      = currentShootTime;
                lastShootTimeTicks = ticks;

                currentHeat += beamWeaponInfo.heatPerTick;

                if (currentHeat > beamWeaponInfo.maxHeat)
                {
                    currentHeat = beamWeaponInfo.maxHeat;

                    overheated = true;
                }
            }

            if (ticks - lastShootTimeTicks < 3)
            {
                var beamcolor     = Color.DodgerBlue;
                var beamcolor_aux = Color.LightSkyBlue;
                var maincolor     = new Vector4(beamcolor.X / 30, beamcolor.Y / 30, beamcolor.Z / 30, 1f);
                var auxcolor      = new Vector4(beamcolor_aux.X / 30, beamcolor_aux.Y / 30, beamcolor_aux.Z / 30, 1f);
                var material      = MyStringId.GetOrCompute("WeaponLaser");
                if (hitBool == false)
                {
                    if (!MyAPIGateway.Utilities.IsDedicated)
                    {
                        if (!MyAPIGateway.Session.CreativeMode)
                        {
                            VRage.Game.MySimpleObjectDraw.DrawLine(from, to, material, ref auxcolor, 0.30f);
                            VRage.Game.MySimpleObjectDraw.DrawLine(from, to, material, ref maincolor, 1.0f);
                        }
                        else
                        {
                            VRage.Game.MySimpleObjectDraw.DrawLine(from, to, material, ref auxcolor, 0.30f * 1.2f);
                            VRage.Game.MySimpleObjectDraw.DrawLine(from, to, material, ref maincolor, 1.0f * 1.2f);
                        }
                    }
                }
                else
                {
                    if (!MyAPIGateway.Utilities.IsDedicated)
                    {
                        if (!MyAPIGateway.Session.CreativeMode)
                        {
                            VRage.Game.MySimpleObjectDraw.DrawLine(from, toTarget, material, ref auxcolor, 0.30f);
                            VRage.Game.MySimpleObjectDraw.DrawLine(from, toTarget, material, ref maincolor, 1.0f);
                        }
                        else
                        {
                            VRage.Game.MySimpleObjectDraw.DrawLine(from, toTarget, material, ref auxcolor, 0.30f * 1.2f);
                            VRage.Game.MySimpleObjectDraw.DrawLine(from, toTarget, material, ref maincolor, 1.0f * 1.2f);
                        }
                    }
                }
            }

            if (chargesInInventory < beamWeaponInfo.keepAtCharge)
            {
                if (resourceSink.RequiredInputByType(electricityDefinition) != (beamWeaponInfo.powerUsage / efficiencyUpgrades))
                {
                    resourceSink.SetRequiredInputByType(electricityDefinition, (beamWeaponInfo.powerUsage / efficiencyUpgrades));

                    setPowerConsumption = (beamWeaponInfo.powerUsage / efficiencyUpgrades);
                    powerConsumption    = (beamWeaponInfo.powerUsage / efficiencyUpgrades);
                }
                else
                {
                    if (!functionalBlock.Enabled)
                    {
                        powerConsumption = 0.0001f;
                    }
                }

                if (resourceSink.CurrentInputByType(electricityDefinition) == (beamWeaponInfo.powerUsage / efficiencyUpgrades))
                {
                    if (!overheated)
                    {
                        m_inventory.AddItems((MyFixedPoint)(beamWeaponInfo.keepAtCharge - chargesInInventory), chargeObjectBuilders [damageUpgrades]);
                    }
                }
            }
            else if (chargesInInventory > beamWeaponInfo.keepAtCharge)
            {
                m_inventory.RemoveItemsOfType((MyFixedPoint)(chargesInInventory - beamWeaponInfo.keepAtCharge), chargeObjectBuilders [damageUpgrades]);
            }
            else
            {
                if (setPowerConsumption != 0.0001f)
                {
                    resourceSink.SetRequiredInputByType(electricityDefinition, 0.0001f);

                    setPowerConsumption = 0.0001f;
                    powerConsumption    = 0.0001f;
                }
            }

            terminalBlock.RefreshCustomInfo();

            ticks++;
        }