blucalm

Upgradable Equips Update, can specify upgrades

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