Advertisement
MrTrivel

MrTS_Learn_Skills_from_Items

Jun 17th, 2014
1,043
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 17.51 KB | None | 0 0
  1. # )----------------------------------------------------------------------------(
  2. # )--     AUTHOR:     Mr. Trivel                                             --(
  3. # )--     NAME:       Learn Skills from Items                                --(
  4. # )--     CREATED:    2014-06-18                                             --(
  5. # )--     VERSION:    1.2                                                    --(
  6. # )----------------------------------------------------------------------------(
  7. # )--                        VERSION HISTORY:                                --(
  8. # )--  v1.0 - Initial script.                                                --(
  9. # )--  v1.1 - Added 2 customizable options - LP_ON_UNEQUIP and <LearnedOnly> --(
  10. # )--  v1.2 - Parts of script rewritten for better speed and compatibility.  --(
  11. # )----------------------------------------------------------------------------(
  12. # )--                          DESCRIPTION                                   --(
  13. # )--  Equipment can have a spell inside of them which requires X amount of  --(
  14. # )--  Learning Points (further LP). LP is gained from slaying enemies.      --(
  15. # )--  Once Actor reaches required LP for that spell, he learns it forever.  --(
  16. # )--                                                                        --(
  17. # )--  Each Class has school affinities. E.g. Healing school affinity will   --(
  18. # )--  help Actor learn healing skills Y times faster, but Destruction spells--(
  19. # )--  Z times slower.                                                       --(
  20. # )--  Actor can use item skills before learning it while item is equipped.  --(
  21. # )----------------------------------------------------------------------------(
  22. # )--                          INSTRUCTIONS                                  --(
  23. # )--      CLASS:                                                            --(
  24. # )--  <Schools: A, B, C, D> E.g. <Schools : 0.5, 1.5, 1, 2> will set school --(
  25. # )--  affinities to 0.5 for Healing, 1.5 for Destruction, 1 for Conjuration --(
  26. # )--  and 2 for Laziness. 0.5 - means actor will receive only 50% of LP for --(
  27. # )--  that school's spell. 2 - actor will receive 200% of LP for that       --(
  28. # )--  school's spell.                                                       --(
  29. # )--                                                                        --(
  30. # )--      EQUIPMENT:                                                        --(
  31. # )--  <Skills: A, B, C> E.g. <Skills: 26, 27, 28> in an Equipmenet Note tag --(
  32. # )--  will mean that it holds 26th, 26th and 28th Skill in database.        --(
  33. # )--                                                                        --(
  34. # )--      SKILL:                                                            --(
  35. # )--  <LpCost: X> E.g. <LPCost: 120> means that it requires 120 LP to be    --(
  36. # )--  learned.                                                              --(
  37. # )--  <School: X> E.g. <School: 0> means the Skill's affinity is Healing.   --(
  38. # )--  So classes Healing affinity affects LP of this skill.                 --(
  39. # )--  <LearnedOnly> Skill has to be learned first before player can use it. --(
  40. # )--                                                                        --(
  41. # )--      ENEMY:                                                            --(
  42. # )--  <LP: X> E.g. <LP: 10> means that enemy will give 10 base LP when it's --(
  43. # )--  slain.                                                                --(
  44. # )----------------------------------------------------------------------------(
  45. # )--                          LICENSE INFO                                  --(
  46. # )--   http://mrtrivelvx.wordpress.com/terms-of-use/                        --(
  47. # )----------------------------------------------------------------------------(
  48.  
  49. module MrTS
  50.   module Spell_Learning
  51.     # )------------------------------------------------------------------------(
  52.     # )--  Does Actor lose all LP for the skill on equipment if unequips it? --(
  53.     # )------------------------------------------------------------------------(
  54.     LOSE_LP_ON_UNEQUIP = true
  55.    
  56.     # )------------------------------------------------------------------------(
  57.     # )--  Your school names go here. If you wanna call the names.           --(
  58.     # )------------------------------------------------------------------------(
  59.     SchoolNames= ["Healing",
  60.                   "Destruction",
  61.                   "Conjuration",
  62.                   "Laziness"
  63.                   ]
  64.     # )------------------------------------------------------------------------(
  65.     # )--  Number of schools you're going to have.                           --(
  66.     # )------------------------------------------------------------------------(
  67.     NumberOfSchools = 4
  68.    
  69.     # )------------------------------------------------------------------------(
  70.     DefaultSpellLearningCost = 30#If not defined, spell will cost 30 LP to learn
  71.     DefaultSpellSchool = 0               #If not defined will return school as 0
  72.     DefaultLP = 1                # Default number of Learning Points enemy gives
  73.     # )------------------------------------------------------------------------(
  74.    
  75.    
  76.     # )------------------------------------------------------------------------(
  77.     # )-- You can call MrTS::Spell_Learning::get_school_name(index) to get   --(
  78.     # )-- school's name.                                                     --(
  79.     # )------------------------------------------------------------------------(
  80.     def self.get_school_name(index)
  81.       SchoolNames[index] ? SchoolNames[index] : nil
  82.     end
  83.   end
  84. end
  85.  
  86. # )----------------------------------------------------------------------------(
  87. # )-- Class: Game_Troop                                                      --(
  88. # )----------------------------------------------------------------------------(
  89. class Game_Troop < Game_Unit
  90.  
  91.   # )--------------------------------------------------------------------------(
  92.   # )-- New Method: lp_total                                                 --(
  93.   # )--------------------------------------------------------------------------(
  94.   def lp_total
  95.     dead_members.inject(0) {|r, enemy| r += enemy.lp }
  96.   end
  97. end
  98.  
  99. # )----------------------------------------------------------------------------(
  100. # )-- Module: BattleManager                                                  --(
  101. # )----------------------------------------------------------------------------(
  102. module BattleManager
  103.   class << self ; alias :mrts_itsk_gain_exp :gain_exp ; end
  104.    
  105.   # )--------------------------------------------------------------------------(
  106.   # )-- Alias Method: gain_exp                                               --(
  107.   # )--------------------------------------------------------------------------(  
  108.   def self.gain_exp
  109.     $game_party.battle_members.each { |a| a.add_lp($game_troop.lp_total) }
  110.     mrts_itsk_gain_exp
  111.   end
  112. end
  113.  
  114. # )----------------------------------------------------------------------------(
  115. # )-- Class: Game_Enemy                                                      --(
  116. # )----------------------------------------------------------------------------(
  117. class Game_Enemy < Game_Battler
  118.  
  119.   # )--------------------------------------------------------------------------(
  120.   # )-- Public Instance Variables                                            --(
  121.   # )--------------------------------------------------------------------------(
  122.   attr_reader   :lp
  123.  
  124.   # )--------------------------------------------------------------------------(
  125.   # )-- Alias To: initialize                                                 --(
  126.   # )--------------------------------------------------------------------------(
  127.   alias mrts_initialize initialize
  128.   def initialize(index, enemy_id)
  129.     mrts_initialize(index, enemy_id)
  130.     @lp = enemy.lpgive
  131.   end
  132. end
  133.  
  134. # )----------------------------------------------------------------------------(
  135. # )-- Class: Game_Actor                                                      --(
  136. # )----------------------------------------------------------------------------(
  137. class Game_Actor < Game_Battler
  138.  
  139.   # )--------------------------------------------------------------------------(
  140.   # )-- ublic Instance Variables                                             --(
  141.   # )--------------------------------------------------------------------------(
  142.   attr_reader   :learning_points
  143.   attr_reader   :school_affinities
  144.  
  145.   # )--------------------------------------------------------------------------(
  146.   # )-- Alias To: setup                                                      --(
  147.   # )--------------------------------------------------------------------------(
  148.   alias mrts_ga_setup setup
  149.   def setup(actor_id)
  150.     mrts_ga_setup(actor_id)
  151.     setup_lp
  152.   end
  153.  
  154.   # )--------------------------------------------------------------------------(
  155.   # )-- Alias To: release_unequippable_items                                 --(
  156.   # )--------------------------------------------------------------------------(
  157.   alias mrts_release_unequippable_items release_unequippable_items
  158.   def release_unequippable_items(item_gain = true)
  159.     mrts_release_unequippable_items
  160.     return unless MrTS::Spell_Learning::LOSE_LP_ON_UNEQUIP
  161.     eq_ar = get_equips_skills
  162.     @learning_points.each_key do |key|
  163.       @learning_points[key] = 0 unless eq_ar.include?(key)
  164.     end if @learning_points
  165.   end
  166.  
  167.   # )--------------------------------------------------------------------------(
  168.   # )-- New Method: setup_lp                                                 --(
  169.   # )--------------------------------------------------------------------------(
  170.   def setup_lp
  171.     @learning_points = {}
  172.     @school_affinities = []
  173.     schools = self.class.note =~ /<schools:\s*(\d+.*\d*),*>/i ? $1 : nil
  174.     schools.scan(/(\d+\.*\d*)/).collect do |i|
  175.       @school_affinities.push(i[0].to_f)
  176.     end if schools
  177.     @school_affinities += [1.0]*MrTS::Spell_Learning::NumberOfSchools unless @school_affinities.size > 0
  178.   end
  179.  
  180.   # )--------------------------------------------------------------------------(
  181.   # )-- New Method: add_lp                                                   --(
  182.   # )--------------------------------------------------------------------------(
  183.   def add_lp(amount)
  184.     ar_sk = get_equips_skills
  185.     ar_sk.each do |i|
  186.       add_learning_points(i, amount)
  187.     end if ar_sk.size > 0
  188.   end
  189.  
  190.   # )--------------------------------------------------------------------------(
  191.   # )-- New Method: get_equips_skills                                        --(
  192.   # )--------------------------------------------------------------------------(
  193.   def get_equips_skills
  194.     arr = []
  195.     equips.each do |eq|
  196.       arr += eq.lp_skills_held if eq
  197.     end
  198.     arr = arr.uniq if arr.size > 1
  199.     arr
  200.   end
  201.  
  202.   # )--------------------------------------------------------------------------(
  203.   # )-- New Method: add_learning_points                                      --(
  204.   # )--------------------------------------------------------------------------(
  205.   def add_learning_points(skill_id, amount)
  206.     return if @skills.include?(skill_id)
  207.     if @learning_points.has_key?(skill_id)
  208.       @learning_points[skill_id] += (amount * get_school_multiplier(skill_id)).to_i
  209.     else
  210.       @learning_points[skill_id] = (amount * get_school_multiplier(skill_id)).to_i
  211.     end
  212.     try_to_learn(skill_id)
  213.   end
  214.  
  215.   # )--------------------------------------------------------------------------(
  216.   # )-- New Method: get_school_multiplier                                    --(
  217.   # )--------------------------------------------------------------------------(
  218.   def get_school_multiplier(skill_id)
  219.     @school_affinities[$data_skills[skill_id].lpschool]
  220.   end
  221.  
  222.   # )--------------------------------------------------------------------------(
  223.   # )-- New Method: try_to_learn                                             --(
  224.   # )--------------------------------------------------------------------------(
  225.   def try_to_learn(skill_id)
  226.     if @learning_points[skill_id] >= $data_skills[skill_id].lpcost
  227.       learn_skill(skill_id)
  228.       @learning_points.delete(skill_id)
  229.     end
  230.   end
  231.  
  232.   # )--------------------------------------------------------------------------(
  233.   # )-- New Method: get_lp_skill_from                                        --(
  234.   # )--------------------------------------------------------------------------(
  235.   def get_lp_skill_from(skill_id)
  236.     @learning_points[skill_id] ? @learning_points[skill_id] : 0
  237.   end
  238.  
  239.   # )--------------------------------------------------------------------------(
  240.   # )-- New Method: include_skill?                                           --(
  241.   # )--------------------------------------------------------------------------(
  242.   def include_skill?(skill_id)
  243.     @skills.include?(skill_id)
  244.   end
  245. end
  246.  
  247. # )----------------------------------------------------------------------------(
  248. # )-- Class: Window_SkillList                                                --(
  249. # )----------------------------------------------------------------------------(
  250. class Window_SkillList < Window_Selectable
  251.  
  252.   # )--------------------------------------------------------------------------(
  253.   # )-- Overwrite Method: make_item_list                                     --(
  254.   # )--------------------------------------------------------------------------(
  255.   def make_item_list
  256.     @data = @actor ? @actor.skills.select {|skill| include?(skill) } : []
  257.     arr = []
  258.     @actor.equips.each { |eq| arr += eq.lp_skills_held if eq }
  259.     arr = arr.uniq
  260.     arr.each do |a|
  261.       skill = Marshal.load(Marshal.dump($data_skills[a]))
  262.       @data.push(skill) if include?(skill) && !@actor.include_skill?(skill.id)
  263.       skill.name += " [" + @actor.get_lp_skill_from(a).to_s + "/" + skill.lpcost.to_s + "]"
  264.     end
  265.   end
  266.  
  267.   # )--------------------------------------------------------------------------(
  268.   # )-- Overwrite Method: enable?                                            --(
  269.   # )--------------------------------------------------------------------------(
  270.   def enable?(item)
  271.     @actor && @actor.usable?(item) && (!item.lponly  || @actor.include_skill?(item.id))
  272.   end
  273. end
  274.  
  275. # )----------------------------------------------------------------------------(
  276. # )-- Class: RPG::Skill                                                      --(
  277. # )----------------------------------------------------------------------------(
  278. class RPG::Skill < RPG::UsableItem
  279.   attr_accessor :lpcost
  280.   attr_accessor :lpschool
  281.   attr_accessor :lponly
  282.  
  283.   # )--------------------------------------------------------------------------(
  284.   # )-- New Method: parse_lp                                                 --(
  285.   # )--------------------------------------------------------------------------(
  286.   def parse_lp
  287.     @lpcost = note =~ /<LPCost:\s*(\d+)>/i ? $1.to_i : MrTS::Spell_Learning::DefaultSpellLearningCost
  288.     @lpschool = note =~ /<school:\s*(\d*)>/i ? $1.to_i : MrTS::Spell_Learning::DefaultSpellSchool
  289.     @lponly = note=~ /<LearnedOnly>/i ? true : false
  290.   end
  291. end
  292.  
  293. # )----------------------------------------------------------------------------(
  294. # )-- Class: RPG::EquipItem                                                  --(
  295. # )----------------------------------------------------------------------------(
  296. class RPG::EquipItem < RPG::BaseItem
  297.  
  298.   attr_accessor :lp_skills_held
  299.  
  300.   # )--------------------------------------------------------------------------(
  301.   # )-- New Method: parse_lp                                                 --(
  302.   # )--------------------------------------------------------------------------(
  303.   def parse_lp
  304.     array = []
  305.     string = note =~ /<Skills:\s*((\d+,*\s*)*)>/i ? $1 : nil
  306.     string.scan(/(\d+)/).collect do |i|
  307.       array.push(i[0].to_i)
  308.     end if string
  309.     @lp_skills_held = array
  310.   end
  311. end
  312.  
  313. # )----------------------------------------------------------------------------(
  314. # )-- Class: RPG::EquipItem                                                  --(
  315. # )----------------------------------------------------------------------------(
  316. class RPG::Enemy
  317.   attr_accessor :lpgive
  318.  
  319.   # )--------------------------------------------------------------------------(
  320.   # )-- New Method: parse_lp                                                 --(
  321.   # )--------------------------------------------------------------------------(
  322.   def parse_lp
  323.     @lpgive = note=~ /<LP:\s*(\d*)>/i ? $1.to_i : MrTS::Spell_Learning::DefaultLP
  324.   end
  325. end
  326.  
  327. # )----------------------------------------------------------------------------(
  328. # )-- Module: DataManager                                                    --(
  329. # )----------------------------------------------------------------------------(
  330. module DataManager
  331.   class << self    
  332.     alias :mrts_lp_create_game_objects    :create_game_objects
  333.   end
  334.  
  335.   # )--------------------------------------------------------------------------(
  336.   # )-- Alias: create_game_objects                                           --(
  337.   # )--------------------------------------------------------------------------(
  338.   def self.create_game_objects
  339.     mrts_lp_create_game_objects
  340.     parse_lp
  341.   end
  342.  
  343.   # )--------------------------------------------------------------------------(
  344.   # )-- New Method: self.parse_lp                                            --(
  345.   # )--------------------------------------------------------------------------(
  346.   def self.parse_lp
  347.     data = $data_skills + $data_armors + $data_weapons + $data_enemies
  348.     for item in data
  349.       next if item.nil?
  350.       item.parse_lp
  351.     end
  352.   end
  353. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement