Module commit
Module commit.
This commit is contained in:
94
_module/nss/pv_buy_item.nss
Normal file
94
_module/nss/pv_buy_item.nss
Normal file
@@ -0,0 +1,94 @@
|
||||
//::///////////////////////////////////////////////
|
||||
//:: Name: pv_buy_item
|
||||
//:://////////////////////////////////////////////
|
||||
/*
|
||||
Once the item buyer confirms the purchase,
|
||||
we place the item in his inventory and remove
|
||||
it from the owners inventory.
|
||||
*/
|
||||
//:://////////////////////////////////////////////
|
||||
|
||||
#include "pv_config_param"
|
||||
|
||||
void main()
|
||||
{
|
||||
object oNPCMerchant = OBJECT_SELF;
|
||||
object oPCOwner = GetLocalObject(oNPCMerchant, "PV_MERCHANT_OWNER");
|
||||
object oBuyer = GetPCSpeaker();
|
||||
object oItemToBuy = GetLocalObject(oNPCMerchant, "PV_ITEM_CHOSEN");
|
||||
|
||||
int iStackToBuy = GetLocalInt(oNPCMerchant, "PV_QUANTITY_TO_BUY");
|
||||
int iStackSize = GetNumStackedItems(oItemToBuy);
|
||||
|
||||
int iItemPrice = GetLocalInt(oNPCMerchant, "PV_ITEM_PRICE" + GetName(oItemToBuy));
|
||||
int iTotalPrice = iItemPrice * iStackToBuy;
|
||||
int iBuyersGold = GetGold(oBuyer);
|
||||
|
||||
string sItemResRef = GetResRef(oItemToBuy);
|
||||
|
||||
|
||||
// Make sure everything is ok!
|
||||
if (GetIsObjectValid(oPCOwner) && GetIsObjectValid(oBuyer) && GetIsObjectValid(oItemToBuy)
|
||||
&& iStackToBuy > 0 && GetStringLength(sItemResRef) > 0 && iTotalPrice > 0)
|
||||
{
|
||||
|
||||
if (iTotalPrice > iBuyersGold)
|
||||
{
|
||||
SetCustomToken(15018,"Sorry, but you don't possess enough gold to buy this item.\n\nThe item costs "
|
||||
+ IntToString(iTotalPrice) + "gp and you only have "
|
||||
+ IntToString(iBuyersGold) + "gp.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Take the necessary gold from the buyer
|
||||
AssignCommand(oNPCMerchant, TakeGoldFromCreature(iTotalPrice, oBuyer, TRUE));
|
||||
// and give it to the Item Owner
|
||||
AssignCommand(oNPCMerchant, GiveGoldToCreature(oPCOwner, iTotalPrice));
|
||||
|
||||
DestroyObject(oItemToBuy);
|
||||
|
||||
// If we are only going to buy one item from the whole stack then we
|
||||
// need to recreate the remaining items in the Owner's inventory
|
||||
if (iStackToBuy == 1 && iStackSize > 1)
|
||||
{
|
||||
CreateItemOnObject(sItemResRef, oPCOwner, iStackSize - iStackToBuy);
|
||||
}
|
||||
|
||||
CreateItemOnObject(sItemResRef, oBuyer, iStackToBuy);
|
||||
|
||||
SetCustomToken(15018, "Good. The item is now in your inventory.\n" +
|
||||
"Would you like to see the rest of my inventory?");
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
SetCustomToken(15018, "Oops, there was a problem with the transaction.");
|
||||
|
||||
if (!SHOW_ERROR_MESSAGE) { return; } // Skip
|
||||
|
||||
|
||||
// Figure out what caused the problem
|
||||
if (!GetIsObjectValid(oPCOwner) || !GetIsObjectValid(oBuyer) || !GetIsObjectValid(oItemToBuy))
|
||||
{
|
||||
SetCustomToken(15019, ":: Invalid Object ::");
|
||||
return;
|
||||
}
|
||||
|
||||
if (GetStringLength(sItemResRef) <= 0)
|
||||
{
|
||||
SetCustomToken(15019, ":: Empty Item ResRef ::");
|
||||
return;
|
||||
}
|
||||
|
||||
if (iStackToBuy > 0)
|
||||
{
|
||||
SetCustomToken(15019, ":: Invalid Item Stack ::");
|
||||
return;
|
||||
}
|
||||
|
||||
if (iTotalPrice > 0)
|
||||
{
|
||||
SetCustomToken(15019, ":: Invalid Total Price ::");
|
||||
return;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user