Skip to content

Commit

Permalink
Merge pull request #158 from ethanmoffat/inventory
Browse files Browse the repository at this point in the history
Implement inventory panel in the UI
  • Loading branch information
ethanmoffat authored Mar 29, 2022
2 parents a4b3c85 + 852ffeb commit 06e6c29
Show file tree
Hide file tree
Showing 36 changed files with 1,475 additions and 1,352 deletions.
24 changes: 24 additions & 0 deletions EOLib.Config/IniReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,30 @@ public bool Load()
return true;
}

public void Save()
{
try
{
using (var sw = new StreamWriter(_filename))
{
foreach (var section in _sections)
{
sw.WriteLine($"[{section.Key}]");

foreach (var kvp in section.Value)
{
sw.WriteLine($"{kvp.Key}={kvp.Value}");
}

sw.WriteLine();
}
}
}
catch (IOException)
{
}
}

public bool GetValue(string section, string key, out string value)
{
value = null;
Expand Down
21 changes: 21 additions & 0 deletions EOLib.IO/Extensions/ItemSizeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using System;

namespace EOLib.IO.Extensions
{
public static class ItemSizeExtensions
{
public static (int Width, int Height) GetDimensions(this ItemSize itemSize)
{
var sizeStr = Enum.GetName(typeof(ItemSize), itemSize);
if (sizeStr == null || sizeStr.Length != 7)
{
return (0, 0);
}

var width = Convert.ToInt32(sizeStr.Substring(4, 1));
var height = Convert.ToInt32(sizeStr.Substring(6, 1));

return (width, height);
}
}
}
22 changes: 9 additions & 13 deletions EOLib.IO/Map/Matrix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace EOLib.IO.Map
{
public class Matrix<T> : IReadOnlyMatrix<T>
{
private static readonly Matrix<T> _empty = new Matrix<T>(0, 0);
private static readonly Matrix<T> _empty = new Matrix<T>(new T[0,0]);
public static Matrix<T> Empty => _empty;

private readonly T[,] _arr;
Expand All @@ -16,22 +16,22 @@ public class Matrix<T> : IReadOnlyMatrix<T>
public int Rows { get; }
public int Cols { get; }

private Matrix(int rows, int cols)
private Matrix(T[,] other)
{
Rows = rows;
Cols = cols;
_arr = new T[rows, cols];
Rows = other.GetLength(0);
Cols = other.GetLength(1);
_arr = other;
}

public Matrix(int rows, int cols, T defaultValue)
: this(rows, cols)
: this(new T[rows, cols])
{
_default = defaultValue;
Fill(defaultValue);
}

public Matrix(Matrix<T> other)
: this(other.Rows, other.Cols)
: this(new T[other.Rows, other.Cols])
{
for (int row = 0; row < other.Rows; ++row)
for (int col = 0; col < other.Cols; ++col)
Expand Down Expand Up @@ -70,16 +70,12 @@ public T[] GetRow(int rowIndex)

public static implicit operator T[,](Matrix<T> array)
{
var ret = new T[array.Rows, array.Cols];
Array.Copy(array._arr, ret, ret.Length);
return ret;
return array._arr;
}

public static implicit operator Matrix<T>(T[,] array)
{
var ret = new Matrix<T>(array.GetLength(0), array.GetLength(1));
Array.Copy(array, ret._arr, array.Length);
return ret;
return new Matrix<T>(array);
}

public IEnumerator<IList<T>> GetEnumerator()
Expand Down
20 changes: 10 additions & 10 deletions EOLib/Domain/Character/CharacterInventoryRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,31 @@ namespace EOLib.Domain.Character
{
public interface ICharacterInventoryRepository
{
List<IInventoryItem> ItemInventory { get; set; }
HashSet<IInventoryItem> ItemInventory { get; set; }

List<IInventorySpell> SpellInventory { get; set; }
HashSet<IInventorySpell> SpellInventory { get; set; }
}

public interface ICharacterInventoryProvider
{
IReadOnlyList<IInventoryItem> ItemInventory { get; }
IReadOnlyCollection<IInventoryItem> ItemInventory { get; }

IReadOnlyList<IInventorySpell> SpellInventory { get; }
IReadOnlyCollection<IInventorySpell> SpellInventory { get; }
}

[AutoMappedType(IsSingleton = true)]
public class CharacterInventoryRepository : ICharacterInventoryRepository, ICharacterInventoryProvider
{
public List<IInventoryItem> ItemInventory { get; set; }
public List<IInventorySpell> SpellInventory { get; set; }
public HashSet<IInventoryItem> ItemInventory { get; set; }
public HashSet<IInventorySpell> SpellInventory { get; set; }

IReadOnlyList<IInventoryItem> ICharacterInventoryProvider.ItemInventory => ItemInventory;
IReadOnlyList<IInventorySpell> ICharacterInventoryProvider.SpellInventory => SpellInventory;
IReadOnlyCollection<IInventoryItem> ICharacterInventoryProvider.ItemInventory => ItemInventory;
IReadOnlyCollection<IInventorySpell> ICharacterInventoryProvider.SpellInventory => SpellInventory;

public CharacterInventoryRepository()
{
ItemInventory = new List<IInventoryItem>(32);
SpellInventory = new List<IInventorySpell>(32);
ItemInventory = new HashSet<IInventoryItem>();
SpellInventory = new HashSet<IInventorySpell>();
}
}
}
15 changes: 15 additions & 0 deletions EOLib/Domain/Character/InventoryItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ public IInventoryItem WithAmount(int newAmount)
{
return new InventoryItem(ItemID, newAmount);
}

public override bool Equals(object obj)
{
var other = obj as InventoryItem;
if (other == null) return false;
return other.ItemID == ItemID && other.Amount == Amount;
}

public override int GetHashCode()
{
int hashCode = 1754760722;
hashCode = hashCode * -1521134295 + ItemID.GetHashCode();
hashCode = hashCode * -1521134295 + Amount.GetHashCode();
return hashCode;
}
}

public interface IInventoryItem
Expand Down
15 changes: 15 additions & 0 deletions EOLib/Domain/Character/InventorySpell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,21 @@ public IInventorySpell WithLevel(short newLevel)
{
return new InventorySpell(ID, newLevel);
}

public override bool Equals(object obj)
{
var other = obj as InventorySpell;
if (other == null) return false;
return other.ID == ID && other.Level == Level;
}

public override int GetHashCode()
{
int hashCode = 1754760722;
hashCode = hashCode * -1521134295 + ID.GetHashCode();
hashCode = hashCode * -1521134295 + Level.GetHashCode();
return hashCode;
}
}

public interface IInventorySpell
Expand Down
5 changes: 3 additions & 2 deletions EOLib/Domain/Login/ILoginRequestGrantedData.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Generic;
using EOLib.Domain.Character;
using EOLib.IO;
using EOLib.Net.Translators;

namespace EOLib.Domain.Login
Expand Down Expand Up @@ -32,7 +33,7 @@ public interface ILoginRequestGrantedData : ITranslatedData

ICharacterStats CharacterStats { get; }

IReadOnlyList<short> Paperdoll { get; }
IReadOnlyDictionary<EquipLocation, short> Paperdoll { get; }

byte GuildRankNum { get; }
short JailMap { get; }
Expand All @@ -59,7 +60,7 @@ public interface ILoginRequestGrantedData : ITranslatedData
ILoginRequestGrantedData WithGuildTag(string guildTag);
ILoginRequestGrantedData WithAdminLevel(AdminLevel adminLevel);
ILoginRequestGrantedData WithCharacterStats(ICharacterStats stats);
ILoginRequestGrantedData WithPaperdoll(IEnumerable<short> paperdollItemIDs);
ILoginRequestGrantedData WithPaperdoll(IReadOnlyDictionary<EquipLocation, short> paperdoll);
ILoginRequestGrantedData WithGuildRankNum(byte rankNum);
ILoginRequestGrantedData WithJailMap(short jailMapID);
ILoginRequestGrantedData WithFirstTimePlayer(bool isFirstTimePlayer);
Expand Down
17 changes: 11 additions & 6 deletions EOLib/Domain/Login/LoginActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
using System.Threading.Tasks;
using AutomaticTypeMapper;
using EOLib.Domain.Character;
using EOLib.Domain.Chat;
using EOLib.Domain.Map;
using EOLib.Domain.NPC;
using EOLib.Domain.Protocol;
using EOLib.Localization;
using EOLib.Net;
using EOLib.Net.Communication;
using EOLib.Net.FileTransfer;
Expand Down Expand Up @@ -35,14 +33,12 @@ public LoginActions(IPacketSendService packetSendService,
IPacketTranslator<IAccountLoginData> loginPacketTranslator,
IPacketTranslator<ILoginRequestGrantedData> loginRequestGrantedPacketTranslator,
IPacketTranslator<ILoginRequestCompletedData> loginRequestCompletedPacketTranslator,
ILocalizedStringFinder localizedStringFinder,
ICharacterSelectorRepository characterSelectorRepository,
IPlayerInfoRepository playerInfoRepository,
ICharacterRepository characterRepository,
ICurrentMapStateRepository currentMapStateRepository,
ILoginFileChecksumRepository loginFileChecksumRepository,
INewsRepository newsRepository,
IChatRepository chatRepository,
ICharacterInventoryRepository characterInventoryRepository,
IPaperdollRepository paperdollRepository)
{
Expand Down Expand Up @@ -116,6 +112,15 @@ public async Task<short> RequestCharacterLogin(ICharacter character)
_playerInfoRepository.IsFirstTimePlayer = data.FirstTimePlayer;
_currentMapStateRepository.CurrentMapID = data.MapID;

_paperdollRepository.VisibleCharacterPaperdolls[data.SessionID] = new PaperdollData()
.WithName(data.Name)
.WithTitle(data.Title)
.WithGuild(data.GuildName)
.WithRank(data.GuildRank)
.WithClass(data.ClassID)
.WithPlayerID(data.SessionID)
.WithPaperdoll(data.Paperdoll);

_loginFileChecksumRepository.MapChecksum = data.MapRID.ToArray();
_loginFileChecksumRepository.MapLength = data.MapLen;

Expand Down Expand Up @@ -171,8 +176,8 @@ public async Task<CharacterLoginReply> CompleteCharacterLogin(short sessionID)
.WithStats(stats)
.WithRenderProperties(mainCharacter.RenderProperties);

_characterInventoryRepository.ItemInventory = data.CharacterItemInventory.ToList();
_characterInventoryRepository.SpellInventory = data.CharacterSpellInventory.ToList();
_characterInventoryRepository.ItemInventory = new HashSet<IInventoryItem>(data.CharacterItemInventory);
_characterInventoryRepository.SpellInventory = new HashSet<IInventorySpell>(data.CharacterSpellInventory);

_currentMapStateRepository.Characters = data.MapCharacters.Except(new[] { mainCharacter }).ToDictionary(k => k.ID, v => v);
_currentMapStateRepository.NPCs = new HashSet<INPC>(data.MapNPCs);
Expand Down
8 changes: 4 additions & 4 deletions EOLib/Domain/Login/LoginRequestGrantedData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public class LoginRequestGrantedData : ILoginRequestGrantedData

public ICharacterStats CharacterStats { get; private set; }

private List<short> _paperdoll = new List<short>((int)EquipLocation.PAPERDOLL_MAX);
public IReadOnlyList<short> Paperdoll => _paperdoll;
private IReadOnlyDictionary<EquipLocation, short> _paperdoll = new Dictionary<EquipLocation, short>();
public IReadOnlyDictionary<EquipLocation, short> Paperdoll => _paperdoll;

public byte GuildRankNum { get; private set; }
public short JailMap { get; private set; }
Expand Down Expand Up @@ -187,10 +187,10 @@ public ILoginRequestGrantedData WithCharacterStats(ICharacterStats stats)
return copy;
}

public ILoginRequestGrantedData WithPaperdoll(IEnumerable<short> paperdollItemIDs)
public ILoginRequestGrantedData WithPaperdoll(IReadOnlyDictionary<EquipLocation, short> paperdoll)
{
var copy = MakeCopy(this);
copy._paperdoll = paperdollItemIDs.ToList();
copy._paperdoll = paperdoll;
return copy;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ public override ILoginRequestCompletedData TranslatePacket(IPacket packet)
var maxWeight = packet.ReadChar();

var inventoryItems = GetInventoryItems(packet).ToList();
if (!inventoryItems.Any(x => x.ItemID == 1))
inventoryItems.Insert(0, new InventoryItem(1, 0));

var inventorySpells = GetInventorySpells(packet).ToList();

if (inventoryItems.All(x => x.ItemID != 1))
Expand Down
9 changes: 5 additions & 4 deletions EOLib/Net/Translators/LoginRequestGrantedPacketTranslator.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using AutomaticTypeMapper;
using EOLib.Domain.Character;
Expand Down Expand Up @@ -93,10 +94,10 @@ public ILoginRequestGrantedData TranslatePacket(IPacket packet)
.WithNewStat(CharacterStat.Constituion, dispCon)
.WithNewStat(CharacterStat.Charisma, dispCha);

var paperDoll = new short[(int)EquipLocation.PAPERDOLL_MAX];
for (int i = 0; i < (int)EquipLocation.PAPERDOLL_MAX; ++i)
var paperDoll = new Dictionary<EquipLocation, short>();
for (var equipLocation = (EquipLocation)0; equipLocation < EquipLocation.PAPERDOLL_MAX; ++equipLocation)
{
paperDoll[i] = packet.ReadShort();
paperDoll[equipLocation] = packet.ReadShort();
}

var guildRankNum = packet.ReadChar();
Expand Down
2 changes: 1 addition & 1 deletion EOLib/Net/Translators/OnlineListPacketTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public OnlineListPacketTranslator(IECFFileProvider classFileProvider)

public IOnlineListData TranslatePacket(IPacket packet)
{
var reply = (InitReply)packet.ReadChar();
var reply = (InitReply)packet.ReadByte();

if (reply != InitReply.AllPlayersList && reply != InitReply.FriendPlayersList)
throw new MalformedPacketException($"Expected online list or friend list init data, but was {reply}", packet);
Expand Down
6 changes: 4 additions & 2 deletions EOLib/PacketHandlers/ItemEquipHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,10 @@ protected bool HandlePaperdollPacket(IPacket packet, bool itemUnequipped)
.Match(some: invItem => invItem.WithAmount(itemUnequipped ? invItem.Amount + amount : amount),
none: () => new InventoryItem(itemId, amount));

_characterInventoryRepository.ItemInventory.RemoveAll(x => x.ItemID == itemId);
_characterInventoryRepository.ItemInventory.Add(updatedItem);
_characterInventoryRepository.ItemInventory.RemoveWhere(x => x.ItemID == itemId);

if (updatedItem.Amount > 0)
_characterInventoryRepository.ItemInventory.Add(updatedItem);
}
else
{
Expand Down
2 changes: 2 additions & 0 deletions EOLib/misc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ public static class Constants
public const string FriendListFile = "config/friends.ini";
public const string IgnoreListFile = "config/ignore.ini";

public const string InventoryFile = "config/inventory.ini";

//Should be easily customizable between different clients (based on graphics)
//not a config option because this shouldn't be exposed at the user level
public static readonly int[] TrapSpikeGFXObjectIDs = {449, 450, 451, 452};
Expand Down
20 changes: 10 additions & 10 deletions EndlessClient/Dialogs/Old/BankAccountDialog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,16 +199,16 @@ public override void Update(GameTime gt)
{
if (!Game.IsActive) return;

if (EOGame.Instance.Hud.IsInventoryDragging())
{
shouldClickDrag = false;
SuppressParentClickDrag(true);
}
else
{
shouldClickDrag = true;
SuppressParentClickDrag(false);
}
//if (EOGame.Instance.Hud.IsInventoryDragging())
//{
// shouldClickDrag = false;
// SuppressParentClickDrag(true);
//}
//else
//{
// shouldClickDrag = true;
// SuppressParentClickDrag(false);
//}

base.Update(gt);
}
Expand Down
Loading

0 comments on commit 06e6c29

Please sign in to comment.