blucalm

Upgradable Equipment Bug fix 5

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