Author Topic: Endless list of homebrew functions - resurrected (again?)  (Read 509 times)

Legacy_Tarot Redhand

  • Hero Member
  • *****
  • Posts: 4165
  • Karma: +0/-0
Endless list of homebrew functions - resurrected (again?)
« on: April 16, 2013, 03:34:12 pm »


               A long, long time ago on the old bioboards there used to be a "Endless list of homebrew functions" thread. After the move to these boards I beleive someone tried to make such a thread on here. So if it's died once why am I trying to get it going once again? Probably 'cos I'm clinically sane but mad and possibly over-optimistic in this case.

The old thread had a couple of rules if I remember correctly. They were make it original or an improvement on an original idea and do not ask questions, just post routines. If possible could we (as though anyone else than me is going to post short routines in here...) stick to these please.

Right I will kick off the list with a set of routines to do with conditional locking in the next message.

TR
               
               

               


                     Modifié par Tarot Redhand, 16 avril 2013 - 02:35 .
                     
                  


            

Legacy_Tarot Redhand

  • Hero Member
  • *****
  • Posts: 4165
  • Karma: +0/-0
Endless list of homebrew functions - resurrected (again?)
« Reply #1 on: April 16, 2013, 03:38:35 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.
*/
//::///////////////////////////////////////////////
//:: 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));  
    [/list] }
   else
   {


       if(GetIsNight())
           

SetLocked(oDoor, iDoLock);  
       else
           

SetLocked(oDoor, !(iDoLock));  
     [/list] }
 [/list] }

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

TR 
               
               

               


                     Modifié par Tarot Redhand, 17 avril 2013 - 01:35 .
                     
                  


            

Legacy_Tarot Redhand

  • Hero Member
  • *****
  • Posts: 4165
  • Karma: +0/-0
Endless list of homebrew functions - resurrected (again?)
« Reply #2 on: April 16, 2013, 03:41:42 pm »


               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;  
    [/list] }

//test and lock/unlock as appropriate

   if((iHourNow >= iStartHour)&&(iHourNow <= iEndHour))
       

SetLocked(oDoor, iDoLock);  
   else
       

SetLocked(oDoor, !(iDoLock));  

   return TRUE;
 [/list] }

//----------------------------------------------------------------------------------------------------------------
TR
               
               

               


                     Modifié par Tarot Redhand, 17 avril 2013 - 01:32 .
                     
                  


            

Legacy_Tarot Redhand

  • Hero Member
  • *****
  • Posts: 4165
  • Karma: +0/-0
Endless list of homebrew functions - resurrected (again?)
« Reply #3 on: April 16, 2013, 03:42:45 pm »


               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;

 [/list] }

//----------------------------------------------------------------------------------------------------------------
TR
               
               

               


                     Modifié par Tarot Redhand, 17 avril 2013 - 01:29 .
                     
                  


            

Legacy_Tarot Redhand

  • Hero Member
  • *****
  • Posts: 4165
  • Karma: +0/-0
Endless list of homebrew functions - resurrected (again?)
« Reply #4 on: April 16, 2013, 03:43:35 pm »


               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++;
   [/list]}
if(iMatchingclass)
       
SetLocked(oDoor, iDoLock);  
   else
       
SetLocked(oDoor, !(iDoLock));  
[/list]}

//----------------------------------------------------------------------------------------------------------------
TR
               
               

               


                     Modifié par Tarot Redhand, 17 avril 2013 - 01:26 .
                     
                  


            

Legacy_Tarot Redhand

  • Hero Member
  • *****
  • Posts: 4165
  • Karma: +0/-0
Endless list of homebrew functions - resurrected (again?)
« Reply #5 on: April 16, 2013, 03:50:16 pm »


               And here is a little test routine ->

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

   if(!(GetIsPC(oPC)))
        return;
//put call(s) to selected routines here
[/list]}

Alternatively you can download the original version here.

TR
               
               

               


                     Modifié par Tarot Redhand, 17 avril 2013 - 01:24 .
                     
                  


            

Legacy_FunkySwerve

  • Hero Member
  • *****
  • Posts: 2325
  • Karma: +0/-0
Endless list of homebrew functions - resurrected (again?)
« Reply #6 on: April 16, 2013, 05:19:25 pm »


               Let's stick with the one we already have, instead of spawning multiples:

http://social.biowar...2/index/3174344

Funky
               
               

               
            

Legacy_Tarot Redhand

  • Hero Member
  • *****
  • Posts: 4165
  • Karma: +0/-0
Endless list of homebrew functions - resurrected (again?)
« Reply #7 on: April 16, 2013, 07:22:57 pm »


               Saw that someone had posted in this thread (other than me) already and eagerly came to see what nugget of scripting wisdom they had to share. Seems I can't win whatever way you look at it. To find that thread you would need to go to page 76 of the index for scripting as the last post in there was over 3 years ago. A thread that only had 4 posts in it anyway. If I had posted in there I would have been accused of necromancy for raising the dead. All I was trying to do was to get some interest and sharing going again.

Cest la vie

TR
               
               

               
            

Legacy_Rolo Kipp

  • Hero Member
  • *****
  • Posts: 4349
  • Karma: +0/-0
Endless list of homebrew functions - resurrected (again?)
« Reply #8 on: April 16, 2013, 08:17:28 pm »


               <pulling a bunch of...>

If I had anything to offer, I'd post something here... but the only original scripting I've done, really, is the Regional Mod system. And that's a bit too much for a forum post :-(

But I do hope people post. And I'll ask no questions :-) Promise!

<...lint out of his pouches>
               
               

               
            

Legacy_GhostOfGod

  • Hero Member
  • *****
  • Posts: 1490
  • Karma: +0/-0
