Esempio n. 1
0
        public bool PutItem(InventoryItemInstance item)
        {
            if (Items.Contains(item))
            {
                return(false);
            }

            Items.Add(item);
            return(true);
        }
Esempio n. 2
0
        public static SerializableItemInstance MakeSerializableItemInstance(InventoryItemInstance rItemInstance)
        {
            var sItemInstance = new SerializableItemInstance();

            sItemInstance.InstanceUID = rItemInstance.InstanceUID;
            sItemInstance.Quantity    = rItemInstance.Quantity;
            sItemInstance.Condition   = rItemInstance.Condition;
            sItemInstance.ItemModel   = rItemInstance.ItemModel.Name;
            return(sItemInstance);
        }
Esempio n. 3
0
        public bool RemoveItem(long uid, int quantity)
        {
            InventoryItemInstance instance = GetItem(uid);

            if (instance == null)
            {
                return(false);
            }
            return(RemoveItem(instance, quantity));
        }
Esempio n. 4
0
        //returns found instance
        public InventoryItemInstance TakeItem(InventoryItemModel item)
        {
            InventoryItemInstance foundInstance = FindItem(item);

            if (foundInstance == null)
            {
                return(null);
            }

            Items.Remove(foundInstance);
            return(foundInstance);
        }
Esempio n. 5
0
        /*
         * //returns quantity taken, or -1 for failure
         * public int TakeItem(InventoryItemModel item, int quantity)
         * {
         *  if (quantity == 1)
         *      return TakeItem(item) == null ? -1 : 1;
         *  else if (quantity <= 0)
         *      return -1;
         *  else
         *  {
         *      //handle stacks
         *      if (!item.Stackable)
         *          return -1;
         *
         *      InventoryItemInstance foundInstance = FindItem(item);
         *
         *
         *  }
         * }
         */
        //started writing that, realized it had no use case and was stupid

        public bool TakeItem(InventoryItemInstance item)
        {
            if (Items.Contains(item))
            {
                Items.Remove(item);
                return(true);
            }
            else
            {
                return(false);
            }
        }
Esempio n. 6
0
        private void CallOnRemove(InventoryItemInstance instance)
        {
            if (string.IsNullOrEmpty(instance?.ItemModel?.Scripts?.OnRemove))
            {
                return;
            }

            ScriptingModule.Call(instance.ItemModel.Scripts.OnRemove, new ScriptExecutionContext()
            {
                Caller = this
            }, instance.ItemModel, instance);
        }
Esempio n. 7
0
        public void EquipItem(InventoryItemInstance item, EquipSlot?slotOverride)
        {
            if (item.Equipped)
            {
                throw new InvalidOperationException();
            }

            EquipSlot slot = slotOverride ?? InventoryModel.GetItemSlot(item.ItemModel);

            if (slot == EquipSlot.None)
            {
                throw new InvalidOperationException();
            }

            //unequip what was in the slot
            if (IsEquipped(slot))
            {
                UnequipItem(Equipped[slot], false);
            }

            //if it's a two-handed weapon, also unequip the other slot
            if (item.ItemModel is WeaponItemModel && item.ItemModel.CheckFlag("TwoHanded") && Equipped.ContainsKey(slot == EquipSlot.LeftWeapon ? EquipSlot.RightWeapon : EquipSlot.LeftWeapon))
            {
                UnequipItem(Equipped[slot == EquipSlot.LeftWeapon ? EquipSlot.RightWeapon : EquipSlot.LeftWeapon], false);
            }

            Equipped[slot] = item;

            //magazine logic
            if (item.ItemModel is RangedWeaponItemModel rwim && rwim.UseMagazine)
            {
                //var rwim = (RangedWeaponItemModel)item.ItemModel;
                AmmoInMagazine[slot] = Math.Min(rwim.MagazineSize, Inventory.CountItem(rwim.AType.ToString()));
                Inventory.RemoveItem(rwim.AType.ToString(), AmmoInMagazine[slot]);
            }

            item.Equipped = true;

            if (!string.IsNullOrEmpty(item?.ItemModel?.Scripts?.OnEquip))
            {
                ScriptingModule.Call(item?.ItemModel?.Scripts?.OnEquip, new ScriptExecutionContext()
                {
                    Caller = this
                }, item.ItemModel, item);
            }

            UpdateStats();

            QdmsMessageBus.Instance.PushBroadcast(new QdmsKeyValueMessage("RpgChangeWeapon", "Slot", slot));
        }
Esempio n. 8
0
        private InventoryItemInstance FindFirstItem(string item)
        {
            InventoryItemInstance instance = null;

            foreach (InventoryItemInstance i in Items)
            {
                if (i.ItemModel.Name == item)
                {
                    instance = i;
                    break;
                }
            }
            return(instance);
        }
Esempio n. 9
0
        public InventoryItemInstance GetItem(long uid)
        {
            InventoryItemInstance instance = null;

            foreach (InventoryItemInstance i in Items)
            {
                if (i.InstanceUID == uid)
                {
                    instance = i;
                    break;
                }
            }
            return(instance);
        }
