Author Sticky: Post Working Scripts Here
Enthas Posted: Saturday, 06 July 07:17PM
Area Spawner:

Features:
- Spawns all types of objects
- Set time between spawns from 6 seconds to any number of hours
- Spawns in a rectangle of any size
- Holds up to 100 blueprints of objects
- Spawns infinite number of creatures
- Can set a range of objects per spawn
- Can set a total number of objects to spawn, then stop
- Any combination of conditions can be set (ex: Day, Night, Anything)
- Any combination of actions can be set for each object spawned (ex: Follow, Attack)
- Can uncomment debug lines for help that tell you whats going on inside the script
- Very easily edited script is very simple and longwinded for readability

What can I do with these features:
- Spawn placables such as campfires at night that disapear when day comes
- Spawn random loot over an area
- Spawn monsters around an npc or monster and tell them to follow
- Have an effect go off when each monster is spawned

Limitations, what can't I do:
- Spawn into an item (working on that)
- Spawn multiple types of objects with one script (need one for each object type)
- Spawn time is rounded to the nearest number divisable by 6 ex: 6, 12, 18, 24

Directions (these can also be found at the end of the script below):
NWScript:
// Directions:

// Begin by attaching the script to any objects heartbeat.  Then set the

// preferences.  After the preferences have been set, you need to make the

// templates.  The templates tell the script the objects chance of being spawned

// and the blueprint of the object.  You MUST make sure the percentages add up

// to 100.

//

// Preferences:

//    SpawnTime 6:

//      -The spawn time is the time you want to wait between each spawn, it is

//       rounded off to the nearest number divisable by 6 because of the heartbeat.

//    SpawnAreaHeight 10:

//      -The spawn area height sets the distance on the Y axis for the spawn area.

//    SpawnAreaWidth 10:

//      -The spawn area width sets the distance on the X axis for the spawn area.

//    FlagName TestSpawn:

//      -The flag name is used by the script to identify objects spawned by this

//       script. Each script needs a UNIQUE name.

//    MaxNumObjects 5:

//      -This sets the maximum number of objects per spawn, if you dont want a

//       range of objects set both max and min to the same number.

//    MinNumObjects 1:

//      -This sets the minimum number of objects per spawn.

//    MaxTotalObjects -1:

//      -This sets the total number of objects to spawn ever -1 is spawn indefinely.

//    ObjectType OBJECT_TYPE_CREATURE

//      -This sets the type of objects you are spawning.  There can only be ONE

//       type of object per script trying to spawn items and creatures with ONE

//       script will cause problems.

//    Target OBJECT_SELF:

//      -This specifies the target for the spawn, usually you want the spawn to

//       occur around the object with the script attached to it, but sometimes

//       you may want to spawn around an item with a HeartBeat that is being used

//       for something else. Or you may want to say have items and monsters spawn

//       around the same object, so you make two waypoints/objects attach this

//       script and give them both the same target. Note that the target must be

//       in the same area as the objects with the scripts attached.

//

// Templates:

//    NumberOfTemplates 2:

//      -This number tells the script how many templates you have. It MUST match

//       the number of templates below.

//    Template1 50_NW_DOG:

//      -(XX_BLUEPRINT)

//       The first two numbers are the percentage change of spawning this object.

//       The rest of the template is the blueprint of the object.  Templates must

//       be made in consecutive order as in, Template1, Template2, Template3 ect.

//       The percentages of the templates MUST add up to 100. If you only want to

//       spawn one object with a 100 percent chance of spawn you must make 2

//       identical templates because the percentage can only be 2 digits.  There

//       is also a limit of 100 templates.

//

// Object Conditions and Actions:

//    Conditions:

//      -This specifies which conditions you want the script to spawn under.

//       The script will only spawn when these conditions are true and will destroy

//       all objects that it has made when these conditions are no longer true.

//       If you want the spawn to occur always, just comment out the

//       iCondition = FakeConditio(); line (note: // is how to comment).  This

//       condition line can be very powerful.  It makes it possible to only

