Author Topic: Problem with a script that randomly chooses a waypoint to teleport to  (Read 377 times)

Legacy_Grani

  • Hero Member
  • *****
  • Posts: 1040
  • Karma: +0/-0


               Hello again. 
I'll try to explain the problem shortly:
I'm working on "random" regions, that is - each region has 10 large areas with encounters + 3 safe areas ("towns).
At the end of each area is a trigger that randomly chooses the next area (or to be more accurate, the waypoint placed in that area) and stores it as a string variable on the current area and then another trigger teleports the player to this waypoint. There is supposed to be 5% chance of teleporting to a town and 95% chance of teleporting to another hostile area.

Edit: Looks like this script can't be properly displayed on the forum.
Here's a more clear version of it:
http://pastebin.com/ni9ZDczq

Here's the script of trigger that chooses the waypoint:

//Put this script on a trigger's OnEnter tag.

void main(){//This line tells us who used the triggering object and in which areaobject oPC = GetEnteringObject();object oArea = GetArea(oPC);
if (!GetIsPC(oPC)) return;
SetLocalInt(oPC, "ready_to_tp", 1);
//This line randomly generates the point the PC will teleport to.int nWaypointNumber = Random(9);
switch (nWaypointNumber)   {   case 0:      SetLocalString(oArea, "next_area", "random210");      break;   case 1:      SetLocalString(oArea, "next_area", "random21");      break;   case 2:      SetLocalString(oArea, "next_area", "random23");      break;   case 3:      SetLocalString(oArea, "next_area", "random24");      break;   case 4:      SetLocalString(oArea, "next_area", "random25");      break;   case 5:      SetLocalString(oArea, "next_area", "random26");      break;   case 6:      SetLocalString(oArea, "next_area", "random27");      break;   case 7:      SetLocalString(oArea, "next_area", "random28");      break;   case 8:      SetLocalString(oArea, "next_area", "random29");      break;   }//A 5% chance of the next area being a village or an inn instead of wildernessif (d100()<=5)   {   int nTownNumber = Random(3);   switch (nWaypointNumber)    {    case 0:        SetLocalString(oArea, "next_area", "random2t1");        break;    case 1:        SetLocalString(oArea, "next_area", "random2t2");        break;    case 2:        SetLocalString(oArea, "next_area", "random2t3");        break;    }
   }
}

At first when I started testing the system, it seemed to work OK, but then I grew suspicious and to test it further, I modified a script so that it would always set the waypoint to one of three towns and not to any hostile area.
Or at least that's how it should be.

I just modified the line:
if (d100()<=5)
to
if (d100()<=100)

Then I proceeded to test it.
First try: OK, PC teleported to "random2t2"
Second try: Same story.
Third try: "random2t3".
Fourth try: tried to teleport me to "random2t1" but then the game crashed at the end of the loading, no error message or anything, the game just shut down. Ever since then, when I repeated my tests, I would get teleported to town 2 and town 3 properly, but I would never teleport to the first town, the one that caused the game to crash earlier. Instead, I teleported sometimes to one of the hostile areas, even though the modified script should always choose a town waypoint.
It looks as if the waypoint of the first town was suddenly considered an invalid location - that would explain why the script sent me to hostile areas at times. However, the waypoint does exist, the area loads without a problem in the toolset and what's more, I could teleport there before I decided to indulge in more testing. ':bandit:'

I'm not sure what's causing this problem. I also don't know if I should post it in the "Scripting" forum, so sorry if it's not related to my script at all.

Any help is welcome.
               
               

               


                     Modifié par Grani, 30 septembre 2013 - 12:00 .
                     
                  


            

Legacy_Squatting Monk

  • Hero Member
  • *****
  • Posts: 776
  • Karma: +0/-0
Problem with a script that randomly chooses a waypoint to teleport to
« Reply #1 on: September 30, 2013, 01:12:59 am »


               Where is the code that does the actual transportation?
               
               

               
            

