TheoAllen

Theo - Enhanced Enemy AI

Jan 15th, 2014
333
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 7.56 KB | None | 0 0
  1. # =============================================================================
  2. # TheoAllen - Enhanced Enemy AI
  3. # Version : 1.0
  4. # Contact : www.rpgmakerid.com (or) http://theolized.blogspot.com
  5. # (This script documentation is written in informal indonesian language)
  6. # =============================================================================
  7. ($imported ||= {})[:Theo_EnemyAI] = true
  8. # =============================================================================
  9. # Change Logs:
  10. # -----------------------------------------------------------------------------
  11. # 2014.01.15 - Finished script
  12. # =============================================================================
  13. =begin
  14.  
  15.   ----------------------------------------------------------------------------
  16.   Perkenalan :
  17.   Secara default, kamu hanya bisa memilih kondisi action pattern di dalam
  18.   database hanya satu. Lalu bagaimana caranya kita gunain dua kondisi secara
  19.   bersamaan? Atawa gunain kondisi sendiri seperti script call? Script ini
  20.   menjawab pertanyaanmu
  21.  
  22.   ----------------------------------------------------------------------------
  23.   Cara penggunaan :
  24.   Pasang script ini di bawah material namun di atas main
  25.   Gunakan tag seperti ini pada notebox enemy di database
  26.  
  27.   AI: NamaKey
  28.  
  29.   Dimana "NamaKey" adalah key untuk custom AI kamu
  30.   Edit konfigurasinya untuk menambahkan custom AI
  31.  
  32.   ----------------------------------------------------------------------------
  33.   Paduan untuk membuat kondisi script :
  34.   Agar kamu bisa menggunakan kondisi default dalam action pattern default, kamu
  35.   harus memanggil script call seperti ini di dalam penulisan kondisi scriptnya
  36.  
  37.   - conditions_met_turns?(start, diff)  # Turn
  38.   - conditions_met_hp?(rate1, rate2)    # HP
  39.   - conditions_met_mp?(rate1, rate2)    # MP
  40.   - conditions_met_state?(state_id)     # State
  41.   - conditions_met_party_level?(level)  # Party level
  42.   - conditions_met_switch?(switch_id)   # Switch
  43.  
  44.   Contoh :
  45.   Jika kamu menginginkan suatu enemy akan melakukan healing di setiap 3 turn
  46.   sekali dan jika hanya HP dibawah 50%, kamu bisa menuliskan seperti ini.
  47.   Wa asumsiin skill_id untuk healing adalah 12
  48.  
  49.   "Heal" => [12,10,"conditions_met_turns?(1,3) && conditions_met_hp?(0.0,0.5)"]
  50.  
  51.   Operand && artinya adalah "AND" dimana jika keduanya benar maka kondisinya
  52.   akan bernilai benar. (Kalo dah diajarin logika matematika pasti dah ngerti)
  53.  
  54.   Kemudian, kamu tinggal menuliskan seperti ini pada notebox
  55.   AI: Heal
  56.  
  57.   Kamu juga bisa mendefinisikan kondisi kamu sendiri. Semisal enemy akan perform
  58.   suatu skill jika nilai suatu variable tertentu mencapai nilai tertentu.
  59.  
  60.   Contoh :
  61.   "Skill-X" => [99, 10, "$game_variables[10] > 999"],
  62.  
  63.   ----------------------------------------------------------------------------
  64.   Terms of Use :
  65.   Credit gw, TheoAllen. Kalo semisal u bisa ngedit2 script gw trus jadi lebih
  66.   keren, terserah. Ane bebasin. Asal ngga ngeklaim aja. Kalo semisal mau
  67.   dipake buat komersil, jangan lupa, gw dibagi gratisannya.  
  68.  
  69.   ----------------------------------------------------------------------------
  70.   Okay, wa akui script ini ribet. Aslinya cuman buat personal use doang sih :v
  71.  
  72. =end
  73. # =============================================================================
  74. # Konfigurasi
  75. # =============================================================================
  76. module Theo
  77.   module AI
  78.     List = { # < --- jangan disentuh
  79.     # =========================================================================
  80.     # Definisikan custom AI kamu disini
  81.     # -------------------------------------------------------------------------
  82.     # Formatnya adalah :
  83.     # "key" => [skill_id, rating, "script eval"]
  84.     #
  85.     # "key" nantinya akan dipanggil untuk notebox
  86.     # Contoh di notebox
  87.     # AI: key
  88.     # -------------------------------------------------------------------------
  89.    
  90. "HealWave" => [41,6,"hp_rate <= 0.65 && conditions_met_turns?(1,2)"],
  91. "Barrier"  => [42,5,"hp_rate <= 0.75 && !state?(14)"],
  92. "Barrier50"  => [42,5,"hp_rate <= 0.50 && !state?(14)"],
  93. "FSkin" => [56,8,"conditions_met_turns?(1,4) && !state?(15)"],
  94. "Barrier+" => [71,6,"conditions_met_turns?(1,2) && !state?(28)"],
  95. "Barrier++" => [71,10,"conditions_met_turns?(3,4) && !state?(28)"],
  96.    
  97.     # tambahin sendiri disini
  98.     # dan disini
  99.     # dan seterusnya
  100.    
  101.     # =========================================================================
  102.     } # < --- jangan disentuh
  103.    
  104.    
  105.     Notetag = /ai\s*:\s*(.+)/i
  106.     # Ga usah diubah-ubah
  107.    
  108.   end
  109. end
  110. # =============================================================================
  111. # Akhir dari konfigurasi
  112. # =============================================================================
  113. class RPG::Enemy
  114.  
  115.   def load_custom_ai
  116.     note.split(/[\r\n]+/).each do |line|
  117.       if line =~ Theo::AI::Notetag
  118.         db = Theo::AI::List[$1.to_s]
  119.         if db
  120.           act = RPG::Enemy::Action.new
  121.           act.condition_type = 7
  122.           act.skill_id = db[0]
  123.           act.rating = db[1]
  124.           act.condition_param1 = db[2]
  125.           actions.push(act)
  126.         else
  127.           msgbox "Uninitialize AI constant #{$1.to_s} for \n" +
  128.             "Enemy #{name}"
  129.           exit
  130.         end
  131.       end
  132.     end
  133.   end  
  134.  
  135. end
  136.  
  137. class << DataManager
  138.  
  139.   alias theo_enemy_ai_load_db load_database
  140.   def load_database
  141.     theo_enemy_ai_load_db
  142.     load_custom_ai
  143.   end
  144.  
  145.   def load_custom_ai
  146.     $data_enemies.compact.each do |enemy|
  147.       enemy.load_custom_ai
  148.     end
  149.   end
  150.  
  151. end
  152.  
  153. class Game_Action
  154.  
  155.   alias theo_custom_ai_prepare prepare
  156.   def prepare
  157.     theo_custom_ai_prepare
  158.     # ------------------------------------------------------------------------
  159.     # Refresh enemy action. Make sure current action is conditionally valid.
  160.     # Two times or more action often performed an action whether the current
  161.     # action is conditionally valid or not
  162.     # ------------------------------------------------------------------------
  163.     if subject.enemy? && !forcing
  164.       skill = subject.enemy_action
  165.       set_enemy_action(skill)
  166.       subject.performed_item.push(item) # mainly aimed for script eval condition
  167.     end
  168.   end
  169.  
  170. end
  171.  
  172. class Game_Enemy
  173.   attr_reader :performed_item
  174.  
  175.   alias theo_custom_ai_init initialize
  176.   def initialize(*args)
  177.     theo_custom_ai_init(*args)
  178.     @performed_item = []
  179.   end
  180.   # --------------------------------------------------------------------------
  181.   # Overwrite conditions met
  182.   # --------------------------------------------------------------------------
  183.   def conditions_met?(action)
  184.     method_table = {
  185.       1 => :conditions_met_turns?,
  186.       2 => :conditions_met_hp?,
  187.       3 => :conditions_met_mp?,
  188.       4 => :conditions_met_state?,
  189.       5 => :conditions_met_party_level?,
  190.       6 => :conditions_met_switch?,
  191.       7 => :conditions_met_script?,
  192.     }
  193.     method_name = method_table[action.condition_type]
  194.     if method_name
  195.       send(method_name, action.condition_param1, action.condition_param2)
  196.     else
  197.       true
  198.     end
  199.   end
  200.  
  201.   def conditions_met_script?(param1, param2)
  202.     eval(param1)
  203.   end
  204.  
  205.   def enemy_action
  206.     action_list = enemy.actions.select {|a| action_valid?(a) }
  207.     return if action_list.empty?
  208.     rating_max = action_list.collect {|a| a.rating }.max
  209.     rating_zero = rating_max - 3
  210.     action_list.reject! {|a| a.rating <= rating_zero }
  211.     return select_enemy_action(action_list, rating_zero)
  212.   end
  213.  
  214. #~   alias theo_custom_ai_turn_end on_turn_end
  215. #~   def on_turn_end
  216. #~     theo_custom_ai_turn_end
  217. #~     @performed_item.clear
  218. #~   end
  219. end
Advertisement
Add Comment
Please, Sign In to add comment