/// <summary>
        /// Adds weapon
        /// </summary>
        /// <param name="weapon">Weapon</param>
        /// <param name="weaponObjectBuilder">Weapon's object builder</param>
        /// <param name="maxWeapons"> </param>
        private void AddWeapon(MySmallShipGunBase weapon, MyMwcObjectBuilder_SmallShip_Weapon weaponObjectBuilder)
        {
            SetAutoMountIfExceededLimit(weaponObjectBuilder);

            MyWeaponSlot emptyWeaponSlot = GetEmptySlotToAdd(weaponObjectBuilder);

            if (emptyWeaponSlot != null)
            {
                switch (emptyWeaponSlot.SlotLocation)
                {
                case MySlotLocationEnum.None:
                    break;

                case MySlotLocationEnum.LeftSide:
                    weaponObjectBuilder.SetAutoMountLeft();
                    break;

                case MySlotLocationEnum.RightSide:
                    weaponObjectBuilder.SetAutoMountRight();
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }

                AddWeaponToSlot(emptyWeaponSlot, weapon, weaponObjectBuilder);
            }
        }
        /// <summary>
        /// Removes weapon from ship
        /// </summary>
        /// <param name="index">Index of slot</param>
        public void RemoveWeapon(int index)
        {
            if (index >= WeaponSlots.Count)
            {
                throw new ArgumentOutOfRangeException("index");
            }
            MyWeaponSlot weaponSlot = WeaponSlots[index];

            if (weaponSlot.IsMounted())
            {
                RemoveWeaponFromSlotAndFromShip(weaponSlot);
            }
        }
        /// <summary>
        /// Initialize slots by small ship's model and preferred weapon's positions
        /// </summary>
        /// <param name="shipTypeProperties">Type properties of ship</param>
        /// <param name="maxSlots">Maximum weapons that can be mounted</param>
        private void InitSlots(MyShipTypeProperties shipTypeProperties, int maxSlots)
        {
            // initialize slot for drill
            DrillSlot = new MyWeaponSlot();
            DrillSlot.WeaponSubObject     = MyModelSubObjects.GetModelSubObject(shipTypeProperties.Visual.ModelLod0Enum, DRILL_NAME);
            DrillSlot.OnWeaponMounting   += m_weaponMounting;
            DrillSlot.OnWeaponDismouting += m_weaponDismounting;

            // initialize slot for harvester
            HarvestingDeviceSlot = new MyWeaponSlot();
            HarvestingDeviceSlot.WeaponSubObject     = MyModelSubObjects.GetModelSubObject(shipTypeProperties.Visual.ModelLod0Enum, HARVESTER_NAME);
            HarvestingDeviceSlot.OnWeaponMounting   += m_weaponMounting;
            HarvestingDeviceSlot.OnWeaponDismouting += m_weaponDismounting;

            // initialize slots for weapons
            WeaponSlots = new List <MyWeaponSlot>();
            AddGunsPositionsFromShipModel(shipTypeProperties.Visual.ModelLod0Enum, maxSlots);
        }
        /// <summary>
        /// Creates gun's positions from ship's model. If find preferred position, then mark it as preffered.
        /// </summary>
        /// <param name="modelEnum">Ship's model</param>
        /// <param name="maxSlots">Weapons maximum on ship</param>
        /// <param name="maxWeapons"> </param>
        private void AddGunsPositionsFromShipModel(MyModelsEnum modelEnum, int maxSlots)
        {
            List <MyModelSubObject> shipsSubObjects = MyModelSubObjects.GetModelSubObjects(modelEnum);

            if (shipsSubObjects != null)
            {
                for (int i = 0; i < maxSlots; i++)
                {
                    string gunPrefix = GUN_PREFIX_NAME + (i + 1).ToString("##00");

                    // try find guns subobject by prefix (GUN_XX)
                    MyModelSubObject subObjectGun = shipsSubObjects.Find(x => x.Name.StartsWith(gunPrefix));

                    // if not found, then stop adding gun's positions, because there are no gun's subobjects
                    if (subObjectGun == null)
                    {
                        return;
                    }

                    // if not exists slot at this index, then create new one
                    if (WeaponSlots.Count <= i)
                    {
                        MyWeaponSlot newWeaponSlot = new MyWeaponSlot();
                        WeaponSlots.Add(newWeaponSlot);
                        newWeaponSlot.OnWeaponMounting   += m_weaponMounting;
                        newWeaponSlot.OnWeaponDismouting += m_weaponDismounting;
                    }

                    WeaponSlots[i].WeaponSubObject = subObjectGun;

                    // if this gun's position is preffered by any gun
                    if (subObjectGun.Name.Length != gunPrefix.Length)
                    {
                        string prefferedGunName = subObjectGun.Name.Substring(gunPrefix.Length + 1, subObjectGun.Name.Length - gunPrefix.Length - 1);
                        MyMwcObjectBuilder_SmallShip_Weapon_TypesEnum weaponType;
                        if (m_weaponsModelNames.TryGetValue(prefferedGunName, out weaponType))
                        {
                            WeaponSlots[i].WeaponSubObject.AuxiliaryParam0 = (int)weaponType;
                        }
                    }
                }
            }
        }
 private void OnWeaponDismounting(MyWeaponSlot sender, MySmallShipGunBase weapon)
 {
     m_allMountedWeapons.Remove(weapon);
 }
 private void OnWeaponMounting(MyWeaponSlot sender, MySmallShipGunBase weapon)
 {
     m_allMountedWeapons.Add(weapon);
 }
 /// <summary>
 /// Removes weapons from slot and from ship child entities
 /// </summary>
 /// <param name="weaponSlot">Weapon's slot</param>
 private void RemoveWeaponFromSlotAndFromShip(MyWeaponSlot weaponSlot)
 {
     weaponSlot.MountedWeapon.MarkForClose();
     weaponSlot.Dismount();
 }
 /// <summary>
 /// Adds weapon to slot
 /// </summary>
 /// <param name="weaponSlot">Weapon's slot</param>
 /// <param name="weapon">Weapon</param>
 /// <param name="weaponObjectBuilder">Weapon's object builder</param>
 private void AddWeaponToSlot(MyWeaponSlot weaponSlot, MySmallShipGunBase weapon, MyMwcObjectBuilder_SmallShip_Weapon weaponObjectBuilder)
 {
     weaponSlot.InitAndMount(weapon, weaponObjectBuilder, Ship);
 }