Jeff's Utilities

Jeff's Utilities Version: 1.1
By: Jeff the Green (Bearcat)

Introduction

This is a collection of utilities I wrote for my own use and will be necessary for most scripts I release post 2010. Until they're a part of the MACL, they'll be available as the standalone script here.

Features
The current list of methods is:
  • Array.average: returns the arithmetic mean (average) of all numeric elements of the array.
  • Array.geommean: returns the geometric mean of all positive numeric elements of the array.
  • Array.harmmean: returns the harmonic mean of all positive numeric elements of the array.
  • Array.permute: returns a random permutation of the array.
  • Array.permute!: permutes self.
  • Bitmap.draw_formatted_text_jeff: draws formatted text using many of the same codes as ccoa's UMS.
  • Bitmap.formatted_text_height_jeff: determines the height of formatted text with a certain width.
  • JeffUtilities.normal: generates a random number from a normal distribution.
  • JeffUtilities.pseudorand: generates a pseudorandom number using a seed stored in $game_system.

Screenshots

This is a bunch of scripting utilities, so it's hard to do a screenshot. If someone can think of one they'd like to see, I'll post it.


Script

  1. =begin
  2. ==============================================================================
  3.  ●● Jeff's Utility functions
  4. ------------------------------------------------------------------------------
  5.  Jeff the Green
  6.  Version 1.1
  7.  
  8. ------------------------------------------------------------------------------
  9.  
  10.  Version notes
  11.  ----------------
  12.    New methods:
  13.      Array.average: returns the arithmetic mean (average) of all numeric
  14.        elements of the array.
  15.      Array.geommean: returns the geometric mean of all positive numeric
  16.        elements of the array.
  17.      Array.harmmean: returns the harmonic mean of all positive numeric
  18.        elements of the array.
  19.      Array.permute: returns a random permutation of the array.
  20.      Array.permute!: permutes self.
  21.      Bitmap.formatted_text_height_jeff: determines the height of formatted
  22.        text with a certain width.
  23.        
  24.    Deprecated methods:
  25.      JeffUtilities.permutation: Replaced by Array.permute
  26.  
  27. ==============================================================================
  28. =end
  29.  
  30. class JeffUtilities
  31.   #-------------------------------------------------------------------------
  32.   # * Normal distribution
  33.   #     Returns a floating-point number drawn from a
  34.   #     normal distribution. If no *args, uses standard distribution.
  35.   #    
  36.   #     mean  : mean of distribution
  37.   #     stdev : standard deviation of distribution
  38.   #-------------------------------------------------------------------------
  39.   def self.normal(mean = 0, stdev = 1)
  40.     w = 2
  41.     while w >= 1 # Generate standard normal distribution
  42.       x1 = 2.0 * rand() - 1.0
  43.       x2 = 2.0 * rand() - 1.0
  44.       w = x1 ** 2 + x2 ** 2
  45.     end
  46.     w = Math.sqrt((-2.0 * Math.log(w)) / w)
  47.     w = w * stdev + mean #Convert to n(mean, stdev)
  48.     return w
  49.   end
  50.   #-------------------------------------------------------------------------
  51.   # * Pseudorandom number
  52.   #     Returns a random number generated using the seed stored in
  53.   #     $game_system.pseudorandom_seed, updates
  54.   #     $game_system.pseudorandom_seed, and re-randomizes the random seed used
  55.   #     by rand.
  56.   #    
  57.   #     max : the maximum of the pseudorandom number. If 0 will return a
  58.   #           floating point number between 0 and 1
  59.   #-------------------------------------------------------------------------
  60.   def self.pseudorand(max = 0)
  61.     srand($game_system.pseudorandom_seed) #set random seed
  62.     bigrandom = rand(4294967294) + 1 #generate random 32-bit integer
  63.     if max == 0
  64.       pseudorand = (bigrandom - 1) / 4294967294.0
  65.     else
  66.       pseudorand = bigrandom % (max + 1)
  67.     end
  68.     $game_system.pseudorandom_seed = bigrandom
  69.     srand
  70.     return pseudorand
  71.   end
  72.   #-------------------------------------------------------------------------
  73.   # * Permutation
  74.   #     Returns a random permutation of an array
  75.   #    
  76.   #     ary : the array to be permuted
  77.   #-------------------------------------------------------------------------
  78.   def self.permutation(ary)
  79.     return ary.permute
  80.   end
  81. end
  82.  
  83. #==============================================================================
  84. # ** Array
  85. #==============================================================================
  86. class Array
  87.   #-------------------------------------------------------------------------
  88.   # * Permutation
  89.   #     Returns a random permutation of self
  90.   #-------------------------------------------------------------------------
  91.   def permute
  92.     ary = self.dup
  93.     permutation = []
  94.     while ary != []
  95.       index = rand(ary.size)
  96.       permutation.push(ary[index]) # add object to permutation
  97.       ary.delete_at(index)
  98.     end
  99.     return permutation
  100.   end
  101.   #-------------------------------------------------------------------------
  102.   # * Permutation
  103.   #     Permutes self as Array.permute, returns self
  104.   #-------------------------------------------------------------------------
  105.   def permute!
  106.     old_ary = self.dup
  107.     self.replace(self.permutation)
  108.     return old_ary
  109.   end
  110.   #-------------------------------------------------------------------------
  111.   # * Average
  112.   #     Returns the arithmetic mean of all numeric elements;
  113.   #     nil if no such elements
  114.   #-------------------------------------------------------------------------
  115.   def average
  116.     count = 0
  117.     sum = 0.0
  118.     for i in self
  119.       if i.is_a? Numeric
  120.         count += 1
  121.         sum += i
  122.       end
  123.     end
  124.     return count > 0 ? sum / count : nil
  125.   end
  126.   #-------------------------------------------------------------------------
  127.   # * Geometric Mean
  128.   #     Returns the geometric mean of all positive numeric elements;
  129.   #     nil if no such elements
  130.   #-------------------------------------------------------------------------
  131.   def geommean
  132.     count = 0
  133.     sum = 0.0
  134.     for i in self
  135.       if i.is_a? Numeric and i > 0
  136.         count += 1
  137.         sum += Math.log(i)
  138.       end
  139.     end
  140.     return count > 0 ? Math.exp(sum / count) : nil
  141.   end
  142.   #-------------------------------------------------------------------------
  143.   # * Harmonic Mean
  144.   #     Returns the harmonic mean of all positive numeric elements;
  145.   #     nil if no such elements
  146.   #-------------------------------------------------------------------------
  147.   def harmmean
  148.     count = 0
  149.     sum = 0.0
  150.     for i in self
  151.       if i.is_a? Numeric and i > 0
  152.         count += 1
  153.         sum += (i.is_a Float) ? i ** -1 : i.to_f ** -1
  154.       end
  155.     end
  156.     return count > 0 ? count / sum : nil
  157.   end
  158. end
  159.  
  160. #==============================================================================
  161. # ** Bitmap
  162. #==============================================================================
  163.  
  164. class Bitmap
  165.   #--------------------------------------------------------------------------
  166.   #   Name      : Draw Formatted Text
  167.   #   Info      : Draws Formatted Wrapped Text
  168.   #   Author    : JeffTheGreen
  169.   #   Call Info : Five Arguments
  170.   #               Integer Offset_x and Offset_Y, define starting position
  171.   #               Integer Width, defines the width of the text rectangle
  172.   #               Integer Height, defines the height of the text drawn
  173.   #               String Text, The text to be drawn
  174.   #   Comment   : \c[0xrrggbb] -> Change Color values are in hex
  175.   #               \c -> Change text color
  176.   #               \b -> Turns on/off Bold
  177.   #               \i -> Turns on/off Italics
  178.   #               \s -> Turns on/off shadow
  179.   #               \o -> Turns on/off outline
  180.   #               \font[name] -> changes font
  181.   #               \oa[i] -> displays armor
  182.   #               \oi -> displays item
  183.   #               \os -> displays skill
  184.   #               \ow -> displays weapon
  185.   #-------------------------------------------------------------------------
  186.   def draw_formatted_text_jeff(x, y, width, height, text)
  187.     old_font = self.font
  188.     last_color = self.font.color
  189.     line_height = 0
  190.     # Set to Normal Color (White)
  191.     self.font.color = Color.new(255, 255, 255)
  192.     # Make Copy of Text Sent
  193.     text = text.dup
  194.     # Change "\\C" to "\001"
  195.     text.gsub!(/\\[Nn]/) { '\012' }
  196.     text.gsub!(/\\[Cc]\[0x([A-F a-f 0-9]+)\]/) {" \001[#{$1}] "}
  197.     text.gsub!(/\\[Cc]\[[Ll][Aa][Ss][Tt]\]/) {'\013'}
  198.     text.gsub!(/\\[Cc]\[([0-9])\]/) {" \002[#{$1}] "}
  199.     # armor, items, skills, and weapons
  200.     text.gsub!(/\\[Oo][Aa]\[([0-9]+)\]/) {
  201.       item = $data_armors[$1.to_i]
  202.       "\008[#{$1}]" + "  " + item.name
  203.       }
  204.     text.gsub!(/\\[Oo][Ii]\[([0-9]+)\]/) {
  205.       item = $data_items[$1.to_i]
  206.       "\009[#{$1}]" + "  " + item.name
  207.       }
  208.     text.gsub!(/\\[Oo][Ss]\[([0-9]+)\]/) {
  209.       item = $data_skills[$1.to_i]
  210.       "\010[#{$1}]" + "  " + item.name
  211.       }
  212.     text.gsub!(/\\[Oo][Ww]\[([0-9]+)\]/) {
  213.       item = $data_weapons[$1.to_i]
  214.       "\\011[#{$1}]" + "  " + item.name
  215.     }
  216.     # bold and italics
  217.     text.gsub!(/\\[Bb]/) { "\003" }
  218.     text.gsub!(/\\[Ii]/) { "\004" }
  219.     # shadow
  220.     text.gsub!(/\\[Ss]/) { "\005" }
  221.     # outline
  222.     text.gsub!(/\\[Oo]/) { "\006" }
  223.     # font
  224.     text.gsub!(/\\[Ff][Oo][Nn][Tt]\[(.*?)\]/) { "\007[#{$1}]" }
  225.     # Setup Cx and Cy
  226.     cx, cy = 0, 0
  227.     # Setup outline and shadow
  228.     outline, shadow = false, false
  229.     # Run Through each text
  230.     text.split(/\s/).each do |word|
  231.       # If String \001 is included
  232.       if word.include?("\001")
  233.         # Slice from String
  234.         word.slice!("\001")
  235.         # Change text color
  236.         word.sub!(/\[([A-F a-f 0-9]+)\]/, '')
  237.         # If matched is not nil and size of match is > 1
  238.         if $1 != nil and $1.size > 1
  239.           # Create Color
  240.           color = Color.new($1[0..1].hex, $1[2..3].hex, $1[4..5].hex)
  241.         else
  242.           # Normal Color
  243.           color = old_font.color
  244.         end
  245.         # Set Color
  246.         last_color = self.font.color.dup
  247.         self.font.color = color
  248.         # Go to next text
  249.         next
  250.       end
  251.       if word.include?("\002")
  252.         # Slice from String
  253.         word.slice!("\002")
  254.         # Change text color
  255.         word.sub!(/\[([0-9])\]/, '')
  256.         # If matched is not nil and size of match is > 1
  257.         if $1 != nil
  258.           # Create Color
  259.           color = text_color($1.to_i)
  260.         else
  261.           # old Color
  262.           color = old_font.color
  263.         end
  264.         # Set Color
  265.         last_color = self.font.color.dup
  266.         self.font.color = color
  267.         # Go to next text
  268.         next
  269.       end
  270.       # If String \003 is included
  271.       if word.include?("\003")
  272.         # Invert Bold Status
  273.         self.font.bold = !self.font.bold
  274.         # Go to next text
  275.         next
  276.       end
  277.       # If String \004 is included
  278.       if word.include?("\004")
  279.         # Invert Italic Status
  280.         self.font.italic = !self.font.italic
  281.         # Go to next text
  282.         next
  283.       end
  284.       # If String \005 is included
  285.       if word.include?("\005")
  286.         # Invert outline
  287.         self.font.shadow = !self.font.shadow
  288.         # Go to next text
  289.         next
  290.       end
  291.       # If String \006 is included
  292.       if word.include?("\006")
  293.         # Invert outline
  294.         outline = !outline
  295.         # Go to next text
  296.         next
  297.       end
  298.       if word.include?("\007")
  299.         # Slice from String
  300.         word.slice!("\001")
  301.         # Change font
  302.         word.sub!(/\[(.*?)\]/, "")
  303.         font = $1.to_s
  304.         if font == ""
  305.           self.font.name = old_font
  306.         else
  307.           self.font.name = font
  308.         end
  309.         # go to next text
  310.         next
  311.       end
  312.       if word.include?("\008")
  313.         # Slice from String
  314.         word.slice!("\007")
  315.         word.sub!(/\[([0-9]+)\]/, "")
  316.         index = $1.to_i
  317.         item = $data_armors[index]
  318.         # draw the icon
  319.         icon = RPG::Cache.icon(item.icon_name)
  320.         self.blt(cx + x +  4, cy + y + 4, icon, Rect.new(0, 0, 24, 24))
  321.         cx += 28
  322.         next
  323.       end
  324.       if word.include?("\009")
  325.         # Slice from String
  326.         word.slice!("\008")
  327.         word.sub!(/\[([0-9]+)\]/, "")
  328.         index = $1.to_i
  329.         item = $data_items[index]
  330.         # draw the icon
  331.         icon = RPG::Cache.icon(item.icon_name)
  332.         self.blt(cx + x + 4, cy + y + 4, icon, Rect.new(0, 0, 24, 24))
  333.         cx += 28
  334.         next
  335.       end
  336.       if word.include?('\010')
  337.         # Slice from String
  338.         word.slice!("\009")
  339.         word.sub!(/\[([0-9]+)\]/, "")
  340.         index = $1.to_i
  341.         item = $data_skills[index]
  342.         # draw the icon
  343.         icon = RPG::Cache.icon(item.icon_name)
  344.         self.blt(cx + x + 4, cy + y + 4, icon, Rect.new(0, 0, 24, 24))
  345.         cx += 28
  346.         next
  347.       end
  348.       if word.include?('\\011')
  349.         # Slice from String
  350.         word.slice!("\010")
  351.         word.sub!(/\[([0-9]+)\]/, "")
  352.         index = $1.to_i
  353.         item = $data_weapons[index]
  354.         # draw the icon
  355.         icon = RPG::Cache.icon(item.icon_name)
  356.         self.blt(cx + x + 4, cy + y + 4, icon, Rect.new(0, 0, 24, 24))
  357.         cx += 28
  358.         next
  359.       end
  360.       # If String \012 is included
  361.       if word.include?('\012')
  362.         # Move to new line
  363.         cy = cy + line_height
  364.         # Move to start
  365.         cx = 0
  366.         # Go to next text
  367.         next
  368.       end
  369.       if word.include?('\013')
  370.         # set color to last used
  371.         self.font.color = last_color
  372.         next
  373.       end
  374.       # Get Word Width
  375.       word_width = text_size("#{word} ").width
  376.       word_height = text_size("#{word} ").height
  377.       line_height = [line_height, word_height].max
  378.       # If can't fit on line
  379.       if (cx + word_width) > width
  380.         # Move to new line
  381.         cy = cy + line_height
  382.         # Move to start
  383.         cx = 0
  384.       end
  385.       # Get Draw X
  386.       dx = cx + x
  387.       # Get Draw Y
  388.       dy = cy + y
  389.       # Draw Text
  390.       if outline
  391.         draw_text_outline(dx, dy, word_width, 32, word)
  392.       end
  393.       draw_text(dx, dy, word_width, 32, word)
  394.       # Increase by Word Width
  395.       cx += word_width
  396.       self.font = old_font
  397.     end
  398.   end
  399.   #--------------------------------------------------------------------------
  400.   #   Name      : Formatted Text Size
  401.   #   Info      : Returns the size (height) of the formatted text at a
  402.   #               given width
  403.   #   Author    : JeffTheGreen
  404.   #   Call Info : Five Arguments
  405.   #               Integer Offset_x and Offset_Y, define starting position
  406.   #               Integer Width, defines the width of the text rectangle
  407.   #               Integer Height, defines the height of the text drawn
  408.   #               String Text, The text to be drawn
  409.   #   Comment   : \c[0xrrggbb] -> Change Color values are in hex
  410.   #               \c[i] -> Change text color
  411.   #               \b -> Turns on/off Bold
  412.   #               \i -> Turns on/off Italics
  413.   #               \s -> Turns on/off shadow
  414.   #               \o -> Turns on/off outline
  415.   #               \font[name] -> changes font
  416.   #               \oa[i] -> displays armor
  417.   #               \oi -> displays item
  418.   #               \os -> displays skill
  419.   #               \ow -> displays weapon
  420.   #-------------------------------------------------------------------------
  421.   def formatted_text_height_jeff(width, text)
  422.     old_font = self.font
  423.     last_color = self.font.color
  424.     line_height = 0
  425.     # Set to Normal Color (White)
  426.     self.font.color = Color.new(255, 255, 255)
  427.     # Make Copy of Text Sent
  428.     text = text.dup
  429.     # Change "\\C" to "\001"
  430.     text.gsub!(/\\[Nn]/) { '\012' }
  431.     text.gsub!(/\\[Cc]\[0x([A-F a-f 0-9]+)\]/) {" \001[#{$1}] "}
  432.     text.gsub!(/\\[Cc]\[[Ll][Aa][Ss][Tt]\]/) {'\013'}
  433.     text.gsub!(/\\[Cc]\[([0-9])\]/) {" \002[#{$1}] "}
  434.     # armor, items, skills, and weapons
  435.     text.gsub!(/\\[Oo][Aa]\[([0-9]+)\]/) {
  436.       item = $data_armors[$1.to_i]
  437.       "\008[#{$1}]" + "  " + item.name
  438.       }
  439.     text.gsub!(/\\[Oo][Ii]\[([0-9]+)\]/) {
  440.       item = $data_items[$1.to_i]
  441.       "\009[#{$1}]" + "  " + item.name
  442.       }
  443.     text.gsub!(/\\[Oo][Ss]\[([0-9]+)\]/) {
  444.       item = $data_skills[$1.to_i]
  445.       "\010[#{$1}]" + "  " + item.name
  446.       }
  447.     text.gsub!(/\\[Oo][Ww]\[([0-9]+)\]/) {
  448.       item = $data_weapons[$1.to_i]
  449.       "\\011[#{$1}]" + "  " + item.name
  450.     }
  451.     # bold and italics
  452.     text.gsub!(/\\[Bb]/) { "\003" }
  453.     text.gsub!(/\\[Ii]/) { "\004" }
  454.     # shadow
  455.     text.gsub!(/\\[Ss]/) { "\005" }
  456.     # outline
  457.     text.gsub!(/\\[Oo]/) { "\006" }
  458.     # font
  459.     text.gsub!(/\\[Ff][Oo][Nn][Tt]\[(.*?)\]/) { "\007[#{$1}]" }
  460.     # Setup Cx and Cy
  461.     cx, cy = 0, 0
  462.     # Setup outline and shadow
  463.     outline, shadow = false, false
  464.     # Run Through each text
  465.     text.split(/\s/).each do |word|
  466.       # If String \001 is included
  467.       if word.include?("\001")
  468.         # Slice from String
  469.         word.slice!("\001")
  470.         # Change text color
  471.         word.sub!(/\[([A-F a-f 0-9]+)\]/, '')
  472.         # If matched is not nil and size of match is > 1
  473.         if $1 != nil and $1.size > 1
  474.           # Create Color
  475.           color = Color.new($1[0..1].hex, $1[2..3].hex, $1[4..5].hex)
  476.         else
  477.           # Normal Color
  478.           color = old_font.color
  479.         end
  480.         # Set Color
  481.         last_color = self.font.color.dup
  482.         self.font.color = color
  483.         # Go to next text
  484.         next
  485.       end
  486.       if word.include?("\002")
  487.         # Slice from String
  488.         word.slice!("\002")
  489.         # Change text color
  490.         word.sub!(/\[([0-9])\]/, '')
  491.         # If matched is not nil and size of match is > 1
  492.         if $1 != nil
  493.           # Create Color
  494.           color = text_color($1.to_i)
  495.         else
  496.           # old Color
  497.           color = old_font.color
  498.         end
  499.         # Set Color
  500.         last_color = self.font.color.dup
  501.         self.font.color = color
  502.         # Go to next text
  503.         next
  504.       end
  505.       # If String \003 is included
  506.       if word.include?("\003")
  507.         # Invert Bold Status
  508.         self.font.bold = !self.font.bold
  509.         # Go to next text
  510.         next
  511.       end
  512.       # If String \004 is included
  513.       if word.include?("\004")
  514.         # Invert Italic Status
  515.         self.font.italic = !self.font.italic
  516.         # Go to next text
  517.         next
  518.       end
  519.       # If String \005 is included
  520.       if word.include?("\005")
  521.         # Invert outline
  522.         self.font.shadow = !self.font.shadow
  523.         # Go to next text
  524.         next
  525.       end
  526.       # If String \006 is included
  527.       if word.include?("\006")
  528.         # Invert outline
  529.         outline = !outline
  530.         # Go to next text
  531.         next
  532.       end
  533.       if word.include?("\007")
  534.         # Slice from String
  535.         word.slice!("\001")
  536.         # Change font
  537.         word.sub!(/\[(.*?)\]/, "")
  538.         font = $1.to_s
  539.         if font == ""
  540.           self.font.name = old_font
  541.         else
  542.           self.font.name = font
  543.         end
  544.         # go to next text
  545.         next
  546.       end
  547.       if word.include?("\008")
  548.         # Slice from String
  549.         word.slice!("\007")
  550.         word.sub!(/\[([0-9]+)\]/, "")
  551.         index = $1.to_i
  552.         item = $data_armors[index]
  553.         # draw the icon
  554.         icon = RPG::Cache.icon(item.icon_name)
  555.         self.blt(cx + 4, cy + 4, icon, Rect.new(0, 0, 24, 24))
  556.         cx += 28
  557.         next
  558.       end
  559.       if word.include?("\009")
  560.         # Slice from String
  561.         word.slice!("\008")
  562.         word.sub!(/\[([0-9]+)\]/, "")
  563.         index = $1.to_i
  564.         item = $data_items[index]
  565.         # draw the icon
  566.         icon = RPG::Cache.icon(item.icon_name)
  567.         self.blt(cx + 4, cy + 4, icon, Rect.new(0, 0, 24, 24))
  568.         cx += 28
  569.         next
  570.       end
  571.       if word.include?('\010')
  572.         # Slice from String
  573.         word.slice!("\009")
  574.         word.sub!(/\[([0-9]+)\]/, "")
  575.         index = $1.to_i
  576.         item = $data_skills[index]
  577.         # draw the icon
  578.         icon = RPG::Cache.icon(item.icon_name)
  579.         self.blt(cx + 4, cy + 4, icon, Rect.new(0, 0, 24, 24))
  580.         cx += 28
  581.         next
  582.       end
  583.       if word.include?('\\011')
  584.         # Slice from String
  585.         word.slice!("\010")
  586.         word.sub!(/\[([0-9]+)\]/, "")
  587.         index = $1.to_i
  588.         item = $data_weapons[index]
  589.         # draw the icon
  590.         icon = RPG::Cache.icon(item.icon_name)
  591.         self.blt(cx + 4, cy + 4, icon, Rect.new(0, 0, 24, 24))
  592.         cx += 28
  593.         next
  594.       end
  595.       # If String \012 is included
  596.       if word.include?('\012')
  597.         # Move to new line
  598.         cy = cy + line_height
  599.         # Move to start
  600.         cx = 0
  601.         # Go to next text
  602.         next
  603.       end
  604.       if word.include?('\013')
  605.         # set color to last used
  606.         self.font.color = last_color
  607.         next
  608.       end
  609.       # Get Word Width
  610.       word_width = text_size("#{word} ").width
  611.       word_height = text_size("#{word} ").height
  612.       line_height = [line_height, word_height].max
  613.       # If can't fit on line
  614.       if (cx + word_width) > width
  615.         # Move to new line
  616.         cy = cy + line_height
  617.         # Move to start
  618.         cx = 0
  619.       end
  620.       # Increase by Word Width
  621.       cx += word_width
  622.       self.font = old_font
  623.     end
  624.     return cy + line_height
  625.   end
  626.   #--------------------------------------------------------------------------
  627.   # * Get Text Color
  628.   #     n : text color number (0-7)
  629.   #--------------------------------------------------------------------------
  630.   def text_color(n)
  631.     case n
  632.     when 0
  633.       return Color.new(255, 255, 255, 255)
  634.     when 1
  635.       return Color.new(128, 128, 255, 255)
  636.     when 2
  637.       return Color.new(255, 128, 128, 255)
  638.     when 3
  639.       return Color.new(128, 255, 128, 255)
  640.     when 4
  641.       return Color.new(128, 255, 255, 255)
  642.     when 5
  643.       return Color.new(255, 128, 255, 255)
  644.     when 6
  645.       return Color.new(255, 255, 128, 255)
  646.     when 7
  647.       return Color.new(192, 192, 192, 255)
  648.     else
  649.       normal_color
  650.     end
  651.   end
  652. end