//       spawn durring the day, when players are near, when a quest is completed,

//       or all three of these combined.  As long as the conditions you check

//       are TRUE and FALSE they can be made to work.

//    Actions:

//      -This allows you to specify a series of actions for the objects to do

//       immidiately after they have been spawned. This also allows for some neat

//       options.  You could have the objects spawned from a head monster and

//       just after spawn tell them to follow or guard the monster.  You could

//       simulate a battle by seting factions and telling them to attack eachother.

//

//      Whatever you do, have fun with it =) If you find a bug or something

//      you think I should add PM it to Enthas or post on biowares scripting forum.




The script:

NWScript:
// Created by: Enthas - Version: 3.1

// Directions are at the very bottom of the page.

// Set all preferences for script below:

void SetPreferences()

{

    //SpeakString("-Set Preferences-", TALKVOLUME_SHOUT);

    SetLocalInt(OBJECT_SELF, "SpawnTime", 6);         // Seconds between spawns

    SetLocalInt(OBJECT_SELF, "SpawnAreaHeight", 10);  // Distance Y

    SetLocalInt(OBJECT_SELF, "SpawnAreaWidth", 10);   // Distance X

    SetLocalString(OBJECT_SELF, "FlagName", "TestSpawn");  // Unique spawn name



    SetLocalInt(OBJECT_SELF, "MaxNumObjects", 6);    // Max object number per spawn

    SetLocalInt(OBJECT_SELF, "MinNumObjects", 5);    // Min object number per spawn

    SetLocalInt(OBJECT_SELF, "MaxNumTotal", 10);    // Will turn off after this number spawned -1 is infinite

    SetLocalInt(OBJECT_SELF, "ObjectType", OBJECT_TYPE_CREATURE); // Use the Constants

    SetLocalObject(OBJECT_SELF, "Target", OBJECT_SELF); // Used to spawn around a different target. Must also change spawn function.

    SetLocalInt(OBJECT_SELF, "NumberOfTemplates", 2); // Number of templates

    SetLocalString(OBJECT_SELF, "Template1", "50_NW_DOG");

    SetLocalString(OBJECT_SELF, "Template2", "50_NW_CHICKEN");

}

//

// Set special conditions you want this spawn to take place in.v

int SpawnConditions()

{

    //SpeakString("Object Actions", TALKVOLUME_SHOUT);

    int iCondition;

    iCondition = TRUE;

    //iCondition = GetIsDay(); // Replace with your codition(s) or comment out for none.

    return iCondition;

}

//

// Set special actions you wish the regular objects to do after creation below:

void ObjectActions(object oSelectedObject)

{

    //SpeakString("Object Actions", TALKVOLUME_SHOUT);

    //AssignCommand(oSelectedObject, ActionAttack(GetObjectByTag(""))); //InsertTag to Attack

    //AssignCommand(oSelectedObject, ActionForceFollowObject(GetObjectByTag(""))); //Insert Tag to Follow

    //ChangeFaction(oSelectedObject, STANDARD_FACTION_DEFENDER); //Change Faction

}

//

// Function Prototyes, these allow the functions to be called in any order.

void DestroyBadObjects(int iObjectType);

    int CheckObject(object oSelectedObject);

int CheckTime();

int CheckTotalNumber();

int CountDeadObjects(int iObjectType);

    int GetRandomMaxMinNumber();

int CreateObjects(int iObjectType, int iDeadObjects);

    string GetTemplate();

    location GetRandomLocation();



//

// The main functions runs all of the other functions.

void main()

{

    int iDeadObjects;

    int iSpawnedObjects;

    SetPreferences();

    DestroyBadObjects(GetLocalInt(OBJECT_SELF, "ObjectType"));

    if(CheckTime() == TRUE && SpawnConditions() == TRUE && CheckTotalNumber() == TRUE)

        {

        iDeadObjects = CountDeadObjects(GetLocalInt(OBJECT_SELF, "ObjectType"));

        iSpawnedObjects = CreateObjects(GetLocalInt(OBJECT_SELF, "ObjectType"), iDeadObjects);

        }

    SetLocalInt(OBJECT_SELF, "TotalSpawnedObjects", (iSpawnedObjects+GetLocalInt(OBJECT_SELF, "TotalSpawnedObjects")));

    //SpeakString("Total Spawned: "+IntToString(GetLocalInt(OBJECT_SELF, "TotalSpawnedObjects")), TALKVOLUME_SHOUT);

}

