Drawing Bitmap Ruby Equation Questions (SOLVED)

Heyo!

Intro and Context

For my next step in Ruby learning: I've been going line by line through previously made (public) scripts and making notes on what I believe each individual line does.

I'm doing this in the hopes that I'll be able to learn more about the parts of RGSS3 that I would like to edit for my own scripts.

So far it's going pretty well.

But I have encountered something I'm not sure how to read and thus figured I'd leave a question about it here:

The Problem / Situation

In Yanfly's Battle Engine Ace script, s/he makes it so the Window_BattleStatus class draws the faces of the Actors, it's pretty neat.

I believe I understand how most of this is done... but when it comes to the equations Yanfly has written for their local fx/fy/fw variables... I'm at a little bit of a loss.

Here is the code (I have left my comments in on the particular lines that are giving me trouble, just so you know where the parts that are confusing me are at):

Ruby:
  #--------------------------------------------------------------------------
  # overwrite method: draw_face
  #--------------------------------------------------------------------------
  def draw_face(face_name, face_index, dx, dy, enabled = true)
    bitmap = Cache.face(face_name)
    
    fx = [(96 - item_rect(0).width + 1) / 2, 0].max
        # local fx variable = something I'll have to ask about
    fy = face_index / 4 * 96 + 2
        # local fy variable = other fancy stuff
    fw = [item_rect(0).width - 4, 92].min
        # local fw variable = other fancy stuff
    
    rect = Rect.new(fx, fy, fw, 92)
        # draws ??? rect as: x, y, width, height
    rect = Rect.new(face_index % 4 * 96 + fx, fy, fw, 92)
        # draws bitmap rect as: x, y, width, height (getting the correct Actor faces)
    
    contents.blt(dx, dy, bitmap, rect, enabled ? 255 : translucent_alpha)
    bitmap.dispose
  end


Outro / Conclusion / The Questions

So there are two things that are tripping me up here:

The first is my lack of decent Maths skills. XD

1) How do I read the fx, fy and fw equations?
In my mind it goes:
Ruby:
    fx = [(96 - item_rect(0).width + 1) / 2, 0].max
fx is equal to 96 minus whatever item_rect.width plus 1 is, then divided by 2 and now why is there a 0 there and why is there also a 0 in brackets after item_rect but before width like whaaaaaaaaat?

Ruby:
    fy = face_index / 4 * 96 + 2
fy is equal to face_index divided by 4 then multiplied by 96 and THEN 2 is added onto the end of it.
Ruby:
    fw = [item_rect(0).width - 4, 92].min
fw is equal to item_rect.width minus 4 but now why is there a 92 after it and why is there a 0 in brackets after item_rect but before .width again whaaaaaat iiiiis thiiiiis?

So as I'm sure you can see, I'm either horribly wrong about all that... or only slightly wrong and need to learn more about how equations work within arrays.


The second thing tripping me up is:
2) Why are there two "rect =" right on top of eachother? Doesn't the second "rect =" just immediately overwrite the first one?
Ruby:
    rect = Rect.new(fx, fy, fw, 92)
    rect = Rect.new(face_index % 4 * 96 + fx, fy, fw, 92)
I've tried commenting out the first one and it doesn't seem to make a difference at all as far as I can tell, whereas when I comment out the second one it mucks up what Actor faces are drawn (because the second "rect = " helps Ruby realise what Bitmap face is equal to what Actor).

So is that first "rect =" just a bit of accidental leftover code by Yanfly?
Or am I missing something about it due to my lack of complete knowledge of Ruby?

And that's that!

Thanks for any answers to my questions, sorry for having a not-great Maths education and for my (current) lack of super-knowledge when it comes to coding and Ruby.

Any help here is appreciated 🙂.
fx is equal to 96 minus whatever item_rect.width plus 1 is, then divided by 2 and now why is there a 0 there
It means capped at 0, so the result never goes below 0.
If 96 - item_rect + 1 is less than zero, then zero.

and why is there also a 0 in brackets after item_rect but before width like whaaaaaaaaat?
It will be more helpful if you actually put a note in the default script first than people's script, to be honest.
item_rect indicating the rectangle size of the item. The (0) means the first index.

fy is equal to face_index divided by 4 then multiplied by 96 and THEN 2 is added onto the end of it.
fy = because faces have 0 to 7 index, by dividing it to 4, you know which row the face is being selected. Is it the bottom or top? THEN you multiplied it by 96 because that's the size of the face. Idk why it has 2.

