=begin
#===============================================================================
Title: Conditional Effects
Author: Hime
Date: Mar 16, 2014
URL: http://www.himeworks.com/2014/03/16/conditional-effects-2/
--------------------------------------------------------------------------------
** Change log
Mar 16, 2014
- 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 allows you to assign custom conditions to skill or item effects.
Effects are only executed if they meet the conditions.
Conditions are specified as a formula, so you can create any condition that
you can think of as long as you can write it.
--------------------------------------------------------------------------------
** Installation
Place this script below Materials and above Main
--------------------------------------------------------------------------------
** Usage
To specify a condition for an effect, note-tag skills or items with
FORMULA
Where the ID is the ID of the effect that you want to assign the condition to.
You can determine an effect's ID by counting their position on the list,
so the first effect has ID 1, second effect has ID 2, and so on.
As a shortcut, you can assign a condition to multiple effects by specifying
multiple ID's separated by commas like this
FORMULA
The formula can be anything that returns true or false.
The following formula variables are available
a - user of the action
b - target of the action
i - item used
t - game troop
p - game party
s - game switches
v - game variables
The formula is evaluated within the context of the effect, so any of the
effect's attributes can be referenced as well (eg: data_id, code)
#===============================================================================
=end
$imported = {} if $imported.nil?
$imported[:TH_ConditionalEffects] = true
#===============================================================================
# ** Configuration
#===============================================================================
module TH
module Conditional_Effects
Ext_Regex = /(.*?)<\/effect[-_ ]condition>/im
end
end
#===============================================================================
# ** Rest of Script
#===============================================================================
module RPG
class UsableItem
alias :th_conditional_effects_effects :effects
def effects
load_notetag_conditional_effects unless @effect_conditions_checked
th_conditional_effects_effects
end
def load_notetag_conditional_effects
@effect_conditions_checked = true
effect_conditions = []
results = self.note.scan(TH::Conditional_Effects::Ext_Regex)
results.each do |res|
ids = res[0].split(",").map {|id| id.to_i}
condition = res[1]
cond = Data_EffectCondition.new
cond.condition = condition
ids.each do |id|
self.effects[id-1].effect_condition = cond
end
end
end
end
class UsableItem::Effect
attr_accessor :effect_condition
def condition_met?(user, target, item)
return true unless @effect_condition
@effect_condition.condition_met?(user, target, item)
end
end
end
class Data_EffectCondition
attr_accessor :condition
def initialize
@condition = "true"
end
def condition_met?(a, b, i, p=$game_party, t=$game_troop, s=$game_switches, v=$game_variables)
eval(@condition)
end
end
class Game_Battler < Game_BattlerBase
alias :th_conditional_effects_item_effect_apply :item_effect_apply
def item_effect_apply(user, item, effect)
return unless effect.condition_met?(user, self, item)
th_conditional_effects_item_effect_apply(user, item, effect)
end
end