Author Topic: Inspired by _knightmare_  (Read 558 times)

Legacy_Tarot Redhand

  • Hero Member
  • *****
  • Posts: 4165
  • Karma: +0/-0
Inspired by _knightmare_
« on: December 19, 2010, 05:48:43 pm »


               Here is a set of routines that deal with conditional locking/unlocking of objects such as chests and sarcophogi. These were inspired by similar routines written by _knightmare_ for use with doors. These routines are designed to be part of an objects OnClick event handler. Because standard doors (as opposed to placeable doors) don't have this event, these routines are not designed for use with them. Anyway I hope someone has a use for them.

TR

//::///////////////////////////////////////////////
//:: Name     Service Locks
//:: FileName scr_do_locks
//:: Copyright © 2001 Bioware Corp.
//:://////////////////////////////////////////////
/*

*/
//:://////////////////////////////////////////////
//:: Created By: Tarot Redhand
//:: Created On: 18th September 2010
//:://////////////////////////////////////////////

void DayNightLock(object oDoor, int iInDayTime = TRUE, int iDoLock = TRUE)

//simple routine to lock/unlock lockable object oDoor depending on whether it is day or night in game terms
//parameter iInDayTime should be set to FALSE for nighttime
//parameter iDoLock should be set to FALSE for the oDoor object to be unlocked in the active period
//Note - this routine assumes (has the pre-condition) that the oDoor object is already closed

{
    if(iInDayTime)
    {
        if(GetIsDay())
            SetLocked(oDoor, iDoLock);
        else
            SetLocked(oDoor, !(iDoLock));
    }
    else
    {
        if(GetIsNight())
            SetLocked(oDoor, iDoLock);
        else
            SetLocked(oDoor, !(iDoLock));
     }
}

//----------------------------------------------------------------------------------------------------------------

int TimeLock(object oDoor, int iStartHour, int iEndHour, int iDoLock = TRUE)

//routine to lock/unlock lockable object oDoor between the hours (inclusive) of iStartHour and iEndHour
//if iDoLock is TRUE the object will Locked during the active period otherwise it will be unlocked
//TimeLock returns FALSE if erroneous data is passed to it in the parameters otherwise TRUE is returned
//NOTE this routine expects a minimum difference of 2 hours between iStartHour and iEndHour
//Possible side-effect - if the period described goes from before midnight to after, iEndHour will be
//increased by 24 upon exit from this routine

{

//check for bad input data

    if((iStartHour < 0)||(iEndHour < 0)||(iStartHour > 23)||(iEndHour > 23)||(iStartHour == iEndHour)||(abs(iEndHour - iStartHour) < 2))
        return FALSE;

//get current game hour

    int iHourNow = GetTimeHour();

//Through midnight? adjust certain parameters to correct for it then.

    if(iStartHour > iEndHour)
    {
        iEndHour += 24;
        if(iStartHour > iHourNow)
            iHourNow += 24;
    }

//test and lock/unlock as appropriate

    if((iHourNow >= iStartHour)&&(iHourNow <= iEndHour))
        SetLocked(oDoor, iDoLock);
    else
        SetLocked(oDoor, !(iDoLock));

    return TRUE;
}

//----------------------------------------------------------------------------------------------------------------

int AlignmentLock(object oDoor, object oPC, int iLawChaosValue = ALIGNMENT_ALL, int iGoodEvilValue = ALIGNMENT_GOOD, int iDoLock = TRUE)

//routine to lock/unlock lockable object oDoor depending on the alignment of the creature oPC
//the parameters iLawChaosValue and iGoodEvilValue are tested against the alignment of oPC to
//determine the action to take
//
//Note - this routine expects iLawChaosValue and iGoodEvilValue to contain values consistant with
//the ALIGNMENT_* constants and values outside this range are considered to be in error
//by doing it this way means that not only can specific alignments can be targetted (ie Lawful good)
//but also that groups of alignments can be specified (the default parameters are set for all good alignments).
//
//the parameter iDoLock determines whether the oDoor object is locked or unlocked when the
//alignment conditions are met for the oDoor object to be unlocked set iDoLock to FALSE
//this routine will terminate and return FALSE if any error conditions are detected otherwise
//TRUE will be returned on a successful completion