Legacy_Grani

  • Hero Member
  • *****
  • Posts: 1040
  • Karma: +0/-0
Problem with a script that randomly chooses a waypoint to teleport to
« Reply #2 on: September 30, 2013, 10:40:07 am »


               

//Teleports players standing near the area exit to the next random area
void main()
{

object oExiter = GetClickingObject();
object oArea = GetArea(oExiter);
object oPC = GetFirstPC();

if (!GetIsPC(oExiter)) return;

while (GetIsObjectValid(oPC) == TRUE)
    {
        object oLocation = GetArea(oPC);

        if (oLocation==oArea)
            {
                if (GetLocalInt(oPC, "ready_to_tp")== 1)
                    {
                        object oTarget = GetWaypointByTag(GetLocalString(oArea, "next_area"));

                        location lTarget = GetLocation(oTarget);

                        if (GetAreaFromLocation(lTarget)==OBJECT_INVALID) return;

                        AssignCommand(oPC, ClearAllActions());

                        AssignCommand(oPC, ActionJumpToLocation(lTarget));

                    }
            }

        oPC = GetNextPC();
    }

}

I don't think it is the cause, but here, that's the code.
Thanks.
               
               

               
            

Legacy_GhostOfGod

  • Hero Member
  • *****
  • Posts: 1490
  • Karma: +0/-0
Problem with a script that randomly chooses a waypoint to teleport to
« Reply #3 on: September 30, 2013, 04:55:06 pm »


               {//This line tells us who used the triggering object and in which area
    object oPC = GetEnteringObject();
    object oArea = GetArea(oPC);
    if (!GetIsPC(oPC)) return;
    SetLocalInt(oPC, "ready_to_tp", 1);
    //This line randomly generates the point the PC will teleport to.
    int nWaypointNumber = Random(9);
    switch (nWaypointNumber)
    {
        case 0: SetLocalString(oArea, "next_area", "random210");    break;
        case 1: SetLocalString(oArea, "next_area", "random21");     break;
        case 2: SetLocalString(oArea, "next_area", "random23");     break;
        case 3: SetLocalString(oArea, "next_area", "random24");     break;
        case 4: SetLocalString(oArea, "next_area", "random25");     break;
        case 5: SetLocalString(oArea, "next_area", "random26");     break;
        case 6: SetLocalString(oArea, "next_area", "random27");     break;
        case 7: SetLocalString(oArea, "next_area", "random28");     break;
        case 8: SetLocalString(oArea, "next_area", "random29");     break;
    }//A 5% chance of the next area being a village or an inn instead of wilderness
    if (d100()<=5)
    {
        int nTownNumber = Random(3);
        switch (nWaypointNumber)
        {
            case 0: SetLocalString(oArea, "next_area", "random2t1");    break;
            case 1: SetLocalString(oArea, "next_area", "random2t2");    break;
            case 2: SetLocalString(oArea, "next_area", "random2t3");    break;
        }
    }
}


I think the problem is that the second switch there is still Random(9). Should be switch (nTownNumber)?
               
               

               
            

Legacy_Grani

  • Hero Member
  • *****
  • Posts: 1040
  • Karma: +0/-0
Problem with a script that randomly chooses a waypoint to teleport to
« Reply #4 on: September 30, 2013, 06:53:41 pm »


               Oh no, don't tell me such a silly mistake on my part is the reason. '<img'>
Well, I'll sure feel stupid if it turns out to be the case.

Going to check it and report the outcome in a moment.
               
               

               
            

Legacy_Grani

  • Hero Member
  • *****
  • Posts: 1040
  • Karma: +0/-0
Problem with a script that randomly chooses a waypoint to teleport to
« Reply #5 on: September 30, 2013, 07:26:18 pm »


               Sigh, looks like this was exactly the problem. At least so it seems after testing it like 10 or 12 times.

A silly mistake. Thank you very much. ^^