using System; using System.Collections.Generic; using System.Linq; using static XUiC_RepairItemRebirth; internal class XUiC_RepairItemRebirth : XUiController { public class TrackedItem { public int Index { get; set; } public int Location { get; set; } public string Name { get; set; } public int Type { get; set; } public string Icon { get; set; } public string Blueprint { get; set; } public int Slot { get; set; } public float Durability { get; set; } public int DurabilityMax { get; set; } public int Tier { get; set; } public string Bonus { get; set; } public int BonusLevel { get; set; } public TrackedItem(int index, int location, string name, int type, string icon, string blueprint, int slot, float durability, int durabilityMax, int tier, string bonus, int bonusLevel) { Location = location; Name = name; Type = type; Icon = icon; Blueprint = blueprint; Index = index; Slot = slot; Durability = durability; DurabilityMax = durabilityMax; Tier = tier; Bonus = bonus; BonusLevel = bonusLevel; } } private static float uiCheck = 0f; public float uiTick = 0.5f; public float barterPerc = 0f; public float durabilityThreshold = 0.5f; public float multiplierCash = 2f; public float multiplierBlueprint = 1f; public float tierMultiplier = 0.35f; public float barterPercUnit = 0.04f; public bool cannotTrade = false; public XUiV_Label LabelTotal; private XUiC_ComboBoxList cbxTargetItem; public XUiV_Label LabelDescription; public XUiV_Panel Panel; public XUiV_Label lblNameLabel; public XUiV_Label lblName; public XUiV_Label lblLocationLabel; public XUiV_Label lblLocation; public XUiV_Label lblSlotLabel; public XUiV_Label lblSlot; public XUiV_Label lblDurabilityLabel; public XUiV_Label lblDurability; public XUiV_Label lblDurabilityPercLabel; public XUiV_Label lblDurabilityPerc; public XUiV_Label lblTotalCash; public XUiV_Label lblTotalBlueprints; public XUiV_Label lblBonusLabel; public XUiV_Label lblBonus; public XUiV_Label lblRepairCost; public XUiV_Sprite spriteItemImage; public XUiV_Sprite spriteBonusTypeIcon; public XUiV_Sprite cashImage; public XUiV_Sprite blueprintImage; private List trackedItems = new List(); private TrackedItem currentTrackedItem; public override void Init() { base.Init(); Panel = (XUiV_Panel)GetChildById("Popup").ViewComponent; //((XUiC_SimpleButton)Panel.Controller.GetChildById("btnDamage")).OnPressed += BtnDamage_OnPressed; ((XUiC_SimpleButton)Panel.Controller.GetChildById("btnRepair")).OnPressed += BtnRepair_OnPressed; ((XUiC_SimpleButton)Panel.Controller.GetChildById("btnCancel")).OnPressed += BtnCancel_OnPressed; LabelDescription = (XUiV_Label)Panel.Controller.GetChildById("lblDescription").ViewComponent; lblNameLabel = (XUiV_Label)Panel.Controller.GetChildById("lblNameLabel").ViewComponent; lblName = (XUiV_Label)Panel.Controller.GetChildById("lblName").ViewComponent; lblLocationLabel = (XUiV_Label)Panel.Controller.GetChildById("lblLocationLabel").ViewComponent; lblLocation = (XUiV_Label)Panel.Controller.GetChildById("lblLocation").ViewComponent; lblSlotLabel = (XUiV_Label)Panel.Controller.GetChildById("lblSlotLabel").ViewComponent; lblSlot = (XUiV_Label)Panel.Controller.GetChildById("lblSlot").ViewComponent; lblDurabilityLabel = (XUiV_Label)Panel.Controller.GetChildById("lblDurabilityLabel").ViewComponent; lblDurability = (XUiV_Label)Panel.Controller.GetChildById("lblDurability").ViewComponent; lblDurabilityPercLabel = (XUiV_Label)Panel.Controller.GetChildById("lblDurabilityPercLabel").ViewComponent; lblDurabilityPerc = (XUiV_Label)Panel.Controller.GetChildById("lblDurabilityPerc").ViewComponent; lblTotalCash = (XUiV_Label)Panel.Controller.GetChildById("lblTotalCash").ViewComponent; lblTotalBlueprints = (XUiV_Label)Panel.Controller.GetChildById("lblTotalBlueprints").ViewComponent; lblBonusLabel = (XUiV_Label)Panel.Controller.GetChildById("lblBonusLabel").ViewComponent; lblBonus = (XUiV_Label)Panel.Controller.GetChildById("lblBonus").ViewComponent; lblRepairCost = (XUiV_Label)Panel.Controller.GetChildById("lblRepairCost").ViewComponent; spriteItemImage = (XUiV_Sprite)GetChildById("itemImage").ViewComponent; spriteBonusTypeIcon = (XUiV_Sprite)GetChildById("bonusTypeIcon").ViewComponent; cashImage = (XUiV_Sprite)GetChildById("CashImage").ViewComponent; blueprintImage = (XUiV_Sprite)GetChildById("BlueprintImage").ViewComponent; this.cbxTargetItem = (base.GetChildById("cbxItem") as XUiC_ComboBoxList); if (this.cbxTargetItem != null) { this.cbxTargetItem.OnValueChanged += this.cbxTargetAmmoType_OnValueChanged; } } private void updateItem() { bool bEmpty = true; EntityPlayer player = xui.playerUI.entityPlayer; currentTrackedItem = null; if (this.cbxTargetItem.Elements.Count > 0) { TrackedItem trackedItem = trackedItems.FirstOrDefault(item => item.Index == int.Parse(cbxTargetItem.Value)); if (trackedItem != null) { currentTrackedItem = trackedItem; spriteItemImage.SpriteName = "ui_game_symbol_" + trackedItem.Icon; lblName.Text = Localization.Get(trackedItem.Name); string location = "-"; if (trackedItem.Location == 0) { location = "ttInventory"; } else if (trackedItem.Location == 1) { location = "ttBackpack"; } else if (trackedItem.Location == 2) { location = "ttCharacter"; } lblLocation.Text = Localization.Get(location); lblSlot.Text = (trackedItem.Slot + 1).ToString(); lblDurability.Text = trackedItem.DurabilityMax.ToString(); lblDurabilityPerc.Text = RebirthUtilities.FormatPercentage(trackedItem.Durability, 0); cashImage.spriteName = "ui_game_symbol_oldCash"; blueprintImage.spriteName = "ui_game_symbol_FR_Blueprint" + trackedItem.Blueprint; lblTotalCash.text = ""; lblTotalBlueprints.Text = ""; bEmpty = false; } } string repairCost = Localization.Get("xuiRepairItemExchange"); if (bEmpty) { lblName.Text = "-"; lblLocation.Text = "-"; lblSlot.Text = "-"; lblDurability.Text = "-"; lblDurabilityPerc.Text = "-"; lblBonus.Text = "-"; spriteItemImage.spriteName = ""; spriteBonusTypeIcon.spriteName = ""; cashImage.spriteName = ""; blueprintImage.spriteName = ""; cashImage.isDirty = true; blueprintImage.isDirty = true; lblTotalCash.Text = ""; lblTotalBlueprints.Text = ""; ((XUiC_SimpleButton)Panel.Controller.GetChildById("btnRepair")).Enabled = false; repairCost = Localization.Get("xuiRepairItemNothingExchange"); } else { cannotTrade = false; float durability = 1 - currentTrackedItem.Durability; float multiplier = 1 + ((float)(currentTrackedItem.Tier * tierMultiplier)); int numBlueprints = (int)(0.16 * (durability * 100 * multiplier) * (1 - barterPerc) * multiplierBlueprint); int cash = (int)(9 * (durability * 100 * multiplier) * (1 - barterPerc) * multiplierCash); string cashColor = "[ffffff]"; string blueprintColor = "[ffffff]"; int totalCash = RebirthUtilities.numItems(ItemClass.GetItem("casinoCoin"), player); int totalBlueprints = RebirthUtilities.numItems(ItemClass.GetItem("geneticBlueprint" + currentTrackedItem.Blueprint), player); if (totalCash < cash) { cashColor = "[c79595]"; cannotTrade = true; } if (totalBlueprints < numBlueprints) { blueprintColor = "[c79595]"; cannotTrade = true; } lblTotalCash.Text = cashColor + cash.ToString() + "[-]"; lblTotalBlueprints.Text = blueprintColor + numBlueprints.ToString() + "[-]"; if (cannotTrade) { ((XUiC_SimpleButton)Panel.Controller.GetChildById("btnRepair")).Enabled = false; } else { ((XUiC_SimpleButton)Panel.Controller.GetChildById("btnRepair")).Enabled = true; } spriteBonusTypeIcon.spriteName = "ui_game_symbol_" + currentTrackedItem.Bonus; if (currentTrackedItem.Bonus.Trim().Length > 0) { lblBonus.Text = $"{Localization.Get("xui" + currentTrackedItem.Bonus)} [c9c7c7]([-]{Localization.Get("ttLevelFull")} [e0dcab]{currentTrackedItem.BonusLevel}[-][c9c7c7])[-]"; } else { lblBonus.Text = "-"; } lblBonus.IsDirty = true; } lblRepairCost.Text = repairCost; spriteItemImage.isDirty = true; spriteBonusTypeIcon.isDirty = true; cashImage.isDirty = true; blueprintImage.isDirty = true; } private void cbxTargetAmmoType_OnValueChanged(XUiController _sender, string _oldValue, string _newValue) { updateItem(); } public override void Update(float _dt) { base.Update(_dt); if ((Time.time - uiCheck) > uiTick) { uiCheck = Time.time; //Log.Out("XUiC_RepairItemRebirth-Update 1"); } } public void Reset() { //Log.Out("XUiC_RepairItemRebirth-Reset START"); EntityPlayer player = xui.playerUI.entityPlayer; // Reset the tracking list trackedItems.Clear(); cbxTargetItem.Elements.Clear(); int index = 0; for (int _idx = 0; _idx < player.inventory.slots.Length - 1; ++_idx) { //Log.Out("XUiC_RepairItemRebirth-Reset A _idx: " + _idx); ItemClass itemClass = ItemClass.GetForId(player.inventory.slots[_idx].itemStack.itemValue.type); ItemStack itemStack = player.inventory.slots[_idx].itemStack; ItemValue itemValue = player.inventory.slots[_idx].itemStack.itemValue; if (!itemStack.IsEmpty() && !itemClass.CanStack() ) { //Log.Out("XUiC_RepairItemRebirth-Reset A Name: " + itemClass.Name); //Log.Out("XUiC_RepairItemRebirth-Reset A ItemTags: " + itemClass.ItemTags); //Log.Out("XUiC_RepairItemRebirth-Reset A itemClass.RepairTools == null: " + (itemClass.RepairTools == null)); //Log.Out("XUiC_RepairItemRebirth-Reset A itemValue.PercentUsesLeft: " + itemValue.PercentUsesLeft); if (itemClass.HasAnyTags(FastTags.Parse("weapon,melee,armor")) && itemClass.RepairTools == null && itemValue.PercentUsesLeft < durabilityThreshold) { //Log.Out("XUiC_RepairItemRebirth-Reset A Added: " + itemClass.Name); index++; this.cbxTargetItem.Elements.Add(index.ToString()); int type = RebirthUtilities.GetType(itemClass); int tier = RebirthUtilities.GetTier(itemClass); string blueprint = RebirthUtilities.GetBlueprint(itemClass); string bonus = ""; int bonusLevel = 0; if (itemValue != null && itemValue.HasMetadata("bonus") && itemValue.HasMetadata("level") && itemValue.HasMetadata("type") && itemValue.HasMetadata("active")) { bonus = (string)itemValue.GetMetadata("bonus"); bonusLevel = (int)itemValue.GetMetadata("level"); } trackedItems.Add(new TrackedItem(index, 0, itemClass.Name, type, itemClass.GetIconName(), blueprint, _idx, itemValue.PercentUsesLeft, itemValue.MaxUseTimes, tier, bonus, bonusLevel)); } } } ItemStack[] itemStackArray = player.bag.GetSlots(); for (int _idx = 0; _idx < itemStackArray.Length; ++_idx) { //Log.Out("XUiC_RepairItemRebirth-Reset B _idx: " + _idx); ItemClass itemClass = ItemClass.GetForId(itemStackArray[_idx].itemValue.type); ItemStack itemStack = itemStackArray[_idx]; ItemValue itemValue = itemStackArray[_idx].itemValue; if (!itemStack.IsEmpty() && !itemClass.CanStack() ) { //Log.Out("XUiC_RepairItemRebirth-Reset B Name: " + itemClass.Name); //Log.Out("XUiC_RepairItemRebirth-Reset B ItemTags: " + itemClass.ItemTags); //Log.Out("XUiC_RepairItemRebirth-Reset B itemClass.RepairTools == null: " + (itemClass.RepairTools == null)); //Log.Out("XUiC_RepairItemRebirth-Reset B itemValue.PercentUsesLeft: " + itemValue.PercentUsesLeft); if (itemClass.HasAnyTags(FastTags.Parse("weapon,melee,armor")) && itemClass.RepairTools == null && itemValue.PercentUsesLeft < durabilityThreshold ) { //Log.Out("XUiC_RepairItemRebirth-Reset B Added: " + itemClass.Name); index++; this.cbxTargetItem.Elements.Add(index.ToString()); int type = RebirthUtilities.GetType(itemClass); int tier = RebirthUtilities.GetTier(itemClass); string blueprint = RebirthUtilities.GetBlueprint(itemClass); string bonus = ""; int bonusLevel = 0; if (itemValue != null && itemValue.HasMetadata("bonus") && itemValue.HasMetadata("level") && itemValue.HasMetadata("type") && itemValue.HasMetadata("active")) { bonus = (string)itemValue.GetMetadata("bonus"); bonusLevel = (int)itemValue.GetMetadata("level"); } trackedItems.Add(new TrackedItem(index, 1, itemClass.Name, type, itemClass.GetIconName(), blueprint, _idx, itemValue.PercentUsesLeft, itemValue.MaxUseTimes, tier, bonus, bonusLevel)); } } } for (int _idx = 0; _idx < player.equipment.GetSlotCount(); ++_idx) { //Log.Out("XUiC_RepairItemRebirth-Reset C _idx: " + _idx); ItemValue slotItem = player.equipment.GetSlotItem(_idx); if (slotItem != null && slotItem.ItemClass != null && slotItem.ItemClass.SDCSData != null) { ItemClass itemClass = slotItem.ItemClass; ItemValue itemValue = slotItem; //Log.Out("XUiC_RepairItemRebirth-Reset C Name: " + itemClass.Name); //Log.Out("XUiC_RepairItemRebirth-Reset C ItemTags: " + itemClass.ItemTags); //Log.Out("XUiC_RepairItemRebirth-Reset C itemClass.RepairTools == null: " + (itemClass.RepairTools == null)); //Log.Out("XUiC_RepairItemRebirth-Reset C itemValue.PercentUsesLeft: " + itemValue.PercentUsesLeft); if (itemValue.PercentUsesLeft < durabilityThreshold) { //Log.Out("XUiC_RepairItemRebirth-Reset C ADDED: " + itemClass.Name); index++; this.cbxTargetItem.Elements.Add(index.ToString()); int type = 3; int tier = RebirthUtilities.GetTier(itemClass); string blueprint = RebirthUtilities.GetBlueprint(itemClass); string bonus = ""; int bonusLevel = 0; if (itemValue != null && itemValue.HasMetadata("bonus") && itemValue.HasMetadata("level") && itemValue.HasMetadata("type") && itemValue.HasMetadata("active")) { bonus = (string)itemValue.GetMetadata("bonus"); bonusLevel = (int)itemValue.GetMetadata("level"); } // Track the item trackedItems.Add(new TrackedItem(index, 2, itemClass.Name, type, itemClass.GetIconName(), blueprint, _idx, itemValue.PercentUsesLeft, itemValue.MaxUseTimes, tier, bonus, bonusLevel)); } } } /*foreach (var item in trackedItems) { Log.Out("XUiC_RepairItemRebirth-Reset item.Name: " + item.Name); Log.Out("XUiC_RepairItemRebirth-Reset item.Index: " + item.Index); Log.Out("XUiC_RepairItemRebirth-Reset item.Location: " + item.Location); Log.Out("XUiC_RepairItemRebirth-Reset item.Icon: " + item.Icon); Log.Out("XUiC_RepairItemRebirth-Reset item.Slot: " + item.Slot); Log.Out("XUiC_RepairItemRebirth-Reset item.Durability: " + item.Durability); }*/ if (this.cbxTargetItem.Elements.Count > 0) { cbxTargetItem.SelectedIndex = 0; } updateItem(); float progressionLevel = 0; ProgressionValue progressionValue = player.Progression.GetProgressionValue("perkBetterBarter"); if (progressionValue != null) { progressionLevel = RebirthUtilities.GetCalculatedLevel(player, progressionValue); } barterPerc = progressionLevel * barterPercUnit; LabelDescription.Text = Localization.Get("xuiItemsToRepairDescription").Replace("{0}", ((int)(barterPerc * 100)).ToString() + "%"); } public override void OnOpen() { Reset(); base.OnOpen(); } private void BtnCancel_OnPressed(XUiController _sender, int _mouseButton) { //Log.Out("XUiC_RepairItemRebirth-BtnCancel_OnPressed START"); Panel.IsVisible = false; xui.playerUI.windowManager.Close(windowGroup.ID); } private void BtnRepair_OnPressed(XUiController _sender, int _mouseButton) { //Log.Out("XUiC_RepairItemRebirth-BtnRepair_OnPressed currentTrackedItem.Slot: " + currentTrackedItem.Slot); EntityPlayer player = xui.playerUI.entityPlayer; ItemValue itemValue = null; if (currentTrackedItem.Location == 0) { itemValue = player.inventory.slots[currentTrackedItem.Slot].itemStack.itemValue; } else if (currentTrackedItem.Location == 1) { ItemStack[] itemStackArray = player.bag.GetSlots(); itemValue = itemStackArray[currentTrackedItem.Slot].itemValue; } else if (currentTrackedItem.Location == 2) { itemValue = player.equipment.GetSlotItem(currentTrackedItem.Slot); } if (itemValue != null) { itemValue.UseTimes = 0; currentTrackedItem.Durability = 0; float durability = 1 - currentTrackedItem.Durability; float multiplier = 1 + ((float)(currentTrackedItem.Tier * tierMultiplier)); int numBlueprints = (int)(0.16 * (durability * 100 * multiplier) * (1 - barterPerc) * multiplierBlueprint); int cash = (int)(9 * (durability * 100 * multiplier) * (1 - barterPerc) * multiplierCash); RebirthUtilities.useInventoryBagItem(player, "casinoCoin", cash); RebirthUtilities.useInventoryBagItem(player, "geneticBlueprint" + currentTrackedItem.Blueprint, numBlueprints); if (currentTrackedItem.Location == 0) { player.inventory.ForceHoldingItemUpdate(); } Reset(); } } private void BtnDamage_OnPressed(XUiController _sender, int _mouseButton) { EntityPlayer player = xui.playerUI.entityPlayer; currentTrackedItem.Durability = 0; ItemValue itemValue = null; if (currentTrackedItem.Location == 0) { itemValue = player.inventory.slots[currentTrackedItem.Slot].itemStack.itemValue; } else if (currentTrackedItem.Location == 1) { ItemStack[] itemStackArray = player.bag.GetSlots(); itemValue = itemStackArray[currentTrackedItem.Slot].itemValue; } else if (currentTrackedItem.Location == 2) { itemValue = player.equipment.GetSlotItem(currentTrackedItem.Slot); } if (itemValue != null) { itemValue.UseTimes = itemValue.MaxUseTimes; } Reset(); } public override void OnClose() { //Log.Out("XUiC_RepairItemRebirth-OnClose START"); if (xui.playerUI.windowManager.HasWindow("dialog") && xui.playerUI.windowManager.IsWindowOpen("dialog")) xui.playerUI.windowManager.Close("dialog"); base.OnClose(); } }