Expand to see the code.


Instructions

Put below SDK (not required) and the MACL (required), and above any of my scripts.

Method specific (included in comments, but these are more explicit instructions for less-experienced scripters):
Array.average Show
Call using ary.average. Returns the arithmetic mean of all numeric elements of ary.

Array.geommean Show
Call using ary.geommean. Returns the geometric mean of all positive numeric elements of ary.

Array.harmmean Show
Call using ary.harmmean. Returns the harmonic mean of all positive numeric elements of ary.

Array.permute Show
Call using ary.permute. Returns a random permutation of [i]ary.

Array.permute! Show
Call using ary.permute. Replaces the contents of ary with a random permutation of ary and returns the non-permuted version of ary.

Bitmap.draw_formatted_text_jeff Show
Call using btmp.draw_formatted_text_jeff(x, y, width, height, text). x and y are the x and y coordinates of the top left corner of the text block. width is the width of the text block (the text is wrapped, left aligned). text is the text to be drawn. You can use the following codes:
  • \c[0xrrggbb] or \c[n] -> changes the color of the text to a hex value or the font color n (same colors as the [Show Text] event command)
  • \c[last] -> changes the color of the text to the last color used.
  • \b -> toggles bold
  • \i -> toggle italics
  • \s -> toggles text shadow
  • \o -> toggles text outline
  • \font[name] -> changes font
  • \oa[n] -> displays armor icon and name
  • \oi[n] -> displays item icon and name
  • \os[n] -> displays skill icon and name
  • \ow[n] -> displays weapon icon and name

