Mathematical /functions? question

Users who are viewing this thread

Cromcrom

Hi all,

my rigale mod uses a lot of random checks through tables.
For example:

Code:
(store_random_in_range,":whatever",1,101),
(try_begin),
(is_between,":whatever",1,30),
(whatever_1),
(else_try),
(is_between,":whatever",30,60),
(whatever_2),
(else_try),
(is_between,":whatever",60,101),
(whatever_3),
(try_end),

my problem is that if I want to change the chance of (whatever_1), occuring, I will have to change almost all the (is_between) numbers.

What I am looking for is a way to simply set a range to (whatever_1), and then have the game sum all the ranges, and then check the random chance of one of the (whatever_x) occuring.
It would look like this ?
Code:
(assign,":whatever_1_chance_to_occur",30),
(assign,":whatever_2_chance_to_occur",30),
(assign,":whatever_3_chance_to_occur",40),
(function_to_check/return_the_result),
I think I could probably code this function, but I was wondering if there is a header_operation.py function that would do this for me ?
I am vastly underexploiting this game because of the lack of header_operation.py various functions explanations.

So any tips or hints or links or bits of code would be vastly appreciated.

Thanks in advance, and take care.
 
Code:
(assign,":whatever_1_chance_to_occur",30),
(assign,":whatever_2_chance_to_occur",30),
(assign,":whatever_3_chance_to_occur",40),
(store_add, ":limit1", ":whatever_1_chance_to_occur", ":whatever_2_chance_to_occur"),
(store_add, ":limit2",  ":limit1", ":whatever_3_chance_to_occur"),
(store_random_in_range,":whatever",0,100),
(try_begin),
(is_between,":whatever",0,":whatever_1_chance_to_occur"),
(whatever_1),
(else_try),
(is_between,":whatever",":whatever_1_chance_to_occur",":limit1"),
(whatever_2),
(else_try),
(is_between,":whatever",":limit1",":limit2"),
(whatever_3),
(try_end),


Better alternative :
Code:
(assign,":whatever_1_chance_to_occur",30),
(assign,":whatever_2_chance_to_occur",30),
(assign,":whatever_3_chance_to_occur",40),
(store_random_in_range,":whatever",0,100),
(try_begin),
(is_between,":whatever",0,":whatever_1_chance_to_occur"),
(whatever_1),
(else_try),
(val_add, ":whatever_2_chance_to_occur", ":whatever_1_chance_to_occur"),
(is_between,":whatever",":whatever_1_chance_to_occur",":whatever_2_chance_to_occur"),
(whatever_2),
(else_try),
(val_add, ":whatever_3_chance_to_occur", ":whatever_2_chance_to_occur"),
(is_between,":whatever",":whatever_2_chance_to_occur",":whatever_3_chance_to_occur"),
(whatever_3),
(try_end),
 
No, there is no operation like the one you describe. You would need to write a script to do that.
My quick take on a function like this would be:
Code:
(call_script, "script_select_outcome_with_probability", ":prob1", ":prob2", ":prob3", ":prob4", ":prob5"),
(assign, ":result", reg0),
(try_begin),
    (eq, ":result", 1),
    (whatever_1),
//...etc...//


  ("select_outcome_with_probability", [
    (store_script_param, ":prob1", 1),
    (store_script_param, ":prob2", 2),
    (store_script_param, ":prob3", 3),
    (store_script_param, ":prob4", 4),
    (store_script_param, ":prob5", 5),
    
    (assign, ":result", -1),
    
    (store_add, ":range", ":prob1", ":prob2"),
    (val_add, ":range", ":prob3"),
    (val_add, ":range", ":prob4"),
    (val_add, ":range", ":prob5"),
    (val_add, ":range", 1),
    
    (store_random_in_range, ":result", 0, ":range"),
    
    (try_begin),
        (gt, ":prob1", 0),
        (is_between, ":result", 0, ":prob1"),
        (assign, ":result", 1),
    (else_try),
        (gt, ":prob1", 0),
        (gt, ":prob2", 0),
        (is_between, ":result", ":prob1", ":prob2"),
        (assign, ":result", 2),
    (else_try),
        (gt, ":prob2", 0),
        (gt, ":prob3", 0),
        (is_between, ":result", ":prob2", ":prob3"),
        (assign, ":result", 3),
    (else_try),
        (gt, ":prob3", 0),
        (gt, ":prob4", 0),
        (is_between, ":result", ":prob3", ":prob4"),
        (assign, ":result", 4),
    (else_try),
           (gt, ":prob4", 0),
        (gt, ":prob5", 0),
        (is_between, ":result", ":prob4", ":prob5"),
        (assign, ":result", 5), 
    (else_try),
        (assign, ":result", -1), #Or something for error handling.
    (try_end),
    
    (assign, reg0, ":result"),
   ]),

