Advertisement
roninator2

Tsukihime Custom Use Conditions mod

Dec 5th, 2024
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 10.72 KB | None | 0 0
  1. =begin
  2. #===============================================================================
  3.  Title: Custom Use Conditions
  4.  Author: Hime
  5.  Date: Jan 4, 2014
  6.  Updated: Nov. 21, 2021
  7.  Updated By: Roninator2, KBGaming as aid to MasterMoes
  8.  URL: https://himeworks.com/2013/11/26/custom-use-conditions/
  9.  For KBGaming: https://kbgamingmusic.itch.io/
  10. --------------------------------------------------------------------------------
  11.  ** Change log
  12.  Nov. 21, 2021
  13.    - Allows one to use features to add skills and check
  14.      for those skills as conditions
  15.    - Considered a QoL update
  16.    - Archived on RPG Maker Central because the internet is volatile
  17.  Jan 4, 2014
  18.    - allows for "recursive" calls. Recursive calls do not check custom use
  19.      conditions
  20.    - added "Actor" condition
  21.  Nov 29, 2013
  22.    - fixed bug where no use conditions caused it to always fail
  23.  Nov 26, 2013
  24.    - Initial release
  25. --------------------------------------------------------------------------------  
  26.  ** Terms of Use
  27.  * Free to use in non-commercial projects
  28.  * Contact me for commercial use
  29.  * No additional commercial use contact necessary for KBGaming
  30.  * No real support. The script is provided as-is
  31.  * Will do bug fixes, but no compatibility patches
  32.  * Features may be requested but no guarantees, especially if it is non-trivial
  33.  * Credits to Hime Works in your project
  34.  * Credits also to Roninator2, and KBGaming (the latter is optional)
  35.  * Preserve this header
  36. --------------------------------------------------------------------------------
  37.  ** Description
  38.  
  39.  This script allows you to define custom skill requirements for your skills.
  40.  By default, you can choose two require up to two weapon types. This script
  41.  allows you to define requirements based on things like
  42.  
  43.    - actor's class
  44.    - equipped weapons
  45.    - equipped armors
  46.    - equipped weapon types
  47.    - equipped armor types
  48.    - learned skills
  49.    - New: skills given by features (if an equip/state gives a skill for example)
  50.    - active states
  51.    - formulas, for anything else
  52.    
  53.  You can create conditions to require multiple conditions to be met, or
  54.  require at least one condition to be met.
  55.  
  56. --------------------------------------------------------------------------------
  57.  ** Installation
  58.  
  59.  In the script editor, place this script below Materials and above Main
  60.  
  61. --------------------------------------------------------------------------------
  62.  ** Usage
  63.  
  64.  -- Specifying Use Conditions --
  65.  
  66.  Note-tag your skills or items with the following
  67.  
  68.    <use conditions>
  69.     TYPE1: VALUE1
  70.     TYPE2: VALUE2
  71.    </use conditions>
  72.    
  73.  Refer to the reference section for a list of available use conditions.
  74.  
  75.  There is a special "formula" type that allows you to evaluate any arbitrary
  76.  formula. The following formula variables are available
  77.  
  78.    a - current actor
  79.    p - game party
  80.    t - game troop
  81.    s - game switches
  82.    v - game variables
  83.    
  84.  -- Use Condition Groups --
  85.  
  86.  All use conditions are organized into separate "use condition groups". The
  87.  notetag that you see above describes a single condition group. You can
  88.  have multiple condition groups by simply defining multiple notetags.
  89.  
  90.  A skill is said to be "usable" if at least one condition group is satisfied.
  91.  A condition group is satisfied only if all conditions within the group are
  92.  satisfied. That is, they evaluate to true. Therefore, if you have multiple
  93.  condition groups, you are only required to satisfy one group in order to
  94.  use the skill. See the example to understand how condition groups are used.
  95.  
  96. --------------------------------------------------------------------------------
  97.  ** Example
  98.  
  99.  Suppose you have a Fire Slash skill that can be used under two different
  100.  conditions as follows
  101.  
  102.  1. You must have the "fire enchant" state (state 7), and equip a sword type
  103.     weapon (wtype 2)
  104.  2. You are using the "Fire Dragon Sword" (weapon 21)
  105.  
  106.  To accomplish this, you will define two use condition groups by notetagging
  107.  your skill with
  108.  
  109.  <use conditions>
  110.    state: 7
  111.    wtype: 2
  112.  </use conditions>
  113.  
  114.  <use conditions>
  115.    weapon: 21
  116.  </use conditions>
  117.  
  118. --------------------------------------------------------------------------------
  119.  ** Reference
  120.  
  121.  The following use condition types are available
  122.  
  123.  type: weapon
  124.  value: ID
  125.  desc: requires the weapon to be equipped
  126.  
  127.  type: armor
  128.  value: ID
  129.  desc: requires the armor to be equipped
  130.  
  131.  type: wtype
  132.  value: ID
  133.  desc: requires the weapon type to be equipped
  134.  
  135.  type: atype
  136.  value: ID
  137.  desc: requires the armor type to be equipped
  138.  
  139.  type: actor
  140.  value: ID
  141.  desc: requires the user to be a specific actor
  142.  
  143.  type: class
  144.  value: ID
  145.  desc: requires the actor to have the given class
  146.  
  147.  type: state
  148.  value: ID
  149.  desc: requires the state to be currently applied to the actor
  150.  
  151.  type: learned
  152.  value: ID
  153.  desc: requires the actor to have learned the specified skill
  154.  
  155. #        New. Looks for if a skill is added by any feature.
  156.  type: osl (other skill learned)
  157.  value: ID
  158.  desc: requires the actor to have acquired the specified skill, via any means including features
  159. #        end New
  160.  
  161.  type: formula
  162.  value: ruby formula
  163.  desc: requires the formula to evaluate to true
  164.  
  165. --------------------------------------------------------------------------------
  166.  ** Examples
  167.  
  168.  
  169.  
  170. #===============================================================================
  171. =end
  172. $imported = {} if $imported.nil?
  173. $imported["TH_CustomUseConditions"] = true
  174. #===============================================================================
  175. # ** Configuration
  176. #===============================================================================
  177. module TH
  178.   module Custom_Use_Conditions
  179.    
  180.     Regex = /<use[-_ ]conditions>(.*?)<\/use[-_ ]conditions>/im
  181.   end
  182. end
  183. #===============================================================================
  184. # ** Rest of Script
  185. #===============================================================================
  186. module RPG
  187.   class UsableItem < BaseItem
  188.    
  189.     def use_conditions
  190.       load_notetag_use_conditions unless @use_conditions
  191.       return @use_conditions
  192.     end
  193.    
  194.     def load_notetag_use_conditions
  195.       @use_conditions = []
  196.      
  197.       res = self.note.scan(TH::Custom_Use_Conditions::Regex)
  198.       res.each do |result|
  199.         group = Data_UseConditionGroup.new
  200.         result[0].strip.split("\r\n").each do |option|
  201.           case option.strip
  202.           when /weapon:\s*(\d+)\s*/i
  203.             cond = make_custom_use_condition(:weapon, $1.to_i)
  204.           when /armor:\s*(\d+)\s*/i
  205.             cond = make_custom_use_condition(:armor, $1.to_i)
  206.           when /learned:\s*(\d+)\s*/i
  207.             cond = make_custom_use_condition(:learned, $1.to_i)
  208. #        New. Looks for if a skill is added by any feature.
  209.           when /osl:\s*(\d+)\s*/i
  210.             cond = make_custom_use_condition(:osl, $1.to_i)
  211. #        end New
  212.           when /wtype:\s*(\d+)\s*/i
  213.             cond = make_custom_use_condition(:wtype, $1.to_i)
  214.           when /atype:\s*(\d+)\s*/i
  215.             cond = make_custom_use_condition(:atype, $1.to_i)
  216.           when /actor:\s*(\d+)\s*/i
  217.             cond = make_custom_use_condition(:actor, $1.to_i)
  218.           when /class:\s*(\d+)\s*/i
  219.             cond = make_custom_use_condition(:class, $1.to_i)
  220.           when /state:\s*(\d+)\s*/i
  221.             cond = make_custom_use_condition(:state, $1.to_i)
  222.           when /formula:\s*(.*)\s*/i
  223.             cond = make_custom_use_condition(:formula, $1)
  224.           end
  225.           group.conditions << cond
  226.         end
  227.         @use_conditions << group
  228.       end
  229.     end
  230.    
  231.     def make_custom_use_condition(type, value)
  232.       return Data_UseCondition.new(type, value)
  233.     end
  234.   end
  235. end
  236.  
  237. class Data_UseConditionGroup
  238.  
  239.   attr_reader :conditions
  240.  
  241.   def initialize
  242.     @conditions = []
  243.   end
  244. end
  245.  
  246. class Data_UseCondition
  247.  
  248.   attr_reader :type
  249.   attr_reader :value
  250.  
  251.   def initialize(type, value)
  252.     @type = type
  253.     @value = value
  254.   end
  255.  
  256.   def eval_use_condition(a, p=$game_party, t=$game_troop, s=$game_switches, v=$game_variables)
  257.     eval(@value)
  258.   end
  259. end
  260.  
  261. class Game_BattlerBase
  262.  
  263.   def custom_use_conditions_met?(item)
  264.     true
  265.   end
  266. end
  267.  
  268. class Game_Actor < Game_Battler
  269.  
  270.   alias :th_use_conditions_usable? :usable?
  271.   def usable?(item)
  272.     bool = th_use_conditions_usable?(item)
  273.     return false unless bool
  274.     unless @check_use_custom_conditions
  275.       @check_use_custom_conditions = true
  276.       bool = custom_use_conditions_met?(item)
  277.       @check_use_custom_conditions = false
  278.     end
  279.     return bool
  280.   end
  281.  
  282.   #-----------------------------------------------------------------------------
  283.   #
  284.   #-----------------------------------------------------------------------------
  285.   alias :th_use_conditions_custom_use_conditions_met? :custom_use_conditions_met?
  286.   def custom_use_conditions_met?(item)
  287.     return false unless th_use_conditions_custom_use_conditions_met?(item)
  288.     return true if item.nil? || item.use_conditions.empty?
  289.     weapons = self.weapons
  290.     armors = self.armors
  291.    
  292.     weapon_ids = weapons.collect {|obj| obj.id}
  293.     wtype_ids = weapons.collect {|obj| obj.wtype_id}
  294.     armor_ids = armors.collect {|obj| obj.id}
  295.     atype_ids = armors.collect {|obj| obj.atype_id}
  296.     state_ids = self.states.collect {|obj| obj.id }
  297. #        New. Looks for if a skill is added by any feature.
  298.     skill_ids = skills.collect {|obj| obj.id}
  299. #        end New
  300.     # for each group
  301.     item.use_conditions.each do |group|      
  302.       # skip if any are not satisfied
  303.       next if group.conditions.any? do |cond|
  304.         value = cond.value
  305.         case cond.type
  306.         when :weapon
  307.           !weapon_ids.include?(value)
  308.         when :armor
  309.           !armor_ids.include?(value)
  310.         when :state
  311.           !state_ids.include?(value)
  312.         when :class
  313.           !(@class_id == value)
  314.         when :actor
  315.           !(@actor_id == value)
  316.         when :wtype
  317.           !wtype_ids.include?(value)
  318.         when :atype
  319.           !atype_ids.include?(value)
  320.         when :learned
  321.           !@skills.include?(value)
  322. #        New. Looks for if a skill is added by any feature.
  323.         when :osl
  324.           !skill_ids.include?(value)
  325. #        end New
  326.         when :formula
  327.           !cond.eval_use_condition(self)
  328.         end
  329.       end
  330.      
  331.       # all are satisfied, so this group is satisfied
  332.       return true
  333.     end
  334.     return false
  335.   end
  336. end
  337.  
  338. class Window_BattleItem < Window_ItemList
  339.  
  340.   #--------------------------------------------------------------------------
  341.   # Overwrite. Item usability is based on actor, not party
  342.   #--------------------------------------------------------------------------
  343.   def include?(item)
  344.     BattleManager.actor.usable?(item)
  345.   end
  346. end
  347.  
  348.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement