Beispiel #1
0
        private static Pickupable CheckIfEggAnExtractPickable(TechType techType)
        {
            Pickupable pickup;

            if (EggHandler.GetDiscoveredEgg(techType, out TechType value))
            {
                pickup = CraftData.InstantiateFromPrefab(value).EnsureComponent <Pickupable>();
            }
            else
            {
                pickup = CraftData.InstantiateFromPrefab(techType).EnsureComponent <Pickupable>();
            }

            return(pickup);
        }
Beispiel #2
0
        internal static Pickupable ConvertToPickupable(ItemData itemData)
        {
            QuickLogger.Debug("ConvertToPickupable", true);

            if (itemData == null)
            {
                QuickLogger.Debug("Item Data is null", true);
                return(null);
            }

            QuickLogger.Debug("1");
            if (EggHandler.GetDiscoveredEgg(itemData.TechType, out TechType value))
            {
                return(CraftData.InstantiateFromPrefab(value).GetComponent <Pickupable>());
            }

            QuickLogger.Debug("2");

            var gameObject = CraftData.GetPrefabForTechType(itemData.TechType);

            QuickLogger.Debug("3");


            if (gameObject == null)
            {
                QuickLogger.Error($"Couldn't get the prefab for the tech-type {itemData.TechType}");
                return(null);
            }

            QuickLogger.Debug("4");

            var go = GameObject.Instantiate(gameObject);

            QuickLogger.Debug("5");

            if (go == null)
            {
                QuickLogger.Debug("GameObject is null in convert to pickupable", true);
                QuickLogger.Debug($"TechType = {itemData.Type}", true);
                return(null);
            }

            switch (itemData.Type)
            {
            case ItemType.Battery:
                go.GetComponent <Battery>()._charge = itemData.BatteryData.Charge;
                break;

            case ItemType.Food:
                var eatable = go.GetComponent <Eatable>();
                eatable.foodValue  = itemData.FoodData.FoodValue;
                eatable.waterValue = itemData.FoodData.WaterValue;
                break;

            case ItemType.Fuel:
                go.GetComponent <FireExtinguisher>().fuel = itemData.FuelData.Fuel;
                break;

            case ItemType.PlayerTool:
                if (itemData.PlayerToolData.HasBattery)
                {
                    var batteryTechType = itemData.PlayerToolData.BatteryInfo.TechType;
                    var tempBattery     = CraftData.GetPrefabForTechType(batteryTechType);
                    var capacity        = tempBattery?.gameObject.GetComponent <IBattery>()?.capacity;

                    if (itemData.PlayerToolData.HasBattery && capacity != null && capacity > 0)
                    {
                        //var pickup = CraftData.InstantiateFromPrefab(itemData.TechType).GetComponent<Pickupable>();
                        var energyMixin      = go.gameObject.GetComponent <EnergyMixin>();
                        var normalizedCharge = itemData.PlayerToolData.BatteryInfo.BatteryCharge / capacity;
                        if (energyMixin.GetBattery() != null)
                        {
                            QuickLogger.Debug("Battery was already in device destroying");
                        }

                        if (!energyMixin.compatibleBatteries.Contains(batteryTechType))
                        {
                            energyMixin.compatibleBatteries.Add(batteryTechType);
                        }

                        energyMixin.SetBattery(itemData.PlayerToolData.BatteryInfo.TechType,
                                               (float)normalizedCharge);
                        QuickLogger.Info($"Gave Player Player tool {itemData.PlayerToolData.TechType} with battery {batteryTechType}");
                    }
                    else
                    {
                        QuickLogger.Error("While trying to get the batter capacity of the battery it returned null or 0.");
                    }
                }
                break;

            case ItemType.FCSFilteredStorage:
                var storage = go.GetComponent <FCSFilteredStorage>();
                storage.Items   = itemData.FcsFilteredStorage;
                storage.Filters = itemData.FcsFilters;
                storage.ForceUpdateDisplay();
                break;
            }

            QuickLogger.Debug("6");

            return(go.GetComponent <Pickupable>());
        }
