Advertisement
roninator2

Random Scope Target Select

Dec 10th, 2024
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 16.74 KB | None | 0 0
  1. # ╔═══════════════════════════════════════════════╦════════════════════╗
  2. # ║ Title: Random Scope Target Select             ║  Version: 1.03     ║
  3. # ║ Author: Roninator2                            ║                    ║
  4. # ╠═══════════════════════════════════════════════╬════════════════════╣
  5. # ║ Function:                                     ║   Date Created     ║
  6. # ║                                               ╠════════════════════╣
  7. # ║  Alter Random Selection                       ║    20 May 2022     ║
  8. # ╚═════════════════════════════════════════════v═╩════════════════════╝
  9. # ╔════════════════════════════════════════════════════════════════════╗
  10. # ║ Requires: nil                                                      ║
  11. # ║                                                                    ║
  12. # ╚════════════════════════════════════════════════════════════════════╝
  13. # ╔════════════════════════════════════════════════════════════════════╗
  14. # ║ Brief Description:                                                 ║
  15. # ║       Allow Target scope to change                                 ║
  16. # ╚════════════════════════════════════════════════════════════════════╝
  17. # ╔════════════════════════════════════════════════════════════════════╗
  18. # ║ Instructions:                                                      ║
  19. # ║    Place note tag on the skill you wish to alter                   ║
  20. # ║                                                                    ║
  21. # ║    Skill must be set to select random enemies 1-4                  ║
  22. # ║                                                                    ║
  23. # ║      <select random: indiv>                                        ║
  24. # ║    Allows choosing the enemies up to number of random              ║
  25. # ║       enemies is set in the skill                                  ║
  26. # ║                                                                    ║
  27. # ║      <select random: group>                                        ║
  28. # ║    Will select all enemies of the same type up to                  ║
  29. # ║        the number of random enemies set for the skill.             ║
  30. # ║        Example - two bats on screen. You select bat and            ║
  31. # ║        skill is set to 4 random enemies. It will                   ║
  32. # ║        randomly select from the two to complete                    ║
  33. # ║        the taregets (bat1, bat1, bat2, bat1)                       ║
  34. # ╚════════════════════════════════════════════════════════════════════╝
  35. # ╔════════════════════════════════════════════════════════════════════╗
  36. # ║ Updates:                                                           ║
  37. # ║ 1.00 - 20 May 2022 - Initial publish                               ║
  38. # ║ 1.01 - 30 May 2022 - Fixed typo                                    ║
  39. # ║ 1.02 - 03 Nov 2022 - Fixed alias bug                               ║
  40. # ║ 1.03 - 03 Nov 2022 - Added Support for Yanfly Battle Engine        ║
  41. # ╚════════════════════════════════════════════════════════════════════╝
  42. # ╔════════════════════════════════════════════════════════════════════╗
  43. # ║ Credits and Thanks:                                                ║
  44. # ║   Roninator2                                                       ║
  45. # ║                                                                    ║
  46. # ╚════════════════════════════════════════════════════════════════════╝
  47. # ╔════════════════════════════════════════════════════════════════════╗
  48. # ║ Terms of use:                                                      ║
  49. # ║  Follow the original Authors terms of use where applicable         ║
  50. # ║    - When not made by me (Roninator2)                              ║
  51. # ║  Free for all uses in RPG Maker except nudity                      ║
  52. # ║  Anyone using this script in their project before these terms      ║
  53. # ║  were changed are allowed to use this script even if it conflicts  ║
  54. # ║  with these new terms. New terms effective 03 Apr 2024             ║
  55. # ║  No part of this code can be used with AI programs or tools        ║
  56. # ║  Credit must be given                                              ║
  57. # ╚════════════════════════════════════════════════════════════════════╝
  58.  
  59. module R2_Random_Enemy_Select
  60.   X_POS = 100 # window x position
  61.   Y_POS = 100 # window y position
  62.   WIDTH = 270 # window width
  63.   HEIGHT = 50 # window height
  64.   Extra = false # add more to the window width if text is long
  65.   Extra_Space = 20 # how much space to add.
  66.   REGEX = /<select[-_ ]random:[ ](\w+)>/i # regex code for script
  67.   Enemy_Text = "Choose the Targets" # text shown in window
  68.   Enemy_Group = "Choose the Target Group" # text shown in window
  69.   # IMPORTANT - set and forget. Used internally to hold targets
  70.   Variable = 9
  71. end
  72.  
  73. #==============================================================================
  74. # ■ DataManager
  75. #==============================================================================
  76.  
  77. module DataManager
  78.  
  79.   #--------------------------------------------------------------------------
  80.   # alias method: load_database
  81.   #--------------------------------------------------------------------------
  82.   class <<self; alias load_database_r2prev_esel load_database; end
  83.   def self.load_database
  84.     load_database_r2prev_esel
  85.     load_notetags_r2_sk_esel
  86.   end
  87.  
  88.   #--------------------------------------------------------------------------
  89.   # new method: load_notetags_lse
  90.   #--------------------------------------------------------------------------
  91.   def self.load_notetags_r2_sk_esel
  92.     for r2skesel in $data_skills
  93.       next if r2skesel.nil?
  94.       r2skesel.load_notetags_r2_sk_esel
  95.     end
  96.   end
  97.  
  98. end # DataManager
  99.  
  100. #==============================================================================
  101. # ■ RPG::Skill
  102. #==============================================================================
  103.  
  104. class RPG::Skill < RPG::UsableItem
  105.  
  106.   #--------------------------------------------------------------------------
  107.   # public instance variables
  108.   #--------------------------------------------------------------------------
  109.   attr_accessor :enm_sel_sk
  110.  
  111.   #--------------------------------------------------------------------------
  112.   # common cache: load_notetags_lse
  113.   #--------------------------------------------------------------------------
  114.   def load_notetags_r2_sk_esel
  115.     @enm_sel_sk = ""
  116.     #---
  117.     self.note.split(/[\r\n]+/).each { |line|
  118.       case line
  119.       #---
  120.       when R2_Random_Enemy_Select::REGEX
  121.         return if $1.nil?
  122.         @enm_sel_sk = ($1.upcase.to_s)
  123.       end
  124.     } # self.note.split
  125.     #---
  126.   end
  127.  
  128. end # RPG::Skill
  129.  
  130. class Window_Enemy_Random < Window_Base
  131.   attr_accessor :enemy_selected
  132.   attr_accessor :enemy_group
  133.   attr_accessor :enemy_count
  134.   #--------------------------------------------------------------------------
  135.   # * Object Initialization
  136.   #--------------------------------------------------------------------------
  137.   def initialize
  138.     x = R2_Random_Enemy_Select::X_POS
  139.     y = R2_Random_Enemy_Select::Y_POS
  140.     super(x, y, window_width, window_height)
  141.     self.width += R2_Random_Enemy_Select::Extra_Space if R2_Random_Enemy_Select::Extra == true
  142.     @enemy_group = false
  143.     @enemy_count = 0
  144.     @enemy_selected = 0
  145.   end
  146.   #--------------------------------------------------------------------------
  147.   # * Get Window Width
  148.   #--------------------------------------------------------------------------
  149.   def window_width
  150.     R2_Random_Enemy_Select::WIDTH
  151.   end
  152.   #--------------------------------------------------------------------------
  153.   # * Get Window Height
  154.   #--------------------------------------------------------------------------
  155.   def window_height
  156.     R2_Random_Enemy_Select::HEIGHT
  157.   end
  158.   #--------------------------------------------------------------------------
  159.   # * Set Skill
  160.   #--------------------------------------------------------------------------
  161.   def set_skill(skill)
  162.     return if skill.enm_sel_sk == []
  163.     skill.enm_sel_sk == "GROUP" ? @enemy_group = true : @enemy_group = false
  164.     @enemy_count = skill.scope.to_i - 2
  165.     @enemy_selected = 0
  166.     refresh
  167.   end
  168.   #--------------------------------------------------------------------------
  169.   # * Set selected
  170.   #--------------------------------------------------------------------------
  171.   def add_selected(num)
  172.     @enemy_selected += num
  173.     refresh
  174.   end
  175.   #--------------------------------------------------------------------------
  176.   # * Refresh
  177.   #--------------------------------------------------------------------------
  178.   def refresh
  179.     contents.clear
  180.     reset_font_settings
  181.     text2 = @enemy_selected.to_s + "/" + @enemy_count.to_s
  182.     if @enemy_group == false
  183.       text1 = R2_Random_Enemy_Select::Enemy_Text.to_s
  184.       text = text1 + ": " + text2
  185.     else
  186.       text = R2_Random_Enemy_Select::Enemy_Group.to_s
  187.     end
  188.     draw_text(0, -15, window_width, window_height, text, 0)
  189.   end
  190. end
  191.  
  192. class Scene_Battle
  193.   #--------------------------------------------------------------------------
  194.   # * Create All Windows
  195.   #--------------------------------------------------------------------------
  196.   alias r2_create_all_enemy_select_windows_824f7  create_all_windows
  197.   def create_all_windows
  198.     r2_create_all_enemy_select_windows_824f7
  199.     enemy_selected_window
  200.   end
  201.   #--------------------------------------------------------------------------
  202.   # * Create Message Window
  203.   #--------------------------------------------------------------------------
  204.   def enemy_selected_window
  205.     @enemy_select_window = Window_Enemy_Random.new
  206.     @enemy_select_window.hide
  207.   end
  208.  
  209.   #--------------------------------------------------------------------------
  210.   # * Start Enemy Selection
  211.   #--------------------------------------------------------------------------
  212.   def select_enemy_random_selection
  213.     @enm_list = []
  214.     @enm_selected = []
  215.     $game_variables[R2_Random_Enemy_Select::Variable] = []
  216.     @enemy_select_window.set_skill(@skill)
  217.     @enemy_window.refresh
  218.     @enemy_window.show.activate
  219.     @enemy_select_window.show
  220.   end
  221.   #--------------------------------------------------------------------------
  222.   # * Pick enemies from the group
  223.   #--------------------------------------------------------------------------
  224.   def pick_enemy
  225.     amt = @enm_selected.size
  226.     num = rand(amt)
  227.     return @enm_selected[num]
  228.   end
  229.   #--------------------------------------------------------------------------
  230.   # * Enemy [OK]
  231.   #--------------------------------------------------------------------------
  232.   alias r2_enemy_ok_skill_check_group on_enemy_ok
  233.   def on_enemy_ok
  234.     if (@actor_command_window.current_symbol == :skill) && (@skill.enm_sel_sk == "GROUP")
  235.       BattleManager.actor.input.target_index = @enemy_window.enemy.index
  236.       choosen = @enemy_window.enemy
  237.       $game_troop.alive_members.each { |enemy|
  238.         @enm_selected.push(enemy) if enemy.enemy_id == choosen.enemy_id
  239.       }
  240.       for i in 1..(@skill.scope.to_i - 2)
  241.         @enm_list.push(pick_enemy)
  242.       end
  243.       $game_variables[R2_Random_Enemy_Select::Variable] = @enm_list
  244.       @enemy_select_window.hide
  245.       @enemy_window.hide
  246.       @skill_window.hide
  247.       @item_window.hide
  248.       next_command
  249.     elsif (@actor_command_window.current_symbol == :skill) && (@skill.enm_sel_sk == "INDIV")
  250.       BattleManager.actor.input.target_index = @enemy_window.enemy.index
  251.       $game_variables[R2_Random_Enemy_Select::Variable].push(@enemy_window.enemy)
  252.       @enemy_select_window.add_selected(1)
  253.       if @enemy_select_window.enemy_selected == (@skill.scope.to_i - 2)
  254.         @enemy_select_window.hide
  255.         @enemy_window.hide
  256.         @skill_window.hide
  257.         @item_window.hide
  258.         next_command
  259.       else
  260.         @enemy_window.refresh
  261.         @enemy_window.show.activate
  262.         @enemy_select_window.show
  263.       end
  264.     else
  265.       r2_enemy_ok_skill_check_group
  266.     end
  267.   end
  268.   #--------------------------------------------------------------------------
  269.   # * Skill [OK]
  270.   #--------------------------------------------------------------------------
  271.   if $imported["YEA-BattleEngine"]
  272.     def on_skill_ok
  273.       @skill = @skill_window.item
  274.       $game_temp.battle_aid = @skill
  275.       BattleManager.actor.input.set_skill(@skill.id)
  276.       BattleManager.actor.last_skill.object = @skill
  277.       if @skill.enm_sel_sk != ""
  278.         select_enemy_random_selection
  279.       elsif @skill.for_opponent?
  280.         select_enemy_selection
  281.       elsif @skill.for_friend?
  282.         select_actor_selection
  283.       else
  284.         @skill_window.hide
  285.         next_command
  286.         $game_temp.battle_aid = nil
  287.       end
  288.     end
  289.   else
  290.     def on_skill_ok
  291.       @skill = @skill_window.item
  292.       BattleManager.actor.input.set_skill(@skill.id)
  293.       BattleManager.actor.last_skill.object = @skill
  294.       if @skill.enm_sel_sk != ""
  295.         select_enemy_random_selection
  296.       elsif !@skill.need_selection?
  297.         @skill_window.hide
  298.         next_command
  299.       elsif @skill.for_opponent?
  300.         select_enemy_selection
  301.       else
  302.         select_actor_selection
  303.       end
  304.     end
  305.   end
  306.   #--------------------------------------------------------------------------
  307.   # * Enemy [Cancel]
  308.   #--------------------------------------------------------------------------
  309.   alias r2_on_enemy_cancel_group_enemy  on_enemy_cancel
  310.   def on_enemy_cancel
  311.     @enemy_select_window.hide
  312.     r2_on_enemy_cancel_group_enemy
  313.   end
  314. end
  315.  
  316. class Game_Unit
  317.   #--------------------------------------------------------------------------
  318.   # * Random Selection of Target
  319.   #--------------------------------------------------------------------------
  320.   def random_specific
  321.     select = $game_variables[R2_Random_Enemy_Select::Variable][0]
  322.     return nil if select == nil
  323.     $game_variables[R2_Random_Enemy_Select::Variable].shift
  324.     return select
  325.   end
  326. end
  327.  
  328. class Game_Action
  329.   #--------------------------------------------------------------------------
  330.   # * OVERWRITE - Random Selection of Target
  331.   #--------------------------------------------------------------------------
  332.   def targets_for_opponents
  333.     if item.for_random?
  334.       if item.is_a?(RPG::Skill) && item.enm_sel_sk != ""
  335.         Array.new(item.number_of_targets) { opponents_unit.random_specific }
  336.       else
  337.         Array.new(item.number_of_targets) { opponents_unit.random_target }
  338.       end
  339.     elsif item.for_one?
  340.       num = 1 + (attack? ? subject.atk_times_add.to_i : 0)
  341.       if @target_index < 0
  342.         [opponents_unit.random_target] * num
  343.       else
  344.         [opponents_unit.smooth_target(@target_index)] * num
  345.       end
  346.     else
  347.       opponents_unit.alive_members
  348.     end
  349.   end
  350. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement