Lengthy damage formula

I need a damage formula which is far too long to fit in the damage formula box so I've been trying to concoct a custom damage snippet for the script editor. So far it's been a disaster. I need a formula which can check if either of these 2 if/else are true, and run either of them if only one is true or both if both are true.

if $game_player.region_id == 40; 4*a.level + a.mat*4 - b.mdf*1.5; else; 4*a.level + a.mat*3 - b.mdf*1.5; end

if b.state?(23); b.add_debuff(6, 3); end; 4*a.level + a.mat*3 - b.mdf*1.5

Can anyone help me out?
Thanks
Orange - First formula
Purple - Second Forumla

if $game_player.region_id == 40; if b.state?(23); b.add_debuff(6, 3); end; 4*a.level + a.mat*4 - b.mdf*1.5; else; 4*a.level + a.mat*3 - b.mdf*1.5; end

See if that works.

if that doesn't fit, create a blank page on your script editor and put this:

[name of your choosing] = formula

For ex,
VECTRA = if $game_player.region_id == 40; if b.state?(23); b.add_debuff(6, 3); end; 4*a.level + a.mat*4 - b.mdf*1.5; else; 4*a.level + a.mat*3 - b.mdf*1.5; end

So, in the damage formula, put eval(VECTRA)
How would the line breaks work, or is it one long line? I'm a scripting idiot.
I actually don't know how they work. I tried multiple times to come up on how to do it :/

I just put it all on one line and just scroll to the right lol.

Maybe Try this:
POWER = if b.state?(23); b.add_debuff(6, 3); end;

VECTRA = if $game_player.region_id == 40; eval(POWER); 4*a.level + a.mat*4 - b.mdf*1.5; else; 4*a.level + a.mat*3 - b.mdf*1.5; end


Has to be in that order because RPG Maker loads up to down.
Sadly when I do either I get

NoMethodError
undefined method 'region_id' for nil:NilClass
Ooo, I believe I know the solution. Do you know how to make a variable equal the region id? If so, you can replace it with

/v[x] == 40

If not, I don't know how your game works, but you might need a script call...
$game_variable[x] = $game_player.region_id

x = variable ID

If that doesn't work,
This makes a variable equal to the Region ID based on your X-Coord and Y-Coord which are found using other variables, so you need 3 variables in total.
X-Coord
Y-Coord
Region ID
And add wait after you find region ID in case it may lag your game a bit.
unknown.png
I am now deeply confused (a frequent condition, I admit).

I can't find the region ID among the possibilities for a variable in the Event Command Menu, only map ID, so I assume that I would use that script call within the variable command (bottom of the first tab in the variables command). Unless you could explain where to find the region ID as an option?

I set a variable to a region ID - and then what? How does that tie in with the damage formula in the script snippet?
I can write you a snippet after work if this isn't solved by then.
The red is where you go to get the Region ID info.
upload_2017-6-14_15-23-21.png


This is the window you are looking for:
In my case, I'm using variable 89 to insert the Region ID. If u look above, the event that checks region ID is parallel process so it constantly checks.
unknown.png


You do v[x] to insert a variable in the damage formula. In your case, it will be...
v[89] == 40; if b.state?(23); b.add_debuff(6, 3); end; 4*a.level + a.mat*4 - b.mdf*1.5; else; 4*a.level + a.mat*3 - b.mdf*1.5; end

You replace "if $game_player.region_id == 40" with "v[89] == 40"
It's v[89] for me because 89 is the variable ID used to equal to the Region ID. if it was 6, it would be v[6]

You MIGHT be able to fit that in damage formula.
Okay, I set a variable to a region ID - in my case variable 236.
Are you saying that I have to have that parallel process running on every single map where this would apply? This makes me uneasy. Particularly as I am using the player's x and y in other events already. Would they clash? Or are you saying that once the variable has been set (which I could do at the very beginning of the game) I only need to refer to it in the snippet?

I'm sorry if these questions seem ultra basic, but I'm having difficulty getting my head around this.
The reason you get this error is probably because you forgot to make your additions actual strings in your script:

If you write
POWER = if b.state?(23); b.add_debuff(6, 3); end;
the colored part is considered actual code and is immediately executed when the script gets loaded.

If you write
POWER = "if b.state?(23); b.add_debuff(6, 3); end;"
the colored part is considered a character string which can be evaluated in your formula using eval.


Of course you can also save some space by storing the region ID in a game variable first.

---

As an alternative, if you are using that formula only for a specific skill, you can also use that skills notebox to store your formula. This way you don't need an additional script and have the formula together with the rest of the skill data on the same page:

To execute a section of your notebox as your damage formula you can put this into your formula box:
eval $data_skills[Skill-ID].note[/<formula>(.*?)<\/formula>/m, 1]