//

// The DestroyBadObjects functions destroys objects that shouldn't be.

void DestroyBadObjects(int iObjectType)

{

    //SpeakString("Destroy Bad Objects", TALKVOLUME_SHOUT);

    int iObjectCount;

    string sSpawnFlag;

    object oSelectedObject;

    oSelectedObject = GetNearestObject(iObjectType, OBJECT_SELF, iObjectCount);

    while(GetIsObjectValid(oSelectedObject))

    {

        if(GetIsDead(oSelectedObject) == FALSE)

            {

                if(CheckObject(oSelectedObject) == TRUE && SpawnConditions() == FALSE)

                    DestroyObject(oSelectedObject);

            }

        iObjectCount++;

        oSelectedObject = GetNearestObject(iObjectType, OBJECT_SELF, iObjectCount);

    }

}

//

// The CheckObject function is used to check if the object is from this spawn.

int CheckObject(object oSelectedObject)

{

    //SpeakString("Check Object", TALKVOLUME_SHOUT);

    int iMatchesConditions;

    iMatchesConditions = FALSE;

    if(GetLocalString(oSelectedObject, "SpawnFlag") == GetLocalString(OBJECT_SELF, "FlagName"))

        iMatchesConditions = TRUE;

    return iMatchesConditions;

}

//

// The CheckTotalNumber functions checks the total number of spawns against the maxnumber variable.

int CheckTotalNumber()

{

    //SpeakString("Check Total Number", TALKVOLUME_SHOUT);

    int iUnderTotal;

    iUnderTotal = TRUE;



    if(GetLocalInt(OBJECT_SELF, "MaxNumTotal") == -1)

        return iUnderTotal;

    else if(GetLocalInt(OBJECT_SELF, "TotalSpawnedObjects") != 0)

        {

            if(GetLocalInt(OBJECT_SELF, "MaxNumTotal") <= GetLocalInt(OBJECT_SELF, "TotalSpawnedObjects"))

                iUnderTotal = FALSE;

        }

    else

        SetLocalInt(OBJECT_SELF, "TotalSpawnedObjects", 1);

    return iUnderTotal;

}

//

// The CheckTime function checks to see if the spawn timer is up.

int CheckTime()

{

    //SpeakString("Check Time", TALKVOLUME_SHOUT);

    int iTime;

    int iSpawnTime;

    int iSpawnNow;

    iSpawnNow = FALSE;

    if(GetLocalInt(OBJECT_SELF, "Time") != 0)

    {

        iSpawnTime = GetLocalInt(OBJECT_SELF, "SpawnTime");

        iTime = GetLocalInt(OBJECT_SELF, "Time");

        if(iTime >= iSpawnTime)

        {

            SetLocalInt(OBJECT_SELF, "Time", 6);

            iSpawnNow = TRUE; // Note: I round off and lose the extra to make it consistant.

        }

        else

            SetLocalInt(OBJECT_SELF, "Time", iTime+6);

    }

    else

    {

        SetLocalInt(OBJECT_SELF, "Time", 6);

        iSpawnNow = TRUE;

    }

    return iSpawnNow;

}

//

// The CountDeadObjects function determines how many objects must be created.

int CountDeadObjects(int iObjectType)