Bitmap.formatted_text_height_jeff Show
Call using btmp.formatted_text_height_jeff(width, text). width is the width of the text block (the text is wrapped, left aligned). text is the text to be drawn. You can use the same codes as in Bitmap.draw_formatted_text_jeff

JeffUtilities.normal Show
Call using JeffUtilities.normal(mean, stdv). Returns a random number drawn from the normal distribution with mean = mean and standard deviation = stdv. Will always return a float, so script accordingly. If called without arguments, will assume a standard normal deviation (mean = 0, stdv = 1).

JeffUtilities.pseudorand Show
Call using JeffUtilities.pseudorand(max). Returns a pseudorandom number between 0 and max. If called without arguments, will return a random float between 0 and 1, inclusive. The seed is set when $game_system is initialized, updated when JeffUtilities.pseudorand() is called, and can be accessed via $game_system.pseudorandom_seed

JeffUtilities.permutation Show
Call using JeffUtilities.permutation(array). Returns a random permutation of array.



Compatibility

Unless you have a class named "JeffUtilities," or methods in Bitmap called "draw_formatted_text_jeff" or "formatted_text_height_jeff" (which would, quite frankly, be weird), I foresee no problems.

Credits and Thanks

The Bitmap.draw_formatted_text_jeff method is based on Trickster's draw_formatted_text in the MACL and uses codes inspired by ccoa's UMS.

Author's Notes

I'll be updating this as I come up with new or better methods; anything that requires this script will specify the version number.

Terms and Conditions

Feel free to use these scripts in games however you like, with or without attribution. If you redistribute it without significant modifications, however, please leave the starting comment (the bit between "=begin" and "=end") as is and link back to this page.
Oh hey, that's great. I've been looking for a way to do better randomisation for a while now.
Updated to v1.1.

New methods:
  • Array.average: returns the arithmetic mean (average) of all numeric elements of the array.
  • Array.geommean: returns the geometric mean of all positive numeric elements of the array.
  • Array.harmmean: returns the harmonic mean of all positive numeric elements of the array.
  • Array.permute: returns a random permutation of the array.
  • Array.permute!: permutes self.
  • Bitmap.formatted_text_height_jeff: determines the height of formatted text with a certain width.

Deprecated methods:
  • JeffUtilities.permutation: Replaced by Array.permute

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=74642