Example #1
0
        private void ChangeShip(ShipType shipType, LoadoutBase loadOut)
        {
            _waitingForLaunchToComplete = true;

            Log($"\tChanging ship to {shipType}");

            var side      = ClientConnection.GetSide();
            var ship      = ClientConnection.GetShip();
            var mission   = side.GetMission();
            var hullTypes = mission.GetHullTypes();
            var station   = ship.GetStation();

            if (station == null)
            {
                return;
            }

            string targetHullTypeName = GetHullTypeString(shipType);

            List <AllegianceInterop.IhullTypeIGCWrapper> buyableHullTypes = new List <AllegianceInterop.IhullTypeIGCWrapper>();

            foreach (var hullType in hullTypes)
            {
                if (hullType.GetGroupID() >= 0 && station.CanBuy(hullType) == true && station.IsObsolete(hullType) == false)
                {
                    Log("\t\tbuyable hullType: " + hullType.GetName());
                    buyableHullTypes.Add(hullType);
                }
            }

            // Get a scout.
            var scoutHull = buyableHullTypes.FirstOrDefault(p => p.GetName().Contains(targetHullTypeName));

            if (scoutHull == null)
            {
                return;
            }

            Log($"\tFound Scout: {scoutHull.GetName()}");

            var partTypes = mission.GetPartTypes();
            List <AllegianceInterop.IpartTypeIGCWrapper> buyablePartTypes = new List <AllegianceInterop.IpartTypeIGCWrapper>();

            foreach (var partType in partTypes)
            {
                if (partType.GetGroupID() >= 0 && station.CanBuy(partType) == true && station.IsObsolete(partType) == false)
                {
                    short equipmentTypeID = partType.GetEquipmentType();

                    bool isIncluded = false;

                    switch ((EquipmentType)equipmentTypeID)
                    {
                    case EquipmentType.NA:
                        isIncluded = true;
                        break;

                    case EquipmentType.ET_Weapon:
                    case EquipmentType.ET_Turret:
                        for (sbyte i = 0; i < scoutHull.GetMaxFixedWeapons(); i++)
                        {
                            isIncluded |= scoutHull.GetPartMask(equipmentTypeID, i) != 0 && scoutHull.CanMount(partType, i) == true;
                        }
                        break;

                    default:
                        isIncluded |= scoutHull.GetPartMask(equipmentTypeID, 0) != 0 && scoutHull.CanMount(partType, 0) == true;

                        break;
                    }

                    if (isIncluded == true)
                    {
                        buyablePartTypes.Add(partType);
                        Log($"\t\tFound part: {partType.GetName()}, capacity for part: {scoutHull.GetCapacity(partType.GetEquipmentType())}");
                    }
                }
            }

            // Sell all the parts
            //var emptyShip = _client.CreateEmptyShip();
            //emptyShip.SetBaseHullType(scoutHull);
            //_client.BuyLoadout(emptyShip, false);

            //emptyShip.Terminate();

            // Now get the new empty ship from the client.
            //ship = _client.GetShip();

            foreach (var part in ship.GetParts())
            {
                ship.DeletePart(part);
            }

            // Change the ship to the scout hull.
            ship.SetBaseHullType(scoutHull);
            ClientConnection.BuyDefaultLoadout(ship, station, scoutHull, ClientConnection.GetMoney());

            ClearShipCargo(ship);


            // Load Weapons, missiles, dispensers, etc.
            if (loadOut.Weapon1 != null)
            {
                var part = buyablePartTypes.FirstOrDefault(p => p.GetName().Contains(loadOut.GetItemTypeString(loadOut.Weapon1.Value)) == true);
                if (part != null)
                {
                    ClientConnection.BuyPartOnBudget(ship, part, 0, ClientConnection.GetMoney());
                }
            }

            if (loadOut.Weapon2 != null)
            {
                var part = buyablePartTypes.FirstOrDefault(p => p.GetName().Contains(loadOut.GetItemTypeString(loadOut.Weapon2.Value)) == true);
                if (part != null)
                {
                    ClientConnection.BuyPartOnBudget(ship, part, 1, ClientConnection.GetMoney());
                }
            }

            if (loadOut.Weapon3 != null)
            {
                var part = buyablePartTypes.FirstOrDefault(p => p.GetName().Contains(loadOut.GetItemTypeString(loadOut.Weapon3.Value)) == true);
                if (part != null)
                {
                    ClientConnection.BuyPartOnBudget(ship, part, 2, ClientConnection.GetMoney());
                }
            }

            if (loadOut.Weapon4 != null)
            {
                var part = buyablePartTypes.FirstOrDefault(p => p.GetName().Contains(loadOut.GetItemTypeString(loadOut.Weapon4.Value)) == true);
                if (part != null)
                {
                    ClientConnection.BuyPartOnBudget(ship, part, 3, ClientConnection.GetMoney());
                }
            }

            if (loadOut.Turret1 != null)
            {
                var part = buyablePartTypes.FirstOrDefault(p => p.GetName().Contains(loadOut.GetItemTypeString(loadOut.Turret1.Value)) == true);
                if (part != null)
                {
                    ClientConnection.BuyPartOnBudget(ship, part, 0, ClientConnection.GetMoney());
                }
            }

            if (loadOut.Turret2 != null)
            {
                var part = buyablePartTypes.FirstOrDefault(p => p.GetName().Contains(loadOut.GetItemTypeString(loadOut.Turret2.Value)) == true);
                if (part != null)
                {
                    ClientConnection.BuyPartOnBudget(ship, part, 1, ClientConnection.GetMoney());
                }
            }

            if (loadOut.Turret3 != null)
            {
                var part = buyablePartTypes.FirstOrDefault(p => p.GetName().Contains(loadOut.GetItemTypeString(loadOut.Turret3.Value)) == true);
                if (part != null)
                {
                    ClientConnection.BuyPartOnBudget(ship, part, 2, ClientConnection.GetMoney());
                }
            }

            if (loadOut.Turret4 != null)
            {
                var part = buyablePartTypes.FirstOrDefault(p => p.GetName().Contains(loadOut.GetItemTypeString(loadOut.Turret4.Value)) == true);
                if (part != null)
                {
                    ClientConnection.BuyPartOnBudget(ship, part, 3, ClientConnection.GetMoney());
                }
            }

            if (loadOut.Dispenser != null)
            {
                var part = buyablePartTypes.FirstOrDefault(p => p.GetName().Contains(loadOut.GetItemTypeString(loadOut.Dispenser.Value)) == true);
                if (part != null)
                {
                    ClientConnection.BuyPartOnBudget(ship, part, 0, ClientConnection.GetMoney());
                }
            }

            if (loadOut.Missiles != null)
            {
                var part = buyablePartTypes.FirstOrDefault(p => p.GetName().Contains(loadOut.GetItemTypeString(loadOut.Missiles.Value)) == true);
                if (part != null)
                {
                    ClientConnection.BuyPartOnBudget(ship, part, 0, ClientConnection.GetMoney());
                }
            }

            if (loadOut.Cargo1 != null)
            {
                var part = buyablePartTypes.FirstOrDefault(p => p.GetName().Contains(loadOut.GetItemTypeString(loadOut.Cargo1.Value)) == true);
                if (part != null)
                {
                    ClientConnection.BuyPartOnBudget(ship, part, -1, ClientConnection.GetMoney());
                }
            }

            if (loadOut.Cargo2 != null)
            {
                var part = buyablePartTypes.FirstOrDefault(p => p.GetName().Contains(loadOut.GetItemTypeString(loadOut.Cargo2.Value)) == true);
                if (part != null)
                {
                    ClientConnection.BuyPartOnBudget(ship, part, -2, ClientConnection.GetMoney());
                }
            }

            if (loadOut.Cargo3 != null)
            {
                var part = buyablePartTypes.FirstOrDefault(p => p.GetName().Contains(loadOut.GetItemTypeString(loadOut.Cargo3.Value)) == true);
                if (part != null)
                {
                    ClientConnection.BuyPartOnBudget(ship, part, -3, ClientConnection.GetMoney());
                }
            }

            if (loadOut.Cargo4 != null)
            {
                var part = buyablePartTypes.FirstOrDefault(p => p.GetName().Contains(loadOut.GetItemTypeString(loadOut.Cargo4.Value)) == true);
                if (part != null)
                {
                    ClientConnection.BuyPartOnBudget(ship, part, -4, ClientConnection.GetMoney());
                }
            }

            if (loadOut.Cargo5 != null)
            {
                var part = buyablePartTypes.FirstOrDefault(p => p.GetName().Contains(loadOut.GetItemTypeString(loadOut.Cargo5.Value)) == true);
                if (part != null)
                {
                    ClientConnection.BuyPartOnBudget(ship, part, -5, ClientConnection.GetMoney());
                }
            }

            //_client.EndLockDown(LockdownCriteria.lockdownLoadout);

            // Launch!
            ClientConnection.BuyLoadout(ship, true);

            ClientConnection.EndLockDown(LockdownCriteria.lockdownLoadout);
        }