Beispiel #3
0
        internal static bool GivePlayerItem(TechType techType, ObjectDataTransferData itemData,
                                            Func <ObjectData, RackSlot> getServerWithObjectData)
        {
            QuickLogger.Debug($"Give Player Item: {techType}", true);

            bool isSuccessful = false;

#if SUBNAUTICA
            var itemSize = CraftData.GetItemSize(techType);
#elif BELOWZERO
            var itemSize = TechData.GetItemSize(techType);
#endif
            if (Inventory.main.HasRoomFor(itemSize.x, itemSize.y))
            {
                //TODO handle null playerToolData
                Pickupable pickup;
                if (itemData.Vehicle == null)
                {
                    if (EggHandler.GetDiscoveredEgg(techType, out TechType value))
                    {
                        pickup = CraftData.InstantiateFromPrefab(value).EnsureComponent <Pickupable>();
                    }
                    else
                    {
                        pickup = CraftData.InstantiateFromPrefab(techType).EnsureComponent <Pickupable>();
                    }
                    if (!itemData.IsServer)
                    {
                        var data = (ObjectData)itemData.data;

                        if (data != null)
                        {
                            switch (data.DataObjectType)
                            {
                            case SaveDataObjectType.PlayerTool:

                                if (data.PlayToolData.HasBattery)
                                {
                                    var batteryTechType = data.PlayToolData.BatteryInfo.TechType;
                                    var tempBattery     = CraftData.GetPrefabForTechType(batteryTechType);
                                    var capacity        = tempBattery?.gameObject.GetComponent <IBattery>()?.capacity;

                                    if (data.PlayToolData.HasBattery && capacity != null && capacity > 0)
                                    {
                                        var energyMixin      = pickup.gameObject.GetComponent <EnergyMixin>();
                                        var normalizedCharge = data.PlayToolData.BatteryInfo.BatteryCharge / capacity;
                                        if (energyMixin.GetBattery() != null)
                                        {
                                            QuickLogger.Debug("Battery was already in device destroying");
                                        }

                                        if (!energyMixin.compatibleBatteries.Contains(batteryTechType))
                                        {
                                            energyMixin.compatibleBatteries.Add(batteryTechType);
                                        }

                                        energyMixin.SetBattery(data.PlayToolData.BatteryInfo.TechType,
                                                               (float)normalizedCharge);
                                        QuickLogger.Info(
                                            $"Gave Player Player tool {data.PlayToolData.TechType} with battery {batteryTechType}");
                                    }
                                    else
                                    {
                                        QuickLogger.Error <DSSServerController>(
                                            "While trying to get the batter capacity of the battery it returned null or 0.");
                                    }
                                }

                                break;



                            case SaveDataObjectType.Eatable:
                                //We are not handling decaying items so I dont need to set anything
                                break;

                            case SaveDataObjectType.Server:
                                var server = pickup.gameObject.GetComponent <DSSServerController>();
                                server.FCSFilteredStorage.Items = new HashSet <ObjectData>(data.ServerData);
                                server.Initialize();
                                server.DisplayManager.UpdateDisplay();
                                break;

                            case SaveDataObjectType.Battery:
                                var battery = pickup.gameObject.GetComponent <Battery>();
                                battery.charge = data.PlayToolData.BatteryInfo.BatteryCharge;
                                break;
                            }
                        }

                        var result = getServerWithObjectData?.Invoke(data);
                        result?.Remove(data);
                        isSuccessful = true;
                    }
                    else
                    {
                        var data       = (HashSet <ObjectData>)itemData.data;
                        var controller = pickup.gameObject.GetComponent <DSSServerController>();
                        controller.Initialize();
                        controller.FCSFilteredStorage.Items   = new HashSet <ObjectData>(data);
                        controller.FCSFilteredStorage.Filters = new List <Filter>(itemData.Filters);
                        controller.DisplayManager.UpdateDisplay();
                        isSuccessful = true;
                    }

                    Inventory.main.Pickup(pickup);
                }
                else if (itemData.Vehicle != null)
                {
                    QuickLogger.Debug("Is Vehicle Item");

                    var vehicleContainers = itemData.Vehicle.gameObject.GetComponentsInChildren <StorageContainer>()
                                            .Select((x) => x.container).ToList();
                    vehicleContainers.AddRange(GetSeamothStorage(itemData.Vehicle));

                    for (var index = 0; index < vehicleContainers.Count; index++)
                    {
                        for (var i = 0; i < vehicleContainers[index].ToList().Count; i++)
                        {
                            var item = vehicleContainers[index].ToList()[i];

                            if (item.item.GetTechType() == techType)
                            {
                                var passedItem = vehicleContainers[index].RemoveItem(item.item);
                                if (passedItem)
                                {
                                    if (Inventory.main.Pickup(item.item))
                                    {
                                        CrafterLogic.NotifyCraftEnd(Player.main.gameObject, item.item.GetTechType());

                                        goto _end;
                                    }
                                }
                            }
                        }
                    }

_end:
                    isSuccessful = true;
                }
            }

            Mod.OnBaseUpdate?.Invoke();
            return(isSuccessful);
        }