Advertisement
blucalm

Upg Equips Regex Correction 12 Dec 12

Dec 12th, 2012
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 39.22 KB | None | 0 0
  1. #===============================================================================
  2. #===============================================================================
  3. # - Upgradable Equipment 0.1 -
  4. # - Eshra -
  5. # - Requested by: Chigoo -
  6. #===============================================================================
  7. #===============================================================================
  8. # -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
  9. # Version 0.8: Release Date August 28, 2012 -
  10. # Compatibility: RPG Maker VX Ace
  11. #
  12. # Updates:
  13. # 2012 Dec 10 - Fixed Freeze during ugrade scene.
  14. # 2012 Dec 9 - Added support to restrict which upgraders can be used during the
  15. # upgrade scene. Use the script call:
  16. # ra_upg(ARRAY)
  17. #
  18. # - replace "ARRAY" with an array of strings -
  19. # to only allow upgrader with a type specified in the array to be
  20. # used.
  21. # 2012 Aug 28 - Added notetag support to limit the amount of upgrades that can
  22. # be used on an item. <!upgradable9> limits the item to no more
  23. # than nine upgrades.
  24. # 2012 Aug 29 - The name of the item has been changed to display how many
  25. # upgrades have been used on it.
  26. # 2012 Sept 4 - UpgradeOrientation::Size::WidthValue can now be used to adjust
  27. # the width of the windows in the upgrade scene
  28. # 2012 Sept 20 - Upgraders can now be specified to only be used with a specific
  29. # Equipment (use the tags <Upgrader "A"> and <Upgraded By "A">)
  30. # -----------------------------------------------------------------------------
  31. # Bug Fixes:
  32. # -----------------------------------------------------------------------------
  33. # 2012 Aug 28 - Added a nil check to fix a crash that was happening when
  34. # selecting a blank item.
  35. # 2012 Aug 28 - Fixed a NameError. Updated a module constant name I had
  36. # forgotten to change after moving the constant to a new module.
  37. # 2012 Aug 28 - Fixed overlapping text inside the item list window
  38. # 2012 Aug 29 - Upgraded equipment is now removed from the database also when it
  39. # is being removed the party's inventory
  40. # 2012 Aug 29 - Text now fits correctly in the equipment selection options window
  41. # 2012 Sept 3 - Misnamed method update_data_base_upg_equips inside RPG::Armor,
  42. # was corrected.
  43. # 2012 Sept 19 - Fixed a problem where armor wasn't being upgraded properly.
  44. # Was referencing $data_weapons inside deep_copy_clone_2544 of
  45. # RPG::Armor, meant to reference $data_armors
  46. # -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
  47. # -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~ Setup -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
  48. # -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
  49. # To install this script, copy and paste it under the materials section and
  50. # above main. This script requires the following bit of code to work:
  51. #
  52. # ($imported||={})["Numeric Form Regex"] = true
  53.  
  54. # module NumericForm
  55. # Regex = /\A(?:-|)\d+(?:\.|)(?:\d+|)\Z/
  56. # end
  57. #
  58. # This is because I don't want to include it in every script I write that needs
  59. # it so just copy and paste that module and the imported hash into a
  60. # section above everything else or into a separate slot in the editor.
  61. #
  62. # -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
  63. # - How to Use -
  64. # -~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-
  65. # You can now add notetags to your items which will allow them to be used as
  66. # upgraders for other items. Add this notetag:
  67. # <!Upgradable>
  68. # To each item that you want to be able to upgrade. Currently only equipment can
  69. # be upgraded.
  70. # You can also add a number after the word upgradable to limit the amount of
  71. # upgrades that can be used on that item. For example:
  72. # <!Upgradable3> limits the amount of upgrades that can be used on an item with
  73. # this tag to 3.
  74. # After you've done that you should create an item that can be used as an
  75. # upgrader. The upgrader notetags should look like this (or just see the regex):
  76. #
  77. # <Mod STAT=5 +90>
  78. # or
  79. # <Mod STAT=8 +90%>
  80. # or
  81. # <Mod STAT=all +90%>
  82. # or
  83. # <Mod STATE=5 15%>
  84. #
  85. # The numbers can be replaced by other numbers. The + sign can also be
  86. # a - sign (a hyphen). The percent signs are required except in the first case.
  87. # When there is no percenet sign present when using the keyword STAT, the
  88. # number following the + or - is considered a raw value (it is required when
  89. # using the keyword STATE). Capitalizaton does not matter.
  90. #
  91. # STAT=number means the stat with an id equal to 'number' will be modified.
  92. # The default id numbers for stats in Ace are:
  93. #
  94. # Max HP: 0 Max MP: 1 Attack Power: 2 Defense: 3 Magic Power: 4 Magic Defense: 5
  95. # Agility: 6 Luck: 7
  96. #
  97. # The notetag <Mod STAT=5 +143> means
  98. # This item will increase the magic defense stat of the equipment it is used to
  99. # upgrade by 143.
  100. # If there was a percent sign after the 143:
  101. # <Mod STAT=5 +143%>
  102. # The magic defense stat for the equipment it was used on would be increased by
  103. # 143% of its original value. If the + sign was changed to a minus the increases
  104. # would become decreases instead. When multiple upgraders with percentage
  105. # modifications are used to upgrade an equipment piece at the same time, the
  106. # percentages modify the original value in ascending order. The change from each
  107. # modification is applied in this order, then the next modification is applied.
  108. #
  109. # The keyword all can be used after STAT= inplace of a number
  110. # <Mod STAT=all +15%>
  111. # This notetag would make all of the stats of the equipment that is upgraded
  112. # with this item to be increased by 15%.
  113. #
  114. # States can also be added to items:
  115. # <Mod STATE=2 45%>
  116. # When an upgrader with this notetag is used to upgrade a piece of equipment,
  117. # that equipment will gain a 45% chance of inflicting state 2.
  118. #
  119. # Upgrades can be specified so that they can only be used on corresponding equips
  120. # Use this tags:
  121. # <Upgrader A>
  122. # on the item you want to use as the upgrader, in this example, this upgrader can
  123. # only be used on upgradable items with this tag: <Upgraded By A>
  124. # The upgraded by tag should be used on an equipment to control which upgrades can
  125. # be used on it. Note, the A can be replaced with any word (no spaces, numbers, etc).
  126. #
  127. # ---------------------------- Calling the Script ------------------------------
  128. # The Upgrade Window can be brought up from an event with this script call:
  129. #
  130. # ra_upg
  131. #
  132. # If you want to restrict which upgraders can be used during the upgrading scene
  133. # use the script call:
  134. # ra_upg(ARRAY)
  135. #
  136. # replace "ARRAY" with an array of strings of the specific upgrader types that
  137. # you want to use during that scene.
  138. # For example if you have 2 items in your database, one is marked with the notetag
  139. # <Upgrader Shield> and the other one is marked with the notetag <Upgrader Defensive>
  140. # and you only wanted to be able to use the "Shield" upgraders during the upgrade
  141. # scene, you would use the script call:
  142. # ra_upg(["Shield"])
  143. #
  144. # ------------------------------- Failure Rate --------------------------------
  145. # The chance that an upgrade will be successful can be modified in
  146. # EquipUpgrades::Player::DefaultFailRate below. Or it can be modified during
  147. # runtime by accessing $game_player.fail_rate_for_upgrades
  148. #
  149. # By default, increasing the value by 1 increases the failure rate by 1%, this
  150. # will change though if EquipUpgrades::Max::BaseRVal is modified.
  151. # The failure rate at the start of a new game will be equal to
  152. # EquipUpgrades::Player::DefaultFailRate/ EquipUpgrades::Max::BaseRVal
  153. #
  154.  
  155.  
  156. $imported = {} if $imported.nil?
  157. raise "Upgradable Equipment requires Tsuki_CustomDataManager" unless $imported["Tsuki_CustomDataManager"]
  158. raise "Upgradable Equipment requires module NumericForm" unless $imported["Numeric Form Regex"]
  159. $imported["Ra Upgradable Equipment"] = true
  160.  
  161. #==============================================================================
  162. # Settings, these can be modified to customize the script a bit.
  163. #==============================================================================
  164. #
  165. # * UpgradeOrientation
  166. # Window orientation
  167. #
  168. module UpgradeOrientation
  169. module Size
  170. WidthValue = 0.6 # Modifies the width of the windows in the upgrade scene
  171. end
  172. end # End - UpgradeOrientation
  173.  
  174. #
  175. # * EquipUpgrades
  176. # Handles settings the user can modify for various effects.
  177. #
  178. module EquipUpgrades
  179. module Regexp # The regular expressions used to parse upgraders, etc.
  180. State = /<Mod STATE(?:=| )(\d+) (\d+)%>/i
  181. StatPercent = /<Mod STAT(?:=| )(\d+|all) (\+|\-)(\d+)%>/i
  182. StatRaw = /<Mod STAT(?:=| )(\d+|all) (\+|\-)(\d+)>/i
  183. Upgradable = /<(?:!|)Upgradable(?: |)(\d+|)>/i
  184. Upgrader = /<(?:!|)Upgrader (.+)>/i # upgraders can be given an upgrader number
  185. Upgraded_By = /<(?:!|)Upgraded By (.+)>/i # only effected be upgraders w/ this no.
  186. end # End - Regexp
  187.  
  188. module Player
  189. DefaultFailRate = 0 # When determining success, a number is generated between
  190. # 0 and 100. if the generated number + 1 is less than
  191. # fail_rate then the upgrade is unsuccessful.
  192. # By default increasing the DefaultFailRate to 1 gives
  193. # a 1% fail rate, 2=2%, etc.
  194. SuccessAnimation = 33
  195. FailAnimation = 96
  196. FailureMod = 0.9 # modifes the effect of failing to upgrade by the
  197. # specified rate
  198. DisplayAnimation = true # Set this to false to turn off the animations
  199. end # End - Player
  200.  
  201. module Max
  202. StatId = 8 # The number of stats the original value in ace is 8
  203. BaseRVal = 100 # The highest random number generated will be this value
  204. # minus 1. the formula for calculating upgrade success is:
  205. # random_val < BaseRVal - DefaultFailRate ? success : fail
  206. IconWidth = 24 # The width of the icons you're using
  207. end # End - Max
  208.  
  209. module EquipPhrase
  210. Equips = "Equipment"
  211. Upgrader = "Upgrades"
  212. end # End - EquipPhrase
  213.  
  214. module Symbols
  215. UpgAll = :upgrade_all
  216. AcpAll = :accept_all
  217. end
  218. end # End - EquipUpgrades
  219. #==============================================================================
  220. # End of Settings, don't modify below here unless you know what you're doing.
  221. #==============================================================================
  222.  
  223. #==============================================================================
  224. # ** Window_Selectable
  225. #==============================================================================
  226. class Window_Selectable < Window_Base
  227. def get_curr_index
  228. @index && @index >= 0 ? @index : 0
  229. end
  230. end # End - Window_Selectable
  231.  
  232. #==============================================================================
  233. # ** Game_Player
  234. # # new attributes are added to the game player to control upgrade failure
  235. # # rate.
  236. #==============================================================================
  237. class Game_Player < Game_Character
  238. attr_accessor :fail_rate_for_upgrades
  239. attr_accessor :fail_mod_for_upgrades
  240.  
  241. alias game_pl_init_method_uni_fail_rate_upgrades_1842693 initialize
  242. def initialize
  243. game_pl_init_method_uni_fail_rate_upgrades_1842693
  244. @fail_rate_for_upgrades = EquipUpgrades::Player::DefaultFailRate
  245. @fail_mod_for_upgrades = EquipUpgrades::Player::FailureMod
  246. end
  247. end # End - Game_Player
  248.  
  249. #==============================================================================
  250. # ** Window_UpgradeItemList
  251. #------------------------------------------------------------------------------
  252. # This window displays a upgradable items and items that can be used as
  253. # upgraders.
  254. #==============================================================================
  255. class Window_UpgradeItemList < Window_ItemList
  256.  
  257. attr_accessor :data
  258. attr_accessor :only_enable_upgradables
  259.  
  260. #
  261. # Get Activation State of Selection Item
  262. #
  263. def current_item_enabled?; enable?(@data[index]); end
  264.  
  265. #
  266. # Display in Enabled State?
  267. #
  268. def enable?(item)
  269. only_enable_upgradables ? item && item.is_upgradable : item && item.upgrade_mods
  270. end
  271.  
  272. #
  273. # * Draw Item
  274. #
  275. def draw_item(index)
  276. item = @data[index]
  277. if item
  278. rect = item_rect(index)
  279. rect.width -= 4
  280. draw_item_name(item, rect.x, rect.y, enable?(item), 90)
  281. draw_item_number(rect, item)
  282. end
  283. end
  284.  
  285. #
  286. # * Draw Item Number
  287. #
  288. def draw_item_number(rect, item)
  289. draw_text(rect, sprintf("%2d", $game_party.item_number(item)), 2)
  290. end
  291. end # End- Window_UpgradeItemList
  292.  
  293. #==============================================================================
  294. # ** Window_UpgradeItemCategory
  295. #------------------------------------------------------------------------------
  296. # The category window for the upgradable items scene
  297. #==============================================================================
  298. class Window_UpgradeItemCategory < Window_ItemCategory
  299. def window_width
  300. Graphics.width * UpgradeOrientation::Size::WidthValue
  301. end
  302.  
  303. def col_max
  304. return 4
  305. end
  306.  
  307. #
  308. # The categories of items that are displayed in the upgradable scene
  309. #
  310. def make_command_list
  311. add_command(Vocab::weapon, :weapon)
  312. add_command(Vocab::armor, :armor)
  313. add_command(Vocab::item, :item)
  314. add_command(Vocab::key_item, :key_item)
  315. end
  316. end # End - Window_UpgradeItemCategory
  317.  
  318. #==============================================================================
  319. # ** Window_UpgradeItemCategory
  320. #------------------------------------------------------------------------------
  321. # The window which displays the icons for the items which are being used to do
  322. # the upgrade
  323. #==============================================================================
  324. class Window_DisplayUpgrades < Window_Base
  325. attr_accessor :content_icons
  326.  
  327. def initialize(x,y,h)
  328. @window_width =(1-UpgradeOrientation::Size::WidthValue)*Graphics.width
  329. super(x,y,@window_width,h)
  330. @content_names, @content_icons = [], []
  331. end # End - initialize
  332.  
  333. def set_item(item)
  334. contents.clear
  335. draw_item_name(item,0,0,true,@window_width-40)
  336. end # End - set_item
  337.  
  338. def add_new_icon(id)
  339. @content_icons.push(id)
  340. draw_all_icons
  341. end # End - add_new_icon
  342.  
  343. def draw_all_icons
  344. contents.clear
  345. index, x_pos = 0,0
  346. @content_icons.each{ |id|
  347. draw_icon(id, x_pos, line_height * index, true)
  348. index += 1
  349. if(index > 6)
  350. index = 0
  351. x_pos += EquipUpgrades::Max::IconWidth
  352. end # End - index > 6
  353. } # End - @content_icons.each
  354. end # End - draw_all_icons
  355.  
  356. def clear_all
  357. contents.clear
  358. @content_names = []
  359. @content_icons = []
  360. end # End - clear_all
  361. end # End - Window_DisplayUpgrades
  362.  
  363.  
  364.  
  365. class Window_UpgradeSelection < Window_HorzCommand
  366.  
  367. #
  368. # Public Instance Variables
  369. #
  370. attr_reader :item_window
  371.  
  372. #
  373. # Object Initialization
  374. #
  375. def initialize(x,y, upgrader)
  376. @type = upgrader
  377. @col_max = upgrader ? 3 : 2
  378. super(x, y)
  379. end # End - initialize
  380.  
  381. #
  382. # Get Window Width
  383. #
  384. def window_width
  385. Graphics.width * (1-UpgradeOrientation::Size::WidthValue)
  386. end # End - window_width
  387.  
  388. #
  389. # Get Digit Count
  390. #
  391. def col_max
  392. return @col_max
  393. end # End - col_max
  394.  
  395. #
  396. # Create Command List
  397. #
  398. def make_command_list
  399. if !@type
  400. add_command(EquipUpgrades::EquipPhrase::Equips, :equip)
  401. add_command("Ok", :advance)
  402. else
  403. add_command(EquipUpgrades::EquipPhrase::Upgrader, :upgrades)
  404. add_command("Clear", :clear)
  405. add_command("Done", :done)
  406. end # End - if !@type
  407. end # End - make_command_list
  408. end # End - Window_UpgradeSelection
  409.  
  410. #==============================================================================
  411. # ** DataManager
  412. #==============================================================================
  413. module DataManager
  414. #
  415. # * alias load_database
  416. #
  417. class <<self; alias load_db_upgrade_mods_82372934 load_database; end
  418. def self.load_database
  419. load_db_upgrade_mods_82372934
  420. load_ntags_upgrade_mods_82372933
  421. end
  422.  
  423. #
  424. # load_ntags_upgrade_mods_82372933
  425. #
  426. def self.load_ntags_upgrade_mods_82372933
  427. groups = [$data_armors, $data_weapons, $data_items]
  428. for group in groups
  429. for obj in group
  430. next if obj.nil?
  431. obj.load_ntags_upgrade_mods
  432. end
  433. end
  434. end
  435.  
  436. end # DataManager
  437.  
  438. #==============================================================================
  439. # ** RPG::BaseItem
  440. #==============================================================================
  441. class RPG::BaseItem
  442. attr_accessor :max_upgrades # The max number of upgrades usable on this item
  443. attr_accessor :curr_upgrades # list of current upgrades on this item
  444. attr_accessor :upgrade_mods
  445. attr_accessor :is_upgradable
  446. attr_accessor :orig_name # Used when making deep copies and it's necessary
  447. # to know the name of the original item while still
  448. # being able to change the name of the new item.
  449. attr_accessor :upgraded_by # A hash of symbols corresponding to ids for items
  450. # that can be used to upgrade this item.
  451. attr_accessor :upgrader_ids # symbol representing the type of upgradable equips
  452. # this can upgrade.
  453. #
  454. # Parses notetags and stores corresponding data
  455. #
  456. def load_ntags_upgrade_mods
  457. self.note.split(/[\r\n]+/).each { |line|
  458. next unless line != nil# && $1 != nil
  459. case line
  460. when EquipUpgrades::Regexp::State
  461. add_state_upgrade_mods($1.to_i, $2.to_i)
  462. when EquipUpgrades::Regexp::StatPercent
  463. add_stat_mod(:percent, $1, $3.to_i, $2)
  464. when EquipUpgrades::Regexp::StatRaw
  465. add_stat_mod(:raw, $1, $3.to_i, $2)
  466. when EquipUpgrades::Regexp::Upgradable
  467. @is_upgradable = true
  468. if (val=$1.to_s) =~ NumericForm::Regex #init max upgrades if it was defined
  469. @max_upgrades = val.to_i
  470. @curr_upgrades = []
  471. end
  472. when EquipUpgrades::Regexp::Upgrader
  473. (@upgrader_ids ||= []).push($1.to_sym)
  474. when EquipUpgrades::Regexp::Upgraded_By
  475. (@upgraded_by ||= {})[$1.to_sym] = true
  476. end
  477. }
  478. init_upgrade_id
  479. end # End - load_ntags_upgrade_mods
  480.  
  481. #
  482. # initializes :accept_all to true inside the upgraded_by hash if it's nil
  483. #
  484. def init_upgrade_id
  485. (@upgraded_by = {})[EquipUpgrades::Symbols::AcpAll] = true if @upgraded_by.nil?
  486. (@upgrader_ids=[]).push(EquipUpgrades::Symbols::UpgAll) unless !@upgrader_ids.nil?
  487. end # End - init_upgrade_id_hashes
  488.  
  489. #
  490. # store state modifiers
  491. #
  492. def add_state_upgrade_mods(id, val)
  493. init_upgrade_mods
  494. @upgrade_mods.store_state_mod(id, val * 0.01)
  495. end # End - add_state_upgrade_mods
  496.  
  497. #
  498. # store base stat modifiers percentages mapped from :percent, raw values from
  499. # :raw
  500. #
  501. def add_stat_mod(sym, id, val, op)
  502. init_upgrade_mods
  503. val *= 0.01 if sym == :percent
  504. val = op.eql?("-") ? -val : val
  505. @upgrade_mods.store_stat_mod(sym, id, val)
  506. end
  507.  
  508. #
  509. # initialize @upgrade_mods if it is nil
  510. #
  511. def init_upgrade_mods
  512. @upgrade_mods = Equipment_Upgrader.new unless @upgrade_mods
  513. end
  514.  
  515. #
  516. # adds a new feature to the item
  517. #
  518. def add_new_feature_upg(feature); @features.push(feature); end
  519.  
  520. #
  521. # Check if This is an Armor or Weapon
  522. #
  523. def is_equip?
  524. self.is_a?(RPG::Armor) || self.is_a?(RPG::Weapon)
  525. end
  526.  
  527. #
  528. # * Upgraded Name
  529. #
  530. def upgrade_name
  531. @name = "Upgraded " + @orig_name + " +#{@curr_upgrades.size}"
  532. end # End - upgrade_name
  533. end # End - RPG::BaseItem
  534.  
  535. #==============================================================================
  536. # ** RPG::Weapon
  537. #------------------------------------------------------------------------------
  538. # A Method to create a deep copy is added
  539. #==============================================================================
  540. class RPG::Weapon < RPG::EquipItem
  541.  
  542. #
  543. # * Deep Copy
  544. #
  545. def deep_copy_clone_2544
  546. (wep = Marshal.load(Marshal.dump(self))).id = $data_weapons.size
  547. wep.orig_name = @orig_name ? @orig_name : (@orig_name = wep.name)
  548. wep.description = wep.description #+ " (Upgraded)"
  549. update_database_upg_equips(wep)
  550. wep
  551. end # End - deep_copy_clone_2544
  552.  
  553. #
  554. # * Update Database
  555. #
  556. def update_database_upg_equips(wep=nil)
  557. print "custom weapons size before update = #{$custom_weapons.size}\n"
  558. $custom_weapons.delete(self)
  559. $custom_weapons.push(wep) unless !wep
  560. $data_weapons.push(wep) unless !wep # update database
  561. print "custom weapons size after update = #{$custom_weapons.size}\n"
  562. end # End - update_database_upg_equips
  563. end # End - RPG::Weapon
  564.  
  565. #==============================================================================
  566. # ** RPG::Armor
  567. #------------------------------------------------------------------------------
  568. # A Method to create a deep copy is added
  569. #==============================================================================
  570. class RPG::Armor < RPG::EquipItem
  571.  
  572. #
  573. # * Deep Copy
  574. #
  575. def deep_copy_clone_2544
  576. (arm = Marshal.load(Marshal.dump(self))).id = $data_armors.size
  577. arm.orig_name = @orig_name ? @orig_name : (@orig_name = arm.name)
  578. arm.description = arm.description #+ " (Upgraded)"
  579. update_database_upg_equips(arm)
  580. end # End - deep_copy_clone_2544
  581.  
  582. #
  583. # * Update Database
  584. #
  585. def update_database_upg_equips(arm=nil)
  586. print "custom armor size before db update #{$custom_armors.size}\n"
  587. $custom_armors.delete(self)
  588. $custom_armors.push(arm) unless !arm
  589. $data_armors.push(arm) unless !arm # update database
  590. print "custom armor size after db update #{$custom_armors.size}\n"
  591. arm
  592. end
  593. end # End - RPG::Armor
  594.  
  595. #==============================================================================
  596. # ** Equipment_Upgrader
  597. #------------------------------------------------------------------------------
  598. # This class represents a new property for items which will allow those items
  599. # to represent upgraders which can increase the stats of weapons and armor.
  600. #==============================================================================
  601. class Equipment_Upgrader
  602. attr_accessor :states
  603. attr_accessor :stat_mods
  604.  
  605. def initialize
  606. @states = {} # maps state ids to the chance of that state being inflicted
  607. @stat_mods = {} # A hash mapping stat Ids to the symbols :percent and :raw
  608. # these determine how much the effected stat is changed.
  609. end
  610.  
  611. # The percentage value is stored as a float between -1.00 and 1.00
  612. def store_stat_mod(sym, stat_id, val, preserve = false, mod = 1)
  613. @stat_mods[stat_id.to_i] = {} unless @stat_mods[stat_id.to_i]
  614. mod_all_stats(sym, stat_id, val, preserve, mod) unless stat_id.to_s =~ NumericForm::Regex
  615. @stat_mods[stat_id.to_i][sym] = preserve ? @stat_mods[stat_id][sym] + val : val
  616. end
  617.  
  618. #
  619. # stores data which will be added to item.features
  620. #
  621. def store_state_mod(state_id, val, preserve = false, mod = 1)
  622. @states[state_id] = preserve ? @states[state_id] + val : val
  623. end
  624.  
  625. #
  626. # stores data which will modify the stats stored in item.params
  627. #
  628. def mod_all_stats(sym, stat_id, val, preserve = false, mod = 1)
  629. (0..7).each{ |id| store_stat_mod(sym, id, val, preserve, mod)}
  630. end
  631.  
  632. #
  633. # Percentage value modifications to stats
  634. #
  635. def get_stat_mod_percent(stat_id); @stat_mods[stat_id][:percent]; end
  636.  
  637. #
  638. # Non percentage value modifications to stats
  639. #
  640. def get_stat_mod_raw(stat_id); @stat_mods[stat_id][:raw]; end
  641.  
  642. #
  643. # The percent chance of happening this upgrader gives to the state with
  644. # id = param: state_id
  645. #
  646. def get_state_mod(state_id); @states[state_id]; end
  647.  
  648. end # End - Equip_Upgrader
  649.  
  650. #==============================================================================
  651. # ** Game_Party
  652. #------------------------------------------------------------------------------
  653. # Alias Methods:
  654. # gain_item: new functionality to remove upgraded equipment when necessary
  655. #==============================================================================
  656. class Game_Party < Game_Unit
  657. #
  658. # * alias method: gain_item
  659. # removes custom equipment from $custom_[type] if all of param item would
  660. # be removed from the party's inventory
  661. #
  662. alias gain_item_upg_equips_update_db gain_item
  663. def gain_item(item, amount, include_equip = false)
  664. item.update_database_upg_equips if delete_custom?(item, amount)
  665. gain_item_upg_equips_update_db(item, amount, include_equip)
  666. end # End - gain_item
  667. #
  668. # * Check to Remove Custom Equipment From Database
  669. #
  670. def delete_custom?(item, amount)
  671. item && item.is_equip? && amount < 0 && -amount >= item_number(item)
  672. end # End - delete_custom?
  673. end # End - Game_Party
  674.  
  675. #==============================================================================
  676. # ** Scene_UpgradableItem
  677. #------------------------------------------------------------------------------
  678. # Allows items to be upgraded by other items
  679. #==============================================================================
  680.  
  681. class Scene_UpgradableItem < Scene_ItemBase
  682. #
  683. # * Start Processing
  684. #
  685. def start
  686. super
  687. create_help_window
  688. create_equip_select_window
  689. create_display_window_upgradable
  690. create_upgrade_select_window
  691. create_category_window((1-UpgradeOrientation::Size::WidthValue)*Graphics.width,@help_window.height,0,0)
  692. create_item_window((1-UpgradeOrientation::Size::WidthValue)*Graphics.width)
  693. create_upgrade_list
  694.  
  695. @upg_equip = nil
  696. @upgraders = [] # list of items to be used as upgraders for the equipment
  697. @item_is_upgrader = false# Determines which list to show items in once clicked
  698. end # End - start
  699. #
  700. # Specify which upgraders can be used to upgrade items here
  701. #
  702. def specify_upgraders(upg_list)
  703. @ok_upgs = upg_list
  704. end
  705. #
  706. # * Window With Options Equipment or Ok
  707. #
  708. def create_display_window_upgradable
  709. w = Graphics.width * (1-UpgradeOrientation::Size::WidthValue)
  710. h = 48
  711. y = @upgrade_equipment_window.height+@help_window.height
  712.  
  713. @display_window = Window_DisplayUpgrades.new(0,y,h)
  714. @display_window.viewport = @viewport
  715. end # End - create_display_window_upgradable
  716. #
  717. # * Create Equip Select Window
  718. # The base window with options to select on equipment or to just click ok
  719. #
  720. def create_equip_select_window
  721. @upgrade_equipment_window = Window_UpgradeSelection.new(0,@help_window.height, false)
  722. @upgrade_equipment_window.viewport = @viewport
  723. @upgrade_equipment_window.deactivate
  724. @upgrade_equipment_window.unselect
  725. @upgrade_equipment_window.set_handler(:ok, method(:on_upgrade_equip_ok))
  726. @upgrade_equipment_window.set_handler(:cancel, method(:on_equip_select_cancel))
  727. @upgrade_equipment_window.set_handler(:advance, method(:on_equip_select_advance))
  728. end # End - create_equip_select_window
  729. #
  730. # * Upgrade Selection Window
  731. #
  732. def create_upgrade_select_window
  733. y = @help_window.height + @upgrade_equipment_window.height + @display_window.height
  734. @upgrades_selection_window = Window_UpgradeSelection.new(0,y, true)
  735. @upgrades_selection_window.viewport = @viewport
  736. @upgrades_selection_window.set_handler(:upgrades, method(:on_choose_upgrades))
  737. @upgrades_selection_window.set_handler(:clear, method(:on_clear_upgrades))
  738. @upgrades_selection_window.set_handler(:done, method(:on_done))
  739. @upgrades_selection_window.set_handler(:cancel, method(:on_upgrade_cancel))
  740. @upgrades_selection_window.deactivate
  741. @upgrades_selection_window.unselect
  742. end # End - create_upgrade_select_window
  743. #
  744. # * Upgrade List
  745. #
  746. def create_upgrade_list
  747. th1, th2 = @help_window.height, @upgrade_equipment_window.height
  748. th3, th4 = @display_window.height, @upgrades_selection_window.height
  749. y = th1 + th2 + th3 + th4
  750. h = Graphics.height - y
  751. @upgrades_display_window = Window_DisplayUpgrades.new(0,y,h)
  752. @upgrades_display_window.viewport = @viewport
  753. end # End - create_upgrade_list
  754. #
  755. # * Create Category Window
  756. #
  757. def create_category_window(x,y,w,h)
  758. @category_window = Window_UpgradeItemCategory.new
  759. @category_window.viewport = @viewport
  760. @category_window.help_window = @help_window
  761. @category_window.y = y
  762. @category_window.x = Graphics.width * (1-UpgradeOrientation::Size::WidthValue)
  763. @category_window.set_handler(:ok, method(:on_category_ok))
  764. @category_window.set_handler(:cancel, method(:on_category_cancel))
  765. end # End - create_category_window
  766. #
  767. # * Create Item Window
  768. #
  769. def create_item_window(x)
  770. wy = @category_window.y + @category_window.height
  771. wh = Graphics.height - wy
  772. ww = UpgradeOrientation::Size::WidthValue*Graphics.width
  773. @item_window = Window_UpgradeItemList.new(x, wy, ww, wh)
  774. @item_window.viewport = @viewport
  775. @item_window.help_window = @help_window
  776. @item_window.set_handler(:ok, method(:on_item_ok))
  777. @item_window.set_handler(:cancel, method(:on_item_cancel))
  778. @item_window.only_enable_upgradables = true
  779. @category_window.item_window = @item_window
  780. end # End - create_item_window
  781. #
  782. # * Category [OK]
  783. #
  784. def on_category_ok
  785. @item_window.activate
  786. @item_window.select_last
  787. end # End - on_category_ok
  788. #
  789. # * Category [Cancel]
  790. #
  791. def on_category_cancel
  792. if !@item_is_upgrader
  793. @upgrade_equipment_window.activate
  794. @upgrade_equipment_window.select(@upgrade_equipment_window.get_curr_index)
  795. @category_window.deactivate
  796. else
  797. turn_on_upgrades_selection
  798. @category_window.deactivate
  799. end
  800. end # End - on_category_cancel
  801. #
  802. # * Item [OK]
  803. #
  804. def on_item_ok
  805. if !@item_is_upgrader
  806. equip_selected
  807. else
  808. upgrader_selected
  809. end # End - !@item_is_upgrader
  810. end # End - on_item_ok
  811. #
  812. # * Method called when an equipment is selected from on_item_ok
  813. #
  814. def equip_selected
  815. $game_party.gain_item(@upg_equip, 1) unless !@upg_equip
  816. @upg_equip = item
  817. @display_window.set_item(@upg_equip)
  818. $game_party.gain_item(item, -1)
  819. on_clear_upgrades
  820. @item_window.data.delete(item) if !$game_party.has_item?(item)
  821. @item_window.refresh
  822. turn_on_upgrades_selection
  823. @item_window.deactivate
  824.  
  825. end
  826. #
  827. # * Method called when an upgrader is selected from on_item_ok
  828. #
  829. def upgrader_selected
  830. if !check_can_upgrade # already max amount of upgrades in list
  831. #turn_on_upgrades_selection
  832. @item_window.activate
  833. Sound.play_buzzer
  834. return
  835. end
  836. @upgraders.push(item)
  837. @upgrades_display_window.add_new_icon(item.icon_index)
  838. $game_party.gain_item(item, -1)
  839. @item_window.data.delete(item) if !$game_party.has_item?(item)
  840. @item_window.refresh
  841. @item_window.activate
  842. end
  843. #
  844. # * Item [Cancel]
  845. #
  846. def on_item_cancel
  847. @item_window.unselect
  848. @category_window.activate
  849. end # End - on_item_cancel
  850. #
  851. # Handler method which allows items to be selected as upgraders
  852. #
  853. def on_choose_upgrades
  854. @item_is_upgrader = true
  855. @item_window.only_enable_upgradables = false
  856. @upgrades_selection_window.deactivate
  857. @category_window.activate
  858. @category_window.select(@category_window.get_curr_index)
  859. end # End - on_choose_upgrades
  860. #
  861. # Handler method which clears all selected upgrades
  862. #
  863. def on_clear_upgrades
  864. empty_upgrades
  865. turn_on_upgrades_selection
  866. end # End - on_clear_upgrades
  867. #
  868. # Common code used when returning items to player after clearing the
  869. # upgrade list
  870. #
  871. def empty_upgrades
  872. @upgraders.each{ |item|
  873. $game_party.gain_item(item, 1)
  874. }
  875. @upgraders = []
  876. @upgrades_display_window.clear_all
  877. @item_window.refresh
  878. end # End - empty_upgrades
  879. #
  880. # Handler method which allows the equipment to be upgraded
  881. #
  882. def on_done
  883. perform_upgrade
  884. end # End - on_done
  885. #
  886. # Handler method called when upgrade menu is unselected
  887. #
  888. def on_upgrade_cancel
  889. @item_is_upgrader = false
  890. @item_window.only_enable_upgradables = true
  891. @upgrades_selection_window.deactivate
  892. @upgrades_selection_window.unselect
  893. @category_window.activate
  894. @category_window.select(@category_window.get_curr_index)
  895. end # End - on_upgrade_cancel
  896. #
  897. # Allows equipment selection
  898. #
  899. def on_upgrade_equip_ok
  900. @upgrade_equipment_window.deactivate
  901. @category_window.activate
  902. @category_window.select(@category_window.get_curr_index)
  903. end # End - on_upgrade_equip_ok
  904. #
  905. # * Allows advancement to upgrade selection without having to click a new equip
  906. #
  907. def on_equip_select_advance
  908.  
  909. return @upgrade_equipment_window.activate unless @upg_equip
  910. @upgrade_equipment_window.deactivate
  911. turn_on_upgrades_selection
  912. end # End - on_equip_select_advance
  913. #
  914. # * About to exit the scene
  915. #
  916. def on_equip_select_cancel
  917. empty_upgrades
  918. $game_party.gain_item(@upg_equip, 1)
  919. SceneManager.return
  920. end # End - on_equip_select_cancel
  921. #
  922. # * Common code used when activating the upgrade selection menu
  923. #
  924. def turn_on_upgrades_selection
  925. #@item_window.only_enable_upgradables = false
  926. @item_window.only_enable_upgradables = @item_is_upgrader = true
  927. @upgrades_selection_window.activate
  928. @upgrades_selection_window.select(@upgrades_selection_window.get_curr_index)
  929. end # End - turn_on_upgrades_selection
  930. #
  931. # * Check to see if the current item can be upgraded by the selected upgrader
  932. #
  933. def check_can_upgrade
  934.  
  935. # print "item.upgrader_ids = #{item.upgrader_ids.inspect}\n"
  936.  
  937. max = @upg_equip.max_upgrades
  938. size = @upg_equip.curr_upgrades.size # amount of upgrades on equip so far
  939. return false if size + @upgrades_display_window.content_icons.size >= max
  940.  
  941. included_type = false
  942. @ok_upgs ||= []
  943. print "@ok_upgs = #{@ok_upgs.inspect}\n"
  944. print "@upg_equip.upgraded_by = #{@upg_equip.upgraded_by}\n"
  945. print "item.upgrader_ids = #{item.upgrader_ids}\n"
  946.  
  947. item.upgrader_ids.each do |sym|
  948. return true if @ok_upgs.include?(sym.to_s)
  949. included_type = true if @upg_equip.upgraded_by[sym]
  950. end
  951. return false if !@ok_upgs.empty?
  952.  
  953. return true if @upg_equip.upgraded_by[EquipUpgrades::Symbols::AcpAll]
  954. return true if item.upgrader_ids.include? EquipUpgrades::Symbols::UpgAll
  955.  
  956. # print "item #{item.name}, upgrader_ids = #{item.upgrader_ids.inspect}\n"
  957. # item.upgrader_ids.each{|sym| return true if @upg_equip.upgraded_by[sym]}
  958. included_type
  959.  
  960. end
  961. #
  962. # * Perform the Upgrade
  963. # Change the stats on the item that is being upgraded (or downgraded)
  964. def perform_upgrade
  965. return unless upgrade_is_valid? # Make sure the current upgrade is ok
  966. @upg_equip = @upg_equip.deep_copy_clone_2544 # deep copy
  967. modify_stats(failure = upgrade_successful?) # Modify stats based on success
  968. add_new_features(13) unless failure# 13 is the default code for states
  969. set_player_animation(failure)
  970. @upg_equip.upgrade_name # Change the name
  971. $game_party.gain_item(@upg_equip, 1)
  972. SceneManager.return
  973. end # End - perform_upgrade
  974.  
  975. # * Check if Upgrading is acceptable (i.e. no nil vals/ non-equip items, etc.)
  976. def upgrade_is_valid?
  977. # Check if the current upgrade is valid, if not then return
  978. return true unless (@upgraders.empty? || !@upg_equip.is_a?(RPG::EquipItem))
  979. turn_on_upgrades_selection
  980. false
  981. end # End - upgrade_is_valid?
  982. #
  983. # * Was the Upgrade Successful?
  984. #
  985. def upgrade_successful?
  986. rand(EquipUpgrades::Max::BaseRVal) < $game_player.fail_rate_for_upgrades
  987. end # End - upgrade_successful?
  988. #
  989. # * Set Player Animation
  990. #
  991. def set_player_animation(fail)
  992. return unless EquipUpgrades::Player::DisplayAnimation
  993. $game_player.animation_id = EquipUpgrades::Player::SuccessAnimation unless fail
  994. $game_player.animation_id = EquipUpgrades::Player::FailAnimation if fail
  995. end # End - set_player_animation
  996. #
  997. # * Add New Features
  998. # Creates new features to add to item.features
  999. #
  1000. def add_new_features(code)
  1001. @upgraders.each{ |item|
  1002. states = item.upgrade_mods.states
  1003. states.keys.each{ |key|
  1004. feature = RPG::BaseItem::Feature.new(code, key, states[key])
  1005. @upg_equip.add_new_feature_upg(feature)
  1006. }
  1007. }
  1008. end # End - add_new_features
  1009. #
  1010. # * Modify Stats
  1011. # percentages are applied to stats starting with the smallest percentage
  1012. # increasing to the largest. Percentage bonuses are applied before raw bonuses
  1013. #
  1014. def modify_stats(failed = false)
  1015. percents = {} # maps stat ids to an array of percentages
  1016. raws = {} # maps stat ids to an array of values
  1017.  
  1018. # Goes through all the items and condenses all the upgrades into above
  1019. # two hashes
  1020. @upgraders.each{ |item|
  1021. update_item_upgrades(item) #add each 1 to curr upgs list
  1022. stats = item.upgrade_mods.stat_mods
  1023. stats.keys.each{ |key|
  1024. if stats[key]
  1025. percents[key] = [] if !percents[key]
  1026. raws[key] = [] if !raws[key]
  1027.  
  1028. percents[key].push(stats[key][:percent]) unless !stats[key][:percent]
  1029. raws[key].push(stats[key][:raw]) unless !stats[key][:raw]
  1030. end # End - if stats[key]
  1031. }
  1032. } # End - @upgraders.each
  1033.  
  1034. (0...EquipUpgrades::Max::StatId).each{|id|
  1035. if percents[id] # Modify stats by multiplying percentages
  1036. orig_val = @upg_equip.params[id]
  1037. percents[id].sort!.each{ |val|
  1038. mod = upgrade_failed?(failed, orig_val * val) # handle failure case
  1039. @upg_equip.params[id] = mod + orig_val
  1040. }
  1041. end # End - if percents[id]
  1042.  
  1043. if raws[id] # Modify stats by adding raw values
  1044. raws[id].each { |val|
  1045. val = upgrade_failed?(failed, val)
  1046. @upg_equip.params[id]+= val # handle failure case
  1047. }
  1048. end # End - if raws[id]
  1049. } # End - (0...EquipUpgrades::Max::StatId).each
  1050. end # End - modify_stats
  1051. #
  1052. # * Update the Equipments List of Current Upgrades
  1053. #
  1054. def update_item_upgrades(item)
  1055. @upg_equip.curr_upgrades.push(item)
  1056. end # End - update_item_upgrades
  1057. #
  1058. # * Upgrade Failed
  1059. # Calculate the appropriate value to add onto the stat if the upgrade failed
  1060. #
  1061. def upgrade_failed?(did_fail, val)
  1062. mod = $game_player.fail_mod_for_upgrades
  1063. did_fail ? (val > 0 ? val*-mod : val * mod) : val
  1064. end # End - upgrade_failed?
  1065. end # End - Scene_UpgradableItem
  1066. #==============================================================================
  1067. # ** Game_Interpreter
  1068. #==============================================================================
  1069. class Game_Interpreter
  1070. def ra_upg(upgs = nil)
  1071. SceneManager.call(Scene_UpgradableItem)
  1072. SceneManager.scene.specify_upgraders(upgs) if upgs
  1073. end
  1074. end
  1075.  
  1076. #==============================================================================
  1077. # -------------------------------End of File-----------------------------------
  1078. #==============================================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement