From aedf2a885d240d1118351463915496306065a2b1 Mon Sep 17 00:00:00 2001 From: AlessandroCH Date: Sun, 9 Mar 2025 16:24:46 +0100 Subject: [PATCH] some bug fix, new improved inventory manager (but still very bad but atleast you can give some items inside the bag) --- Campofinale/Commands/Handlers/CommandLevel.cs | 2 +- Campofinale/Game/Inventory/InventoryList.cs | 180 ++++++++++++++++++ .../Game/Inventory/InventoryManager.cs | 52 ++--- Campofinale/Game/Inventory/Item.cs | 11 +- Campofinale/Game/SceneManager.cs | 10 +- .../Packets/Sc/PacketScItemBagScopeModify.cs | 5 + .../Packets/Sc/PacketScItemBagScopeSync.cs | 18 +- Campofinale/Player.cs | 16 +- Campofinale/Resource/ResourceManager.cs | 13 ++ 9 files changed, 258 insertions(+), 49 deletions(-) create mode 100644 Campofinale/Game/Inventory/InventoryList.cs diff --git a/Campofinale/Commands/Handlers/CommandLevel.cs b/Campofinale/Commands/Handlers/CommandLevel.cs index 955a8c8..b91a550 100644 --- a/Campofinale/Commands/Handlers/CommandLevel.cs +++ b/Campofinale/Commands/Handlers/CommandLevel.cs @@ -61,7 +61,7 @@ namespace Campofinale.Game.Character } int updatedItemCount = 0; - foreach (var item in target.inventoryManager.items) + foreach (var item in target.inventoryManager.items.items) { if (item.id.StartsWith("wpn_")) { diff --git a/Campofinale/Game/Inventory/InventoryList.cs b/Campofinale/Game/Inventory/InventoryList.cs new file mode 100644 index 0000000..e7d4fa5 --- /dev/null +++ b/Campofinale/Game/Inventory/InventoryList.cs @@ -0,0 +1,180 @@ +using Campofinale.Packets.Sc; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Campofinale.Game.Inventory +{ + public class InventoryList + { + public List items = new(); + public Dictionary bag = new(); + public int maxBagSize = 30; + + public Player player; + public InventoryList(Player player) + { + this.player = player; + } + public enum FindType + { + Items, + FactoryDepots, + Bag + } + public void UpdateInventoryPacket() + { + + } + public void UpdateBagInventoryPacket() + { + player.Send(new PacketScItemBagScopeSync(this.player,Resource.ItemValuableDepotType.Invalid)); + + } + private void AddToBagAvailableSlot(Item item) + { + for (int i = 0; i < maxBagSize; i++) + { + if (!bag.ContainsKey(i)) + { + bag.Add(i, item); + return; + } + } + } + /// + ///Add a item directly to the bag if there is enough space or increment current stack value + /// + public bool AddToBag(Item item) + { + Item existOne = Find(i=>i.id == item.id && i.amount < item.GetItemTable().maxStackCount,FindType.Bag); + if (existOne != null) + { + + if(existOne.amount+item.amount > item.GetItemTable().maxStackCount) + { + int max = existOne.GetItemTable().maxStackCount; + int toAddInNewSlotAmount = existOne.amount + item.amount - max; + item.amount = toAddInNewSlotAmount; + if (SlotAvailableInBag()) + { + existOne.amount = max; + AddToBagAvailableSlot(item); + UpdateBagInventoryPacket(); + return true; + } + else + { + return false; + } + } + else + { + existOne.amount += item.amount; + UpdateBagInventoryPacket(); + return true; + } + } + else + { + if(bag.Count < maxBagSize) + { + AddToBagAvailableSlot(item); + UpdateBagInventoryPacket(); + return true; + } + else + { + return false; + } + } + } + public bool SlotAvailableInBag() + { + bool availableSlot = false; + for (int i = 0; i < maxBagSize; i++) + { + if (!bag.ContainsKey(i)) + { + return true; + } + } + return availableSlot; + } + public Item FindInAll(Predicate match) + { + var item = items.Find(match); + if (item != null) + { + return item; + } + var itemB = bag.Values.ToList().Find(match); + if (itemB != null) + { + return itemB; + } + return null; + } + public Item Find(Predicate match,FindType findType = FindType.Items) + { + switch (findType) + { + case FindType.Items: + var item = items.Find(match); + if (item != null) + { + return item; + } + break; + case FindType.FactoryDepots: + //TODO + break; + case FindType.Bag: + var itemB = bag.Values.ToList().Find(match); + if (itemB != null) + { + return itemB; + } + break; + } + + return null; + } + + /// + ///Add an item to the inventory (or increment it's amount if it's not an instance type, else add a new one or add to bag if it's a Factory item + /// + public Item Add(Item item) + { + if (item.StorageSpace() == Resource.ItemStorageSpace.BagAndFactoryDepot) + { + AddToBag(item); + return null; + } + if (item.InstanceType()) + { + items.Add(item); + return item; + } + else + { + Item exist=Find(i=>i.id==item.id); + if (exist != null) + { + exist.amount += item.amount; + return exist; + } + else + { + items.Add(item); + return item; + } + } + + } + + + } +} diff --git a/Campofinale/Game/Inventory/InventoryManager.cs b/Campofinale/Game/Inventory/InventoryManager.cs index 3ded7b1..03851f3 100644 --- a/Campofinale/Game/Inventory/InventoryManager.cs +++ b/Campofinale/Game/Inventory/InventoryManager.cs @@ -15,7 +15,7 @@ namespace Campofinale.Game.Inventory public class InventoryManager { public Player owner; - public List items= new List(); + public InventoryList items; public int item_diamond_amt { @@ -36,12 +36,12 @@ namespace Campofinale.Game.Inventory public Item GetItemById(string id) { - return items.Find(i => i.id == id); + return items.FindInAll(i => i.id == id); } public InventoryManager(Player o) { owner = o; - + items=new(o); } public void AddRewards(string rewardTemplateId, Vector3f pos, int sourceType=1) { @@ -110,55 +110,35 @@ namespace Campofinale.Game.Inventory } public void Save() { - foreach (Item item in items) + foreach (Item item in items.items) { DatabaseManager.db.UpsertItem(item); } } public void Load() { - items = DatabaseManager.db.LoadInventoryItems(owner.roleId); + items.items = DatabaseManager.db.LoadInventoryItems(owner.roleId); } - public Item AddItem(string id, int amt) + public Item AddItem(string id, int amt, bool notify=false) { - Item it = new() + Item item = new Item(owner.roleId, id, amt); + + Item itemNew = items.Add(item); + if (notify && itemNew != null) { - id = id, - }; - if(!it.InstanceType()) - { - - Item item = items.Find(i=>i.id == id); - if (item != null) - { - // Logger.Print(id + ": " + amt+" added to existing"); - item.amount += amt; - return item; - } - else - { - // Logger.Print(id + ": " + amt + " added to new"); - item = new Item(owner.roleId, id, amt); - items.Add(item); - return item; - } + this.owner.Send(new PacketScItemBagScopeModify(this.owner, itemNew)); } - else - { - //Logger.Print(id + ": " + amt + " added to new as instance"); - Item item = new Item(owner.roleId, id, amt); - items.Add(item); - return item; - } + return item; } public void RemoveItem(Item item,int amt) { item.amount -= amt; if(item.amount <= 0) { - items.Remove(item); + items.items.Remove(item); DatabaseManager.db.DeleteItem(item); } + this.owner.Send(new PacketScItemBagScopeModify(this.owner, item)); } public bool ConsumeItems(MapField costItemId2Count) @@ -211,11 +191,11 @@ namespace Campofinale.Game.Inventory public Dictionary GetInventoryChapter(string chapterId) { Dictionary dir= new Dictionary(); - List citems = items.FindAll(i=>!i.InstanceType()); + /*List citems = items.FindAll(i=>!i.InstanceType()); foreach (Item item in citems) { dir.Add((uint)ResourceManager.strIdNumTable.item_id.dic[item.id], item.amount); - } + }*/ return dir; } diff --git a/Campofinale/Game/Inventory/Item.cs b/Campofinale/Game/Inventory/Item.cs index 2a5bf89..d887947 100644 --- a/Campofinale/Game/Inventory/Item.cs +++ b/Campofinale/Game/Inventory/Item.cs @@ -48,6 +48,10 @@ namespace Campofinale.Game.Inventory this.level = level; guid = GetOwner().random.Next(); } + public ItemStorageSpace StorageSpace() + { + return ResourceManager.itemTypeTable[GetItemTable().type].storageSpace; + } public ulong GetDefaultLevel() { switch (ItemType) @@ -68,10 +72,15 @@ namespace Campofinale.Game.Inventory } public ItemValuableDepotType ItemType { - get{ + get + { return ResourceManager.GetItemTable(id).valuableTabType; } } + public ItemTable GetItemTable() + { + return ResourceManager.GetItemTable(id); + } public virtual ScdItemGrid ToProto() { try diff --git a/Campofinale/Game/SceneManager.cs b/Campofinale/Game/SceneManager.cs index 781ade0..1085781 100644 --- a/Campofinale/Game/SceneManager.cs +++ b/Campofinale/Game/SceneManager.cs @@ -379,7 +379,15 @@ namespace Campofinale.Game if (!en.spawned) { en.spawned = true; - GetOwner().Send(new PacketScObjectEnterView(GetOwner(), new List() { en })); + try + { + GetOwner().Send(new PacketScObjectEnterView(GetOwner(), new List() { en })); + } + catch(Exception e) + { + + } + } } else diff --git a/Campofinale/Packets/Sc/PacketScItemBagScopeModify.cs b/Campofinale/Packets/Sc/PacketScItemBagScopeModify.cs index abea4a1..2f3fc24 100644 --- a/Campofinale/Packets/Sc/PacketScItemBagScopeModify.cs +++ b/Campofinale/Packets/Sc/PacketScItemBagScopeModify.cs @@ -15,6 +15,11 @@ namespace Campofinale.Packets.Sc public PacketScItemBagScopeModify(Player client, Item item) { + if (item == null) + { + SetData(ScMsgId.ScItemBagScopeModify, new ScItemBagScopeModify()); + return; + } ScItemBagScopeModify proto = new ScItemBagScopeModify() { Depot = diff --git a/Campofinale/Packets/Sc/PacketScItemBagScopeSync.cs b/Campofinale/Packets/Sc/PacketScItemBagScopeSync.cs index 6b8584a..7cd2dfc 100644 --- a/Campofinale/Packets/Sc/PacketScItemBagScopeSync.cs +++ b/Campofinale/Packets/Sc/PacketScItemBagScopeSync.cs @@ -19,10 +19,10 @@ namespace Campofinale.Packets.Sc { Bag = new() { - GridLimit = 30, + GridLimit = client.inventoryManager.items.maxBagSize, Grids = { - new ScdItemGrid() + /*new ScdItemGrid() { GridIndex=0, Count=1, @@ -79,7 +79,7 @@ namespace Campofinale.Packets.Sc InstId=300000000004, }, - } + }*/ } }, FactoryDepot = @@ -114,7 +114,17 @@ namespace Campofinale.Packets.Sc proto.Bag = null; } proto.Depot.Add(i, new ScdItemDepot()); - List items = client.inventoryManager.items.FindAll(item => item.ItemType == (ItemValuableDepotType)i); + foreach (var item in client.inventoryManager.items.bag) + { + proto.Bag.Grids.Add(new ScdItemGrid() + { + Count=item.Value.amount, + GridIndex=item.Key, + Id=item.Value.id, + Inst=item.Value.ToProto().Inst, + }); + } + List items = client.inventoryManager.items.items.FindAll(item => item.ItemType == (ItemValuableDepotType)i); items.ForEach(item => { if (item.InstanceType()) diff --git a/Campofinale/Player.cs b/Campofinale/Player.cs index f56b107..382272d 100644 --- a/Campofinale/Player.cs +++ b/Campofinale/Player.cs @@ -215,15 +215,19 @@ namespace Campofinale foreach(var item in itemTable) { - if(item.Value.maxStackCount == -1) + if(item.Value.GetStorage()!= ItemStorageSpace.BagAndFactoryDepot) { - inventoryManager.items.Add(new Item(roleId, item.Value.id, 10000000)); - } - else - { - inventoryManager.items.Add(new Item(roleId, item.Value.id, item.Value.maxStackCount)); + if (item.Value.maxStackCount == -1) + { + inventoryManager.items.Add(new Item(roleId, item.Value.id, 10000000)); + } + else + { + inventoryManager.items.Add(new Item(roleId, item.Value.id, item.Value.maxStackCount)); + } } + } teams.Add(new Team() { diff --git a/Campofinale/Resource/ResourceManager.cs b/Campofinale/Resource/ResourceManager.cs index efeb9ca..ae726a4 100644 --- a/Campofinale/Resource/ResourceManager.cs +++ b/Campofinale/Resource/ResourceManager.cs @@ -67,6 +67,7 @@ namespace Campofinale.Resource public static Dictionary factoryBuildingTable = new(); public static Dictionary facSTTNodeTable = new(); public static Dictionary facSTTLayerTable = new(); + public static Dictionary itemTypeTable = new(); public static InteractiveTable interactiveTable = new(); public static List levelDatas = new(); public static List interactiveData = new(); @@ -134,6 +135,7 @@ namespace Campofinale.Resource factoryBuildingTable = JsonConvert.DeserializeObject>(ReadJsonFile("TableCfg/FactoryBuildingTable.json")); facSTTNodeTable = JsonConvert.DeserializeObject>(ReadJsonFile("TableCfg/FacSTTNodeTable.json")); facSTTLayerTable = JsonConvert.DeserializeObject>(ReadJsonFile("TableCfg/FacSTTLayerTable.json")); + itemTypeTable = JsonConvert.DeserializeObject>(ReadJsonFile("TableCfg/ItemTypeTable.json")); interactiveTable = JsonConvert.DeserializeObject(ReadJsonFile("Json/Interactive/InteractiveTable.json")); LoadInteractiveData(); LoadLevelDatas(); @@ -843,6 +845,11 @@ namespace Campofinale.Resource public string enemyId; public string templateId; } + public class ItemTypeTable + { + public int itemType; + public ItemStorageSpace storageSpace; + } public class ItemTable { public ItemValuableDepotType valuableTabType; @@ -850,6 +857,12 @@ namespace Campofinale.Resource public int maxStackCount; public bool backpackCanDiscard; public string modelKey; + public int type; + + public ItemStorageSpace GetStorage() + { + return ResourceManager.itemTypeTable[type].storageSpace; + } } public class WeaponBasicTable {