185 lines
6.0 KiB
Plaintext
185 lines
6.0 KiB
Plaintext
/*
|
|
* The way this works:
|
|
* Initially you put an item (the treasure object) with one of
|
|
* several resRefs in the chest. The 1st time the chest is opened
|
|
* or destroyed, the item is found and a local string is placed on
|
|
* the chest that keeps track of what item should be in it.
|
|
* At any time you can put another treasure object in the chest.
|
|
* if the chest has a treasure object in it, it will use that value
|
|
* for treasure determination.
|
|
* A "monitor" object (an invisible object with the tag "monitor"
|
|
* is used to create destroyed chests. You will have to put a monitor
|
|
* object in every area you intend to use this script in.
|
|
*
|
|
* How to use:
|
|
* 1. Place a chest in an area. Subtitute this script for the script
|
|
* called in OnDestroy and OnOpen
|
|
* 2. Place any ONE of the Items from Special->Custom 2 in the chest
|
|
* 3. Create an invisible placeable object with the tag "monitor" and
|
|
* place it in the area with the chest(s).
|
|
*/
|
|
#include "nw_o2_coninclude"
|
|
|
|
void Action(object oObject);
|
|
void ActionCreateChest(string sChestResRef, string sTreasureResRef, location lChestLoc);
|
|
int AreOtherItemsInChest(object oChest);
|
|
object GetChestTreasureObject(object oChest);
|
|
|
|
void main()
|
|
{
|
|
// initialize variables
|
|
float fDelay = 45.0;
|
|
int iLooted = GetLocalInt(OBJECT_SELF, "iLooted");
|
|
|
|
// Get the treasure object and/or the treasure resRef
|
|
object oTreasureObj = GetChestTreasureObject(OBJECT_SELF);
|
|
string sTreasureResRef = "";
|
|
if (GetIsObjectValid(oTreasureObj))
|
|
{
|
|
sTreasureResRef = GetResRef(oTreasureObj);
|
|
// Get rid of the treasure object
|
|
DestroyObject(oTreasureObj);
|
|
}
|
|
else
|
|
sTreasureResRef = GetLocalString(OBJECT_SELF, "sTreasureResRef");
|
|
|
|
// bulletproofing
|
|
if (sTreasureResRef == "")
|
|
sTreasureResRef = "low_1";
|
|
|
|
// Set the sTreasureResRef on the chest
|
|
SetLocalString(OBJECT_SELF, "sTreasureResRef", sTreasureResRef);
|
|
|
|
// Was the chest destroyed?
|
|
int iHp = GetCurrentHitPoints(OBJECT_SELF);
|
|
int iChestDestroyed = FALSE;
|
|
if (iHp < 1)
|
|
{
|
|
object oMonitor = GetObjectByTag("monitor");
|
|
location lMyLocation = GetLocation(OBJECT_SELF);
|
|
string sChestResRef = GetResRef(OBJECT_SELF);
|
|
AssignCommand(oMonitor, DelayCommand(fDelay, ActionCreateChest(sChestResRef, sTreasureResRef, lMyLocation)));
|
|
iChestDestroyed = TRUE;
|
|
}
|
|
|
|
// Is there anything in the chest?
|
|
object oItem = GetFirstItemInInventory(OBJECT_SELF);
|
|
|
|
// If the chest is marked as looted (this means we're
|
|
// waiting for the timer to cycle) or if there are
|
|
// any items other that the treasure object, we return
|
|
if (iLooted || (GetIsObjectValid(oItem) && (GetTag(oItem) != GetTag(oTreasureObj))))
|
|
return;
|
|
|
|
object oLastOpener = GetLastOpenedBy();
|
|
object oDelItem;
|
|
string sTag = "";
|
|
string sType = "";
|
|
int iNum = 0;
|
|
int iRoll = 0;
|
|
int iNdx = 0;
|
|
string sResRef;
|
|
|
|
sType = GetStringUpperCase(GetStringLeft(sTreasureResRef, 1));
|
|
iNum = StringToInt(GetStringRight(sTreasureResRef, 1));
|
|
if (sType == "L")
|
|
{
|
|
for (iNdx = iNum; iNdx > 0; iNdx-- )
|
|
{
|
|
if (iNdx == 1)
|
|
GenerateLowTreasure(oLastOpener, OBJECT_SELF);
|
|
else
|
|
{
|
|
iRoll = d100();
|
|
if (iRoll <= 10)
|
|
GenerateMediumTreasure(oLastOpener, OBJECT_SELF);
|
|
else if (iRoll <= 75)
|
|
GenerateLowTreasure(oLastOpener, OBJECT_SELF);
|
|
}
|
|
}
|
|
}
|
|
else if (sType == "M")
|
|
{
|
|
for (iNdx = iNum; iNdx > 0; iNdx-- )
|
|
{
|
|
if (iNdx == 1)
|
|
GenerateMediumTreasure(oLastOpener, OBJECT_SELF);
|
|
else
|
|
{
|
|
iRoll = d100();
|
|
if (iRoll <= 10)
|
|
GenerateHighTreasure(oLastOpener, OBJECT_SELF);
|
|
else if (iRoll <= 75)
|
|
GenerateLowTreasure(oLastOpener, OBJECT_SELF);
|
|
else
|
|
GenerateMediumTreasure(oLastOpener, OBJECT_SELF);
|
|
}
|
|
}
|
|
}
|
|
else if (sType == "H")
|
|
{
|
|
for (iNdx = iNum; iNdx > 0; iNdx-- )
|
|
{
|
|
if (iNdx == 1)
|
|
GenerateHighTreasure(oLastOpener, OBJECT_SELF);
|
|
else
|
|
{
|
|
iRoll = d100();
|
|
if (iRoll <= 10)
|
|
GenerateLowTreasure(oLastOpener, OBJECT_SELF);
|
|
else if (iRoll <= 75)
|
|
GenerateMediumTreasure(oLastOpener, OBJECT_SELF);
|
|
else
|
|
GenerateHighTreasure(oLastOpener, OBJECT_SELF);
|
|
}
|
|
}
|
|
}
|
|
ShoutDisturbed();
|
|
// We've looted the chest to mark it so and set it for refilling
|
|
SetLocalInt(OBJECT_SELF, "iLooted", TRUE);
|
|
DelayCommand(fDelay, SetLocalInt(OBJECT_SELF, "iLooted", FALSE));
|
|
}
|
|
|
|
/*
|
|
* This method searches through an object's inventory
|
|
* looking for the first item with a resRef equal to
|
|
* one of the expected treasure object resRefs. It
|
|
* returns the treasure object if it finds it,
|
|
*/
|
|
object GetChestTreasureObject(object oChest)
|
|
{
|
|
object oItem = GetFirstItemInInventory();
|
|
string sTag = "";
|
|
|
|
while (GetIsObjectValid(oItem) == TRUE)
|
|
{
|
|
sTag = GetStringLowerCase(GetTag(oItem));
|
|
if (sTag == "low_1" || sTag == "low_2" || sTag == "low_3" ||
|
|
sTag == "med_1" || sTag == "med_2" || sTag == "med_3" ||
|
|
sTag == "high_1" || sTag == "high_2" || sTag == "high_3")
|
|
return oItem;
|
|
else
|
|
oItem = GetNextItemInInventory(oChest);
|
|
}
|
|
|
|
return oItem;
|
|
}
|
|
|
|
/*
|
|
* When a chest is created, you want to create a new one
|
|
* in the spot where the old one was with a local string
|
|
* for the treasure type.
|
|
*/
|
|
void ActionCreateChest(string sChestResRef, string sTreasureResRef, location lChestLoc)
|
|
{
|
|
object oNewChest = CreateObject(OBJECT_TYPE_PLACEABLE, sChestResRef, lChestLoc);
|
|
SetLocalString(oNewChest, "sTreasureResRef", sTreasureResRef);
|
|
}
|
|
|
|
void Action(object oObject)
|
|
{
|
|
return;
|
|
}
|
|
|
|
//void main() {;}
|