//::///////////////////////////////////////////////////////
//:: Secret Door Trigger by Nol Drek
//::///////////////////////////////////////////////////////

/* When a PC enters this trigger, a check will be made to
   see if they detect a secret door. If so, a secret door
   will be created at the location of the nearest invisible
   object with a tag name "s_door_loc". The secret door
   has a destination waypoint with the tag DST_<this tag>.

   Based on the hidden trap door script of Robert Babiak.
*/

void main()
{

    // Set the DC of the secret door

    int nDifficulty = 18;

    // Get the tag of the trigger to be used as the tag
    // of the destination waypoint DST_<tag of the trigger>

    string sTag = GetTag(OBJECT_SELF);

    // Check the nDone flag to see if the secret door has
    // already been found

    int nDone = GetLocalInt(OBJECT_SELF,"D_"+sTag);

    // If the PC is searching, then use their search skill bonus

    object oPC = GetNearestCreature(CREATURE_TYPE_PLAYER_CHAR,PLAYER_CHAR_IS_PC,OBJECT_SELF);
    int nDetectMode = GetDetectMode(oPC);
    int nSkill;
    if (nDetectMode == 0)
        nSkill = GetSkillRank(SKILL_SEARCH,oPC);
    else
        nSkill = 0;

    // Check to see if the PC found it

    int nDieRoll = d20();
    if ((nDone == 0) &&
        (nSkill + nDieRoll >= nDifficulty))
    {

        // The PC found it, so create a secret door at the
        // location of the nearest s_door_loc invisible
        // object, set the nDone flag, and make the secret door
        // a plot object that can not be destroyed

        location locSecret = GetLocation(GetNearestObjectByTag("s_door_loc"));
        object oDoor;
        oDoor = CreateObject(OBJECT_TYPE_PLACEABLE,"s_door",locSecret,TRUE);
        SetLocalString(oDoor,"Destination","DST_"+sTag);
        SendMessageToPC(oPC,"You have found a secret door!");
        SetLocalInt(OBJECT_SELF,"D_"+sTag,1);
        SetPlotFlag(oDoor,1);
    }

}
