This destroy item with highest value script destroys the most valuable item in a player character's inventory. It can be adapted to look through other inventories as well (creature, placeable, or item).
This script is suitable for use in a conversation.
/* **************************************************************************
* Destroys the most valuable item in a PC's inventory.
* For use in a conversation script.
* ************************************************************************** */
// Returns the most valuable item in an creature's inventory (including slots).
// For stacked items, this considers only one item in the stack.
object GetMostValuable(object oTarget);
// Get the gold piece value of a single item, even if stacked.
int GetGoldPieceValueSingle(object oItem);
// Destroys an item. Only one item, even if it is stacked.
void DestroyItem(object oItem);
void main()
{
object oPC = GetPCSpeaker(); // This is the line to change to use this in other events.
DestroyItem(GetMostValuable(oPC));
}
// Returns the most valuable item in an creature's inventory (including slots).
// For stacked items, this considers only one item in the stack.
object GetMostValuable(object oTarget)
{
object oHighestValue = OBJECT_INVALID;
int nHighestValue = -1; // So we can find items whose value is zero.
int nSlot;
// Loop through main inventory.
object oItem = GetFirstItemInInventory(oTarget);
while ( oItem != OBJECT_INVALID ) {
int nValue = GetGoldPieceValueSingle(oItem);
// Is this item the most valuable seen so far?
if ( nValue > nHighestValue ) {
oHighestValue = oItem;
nHighestValue = nValue;
}
// Update the loop.
oItem = GetNextItemInInventory(oTarget);
}
// Check inventory slots.
for ( nSlot = 0; nSlot < NUM_INVENTORY_SLOTS; ++nSlot ) {
oItem = GetItemInSlot(nSlot, oTarget);
int nValue = GetGoldPieceValueSingle(oItem);
// Is this item the most valuable seen so far?
if ( nValue > nHighestValue ) {
oHighestValue = oItem;
nHighestValue = nValue;
}
}
return oHighestValue;
}
// Get the gold piece value of a single item, even if stacked.
int GetGoldPieceValueSingle(object oItem)
{
int nStack = GetItemStackSize(oItem);
// Avoid division by zero.
return (nStack == 0) ?
GetGoldPieceValue(oItem) :
GetGoldPieceValue(oItem) / nStack;
}
// Destroys an item. Only one item, even if it is stacked.
void DestroyItem(object oItem)
{
int nStack = GetItemStackSize(oItem);
if ( nStack > 1 )
SetItemStackSize(oItem, nStack - 1);
else
DestroyObject(oItem);
}