Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #==============================================================================
- #
- #
- # L'James Notetag System v.1.1
- #
- #
- #==============================================================================
- # Changelog
- #==============================================================================
- #
- # 23.IX.15 - v.1.0 finished
- # 29.IX.15 - parameters with underscore in name can parse tags with space
- # hash.set_limit changed to hash_set_key_limit, add set_limit to
- # value, array and hash parameters
- # 30.IX.15 - shared parsers bugfix
- # 01. X.15 - array unique bugfix. Unique is now true by default
- # 02. X.15 - rewrite internal scheme structure. New scheme types use
- # another methods. Add block parameters
- # 06. X.15 - another unique bugfix
- # 14. I.16 - fix bug with battle test
- #
- #==============================================================================
- # Description
- #==============================================================================
- #
- # This scrit serves to simplify work with notetags in custom scripts.
- #
- # Features:
- # * Executing specifed action when note matches with regex
- # * Automatic receiving data from notetags
- #
- #==============================================================================
- # Introduction
- #==============================================================================
- #
- # Warning: very bad english. I'll correct mistakes if you show me them
- #
- # Main idea of the system - automatic notetag scanning and converting into
- # values, called parameters, which can be received later through get_param
- #
- # There are five types of parameters, though you can create your own
- # * Values - just a single value of certain type, e.g. integer.
- # Default tag is <name value>
- # * Arrays - array of values. Default tag: <name val1, val2, val3>
- # * Hashes - collection of key-value pairs. Default tag:
- # <name key1 val1, key2 val2>
- # * Traits - boolean values. Notetag presence assigns 'true' value to trait,
- # absence - 'false'. Default tag: <<name>>
- # * Groups - groups of parameters that will be recorded in one Hash
- #
- # In order to scan notetags, system requires to create system extension,
- # which is used for styling and registering parameter schemes (scanning rules)
- # for specific database classes. This is done through simple DSL.
- #
- # Extension looks like this
- #
- # --------------------------------------------------------------------------
- # LJ::NOTETAGS.extension do
- #
- # # Extension styling
- # trait_parser create_parser.add_anon_matcher("[%s]"){|x| true}
- #
- # # Parameter schemes' registraion
- #
- # declare_feature do
- #
- # register :power, hash(:percent).limit(:fire, :ice, :lightning)
- #
- # # Registers notetags like <power fire 150%> - hash parameter.
- # # After loading obj.power[:fire] == 1.5 will be true
- # # Another way:
- # # hash(:percent).set_limit(:fire, :ice, :lightning).register(:power)
- #
- # end
- #
- # declare_skill do
- # register :piercing, trait
- # end
- #
- # declare(RPG::Actor, RPG::Enemy) do
- # register :undead, trait
- # end
- # end
- # -------------------------------------------------------------------------
- #
- # In order to get parameter value you need to use obj.get_param(:param_name)
- # or just obj.param_name, where obj - is a instance of database class.
- #
- # Parameters can be used in a way like RPG Maker VXA features - i.e.,
- # battler equipment and state parameters are applied to that battler.
- # To receive parameter value you need to use battler.get_feature(:feature_name)
- # or just battler.feature_name, where battler - is a Game_Battler instance
- #
- #==============================================================================
- # Styling
- #==============================================================================
- #
- # Extension style applies to all parameters in that extension. For example,
- # in sample extension above traits are set to look like [name] instead of
- # <<name>>. Styling is done through these methods:
- #
- # * param_parser - sets regex via parser(see below) that will be used for
- # value, hash and array parameters.
- # Default: <name value> and <name> \n value \n <name>
- # * trait_parser - sets regex via parser that will be used for traits
- # Default: <<name>>
- # * arg_selector - sets regex that will be used by hash and array schemes
- # for selecting arguments from notetag. Has one capture group
- # By default, argument is everything but line separators
- # and comma: arguments of <name val1, val2, val 3> are
- # "val1", "val2" and "val3"
- # * hash_splitter - sets regex that will be used by hashes to divide key and
- # value in argument string. Has two capture groups, first
- # captures key, second - value.
- # By default split happens on first space: "ab cde fgh"
- # will make pair :ab => "cde fgh"
- #
- # Example: Suppose we need to parse tag <hash a aaa, b bb b, c cccc cccc>. From
- # "a aaa, bb bb b, c cccc cccc" selector selects arguments "a aaa", "bb bb b"
- # ΠΈ "c cccc cccc". Then splitter splits each argument to key and value
- # "a" -> "aaa", "bb" -> "bb b", "c" -> "cccc cccc". After that key-value pairs
- # are processed by converters and accumulators(see below), and result is hash
- # {:a => "aaa", :bb => "bbb", :c => "cccc cccc"}
- #
- # Parsers
- # Parsers are used to parse notes. Parser - is a collection of regex and code
- # blocks that create a value string from MatchData. You can create your parser
- # with create_parser, and then add regexes with next methods:
- # * add_matcher(regex){matcher} adds detection of a fixed regex. Matcher takes
- # MatchData as argument and returns string which will be used by converter
- # * add_anon_regex(regex_str){matcher} adds detection of a regex with variable
- # param name. regex_str is a string which will be converted to a regex after
- # replacing symbols %s with parameter name
- # These methods return self, so they can be changed
- #
- # Styling example:
- #
- # trait_parser create_parser.add_anon_matcher("[%s]"){|x| true}.
- # add_anon_matcher("\"%s\""){|x| true}
- # # This parser detects traits [name] and "name"
- #
- # hash_splitter /^([^\s]+)\s*=>\s*(.*?)&/
- # Hashes will look like <power fire => 150%>
- #
- #==============================================================================
- # Registration
- #==============================================================================
- #
- # Parameters need to be registered in a system for specified classes.
- # To start class parameters declaration you need to use method
- #
- # declare(*classes) do
- # # scheme registraion
- # end
- #
- # *classes - is a list of classes where notetags will be searched. Classes
- # need to have mehod .note in order to work
- #
- # Also database classes have special methods: declare_actor, declare_class,
- # declare_item etc. Methods declare_usableitem, declare_equipitem and
- # declare_baseitem register schemes in several classes.
- # declare_features allows to register parameter that will behave similar to
- # RPG Maker VXA features - e.g. hero parameters depend on his equipment
- #
- # Scheme registration is done like this
- # register name, scheme
- # or
- # scheme.register(name)
- #
- # If name has underscore symbol, it will be interpreted as both underscore
- # and space
- #
- # Scheme - is a rule for notetag parsing. To create scheme you need to call
- # methods value(), array(), hash() or trait(), and then customize scheme.
- # value, array and hash methods can be called with parameter template -
- # like value(:int) - to get partially customized scheme
- #
- # Also you can create your parameter type. In order to do this you need
- # to inherit class LJ::NOTETAGS::SchemeBase or LJ::NOTETAGS::ParamScheme
- # * In SchemeBase you need to override methods:
- # ** load(note) which takes note and returns parameter value.
- # ** accumulate(a,b) takes two values of your parameter and returns
- # new value - "product" of that vaues.
- # * ParamScheme in constructor takes loader (self can be passed in
- # declaration), parser and default value, which will be returned in absence
- # of a notetag. You need to override methods:
- # ** construct(str) - takes string result of parser and constructs object
- # from that string
- # ** accumulate(a,b) - ...
- #
- # Also, besides schemes, you can register regex and action that will be
- # exexuted when regex is detected. This is done with
- # register_regex(regex){action} method. Action takes MatchData and current
- # object
- #
- #==============================================================================
- # Scheme customization
- #==============================================================================
- #
- # Converter - a function that takes string and returns parameter value
- #
- # Accumulator - a function that takes current parameter value and new parameter
- # value and returns chhanged parameter value. Accumulator is used in cases when
- # there're multiple notetags for that parameter in one object
- #
- # To customize standard type parameters use following methods, different for
- # each scheme type. Methods return self and can be chained
- #
- # Value:
- # * set_converter - sets converter. Default leaves value as string
- # * set_accumulator - sets accumulator. Default replaces current value with new
- # * set_method_accumulator - sets accumulator {|acc, val| acc.m(val)} where m -
- # is a method name that is passed as argument
- # * set_default - sets value, that will be saved in absence of notetag in note
- # * set_startval - sets value that accumulator will use as current in first
- # value assignment. E.g. integer parameter with startval=1 and default = 0
- # will always add 1 to value, but return zero when no tags is found. When nil
- # startval is used (default startval value), first notetag just saves value
- # without accumulation
- # * set_limit - set list of values which will be accepted. When nil
- # (default) limit is removed
- #
- # Example
- # value.set_default(0).set_converter{|x| x.to_i}.set_method_accumulator(:+)
- # # summed integer parameter. Equivalent to value(:int)
- #
- # Array:
- # * set_converter - sets converter for array elements. Default leaves value as
- # string
- # * set_unique - when true, repeating values won't be saved. Default is true
- # * set_limit - set list of values which will be accepted. When nil
- # (default) limit is removed
- #
- # Example
- # array.set_converter{|x| x.to_sym}.set_unique
- # # array with unique symbol values
- #
- # Hash:
- # * set_key_converter - set converter for keys. By default keys convert to
- # symbols
- # * set_key_limit - set list of values which will be accepted as key. When nil
- # (default) limit is removed
- # * Value, returned by hash[key] behaves like value parameter, returned by
- # get_param, and is customized by the same methods
- # ** set_converter
- # ** set_accumulator
- # ** set_method_accumulator
- # ** set_default
- # ** set_startval
- # ** set_limit
- #
- # Example
- # hash.set_key_limit(:fire :ice, :lightning).set_val_default(1).
- # set_val_converter{|x| x.chomp("%").to_i/100.0}.
- # set_val_method_accumulator(:*)
- # # sets parameter with keys "ice", "fire", "lightning" and works like
- # # hash(:percent)
- #
- # Traits cannot be customized
- #
- # Groups:
- # Groups are different in that they are defined with special syntax:
- #
- # register :name, group do
- # #register group parameters
- # end
- # or
- # register :name, group.declare{ #register group parameters }
- # or
- # group.declare{ #register group parameters }.register(:name)
- #
- # Before do or declare you can use extension styling methods to change
- # style in group. These methods can be chained
- #
- # Example:
- # register :gr, group.hash_splitter(/^([^\s]+)\s*=>\s*(.*?)&/) do
- # register :a1, value(:int)
- # register :a2, value(:int)
- # end
- #
- # With this scheme notetag <gr><a1 10><a2 20></gr> will be saved as
- # :gr => {:a1 => 10, :a2 => 20}
- #
- # Warning: do not use same names inside and outside of group. System has no
- # way to distinguish between them. Also be careful with styling
- #
- #==============================================================================
- # Parameter templates
- #==============================================================================
- #
- # Value, array and hash parameters can be created using templates to get
- # pre-customized scheme with ready converters, accumulators (for hashes and
- # value parameters) and defaults.
- #
- # To create templated parameters you need to specify it after constructor,
- # like hash(:int)
- #
- # * :int - integer values. Default value is 0, converter does to_i, accumulator
- # sums values
- # * :str - string values. Default is "", accumulator concatenates strings but
- # divides with space
- # * :sym - symbol values. Default is nil, accumulator replaces old with new
- # * :percent - converter chops "%" if exist, makes to_i and divides by 100.0
- # "150%" becomes 1,5. Default is 1, accumulator multiplies values
- # * :pluspercent - converter's the same as :percent, but accumulator sums
- # percents instead of multiplying. Two +50% tags will make 2.0, not 2.25 or
- # 0.25
- #
- #==============================================================================
- # Terms of use
- #==============================================================================
- #
- # You can use L'JNT in commercial and non-commercial projects as long as you
- # give credit to L'James. Custom scripts that use this system, can have wtheir
- # own terms.
- #
- #==============================================================================
- # Some black magic. Better don't touch
- #==============================================================================
- module LJ
- module NOTETAGS
- @exts = []
- def self.extension(&block)
- @exts << Extension.new(block)
- end
- def self.get_exts
- @exts
- end
- class Extension
- def initialize(reg_block)
- @blocks = {}
- instance_eval(®_block)
- end
- def get_trait_parser
- @trait_parser ||= Parser.new.add_anon_matcher("<<%s>>"){|x| true}
- end
- def get_param_parser
- @param_parser ||= Parser.new.add_anon_matcher("<%s\\s+(.+?)>"){|x| x[0]}.
- add_anon_matcher("<(%s)>\\n?(.+?)\\n?<\/\\1>", Regexp::MULTILINE){|x| x[1]}
- end
- def get_arg_selector
- @arr_sel ||= /\s*?([^\n\r,]+)\s*?/
- end
- def get_hash_splitter
- @splitter ||= /^([^\s]+)\s+(.+?)$/
- end
- def get_blocks
- @blocks
- end
- def create_parser
- Parser.new
- end
- def trait_parser(parser)
- @trait_parser = parser
- end
- def param_parser(parser)
- @param_parser = parser
- end
- def arg_selector(selector)
- @arg_sel = selector
- end
- def hash_splitter(splitter)
- @splitter = splitter
- end
- def declare(*classes, &block)
- classes.each do
- |klass|
- Manager.add_class(klass)
- @blocks[klass] ||= []
- @blocks[klass] << block
- end
- end
- def declare_baseitem(&block)
- declare(RPG::Actor, RPG::Class, RPG::Skill, RPG::Item,
- RPG::Weapon, RPG::Armor, RPG::Enemy, RPG::State, &block)
- #data_groups($data_actors, $data_classes, $data_skills, $data_items
- # $data_weapons, $data_armors, $data_states, $data_enemies)
- end
- def declare_features(&block)
- declare(RPG::Actor, RPG::Class,
- RPG::Weapon, RPG::Armor, RPG::Enemy, RPG::State, &block)
- #data_groups($data_actors, $data_classes
- # $data_weapons, $data_armors, $data_states, $data_enemies)
- end
- def declare_actor(&block)
- declare(RPG::Actor, &block)
- #data_groups($data_actors)
- end
- def declare_class(&block)
- declare(RPG::Class, &block)
- #data_groups($data_classes)
- end
- def declare_usableitem(&block)
- declare(RPG::Item, RPG::Skill, &block)
- #data_groups($data_items, $data_skills)
- end
- def declare_skill(&block)
- declare(RPG::Skill, &block)
- #data_groups($data_skills)
- end
- def declare_item(&block)
- declare(RPG::Item, &block)
- #data_groups($data_items)
- end
- def declare_equipitem(&block)
- declare(RPG::Weapon, RPG::Armor, &block)
- #data_groups($data_weapons, $data_armors)
- end
- def declare_weapon(&block)
- declare(RPG::Weapon, &block)
- #data_groups($data_weapons)
- end
- def declare_armor(&block)
- declare(RPG::Armor, &block)
- #data_groups($data_armors)
- end
- def declare_enemy(&block)
- declare(RPG::Enemy, &block)
- #data_groups($data_enemies)
- end
- def declare_state(&block)
- declare(RPG::State, &block)
- #data_groups($data_states)
- end
- end #Extension
- #-------------------------------------------------------------------
- class Parser
- attr_accessor :matchers
- attr_accessor :anon_matchers
- def initialize
- @matchers = {}
- @anon_matchers = {}
- end
- def add_matcher(regex, &matcher)
- @matchers[regex] = matcher
- self
- end
- def add_anon_matcher(str, rules = 0, &matcher)
- @anon_matchers[str] = [rules, matcher]
- self
- end
- def deanon(name)
- name = name.to_s
- @anon_matchers.each{|key, val|
- add_matcher(Parser.make_regex(key, name, val[0]), &val[1])
- }
- if name.tr!('_', ' ')
- @anon_matchers.each{|key, val|
- add_matcher(Parser.make_regex(key, name, val[0]), &val[1])
- }
- end
- @anon_matchers.clear
- end
- def load(note)
- @matchers.collect{ |regex, matcher|
- note.scan(regex).collect{|x| matcher.call(x)}
- }.flatten
- end
- def self.make_regex(str, name, options = 0)
- Regexp.compile(sprintf(str, name), options)
- end
- def copy
- parser = Parser.new
- parser.matchers = @matchers.clone
- parser.anon_matchers = @anon_matchers.clone
- parser
- end
- end
- #===================================================================#
- Identical = Proc.new{|x| x}
- class SchemeBase
- def load(note)
- end
- def construct(a, b)
- end
- end
- class ParamScheme < SchemeBase
- attr_reader :parser
- attr_accessor :default
- attr_accessor :startval
- def construct(acc, value)
- end
- def initialize(parser, loader, default = nil, startval = nil)
- @parser = parser
- @loader = loader
- @default = default
- @startval = startval
- end
- def load(note)
- values = @parser.load(note).collect{|x| construct(x)}.compact
- return @default if values.empty?
- if @startval != nil
- values.inject(@startval){|acc, val| accumulate(acc, val) }
- else
- values.inject{|acc, val| accumulate(acc, val) }
- end
- end
- def register(name)
- @loader.register(name, self)
- end
- end
- #---------------------------------------------------------------
- class ValueParam < ParamScheme
- def initialize(parser, loader)
- super(parser, loader)
- @converter = Identical
- @accumulator = Proc.new{|acc, x| x}
- @shared_acc = nil
- @limit = nil
- end
- def set_default(dfl)
- self.default = dfl
- self
- end
- def set_startval(st)
- self.startval = st
- self
- end
- def set_limit(*limit)
- if limit.first == nil
- @limit = nil
- else
- @limit = limit
- end
- self
- end
- def set_converter(&converter)
- @converter = converter
- self
- end
- def set_accumulator(&acc)
- @accumulator = acc
- self
- end
- def set_method_accumulator(m)
- set_accumulator{
- |acc, val|
- acc.method(m).call(val)
- }
- end
- def construct(value)
- value = @converter.call(value)
- if (@limit == nil || @limit.include?(value))
- value
- else
- nil
- end
- end
- def accumulate(a,b)
- @accumulator.call(a, b)
- end
- end
- #------------------------------------------------------------------
- class ArrayParam < ParamScheme
- def initialize(parser, loader, selector)
- super(parser, loader, [])
- self.startval = self.default
- @selector = selector
- @converter = Proc.new{|x| x}
- @unique = true
- @limit = nil
- end
- def set_converter(&converter)
- @converter = converter
- self
- end
- def set_unique(unique)
- @unique = unique
- self
- end
- def set_limit(*limit)
- if limit.first == nil
- @limit = nil
- else
- @limit = limit
- end
- self
- end
- def construct(val)
- res = val.scan(@selector).collect do
- |x|
- value = @converter.call(x[0].strip)
- if (@limit == nil || @limit.include?(value))
- value
- else
- nil
- end
- end.compact
- res.uniq! if @unique
- res
- end
- def accumulate(a,b)
- if (@unique) then
- a | b
- else
- a + b
- end
- end
- end
- #---------------------------------------------------------------
- class HashParam < ParamScheme
- def initialize(parser, loader, selector, splitter)
- super(parser, loader, {})
- self.startval = self.default
- @kconv = Proc.new{|v| v.to_sym}
- @vconv = Identical
- @selector = selector
- @splitter = splitter
- @vacc = Proc.new{|acc, v| v}
- @vdefault = nil
- @limit = nil
- @klimit = nil
- @vstartval = nil
- end
- def set_key_limit(*limit)
- if limit.first == nil
- @klimit = nil
- else
- @klimit = limit
- end
- self
- end
- def set_key_converter(&converter)
- @kconv = converter
- self
- end
- def set_default(default)
- @vdefault = default
- self.default.default = default #WTH
- self
- end
- def set_startval(startval)
- @vstartval = startval
- self
- end
- def set_limit(*limit)
- if limit.first == nil
- @limit = nil
- else
- @limit = limit
- end
- self
- end
- def set_converter(&converter)
- @vconv = converter
- self
- end
- def set_accumulator(&acc)
- @vacc = acc
- self
- end
- def set_method_accumulator(m)
- set_accumulator{
- |acc, val|
- acc.method(m).call(val)
- }
- end
- def construct(rawval)
- acc = {}
- rawval.scan(@selector).each{ |l|
- m = l[0].strip.match(@splitter)
- if m
- key = @kconv.call(m[1])
- val = @vconv.call(m[2])
- if (@klimit==nil || @klimit.include?(key)) &&
- (@limit == nil || @limit.include?(val))
- unless acc.has_key? key || @vstartval != nil
- acc[key] = val
- else
- acc[key] = @vacc.call(acc.has_key?(key) ? acc[key] : @vstartval,
- val)
- end
- end
- end
- }
- acc
- end
- def accumulate(a,b)
- a = a.clone
- b.each do
- |key, value|
- if a.has_key?(key)
- a[key] = @vacc.call(a[key], value)
- else
- a[key] = value
- end
- end
- a
- end
- end
- # --------------------------------------------------------------------
- class Trait < ParamScheme
- def initialize(parser, loader)
- super(parser, loader, false)
- end
- def construct(val)
- val ? true : false
- end
- def accumulate(a,b)
- a || b
- end
- end
- # --------------------------------------------------------------------
- class GroupSchemeBase
- attr_reader :parser
- def initialize(parser, loader)
- @parser = parser
- @loader = loader
- @ext = Extension.new(Proc.new{})
- end
- def set_trait_parser(p)
- @ext.trait_parser(p)
- self
- end
- def set_param_parser(p)
- @ext.param_parser(p)
- self
- end
- def set_arg_selector(p)
- @ext.arg_selector(p)
- self
- end
- def set_hash_splitter(p)
- @ext.hash_splitter(p)
- self
- end
- def declare(&block)
- GroupParam.new(@parser, @loader, @ext, block)
- end
- end
- class GroupParam < SchemeBase
- attr_reader :parser
- def initialize(parser, loader, ext, block)
- @parser = parser
- @loader = loader
- @group_loader = Loader.new(ext, block)
- end
- def load(note)
- @parser.load(note).inject({}) do
- |acc, val|
- accumulate(acc, @group_loader.load_params(val, {}))
- end
- end
- def accumulate(a, b)
- a = a.clone
- shac = @group_loader.shared_accs
- b.each do
- |key, value|
- if a.has_key?(key)
- a[key] = shac[key].call(a[key], value)
- else
- a[key] = value
- end
- end
- a
- end
- def register(name)
- @loader.register(name, self)
- end
- end
- #====================================================================#
- class Loader
- attr_reader :shared_accs
- def initialize(ext, reg_block)
- @ext = ext
- @actions = {}
- @params = {}
- #ext.method(m).call(self)
- instance_eval ®_block
- @shared_accs = {}
- @params.each do
- |key, value|
- @shared_accs[key] = value.method(:accumulate)
- end
- end
- def register_regex(regex, &action)
- @actions[regex] = action
- end
- def register(name, scheme, &block)
- scheme.parser.deanon(name) if scheme.respond_to?(:parser)
- if block_given? && scheme.respond_to?(:declare)
- @params[name] = scheme.declare(&block)
- else
- @params[name] = scheme
- end
- end
- def value(type = nil)
- res = ValueParam.new(@ext.get_param_parser.copy, self)
- case type
- when :int
- res.set_converter{|x| x.to_i}.set_method_accumulator(:+).
- set_default(0).set_startval(0)
- when :str
- res.set_accumulator{|acc, val| "#{acc} #{val}"}.set_default("")
- when :sym
- res.set_converter{|x| x.to_sym}
- when :percent
- res.set_converter{|x| x.chomp("%").to_i/100.0}.
- set_method_accumulator(:*).set_default(1).set_startval(1)
- when :pluspercent
- res.set_converter{|x| 1+x.chomp("%").to_i/100.0}.
- set_accumulator{|acc, val| acc+val - 1}.set_default(1).
- set_startval(1)
- end
- res
- end
- def array(type = nil)
- res = ArrayParam.new(@ext.get_param_parser.copy, self,
- @ext.get_arg_selector)
- case type
- when :int
- res.set_converter{|x| x.to_i}
- when :str
- when :sym
- res.set_converter{|x| x.to_sym}
- when :percent
- res.set_converter{|x| x.chomp("%").to_i/100.0}
- when :pluspercent
- res.set_converter{|x| 1+x.chomp("%").to_i/100.0}
- end
- res
- end
- def hash(type = nil)
- res = HashParam.new(@ext.get_param_parser.copy, self,
- @ext.get_arg_selector, @ext.get_hash_splitter)
- case type
- when :int
- res.set_converter{|x| x.to_i}.set_method_accumulator(:+).
- set_default(0).set_startval(0)
- when :str
- res.set_accumulator{|acc, val| "#{acc} #{val}"}.set_default("")
- when :sym
- res.set_converter{|x| x.to_sym}
- when :percent
- res.set_converter{|x| x.chomp("%").to_i/100.0}.
- set_method_accumulator(:*).set_default(1).set_startval(1)
- when :pluspercent
- res.set_converter{|x| 1+x.chomp("%").to_i/100.0}.
- set_accumulator{|acc, val| acc+val - 1}.set_default(1).
- set_startval(1)
- end
- res
- end
- def trait
- Trait.new(@ext.get_trait_parser.copy, self)
- end
- def group(&bl)
- res = GroupSchemeBase.new(@ext.get_param_parser.copy, self)
- res = res.declare(&bl) if block_given?
- res
- end
- def empty?
- @actions.empty? && @params.empty?
- end
- def load(obj, holder = {})
- note = obj.note
- @actions.each{|regex, action| note.scan(regex){|m| action.call(m, obj)}}
- load_params(note, holder)
- end
- def load_params(note, holder)
- @params.each do
- |name, param|
- val = param.load(note)
- holder[name] = val unless val == nil
- end
- holder
- end
- end #Loader
- #===============================================================
- class Manager
- @@instances = []
- @classes = []
- def initialize(klass)
- @@instances << self
- @note_class = klass
- @loaders = []
- @shared_accs = {}
- end
- module NoteClass
- def create_ljnt_manager
- @manager = Manager.new(self)
- end
- def note_manager
- @manager
- end
- def load_ljnt(obj)
- note_manager.load(obj)
- end
- def accumulate_param(name, *values)
- note_manager.accumulate_param(name, values)
- end
- def accumulate_param_obj(name, *objs)
- note_manager.accumulate_param(name,
- objs.compact.collect{|obj| obj.get_param(name)}.compact)
- end
- end
- module NoteClassInstance
- def load_ljnt
- @ljntholder = self.class.load_ljnt(self)
- end
- def get_param(name)
- @ljntholder[name]
- end
- def method_missing(method, *opts)
- m = method.to_sym
- res = get_param(m)
- return res unless res == nil
- super
- end
- end
- def self.add_class(note_class)
- unless @classes.include? note_class
- note_class.extend(NoteClass)
- note_class.send(:include, NoteClassInstance)
- note_class.create_ljnt_manager
- @classes << note_class
- end
- end
- def self.classes
- @classes
- end
- def self.register_parsers
- @@instances.each{|x| x.prepare}
- end
- def prepare
- LJ::NOTETAGS.get_exts.each do
- |ext|
- ext.get_blocks.select{|key, val| key == @note_class}.each do
- |key, blocks|
- blocks.each do
- |block|
- loader = Loader.new(ext, block)
- @loaders << loader
- loader.shared_accs.each do
- |key, val|
- @shared_accs[key] = val
- end
- end
- end
- end
- end
- def load(obj)
- @loaders.inject({}){|holder, loader| loader.load(obj, holder)}
- end
- def accumulate_param(name, values)
- acc = @shared_accs[name]
- if acc
- values.inject(&acc)
- else
- values.last
- end
- end
- end # MANAGER
- end #NOTETAGS
- end #LJ
- module DataManager
- class <<self; alias init_ljnt init; end
- def self.init
- if $BTEST
- $BTEST = false
- init_ljnt
- load_ljnotetags
- $BTEST = true
- setup_battle_test
- else
- init_ljnt
- load_ljnotetags
- end
- end
- def self.load_ljnotetags
- LJ::NOTETAGS::Manager.register_parsers
- LJ::NOTETAGS::Manager.classes.each do
- |x|
- ObjectSpace.each_object x do
- |obj|
- obj.load_ljnt unless obj.nil?
- end
- end
- end
- end
- class Game_Battler < Game_BattlerBase
- def get_feature(name)
- end
- def method_missing(method, *opts)
- m = method.to_sym
- res = get_feature(m)
- return res unless res == nil
- super
- end
- end
- class Game_Actor < Game_Battler
- def get_feature(name)
- RPG::Actor.accumulate_param_obj(name,
- self.class,
- actor,
- *equips,
- *states
- )
- end
- end
- class Game_Enemy < Game_Battler
- def get_feature(name)
- RPG::Enemy.accumulate_param_obj(name,
- enemy,
- *states
- )
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement