Jump to content
  • 0

Problem with loop and random option.


InfectedX

Question


  • Group:  Members
  • Topic Count:  9
  • Topics Per Day:  0.00
  • Content Count:  65
  • Reputation:   1
  • Joined:  01/12/12
  • Last Seen:  

 Hello, i'm making a script but i'm having problems with the rand command

This is the error i have:
 

[Error]: buildin_rand: range (0) is too small. No randomness possible.
[Warning]: Script command 'rand' returned failure.
[Debug]: Source (NPC): test#thts at 1@thts (156,371)
[Debug]: Source (NPC): test#thts is located in: INSTANCING

And this is my script... 

	setarray 'floors[0],3,4,5,6;
	setarray 'maps$[0],instance_mapname("3@thts"),instance_mapname("4@thts"),instance_mapname("5@thts"),instance_mapname("6@thts");
	setarray 'xs[0],27,18,155,50;
	setarray 'ys[0],44,97,100,18;
	.@loop = getarraysize('floors);
	for (.@i = 1; .@i <= .@loop ; .@i++) {
		.@r = rand(getarraysize('floors));
		'order[.@i] = 'floors[.@r];
		deletearray 'floors[.@r],1;
		'wm$[.@i] = 'maps$[.@r];
		deletearray 'maps$[.@r],1;
		'wx[.@i] = 'xs[.@r];
		deletearray 'xs[.@r],1;
		'wy[.@i] = 'ys[.@r];
		deletearray 'ys[.@r],1;
	}



The purpose of the code is to randomly shuffle and assign properties (map name, X-coordinate, and Y-coordinate) to a set of floors in a tower instance i'm making. Here's a breakdown of the code:

1. Initialize arrays for floors, map names, X-coordinates, and Y-coordinates.
2. Determine the number of floors in the tower.
3. Use a loop to randomly select a floor from the remaining options and assign its properties (map name, X-coordinate, and Y-coordinate).
4. Remove the selected floor and its associated properties from the available options to avoid repetition.

Possible reasons for issues in the code:
- The algorithm relies on the 'rand' function to generate random numbers.
- Confirm that the arrays are properly initialized and that the indices match between arrays.

Could anyone here help me? what i'm doing wrong? xD

Link to comment
Share on other sites

8 answers to this question

Recommended Posts

  • 0

  • Group:  Members
  • Topic Count:  16
  • Topics Per Day:  0.00
  • Content Count:  662
  • Reputation:   671
  • Joined:  11/12/12
  • Last Seen:  

There is no issue with the code sample you've provided. It's a bit hard to read, but besides that, it works fine for me. To make your life easier though, may I suggest using a command like shufflearray instead? Like so:

BUILDIN_DEF(shufflearray,"r?"),

BUILDIN_FUNC(shufflearray)
{
	map_session_data* sd = NULL;
	int i, j;
	int array_size = script_hasdata(st, 3) ? script_getnum(st, 3) : -1;

	struct script_data* data = script_getdata(st, 2);

	if (!data_isreference(data)) {
		ShowError("buildin_shufflearray: not a variable\n");
		script_reportdata(data);
		script_pushnil(st);
		st->state = END;
		return SCRIPT_CMD_FAILURE;
	}

	const char* name = reference_getname(data);
	int32 id = reference_getid(data);

	if (not_server_variable(*name)) {
		sd = map_id2sd(st->rid);

		if (sd == NULL)
			return SCRIPT_CMD_SUCCESS;
	}

	if (array_size < 0) {
		array_size = script_array_highest_key(st, sd, reference_getname(data), reference_getref(data));
	}

	// Start shuffling the array
	if (is_string_variable(name)) {
		for (i = array_size - 1; i > 0; i--) {
			j = rnd() % (i + 1);

			const char* temp_val = get_val2_str(st, reference_uid(id, i), reference_getref(data));
			set_reg_str(st, sd, reference_uid(id, i), name, get_val2_str(st, reference_uid(id, j), reference_getref(data)), reference_getref(data));
			set_reg_str(st, sd, reference_uid(id, j), name, temp_val, reference_getref(data));
			script_removetop(st, -1, 0);
			script_removetop(st, -1, 0);
		}
	}
	else {
		for (i = array_size - 1; i > 0; i--) {
			j = rnd() % (i + 1);

			int64 temp_val = get_val2_num(st, reference_uid(id, i), reference_getref(data));
			set_reg_num(st, sd, reference_uid(id, i), name, get_val2_num(st, reference_uid(id, j), reference_getref(data)), reference_getref(data));
			set_reg_num(st, sd, reference_uid(id, j), name, temp_val, reference_getref(data));
			script_removetop(st, -1, 0);
			script_removetop(st, -1, 0);
		}
	}

	return SCRIPT_CMD_SUCCESS;
}

Then you can simply code it like this:

1@thts,0,0,0	script	#thanatos_main	-1,{
OnInit:
	setarray .@rng_floors, 3, 4, 5, 6;
	shufflearray .@rng_floors;
	setarray 'floors, 1, 2, .@rng_floors[0], .@rng_floors[1], .@rng_floors[2], .@rng_floors[3], 7, 8;
	setarray 'maps$,
		instance_mapname("1@thts"),
		instance_mapname("2@thts"),
		instance_mapname("3@thts"),
		instance_mapname("4@thts"),
		instance_mapname("5@thts"),
		instance_mapname("6@thts"),
		instance_mapname("7@thts"),
		instance_mapname("8@thts");
	
	setarray 'coords_coords,
		0, 0,
		0, 0,
		27, 44,	// 3rd floor
		18, 97,
		155, 100,
		50, 18,
		50, 18,
		50, 18;
	
	
	// other stuff here...
	end;
}

1@thts,32,166,0	script	#setp02	45,2,2,{
	end;
OnTouch:
	.@next_floor = 3;
	warp 'maps$[.@next_floor], 'coords_coords[2 * .@next_floor + 0], 'coords_coords[2 * .@next_floor + 1];
	end;
}

As you're clearly implementing Thanatos Tower, you might find some inspiration from when I wrote the script (though it uses quite a few custom script functions and it also has a custom hard mode, but it can probably still be of use). It's from kRO replay files, so the NPC coordinates should be very accurate.

 

thanatos.txt

Edited by Tokei
Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  9
  • Topics Per Day:  0.00
  • Content Count:  65
  • Reputation:   1
  • Joined:  01/12/12
  • Last Seen:  

Wow, i'll try the first option, Thank you...

Yeah, it's a bit hard to read 😅 but anyway, it gave me that error above... idk why, I just tried to script it like in kind of JS style lol.

maybe rathena didin't recognize the syntaxis...

I'll keep you updated in any case...

Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  9
  • Topics Per Day:  0.00
  • Content Count:  65
  • Reputation:   1
  • Joined:  01/12/12
  • Last Seen:  

6 hours ago, Tokei said:

 

As you're clearly implementing Thanatos Tower, you might find some inspiration from when I wrote the script (though it uses quite a few custom script functions and it also has a custom hard mode, but it can probably still be of use). It's from kRO replay files, so the NPC coordinates should be very accurate.

 

thanatos.txt 109.37 kB · 2 downloads

This script is not working for me i tried to check what's different and yes, it is very different XD

Has a lot of functions i don't have, but it could help... thx a lot... I still no success XD but i will keep trying... if i ever find a solution, i'll share...

PS: Sorry!!! i think i click on edit, it wasn't my intention to make double post!

Edited by InfectedX
Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  16
  • Topics Per Day:  0.00
  • Content Count:  662
  • Reputation:   671
  • Joined:  11/12/12
  • Last Seen:  

9 hours ago, InfectedX said:

This script is not working for me i tried to check what's different and yes, it is very different XD

Has a lot of functions i don't have, but it could help... thx a lot... I still no success XD but i will keep trying... if i ever find a solution, i'll share...

PS: Sorry!!! i think i click on edit, it wasn't my intention to make double post!

Ah no, the script I posted isn't meant to just copy paste and run. It's just if you have doubts with a mechanic or something; it's way too custom to be used and I'm too lazy to convert it to rAthena's standards.

Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  9
  • Topics Per Day:  0.00
  • Content Count:  65
  • Reputation:   1
  • Joined:  01/12/12
  • Last Seen:  

10 hours ago, Tokei said:

Ah no, the script I posted isn't meant to just copy paste and run. It's just if you have doubts with a mechanic or something; it's way too custom to be used and I'm too lazy to convert it to rAthena's standards.

right, thx 🙂 it's helpful anyway

 

Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  23
  • Topics Per Day:  0.04
  • Content Count:  57
  • Reputation:   4
  • Joined:  07/10/22
  • Last Seen:  

On 2/9/2024 at 12:58 AM, InfectedX said:

 Hello, i'm making a script but i'm having problems with the rand command

This is the error i have:
 

[Error]: buildin_rand: range (0) is too small. No randomness possible.
[Warning]: Script command 'rand' returned failure.
[Debug]: Source (NPC): test#thts at 1@thts (156,371)
[Debug]: Source (NPC): test#thts is located in: INSTANCING

And this is my script... 

	setarray 'floors[0],3,4,5,6;
	setarray 'maps$[0],instance_mapname("3@thts"),instance_mapname("4@thts"),instance_mapname("5@thts"),instance_mapname("6@thts");
	setarray 'xs[0],27,18,155,50;
	setarray 'ys[0],44,97,100,18;
	.@loop = getarraysize('floors);
	for (.@i = 1; .@i <= .@loop ; .@i++) {
		.@r = rand(getarraysize('floors));
		'order[.@i] = 'floors[.@r];
		deletearray 'floors[.@r],1;
		'wm$[.@i] = 'maps$[.@r];
		deletearray 'maps$[.@r],1;
		'wx[.@i] = 'xs[.@r];
		deletearray 'xs[.@r],1;
		'wy[.@i] = 'ys[.@r];
		deletearray 'ys[.@r],1;
	}



The purpose of the code is to randomly shuffle and assign properties (map name, X-coordinate, and Y-coordinate) to a set of floors in a tower instance i'm making. Here's a breakdown of the code:

1. Initialize arrays for floors, map names, X-coordinates, and Y-coordinates.
2. Determine the number of floors in the tower.
3. Use a loop to randomly select a floor from the remaining options and assign its properties (map name, X-coordinate, and Y-coordinate).
4. Remove the selected floor and its associated properties from the available options to avoid repetition.

Possible reasons for issues in the code:
- The algorithm relies on the 'rand' function to generate random numbers.
- Confirm that the arrays are properly initialized and that the indices match between arrays.

Could anyone here help me? what i'm doing wrong? xD

Error me too same like this

I think because after update

https://github.com/rathena/rathena/commit/7c153416bc2b69bbb515c0a3e7a90bcb216d5e4a

Script error after update

.@npc_size = rand(0, 0);

i'm not sure just i think.

 

 

Screenshot 2024-02-10 154527.png

Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  16
  • Topics Per Day:  0.00
  • Content Count:  662
  • Reputation:   671
  • Joined:  11/12/12
  • Last Seen:  

10 hours ago, Outlook said:

Error me too same like this

I think because after update

https://github.com/rathena/rathena/commit/7c153416bc2b69bbb515c0a3e7a90bcb216d5e4a

Script error after update

.@npc_size = rand(0, 0);

i'm not sure just i think.

That's a different issue though; rand(0, 0) doesn't make sense. You could edit the BUILDIN function to ignore such a case, since one could argue that rand(0, 0) should return 0. In the original script posted by InfectedX, it was impossible to have such a scenario so the error didn't make sense. That's why I suggested him to use an entirely different method instead.

Link to comment
Share on other sites

  • 0

  • Group:  Members
  • Topic Count:  9
  • Topics Per Day:  0.00
  • Content Count:  65
  • Reputation:   1
  • Joined:  01/12/12
  • Last Seen:  

I see... Well as you said, I used that script to make a Thanatos tower revamped xD but if 2 of us are having this error... Then it's a bit hard to know why... Because logically is correct but idk why Rathena didn't get it... Anyway, I have 1 week working on this code... I hope I'll fix it soon 

Edited by InfectedX
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...