- Joined
- Jun 22, 2014
- Messages
- 11
- Reaction score
- 7
- First Language
- English
- Primarily Uses
Paramecium Core
This is my core script, used by my other scripts. It contains classes and methods that may be useful to other script writers. It requires Paramecium Save Extensions.
The Script
################################################## ## Paramecium Core V 1 ## August 22, 2015 ## ##################################################=beginThis is my core script, required for some of my other scripts. You may use the classes and methods in it in your scripts as long as you give me credit.=end($imported ||= {})["Paramecium"] = truemodule Paramecium#Contains some methods #Uses the Box–Muller transform to generate normally distributed random numbers def self.stnRand() return Math.sqrt( -2 * Math.log(rand())) * Math.cos( 2 * Math:
I * rand()) end #Generates normally distributed numbers with a mean of μ and a standard deviation of σ. def self.nRand(μ,σ) return μ + stnRand()* σ end #Performs a "skill check" with a difficulty level of 'difficulty'. #returns true if the player (or whoever is performing the skill check) #passes it and false if they fail. When the skill level is equal to the #difficulty level, there is a fifty percent chance of passing. This #method uses a default standard deviation of 2.5, which was chosen arbitrarily. def self.skillCheck(skillLevel, difficulty,s = 2.5) x = nRand(skillLevel, s) return x >= difficulty end def self.show_selection(array) params = [] choices = [] array.each{|item| choices.push(item[0]) } params.push(choices) params.push(0) params[0].each {|s| $game_message.choices.push(s) } $game_message.choice_proc = Proc.new {|x| $paramecium_choice =(x) } end def self.get_choice(array) array[$paramecium_choice][1] end # This method calls a scene that shows a list of choices for the player and ] # returns an object depending on their choice and the array you put into this # method. The parameter 'array' is an array of the form: #[["string_0",object_0],["string_1",object_1],...,["string_(n-1)",object_(n-1)]]; # with a length of n. The method will display the strings in a choice window, # similar to the one used for events def self.selection(array) show_selection(array) Fiber.yield until not $game_message.busy? get_choice(array) end # This is similar to the above method but allows the user to select multiple values. def self.multipleChoice(array,duplicates=false) array << ["End","End"] results = [] while true r = selection(array) r == "End" ? break : results << r end duplicates ? results : results.uniq || results end def self.invContains?(array,type) case type.downcase[0,1] when "i" getVal = Proc.new {|n| $game_party.item_number($data_items[n])} when "w" getVal = Proc.new {|n| $game_party.item_number($data_weapons[n])} when "a" getVal = Proc.new {|n| $game_party.item_number($data_armors[n])} else return false end array.each {|entry| return false unless getVal.call(entry[0]) >= entry[1] } end def self.changeInv(array,type) case type.downcase[0,1] when "i" change = Proc.new {|n, amount| if n >= 0 $game_party.gain_item($data_items[n], amount) else $game_party.lose_item($data_items[n], amount) end } when "w" change = Proc.new {|n, amount| if n >= 0 $game_party.gain_item($data_weapons[n], amount) else $game_party.lose_item($data_weapons[n], amount) end } when "a" change = Proc.new {|n, amount| if n >= 0 $game_party.gain_item($data_armors[n], amount) else $game_party.lose_item($data_armors[n], amount) end } else return false end array.each {|entry| change.call(entry[0],entry[1]) } end # Returns an array with the specified position(s) in its sub-arrays negated. def self.negatePos(array,pos) r = [] if pos.is_a? Array myCheck = Proc.new {|i| pos.include?(i)} else myCheck = Proc.new {|i| i == pos} end array.each{|entry| entryClone = [] entry.each_index {|i| entryClone.push(myCheck.call(i)? -1*entry : entry) } r.push(entryClone) } r end # Loads a square array of strings from a file. def self.getStrArr(filename,min[=2],max[=4]) return nil unless FileTest.exist?(filename) strArr = [] i = 0 myFile = File.open(filename) myFile.each_line {|line| strArr = line.scan(/\S{min,max}/) i += 1 } return strArr end def self.intParse(str) end EX_ACTION_HASH = { :items => { 17 => 1, 13 => 2 }, :armors => { 1 => 4, 14 => 1 }, :weapons =>{ 18 => 3 }, :SE => ["wind7.ogg", 100, 100], } PROC_ACTIONS = { :items => Proc.new{|items| items.each_pair{|id,n| n > 0 ? $game_party.gain_item($data_items[id], n) : $game_party.lose_item($data_items[id], n) } }, :armors => Proc.new{|armors| armors.each_pair{|id,n| n > 0 ? $game_party.gain_item($data_armors[id], n) : $game_party.lose_item($data_armors[id], n) } }, :weapons => Proc.new{|weapons| weapons.each_pair{|id,n| n > 0 ? $game_party.gain_item($data_armors[id], n) : $game_party.lose_item($data_armors[id], n) } }, :SE => Proc.new {|args| Audio.se_play(args[0], args[1], args[2])} } # A method that does the commands in a hash, the things it does can all be done in events but this is # useful when you want something to be entirely contained in a script (e.g. in the config) or for making # something a bit more dynamic. I originally intended to add more functionality to this but I am currently # working on something that will essentially the same thing but be better def self.doAction(actHash) actHash.each_pair{|key,value| PROC_ACTIONS[key].call(value) if PROC_ACTIONS[key] } end class MultiMap def initialize(hash) @data = hash @length = hash.length @domain = hash.keys end def image(x) return @hash[x] end def preImage(y) arr = Array.new for i in 0..@length-1 if @data[@domain].index(y) arr << @domain end end return arr end def compose(map) #... end def inverse #... end end class Ranking # This implements a priority queue in which each element consists of an # object (e.g a string, an array, or a number [such as an item id]) and a rank # (e.g actor ID and number of skills), in that order. It is initialized from # an array of the form: # [[object-1,rank-1],[object-2,rank-2],...[object-n,rank-n]]. # The nested array [object-i,rank-i] is called an entry. # # The methods 'max' and 'min' return the entries with the highest and lowest # rankings respectively. When two or more entries have the same rank, it # returns the one that has the lowest index in the array {i.e. that comes # first). # The method 'sort!' sorts the data from highest ranked to lowest ranked. If # two or more entries have the same rank, the one that had the # lowest index before 'sort!' will have the lowest index after, with entries # of equal rank following behind. The method 'add' adds another entry to the # Ranking. # The method 'nextR!' returns the entry with the highest rank (in cases of # equal rank, the same rule as above applies) and removes it from the Ranking. # The method 'next!' returns the object in the entry # with the highest rank and removes its entry from the Ranking. # The 'blackjack(n)' method returns the object with the value closest to but # not exceeding n, like in the card game blackjack the goal is to get as close # to 21 without going over. # The 'blackjack!(n)' method does the same thing as 'blackjack' but also # removes entry of the returned object. def initialize(array) @data = array @length = array.length end def min m = @data[0] for i in 1..@length-1 if @data[1]<m[1] m = @data end end return m; end def max() m = @data[0] for i in 1..@length-1 if @data[1]>m[1] m = @data end end return m; end def sort! arr = Array.new for i in 0..@length-1 arr <<self.nextR! end @data = arr end def add(newEntry) if newEntry.is_a?(Array) && newEntry[1].is_a(Numeric) @data << newEntry @length += 1 end end def nextR! m = @data[0] index = 0 for i in 1..@length-1 if @data[1]>m[1] m = @data index = i end end @data.delete_at(index) @length -= 1 return m; end def next! return self.nextR![0] end def blackjack(n) b = @data[0] for i in 1..@length-1 if ((m[1]>n&&@data[1]<=n)||(@data[1]<n&&@data[1]>m[1])) m = @data end end return m end def blackjack!(n) b = @data[0] index = 0 for i in 1..@length-1 if ((m[1]>n&&@data[1]<=n)||(@data[1]<n&&@data[1]>m[1])) m = @data index = i end end @data.delete_at(index) return m end end class EventChannel # This is a class for an event channel. Currently, all event channels are stored in the global variable # '$Global_Events', which is a hash that stores them by name. I am considering making another global # variable to store event channels for the current map and may do so in the future. Any object can subscribe # to an event channel (using the subscribeTo method in the module Subscription). When subscribing, it must # provide the name (as a symbol) of one of its methods. This method will be called whenever the event channel # is published/fires/occurs. The method will be called with the arguments data, which is the data for the event, # and sender, which is the object that sent the event. Any object can publish to any event (using a method in # the module Publishing). def initialize() @subscribers = {} end def subscribe(obj,m) @subscribers[obj] = m if m.is_a?(Symbol) end def unsubscribe(obj) @subscribers[obj] = nil end def publish(data,sender) return unless @subscribers && (not @subscibers.empty?) @subscribers.each_pair{|obj,m| obj.method(m).call(data,sender) } end end # This is the module for subscribing to events. 'include' this in any class you want to make # subscribe to events. To make an object subscribe to an event, call the method 'subscribeTo(event,m)' # where 'event' is the name of the event channel for it to subscribe to (if the event channel # does not exist, it will create a new one.) module Subscription def subscribeTo(event,m) ($Global_Events[event]||=EventChannel.new).subscribe(self,m) end def unsubscribeTo(event) ($Global_Events[event]||=EventChannel.new).unsubscribe(self) end end # This method allows objects to publish events. 'include' this in any class you want to make # publish events. To make an object publish an event, call the method 'publish(event,data)' # where 'event' is the name of the event and 'data' is the data to publish. module Publishing def publish(event,data) return unless $Global_Events[event] $Global_Events[event].publish(data,self) end def anonPublish(event,data) return unless $Global_Events[event] $Global_Events[event].publish(data,nil) end end end# Adds two new methods to the 'Array' classclass Array # Is this array a subset of 'other'? (i.e are all elements of this array # elements of the other array?) def subset?(other) self.each{|item| return false unless other.include?(item) } return true end # Is this array a superset of 'other' (is 'other' a subset of this array)? # (i.e are all elements of the other array elements of this array?) def superset?(other) other.subset?(self) end # Does 'other' have the same elements as 'self' and 'self' the same elements # as 'other'? # [Are the two arrays equal, ignoring order?] def setEquals?(other) self.subset?(other) && self.superset?(other) end unless $imported[:ve_basic_module] def random self[rand(self.length)] end endendSaveExtensions::New_Game_Procs.push(Proc.new{ $Global_Events = {}})SaveExtensions::Save_Procs.push(Proc.new{|contents| contents[:eventChannels] = $Global_Events})SaveExtensions::Load_Procs.push(Proc.new{|contents| $Global_Events = contents[:eventChannels]})
Event Based Programming: Creating a Kill Counter
Here is a script that uses the tools for event based programming in Paramecium Core to to create a kill counter.
class KillCounter# Stored in the global variable $KillCount include Paramecium::Subscription def initialize @count = Hash.new(0) subscribeTo("EnemyDeath",:updateCount) end # Call this method ($KillCount.count(key)) to get the kill count for enemy_id 'key'. def count(key) return @count[key] if key.is_a?(Integer) end # This is the method that it provides when it subscribes to the "EnemyDeath" event channel def updateCount(data,sender) @count[sender.enemy_id] += 1 endend# This makes enemies publish to the event channel "EnemyDeath" when they die.class Game_Enemy < Game_Battler alias oldDie die include Paramecium:
ublishing def die publish("EnemyDeath",nil) oldDie endendSaveExtensions::New_Game_Procs.push(Proc.new{ $KillCount = KillCounter.new})SaveExtensions::Save_Procs.push(Proc.new{|contents| contents[:kilCount] = $KillCount})SaveExtensions::Load_Procs.push(Proc.new{|contents| $KillCount = contents[:kilCount]})
Terms of Use
This script is free to use in any commercial or non-commercial game provided I am given Credit.
This is my core script, used by my other scripts. It contains classes and methods that may be useful to other script writers. It requires Paramecium Save Extensions.
The Script
################################################## ## Paramecium Core V 1 ## August 22, 2015 ## ##################################################=beginThis is my core script, required for some of my other scripts. You may use the classes and methods in it in your scripts as long as you give me credit.=end($imported ||= {})["Paramecium"] = truemodule Paramecium#Contains some methods #Uses the Box–Muller transform to generate normally distributed random numbers def self.stnRand() return Math.sqrt( -2 * Math.log(rand())) * Math.cos( 2 * Math:
Event Based Programming: Creating a Kill Counter
Here is a script that uses the tools for event based programming in Paramecium Core to to create a kill counter.
class KillCounter# Stored in the global variable $KillCount include Paramecium::Subscription def initialize @count = Hash.new(0) subscribeTo("EnemyDeath",:updateCount) end # Call this method ($KillCount.count(key)) to get the kill count for enemy_id 'key'. def count(key) return @count[key] if key.is_a?(Integer) end # This is the method that it provides when it subscribes to the "EnemyDeath" event channel def updateCount(data,sender) @count[sender.enemy_id] += 1 endend# This makes enemies publish to the event channel "EnemyDeath" when they die.class Game_Enemy < Game_Battler alias oldDie die include Paramecium:
Terms of Use
This script is free to use in any commercial or non-commercial game provided I am given Credit.
Last edited by a moderator:


