Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Using Random() {Major Help needed} (https://forums.graalonline.com/forums/showthread.php?t=134258411)

Zeltino 03-16-2010 11:55 PM

Using Random() {Major Help needed}
 
Hey guys,

I'm trying to put together a script that will make an NPC warp from one location to a list of ~100 locations on a server. The locations will be pre-determined. That means that I will have a list of the level, x, and y position that the NPC will warp to. I just need to know how to make the NPC randomly select between these locations, and how it will warp there, etc. It will randomly choose from the specified list of levels every 5 minutes.

I have very little scripting knowledge. I'm just trying to pull this together last-minute (needs to be done by tonight, ew). Any help would be tremendously helpful.

So far, I've started scripting the NPC in a class script (I assumed this would be better than a level NPC).

This is all I have so far:
NPC Code:
function onCreated(){ 
setimg("block.png");
this.locationsarray = {"test.nw","30","30"}, //Random Movement
{"zeltest.nw","40","40"};

scheduleevent("0.05","MoveMe","");
}

function onMoveMe(){
temp.i=int(random(0,99));
this.location=this.locationsarray[temp.i];
scheduleevent("300","MoveMe","");
}

function onPlayertouchsme(){
}


Almost certain none of the above is correct.

Can anyone help me <3?

DrakilorP2P 03-17-2010 12:08 AM

I suggest putting it in a DBNPC since you're in a hurry. Doing so might be kinda ugly but eliminates a range of possible errors.

You can define the array of locations like this:
PHP Code:

this.locations = {
  {
"test.nw"3030},
  {
"foo.nw"1040},
}; 

The array indeces range from 0 to array size - 1, so you'd generate a random index like this:
PHP Code:

temp.index int(random(0this.locations.size())); 

This will work regardless of how many locations you've put into the array.

I think warpto() is what you use for NPCs.
PHP Code:

temp.newLocation this.locations[temp.index];
this.warpto(temp.newLocation[0], temp.newLocation[1], temp.newLocation[2]);
echo(
this.name " warped to " temp.newLocation); 

I think the rest of the code you posted should work.

fowlplay4 03-17-2010 12:14 AM

Edit: Appears I'm a couple minutes late..

For arrays inside arrays (multi-dimensional) you need to wrap { }'s around them like so:

PHP Code:

temp.array = {
  {
"innerarray""member"},
  {
"another""array"}
}; 

You can then use random, and the size of the array to pick a random array out of it.

PHP Code:

function onCreated() {
  
temp.examplearray = {
    {
"a""b""c"},
    {
"d""e""f"}
  };
  
temp.randomindex int(random(0temp.examplearray.size()));
  
temp.example temp.examplearray[temp.randomindex];


int(value) - Rounds the number (technically it converts it to an integer, but this is the result) to the number below it. I.e: int(3.8) = 3

random(float min, float max) - Picks a number between the minimum and maximum number, it will/should never equal the max value.

Therefore int(random(0, temp.array.size())) will return a valid index in the array.

To move NPCs you have to use warpto which accepts the same parameters as setlevel2.

warpto(str levelname, float x, float y);

When we put it altogether we get a nice little function like so:

PHP Code:

function warp() {
  
// Declare Locations Array
  
temp.locations = {
    {
"onlinestartlocal.nw"3030},
    {
"someotherlevel.nw"3030}
  };
  
// Select a location
  
temp.location temp.locations[int(random(0temp.locations.size()))];
  
// Warpto Location
  
warpto(temp.location[0], temp.location[1], temp.location[2]);
  
// Update (Forces NPC to save changes, probably not needed but whatever)
  
this.trigger("NPCUpdate""");



xAndrewx 03-17-2010 08:56 AM

HTML Code:

function warp() {
  // Declare Locations Array
  temp.locations = {
    {"onlinestartlocal.nw", 30, 30},
    {"someotherlevel.nw", 30, 30}
  };

  // Select a location
  temp.location = randomstring(temp.locations);

  // Warpto Location
  warpto(temp.location[0], temp.location[1], temp.location[2]);

  // Update (Forces NPC to save changes, probably not needed but whatever)
  this.trigger("NPCUpdate", "");

I just used randomstring ^^

cbk1994 03-17-2010 01:05 PM

Quote:

Originally Posted by xAndrewx (Post 1563152)
I just used randomstring ^^

randomstring has some weird problems when working with arrays with one object or less. I try to avoid it whenever possible because it just tends to lead to problems in the future.

xAndrewx 03-17-2010 08:43 PM

Oh, such as...?

WhiteDragon 03-17-2010 10:40 PM

Quote:

Originally Posted by xAndrewx (Post 1563231)
Oh, such as...?

I don't know precisely what Chris is referring to, but I have my own issues with it. The way randomstring works is something like this:

Your array of:
PHP Code:

  temp.locations = { 
    {
"onlinestartlocal.nw"3030}, 
    {
"someotherlevel.nw"3030
  }; 

Turns into
PHP Code:

  temp.locations = { 
    
"onlinestartlocal.nw,30,30,"
    
"someotherlevel.nw,30,30," 
  
}; 

Which randomstring then runs on, and returns a single value such as
PHP Code:

temp.value "onlinestartlocal.nw,30,30,"

When you then run temp.value[0] it changes temp.value into
PHP Code:

temp.value = {"onlinestartstartlocal.nw""30""30"}; 

And you continue on with your script.



The issue with this is all the implicit casting between arrays and strings, and it is essentially an abuse of the internal handling of data in GS2.

In the midst of all this complexity, there is a lot of room for error and inefficiency.

cbk1994 03-17-2010 11:08 PM

Quote:

Originally Posted by xAndrewx (Post 1563231)
Oh, such as...?

PHP Code:

temp.arrayExample = {"one"};

for (
temp.010++) {
  echo(
": " randomString(arrayExample));


echoed the following on Era with the latest NPC-server:

Quote:

Originally Posted by RC
0:
1:
2:
3: one
4:
5:
6: one
7: one
8:
9: one


adam 03-18-2010 03:38 AM

Yes, when using an array with one element, it is totally broken. Seems to work with more than one well enough. And I didn't have any trouble with multi-dimensional arrays. But I would avoid it just on the single element array issue.

Zeltino 03-19-2010 09:35 AM

Hey guys, thanks for the help. Got it working :).

xAndrewx 03-19-2010 09:45 AM

Quote:

Originally Posted by cbk1994 (Post 1563257)
PHP Code:

temp.arrayExample = {"one"};

for (
temp.010++) {
  echo(
": " randomString(arrayExample));


echoed the following on Era with the latest NPC-server:

Oh I see what you mean, however it's looking for at least two or more options.

cbk1994 03-19-2010 12:35 PM

Quote:

Originally Posted by xAndrewx (Post 1563536)
Oh I see what you mean, however it's looking for at least two or more options.

Yeah, but in the future it might not be, so I would avoid it just for the sake of making the code require the least maintenance. In the future if it was changed it might result in that glitch if you didn't recheck the code, and it's best to avoid potential glitches like that from the start.


All times are GMT +2. The time now is 11:00 PM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.