Author Topic: A quick little mob spawner gift requested by a form member  (Read 652 times)

Legacy_KMdS!

  • Sr. Member
  • ****
  • Posts: 364
  • Karma: +0/-0
A quick little mob spawner gift requested by a form member
« on: March 29, 2016, 12:37:00 am »


               

I just created this for a forum member and though it might be something others might like.


 


// encounter_oe
 
/*
 
    A quick simple spawn system I just now created for a forum menber that gives you the
    option to have random spawn locations.  To implement, place in your generic triggers
    on enter event and give your generic trigger a uniquie tag. Give your spawn waypoints
    the same tag but append a consecutive numeric value to the tag
 
    Example, generic trigger tag...
        MOBSPAWN
 
    Associated waypoint tags...
        MOBSPAWN1
        MOBSPAWN2
        etc....
 
    Set a local int named "NUMOFWPS' on WP#1 with the number of waypoints
    to be chosen from for the spawn location. This system will randomly
    pick between WP#1 and the qty to set as the spawn location.
 
    Set a local int named "QTY' on on WP#1 with the total number of creatures
    to be spawned.
 
    Set a local string named "RESREF" on WP#1 with the base resref of the
    creature to be spawned. Give all the creatures you want to spawn the
    same resref and append a consecutive numeric value
 
    Example stored "RESREF" value
    monster
 
    Example Creature resrefs...
    monster1
    monster2
    etc...
 
    Set a local int named "CREATURETYPES' on WP#1 with the numer of diferent creatures to
    be spawned. Using the resref examples above that list only 2 resrefs, monster1
    and monster2, set CREATURETYPES = 2 and you will get equal amounts of both in
    the spawn
 
    Set a local int named "PERCENT_CHANCE' on WP#1 for the chance of a spawn occurring.
 
    Set a local float named "DELAY' on WP#1 for the time before the trigger is active again.
    This is so that you can't activate the trigger again untill the passing in seconds
    of whatever delay you set. There is a default value of 20.0 seconds
 
    I think I covered it all. Have fun.... I like it so much I think I'll post it to
    the forum. Modify to suit your needs :)
 
*/
//  Set "QTY' with thE total number of creatures to be spawned
//  Set "CREATURETYPES' with the numer of diferent creature to be spawned
//  Set "RESREF' with the base resref
//  Set "NUMOFWPS' with the number of waypoints to be chosen from for the spawn location
//  Set "PERCENT_CHANCE' for the chance of a spawn occurring
void spawncreatures(int iQty, int iCreatureTypes, string sBaseTag, string sResRef, int nNbrOfWypnts, int iChance = 100);
void spawncreatures(int iQty, int iCreatureTypes, string sBaseTag, string sResRef, int nNbrOfWypnts, int iChance = 100)
{
    if(d100()<=iChance)
    {
        string sWPTag = sBaseTag+IntToString(Random(nNbrOfWypnts)+1);
        object oSpawnWP = GetWaypointByTag(sWPTag);
        location lSpawnPoint = GetLocation(oSpawnWP);
 
        int i;
        for(i=0;i<iQty;i++)
        {
            object oSpawn = CreateObject(OBJECT_TYPE_CREATURE, sResRef+IntToString((i%iCreatureTypes)+1), lSpawnPoint);
        }
    }
}
 
void ResetEncounter(float fDelay, object oRepository)
{
    DelayCommand(fDelay, SetLocalInt(oRepository, "ACTIVATED", FALSE));
}
 
void main()
{
    object oPC = GetEnteringObject();
    if(GetIsPC(oPC))
    {
        string sBaseTag = GetTag(OBJECT_SELF);
        object oRepository = GetObjectByTag(sBaseTag+"1");
 
        int bDoNotProcess = GetLocalInt(oRepository, "ACTIVATED");
        if(bDoNotProcess==FALSE)
        {
            int iQty =   GetLocalInt(oRepository, "QTY");
            int iCTypes = GetLocalInt(oRepository, "CREATURETYPES");
            string sResRef = GetLocalString(oRepository, "RESREF");
            int iWPs = GetLocalInt(oRepository, "NUMOFWPS");
            int iPctChance = GetLocalInt(oRepository, "PERCENT_CHANCE");
 
            spawncreatures(iQty, iCTypes, sBaseTag, sResRef, iWPs, iPctChance);
            SetLocalInt(oRepository, "ACTIVATED", TRUE);
            float fDelay =   GetLocalFloat(oRepository, "DELAY");
            if(fDelay == 0.0)
                fDelay = 20.0;
            AssignCommand(GetModule(), ResetEncounter(fDelay, oRepository));
        }
    }
}

               
               

               


                     Modifié par KMdS!, 01 avril 2016 - 04:49 .
                     
                  


            

Legacy_KMdS!

  • Sr. Member
  • ****
  • Posts: 364
  • Karma: +0/-0
A quick little mob spawner gift requested by a form member
« Reply #1 on: March 29, 2016, 03:07:17 am »


               

Had an error in the code, fixed it .....


 


Its a pain getting older.....Glasses where are my GLASSES????? lol



               
               

               
            

Legacy_mornnova

  • Newbie
  • *
  • Posts: 30
  • Karma: +0/-0
A quick little mob spawner gift requested by a form member
« Reply #2 on: March 29, 2016, 04:21:17 am »


               

