Skip to content

Commit

Permalink
equip and unequip commands
Browse files Browse the repository at this point in the history
  • Loading branch information
bierdosenhalter committed Dec 14, 2024
1 parent 3a890e9 commit d3b8802
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 97 deletions.
41 changes: 41 additions & 0 deletions Client/Commands/Equip.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using System.Collections.Generic;
using API;
using API.Commands;

namespace Client.Commands
{
public class Equip : CommandBase
{
public override string CmdName => "Equip";

public override string CmdUsage => "<bagPath> <invPath>";

public override string CmdDesc => "Equip an item from the bag.";

public override string Run(IClient handler, string command, Dictionary<string, object> localVars)
{
if (!(handler is RyzomClient ryzomClient))
throw new Exception("Command handler is not a Ryzom client.");

var args = GetArgs(command);

if (args.Length != 2)
return "Please specify a bag path (INVENTORY:BAG:165) and an inventory path (INVENTORY:HAND:0 OR INVENTORY:EQUIP:5).";

var bagPath = args[0].ToUpper().Trim();
var invPath = args[1].ToUpper().Trim();

var inventoryManager = ryzomClient.GetInventoryManager();

inventoryManager.Equip(bagPath, invPath);

return "";
}

public override IEnumerable<string> GetCmdAliases()
{
return new[] { "" };
}
}
}
14 changes: 7 additions & 7 deletions Client/Commands/AutoEquip.cs → Client/Commands/UnEquip.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

namespace Client.Commands
{
public class AutoEquip : CommandBase
public class UnEquip : CommandBase
{
public override string CmdName => "AutoEquip";
public override string CmdName => "UnEquip";

public override string CmdUsage => "<index>";
public override string CmdUsage => "<invPath>";

public override string CmdDesc => "Auto equip an item from the bag.";
public override string CmdDesc => "Unequip an item.";

public override string Run(IClient handler, string command, Dictionary<string, object> localVars)
{
Expand All @@ -21,13 +21,13 @@ public override string Run(IClient handler, string command, Dictionary<string, o
var args = GetArgs(command);

if (args.Length != 1)
return "Please specify the index of an item in the bag.";
return "Please inventory path (INVENTORY:HAND:0 OR INVENTORY:EQUIP:5).";

var itemIndex = int.Parse(args[0]);
var invPath = args[0].ToUpper().Trim();

var inventoryManager = ryzomClient.GetInventoryManager();

inventoryManager.AutoEquip(itemIndex, true);
inventoryManager.UnEquip(invPath);

return "";
}
Expand Down
194 changes: 104 additions & 90 deletions Client/Inventory/InventoryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -311,8 +311,8 @@ public void UnwearBagItem(int bagEntryIndex)
/// <summary>
/// Equip a bag item
/// </summary>
/// <param name="bagPath">SERVER:INVENTORY:BAG:165</param>
/// <param name="invPath">SERVER:INVENTORY:HAND:0 OR SERVER:INVENTORY:EQUIP:5</param>
/// <param name="bagPath">INVENTORY:BAG:165</param>
/// <param name="invPath">INVENTORY:HAND:0 OR INVENTORY:EQUIP:5</param>
public void Equip(in string bagPath, in string invPath)
{
if (bagPath.Length == 0 || invPath.Length == 0)
Expand All @@ -321,7 +321,7 @@ public void Equip(in string bagPath, in string invPath)
}

// Get inventory and slot
var sIndexInBag = bagPath.Substring(bagPath.LastIndexOf(':') + 1, bagPath.Length);
var sIndexInBag = bagPath[(bagPath.LastIndexOf(':') + 1)..];
if (!ushort.TryParse(sIndexInBag, out var indexInBag))
return;

Expand All @@ -331,15 +331,20 @@ public void Equip(in string bagPath, in string invPath)
if (invPath.StartsWith("INVENTORY:HAND", StringComparison.InvariantCultureIgnoreCase))
{
inventory = (ushort)INVENTORIES.handling;
if (!ushort.TryParse(invPath.Substring(21, invPath.Length), out invSlot))
if (!ushort.TryParse(invPath[15..], out invSlot))
return;
}
else if (invPath.StartsWith("INVENTORY:EQUIP", StringComparison.InvariantCultureIgnoreCase))
{
inventory = (ushort)INVENTORIES.equipment;
if (!ushort.TryParse(invPath.Substring(22, invPath.Length), out invSlot))
if (!ushort.TryParse(invPath[16..], out invSlot))
return;
}
//else if (strnicmp(invPath.c_str(), "LOCAL:INVENTORY:HOTBAR", 22) == 0)
//{
// inventory = INVENTORIES::hotbar;
// fromString(invPath.substr(23, invPath.size()), invSlot);
//}

// Hands management: check if we have to unequip left hand because of incompatibility with right hand item
var oldRightIndexInBag = _client.GetDatabaseManager().GetServerNode("SERVER:" + invPath + ":INDEX_IN_BAG").GetValue16();
Expand Down Expand Up @@ -373,114 +378,123 @@ public void Equip(in string bagPath, in string invPath)
}
else
{
_client.Log.Warn($"Don't know message name {sMsg}");
_client.Log.Error($"Don't know message name {sMsg}");
}
}
else
{
_client.Log.Error($"Inventory is undefined.");
}
}

