Author Topic: Rock Spawning Script  (Read 664 times)

Legacy_Li'l Rose

  • Jr. Member
  • **
  • Posts: 52
  • Karma: +0/-0
Rock Spawning Script
« on: February 16, 2016, 05:16:27 pm »


               

 I'm trying to make an on enter script that spawns rocks at certain waypoints. There are several of the waypoints in the area, and I would like the script to check each waypoint, and have a 5% chance of spawning a rock. However the script is not working as it should, and not spawning any rocks, even if I set the percentage to 99%.


 


Here is the script. Any help would be appreciated.



void main()
{
    object oRock;

    // Get the creature who triggered this event.
    object oPC = GetEnteringObject();

    // Only fire for (real) PCs.
    if ( !GetIsPC(oPC)  ||  GetIsDMPossessed(oPC) )
        return;

    // Only fire once.
    if ( GetLocalInt(OBJECT_SELF, "RoseSpawnSilver") == 1 )
        return;

    SetLocalInt(OBJECT_SELF, "RoseSpawnSilver", 1);

    string sAreaTag = GetTag(GetArea(oPC));

    if (sAreaTag == "McGrukkMines1A")
    {
        // If the PC's total level is at least 10.
        if ( GetHitDice(oPC) >= 10 )
        {
            oRock == GetFirstObjectInArea(OBJECT_SELF);
            {
                if ( (GetIsObjectValid(oRock)) && (GetObjectType(oRock) == OBJECT_TYPE_WAYPOINT) )
                {
                    if ( GetTag(oRock) == "WP_ROSE_ROCK" )
                    {
                        // If success on a 5% chance.
                        if ( Random(100) < 5 )
                        {
                            CreateObject(OBJECT_TYPE_PLACEABLE, "cnrrocksilv", GetLocation(oRock));
                        }
                    }
                }
                oRock == GetNextObjectInArea(OBJECT_SELF);
            }
        }
    }
}


               
               

               
            

Legacy_henesua

  • Hero Member
  • *****
  • Posts: 6519
  • Karma: +0/-0
Rock Spawning Script
« Reply #1 on: February 16, 2016, 05:55:46 pm »


               

I put this together for you. Haven't tested it, but it should work. My assumption is that this is an Area OnEnter event script, but it should work for triggers too.



void main()
{
    // Get the creature who triggered this event.
    object oPC = GetEnteringObject();

    // EXCLUSIONS
    // Only execute for (real) PCs.
    if ( !GetIsPC(oPC)  ||  GetIsDM(oPC) )
        return;
    // Only execute once.
    if (GetLocalInt(OBJECT_SELF, "RoseSpawnSilver"))
        return;

    SetLocalInt(OBJECT_SELF, "RoseSpawnSilver", TRUE); // this won't happen again

    // CONFIGURATION
    int percentage_chance_to_appear     = 5; // a number between 0 and 100
    int required_pc_level               = 10;
    string resref_of_object_created     = "cnrrocksilv";
    string tag_of_waypoint              = "WP_ROSE_ROCK";
    int debug_mode_active               = FALSE; // change to TRUE to get better feedback while testing

    // LOOP OVER EVERY WAYPOINT IN THE AREA
    object this_area    = GetArea(OBJECT_SELF);
    int nNth    = 0;
    object oWP  = GetObjectByTag(tag_of_waypoint, nNth);
    object oPLC;
    while(GetIsObjectValid(oWP))
    {
        // determine if we should create this object
        if(     GetArea(oWP)==this_area // must be same area as this one
            &&  GetHitDice(oPC) >= required_pc_level
            &&  Random(100)+1 <= percentage_chance_to_appear
          )
        {
            oPLC = CreateObject(OBJECT_TYPE_PLACEABLE, resref_of_object_created, GetLocation(oWP));
            // Comment out the following debug line when this is ready to go live
            if(debug_mode_active){SendMessageToPC(GetFirstPC(),"Created "+GetName(oPLC)+" ("+ObjectToString(oPLC)+")");}
        }
        else
        {
            // Comment out the following debug line when this is ready to go live
            if(debug_mode_active){SendMessageToPC(GetFirstPC(),"Waypoint Found "+GetName(oWP)+" but object not created.");}
        }

        oWP  = GetObjectByTag(tag_of_waypoint, ++nNth);
    }
}


               
               

               
            

Legacy_Li'l Rose

  • Jr. Member
  • **
  • Posts: 52
  • Karma: +0/-0
Rock Spawning Script
« Reply #2 on: February 16, 2016, 11:39:44 pm »


               

I will try that, thank you very much.



               
               

               
            

Legacy_Li'l Rose

  • Jr. Member
  • **
  • Posts: 52
  • Karma: +0/-0
Rock Spawning Script
« Reply #3 on: February 17, 2016, 12:09:36 am »


               

I just tried it, and it worked great. Thank you.  ':wub:'



               
               

               
            

