neonblack

Random Troops v1.0

Mar 13th, 2013
139
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ##----------------------------------------------------------------------------##
  2. ## Random Enemy Troops v1.0
  3. ## Created by Neon Black
  4. ##
  5. ## For both commercial and non-commercial use as long as credit is given to
  6. ## Neon Black and any additional authors.  Licensed under Creative Commons
  7. ## CC BY 3.0 - http://creativecommons.org/licenses/by/3.0/.
  8. ##----------------------------------------------------------------------------##
  9.                                                                               ##
  10. ##----------------------------------------------------------------------------##
  11. ##    Revision Info:
  12. ## v1.0 - 3.13.2013
  13. ##  Wrote and debugged main script
  14. ##----------------------------------------------------------------------------##
  15.                                                                               ##
  16. $imported ||= {}                                                              ##
  17. $imported["CP_RANDOM_TROOP"] = 1.0                                            ##
  18.                                                                               ##
  19. ##----------------------------------------------------------------------------##
  20. ##    Instructions:
  21. ## Place this script in the script editor below "Materials" and above "Main".
  22. ## This script works by adding comments to troops as tags.  These comments
  23. ## allow for existing members of the troop to be replaced with other enemies in
  24. ## the same location.  This has three tags that can be added to any page in
  25. ## under any conditions.
  26. ##
  27. ## <random troop>  -or-  <randomize troop>
  28. ##  - Scrambles enemies in the current troop.  Every enemy has the same chance
  29. ##    to be selected.
  30. ## troop range[1 8]  -or-  troop range[1-8]  -etc-
  31. ##  - Sets the number of enemies that can appear in the troop.  Enemies appear
  32. ##    in positions randomly.  The range can be any positive values between 0 and
  33. ##    the number of members in the current troop.
  34. ## enemy[5]  -or-  enemy[5, 20]
  35. ##  - Adds a random enemy to the random enemies list.  If you use this tag, none
  36. ##    of the default enemies in the troop will appear unless you use a tag with
  37. ##    their ID.  The first number is the enemy's ID in the database.  The second
  38. ##    number is the chance to appear in the group.  This can be any number at
  39. ##    all.  Note that higher numbers have a higher chance of appearing.
  40. ##----------------------------------------------------------------------------##
  41.                                                                               ##
  42.                                                                               ##
  43. ##----------------------------------------------------------------------------##
  44. ## The following lines are the actual core code of the script.  While you are
  45. ## certainly invited to look, modifying it may result in undesirable results.
  46. ## Modify at your own risk!
  47. ###----------------------------------------------------------------------------
  48.  
  49. class Game_Troop < Game_Unit
  50.   ## Alias the clear method to clear out the custom troop data.
  51.   alias :cp_troop_clear :clear
  52.   def clear
  53.     cp_troop_clear
  54.     @data_troop = nil
  55.   end
  56.  
  57.   ## Alias the troop method to allow a new troop to be created.
  58.   alias :cp_troop_troop :troop
  59.   def troop
  60.     return @data_troop if @data_troop
  61.     @data_troop = cp_troop_troop.dup
  62.     @data_troop.scramble
  63.     return @data_troop
  64.   end
  65. end
  66.  
  67. class RPG::Troop
  68.   ## 4 part method that scrambles troop data.
  69.   def scramble
  70.     enemies = []
  71.     range = [members.size, members.size]
  72.     range_changed = false
  73.     randomize = false
  74.     pages.each do |page|
  75.       page.list.each do |line|
  76.         next unless [108, 408].include?(line.code)
  77.         case line.parameters[0]
  78.         when /<random(ize)? troop>/i
  79.           randomize = true
  80.           range_changed = true
  81.         when /troop range\[(\d+)[- ]*(\d+)\]/i
  82.           range = [[$1.to_i, members.size].min, [$2.to_i, members.size].min]
  83.           range_changed = true
  84.         when /enemy\[(\d+)[, ]*(\d*)\]/i
  85.           enemies.push([$1.to_i, [$2.to_i, 1].max])
  86.         end
  87.       end
  88.     end
  89.     return if enemies.empty? && !range_changed
  90.     if enemies.empty? && randomize
  91.       members.each do |mem|
  92.         ar = [mem.enemy_id, 10]
  93.         next if enemies.include?(ar)
  94.         enemies.push(ar)
  95.       end
  96.     end
  97.     unless enemies.empty?
  98.       max_weight = enemies.inject(0) { |o,e| o += e[1] }
  99.       members.each do |mem|
  100.         i = max_weight - rand(max_weight)
  101.         enemies.each do |a|
  102.           i -= a[1]
  103.           next unless i <= 0
  104.           mem.enemy_id = a[0]
  105.           break
  106.         end
  107.       end
  108.     end
  109.     if range[0] == range[1]
  110.       pops = members.size - range[0]
  111.     else
  112.       pops = (rand(range[1] - range[0] + 1))
  113.     end
  114.     pops.times do
  115.       members.delete_at(rand(members.size))
  116.     end
  117.   end
  118. end
  119.  
  120.  
  121. ###--------------------------------------------------------------------------###
  122. #  End of script.                                                              #
  123. ###--------------------------------------------------------------------------###
RAW Paste Data