ty KMdS.



               
               

               
            

Legacy_KMdS!

  • Sr. Member
  • ****
  • Posts: 364
  • Karma: +0/-0
A quick little mob spawner gift requested by a form member
« Reply #3 on: March 29, 2016, 06:59:19 am »


               

Added a check to only run if a PC is entering object



               
               

               
            

Legacy_KMdS!

  • Sr. Member
  • ****
  • Posts: 364
  • Karma: +0/-0
A quick little mob spawner gift requested by a form member
« Reply #4 on: March 29, 2016, 07:30:38 am »


               

I use this script on a generic trigger,



               
               

               
            

Legacy_WhiZard

  • Hero Member
  • *****
  • Posts: 2149
  • Karma: +0/-0
A quick little mob spawner gift requested by a form member
« Reply #5 on: March 29, 2016, 05:25:10 pm »


               


 


(i+1)%iCreatureTypes)+1


 




 


Nothing wrong with this expression, but it seems a little odd compared to simply doing (i%iCreatureTypes) + 1.  The (i+1) here causes you to begin with "Monster2" and have "Monster1" as the last element before looping back.  This makes "Monster1" average the least generated and "Monster2" average the most generated with "Monster3" and thereafter as diminishing in average appearances.


               
               

               
            

Legacy_KMdS!

  • Sr. Member
  • ****
  • Posts: 364
  • Karma: +0/-0
A quick little mob spawner gift requested by a form member
« Reply #6 on: March 29, 2016, 05:36:41 pm »


               

The modulo "%" operation returns the remainder of the division by the modulo . Therefore if you set to modulo 3 and the integer operated on is 6, the result is '0'. 7 is 1, 8 is 2, 9 is 0. You should never get the value of the modulo .


 


Since the int 'i' starts the count at '0' you have the same situation. The 'i++' in the 'for' condition means that the value of 'i' is not modified until after the iteration of the 'for' conditional loop. Every reference to 'i' within the 'for' loop will return the value of 'i' prior the the 'i++', or '0' on the first loop, '1' on the second, etc....


 


Most people are comfortable starting a count on 1 instead of 0 (count to ten, bet you didn't start at '0', lol) and any modification of 'i' within the loop would modify the conditional parameters, I added the +1's.


 


Those of us who are more familiar with code/script operation, like you, will notice my little "cheat".  '<img'> It was more of an adaptation/accommodation for newer scriptors/mod builders.



               
               

               
            

Legacy_meaglyn

  • Hero Member
  • *****
  • Posts: 1451
  • Karma: +0/-0
A quick little mob spawner gift requested by a form member
« Reply #7 on: March 29, 2016, 07:03:17 pm »


               

"Modulo" you mean?


 


@WhiZard is right.   Your comments refer to the second +1, the one after the modulo operation. The (i+1) before it skews the odds as WhiZard described.  Without that first one they can still use resref1...resrefN rather than resref0..resref(N-1). 


 


One question I have is why are you using a variable just for the resref but hardcoding the number of creatures and the chance and whatnot?   In that case you need a copy of the script for every different trigger anyway so why not hardcode the resref.  Or better use variables for all of those settings as well. 


 


Also, it would better, I think, to use variables for resref named RESREF1 through RESREFN so that you can easily have a spawn do a bunch of different types of creatures without having to have resrefs that match a naming pattern.


 


My two coppers anyway '<img'>



               
               

               
            

Legacy_KMdS!

  • Sr. Member
  • ****
  • Posts: 364
  • Karma: +0/-0
A quick little mob spawner gift requested by a form member
« Reply #8 on: March 29, 2016, 07:47:53 pm »


               

Yes, Modulo, hehe. It has been years since I had to refer to it by name. I caught it before your post, though. I knew it was wrong but took some time to find it in my reference materials.


 


You and @WhiZard is right on the second point, your posts made me take another deeper look.


 


Your third point is a very good and valid point. My goal in creating this code was to Create a short, SIMPLE, , easy to read, well documented little custom spawner for a forum member request. I liked the resulting very compact bit work enough that I thought it might be a way to offer any new scripter how to  implement multiple random spawn locations for a single spawner and show how you could reference and spawn multiple creatures.


Basically an entry level bit of something to open the imagination. hence my invitation to "Modify to suit your needs."


 


You know how it goes, sometimes you can't see the forest for the trees and a fresh look helps evolve the subject. Thanks


 


*Original post edited.



               
               

               
            

Legacy_KMdS!

  • Sr. Member
  • ****
  • Posts: 364
  • Karma: +0/-0
A quick little mob spawner gift requested by a form member
« Reply #9 on: April 01, 2016, 06:05:31 am »


               

I had to edit/modify the original posted code because it wouldn't work with encounter triggers. Seems they are all single shot. Even when set to continuous, the instance is destroyed and a duplicate is created. Because of this, implementing the delay is problematic and It took a little time to test the work over and over to figure it out.


 


The original implementation worked just fine with the generic triggers, they are not destroyed on use, but the code the way it is now should woek with any trigger. It's funny how a simple idea can be so much trouble to implement. hehe.


 


Anyway, i have tested it into the ground and it works in all cases.


 


Just to give others an idea, the system now uses the first waypoint as the repository for the variables required for the system. It could be easily modified so that you could set multiple waypoints with different variables on each to allow you to spawn a different set of creatures at different waypoints, etc...