{
//check parameters iLawChaosValue and iGoodEvilValue are in the valid range and if not exit returning FALSE

    if((iLawChaosValue < 0)||(iLawChaosValue > 5)||(iGoodEvilValue < 0)||(iGoodEvilValue > 5))
        return FALSE;

//get the actual law/chaos value of the creature oPC. If the oPC object does not have
//an alignment exit returning FALSE.

    int iActualLawChaos = GetAlignmentLawChaos(oPC);

    if(iActualLawChaos == -1)
        return FALSE;

//get the actual good/evil value of the creature oPC. We do not have to test for an invalid object here
//because the previous test has already done so

    int iActualGoodEvil = GetAlignmentGoodEvil(oPC);

//by setting iActivate to False here we only need to test for conditions that necessitate changing
//its value in the iLawChaosValue tests

    int iActivate = FALSE;

    if((iLawChaosValue == ALIGNMENT_ALL)||(iActualLawChaos == iLawChaosValue))
        iActivate = TRUE;

//if iActivate == FALSE at this point, no further tests are necessary otherwise we only need to test for
//the 1 condition that will necessitate changing its value in the iGoodEvilValue tests

    if(iActivate && (iGoodEvilValue != ALIGNMENT_ALL)&&(iActualGoodEvil != iGoodEvilValue))
            iActivate = FALSE;

//finally if the conditions have been met we put the lock in the state specified otherwise
//we set the lock in the opposite state. We then exit the routine returning TRUE to indicate
//successful completion.

    if(iActivate)
        SetLocked(oDoor, iDoLock);
    else
        SetLocked(oDoor, !(iDoLock));

    return TRUE;

}

//----------------------------------------------------------------------------------------------------------------

void classLock(object oDoor, object oPC, int iTargetclass = class_TYPE_FIGHTER, int iDoLock = TRUE)

//routine to lock/unlock lockable object oDoor depending on the class of the creature oPC
//the parameter iTargetclass is tested against the class of oPC to determine the action to take
//
//Note - this routine expects iTargetclass to contain a value consistant with the class_TYPE_* constants
//
//the parameter iDoLock determines whether the oDoor object is locked or unlocked when the
//class conditions are met. for the oDoor object to be unlocked set iDoLock to FALSE

{
    int iMatchingclass = FALSE;
    int iIndex = 1;

    while(iIndex < 4)
    {
        if(GetclassByPosition(iIndex, oPC) == iTargetclass)
        {
            iMatchingclass = TRUE;
            break;
        }
        iIndex++;
    }

    if(iMatchingclass)
        SetLocked(oDoor, iDoLock);
    else
        SetLocked(oDoor, !(iDoLock));

}

//----------------------------------------------------------------------------------------------------------------

void main()
{
    object oPC = GetClickingObject();

    if(!(GetIsPC(oPC)))
        return;

    //put call(s) to selected routines here

}
               
               

               
            

Legacy_Greyfort

  • Sr. Member
  • ****
  • Posts: 442
  • Karma: +0/-0
Inspired by _knightmare_
« Reply #1 on: December 20, 2010, 02:02:46 am »


               Looked over your functions script very well done.  