fw is equal to item_rect.width minus 4 but now why is there a 92 after it and why is there a 0 in brackets after item_rect but before .width again whaaaaaat iiiiis thiiiiis?
The value is capped at 92 at maximum. If the values goes over than 92, it will always be 92

The second thing tripping me up is:
2) Why are there two "rect =" right on top of eachother? Doesn't the second "rect =" just immediately overwrite the first one?
Two rects were probably an oversight. As it is overwriting each other.

Again, I would advise you to just try to understand the default script first before going further. If you know every piece of the default Window_Base and Window_Selectable, you're already winning.
It means capped at 0, so the result never goes below 0.
If 96 - item_rect + 1 is less than zero, then zero.
Ah, that makes sense, cool thanks.
It will be more helpful if you actually put a note in the default script first than people's script, to be honest.
item_rect indicating the rectangle size of the item. The (0) means the first index.
Whenever I come across a method I don't know well, I typically ctrl, shift, f it to see what it originally was (if a default method) and what it is now (if overwritten in the YEA script or just created brand new in the YEA script).

For some reason I failed to clock the (index) after item_rect.

That makes sense.
Thanks for that.
fy = because faces have 0 to 7 index, by dividing it to 4, you know which row the face is being selected. Is it the bottom or top? THEN you multiplied it by 96 because that's the size of the face. Idk why it has 2.
Ohh okay, think I getchu.
At least, to an extent.
That dividing by 4 thing to get top or bottom row... are you saying (for example) if the result of the number divided by 4 is less than 1, then it's row X but if it's 1 or above then it's row Y?

As 0/4 = 0, 1/4 = 0.25, 2/4 = 0.5, 3/4 = 0.75 4/4 = 1, 5/4 = 1.25, 6/4 = 1.5 and 7/4 = 1.75 ?

So the cut off point for row X or row Y is index number 4?
The value is capped at 92 at maximum. If the values goes over than 92, it will always be 92
Right, same as the random , 0 before, but this time a , 92.
Cool, simple enough to understand, never seen that before though so sorry for my ignorance on it.
Two rects were probably an oversight. As it is overwriting each other.
Yeah I thought so, cool.
Again, I would advise you to just try to understand the default script first before going further. If you know every piece of the default Window_Base and Window_Selectable, you're already winning.
I appreciate that's probably the best thing to do in hindsight, but I'm about 1600 lines done on this 3000 line YEA script so I'mma finish this one off first.

The way I was looking at the default stuff was more of a need-to-know basis before, like, I'd learn bits about it when I was trying to figure out how to manipulate those particular bits.

The idea of starting from the top and reading all of it at once is... intimidating lol.

But probably a good idea, you're not wrong.
That's method Array.max and Array.min for you~

That dividing by 4 thing to get top or bottom row... are you saying (for example) if the result of the number divided by 4 is less than 1, then it's row X but if it's 1 or above then it's row Y?

As 0/4 = 0, 1/4 = 0.25, 2/4 = 0.5, 3/4 = 0.75 4/4 = 1, 5/4 = 1.25, 6/4 = 1.5 and 7/4 = 1.75 ?

face_index is an integer, NOT float.
Division of an integer ALWAYS return integer (rounded down), so instead of:
0, 0.25, 0.5, 0.75, 1, 1.25, 1.5, 1.75
the outcome of the iteration will be:
0, 0, 0, 0, 1, 1, 1, 1
multiplied by 96 and it will become:
0, 0, 0, 0, 96, 96, 96, 96
+2 for the offset:
2, 2, 2, 2, 98, 98, 98, 98

Such trick is often used for indexing.
Division for the X index, and modulo for the Y index.
That's method Array.max and Array.min for you~



Division of integer is rounded down, so instead of:
0, 0.25, 0.5, 0.75, 1, 1.25, 1.5, 1.75
the outcome of the iteration will be:
0, 0, 0, 0, 1, 1, 1, 1
multiplied by 96 and added 2 and it will become:
0, 0, 0, 0, 98, 98, 98, 98
Thanks for that. 🙂

--Edit--
Should I report this thread for closing now?
I feel like everything's been answered but I also feel like maybe it's also mid-potential-conversation with TheoAllen.

Hmm, alrighty, I'm gonna report it for closing, @TheoAllen feel free to message me if you have anything else to add.

Thanks everyone! 🙂

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/drawing-bitmap-ruby-equation-questions-solved.125407/