public int AddItem(MyInventoryItem item) { int newKey = m_lastUsedKey++; m_itemsWithKeys.Add(newKey, item); return(newKey); }
/// <summary> /// Called where amount of inventory item changed /// </summary> /// <param name="sender"></param> /// <param name="amountChanged"></param> private void OnAmountChange(MyInventoryItem sender, float amountChanged) { if (OnInventoryItemAmountChange != null) { OnInventoryItemAmountChange(this, sender, amountChanged); } }
/// <summary> /// Removes inventory item from inventory /// </summary> /// <param name="item">Inventory item</param> /// <param name="closeInventoryItem">If true, then close inventory item's instance in pool</param> public void RemoveInventoryItem(MyInventoryItem item, bool closeInventoryItem = false) { if (RemoveItemFromInventoryPrivate(item, closeInventoryItem)) { CallContentChange(); } }
private MyToolKit(MySmallShip smallShipOwner, MyPlayer playerOwner, MyInventoryItem toolInventoryItem, MyToolKitDelegates[] delegates) { m_smallShipOwner = smallShipOwner; m_playerOwner = playerOwner; m_toolInventoryItem = toolInventoryItem; m_delegates = delegates; }
public static MyInventoryItem CreateInventoryItemFromInventoryItemObjectBuilder(MyMwcObjectBuilder_InventoryItem inventoryItemBuilder) { MyInventoryItem item = CreateInventoryItemFromObjectBuilder(inventoryItemBuilder.ItemObjectBuilder, inventoryItemBuilder.Amount); item.TemporaryFlags = inventoryItemBuilder.TemporaryFlags; return(item); }
private void AddItemToInventoryPrivate(MyInventoryItem item) { if (IsDisabled(item)) { MyInventory.CloseInventoryItem(item); } else { if (InventorySynchronizer != null && InventorySynchronizer.MustBeSynchronized()) { InventorySynchronizer.AddInventoryItemForSynchronization(item); } else { if (!UnlimitedCapacity) { if (m_inventoryItems.Count >= m_maxItems) { throw new Exception("Inventory has full capacity"); } } item.OnAmountChange += m_onItemAmountChangeHandler; item.Owner = this; m_inventoryItems.Add(item); OnAmountChange(item, item.Amount); if (UnlimitedCapacity) { MaxItems = Math.Max(m_inventoryItems.Count, MaxItems); } } } }
void EntityInventoryItemAmountChanged(MyEntity entity, MyInventory inventory, MyInventoryItem item, float number) { if (MyScriptWrapper.IsPlayerShip(entity)) { CheckOre(); } }
/// <summary> /// Initialize inventory from objectbuilder /// </summary> /// <param name="inventoryObjectBuilder">Inventory objectbuilder</param> public void Init(MyMwcObjectBuilder_Inventory inventoryObjectBuilder, float defaultPriceCoeficient = DEFAULT_PRICE_COEFICIENT) { Debug.Assert(inventoryObjectBuilder != null); if (inventoryObjectBuilder.MaxItems == 0) { MaxItems = MyInventory.DEFAULT_MAX_ITEMS; } else { MaxItems = inventoryObjectBuilder.MaxItems; } PriceCoeficient = inventoryObjectBuilder.PriceCoeficient != null ? inventoryObjectBuilder.PriceCoeficient.Value : defaultPriceCoeficient; TemplateType = inventoryObjectBuilder.TemplateType; UnlimitedCapacity = inventoryObjectBuilder.UnlimitedCapacity; if (inventoryObjectBuilder.InventoryItems != null && inventoryObjectBuilder.InventoryItems.Count > 0) { RemoveAllInventoryItemsPrivate(true); List <MyInventoryItem> inventoryItemsToAdd = new List <MyInventoryItem>(); foreach (MyMwcObjectBuilder_InventoryItem inventoryItemObjectBuilder in inventoryObjectBuilder.InventoryItems) { if (inventoryItemsToAdd.Count >= MaxItems && !UnlimitedCapacity) { break; } // if old foundation factory is in inventory, we replace it with new prefab foundation factory if (inventoryItemObjectBuilder.ItemObjectBuilder is MyMwcObjectBuilder_FoundationFactory) { inventoryItemObjectBuilder.ItemObjectBuilder = MyMwcObjectBuilder_Base.CreateNewObject(MyMwcObjectBuilderTypeEnum.PrefabFoundationFactory, (int)MyMwcObjectBuilder_PrefabFoundationFactory_TypesEnum.DEFAULT); } // hack: we need set default prefab health and max health to prefab object builder if (inventoryItemObjectBuilder.ItemObjectBuilder is MyMwcObjectBuilder_PrefabBase) { MyMwcObjectBuilder_PrefabBase prefabBuilder = inventoryItemObjectBuilder.ItemObjectBuilder as MyMwcObjectBuilder_PrefabBase; if (prefabBuilder.PrefabHealthRatio == 0f) { prefabBuilder.PrefabHealthRatio = MyGameplayConstants.HEALTH_RATIO_MAX; } if (prefabBuilder.PrefabMaxHealth == 0f) { prefabBuilder.PrefabMaxHealth = MyGameplayConstants.MAXHEALTH_PREFAB; } } MyInventoryItem inventoryItem = CreateInventoryItemFromObjectBuilder(inventoryItemObjectBuilder.ItemObjectBuilder, inventoryItemObjectBuilder.Amount); inventoryItem.TemporaryFlags = inventoryItemObjectBuilder.TemporaryFlags; inventoryItemsToAdd.Add(inventoryItem); } AddInventoryItems(inventoryItemsToAdd); } else { ClearInventoryItems(true); } }
private bool RemoveInventoryItemAmountPrivate(MyInventoryItem inventoryItem, float amount) { bool removed = false; inventoryItem.Amount -= amount; if (inventoryItem.Amount <= 0f) { removed = RemoveItemFromInventoryPrivate(inventoryItem, true); } return(removed); }
/// <summary> /// Removes amout of inventory items from inventory. /// </summary> /// <param name="inventoryItem">Inventory item</param> /// <param name="amount">Amount to remove</param> public void RemoveInventoryItemAmount(ref MyInventoryItem inventoryItem, float amount) { //inventoryItem.Amount -= amount; //if (inventoryItem.Amount <= 0f) //{ // RemoveInventoryItem(inventoryItem, true); //} if (RemoveInventoryItemAmountPrivate(inventoryItem, amount)) { CallContentChange(); } }
private void FixItemsOverMaxLimit() { bool removed = false; while (m_inventoryItems.Count > MaxItems) { MyInventoryItem itemToRemove = m_inventoryItems[m_inventoryItems.Count - 1]; RemoveInventoryItem(itemToRemove, true); removed = true; } if (removed) { CallContentChange(); } }
private bool RemoveItemFromInventoryPrivate(MyInventoryItem item, bool closeInventoryItem) { bool result = m_inventoryItems.Remove(item); item.OnAmountChange -= m_onItemAmountChangeHandler; if (item.Amount > 0f) { OnAmountChange(item, -item.Amount); } if (closeInventoryItem) { MyInventory.CloseInventoryItem(item); } else { item.Owner = null; } return(result); }
/// <summary> /// Returns inventory items from inventory /// </summary> /// <param name="objectBuilderType">Item's object builder type</param> /// <param name="objectBuilderId">Item's object builder id</param> /// <param name="amount">Amount to remove</param> public bool RemoveInventoryItemAmount(MyMwcObjectBuilderTypeEnum objectBuilderType, int?objectBuilderId, float amount) { bool removed = false; float inventoryItemsAmount = GetTotalAmountOfInventoryItems(objectBuilderType, objectBuilderId); if (inventoryItemsAmount < amount) { return(false); } float amountToRemoveLeft = amount; m_helperInventoryItemsForAddAndRemove.Clear(); GetInventoryItems(ref m_helperInventoryItemsForAddAndRemove, objectBuilderType, objectBuilderId); m_helperInventoryItemsForAddAndRemove.Sort((x, y) => x.Amount.CompareTo(y.Amount)); foreach (MyInventoryItem inventoryItem in m_helperInventoryItemsForAddAndRemove) { float amountToRemove = Math.Min(inventoryItem.Amount, amountToRemoveLeft); MyInventoryItem inventoryItemToRemoveAmount = inventoryItem; if (RemoveInventoryItemAmountPrivate(inventoryItemToRemoveAmount, amountToRemove)) { removed = true; } amountToRemoveLeft -= amountToRemove; if (amountToRemoveLeft <= 0f) { break; } } if (removed) { CallContentChange(); } return(true); }
public static MyInventoryItem CreateInventoryItemFromObjectBuilder(MyMwcObjectBuilder_Base objectBuilder, float amount) { float maxAmount = MyGameplayConstants.GetGameplayProperties(objectBuilder, MyMwcObjectBuilder_FactionEnum.Euroamerican).MaxAmount; float amountToAdd = Math.Min(maxAmount, amount); int objectBuilderId = objectBuilder.GetObjectBuilderId().HasValue ? objectBuilder.GetObjectBuilderId().Value : 0; MyGuiHelperBase guiHelper = MyGuiObjectBuilderHelpers.GetGuiHelper(objectBuilder.GetObjectBuilderType(), objectBuilderId); MyCommonDebugUtils.AssertDebug(guiHelper != null); //warning: use default faction for get gameplay properties for inventory item MyGameplayProperties inventoryItemProperties = MyGameplayConstants.GetGameplayProperties(objectBuilder.GetObjectBuilderType(), objectBuilderId, MyMwcObjectBuilder_FactionEnum.Euroamerican); MyCommonDebugUtils.AssertDebug(inventoryItemProperties != null); MyInventoryItem item = MyInventory.InventoryItemsPool.Allocate(); item.Start(guiHelper, inventoryItemProperties, objectBuilder, amountToAdd); return(item); }
private void DisabledInvalidListboxesForDrop(MyInventoryItem draggedInventoryItem) { foreach (KeyValuePair<MyGuiControlListbox, Predicate<MyInventoryItem>> keyValuePair in m_listboxDropConditions) { if (!keyValuePair.Value(draggedInventoryItem)) { keyValuePair.Key.Enabled = false; } } }
private void InventoryContentChanged(MyInventory sender, MyInventoryItem inventoryItem, float amountChanged) { float oreAmount = sender.GetInventoryItemsCount(MyMwcObjectBuilderTypeEnum.Ore, null); if (oreAmount >= 50) { m_objectives.Find(submission => submission.ID == MyMissionID.M01_Intro_Mining).Success(); } }
private static bool IsDisabled(MyInventoryItem item) { return(MyMwcObjectBuilder_InventoryItem.IsDisabled(item.ObjectBuilderType, item.ObjectBuilderId)); }
public static MyToolKit CreateInstance(MySmallShip smallShipOwner, MyPlayer playerOwner, MyInventoryItem toolInventoryItem) { Debug.Assert(smallShipOwner != null); Debug.Assert(playerOwner != null); Debug.Assert(IsSupportedToolKitItem(toolInventoryItem)); MyToolKitDelegates[] delegates = m_delegatesPerToolKitType[toolInventoryItem.ObjectBuilderId.Value]; Debug.Assert(delegates.Length > 0); return new MyToolKit(smallShipOwner, playerOwner, toolInventoryItem, delegates); }
/// <summary> /// Adds new inventory item from object builder /// </summary> /// <param name="objectBuilder">Item object builder</param> /// <param name="amount">Amount of inventory item</param> /// <param name="allAmountAddAsNewInventoryItems">If true, then all amount adds as new inventory items, if false, then try find old inventory items of same type, and add amount to them as first</param> public float AddInventoryItem(MyMwcObjectBuilder_Base objectBuilder, float amount, bool allAmountAddAsNewInventoryItems, bool increaseCapacityIfIsFull = false) { bool added = false; MyMwcObjectBuilderTypeEnum objectBuilderType = objectBuilder.GetObjectBuilderType(); int?objectBuilderId = objectBuilder.GetObjectBuilderId(); m_helperInventoryItemsForAddAndRemove.Clear(); GetInventoryItems(ref m_helperInventoryItemsForAddAndRemove, objectBuilderType, objectBuilderId); float amountToAddLeft = amount; if (!allAmountAddAsNewInventoryItems) { foreach (MyInventoryItem inventoryItem in m_helperInventoryItemsForAddAndRemove) { if (amountToAddLeft <= 0f) { break; } float amountToAdd = Math.Min(inventoryItem.MaxAmount - inventoryItem.Amount, amountToAddLeft); if (InventorySynchronizer != null && InventorySynchronizer.MustBeSynchronized()) { InventorySynchronizer.AddInventoryItemAmountChangeForSynchronization(objectBuilderType, objectBuilderId, inventoryItem.Amount, amountToAdd); } else { inventoryItem.Amount += amountToAdd; } amountToAddLeft -= amountToAdd; } } while (amountToAddLeft > 0f) { if (IsFull) { if (increaseCapacityIfIsFull) { MaxItems++; } else { break; } } MyInventoryItem newInventoryItem = MyInventory.CreateInventoryItemFromObjectBuilder(objectBuilder); float amountToAdd = Math.Min(newInventoryItem.MaxAmount, amountToAddLeft); //AddInventoryItem(newInventoryItem); AddItemToInventoryPrivate(newInventoryItem); newInventoryItem.Amount = amountToAdd; //After AddInventoryItem otherwise Amount event is not called amountToAddLeft -= amountToAdd; added = true; } if (added) { CallContentChange(); } return(amountToAddLeft); }
private MyGuiControlListboxItem CreateListboxItemAndAddToRepository(MyInventoryItem inventoryItem) { int inventoryItemKey = m_inventoryItemsRepository.AddItem(inventoryItem); StringBuilder description = null; if (inventoryItem.Icon == null) { description = inventoryItem.MultiLineDescription; } MyToolTips toolTips = new MyToolTips(); toolTips.AddToolTip(inventoryItem.MultiLineDescription, Color.White, 0.7f); MyGuiControlListboxItem listboxItem = new MyGuiControlListboxItem(inventoryItemKey, description, inventoryItem.Icon, toolTips, MyGuiConstants.LABEL_TEXT_SCALE); listboxItem.IconTexts = new MyIconTexts(); // add amount icon's text if (inventoryItem.Amount != 1f || inventoryItem.Amount != inventoryItem.MaxAmount) { StringBuilder amount = new StringBuilder(); amount.Append(inventoryItem.Amount.ToString()); MyGuiDrawAlignEnum align; Vector2 offset; if (inventoryItem.AmountTextAlign == MyInventoryAmountTextAlign.MiddleRight) { align = MyGuiDrawAlignEnum.HORISONTAL_RIGHT_AND_VERTICAL_CENTER; offset = new Vector2(-0.004f, 0.0038f); } else if (inventoryItem.AmountTextAlign == MyInventoryAmountTextAlign.BottomRight) { align = MyGuiDrawAlignEnum.HORISONTAL_RIGHT_AND_VERTICAL_BOTTOM; offset = new Vector2(-0.004f, -0.0025f); } else { throw new MyMwcExceptionApplicationShouldNotGetHere(); } listboxItem.IconTexts[align] = new MyColoredText(amount, Color.White, Color.White, MyGuiManager.GetFontMinerWarsWhite(), 0.5f, offset); } return listboxItem; }
public static bool IsSupportedToolKitItem(MyInventoryItem toolInventoryItem) { return toolInventoryItem != null && toolInventoryItem.Amount > 0f && toolInventoryItem.ObjectBuilderType == MyMwcObjectBuilderTypeEnum.SmallShip_Tool && toolInventoryItem.ObjectBuilderId != null && m_delegatesPerToolKitType.ContainsKey(toolInventoryItem.ObjectBuilderId.Value); }
private float GetTradeItemPrice(MyInventoryItem inventoryItem) { Debug.Assert(m_otherSideInventoryBuilder.PriceCoeficient != null); Debug.Assert(m_smallShipsBuilders[m_currentShipBuilderIndex].Builder.Inventory.PriceCoeficient != null); float otherSidePriceCoeficient = m_otherSideInventoryBuilder.PriceCoeficient.Value; float shipPriceCoeficient = m_smallShipsBuilders[m_currentShipBuilderIndex].Builder.Inventory.PriceCoeficient.Value; float priceCoeficient = 1f; if (inventoryItem.Owner == m_otherSideInventoryBuilder) { priceCoeficient = otherSidePriceCoeficient / shipPriceCoeficient; } else { priceCoeficient = shipPriceCoeficient / otherSidePriceCoeficient; } return inventoryItem.Price * priceCoeficient; }
public int AddItem(MyInventoryItem item) { int newKey = m_lastUsedKey++; m_itemsWithKeys.Add(newKey, item); return newKey; }
public static void CloseInventoryItem(MyInventoryItem item) { item.Owner = null; MyInventory.InventoryItemsPool.Deallocate(item); }
void OreAmountChanged(MyEntity entity, MyInventory inventory, MyInventoryItem item, float number) { if (MyScriptWrapper.IsPlayerShip(entity)) { if (item.ObjectBuilderType == MyMwcObjectBuilderTypeEnum.Ore) { if (!MyScriptWrapper.IsMissionFinished(MyMissionID.RIFT_URANITE)) { if (item.ObjectBuilderId == (int)MyMwcObjectBuilder_Ore_TypesEnum.URANITE) { float ammount = MyScriptWrapper.GetInventoryItemAmount(MyScriptWrapper.GetPlayerInventory(), MyMwcObjectBuilderTypeEnum.Ore, (int)MyMwcObjectBuilder_Ore_TypesEnum.URANITE); if (ammount > 0.5f && m_miningquotes == 1) { MyScriptWrapper.PlayDialogue(MyDialogueEnum.RIFT_0700_MINING_COLOR); m_miningquotes++; } else if (ammount > 1.0f && m_miningquotes == 2) { MyScriptWrapper.PlayDialogue(MyDialogueEnum.RIFT_0800_MINING_TUNE); m_miningquotes++; } else if (ammount > 1.5f && m_miningquotes == 3) { MyScriptWrapper.PlayDialogue(MyDialogueEnum.RIFT_0900_MINING_TUNE_2); m_miningquotes++; } } } } } }
private bool CanDropItem(MyInventoryItem item, MyGuiControlListbox dropFrom, MyGuiControlListbox dropTo) { if (dropTo != dropFrom && NeedHandleSmallShipDrop(item) && dropTo == m_shipInventoryListBox) { MySmallShipBuilderWithName builderWithName = m_smallShipsBuilders.Find(x => x.Builder == item.GetInventoryItemObjectBuilder(false)); Debug.Assert(builderWithName != null); int index = m_smallShipsBuilders.IndexOf(builderWithName); Debug.Assert(index > -1); // we can't drop same ship to current ship's inventory return index != m_currentShipBuilderIndex; } else { return true; } }
private bool HandleTradeForMoney(MyGuiControlListbox listboxFrom, MyGuiControlListbox listboxTo, MyInventoryItem inventoryItem) { float moneyToAdd = 0f; float itemPrice = GetTradeItemPrice(inventoryItem); // item move from other side to player's ship) if (listboxFrom == m_otherSideInventoryListBox && listboxTo != m_otherSideInventoryListBox) { moneyToAdd = -itemPrice; } // item move from player's ship to other side else if (listboxFrom != m_otherSideInventoryListBox && listboxTo == m_otherSideInventoryListBox) { moneyToAdd = itemPrice; } if (m_money + moneyToAdd < 0f) { // this can't happen throw new MyMwcExceptionApplicationShouldNotGetHere(); //MyGuiManager.AddScreen(new MyGuiScreenMessageBox(MyMessageBoxType.ERROR, // MyTextsWrapperEnum.NotificationYouDontHaveEnoughMoney, // MyTextsWrapperEnum.TradeResultTitle, // MyTextsWrapperEnum.Ok, YouDontHaveEnoughMoneyMessageBoxCallBack)); return false; } SetMoney(m_money + moneyToAdd); return true; }
public void AddInventoryItemForSynchronization(MyInventoryItem item) { m_inventoryItemsToAdd.Add(item); }
private bool NeedHandleSmallShipDrop(MyInventoryItem item) { return item.ObjectBuilderType == MyMwcObjectBuilderTypeEnum.SmallShip_Player && m_inventoryScreenType == MyGuiScreenInventoryType.Game && !m_tradeForMoney; }
/// <summary> /// Adds inventory item to inventory /// </summary> /// <param name="item">Inventory item</param> public void AddInventoryItem(MyInventoryItem item) { AddItemToInventoryPrivate(item); CallContentChange(); }
private void HandleSmallShipDrop(MyGuiControlListbox listboxFrom, MyGuiControlListbox listboxTo, MyInventoryItem item) { if (listboxFrom != listboxTo) { if (listboxTo == m_otherSideInventoryListBox) { MySmallShipBuilderWithName builderWithName = new MySmallShipBuilderWithName(item.GetInventoryItemObjectBuilder(false) as MyMwcObjectBuilder_SmallShip_Player); m_smallShipsBuilders.Add(builderWithName); m_smallShipsInventoryItemIDs.Add(GetInventoryItemIDsFromObjectBuilder(builderWithName.Builder)); if (m_shipsCombobox != null) { m_shipsCombobox.AddItem(m_shipsCombobox.GetItemsCount(), builderWithName.Name); } item.IsTemporaryItem = true; } else if (listboxTo == m_shipInventoryListBox) { MySmallShipBuilderWithName builderWithName = m_smallShipsBuilders.Find(x => x.Builder == item.GetInventoryItemObjectBuilder(false)); Debug.Assert(builderWithName != null); int index = m_smallShipsBuilders.IndexOf(builderWithName); Debug.Assert(index > -1); SaveIntentoryItemsToObjectBuilder(index); item.ObjectBuilder = m_smallShipsBuilders[index].Builder; DealocateInventoryItemsFromInventoryIDs(index); m_smallShipsInventoryItemIDs.RemoveAt(index); m_smallShipsBuilders.RemoveAt(index); if (m_shipsCombobox != null) { m_shipsCombobox.RemoveItemByIndex(index); } if (m_currentShipBuilderIndex > index) { m_currentShipBuilderIndex--; } Debug.Assert(m_currentShipBuilderIndex >= 0 && m_currentShipBuilderIndex <= m_smallShipsBuilders.Count - 1); item.IsTemporaryItem = false; //bool reloadShipInventory = m_currentShipBuilderIndex > index; //m_currentShipBuilderIndex = Math.Min(m_currentShipBuilderIndex, m_smallShipsBuilders.Count - 1); //if (reloadShipInventory) //{ // LoadShipInventory(m_currentShipBuilderIndex); //} } else { throw new MyMwcExceptionApplicationShouldNotGetHere(); } } }