Legacy_Failed.Bard

  • Hero Member
  • *****
  • Posts: 1409
  • Karma: +0/-0
Rock Spawning Script
« Reply #4 on: February 17, 2016, 12:43:27 am »


               

oRock == GetFirstObjectInArea(OBJECT_SELF);


oRock == GetNextObjectInArea(OBJECT_SELF);


 


You used == instead of = in your script, which is likely why it wasn't working.  == is comparative, = is for assigning values.



               
               

               
            

Legacy_Li'l Rose

  • Jr. Member
  • **
  • Posts: 52
  • Karma: +0/-0
Rock Spawning Script
« Reply #5 on: February 17, 2016, 02:35:31 am »


               

Thanks, I always wondered why sometimes I needed =, and other times I needed  ==.



               
               

               
            

Legacy_Li'l Rose

  • Jr. Member
  • **
  • Posts: 52
  • Karma: +0/-0
Rock Spawning Script
« Reply #6 on: February 17, 2016, 12:14:41 pm »


               

Well, I tried the script with these changes. With the percentage at 99%, it spawn one rock, and then it stop running.


 


Hmmm, the quote not show. Well, I tried it like Failed.Bard suggested.



               
               

               
            

Legacy_henesua

  • Hero Member
  • *****
  • Posts: 6519
  • Karma: +0/-0
Rock Spawning Script
« Reply #7 on: February 17, 2016, 01:48:45 pm »


               

the script I gave you, only runs once per "server" load which appears to be what you want. not clear on what you are saying here.



               
               

               
            

Legacy_Li'l Rose

  • Jr. Member
  • **
  • Posts: 52
  • Karma: +0/-0
Rock Spawning Script
« Reply #8 on: February 17, 2016, 02:42:15 pm »


               

Yes, your script worked very well, and I thank you for that. However I am wanting the same script to work in several floors of the silver mine, but with different percentages depending on the area and character level. What I am wanting is that as players go deeper into the mine, the better chance of rocks spawning. Which is why I had the area check in the script. Is it possible to do this with one script, or should I just put a different script in each area?



               
               

               
            

Legacy_meaglyn

  • Hero Member
  • *****
  • Posts: 1451
  • Karma: +0/-0
Rock Spawning Script
« Reply #9 on: February 17, 2016, 03:53:14 pm »


               

Yes, you can do it in one script...


 


The simple change is to add the tag of the area to the variable that is used to make it only happen once. But if you want different values for the different areas you either need to initialize those values differently for each area (again probably based on the area tag) or use variables on the areas to provide the required settings.



               
               

               
            

Legacy_henesua

  • Hero Member
  • *****
  • Posts: 6519
  • Karma: +0/-0
Rock Spawning Script
« Reply #10 on: February 17, 2016, 07:51:28 pm »


               

all of the configurable stuff can be fed from local variables set on the area if you make the following change:



// CONFIGURATION
object this_area    = GetArea(OBJECT_SELF);  // move this up here so that we can reference this_area for local variables
int percentage_chance_to_appear = GetLocalInt(this_area,"ROCK_percentage_chance_to_appear");
if(!percentage_chance_to_appear){percentage_chance_to_appear=5;}
int required_pc_level= GetLocalInt(this_area,"ROCK_required_pc_level");
if(!required_pc_level){required_pc_level=10;}
string resref_of_object_created = GetLocalString(this_area,"ROCK_resref_of_object_created");
if(resref_of_object_created==""){resref_of_object_created="cnrrocksilv";}
string tag_of_waypoint = GetLocalString(this_area,"ROCK_tag_of_waypoint");
if(tag_of_waypoint==""){tag_of_waypoint="WP_ROSE_ROCK";}
int debug_mode_active = GetLocalInt(this_area,"ROCK_debug_mode_active");
if(debug_mode_active!=1){debug_mode_active=FALSE;}

// LOOP OVER EVERY WAYPOINT IN THE AREA
int nNth = 0;
object oWP = GetObjectByTag(tag_of_waypoint, nNth);
object oPLC;
while(GetIsObjectValid(oWP))

This means that you can set the following on each area with this script to change the details of the rock spawn:


  • ROCK_percentage_chance_to_appear   --   int   --   value from 1 to 100  (for no chance set a value of -1). If unset, default is 5

  • ROCK_required_pc_level    --    int    --    value of 1 or greater. If unset, default is 10.

  • ROCK_resref_of_object_created    --    string    --    resref of your rock placeable. If unset, default is "cnrrocksilv"

  • ROCK_tag_of_waypoint    --    string    --    tag of the waypoints in this area. If unset, default is "WP_ROSE_ROCK"

  • ROCK_debug_mode_active    --    int    --    set to 1 if you want to see debug strings. all other values will turn off debug


               
               

               
            

Legacy_Li'l Rose

  • Jr. Member
  • **
  • Posts: 52
  • Karma: +0/-0
Rock Spawning Script
« Reply #11 on: February 18, 2016, 12:09:13 am »


               

Thank you very much.  '<img'>