Math Formula Help

Hi, I don't know where else to ask this.

I'm using Yanfly's System Options script: http://yanflychannel.wordpress.com/rmvxa/menu-scripts/system-options/

And I've placed an additional option where you can adjust encounter rates at will based on a variable, min value 0 and max value 3, with 0 being no encounters.

And so, I wanna modify this script:

class Game_Player < Game_Character #-------------------------------------------------------------------------- # * Create Encounter Count #-------------------------------------------------------------------------- def make_encounter_count n = $game_map.encounter_step @encounter_count = rand(n) + rand(n) + 1 endendNow, I have no idea how to interpret the formula for @encounter_count because I suck at programming. I want it so that when the player sets the variable 30 to max 3, they will encounter battles very frequently, but if set to 0, they will have no random encounters at all. I don't know how to do this. Any help? I tried dividing by $game.variables[30], but if set to 0, it'll divide by 0 and that probably won't work, right?
hmmm... probably something like this

def make_encounter_count if $game_variables[30] == 0 @encounter_count = 9999999 else n = $game_map.encounter_step/$game_variables[30] @encounter_count = rand(n) + rand(n) + 1 end endso, as the value of the variable goes up, the probability to have encounters become higher (since you now randomize from a smaller range), but since dividing by zero will cause error, we simply give it a different value when it's 0... in this case a very high number...you can change that 1 into another digit that you want to set as "minimum number of steps" for an encounter to happen for reasons as explained by Racheal below...
The way the formula works by default is as follows:

n is set to whatever "Steps Average" you set the map to.

rand(n) will get a random number between 0 and n-1.

So if you set the map to 30 steps average, @encounter_count will be set to a number between 1 and 59, with a slight preference towards to middle. Think of it like rolling two thirty-sided dice.

While I was typing that up, Shana replied, so see that reply for the actual script change. I'm posting this anyway so you can understand how the formula works. If you're changing it anyway for your script, you might consider improving it since as I pointed out, it is possible to have an encounter every single step with the current formula.

You might consider something like the following:

def make_encounter_count n = $game_map.encounter_step d = $game_variables[30] @encounter_count = d == 0 ? 99999 : rand(n) + ((3 - d) * n)/2 + 1 endWith this formula and a Steps Average of 30, you'd see an encounter in:1: 30-59

2: 15-44

3: 1-29
Okay, Shana's script didn't have any crashes or anything, but now there are no encounters at all, even when the variable is set to 3.

EDIT: It seems the script is causing it to get stuck on

if $game_variables[30] == 0@encounter_count = 9999999even if the variable is set to something other than 0. It's like it ignores the rest of the script.
Ah wait, I think I know why, the variable is initially zero, so it brings up the 999999 value at beginning... you'd need to re-call this method when you change the variable value, or give it a non-zero starting value using some other method

I know, try this one:

Code:
  def make_encounter_count    if $game_variables[30] == 0 and $game_switches[id]      @encounter_count = 9999999    else      $game_switches[id] = true      n =  $game_map.encounter_step/$game_variables[30]      @encounter_count = rand(n) + rand(n) + 1    end  end
set id to a game switch that you don't use... make sure that both ids are the same, and make sure to NEVER modify that switch in game...
You're going to need to change the change_custom_variables method in Yanfly's options script:

#-------------------------------------------------------------------------- # change_custom_variables #-------------------------------------------------------------------------- def change_custom_variables(direction) Sound.play_cursor value = direction == :left ? -1 : 1 value *= 10 if Input.press?🙂A) ext = current_ext var = YEA::SYSTEM::CUSTOM_VARIABLES[ext][0] minimum = YEA::SYSTEM::CUSTOM_VARIABLES[ext][4] maximum = YEA::SYSTEM::CUSTOM_VARIABLES[ext][5] $game_variables[var] += value $game_variables[var] = [[$game_variables[var], minimum].max, maximum].min $game_player.make_encounter_count if var == 30 #new line draw_item(index) endYou might also want to check out the edit to my initial post above. I made some suggestions about the encounter rate formula.[Edit] Changed my mind on this a little. Now it recalculates the encounter rate every time you change it instead of just when changing from 0.
Put my edited script first, so that it won't do the 999999 thingy for the game set-up, then add Racheal's code so that it auto-updates the encounter count when you change variables...


@Racheal - I think you should remove the and old_val == 0, so that it will reset everytime the variable is changed, which is actually needed for it to work correctly... coz right now, if I use 3 then I reset it to zero, the encounter rate won't reflect that change immediately and will only reflect once an encounter has happened... 🙂
@Shana: your new script gave me an error about dividing by zero. 🙁

@Rachael: I will try that ...
ah wait, do this:

def make_encounter_count if $game_variables[30] == 0 if $game_switches[id] #so that it doesn't run on start game @encounter_count = 9999999 else n = $game_map.encounter_step/1 @encounter_count = rand(n) + rand(n) + 1 $game_switches[id] = true end else n = $game_map.encounter_step/$game_variables[30] @encounter_count = rand(n) + rand(n) + 1 end endAssuming 1 is your default value for the variable... then get Racheal's code, but probably remove the and old_val == 0 part as I explained aboveand of course make sure to change the id into the id of the switch that you will be using...
My second snippet alone will not get the results you want. It was made just to address the updating encounter rate issue. You'll want to include either Shana's first snippet or my first one.


@Shana: Yeah, I realised that and had already edited it. For some reason my brain was stuck on "if you re-calculate the encounter rate every time, they could use it to avoid battles!!" Yeah, sometimes my brain is really dumb.


Also, you shouldn't need that switch stuff with my auto-update code?
I need it for one reason alone I think, the encounter set-up runs on the start of the map, so since the variable is probably 0 at the start of the very first map, I needed it for protection purposes...


not unless Yanfly's script auto-changes that value right away before the map initializes

"if you re-calculate the encounter rate every time, they could use it to avoid battles!!"
well that's true... but if you won't recalculate, it won't work correctly either... plus they could always just set it to 0 if they want to avoid encounters... 🙂
Ah, that is true, though assuming the first map has encounters (which it's possible it does not), you could just call $game_player.make_encounter_count after you set the variable. It doesn't seem worth the use of a switch to me.
Okay, problem solved! I used Rachael's updated snippet with Shana's way WITHOUT the switch and now it works perfectly! Setting the variable to 0 causes no encounters, even when i set the average steps to 1. Yay!
Yanfly's probably run the default set-up somewhere, so it ran Racheal's code... XD


Take note that it will still cause an encounter, after 9999999 steps on that map... 🙂
Yes, I realize that haha ... although I very much doubt someone will do that xD
oh well, I can give you an additional layer of protection for that... if you want to make sure it never happens

add this inside of that script (so that it's still inside Game_Player):

Code:
  def encounter    return false if $game_map.interpreter.running?    return false if $game_system.encounter_disabled    return false if @encounter_count > 0    return false if $game_variables[30] == 0 #this is PROTECTION    make_encounter_count    troop_id = make_encounter_troop_id    return false unless $data_troops[troop_id]    BattleManager.setup(troop_id)    BattleManager.on_encounter    return true  end

Original thread: https://forums.rpgmakerweb.com/threads/math-formula-help.23317/