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;
|
||||
foreach (var item in target.inventoryManager.items)
|
||||
foreach (var item in target.inventoryManager.items.items)
|
||||
{
|
||||
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 Player owner;
|
||||
public List<Item> items= new List<Item>();
|
||||
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()
|
||||
{
|
||||
id = id,
|
||||
};
|
||||
if(!it.InstanceType())
|
||||
{
|
||||
Item item = new Item(owner.roleId, id, amt);
|
||||
|
||||
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
|
||||
Item itemNew = items.Add(item);
|
||||
if (notify && itemNew != null)
|
||||
{
|
||||
//Logger.Print(id + ": " + amt + " added to new as instance");
|
||||
Item item = new Item(owner.roleId, id, amt);
|
||||
items.Add(item);
|
||||
return item;
|
||||
this.owner.Send(new PacketScItemBagScopeModify(this.owner, itemNew));
|
||||
}
|
||||
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<string, ulong> costItemId2Count)
|
||||
@ -211,11 +191,11 @@ namespace Campofinale.Game.Inventory
|
||||
public Dictionary<uint, int> GetInventoryChapter(string chapterId)
|
||||
{
|
||||
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)
|
||||
{
|
||||
dir.Add((uint)ResourceManager.strIdNumTable.item_id.dic[item.id], item.amount);
|
||||
}
|
||||
}*/
|
||||
|
||||
return dir;
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -379,7 +379,15 @@ namespace Campofinale.Game
|
||||
if (!en.spawned)
|
||||
{
|
||||
en.spawned = true;
|
||||
GetOwner().Send(new PacketScObjectEnterView(GetOwner(), new List<Entity>() { en }));
|
||||
try
|
||||
{
|
||||
GetOwner().Send(new PacketScObjectEnterView(GetOwner(), new List<Entity>() { en }));
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
else
|
||||
|
@ -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 =
|
||||
|
@ -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<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 =>
|
||||
{
|
||||
if (item.InstanceType())
|
||||
|
@ -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()
|
||||
{
|
||||
|
@ -67,6 +67,7 @@ namespace Campofinale.Resource
|
||||
public static Dictionary<string, FactoryBuildingTable> factoryBuildingTable = new();
|
||||
public static Dictionary<string, FacSTTNodeTable> facSTTNodeTable = new();
|
||||
public static Dictionary<string, FacSTTLayerTable> facSTTLayerTable = new();
|
||||
public static Dictionary<int, ItemTypeTable> itemTypeTable = new();
|
||||
public static InteractiveTable interactiveTable = new();
|
||||
public static List<LevelScene> levelDatas = new();
|
||||
public static List<InteractiveData> interactiveData = new();
|
||||
@ -134,6 +135,7 @@ namespace Campofinale.Resource
|
||||
factoryBuildingTable = JsonConvert.DeserializeObject<Dictionary<string, FactoryBuildingTable>>(ReadJsonFile("TableCfg/FactoryBuildingTable.json"));
|
||||
facSTTNodeTable = JsonConvert.DeserializeObject<Dictionary<string, FacSTTNodeTable>>(ReadJsonFile("TableCfg/FacSTTNodeTable.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"));
|
||||
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
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user