Esempio n. 10
0
        public static InventoryItemInstance MakeItemInstance(SerializableItemInstance sItemInstance)
        {
            InventoryItemModel model = InventoryModel.GetModel(sItemInstance.ItemModel);

            if (model == null)
            {
                CDebug.LogEx(string.Format("Couldn't find model {0} for SerializableItemInstance", sItemInstance.ItemModel), LogLevel.Error, sItemInstance);
                return(null);
            }


            InventoryItemInstance rItemInstance = new InventoryItemInstance(model, sItemInstance.Condition, sItemInstance.Quantity, false);

            return(rItemInstance);
        }
Esempio n. 11
0
        //this is arguably useless actually but I'm drunk and tired

        private InventoryItemInstance FindItem(InventoryItemModel item)
        {
            InventoryItemInstance foundInstance = null;

            foreach (InventoryItemInstance i in Items)
            {
                if (i.ItemModel == item)
                {
                    foundInstance = i;
                    break;
                }
            }

            return(foundInstance);
        }
Esempio n. 12
0
        private void AddItem(string item, int quantity, bool enforceQuantityLimit, out int quantityRemaining)
        {
            quantityRemaining = 0;

            if (quantity <= 0)
            {
                return;
            }

            InventoryItemModel mdl = Models[item];

            enforceQuantityLimit &= mdl.MaxQuantity > 0;

            if (enforceQuantityLimit)
            {
                int quantityToAdd = Math.Min(quantity, mdl.MaxQuantity);
                quantityRemaining = quantity - quantityToAdd;
                quantity          = quantityToAdd;
            }

            if (mdl.Stackable)
            {
                InventoryItemInstance instance = FindFirstItem(mdl.Name);
                if (instance == null)
                {
                    instance = new InventoryItemInstance(mdl);
                    Items.Add(instance);
                    CallOnAdd(instance);
                    instance.Quantity = 0;
                }
                int oldQuantity = instance.Quantity;

                instance.Quantity += quantity;
                CallOnQuantityChanged(instance, oldQuantity);
            }
            else
            {
                for (int i = 0; i < quantity; i++)
                {
                    var instance = new InventoryItemInstance(mdl);
                    Items.Add(instance);
                    CallOnAdd(instance);
                }
            }
        }
Esempio n. 13
0
        //scripting handling

        private void CallOnQuantityChanged(InventoryItemInstance instance, int oldQuantity, int?newQuantity = null)
        {
            if (string.IsNullOrEmpty(instance?.ItemModel?.Scripts?.OnQuantityChange))
            {
                return;
            }

            if (!instance.ItemModel.Stackable)
            {
                Debug.LogError($"Tried to call OnQuantityChange script \"{instance.ItemModel.Scripts.OnQuantityChange}\" on non-stackable model \"{instance.ItemModel}\"");
                return;
            }

            ScriptingModule.Call(instance.ItemModel.Scripts.OnQuantityChange, new ScriptExecutionContext()
            {
                Caller = this
            }, instance.ItemModel, instance, oldQuantity, newQuantity ?? instance.Quantity);
        }
Esempio n. 14
0
        public static ContainerModel MakeContainerModel(SerializableContainerModel sContainerModel)
        {
            ContainerModel rContainerModel = new ContainerModel();

            foreach (SerializableItemInstance sItemInstance in sContainerModel.Items)
            {
                InventoryItemInstance rItemInstance = SerializableItemInstance.MakeItemInstance(sItemInstance);
                if (rItemInstance != null)
                {
                    rContainerModel.PutItem(rItemInstance);
                }
                else
                {
                    CDebug.LogEx(string.Format("Couldn't create real item instance for item in container"), LogLevel.Error, sContainerModel);
                }
            }

            return(rContainerModel);
        }
Esempio n. 15
0
        private void UnequipItem(InventoryItemInstance item, bool postMessage)
        {
            if (!item.Equipped)
            {
                throw new InvalidOperationException();
            }

            EquipSlot slot = InventoryModel.GetItemSlot(item.ItemModel);

            if (slot != EquipSlot.None)
            {
                Equipped.Remove(slot);
            }
            //allow continuing even if it's not actually equippable, for fixing bugs

            //magazine logic
            if (item.ItemModel is RangedWeaponItemModel rwim && rwim.UseMagazine)
            {
                Inventory.AddItem(rwim.AType.ToString(), AmmoInMagazine[slot]);
                AmmoInMagazine[slot] = 0;
            }

            item.Equipped = false;

            if (!string.IsNullOrEmpty(item?.ItemModel?.Scripts?.OnUnequip))
            {
                ScriptingModule.Call(item?.ItemModel?.Scripts?.OnUnequip, new ScriptExecutionContext()
                {
                    Caller = this
                }, item.ItemModel, item);
            }

            UpdateStats();

            if (postMessage)
            {
                QdmsMessageBus.Instance.PushBroadcast(new QdmsKeyValueMessage("RpgChangeWeapon", "Slot", slot));
            }
        }
