=begin #=============================================================================== Title: Utils Party Level Author: Hime Date: Jul 24, 2013 -------------------------------------------------------------------------------- ** Change log Jul 24, 2013 - Initial release -------------------------------------------------------------------------------- ** Terms of Use * Free to use in non-commercial projects * Contact me for commercial use * No real support. The script is provided as-is * Will do bug fixes, but no compatibility patches * Features may be requested but no guarantees, especially if it is non-trivial * Credits to Hime Works in your project * Preserve this header -------------------------------------------------------------------------------- ** Description This script provides several utility functions for working with party levels. You can get information such as Leader level Max level Min level Median level Mode level Mean level -------------------------------------------------------------------------------- ** Installation Place this script below Materials and above Main -------------------------------------------------------------------------------- ** Usage The following methods are available to Game_Party objects. leader_level - returns the level of the current party's leader max_level - returns the highest level in the party min_level - returns the lowest level in the party mode_level - returns the level that occurs the most frequent mean_level - returns the average level median_level - returns the meadian level Note that these methods operate on all members, not just the active battle members. If you want it to check whether you are in battle or not, set All_Member_Mode to false. #=============================================================================== =end $imported = {} if $imported.nil? $imported["TH_UtilsPartyLevel"] = true #=============================================================================== # ** Configuration #=============================================================================== module TH module Utils_Party_Level # If this is true, then by default all calculations are based on all party # members. If set to false, then when you are in battle, only battle # members will be considered All_Member_Mode = true end end #=============================================================================== # ** Rest of Script #=============================================================================== class Game_Party < Game_Unit include TH::Utils_Party_Level def member_levels(include_all) if include_all all_members.collect{|mem| mem.level} else members.collect{|mem| mem.level} end end #----------------------------------------------------------------------------- # Returns the level of the current leader #----------------------------------------------------------------------------- def leader_level self.leader.level end #----------------------------------------------------------------------------- # Returns the highest level #----------------------------------------------------------------------------- def max_level(include_all=All_Member_Mode) member_levels(include_all).max end #----------------------------------------------------------------------------- # Returns the lowest level #----------------------------------------------------------------------------- def min_level(include_all=All_Member_Mode) member_levels(include_all).min end #----------------------------------------------------------------------------- # Returns the level that occurs the most #----------------------------------------------------------------------------- def mode_level(include_all=All_Member_Mode) member_levels(include_all).group_by{|i| i}.max{|x,y| x[1].length <=> y[1].length}[0] end #----------------------------------------------------------------------------- # Returns the mean level of the party members. # mean = sum(levels) / num_levels #----------------------------------------------------------------------------- def mean_level members.inject(0.0) {|sum, mem| sum + mem.level} / members.size end #----------------------------------------------------------------------------- # Returns the median level of the party members. # If the number of members is even, take the average of the two elements in # the middle #----------------------------------------------------------------------------- def median_level(include_all=All_Member_Mode) arr = member_levels(include_all).sort len = arr.size len % 2 == 1 ? arr[len / 2] : (arr[len / 2 - 1] + arr[len/2]) / 2.0 end end