Tutorials

 Closed TopicStart new topic

[TUT]Randomization

Deji
post Jun 7 2009, 01:14 AM
Post #1


Coding like a Rockstar!

Group Icon

Posts: 1,468
From: ???
Joined: 28-May 09



Randomization Tutorial


Here's a lil tutorial on ways to make something random.

There are 3 ways to do this. The first way is to use a jump table like this...

0871: init_jump_table $VIDEO_GAME total_jumps 8 default_jump 0 @MAIN_5502 jumps 0 @MAIN_5335 1 @MAIN_5353 2 @MAIN_5371 3 @MAIN_5389 4 @MAIN_5407 5 @MAIN_5425 6 @MAIN_5443

0872: jump_table_jumps 7 @MAIN_5461 -1 @MAIN_5502 -1 @MAIN_5502 -1 @MAIN_5502 -1 @MAIN_5502 -1 @MAIN_5502 -1 @MAIN_5502 -1 @MAIN_5502 -1 @MAIN_5502

I'm not familliar with this and could use some confirmations or extra info from someone who knows more about it but I think it would work in a pretty simple way.

The global variable; $VIDEO_GAME would have a pre-defined value. If the value was 2, then it'd jump to :XX_XX - This is just my idea. It's probably totally wrong so ignore it.


The second way is my favourite. All you have to do is use this opcode...

0209: 17@ = random_int_in_ranges 0 10

The variable is where the random number is stored and the random number is between the 1st number and the second number.

So, let's break the parameters down and explain a bit more...

17@ is the variable to store the number.
0 is the first integer (number). This is the lowest possible answer for the random number to be.
10 is the second int (number). This is the highest possible answer for the random number to be.


Now for an example:

0209: 5@ = random_int_in_ranges 150 165

Here the random number could be anything between 150 and 165.

The number could be... 150,151,152,153,154 ect. all the way to 164, 165...

Any one of those numbers will be stored in a variable which means it'll be remembered by the game via that chunk of space... Best way I can describe it, really...

So, 5@ is our chunk of space (variable). That is where our number is stored.

We can ask the game what the number is in the same way that you usually might with 0039. So lets see a larger example of using the random_int.

:MYTHREAD
wait 1000
0209: 5@ = random_int_in_ranges 150 165
if
0039: 5@ == 154
else_jump @MYTHREAD
jump @MYTHREAD1

:MYTHREAD1
02A3: enable_widescreen 1
jump @MYTHREAD

Here we have a random number being created in a loop running every second meaning that we get 1 random number per sec. Once the random number is 154, widescreen mode is activated. If not, a random number will be made again after one second until it is 154.

Eventually, the widescreen will be activated. It could take a second, it could take a year (unlikely). It all depends on what random number is selected. Of course, there are more useful ways to use that opcode than what the example shows... You could use it for thread jumping or even simply getting random numbers to use. Imagine you wanted a level where the player had to press the number that appears on screen as fast as possible... You could define the numbers one by one... But then the player could memorize them making it easy work. With the beauty of the random int opcode, you can make it so it's a different number each time.


The third way is a little restricted. You can only use it for one type of thing. This way is very simillar to the second way.

Here it is...

0208: 90@ = random_float_from -1620.9921 _to_ -1642.4629

This is just like the random number/integer except with a float which means a decimal number.

To me, this just screams out "Coordinates!". It's perfect for randomization of locations. If you've been to ammunation on the shooting range, you'll see how on the third round... The target is created anywhere between the range. This is a great way to do just that!

I used it myself for a shooting range code in my Vigilante Justice Mod. It was great!

Here's a little sample (slightly edited) of my use of the code in Vigilante Justice...

0208: 1@ = random_float_from -1620.9921 _to_ -1642.4629
0107: 0@ = create_object #MYOBJECT at 1@ 648.2003 -5.2422

Here it gets a random float (decimal number) from -1620.9921 to -1642.4629. These coords are each side of one of the walls at SFPD underground. So, the object is created at the exact Y and Z axis. But the X axis has been randomized, so it'll spawn anywhere from the left to the right smile.gif

Again, I'll simplify that.

1@ is the result to the random float that is created.

The floats you should know by reading the second way.

0@ is the object. This is not relevant to this tutorial, ignore it.

#MYOBJECT See above. This is the object model.

1@ this is the X axis which travels from left to right in the PD compound. This is the only axis to change.

The other 2 floats are the Y and Z axis. They will stay the same, otherwise chances are that they will go off the screen. We dont want the object anywhere in San Andreas, do we? We only want it to be anywhere along the X axis.

It's very simple. Try testing it out. You might try making a system where the player has to run around to different markers and collect them. You dont have to leave out the Y and Z axis. Just set the right from - to values. Also, make sure that you dont make it possible for one of the axis' to f- things up. For example: You might put -100.0 as the from value of the z axis meaning people would have to fall through the floor to get there...


--------------------
Go to the top of the page
 
+Quote Post
Arijit Sen
post Oct 3 2012, 12:42 AM
Post #2


Trained Member

Posts: 85
Joined: 21-September 12



Nice & Simple..Good work..


--------------------
Go to the top of the page
 
+Quote Post
Deji
post Oct 3 2012, 01:27 AM
Post #3


Coding like a Rockstar!

Group Icon

Posts: 1,468
From: ???
Joined: 28-May 09



A bit of an old topic. I could make a million improvements to this topic, but I can't be bothered to write another to tutorial not using SCR syntax...


--------------------
Go to the top of the page
 
+Quote Post
Spider-Vice
post Oct 3 2012, 08:49 PM
Post #4


In motus...

Group Icon

Posts: 361
From: Portugal
Joined: 28-May 09



Aw gawd my eyes, no Sanny code! tongue.gif
Go to the top of the page
 
+Quote Post
Mora Hannover
post Nov 5 2014, 02:46 PM
Post #5


Ameteur Member

Posts: 42
Joined: 3-November 14



That is what i was looking for.
Thanks. You are a real coding like a rockstar


--------------------
Rest in Peace Yeardley Diamond (Dec. 21, 1985 - Aug. 20, 2014)
Rest in Peace Kitty0706 (1994 - Jan. 25, 2015)
Go to the top of the page
 
+Quote Post
Closed TopicStart new topic

2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members: