//Script gives experience points to a PC who picks a lock.
//Place this script in the OnUnLock Event of the door, chest, etc.
//They must not have key in inventory for it to work.

//Note: Although I arrived at this idea independently, I later
//found a similar script on NWN Vault to give xp when a trap
//is disarmed.  This script was written by Velmar.  I give credit
//to a like minded individual. :)

//VARIABLES:
//
//nExp = XP to be given
//oPC = PC who unlocked object
//oItem = variable to check through oPC's inventory
//sKey = string of key required to unlock object
//oKey = key object required to unlock object
//nDC = DC to unlock object
//nAlreadyPicked = local integer on calling object to prevent PC
//                 from locking object and picking to gain xp
//                 repeatedly

void main()
{

//VARIABLE DECLARATIONS----------------------------

    int nExp = 0;
    object oPC = GetLastUnlocked();
    object oItem = GetFirstItemInInventory(oPC);
    string sKey = GetTrapKeyTag(OBJECT_SELF);
    object oKey = GetObjectByTag(sKey);
    int nDC = GetLockUnlockDC(OBJECT_SELF);
    int nKeyAssociated = TRUE;
    int nAlreadyUnlocked = FALSE;

//if object's lock has already been unlocked before, don't give xp

    nAlreadyUnlocked = GetLocalInt(OBJECT_SELF,"AlreadyUnlocked");

    if (nAlreadyUnlocked == TRUE)
    {
        return;
    }

//Regardless of whether lock was just picked or key was used,
//localint "AlreadyUnlocked" will be set to indicate unlocking.

    SetLocalInt(OBJECT_SELF,"AlreadyUnlocked",TRUE);

//if oKey is OBJECT_INVALID then no key is associated with object

    if (oKey == OBJECT_INVALID)
    {
        nKeyAssociated = FALSE;
    }

//If a key is associated with the door, check oPC's inventory for the key
//If oPC has key, return from script

    while (oItem != OBJECT_INVALID && nKeyAssociated == TRUE)
    {
        if (oItem == oKey)
        {
            return;
        }
        oItem = GetNextItemInInventory(oPC);
    }
//The amount of experience given will depend on DC of the lock.
//This section is adjustable to suit level of XP required.
     // put similar xp from traps award
     int iXPawarddc = (GetLockUnlockDC(OBJECT_SELF) *2);
    // Multiplies the Trap Disarm DC by 8

    int iXPaward = (iXPawarddc -((GetHitDice(oPC)-1)*2));
    // Subtracts 8 for every level -1 of the pc from the XP award (this way 1st level Pcs get full xp).

    SendMessageToPC( (oPC ),"Unlocked");
    // Sends a message to the PC letting them Know that the trap was disarmed.

    if (iXPaward >= 1)
    // checks to make sure that the xp award is at least 1.

    GiveXPToCreature(oPC, iXPaward);
    // If the XP reward is at least 1 then give the PC a reward.

    else
    // If the XP award is less then 1.


    GiveXPToCreature(oPC, 1);
}