I found some errors:
const int iTargetSet= 0;//           had int iTargetclass= class_TYPE_FIGHTER
void classLock(object oDoor, object oPC, int iTargetclass=iTargetSet, int iDoLock = TRUE)
{
   int iMatchingclass = FALSE;
   int iIndex = 1;

   while(iIndex
   {
       if(GetclassByPosition(iIndex, oPC) == iTargetclass )
       {
           iMatchingclass = TRUE;
           break;
       }
       iIndex++;
   }

   if(iMatchingclass){
       SetLocked(oDoor, iDoLock);
   }else{
       SetLocked(oDoor, !(iDoLock));}

}
I corrected and made it work with NWN1

Here is Edited compiled working version:

//::///////////////////////////////////////////////
//:: Name     Service Locks
//:: FileName scr_do_locks
//:: Copyright © 2001 Bioware Corp.
//:://////////////////////////////////////////////
/*
Here is a set of routines that deal with conditional locking/unlocking of
objects such as chests and sarcophogi. These were inspired by similar
routines written by _knightmare_ for use with doors. These routines are
designed to be part of an objects OnClick event handler. Because standard
doors (as opposed to placeable doors) don't have this event, these
routines are not designed for use with them. Anyway I hope someone has a
use for them.
*/
//:://////////////////////////////////////////////
//:: Created By: Tarot Redhand
//:: Created On: 18th September 2010
//:://////////////////////////////////////////////


//simple routine to lock/unlock lockable object oDoor depending on whether it is day or night in game terms
//parameter iInDayTime should be set to FALSE for nighttime
//parameter iDoLock should be set to FALSE for the oDoor object to be unlocked in the active period
//Note - this routine assumes (has the pre-condition) that the oDoor object is already closed
void DayNightLock(object oDoor, int iInDayTime = TRUE, int iDoLock = TRUE)
{
   if(iInDayTime)
   {
       if(GetIsDay())
           SetLocked(oDoor, iDoLock);
       else
           SetLocked(oDoor, !(iDoLock));
   }
   else
   {
       if(GetIsNight())
           SetLocked(oDoor, iDoLock);
       else
           SetLocked(oDoor, !(iDoLock));
    }
}

//----------------------------------------------------------------------------------------------------------------


//routine to lock/unlock lockable object oDoor between the hours (inclusive) of iStartHour and iEndHour
//if iDoLock is TRUE the object will Locked during the active period otherwise it will be unlocked
//TimeLock returns FALSE if erroneous data is passed to it in the parameters otherwise TRUE is returned
//NOTE this routine expects a minimum difference of 2 hours between iStartHour and iEndHour
//Possible side-effect - if the period described goes from before midnight to after, iEndHour will be
//increased by 24 upon exit from this routine
int TimeLock(object oDoor, int iStartHour, int iEndHour, int iDoLock = TRUE)
{

//check for bad input data

   if((iStartHour  23)||(iEndHour > 23)||(iStartHour == iEndHour)||(abs(iEndHour - iStartHour)
       {return FALSE;}

//get current game hour

   int iHourNow = GetTimeHour();

//Through midnight? adjust certain parameters to correct for it then.

   if(iStartHour > iEndHour)
   {
       iEndHour += 24;
       if(iStartHour > iHourNow)
           {iHourNow += 24;}
   }

//test and lock/unlock as appropriate

   if((iHourNow >= iStartHour)&&(iHourNow
       {SetLocked(oDoor, iDoLock);
   }else
       {SetLocked(oDoor, !(iDoLock));}

   return TRUE;
}

//----------------------------------------------------------------------------------------------------------------


//routine to lock/unlock lockable object oDoor depending on the alignment of the creature oPC
//the parameters iLawChaosValue and iGoodEvilValue are tested against the alignment of oPC to
//determine the action to take
//
//Note - this routine expects iLawChaosValue and iGoodEvilValue to contain values consistant with
//the ALIGNMENT_* constants and values outside this range are considered to be in error
//by doing it this way means that not only can specific alignments can be targetted (ie Lawful good)
//but also that groups of alignments can be specified (the default parameters are set for all good alignments).
//
//the parameter iDoLock determines whether the oDoor object is locked or unlocked when the
//alignment conditions are met for the oDoor object to be unlocked set iDoLock to FALSE
//this routine will terminate and return FALSE if any error conditions are detected otherwise
//TRUE will be returned on a successful completion
int AlignmentLock(object oDoor, object oPC, int iLawChaosValue = ALIGNMENT_ALL, int iGoodEvilValue = ALIGNMENT_GOOD, int iDoLock = TRUE)
{
//check parameters iLawChaosValue and iGoodEvilValue are in the valid range and if not exit returning FALSE

   if((iLawChaosValue  5)||(iGoodEvilValue  5))
      { return FALSE; }

//get the actual law/chaos value of the creature oPC. If the oPC object does not have
//an alignment exit returning FALSE.

   int iActualLawChaos = GetAlignmentLawChaos(oPC);

   if(iActualLawChaos == -1)
       {return FALSE; }

//get the actual good/evil value of the creature oPC. We do not have to test for an invalid object here
//because the previous test has already done so

   int iActualGoodEvil = GetAlignmentGoodEvil(oPC);

//by setting iActivate to False here we only need to test for conditions that necessitate changing
//its value in the iLawChaosValue tests

   int iActivate = FALSE;

   if((iLawChaosValue == ALIGNMENT_ALL)||(iActualLawChaos == iLawChaosValue))
       {iActivate = TRUE;}

//if iActivate == FALSE at this point, no further tests are necessary otherwise we only need to test for
//the 1 condition that will necessitate changing its value in the iGoodEvilValue tests

   if(iActivate && (iGoodEvilValue != ALIGNMENT_ALL)&&(iActualGoodEvil != iGoodEvilValue))
           {iActivate = FALSE;}

//finally if the conditions have been met we put the lock in the state specified otherwise
//we set the lock in the opposite state. We then exit the routine returning TRUE to indicate
//successful completion.

   if(iActivate){
       SetLocked(oDoor, iDoLock);
   }else{
       SetLocked(oDoor, !(iDoLock));}

   return TRUE;

}

//----------------------------------------------------------------------------------------------------------------


//-------------------------------- class Type Constants ------------------------
//::
//::                            Base classes
//::
//::    class_TYPE_BARBARIAN - 0      class_TYPE_PALADIN -  6
//::    class_TYPE_BARD -      1      class_TYPE_RANGER -   7
//::    class_TYPE_CLERIC -    2      class_TYPE_ROGUE -    8
//::    class_TYPE_DRUID -     3      class_TYPE_SORCERER - 9
//::    class_TYPE_FIGHTER -   4      class_TYPE_WIZARD -   10
//::    class_TYPE_MONK -      5
//::
//::                            Prestige classes
//::
//::    class_TYPE_SHADOWDANCER -     27
//::    class_TYPE_HARPER -           28
//::    class_TYPE_ARCANE_ARCHER -    29
//::    class_TYPE_ASSASSIN -         30
//::    class_TYPE_BLACKGUARD -       31
//::    class_TYPE_DIVINECHAMPION -   32 (Champion of Torm)
//::    class_TYPE_WEAPON_MASTER -    33
//::    class_TYPE_PALE_MASTER -      34
//::    class_TYPE_SHIFTER -          35
//::    class_TYPE_DWARVENDEFENDER -  36
//::    class_TYPE_DRAGONDISCIPLE -   37
//::
//------------------------------------------------------------------------------
//routine to lock/unlock lockable object oDoor depending on the class of the creature oPC
//the parameter iTargetclass is tested against the class of oPC to determine the action to take
//
//Note - this routine expects iTargetclass to contain a value consistant with the class_TYPE_* constants
//
//the parameter iDoLock determines whether the oDoor object is locked or unlocked when the
//class conditions are met. for the oDoor object to be unlocked set iDoLock to FALSE
//                                   had int iTargetclass= class_TYPE_FIGHTER

const int iTargetSet= 0;// use class numbers from above
void classLock(object oDoor, object oPC, int iTargetclass=iTargetSet, int iDoLock = TRUE)
{
   int iMatchingclass = FALSE;
   int iIndex = 1;

   while(iIndex
   {
       if(GetclassByPosition(iIndex, oPC) == iTargetclass )
       {
           iMatchingclass = TRUE;
           break;
       }
       iIndex++;
   }

   if(iMatchingclass){
       SetLocked(oDoor, iDoLock);
   }else{
       SetLocked(oDoor, !(iDoLock));}

}

//----------------------------------------------------------------------------------------------------------------

void main()
{
   object oPC = GetClickingObject();

   if(!(GetIsPC(oPC)))
      { return;}

   //put call(s) to selected routines here

}

Will work on a include version, But it seems better especialy with the class issue to have that function at the top of your script so the const setting can be done according to unique object.

For those who are working on nwn2 use first version not the one I've posted
               
               

               


                     Modifié par Greyfort, 20 décembre 2010 - 02:06 .
                     
                  


            

Legacy_Tarot Redhand

  • Hero Member
  • *****
  • Posts: 4165
  • Karma: +0/-0
Inspired by _knightmare_
« Reply #2 on: December 20, 2010, 11:42:42 am »


               Thank you for trying this out Greyfort. I personally had no problems compiling my version and was not aware of any problem of using standard nwn constants in the header for a function as a default parameter. Are you by any chance using an external compiler as opposed to the one built into the toolset?

I would like to point out that as it stands your version will not compile because 1 line that you aliered for a more elegant solution, somehow got mangled somewhere between your compiler and here -

if((iStartHour 23)||(iEndHour > 23)||(iStartHour == iEndHour)||(abs(iEndHour - iStartHour)

As you can see the '>' has vanished and the line doesn't terminate properly either.

Having compared the 2 versions I am unsure if there were supposed to be any other errors than what I have come across. Please let me know if there are.

TR
               
               

               


                     Modifié par Tarot Redhand, 20 décembre 2010 - 03:58 .
                     
                  


            

Legacy_Tarot Redhand

  • Hero Member
  • *****
  • Posts: 4165
  • Karma: +0/-0
Inspired by _knightmare_
« Reply #3 on: December 20, 2010, 03:55:14 pm »


               Because these boards constantly seem to screw up posted code I have put a plain text version of my code (see above) which I have archived into a 7zip file. You can get it from my dropbox public folder here.

TR
               
               

               


                     Modifié par Tarot Redhand, 20 décembre 2010 - 03:56 .
                     
                  


            

Legacy_Greyfort

  • Sr. Member
  • ****
  • Posts: 442
  • Karma: +0/-0
Inspired by _knightmare_
« Reply #4 on: December 25, 2010, 12:38:21 am »


               I hadn't noticed the forums taking away the '>', I will look in to that constant issue.  I use the auora toolset not any external compilers.  perhaps the issue you speak of is why I had error.  Still a great script '<img'>
               
               

               
            

Legacy_Tarot Redhand

  • Hero Member
  • *****
  • Posts: 4165
  • Karma: +0/-0
Inspired by _knightmare_
« Reply #5 on: December 25, 2010, 01:32:56 am »


               Did you try the version I put into my dropbox public folder? Perhaps this site mangled my original code and I'm too blind to see where. Thank you for your kind comment.



TR
               
               

               
            

Legacy_Greyfort

  • Sr. Member
  • ****
  • Posts: 442
  • Karma: +0/-0
Inspired by _knightmare_
« Reply #6 on: December 25, 2010, 01:39:19 am »


               Yes and it compiled with no problems,  I think your right about the forums altering our script work.  I had no issue with the constant class TYPE_FIGHTER etc.
               
               

               
            

Legacy__Knightmare_

  • Full Member
  • ***
  • Posts: 191
  • Karma: +0/-0
Inspired by _knightmare_
« Reply #7 on: December 25, 2010, 05:17:39 am »


               Ooooooo, I'm "inspiration"!!!!!! LOL.



Agreed, nice set of scripts you have going there. '<img'>
               
               

               
            

Legacy_Tarot Redhand

  • Hero Member
  • *****
  • Posts: 4165
  • Karma: +0/-0
Inspired by _knightmare_
« Reply #8 on: December 25, 2010, 10:27:33 pm »


               Greyfort, I know of 2 things that get mangled - the upper case "C" of class get changed to lowercase and backslashes get doubled - type a single one and this site displays 2 etc. There are probably others.



_knightmare_ thank you for your kind words.



TR