This will search the notebox for a section surrounded by
<formula>
...
</formula>

tags and execute it.

Note that the line breaks in your notebox are actually considered line breaks in your script.
They will not clash.

Make it into a parallel process common event. After that, all you need to do is call a switch. What switch? You set with switch is used to turn the common event on by picking a conditional switch in the upper-right corner of the below picture.
unknown.png


EDIT: I forgot to mention you needed quotes.
It would be probably enough to set the variable at the start of each battle instead of using a parallel process for this.

I personally try to avoid setting a game variable each frame since that usually means that all events on your map are checked for page switches every frame as well.

All variables do is storing numbers, they do not interfere with the sources they take the numbers from. So you shouldn't have to worry how many variables use the players coordinates.
^ oooo, I didn't think of that.
@King Vectra This is why I disliked the thought of having this running from a parallel process.

@Another Fen I can't set this at the beginning of each battle because I give my players the choice between having visible (on map, evented) enemies or invisible (random) enemies because I know that some players have very strong preferences for one or the other. Obviously there would be no problem for evented enemies, but with random....

I think your notebox idea should work with either. I have 2 questions.
I assume the [Skill-ID] part is simply the ID of that particular skill - correct?
Is this the correct arrangement of line breaks?
if $game_player.region_id == 40;
if b.state?(23); b.add_debuff(6, 3); end;
4*a.level + a.mat*4 - b.mdf*1.5;
else; 4*a.level + a.mat*3 - b.mdf*1.5;
end
I know some don't like parallel processes, but it is the quickest route. And interesting feature u got there ^_^
@King Vectra If I can crack this, I have several skills where I could apply the solution. I have a number of skills where an additional status affliction of some type is applied if the enemy is weak to the element of the skill (that's the b.state?(x) thing - the passive state identifies the particular element weakness) and it would be good if I can get this terrain layer onto a particular character's skills as well.
I'll risk to say, you can trigger a common event through the formula, use the common event to establish a global function, and then trigger the function from the formula, and return the result of that function as result of the formula.
it's a bit tricky, but it should work, given that everything is run at interpreter level.

I believe I ran a common event manually through unconventional means, but can't remember if it was through a formula.

EDIT
For ex,
VECTRA = if $game_player.region_id == 40; if b.state?(23); b.add_debuff(6, 3); end; 4*a.level + a.mat*4 - b.mdf*1.5; else; 4*a.level + a.mat*3 - b.mdf*1.5; end

So, in the damage formula, put eval(VECTRA)

basically, that, but from the common event section, without modifying the scripts.
@Kes:
If you want to run event commands at the beginning of each battle, the easiest solution would be to use a troop event page for this. Since that would require you to copy that troop event for every troop you use I'd suggest to only use a "Call Common Event" command there or use a script that provides global troop events.

I assume the [Skill-ID] part is simply the ID of that particular skill - correct?
That's correct (though the square brackets are part of the code and should be retained). 🙂
For items, you would also replace $data_skills by $data_items.

Is this the correct arrangement of line breaks?
This arrangement should work.

Line breaks in Ruby are used to separate commands. Thus a line break after a semicolon (which explicitely indicates the end of a command already) shouldn't be a problem.
Since you should have enough space in the notebox, you don't necessarily have to squeeze multiple commands in one line of course.

Also, right now your target only gets their agility debuffed if it is inflicted with state 23 and the player stands within region 40 (since the condition is located within the "condition fulfilled" block of the first if-statement). If you want to debuff the target regardless of the region, you might want to move the second line of your formula to the top.
Okay... I haven't read through the other responses yet, but here's the snippet.
I suggest renaming the method to something suitable. Just follow the same format when you call it from your damage formula text box.

Spoiler: Custom Formula
Code:
#==============================================================================
# ** Game_Battler
#------------------------------------------------------------------------------
#  A battler class with methods for sprites and actions added. This class 
# is used as a super class of the Game_Actor class and Game_Enemy class.
#==============================================================================

class Game_Battler < Game_BattlerBase
  #--------------------------------------------------------------------------
  # * (Insert Name Here)
  # Damage Formula: a.custom_formula(a, b)
  #--------------------------------------------------------------------------
  def custom_formula(a, b)
   
    b.add_debuff(6, 3) if b.state?(23)
   
    if $game_player.region_id.==(40)
      4 * a.level + a.mat * 4 - b.mdf * 1.5
    else
      4 * a.level + a.mat * 3 - b.mdf * 1.5
    end
   
  end
end

Since it's checking for a region ID, this formula will not work in test battles.

EX: Renaming the method:
def custom_formula(a, b) => def freeze_magic(a, b)

EX: Damage Formula:
a.custom_formula(a, b) => a.freeze_magic(a, b)

Original thread: https://forums.rpgmakerweb.com/threads/lengthy-damage-formula.79942/