Random enemy parties

This script let you use random enemy parties like games like Dragon Quest. You
define a max, min, and the appear rate of each type of enemy in a set of maps, and
the rest is random.

  1. #==============================================================================
  2. # Random enemies parties
  3. # By gerkrt/gerrtunk, Heretic86(Idea)
  4. # Version: 1.0
  5. # License: MIT, credits
  6. # Date: 12/07/2011
  7. # IMPORTANT NOTE: to acces the more actualitzed or corrected version of this
  8. # script check here: http://usuarios.multimania.es/kisap/english_list.html
  9. #==============================================================================
  10.  
  11. =begin
  12.  
  13. ------INTRODUCTION------
  14.  
  15. This script let you use random enemy parties like games like Dragon Quest. You
  16. define a max, min, and the appear rate of each type of enemy in a set of maps, and
  17. the rest is random.
  18.  
  19. -------TROOPS EFFECTS---------
  20.  
  21. The main configuration to do is to add in Random_Enemies hash the troops effects
  22. you need. A troop effect is a seed for a random generation in a set of maps.
  23.  
  24.   Random_enemies = {
  25.     :troop1 => {
  26.       :maps_ids => [1,2,3],
  27.       # Put this to false if not want confiugre manually, all 50% 1-10
  28.       :enemies_appear => false,
  29.       :defined_positions => false,
  30.      
  31.       :max_enemies => 5,
  32.       :min_enemies => 2,
  33.     }
  34.   }
  35.  
  36. :maps_ids : a list of maps ids where will have effect, in any other map it wont.
  37. :max_enemies : to control the challengue. the max enemies to create.
  38. :mix_enemies : to control the challengue. the min enemies to create. The number
  39. of enemies is generated randomly with this range.
  40.  
  41. You have configurated the maps where it happens and the number range, but what
  42. about the enemies types?
  43.  
  44. To do that you have to add a enemy encounter in a map thats in the maps_ids list,
  45. for example, in the map 2. When that combat starts, the number of enemies will
  46. appear in a random way.
  47.  
  48. Note that normally all the enemies have the same % to appear in a combat, but you
  49. can changue that using the :enemies_appear option.
  50.  
  51. :enemies_appear => [[1,3, false,false], [4,5, false, false], [5,8, false, false], ],
  52. Using this, you configure the % of appearance of each enemy, and also some of his
  53. partydefined attributes like immortal(cant dead) and invisible.
  54.  
  55. [1,3, false,false]
  56. Enemy id, % appear(1 to 10), invisible, immortal. Set false to true to make that
  57. the enemy have these options.
  58.  
  59. Anyway if just dont need that, make it =>false and they will have the same probabilities.
  60.  
  61.  
  62. ------ENEMIES POSITIONS---------
  63.  
  64. As the number of the enemies are random,  you cant use the positions in the
  65. database normally. You can define a set of enemy positions for each troop, in order,
  66. to the max of the rpgmaker thats 8.
  67.  
  68. You can have some troubles with enemies only using a enemies positions list  
  69. for all of them, thats why each troop can define his own positions.
  70.  
  71. To define the default enemies positions you have to go to troop n1 in the database
  72. and set any up 8 enemies(or the max you will use) in the positions you like.
  73. The scipt will use these in the same order and for all enemies.
  74.  
  75. To define defined enemies you have to add this line to a troop:
  76. :defined_positions => 4,
  77. instead of false.
  78.  
  79. Then it will work like the default troop in database, but with troop id=4 this time.
  80. Make the same thing.
  81.  
  82. -------USING THE DEFAULT WAY---------
  83.  
  84. If you go in a combat that havent defined a troop in this script, that will be loaded
  85. like a normal one, using the database.
  86.  
  87. ------SYNTAX--------------
  88.  
  89. To add new troop effects just add these each time:
  90.  
  91.  
  92.     :troop2 => {
  93.       :maps_ids => [4,5],
  94.       # Put this to false if not want confiugre manually, all 50% 1-10
  95.       :enemies_appear => false,
  96.       :defined_positions => 1,
  97.      
  98.       :max_enemies => 8,
  99.       :min_enemies => 5,
  100.     },
  101.     but updating troopX, to 3, 4, etc.
  102.  
  103. =end
  104.  
  105. module Wep
  106.   Random_enemies = {
  107.     :troop1 => {
  108.       :maps_ids => [1,2,3],
  109.       # Put this to false if not want confiugre manually, all 50% 1-10
  110.       :enemies_appear => [[1,3, false,false], [4,5, false, false], [5,8, false, false], ],
  111.       :defined_positions => false,
  112.      
  113.       :max_enemies => 5,
  114.       :min_enemies => 2,
  115.     },
  116.    
  117.     :troop2 => {
  118.       :maps_ids => [4,5],
  119.       # Put this to false if not want confiugre manually, all 50% 1-10
  120.       :enemies_appear => false,
  121.       :defined_positions => 1,
  122.      
  123.       :max_enemies => 8,
  124.       :min_enemies => 5,
  125.     }
  126.   }
  127.  
  128.  
  129. end
  130.  
  131.  
  132.  
  133.  
  134. class Game_Enemy
  135.   attr_reader :enemy_id
  136. end
  137.  
  138. #==============================================================================
  139. # ** Game_Troop
  140. #------------------------------------------------------------------------------
  141. #  This class deals with troops. Refer to "$game_troop" for the instance of
  142. #  this class.
  143. #==============================================================================
  144.  
  145. class Game_Troop
  146.    
  147.   #--------------------------------------------------------------------------
  148.   # * Setup
  149.   #     troop_id : troop ID
  150.   #--------------------------------------------------------------------------
  151.   def setup(troop_id)
  152.     # Set array of enemies who are set as troops
  153.     @enemies = []
  154.    
  155.     # Initialize roulette
  156.     roulette = []
  157.     enes_appear = []
  158.    
  159.     # Loop
  160.     count = 0
  161.     # Iterate in troops for the one of active map
  162.     for key, value in Wep::Random_enemies
  163.       troop = Wep::Random_enemies[key]
  164.       # Check it
  165.       if troop[:maps_ids].include? $game_map.map_id
  166.         # Save it
  167.         used_troop = troop
  168.         # Iterate in all enemies
  169.         for enemy in $data_troops[troop_id].members
  170.           #p enemy
  171.           # Push the game enemy n times.
  172.           n = 0
  173.           # When default use 5
  174.           if not troop[:enemies_appear]
  175.             n = 5
  176.           else
  177.             enes_appear = troop[:enemies_appear]
  178.            for ene in troop[:enemies_appear]
  179.              
  180.              #p 'ene id ', ene[0], enemy.enemy_id
  181.              if ene[0] == enemy.enemy_id
  182.                n = ene[1]
  183.              end
  184.            end
  185.            # A default value herE?
  186.           end
  187.           #p 'N', n
  188.           n.times do
  189.             roulette.push(enemy.enemy_id) ;# p enemy.enemy_id
  190.    
  191.            
  192.         end
  193.  
  194.        
  195.       end
  196.       count+=1
  197.      
  198.     end
  199.     #p roulette
  200.     # If roulette size is 0 means that no map have been found for this effect.
  201.     # Then uses the normal way
  202.     if roulette.size == 0
  203.       #p 'normal'
  204.       troop = $data_troops[troop_id]
  205.       for i in 0...troop.members.size
  206.         enemy = $data_enemies[troop.members[i].enemy_id]
  207.         if enemy != nil
  208.           @enemies.push(Game_Enemy.new(troop_id, i))
  209.         end
  210.       end
  211.      
  212.     # The random way
  213.     else
  214.       #p 'rando'
  215.       # Generate enemies
  216.       # Select random enemy until max and starting in min
  217.  
  218.       val = 0
  219.      
  220.       val = rand(troop[:max_enemies])
  221.       val = troop[:min_enemies] if val < troop[:min_enemies]
  222.       val = troop[:max_enemies] if val > troop[:max_enemies]
  223.      
  224.       count = 0
  225.       for i in 0..val
  226.         #p 'times',i, val, $data_troops
  227.         if used_troop[:defined_positions]
  228.           j = [used_troop[:defined_positions]][0]
  229.           #p 'defined', [used_troop[:defined_positions]][0], i
  230.           x = $data_troops[j].members[count].x
  231.           y = $data_troops[j].members[count].y
  232.         else
  233.           #p 'default'
  234.           x = $data_troops[1].members[count].x
  235.           y = $data_troops[1].members[count].y
  236.         end    
  237.         #x = arr[count][:x]
  238.         #y = arr[count][:y]
  239.         ene = roulette[rand(roulette.size)]
  240.         for eneapp in enes_appear
  241.           if eneapp[0] == ene
  242.             hidd = eneapp[2]
  243.             imm = eneapp[3]
  244.           end
  245.         end
  246.         # Troop_id, member index
  247.         @enemies.push(Game_Enemy.new(troop_id, i, ene, hidd, imm, x, y))
  248.          count +=1
  249.       end
  250.    
  251.     end
  252.   end
  253.  end
  254. end
  255.  
  256. #==============================================================================
  257. # ** Game_Enemy
  258. #------------------------------------------------------------------------------
  259. #  This class handles enemies. It's used within the Game_Troop class
  260. #  ($game_troop).
  261. #==============================================================================
  262.  
  263. class Game_Enemy < Game_Battler
  264.   #--------------------------------------------------------------------------
  265.   # * Object Initialization
  266.   #     troop_id     : troop ID
  267.   #     member_index : troop member index
  268.   #--------------------------------------------------------------------------
  269.   def initialize(troop_id, member_index, enemy_id, hidden, immortal, x, y)
  270.     super()
  271.     @troop_id = troop_id
  272.     @member_index = member_index
  273.     troop = $data_troops[@troop_id]
  274.     @enemy_id = enemy_id
  275.     enemy = $data_enemies[@enemy_id]
  276.     @battler_name = enemy.battler_name
  277.     @battler_hue = enemy.battler_hue
  278.     @hp = maxhp
  279.     @sp = maxsp
  280.     @x = x
  281.     @y = y
  282.     @hidden = hidden
  283.     @immortal = immortal
  284.   end
  285.     #--------------------------------------------------------------------------
  286.   # * Get Battle Screen X-Coordinate
  287.   #--------------------------------------------------------------------------
  288.   def screen_x
  289.     return @x
  290.   end
  291.   #--------------------------------------------------------------------------
  292.   # * Get Battle Screen Y-Coordinate
  293.   #--------------------------------------------------------------------------
  294.   def screen_y
  295.     return @y
  296.   end
  297.   #--------------------------------------------------------------------------
  298.   # * Get Battle Screen Z-Coordinate
  299.   #--------------------------------------------------------------------------
  300.   def screen_z
  301.     return screen_y
  302.   end
  303. end
Expand to see the code.
You know what... I thought of something remotely like this, even quite a while ago... that was when I had no fucking clue how to script anything, and I never thought of it again. Now you made it, and while I personally don't use RM anymore, I wonder how everyone else could've lived without a script like this really... good idea!

Now for the critics... I'd really make use of the Monster Party tab if I were you, instead of creating a redundant hash or two in your script that are even harder to use. Simply follow these:
- each map ID translates to the same monster party ID
- all enemies you put in a monster party may appear in battles on that map
- the more of one enemy there is in the same monster party, the more lkely that monster is to appear
- the name of the monster party determines min and max monster amount, if not defined globally (which might probably make sense)

I can imagine that to be more awesome, easier to use and easier to maintain, as you got visual feedback on what you're doing.
I will update it when i have time.

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=75064