/// <summary>
/// Auto equip an item (given by index) from the bag (return true if equipped)
/// Unequip an item
/// </summary>
internal bool AutoEquip(int bagEntryIndex, bool allowReplace)
/// <param name="invPath">INVENTORY:HAND:0 OR INVENTORY:EQUIP:5</param>
internal void UnEquip(string invPath)
{
/*
uint i;
InterfaceManager pIM = InterfaceManager.getInstance();
ListSheetBase pList = WidgetManager.getInstance().getElementFromId(LIST_BAG_TEXT) as IListSheetBase;
DatabaseCtrlSheet pCSSrc = null;
if (pList == null)
return false;
for (i = 0; i < MAX_BAGINV_ENTRIES; ++i)
if (invPath.Length == 0)
{
pCSSrc = pList.getSheet(i);
string sTmp = pCSSrc.getSheet();
sTmp = sTmp.Substring(sTmp.LastIndexOf(':') + 1, sTmp.Length);
sint nTmp = new sint();
fromString(sTmp, nTmp);
if (nTmp == bagEntryIndex)
break;
return;
}

if (i == MAX_BAGINV_ENTRIES)
return false;
var oldIndexInBag = _client.GetDatabaseManager().GetProp("SERVER:" + invPath + ":INDEX_IN_BAG");
if (oldIndexInBag == 0)
{
return;
}

if (pCSSrc == null)
return false;
// Get inventory and slot
var inventory = (ushort)INVENTORIES.UNDEFINED;
ushort invSlot = 0xffff;

if (invPath.StartsWith("INVENTORY:HAND", StringComparison.InvariantCultureIgnoreCase))
{
inventory = (ushort)INVENTORIES.handling;
if (!ushort.TryParse(invPath[15..], out invSlot))
return;
}
else if (invPath.StartsWith("INVENTORY:EQUIP", StringComparison.InvariantCultureIgnoreCase))
{
inventory = (ushort)INVENTORIES.equipment;
if (!ushort.TryParse(invPath[16..], out invSlot))
return;
}
//else if (strnicmp(invPath.c_str(), "LOCAL:INVENTORY:HOTBAR", 22) == 0)
//{
// inventory = INVENTORIES.hotbar;
// fromString(invPath.substr(23, invPath.size()), invSlot);
//}

// TODO Hands management : check if we have to unequip left hand because of incompatibility with right hand item
//if (inventory == INVENTORIES.handling && invSlot == 0)
//{
// DatabaseCtrlSheet pCSLeftHand = CWidgetManager.getInstance().getElementFromId(CTRL_HAND_LEFT) as CDBCtrlSheet;
// if (pCSLeftHand == null)
// {
// return;
// }
//
// DatabaseNodeLeaf pNL = NLGUI.CDBManager.getInstance().getDbProp(LOCAL_INVENTORY ":HAND:1:INDEX_IN_BAG", false);
// if (pNL == null)
// {
// return;
// }
//
// // get sheet of left item
// uint leftSheet = getInventory().getBagItemSheet(pNL.getValue32() - 1);
//
// // get sheet of previous right hand item
// uint lastRightSheet = getInventory().getBagItemSheet(oldIndexInBag - 1);
//
// // sheet of new right hand item
// uint rightSheet = 0;
//
// // If incompatible -> remove
// if (!getInventory().isLeftHandItemCompatible(leftSheet, rightSheet, lastRightSheet))
// {
// getInventory().unequip(LOCAL_INVENTORY ":HAND:1");
// }
//}

// TODO not needed since we only work with server db
//_client.GetDatabaseManager().GetNode(invPath + ":INDEX_IN_BAG",false).SetValue16(0);

// Update trade window if any - not used in RCC

for (i = 0; i < MAX_HANDINV_ENTRIES; ++i)
// Send message to the server
if (inventory != (ushort)INVENTORIES.UNDEFINED)
{
DatabaseCtrlSheet pCSDst = getHandSheet(i);
if (pCSDst == null)
BitMemoryStream @out = new BitMemoryStream();
const string sMsg = "ITEM:UNEQUIP";
if (_client.GetNetworkManager().GetMessageHeaderManager().PushNameToStream(sMsg, @out))
{
continue;
}
string dstPath = getDBIndexPath(pCSDst);
// Fill the message (equipped inventory, equipped inventory slot)
@out.Serial(ref inventory);
@out.Serial(ref invSlot);
_client.GetNetworkManager().Push(@out);

int indexDstPath = NLGUI.CDBManager.getInstance().getDbProp(dstPath + ":INDEX_IN_BAG").getValue16();
//pIM.incLocalSyncActionCounter();

// Already something in that slot?
if (!allowReplace && indexDstPath > 0)
{
continue;
//nlinfo("impulseCallBack : %s %d %d sent", sMsg.c_str(), inventory, invSlot);
}
// Does the source and destination are items ?
if (pCSSrc.getType() == CtrlSheetInfo.SheetType_Item)
else
{
if (pCSDst.getType() == CtrlSheetInfo.SheetType_Item)
{
// Right Slot ?
if (pCSDst.canDropItem(pCSSrc))
{
// Ok let us equip with this item
string srcPath = pCSSrc.getSheet();
equip(srcPath, dstPath);
return true;
}
}
_client.Log.Error($"Don't know message name {sMsg}");
}
}
for (i = 0; i < MAX_EQUIPINV_ENTRIES; ++i)
else
{
DatabaseCtrlSheet pCSDst = getEquipSheet(i);
if (pCSDst == null)
{
continue;
}
string dstPath = getInventory().getDBIndexPath(pCSDst);
int indexDstPath = NLGUI.CDBManager.getInstance().getDbProp(dstPath + ":INDEX_IN_BAG").getValue16();
// Already something in that slot?
if (!allowReplace && indexDstPath > 0)
{
continue;
}
// Does the source and destination are items ?
if (pCSSrc.getType() == CtrlSheetInfo.SheetType_Item)
{
if (pCSDst.getType() == CtrlSheetInfo.SheetType_Item)
{
// Right Slot ?
if (pCSDst.canDropItem(pCSSrc))
{
// Ok let us equip with this item
string srcPath = pCSSrc.getSheet();
equip(srcPath, dstPath);
return true;
}
}
}
}*/
_client.Log.Error($"Inventory is undefined.");
}
}

return false;
/// <summary>
/// Auto equip an item (given by index) from the bag (return true if equipped)
/// </summary>
internal bool AutoEquip(int bagEntryIndex, bool allowReplace)
{
throw new NotImplementedException();
}
}
}

0 comments on commit d3b8802

Please sign in to comment.