Advertisement
blucalm

Upg Equips - Restrict upgraders used during any scene call

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