Random Stat Variance for Items

Aug 7th, 2011
193
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #==============================================================================
2. #    Random Stat Variance for Items
3. #    Version: 1.0
4. #    Author: modern algebra (rmrk.net)
5. #    Date: August 7, 2011
6. #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7. #  Description:
8. #
9. #    This script allows weapons or armors to have randomly varying stats. So,
10. #   one long sword could have 5 ATK while another could have 7 ATK.
11. #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
12. #  Instructions:
13. #    This script REQUIRES Instance Items Base.
14. #
15. #    It is very easy to use, however, and it will work for any item stat that
16. #   is stored as an integer. All you need to do is use this code in the item's
17. #   notebox:
18. #      \VAR_stat[variance]
19. #        stat : the stat you want to vary. It has to be a stat that stores an
20. #          integer, and it must be the name of the method that returns it.
21. #        variance : maximum amount you want the stat to vary by. It must be an
22. #          integer. It will take a random number between 0 and this number and
23. #          add it to the default stat value.
24. #
25. #    Valid stat strings:
26. #      ITEM - base_damage, variance, atk_f, spi_f, parameter_type,
27. #        parameter_points, hp_recovery_rate, hp_recovery, scope, speed,
28. #        occasion, mp_recovery_rate, mp_recovery, price, common_event_id
29. #      WEAPON - atk, def, agi, spi, hit, price
30. #      ARMOR - atk, def, spi, agi, eva, kind, price
31. #
32. #    EXAMPLES:
33. #      \VAR_ATK[4] - If the ATK stat is set as, for instance 5, then the atk
34. #        stat of this item will be anywhere between 5 and 9.
35. #      \var_def[3] - The DEF of this weapon or armor will be between the
36. #        default + rand(4)
37. #==============================================================================
38.
39. if \$imported && \$imported["MAInstanceItemsBase"]
40.
41. \$imported["MARandomVarianceItems"] = true
42. #==============================================================================
43. # ** BaseItem
44. #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
45. #  Summary of Changes:
46. #    new method - random_variance
47. #==============================================================================
48.
49. class RPG::BaseItem
50.   # Push the Variance code into the checks for instance items
51.   MA_INSTANCE_CHECKS.push (/\\VAR_.*?\[\d+\]/i)
52.   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
53.   # * Random Variance
54.   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
55.   def random_variance
56.     unless @rand_vars
57.       @rand_vars = {}
58.       self.note.scan (/\\VAR_(.*?)\[(\d+)\]/i) { |stat, var| @rand_vars[stat.downcase] = var.to_i }
59.     end
60.     return @rand_vars
61.   end
62. end
63.
64. #==============================================================================
65. # *** II_BaseItem
66. #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
67. #  Summary of Changes:
68. #    aliased method - setup
69. #==============================================================================
70.
71. module II_BaseItem
72.   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
73.   # * Setup
74.   #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
75.   alias malgbr_etups_ranvarsts_7jh3 setup
76.   def setup (item, *args, &block)
77.     malgbr_etups_ranvarsts_7jh3 (item, *args, &block) # Run Original Method
78.     item.random_variance.each { |stat, variance|
79.       # If the operation will work
80.       if self.class.method_defined? (stat.to_sym) && (self.send (stat)).is_a? (Numeric) &&
81.           self.class.method_defined? ("#{stat}=".to_sym)
82.         self.send ("#{stat}=", self.send (stat) + rand(variance + 1))
83.       end
84.     }
85.   end
86. end
87.
88. end
RAW Paste Data