{

    //SpeakString("Count Dead Objects", TALKVOLUME_SHOUT);

    int iLivingObjects;

    int iDeadObjects;

    int iObjectCount;

    string sSpawnFlag;

    object oSelectedObject;

    iLivingObjects = 0;

    iObjectCount = 1;

    oSelectedObject = GetNearestObject(iObjectType, OBJECT_SELF, iObjectCount);

    while(GetIsObjectValid(oSelectedObject))

    {

        if(GetIsDead(oSelectedObject) == FALSE)

            {

                if(CheckObject(oSelectedObject) == TRUE)

                    iLivingObjects++;

            }

        iObjectCount++;

        oSelectedObject = GetNearestObject(iObjectType, OBJECT_SELF, iObjectCount);

    }

    iDeadObjects = GetLocalInt(OBJECT_SELF, "MinNumObjects") - iLivingObjects;

    if(iDeadObjects < 0)

        iDeadObjects = 0;

    if(iDeadObjects > 0)

        iDeadObjects = iDeadObjects + GetRandomMaxMinNumber();

    //SpeakString("DeadObjects: "+IntToString(iDeadObjects), TALKVOLUME_SHOUT);

    return iDeadObjects;

}

//

// The GetRandomMaxMinNumber function will get a random number to add to the dead count.

int GetRandomMaxMinNumber()

{

    //SpeakString("Get Random Max/Min Number", TALKVOLUME_SHOUT);

    int iRandom;

    int iMaxMinDifference;

    iMaxMinDifference = GetLocalInt(OBJECT_SELF, "MaxNumObjects") - GetLocalInt(OBJECT_SELF, "MinNumObjects");

    iRandom = Random(iMaxMinDifference+1);

    //SpeakString("RandomMaxMin: "+IntToString(iRandom), TALKVOLUME_SHOUT);

    return iRandom;

}

//

// The CreateObjects function is what spawns the objects.

int CreateObjects(int iObjectType, int iDeadObjects)

{

    //SpeakString("Create Objects", TALKVOLUME_SHOUT);

    int iCounter;

    string sSelectedTemplate;

    object oSelectedObject;

    for(iCounter = 1; iCounter <= iDeadObjects; iCounter++)

    {

        sSelectedTemplate = GetTemplate();

        sSelectedTemplate = GetSubString(sSelectedTemplate, 3, GetStringLength(sSelectedTemplate)-1);

        //SpeakString("Template: "+sSelectedTemplate, TALKVOLUME_SHOUT);

        oSelectedObject = CreateObject(iObjectType, sSelectedTemplate, GetRandomLocation(), TRUE);

        SetLocalString(oSelectedObject, "SpawnFlag", GetLocalString(OBJECT_SELF, "FlagName"));

        ObjectActions(oSelectedObject);

    }



    return iCounter-1;

}

//

// The GetTemplate function randomly determines which template should be used to spawn from.

string GetTemplate()

{

    //SpeakString("Get Template", TALKVOLUME_SHOUT);

    int iCounter;

    int iRandom;

    int iProbability;

    int iLow;

    int iHigh;

    iLow = 1;

    iRandom = Random(99)+1;

    for(iCounter = 1; iCounter <= GetLocalInt(OBJECT_SELF, "NumberOfTemplates"); iCounter++)

    {

        iProbability = StringToInt(GetSubString(GetLocalString(OBJECT_SELF, "Template"+IntToString(iCounter)), 0, 2));

        iHigh = iHigh+iProbability;

        if(iRandom >= iLow && iRandom <= iHigh)

            return GetLocalString(OBJECT_SELF, "Template"+IntToString(iCounter));

        iLow = iHigh+1;

    }

    return "Error";

}

//

// The GetRandomLocation function gets a random location within the spawn area specified.

location GetRandomLocation()