Endless list of homebrew functions - resurrected (again?)
« Reply #9 on: April 16, 2013, 11:11:13 pm »


               This is nothing special. I just needed a function to see if a player possessed an item by resref rather than by tag since I have multiple widgets that use the same tag for tag based scripting(language tokens, class ability widgets, mounts, etc.). There might already be a function for this in one of the many includes in the game. I didn't bother trying to find one.


int GetHasItemByResRef(object oTarget, string sResRef)
{
    object oItem = GetFirstItemInInventory(oTarget);
    while (GetIsObjectValid(oItem))
    {
        if (GetResRef(oItem) == sResRef) return TRUE;
        oItem = GetNextItemInInventory(oTarget);
    }
    int iSlot;
    for (iSlot=0; iSlot<NUM_INVENTORY_SLOTS; iSlot++)
    {
        oItem = GetItemInSlot(iSlot, oTarget);
        if (GetIsObjectValid(oItem))
        {
            if (GetResRef(oItem) == sResRef) return TRUE;
        }
    }
    return FALSE;
}
               
               

               
            

Legacy_GhostOfGod

  • Hero Member
  • *****
  • Posts: 1490
  • Karma: +0/-0
Endless list of homebrew functions - resurrected (again?)
« Reply #10 on: April 17, 2013, 03:22:39 am »


               A couple others I made specifically for random lightning but they can be used for other stuff:


//Return random float between 0.00 and integer parameter
float RandomFloat(int iInt)
{
    int iRandomWhole = Random(iInt);
    int iRandomHundreds = Random(100);
    float fFloat = IntToFloat(iRandomWhole) + (IntToFloat(iRandomHundreds) * 0.01);
    return fFloat;
}


//Get a random location in an area. Does not account for change in z-axis. Best if used in flat area.
location RandomLocation(object oArea)
{
    int iAreaWidth = GetAreaSize(AREA_WIDTH, oArea);
    int iAreaHeight = GetAreaSize(AREA_HEIGHT, oArea);
    int iX = iAreaWidth * 10;
    int iY = iAreaHeight * 10;
    vector vVector = Vector(IntToFloat(Random(iX)), IntToFloat(Random(iY)), 0.0);
    location lLoc = Location(oArea, vVector, IntToFloat(Random(360)));
    return lLoc;
}
               
               

               
            

Legacy_FunkySwerve

  • Hero Member
  • *****
  • Posts: 2325
  • Karma: +0/-0
Endless list of homebrew functions - resurrected (again?)
« Reply #11 on: April 17, 2013, 07:20:06 am »


               

Tarot Redhand wrote...

Saw that someone had posted in this thread (other than me) already and eagerly came to see what nugget of scripting wisdom they had to share. Seems I can't win whatever way you look at it. To find that thread you would need to go to page 76 of the index for scripting as the last post in there was over 3 years ago. A thread that only had 4 posts in it anyway. If I had posted in there I would have been accused of necromancy for raising the dead. All I was trying to do was to get some interest and sharing going again.

Cest la vie

TR


So your reason for not using the existing thread is that it's unused. Which leaves me to wonder why you would create yet another such thread. '<img'>

More seriously, it's not thread necromancy if you post in a stickied thread - or one you're hoping to get stickied. The entirely point of such threads is that they stick without constant posts.

But by all means, ignore me. Who cares about keeping things in one place when we can put them in two and get our names on the thread? ':whistle:'

Funky
               
               

               
            

Legacy_Tarot Redhand

  • Hero Member
  • *****
  • Posts: 4165
  • Karma: +0/-0
Endless list of homebrew functions - resurrected (again?)
« Reply #12 on: April 17, 2013, 02:58:59 pm »


               @FunkySwerve I would hazard a guess that after 3+ years there is very little chance of the previous thread (linked to above) being stickied now. Also if I had as much pride as you seem to think I could just as easily have created a thread called "The Ego Has Landed - TR is here." ('^_^'). As I have already given my genuine reasons for starting this thread, I will not repeat them here. Rather than ignore you I will instead ask you to share some small function that you have created. Having been around both the original and these newer boards I have the impression that you are quite a good scripter. Use either this or the original thread - I don't care which. All I care about is the dissemination of good ideas.

Talking of which, having rootled around in the ancient threads from when these newer ones were just starting up I came across a very interesting one - Ways to post code and links on new boards. - which I have put to use by editing my earlier code posts above. As you can see the results are not perfect, but they are a lot better than not using them.

Have fun and good scripting.

TR
               
               

               


                     Modifié par Tarot Redhand, 20 avril 2013 - 10:08 .
                     
                  


            

Legacy_WhiZard

  • Hero Member
  • *****
  • Posts: 2149
  • Karma: +0/-0
Endless list of homebrew functions - resurrected (again?)
« Reply #13 on: April 17, 2013, 03:31:25 pm »


               I have centralized these threads and posted on the relevant sticky.  ShadOoOw's continuation is cited as the central continuation and I have linked to this thread from ShadOoOw's.
               
               

               
            

Legacy_FunkySwerve

  • Hero Member
  • *****
  • Posts: 2325
  • Karma: +0/-0
Endless list of homebrew functions - resurrected (again?)
« Reply #14 on: April 19, 2013, 05:08:19 am »


               @ Whiz: Thanks, keeping a semblance of organization is crucial to the usefulness of these forums - especially given the somewhat crippled search feature.

@ Tarot: I don't generally contribute to these threads, because I found them useful only for a brief window in my scripting career, as learning examples. I do post a lot of code, but only when it's specific to a question in a given thread, as I think it has a much better chance of being searched up and used productively that way.

/free bump '<img'>

Funky
               
               

               


                     Modifié par FunkySwerve, 19 avril 2013 - 04:08 .