bgillisp

Encounter Control

Oct 26th, 2014
971
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.29 KB | None | 0 0
  1. # Script: Encounter Control, Version 0.41
  2. # Author: bgillisp
  3. # Released 1/26/2015
  4.  
  5. # This script is designed to give game developers more control over
  6. # how encounters are managed in the game. Currently in developement
  7.  
  8. # Current Features
  9. # -Ability to designate a switch that if the switch is ON, all random
  10. # fights are off for the map.
  11. # -Ability to set a minimum number of steps per map that must
  12. # be taken before an encounter is met. Now compatabile with Yanfly's
  13. # encounter manager, and will use the encounter manager's global
  14. # minimum if no minimum is set for the map.
  15. # -Ability to turn off all random fights in the game with one swtich
  16. # -Ability to set a uniform distribution, using the number in the
  17. # editer as the mean, then the number in the notetag as the
  18. # variance. Currently it goes 3 standard deviations out from the
  19. # mean in either direction
  20. # -Ability to set a maximum number of battles per map (resets when
  21. # map changed or game loaded). Works exactly like Ar Tonelico if
  22. # you played that game.
  23. # -You can now set a switch to make it so that fixed battles
  24. # can be preemptive or surprise attack now.
  25. # -You can set a swtich to make it so that all battles are
  26. # surprise or preemptive.
  27. # -Combine the last two to make a fixed fight on the map
  28. # be a surprise attack. Now those thiefs really can get the
  29. # drop on you!
  30.  
  31.  
  32. # Compatability (Known issues):
  33. # -To work with Yanfly's Encounter Rate Manager, this script must
  34. # be installed below it. If you install this script above it
  35. # Yanfly's script will overwrite this script and nothing in here
  36. # will work.
  37.  
  38. # Terms of use: Free for use in commercial and non-commercial games
  39. # with credit given.
  40.  
  41. $imported = {} if $imported.nil?
  42. $imported["BG-EncounterControl"] = true
  43.  
  44. #-----------------------------------------------------------------------------
  45.  
  46. #Editable region
  47.  
  48. #-----------------------------------------------------------------------------
  49.  
  50. module BGEncounter_Control
  51.  
  52. #Set the switch that if it is off, no random fights will occur no matter what
  53. #Set to zero to disable.
  54. Encounter_Off = 0
  55.  
  56. #If this switch is on, the party cannot be surprised
  57. Surprise_Off = 121
  58.  
  59. #If this switch is on, the party cannot get preemptive attacks.
  60. Preemptive_Off = 122
  61.  
  62. #Turn on to force a premeptive fight
  63. Preemptive_Always = 123
  64.  
  65. #Turn on to force a surprise fight
  66. Surprise_Always = 124
  67.  
  68. #Turn on to force fixed battles (or evented battles) to check for surprise
  69. #and preemptive rates. Required to make the four switchs above this work
  70. #on fixed encounters
  71. Check_Preemp_and_Surprise_Always = 125
  72.  
  73. #Set a variable that if this variable is not zero, all random fights will
  74. #be only with the troop id stored in the variable until it is set back to zero.
  75. Troop_Var = 0
  76.  
  77. #Set the Global Maximum number of battles you want per map
  78. Max_Encounters = 30
  79.  
  80. #Set how random fights are determined, according to the following flags
  81. # 1 - Value in the database is the maximum
  82. # 2 - Default RPGMaker setting
  83. # If set to an invalid number, it will default to 1
  84. Encounter_Method = 2
  85.  
  86. #Set the Global Fear Level. The fear level is the point when enemies
  87. #will avoid the party. Default is 99, so random fights will only
  88. #avoid the party once they hit max level.
  89. Fear_Level = 99
  90.  
  91. end
  92.  
  93. #----------------------------------------------------------------------------
  94.  
  95. #Do not edit anything below here unless you know what you are doing.
  96.  
  97. #----------------------------------------------------------------------------
  98.  
  99. class RPG::Map
  100.  
  101. attr_accessor :encounter_switch
  102. attr_accessor :region_switch
  103. attr_accessor :encounter_minimum
  104. attr_accessor :standard_deviation
  105. attr_accessor :number_of_encounters
  106. attr_accessor :fear_level
  107. attr_accessor :region_fear_level
  108.  
  109. def initialize
  110. @region_switch = []
  111. @region_fear_level = []
  112. end
  113.  
  114.  
  115. def load_notetags_encounter_switch
  116.  
  117. #Set the encounter switch to 0 to say no switch used.
  118. @encounter_switch = 0
  119.  
  120. #Set the number of encounters to the global maximum
  121. @number_of_encounters = BGEncounter_Control::Max_Encounters
  122.  
  123. #Yanfly Compatability
  124. if($imported["YEA-EncounterRateManager"] = true)
  125. @encounter_minimun = YEA::ENCOUNTER::MINIMUM_FREE_STEPS
  126. else
  127. @encounter_minimum = 1
  128. end
  129.  
  130. @standard_deviation = 0
  131.  
  132. @fear_level = BGEncounter_Control::Fear_Level
  133.  
  134. #Set the encounter switch, if appliciable
  135. @store = self.note[/(?<=<Encounter Switch: ).*?(?=[>])/].to_i
  136. if(@store && @store != 0)
  137. @encounter_switch = @store
  138. end
  139.  
  140. #Loads the minimum number of steps
  141. @store = self.note[/(?<=<Encounter Minimum: ).*?(?=[>])/].to_i
  142. if(@store && @store != 0)
  143. @encounter_minimum = @store
  144. end
  145.  
  146. #for the normal distribution, experimental
  147. @store = self.note[/(?<=<Normal Distribution: ).*?(?=[>])/].to_i
  148. if(@store && @store != 0)
  149. @standard_deviation = @store
  150. end
  151.  
  152. #Loads the maximum number of battles allowed
  153. @store = self.note[/(?<=<Maximum Encounters: ).*?(?=[>])/].to_i
  154. if(@store && @store != 0)
  155. @number_of_encounters = @store
  156. end
  157.  
  158. #Loads the Fear Level
  159. @store = self.note[/(?<=<Fear Level: ).*?(?=[>])/].to_i
  160. if(@store && @store != 0)
  161. @fear_level = @store
  162. end
  163.  
  164. end
  165.  
  166. end # End RPG::Map
  167.  
  168.  
  169. class Game_Party < Game_Unit
  170.  
  171. #Overwritten method, rate_preemptive
  172. def rate_preemptive(troop_agi)
  173. @prate = (agi >= troop_agi ? 0.05 : 0.03) * (raise_preemptive? ? 4 : 1)
  174.  
  175. if($game_switches[BGEncounter_Control::Preemptive_Always] == true)
  176. @prate = 1.0
  177. end
  178.  
  179. if($game_switches[BGEncounter_Control::Preemptive_Off] == true)
  180. @prate = 0.0
  181. end
  182.  
  183. return @prate
  184. end #End rate_preemptive
  185.  
  186. #Overwritten method, rate_surprise
  187. def rate_surprise(troop_agi)
  188. @srate = cancel_surprise? ? 0 : (agi >= troop_agi ? 0.03 : 0.05)
  189.  
  190. if($game_switches[BGEncounter_Control::Surprise_Always] == true)
  191. return 1.0
  192. end
  193.  
  194. if($game_switches[BGEncounter_Control::Surprise_Off] == true)
  195. return 0.0
  196. end
  197.  
  198. return @srate
  199. end
  200.  
  201. #Average level function is from Yanfly's Enemy Levels script (thanks Yanfly!),
  202. #Pasted here so as to not require the enemy level script for this script to work.
  203. def average_level
  204. lv = 0
  205. for member in all_members; lv += member.level; end
  206. lv /= all_members.size
  207. return lv
  208. end
  209.  
  210. end #End Game_Party Class
  211.  
  212.  
  213. class Game_Map
  214.  
  215. alias bg_map_encounter_switch setup
  216.  
  217. #Alias method: setup
  218. def setup(map_id)
  219.  
  220. #Call Aliased method
  221. bg_map_encounter_switch(map_id)
  222.  
  223. #Set up the map
  224. @map.load_notetags_encounter_switch
  225.  
  226. end
  227.  
  228. def encounter_switch
  229. return @map.encounter_switch ||=0
  230. end
  231.  
  232. def encounter_minimum
  233. return @map.encounter_minimum ||=1
  234. end
  235.  
  236. def standard_deviation
  237. return @map.standard_deviation ||=0
  238. end
  239.  
  240. def number_of_encounters
  241. return @map.number_of_encounters ||= BGEncounter_Control::Max_Encounters
  242. end
  243.  
  244. def fear_level
  245. return @map.fear_level ||= BGEncounter_Control::Fear_Level
  246. end
  247.  
  248.  
  249. #New method, update encounter count
  250. #Num is the number to add (or subtract if negative) from the encounter count
  251.  
  252. #To call manually, use the command $game_map.update_encounter_count(x), where
  253. #x is the number you want the count increased by (negative to decrease)
  254. def update_encounter_count(num)
  255. @map.number_of_encounters += num
  256. if(@map.number_of_encounters < 0)
  257. @map.number_of_encounters = 0
  258. end
  259.  
  260. end #End Update_Encounter_Count
  261.  
  262. end # End Class Game_Map
  263.  
  264.  
  265. class Game_Player < Game_Character
  266.  
  267. attr_accessor :encounter_rate
  268.  
  269. #Set up all aliased functions here
  270. alias bg_map_encounter_switch_update_encounter update_encounter
  271. alias bg_map_encounter_switch_initialize initialize
  272. alias bg_map_encounter_switch_encounter encounter
  273. alias bg_map_encounter_switch_make_encounter_troop_id make_encounter_troop_id
  274.  
  275. #Aliased method, initialize
  276. def initialize
  277. bg_map_encounter_switch_initialize
  278. @encounter_rate = 1.0
  279. end
  280.  
  281. #Alias method, update_encounter
  282. def update_encounter
  283.  
  284. #Return if the parties average level is above or equal to the fear level
  285. if($game_party.average_level >= $game_map.fear_level)
  286. return
  287. end
  288.  
  289. #Call code if the encounter switch is set to a valid number
  290. if($game_map.encounter_switch > 0)
  291.  
  292. #Return if the switch is on, as no encounters.
  293. if($game_switches[$game_map.encounter_switch] == true)
  294. return
  295. end
  296.  
  297. end
  298.  
  299. #If the universal random encounter swtich is off, return as no
  300. #random fights allowed.
  301. if($game_switches[BGEncounter_Control::Encounter_Off] == false && BGEncounter_Control::Encounter_Off != 0)
  302. return
  303. end
  304.  
  305. #If no more encounters left on the map, return
  306. if($game_map.number_of_encounters <= 0)
  307. return
  308. end
  309.  
  310. #Call and return the aliased method
  311. return bg_map_encounter_switch_update_encounter
  312. end
  313.  
  314. #Overwrite method, make encounter count
  315. def make_encounter_count
  316. n = $game_map.encounter_step - $game_map.encounter_minimum
  317.  
  318. #error correction in case encounter min > encounter step
  319. if(n <= 0)
  320. n = 1
  321. end
  322.  
  323. if($game_map.standard_deviation == 0)
  324.  
  325. #Check to see whether to use the default RPGMaker setting, or the other
  326. if(BGEncounter_Control::Encounter_Method == 2)
  327. @encounter_count = rand(n) + rand(n) + $game_map.encounter_minimum
  328. else
  329. @encounter_count = rand(n) + $game_map.encounter_minimum
  330. end
  331.  
  332. elsif
  333. #Ad-hoc normal distribution roll. Currently rolls any value between -3 and 3
  334. #for the value for z
  335. ctr = (rand * 6.0) - 3.0
  336.  
  337. #Compute the value x as follows: x = mu + z * sigma.
  338. @encounter_count = ($game_map.encounter_step + ctr * $game_map.standard_deviation).to_i
  339.  
  340. #Safety check, if the value is < 1 or the minimum, set it to the minimum.
  341. if(@encounter_count < $game_map.encounter_minimum)
  342. @encounter_count = $game_map.encounter_minimum
  343. end #End of check to see if above the minimum
  344.  
  345. end #End of check to see if normal distribution was used.
  346.  
  347. #Add on the encounter boost from Yanfly's script, if installed.
  348. #Note that this script will allow you to go below the minimum with the
  349. #variable is so desired, so be careful!
  350. if($imported["YEA-EncounterRateManager"] = true)
  351. @encounter_count += Variable.encounter_boost
  352. end
  353.  
  354. #Make sure we never went below 1 before exiting.
  355. if(@encounter_count < 1)
  356. @encounter_count = 1
  357. end
  358.  
  359. end
  360.  
  361. #Aliased method encounter
  362. def encounter
  363. #Call aliased method
  364. have_a_battle = bg_map_encounter_switch_encounter
  365.  
  366. #If we just ended a fight, decrease the encounters on the map by one.
  367. if(have_a_battle == true)
  368. $game_map.update_encounter_count(-1)
  369. end
  370.  
  371. #Return the original value for game processing.
  372. return have_a_battle
  373.  
  374. end #End method encounter
  375.  
  376. #Aliased method make_encounter_troop_id
  377. def make_encounter_troop_id
  378. #Call aliased method, store returned value
  379. current_troop = bg_map_encounter_switch_make_encounter_troop_id
  380.  
  381. #If the variable is set for a forced troop, return that.
  382. if($game_variables[BGEncounter_Control::Troop_Var] != 0 && current_troop != 0)
  383. return $game_variables[BGEncounter_Control::Troop_Var]
  384. end
  385.  
  386. #Return the returned value from the aliased method
  387. return current_troop
  388. end #End make_encounter_troop_id
  389.  
  390. end # Game_Player
  391.  
  392. #Class Game_Interpreter, for the new handing of surprise and preemptive rates
  393. class Game_Interpreter
  394.  
  395. #Overwriten method, command_301, also known as Battle Processing in the
  396. #Event list
  397. def command_301
  398. return if $game_party.in_battle
  399. if @params[0] == 0 # Direct designation
  400. troop_id = @params[1]
  401. elsif @params[0] == 1 # Designation with variables
  402. troop_id = $game_variables[@params[1]]
  403. else # Map-designated troop
  404. troop_id = $game_player.make_encounter_troop_id
  405. end
  406. if $data_troops[troop_id]
  407. BattleManager.setup(troop_id, @params[2], @params[3])
  408.  
  409. if($game_switches[BGEncounter_Control::Check_Preemp_and_Surprise_Always] == true)
  410. BattleManager.on_encounter
  411. end
  412.  
  413. BattleManager.event_proc = Proc.new {|n| @branch[@indent] = n }
  414. $game_player.make_encounter_count
  415. SceneManager.call(Scene_Battle)
  416. end
  417. Fiber.yield
  418. end #End command_301
  419.  
  420. end #End Class Game_Interpreter
Advertisement
Add Comment
Please, Sign In to add comment