Skip to content

Wandering Monsters

ZephiriusZephirius Member Posts: 411
edited August 2022 in Builders - Scripting
I've been toying with a random monster spawner -
I'm looking for suggestions on how to improve it... I'm sure You guys will let me have it... lol :)
I know there's plenty of systems out there, but I kind of wanted to try doing it myself, I might be in over my head... not sure.

For OnHeartbeat
Code so far...
void SpawnWanderingMonster(string sResRef, location lLoc)
{
    CreateObject(OBJECT_TYPE_CREATURE, "", lLoc);
}
void main()
{

    object oPC = GetNearestCreature(CREATURE_TYPE_PLAYER_CHAR, PLAYER_CHAR_IS_PC);
    if (!GetIsPC(oPC)) return;

    location oWay1 = GetLocation(GetWaypointByTag("WP_01"));
    location oWay2 = GetLocation(GetWaypointByTag("WP_02"));
    location oWay3 = GetLocation(GetWaypointByTag("WP_03"));
    location oWay4 = GetLocation(GetWaypointByTag("WP_04"));
    location oWay5 = GetLocation(GetWaypointByTag("WP_05"));
    location oWay6 = GetLocation(GetWaypointByTag("WP_06"));
    location oWay7 = GetLocation(GetWaypointByTag("WP_07"));

    if (d100() > 90 )
    {
        switch (Random(7))
        {
            SpawnWanderingMonster("", oWay1);
            // Lots more monster stuff here
        }
    }
}

Comments

  • TerrorbleTerrorble Member Posts: 169
    Day and night checks. Don't let the vampire rogues come out at high noon.

    If it's a predatory beast it may be attracted to corpses and spawn near them assuming nothing else is too close.

    I see the code picks a random waypoint and creates the monster, but doesn't tell it to go anywhere yet. I suppose things like random wandering, spawning in with stealth, moving/stalking to a PC's location, etc might be handled within the creature's events and not this system.
  • ZephiriusZephirius Member Posts: 411
    Thanks for the comments Terrorble. Will look into creature events. I'll have to read up on event scripting .
  • TerrorbleTerrorble Member Posts: 169
    Here are a couple examples to (potentially uneccessarily - becauase that's what I do :p ) elaborate/clarify what I meant by some aspects being handled outside the system.

    I've spent so long in my own module I can't tell you what is original or default behavior and what isn't, but the OnSpawn for creatures I think has a flag for spawning in stealth. If the creature should always do that, then that flag/variable would be set within the creature's properties.

    The spawn system could set a variable (e.g. STALKER) which the creature's heartbeat script could contain the behavior for. Like moving to their location to try for a sneak attack.

  • NeverwinterWightsNeverwinterWights Member Posts: 339
    edited August 2022
    A couple suggestions so far (I added commenting to your script to explain):
    void SpawnWanderingMonster(string sResRef, location lLoc)
    {
        CreateObject(OBJECT_TYPE_CREATURE, sResRef, lLoc);
    }
    void main()
    {
    
        object oPC = GetNearestCreature(CREATURE_TYPE_PLAYER_CHAR, PLAYER_CHAR_IS_PC);
        if (!GetIsPC(oPC)) return;
    
        //You can eliminate a whole list of waypoints if you have a specific naming convention
        /*location oWay1 = GetLocation(GetWaypointByTag("WP_01"));
        location oWay2 = GetLocation(GetWaypointByTag("WP_02"));
        location oWay3 = GetLocation(GetWaypointByTag("WP_03"));
        location oWay4 = GetLocation(GetWaypointByTag("WP_04"));
        location oWay5 = GetLocation(GetWaypointByTag("WP_05"));
        location oWay6 = GetLocation(GetWaypointByTag("WP_06"));
        location oWay7 = GetLocation(GetWaypointByTag("WP_07"));*/
    
        if (d100() > 90 )
        {
            //this doesnt need to be a switch
            int iRandom = Random(7)+1;
    
            //Also recommend changing "oWay" to "lWay" since it is supposed to be a location
            //and not an object. As far as naming convention goes it just makes it easier to read
            //for other people looking at your script and yourself looking at it later
    
            //Here you can just grab the waypoint you need without making a list if the waypoint
            //naming convention is consistant. In this case if you end up with more than 9
            //waypoints you would have to continue naming them "WP_010", "WP_011", etc.
            location lWay = GetLocation(GetWaypointByTag("WP_0"+IntToString(iRandom)));
            SpawnWanderingMonster("", lWay);
            // Lots more monster stuff here
        }
    }
    

    Hope it's helpful.
    Post edited by NeverwinterWights on
  • ZephiriusZephirius Member Posts: 411
    Cool. Gonna try this out. Thanks for the help! B)
  • 4BOLTMAIN4BOLTMAIN Member Posts: 90
    edited August 2022
    When I do random waypoints I do this...

    EDIT:
    Nvm... NeverwinterWights covered this already with the Random(7) explanation.

    Don't forget that Random(7) returns values from 0 - 6 and not 1 - 7.
  • NeverwinterWightsNeverwinterWights Member Posts: 339
    4BOLTMAIN wrote: »
    When I do random waypoints I do this...

    EDIT:
    Nvm... NeverwinterWights covered this already with the Random(7) explanation.

    Don't forget that Random(7) returns values from 0 - 6 and not 1 - 7.

    Good catch. Fixed it.
Sign In or Register to comment.