some bug fix, new improved inventory manager (but still very bad but atleast you can give some items inside the bag)
This commit is contained in:
parent
feb6922244
commit
aedf2a885d
@ -61,7 +61,7 @@ namespace Campofinale.Game.Character
|
|||||||
}
|
}
|
||||||
|
|
||||||
int updatedItemCount = 0;
|
int updatedItemCount = 0;
|
||||||
foreach (var item in target.inventoryManager.items)
|
foreach (var item in target.inventoryManager.items.items)
|
||||||
{
|
{
|
||||||
if (item.id.StartsWith("wpn_"))
|
if (item.id.StartsWith("wpn_"))
|
||||||
{
|
{
|
||||||
|
180
Campofinale/Game/Inventory/InventoryList.cs
Normal file
180
Campofinale/Game/Inventory/InventoryList.cs
Normal file
@ -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<Item> items = new();
|
||||||
|
public Dictionary<int, Item> 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
///
|
||||||
|
///<summary>Add a item directly to the bag if there is enough space or increment current stack value</summary>
|
||||||
|
///
|
||||||
|
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<Item> 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<Item> 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
///<summary>
|
||||||
|
///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
|
||||||
|
///</summary>
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -15,7 +15,7 @@ namespace Campofinale.Game.Inventory
|
|||||||
public class InventoryManager
|
public class InventoryManager
|
||||||
{
|
{
|
||||||
public Player owner;
|
public Player owner;
|
||||||
public List<Item> items= new List<Item>();
|
public InventoryList items;
|
||||||
|
|
||||||
public int item_diamond_amt
|
public int item_diamond_amt
|
||||||
{
|
{
|
||||||
@ -36,12 +36,12 @@ namespace Campofinale.Game.Inventory
|
|||||||
|
|
||||||
public Item GetItemById(string id)
|
public Item GetItemById(string id)
|
||||||
{
|
{
|
||||||
return items.Find(i => i.id == id);
|
return items.FindInAll(i => i.id == id);
|
||||||
}
|
}
|
||||||
public InventoryManager(Player o) {
|
public InventoryManager(Player o) {
|
||||||
|
|
||||||
owner = o;
|
owner = o;
|
||||||
|
items=new(o);
|
||||||
}
|
}
|
||||||
public void AddRewards(string rewardTemplateId, Vector3f pos, int sourceType=1)
|
public void AddRewards(string rewardTemplateId, Vector3f pos, int sourceType=1)
|
||||||
{
|
{
|
||||||
@ -110,55 +110,35 @@ namespace Campofinale.Game.Inventory
|
|||||||
}
|
}
|
||||||
public void Save()
|
public void Save()
|
||||||
{
|
{
|
||||||
foreach (Item item in items)
|
foreach (Item item in items.items)
|
||||||
{
|
{
|
||||||
DatabaseManager.db.UpsertItem(item);
|
DatabaseManager.db.UpsertItem(item);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
public void Load()
|
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()
|
|
||||||
{
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
//Logger.Print(id + ": " + amt + " added to new as instance");
|
|
||||||
Item item = new Item(owner.roleId, id, amt);
|
Item item = new Item(owner.roleId, id, amt);
|
||||||
items.Add(item);
|
|
||||||
return item;
|
Item itemNew = items.Add(item);
|
||||||
|
if (notify && itemNew != null)
|
||||||
|
{
|
||||||
|
this.owner.Send(new PacketScItemBagScopeModify(this.owner, itemNew));
|
||||||
}
|
}
|
||||||
|
return item;
|
||||||
}
|
}
|
||||||
public void RemoveItem(Item item,int amt)
|
public void RemoveItem(Item item,int amt)
|
||||||
{
|
{
|
||||||
item.amount -= amt;
|
item.amount -= amt;
|
||||||
if(item.amount <= 0)
|
if(item.amount <= 0)
|
||||||
{
|
{
|
||||||
items.Remove(item);
|
items.items.Remove(item);
|
||||||
DatabaseManager.db.DeleteItem(item);
|
DatabaseManager.db.DeleteItem(item);
|
||||||
}
|
}
|
||||||
|
|
||||||
this.owner.Send(new PacketScItemBagScopeModify(this.owner, item));
|
this.owner.Send(new PacketScItemBagScopeModify(this.owner, item));
|
||||||
}
|
}
|
||||||
public bool ConsumeItems(MapField<string, ulong> costItemId2Count)
|
public bool ConsumeItems(MapField<string, ulong> costItemId2Count)
|
||||||
@ -211,11 +191,11 @@ namespace Campofinale.Game.Inventory
|
|||||||
public Dictionary<uint, int> GetInventoryChapter(string chapterId)
|
public Dictionary<uint, int> GetInventoryChapter(string chapterId)
|
||||||
{
|
{
|
||||||
Dictionary<uint, int> dir= new Dictionary<uint, int>();
|
Dictionary<uint, int> dir= new Dictionary<uint, int>();
|
||||||
List<Item> citems = items.FindAll(i=>!i.InstanceType());
|
/*List<Item> citems = items.FindAll(i=>!i.InstanceType());
|
||||||
foreach (Item item in citems)
|
foreach (Item item in citems)
|
||||||
{
|
{
|
||||||
dir.Add((uint)ResourceManager.strIdNumTable.item_id.dic[item.id], item.amount);
|
dir.Add((uint)ResourceManager.strIdNumTable.item_id.dic[item.id], item.amount);
|
||||||
}
|
}*/
|
||||||
|
|
||||||
return dir;
|
return dir;
|
||||||
}
|
}
|
||||||
|
@ -48,6 +48,10 @@ namespace Campofinale.Game.Inventory
|
|||||||
this.level = level;
|
this.level = level;
|
||||||
guid = GetOwner().random.Next();
|
guid = GetOwner().random.Next();
|
||||||
}
|
}
|
||||||
|
public ItemStorageSpace StorageSpace()
|
||||||
|
{
|
||||||
|
return ResourceManager.itemTypeTable[GetItemTable().type].storageSpace;
|
||||||
|
}
|
||||||
public ulong GetDefaultLevel()
|
public ulong GetDefaultLevel()
|
||||||
{
|
{
|
||||||
switch (ItemType)
|
switch (ItemType)
|
||||||
@ -68,10 +72,15 @@ namespace Campofinale.Game.Inventory
|
|||||||
}
|
}
|
||||||
public ItemValuableDepotType ItemType
|
public ItemValuableDepotType ItemType
|
||||||
{
|
{
|
||||||
get{
|
get
|
||||||
|
{
|
||||||
return ResourceManager.GetItemTable(id).valuableTabType;
|
return ResourceManager.GetItemTable(id).valuableTabType;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
public ItemTable GetItemTable()
|
||||||
|
{
|
||||||
|
return ResourceManager.GetItemTable(id);
|
||||||
|
}
|
||||||
public virtual ScdItemGrid ToProto()
|
public virtual ScdItemGrid ToProto()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
@ -379,8 +379,16 @@ namespace Campofinale.Game
|
|||||||
if (!en.spawned)
|
if (!en.spawned)
|
||||||
{
|
{
|
||||||
en.spawned = true;
|
en.spawned = true;
|
||||||
|
try
|
||||||
|
{
|
||||||
GetOwner().Send(new PacketScObjectEnterView(GetOwner(), new List<Entity>() { en }));
|
GetOwner().Send(new PacketScObjectEnterView(GetOwner(), new List<Entity>() { en }));
|
||||||
}
|
}
|
||||||
|
catch(Exception e)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
@ -15,6 +15,11 @@ namespace Campofinale.Packets.Sc
|
|||||||
|
|
||||||
public PacketScItemBagScopeModify(Player client, Item item) {
|
public PacketScItemBagScopeModify(Player client, Item item) {
|
||||||
|
|
||||||
|
if (item == null)
|
||||||
|
{
|
||||||
|
SetData(ScMsgId.ScItemBagScopeModify, new ScItemBagScopeModify());
|
||||||
|
return;
|
||||||
|
}
|
||||||
ScItemBagScopeModify proto = new ScItemBagScopeModify()
|
ScItemBagScopeModify proto = new ScItemBagScopeModify()
|
||||||
{
|
{
|
||||||
Depot =
|
Depot =
|
||||||
|
@ -19,10 +19,10 @@ namespace Campofinale.Packets.Sc
|
|||||||
{
|
{
|
||||||
Bag = new()
|
Bag = new()
|
||||||
{
|
{
|
||||||
GridLimit = 30,
|
GridLimit = client.inventoryManager.items.maxBagSize,
|
||||||
Grids =
|
Grids =
|
||||||
{
|
{
|
||||||
new ScdItemGrid()
|
/*new ScdItemGrid()
|
||||||
{
|
{
|
||||||
GridIndex=0,
|
GridIndex=0,
|
||||||
Count=1,
|
Count=1,
|
||||||
@ -79,7 +79,7 @@ namespace Campofinale.Packets.Sc
|
|||||||
InstId=300000000004,
|
InstId=300000000004,
|
||||||
|
|
||||||
},
|
},
|
||||||
}
|
}*/
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
FactoryDepot =
|
FactoryDepot =
|
||||||
@ -114,7 +114,17 @@ namespace Campofinale.Packets.Sc
|
|||||||
proto.Bag = null;
|
proto.Bag = null;
|
||||||
}
|
}
|
||||||
proto.Depot.Add(i, new ScdItemDepot());
|
proto.Depot.Add(i, new ScdItemDepot());
|
||||||
List<Item> 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<Item> items = client.inventoryManager.items.items.FindAll(item => item.ItemType == (ItemValuableDepotType)i);
|
||||||
items.ForEach(item =>
|
items.ForEach(item =>
|
||||||
{
|
{
|
||||||
if (item.InstanceType())
|
if (item.InstanceType())
|
||||||
|
@ -215,7 +215,9 @@ namespace Campofinale
|
|||||||
|
|
||||||
foreach(var item in itemTable)
|
foreach(var item in itemTable)
|
||||||
{
|
{
|
||||||
if(item.Value.maxStackCount == -1)
|
if(item.Value.GetStorage()!= ItemStorageSpace.BagAndFactoryDepot)
|
||||||
|
{
|
||||||
|
if (item.Value.maxStackCount == -1)
|
||||||
{
|
{
|
||||||
inventoryManager.items.Add(new Item(roleId, item.Value.id, 10000000));
|
inventoryManager.items.Add(new Item(roleId, item.Value.id, 10000000));
|
||||||
}
|
}
|
||||||
@ -223,6 +225,8 @@ namespace Campofinale
|
|||||||
{
|
{
|
||||||
inventoryManager.items.Add(new Item(roleId, item.Value.id, item.Value.maxStackCount));
|
inventoryManager.items.Add(new Item(roleId, item.Value.id, item.Value.maxStackCount));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
teams.Add(new Team()
|
teams.Add(new Team()
|
||||||
|
@ -67,6 +67,7 @@ namespace Campofinale.Resource
|
|||||||
public static Dictionary<string, FactoryBuildingTable> factoryBuildingTable = new();
|
public static Dictionary<string, FactoryBuildingTable> factoryBuildingTable = new();
|
||||||
public static Dictionary<string, FacSTTNodeTable> facSTTNodeTable = new();
|
public static Dictionary<string, FacSTTNodeTable> facSTTNodeTable = new();
|
||||||
public static Dictionary<string, FacSTTLayerTable> facSTTLayerTable = new();
|
public static Dictionary<string, FacSTTLayerTable> facSTTLayerTable = new();
|
||||||
|
public static Dictionary<int, ItemTypeTable> itemTypeTable = new();
|
||||||
public static InteractiveTable interactiveTable = new();
|
public static InteractiveTable interactiveTable = new();
|
||||||
public static List<LevelScene> levelDatas = new();
|
public static List<LevelScene> levelDatas = new();
|
||||||
public static List<InteractiveData> interactiveData = new();
|
public static List<InteractiveData> interactiveData = new();
|
||||||
@ -134,6 +135,7 @@ namespace Campofinale.Resource
|
|||||||
factoryBuildingTable = JsonConvert.DeserializeObject<Dictionary<string, FactoryBuildingTable>>(ReadJsonFile("TableCfg/FactoryBuildingTable.json"));
|
factoryBuildingTable = JsonConvert.DeserializeObject<Dictionary<string, FactoryBuildingTable>>(ReadJsonFile("TableCfg/FactoryBuildingTable.json"));
|
||||||
facSTTNodeTable = JsonConvert.DeserializeObject<Dictionary<string, FacSTTNodeTable>>(ReadJsonFile("TableCfg/FacSTTNodeTable.json"));
|
facSTTNodeTable = JsonConvert.DeserializeObject<Dictionary<string, FacSTTNodeTable>>(ReadJsonFile("TableCfg/FacSTTNodeTable.json"));
|
||||||
facSTTLayerTable = JsonConvert.DeserializeObject<Dictionary<string, FacSTTLayerTable>>(ReadJsonFile("TableCfg/FacSTTLayerTable.json"));
|
facSTTLayerTable = JsonConvert.DeserializeObject<Dictionary<string, FacSTTLayerTable>>(ReadJsonFile("TableCfg/FacSTTLayerTable.json"));
|
||||||
|
itemTypeTable = JsonConvert.DeserializeObject<Dictionary<int, ItemTypeTable>>(ReadJsonFile("TableCfg/ItemTypeTable.json"));
|
||||||
interactiveTable = JsonConvert.DeserializeObject<InteractiveTable>(ReadJsonFile("Json/Interactive/InteractiveTable.json"));
|
interactiveTable = JsonConvert.DeserializeObject<InteractiveTable>(ReadJsonFile("Json/Interactive/InteractiveTable.json"));
|
||||||
LoadInteractiveData();
|
LoadInteractiveData();
|
||||||
LoadLevelDatas();
|
LoadLevelDatas();
|
||||||
@ -843,6 +845,11 @@ namespace Campofinale.Resource
|
|||||||
public string enemyId;
|
public string enemyId;
|
||||||
public string templateId;
|
public string templateId;
|
||||||
}
|
}
|
||||||
|
public class ItemTypeTable
|
||||||
|
{
|
||||||
|
public int itemType;
|
||||||
|
public ItemStorageSpace storageSpace;
|
||||||
|
}
|
||||||
public class ItemTable
|
public class ItemTable
|
||||||
{
|
{
|
||||||
public ItemValuableDepotType valuableTabType;
|
public ItemValuableDepotType valuableTabType;
|
||||||
@ -850,6 +857,12 @@ namespace Campofinale.Resource
|
|||||||
public int maxStackCount;
|
public int maxStackCount;
|
||||||
public bool backpackCanDiscard;
|
public bool backpackCanDiscard;
|
||||||
public string modelKey;
|
public string modelKey;
|
||||||
|
public int type;
|
||||||
|
|
||||||
|
public ItemStorageSpace GetStorage()
|
||||||
|
{
|
||||||
|
return ResourceManager.itemTypeTable[type].storageSpace;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
public class WeaponBasicTable
|
public class WeaponBasicTable
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user