I'm not certain it gains you a whole lot, but if it is easier for you to think about that way, then so be it.
 
I would suggest a small change to Dunde's Code.

Instead of:
(store_random_in_range,":whatever",0,100)

Try:
(store_random_in_range,":whatever",0,":limit2")

So you can change the values for every chance independently from the sum of 100.

Edit: Caba'drin's Code doing exactly this.
 
Thank you so much guys. So I guess I will have to use home made functions to this thing, thank you very much again.  :grin:
 
Hmmm, after testing mister Caba'drin 's code, I have to come with this fix (red code), that goes just below the original blue line:
(val_add, ":range",1),

(val_add,":prob2",":prob1"),
(val_add,":prob3",":prob2"),
(val_add,":prob4",":prob3"),
(val_add,":prob5",":prob4"),


Indeed, if I assign, for example, 5 to prob3, and 20 to prob2 , (is_between,":result",":prob2",":prob3")  won't work (is between 20,5). But if I add prob3 to prob2, it works (is between, 20,20+5).

Or am I wrong ?
 
Computica said:
I feel like this anytime Caba`drin, dunde, Somebody, or SPD_Phoenix post there version of a similar code on one post ->  :oops::!: :?: :idea: :shock:
Since you'll be calling the script, you should make sure the probabilities are in order. You can also try something like this:
Code:
(store_script_param, ":num_param", 1), #pass in the number of probabilities + 2
(assign, ":max", 1),
(try_for_range, ":parameters", 2, ":num_param"),
  (store_script_param, ":prob", ":parameters"),
  (val_add, ":max", ":prob"),
(try_end),
#or assume/pass in arbitrary max instead of calculating on-the-fly
(store_random_in_range, ":result", 0, ":max"),
(try_for_range, ":parameters", 2, ":num_param"),
  (store_script_param, ":prob", ":parameters),
  (lt, ":result", ":prob"),
  (assign, ":num_param", 0),
  (store_sub, ":result", ":parameters", 1), #first prob = 1, etc
(else_try), #no need to re-add probabilities
  (val_sub, ":result", ":prob"),
(try_end),

(try_begin), #result found
  (eq, ":num_param", 0),
  (assign, reg0, ":result"),
(else_try), #invalid
  (assign, reg0, -1),
(try_end),
You can probably use a temp slot or something in the first loop to store the parameters instead of store_script_param again in the second. Haven't tested this but it should work.
 
Yeah, Crom, I think mine would need those lines that you indicated, right where you indicate them. Apologies for not thinking through that.

Though Somebody's code, as always, is far more elegant.
But either comes down to exactly how you'll be passing data to the script.
 
Yeah, Crom, I think mine would need those lines that you indicated, right where you indicate them. Apologies for not thinking through that.
Please no apologies, I wasn't requesting you to code everything, which you did anyways. As a matter of fact, you make me feel like a genius "fixing" one of your code, so please no apologies :lol:

Though Somebody's code, as always, is far more elegant.
I do not doubt it, and please mister Somebody don't take it bad, but I simply don't understand your code and how it works. I will try harder, as it's early in the morning here, and mister caba'drin 's code is perfect for my simple mind.

Anyways, thank you very much for your time and patience, I already make good use of these scripts. :mrgreen:

Take care  :grin:
 
Back
Top Bottom