Esempio n. 16
0
        /// <summary>
        /// Adds a non-stackable item if it is possible to do so while respecting quantity limits
        /// </summary>
        public bool AddItemIfPossible(InventoryItemInstance item)
        {
            if (item.ItemModel.Stackable)
            {
                throw new InvalidOperationException("Stackable items cannot be added with this API, use AddItemsToQuantityLimit instead!");
            }

            if (item.ItemModel.MaxQuantity <= 0)
            {
                AddItem(item);
                return(true);
            }

            int numItem = CountItem(item.ItemModel.Name);

            if (numItem < item.ItemModel.MaxQuantity)
            {
                AddItem(item);
                return(true);
            }

            return(false);
        }
Esempio n. 17
0
        public void AddItem(string item, int quantity)
        {
            if (quantity <= 0)
            {
                return;
            }

            InventoryItemModel mdl = Models[item];

            if (mdl.Stackable)
            {
                InventoryItemInstance instance = null;
                foreach (InventoryItemInstance i in Items)
                {
                    if (i.ItemModel.Name == mdl.Name)
                    {
                        instance = i;
                        break;
                    }
                }
                if (instance == null)
                {
                    instance = new InventoryItemInstance(mdl);
                    Items.Add(instance);
                    instance.Quantity = 0;
                }

                instance.Quantity += quantity;
            }
            else
            {
                for (int i = 0; i < quantity; i++)
                {
                    Items.Add(new InventoryItemInstance(mdl));
                }
            }
        }
Esempio n. 18
0
 public bool RemoveItem(InventoryItemInstance item, int quantity)
 {
     if (item.ItemModel.Stackable)
     {
         //reduce quantity
         item.Quantity = Math.Max(0, (item.Quantity - quantity));
         if (item.Quantity == 0)
         {
             return(RemoveItem(item));
         }
         return(true);
     }
     else
     {
         if (quantity == 1)
         {
             return(RemoveItem(item));
         }
         else
         {
             return(false);
         }
     }
 }
Esempio n. 19
0
 protected static AidItemScriptResult ApplyScript(AidItemModel item, CharacterModel player, InventoryItemInstance itemInstance)
 {
     return(ScriptingModule.CallForResult <AidItemScriptResult>(item.Scripts.OnApply, new ScriptExecutionContext()
     {
         Caller = player
     }, item, itemInstance));
 }
Esempio n. 20
0
 public void AddItem(InventoryItemInstance item)
 {
     Items.Add(item);
 }
Esempio n. 21
0
 public bool RemoveItem(InventoryItemInstance item)
 {
     return(Items.Remove(item));
 }
Esempio n. 22
0
 public void UnequipItem(InventoryItemInstance item)
 {
     UnequipItem(item, true);
 }
Esempio n. 23
0
 public void EquipItem(InventoryItemInstance item)
 {
     EquipItem(item, null);
 }
Esempio n. 24
0
        public override AidItemApplyResult Apply(CharacterModel target, InventoryItemInstance itemInstance)
        {
            StringBuilder       messageBuilder = new StringBuilder();
            AidItemScriptResult scriptResult   = new AidItemScriptResult()
            {
                ReturnSuccess = true, ContinueApply = true, ConsumeItem = !CheckFlag(ItemFlag.AidDoNotConsume), ShowMessage = !CheckFlag(ItemFlag.AidNoMessage)
            };

            //apply script
            if (!string.IsNullOrEmpty(Scripts.OnApply))
            {
                scriptResult = ApplyScript(this, target, itemInstance);
            }

            if (scriptResult.ContinueApply)
            {
                //apply base
                if (AType != AidType.None)
                {
                    float amountRestored = ApplyBase(this, target);
                    messageBuilder.AppendLine(base.GetSuccessMessage(amountRestored));
                }

                //apply nodes
                if (RestoreNodes != null && RestoreNodes.Count > 0)
                {
                    foreach (var restoreNode in RestoreNodes)
                    {
                        if (!restoreNode.AutoApply)
                        {
                            continue;
                        }
                        float amountRestored = ApplyNode(restoreNode.AType, restoreNode.RType, restoreNode.Amount, target);
                        messageBuilder.AppendLine($"Restored {(int)amountRestored} {restoreNode.AType.ToString()}!");
                    }
                }
            }

            //concat messages
            string message;

            if (scriptResult.MessageOverride != null)
            {
                if (CheckFlag(ItemFlag.ComboAidKeepAllMessages))
                {
                    message = scriptResult.MessageOverride + '\n' + messageBuilder.ToString();
                }
                else
                {
                    message = scriptResult.MessageOverride;
                }
            }
            else
            {
                message = messageBuilder.ToString();
            }

            return(new AidItemApplyResult()
            {
                ConsumeItem = scriptResult.ConsumeItem, Message = message, ShowMessage = scriptResult.ShowMessage, Success = scriptResult.ReturnSuccess
            });
        }