{

    //SpeakString("Get Random Location", TALKVOLUME_SHOUT);

    float fRandomOrientation;

    float fRandomVectorX;

    float fRandomVectorY;

    vector vRandom;

    location lSpawnLocation;

    fRandomOrientation = IntToFloat(Random(360) - 180);

    fRandomVectorX = IntToFloat(Random(GetLocalInt(OBJECT_SELF, "SpawnAreaWidth")*2)-GetLocalInt(OBJECT_SELF, "SpawnAreaWidth"));

    fRandomVectorY = IntToFloat(Random(GetLocalInt(OBJECT_SELF, "SpawnAreaHeight")*2)-GetLocalInt(OBJECT_SELF, "SpawnAreaHeight"));

    vRandom = Vector(fRandomVectorX, fRandomVectorY, 0.0) + GetPositionFromLocation(GetLocation(GetLocalObject(OBJECT_SELF, "Target")));

    lSpawnLocation = Location(GetArea(OBJECT_SELF), vRandom, fRandomOrientation);

    return lSpawnLocation;

}

// Directions:

// Begin by attaching the script to any objects heartbeat.  Then set the

// preferences.  After the preferences have been set, you need to make the

// templates.  The templates tell the script the objects chance of being spawned

// and the blueprint of the object.  You MUST make sure the percentages add up

// to 100.

//

// Preferences:

//    SpawnTime 6:

//      -The spawn time is the time you want to wait between each spawn, it is

//       rounded off to the nearest number divisable by 6 because of the heartbeat.

//    SpawnAreaHeight 10:

//      -The spawn area height sets the distance on the Y axis for the spawn area.

//    SpawnAreaWidth 10:

//      -The spawn area width sets the distance on the X axis for the spawn area.

//    FlagName TestSpawn:

//      -The flag name is used by the script to identify objects spawned by this

//       script. Each script needs a UNIQUE name.

//    MaxNumObjects 5:

//      -This sets the maximum number of objects per spawn, if you dont want a

//       range of objects set both max and min to the same number.

//    MinNumObjects 1:

//      -This sets the minimum number of objects per spawn.

//    MaxTotalObjects -1:

//      -This sets the total number of objects to spawn ever -1 is spawn indefinely.

//    ObjectType OBJECT_TYPE_CREATURE

//      -This sets the type of objects you are spawning.  There can only be ONE

//       type of object per script trying to spawn items and creatures with ONE

//       script will cause problems.

//    Target OBJECT_SELF:

//      -This specifies the target for the spawn, usually you want the spawn to

//       occur around the object with the script attached to it, but sometimes

//       you may want to spawn around an item with a HeartBeat that is being used

//       for something else. Or you may want to say have items and monsters spawn

//       around the same object, so you make two waypoints/objects attach this

//       script and give them both the same target. Note that the target must be

//       in the same area as the objects with the scripts attached.

//

// Templates:

//    NumberOfTemplates 2:

//      -This number tells the script how many templates you have. It MUST match

//       the number of templates below.

//    Template1 50_NW_DOG:

//      -(XX_BLUEPRINT)

//       The first two numbers are the percentage change of spawning this object.

//       The rest of the template is the blueprint of the object.  Templates must

//       be made in consecutive order as in, Template1, Template2, Template3 ect.

//       The percentages of the templates MUST add up to 100. If you only want to

//       spawn one object with a 100 percent chance of spawn you must make 2

//       identical templates because the percentage can only be 2 digits.  There

//       is also a limit of 100 templates.

//

// Object Conditions and Actions:

//    Conditions:

//      -This specifies which conditions you want the script to spawn under.

//       The script will only spawn when these conditions are true and will destroy

//       all objects that it has made when these conditions are no longer true.

//       If you want the spawn to occur always, just comment out the

//       iCondition = FakeConditio(); line (note: // is how to comment).  This

//       condition line can be very powerful.  It makes it possible to only

//       spawn durring the day, when players are near, when a quest is completed,

//       or all three of these combined.  As long as the conditions you check

//       are TRUE and FALSE they can be made to work.

//    Actions:

//      -This allows you to specify a series of actions for the objects to do

//       immidiately after they have been spawned. This also allows for some neat

//       options.  You could have the objects spawned from a head monster and

//       just after spawn tell them to follow or guard the monster.  You could

//       simulate a battle by seting factions and telling them to attack eachother.

//

//      Whatever you do, have fun with it =) If you find a bug or something

//      you think I should add PM it to Enthas or post on biowares scripting forum.



_________________
-Enthas