Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Random Enchant (I.E. ..."of the Monkey, of the Wolf"

Random Enchant
RMVX Script

Anyone who's played any dungeon crawl or MMO should know of what I'm requesting. Essentially, when I put the tag <random +X> in an item's notes, I want it to add a Random Enchant to it, which will add a suffix to the weapon (like "of the Monkey" and add stats (in addition to the weapon's basic stats) to the weapon (which would be the X specified in the tag.)

I want the following suffixes to be made for items:

"of Strength"
adds Strength

"of Defense"
adds Defense

"of Intellect"
adds Spirit

"of Agility"
adds Agility

"of the Warrior"
adds Strength and Defense

"of the Sorcerer"
adds Strength and Spirit

"of the Beserker"
adds Strength and Agility

"of the Medic"
adds Defense and Spirit

"of the Monk"
adds Defense and Agility

"of the Focused"
adds Spirit and Agility

"of the Cruel"
adds Strength and +Crit

"of the Cunning"
adds Agility and +Crit

"of the Sly"
adds Agility and +Eva

Thank you.
 
All right. We're willing to take this request, but we have a few questions. Are you willing for me to take a few creative liberties to make this easier to use? Specifically, it would be much easier for all involved if the modified values were specified elsewhere, instead of in the tag. Also, give us a week or so to complete the request, because we are a little busy at the moment.

(If you think we're using the royal we, think again. Check out the signature for details.)
 
Scriptastic":wlj5pofg said:
All right. We're willing to take this request, but we have a few questions. Are you willing for me to take a few creative liberties to make this easier to use? Specifically, it would be much easier for all involved if the modified values were specified elsewhere, instead of in the tag. Also, give us a week or so to complete the request, because we are a little busy at the moment.

(If you think we're using the royal we, think again. Check out the signature for details.)

Aye, you can take some creative liberties.
 
All right. We're finished with your request. We actually decided to finish it exactly as you requested, including defining all the enchantments you specified. If you wish to change the enchantments, the instructions are in the header. Otherwise, this script will work exactly like you asked.

Code:
#==============================================================================

# ** Team Scriptastic's Random Enchants

#    Version 0.10

#------------------------------------------------------------------------------

#  This script will give pre-specifiec weapons a random enchant when they are

#  given to the player.

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# * Version History

#  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -

#   Version [0.10] --------------------------------------------- ([2009-07-02])

#     -Initial version

#     -Author: Glitchfinder

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# * Instructions

#  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -

#  Place this script above Main, and below the default scripts. (I realize this

#  is obvious to most, but some people don't get it.)

#

#  This script requires a slight knowledge of scripting.  To enable an item to

#  have random variants, simply add a tag to the notes box, that looks like the

#  following:

#    <random +10>

#  Where 10 is the value you want added to the stats enchanted.

#

#  Below is a list of all possible enchantments.  An enchantment looks like the

#  following hash:

#

#  @enchants['of Strength'] =      {'atk' => true, 'def' => false,

#                                  'spi' => false, 'agi' => false,

#                                  'critical' => false, 'eva' => false}

#

#  Where 'of Strength' is the string added to the name of the base item.  This

#  also serves as an identifier for this enchantment, so two enchantments with

#  the same name will result in only the latter occuring.

#

#  'atk' => false        | if this is true, it would add XX from <random +xx>

#                        | to the weapon's attack stat.

#  'def' => false        | if this is true, it would add XX from <random +xx>

#                        | to the weapon's defense stat.

#  'spi' => false        | if this is true, it would add XX from <random +xx>

#                        | to the weapon's spirit stat.

#  'agi' => false        | if this is true, it would add XX from <random +xx>

#                        | to the weapon's agility stat.

#  'eva' => false        | if this is true, it would add XX from <random +xx>

#                        | to the weapon's evasion stat.

#  'critical' => false   | if this is true, it would add XX from <random +xx>

#                        | to the weapon's critical hit ratio.

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# * Terms of Service

#  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -

#  This script is free for use in private or commercial applications, under the

#  condition that this header remains intact and credit to Team Scriptastic and

#  all authors mentioned in the version history is mentioned in an appropriate

#  section visible to users of the product.

#==============================================================================

 

#==============================================================================

# + Enchants (Module)

#==============================================================================

module Enchants

  @enchants = {}

  @enchants.default =             {'atk' => false, 'def' => false,

                                  'spi' => false, 'agi' => false,

                                  'critical' => false, 'eva' => false}

  @enchants['of Strength'] =      {'atk' => true, 'def' => false,

                                  'spi' => false, 'agi' => false,

                                  'critical' => false, 'eva' => false}

  @enchants['of Defense'] =       {'atk' => false, 'def' => true,

                                  'spi' => false, 'agi' => false,

                                  'critical' => false, 'eva' => false}

  @enchants['of Intellect'] =     {'atk' => false, 'def' => false,

                                  'spi' => true, 'agi' => false,

                                  'critical' => false, 'eva' => false}

  @enchants['of Agility'] =       {'atk' => false, 'def' => false,

                                  'spi' => false, 'agi' => true,

                                  'critical' => false, 'eva' => false}

  @enchants['of the Warrior'] =   {'atk' => true, 'def' => true,

                                  'spi' => false, 'agi' => false,

                                  'critical' => false, 'eva' => false}

  @enchants['of the Sorcerer'] =  {'atk' => true, 'def' => false,

                                  'spi' => true, 'agi' => false,

                                  'critical' => false, 'eva' => false}

  @enchants['of the Berserker'] = {'atk' => true, 'def' => false,

                                  'spi' => false, 'agi' => true,

                                  'critical' => false, 'eva' => false}

  @enchants['of the Medic'] =     {'atk' => false, 'def' => true,

                                  'spi' => true, 'agi' => false,

                                  'critical' => false, 'eva' => false}

  @enchants['of the Monk'] =      {'atk' => false, 'def' => true,

                                  'spi' => false, 'agi' => true,

                                  'critical' => false, 'eva' => false}

  @enchants['of the Focused'] =   {'atk' => false, 'def' => false,

                                  'spi' => true, 'agi' => true,

                                  'critical' => false, 'eva' => false}

  @enchants['of the Cruel'] =     {'atk' => true, 'def' => false,

                                  'spi' => false, 'agi' => false,

                                  'critical' => true, 'eva' => false}

  @enchants['of the Cunning'] =   {'atk' => false, 'def' => false,

                                  'spi' => false, 'agi' => true,

                                  'critical' => true, 'eva' => false}

  @enchants['of the Sly'] =       {'atk' => false, 'def' => false,

                                  'spi' => false, 'agi' => true,

                                  'critical' => false, 'eva' => true}

  #--------------------------------------------------------------------------

  # * Enchant Weapon

  #     weapon : data for specific weapon

  #--------------------------------------------------------------------------

  def self.enchant(weapon)

    # Check if the random tag is in the notes box

    if weapon.note.include?('<random +')

      # If so, load the default weapon index

      weapon_data = load_data("Data/Weapons.rvdata")

      # Check which id the weapon is based on

      weapon_id = weapon.id * rand(@enchants.keys.size) + weapon_data.size

      # Return this weapon data

      weapon = $data_weapons[weapon_id]

    end

    return weapon

  end

  #--------------------------------------------------------------------------

  # * Create Enchanted Weapons

  #     weapon : data for specific weapon

  #--------------------------------------------------------------------------

  def self.create_enchants

    # Create blank weapon array

    weapons = []

    # Iterate through all weapons in $data_weapons

    for weapon in $data_weapons

      # Skip if the current weapon is nil

      next if weapon == nil

      # If the weapon includes the random tag

      if weapon.note.include?('<random +')

        # Add the enchanted weapons to the appropriate classes

        enchant_class(weapon)

        # Iterate through all enchants

        for enchant in @enchants.keys

          # Clone and enchant current weapon

          new_weapon = weapon.clone

          new_weapon = enchant_stats(new_weapon, enchant)

          # Remove the random tag from the enchanted weapon

          new_weapon.note = new_weapon.note.gsub(/<random \+([0-9]+)>/) {''}

          # Push new weapon data to $data_weapons

          $data_weapons.push new_weapon

          # Push new weapons into weapons array

          weapons.push new_weapon

        end

      end

    end

    # Save the new data using the weapons array

    save_enchant(weapons) unless weapons.empty?

  end

  #--------------------------------------------------------------------------

  # * Create Enchant

  #     weapon : data for specific weapon

  #     str    : name of specific enchant

  #--------------------------------------------------------------------------

  def self.enchant_stats(weapon, str)

    # Set new weapon id

    weapon.id = $data_weapons.size

    # Set new weapon name

    weapon.name += ' ' + str

    # Remove the tag from the weapon note

    value = weapon.note.slice(/<random \+([0-9]+)>/)

    value.gsub!(/<random \+/) {''}

    value.gsub!(/>/) {''}

    # Convert the number within the tag string to an integer

    value = value.to_i

    # Add the value to the appropriate stat

    weapon.atk += value if @enchants[str]['atk']

    weapon.def += value if @enchants[str]['def']

    weapon.spi += value if @enchants[str]['spi']

    weapon.agi += value if @enchants[str]['agi']

    weapon.critical += value if @enchants[str]['critical']

    weapon.eva += value if @enchants[str]['eva']

    # Return the weapon data

    return weapon

  end

  #--------------------------------------------------------------------------

  # * Add Enchanted Weapon to Class Lists

  #     weapon : data for specific weapon

  #--------------------------------------------------------------------------

  def self.enchant_class(weapon)

    # Create base class array

    classes = [nil]

    # Iterate through all classes

    for new_class in $data_classes

      # Skip if the current class is nil

      next if new_class == nil

      # If the class used the base weapon 

      if new_class.weapon_set.include? weapon.id

        # Iterate through all IDs for the enchanted version

        for id in [email=0...@enchants.keys.size]0...@enchants.keys.size[/email]

          # Add the new IDs to the useable weapons array

          new_class.weapon_set.push $data_weapons.size + id

        end

      end

      # Add the modified class to the classes array

      classes.push new_class

    end

    # Replace the default classes array with the modified one

    $data_classes = classes

  end      

  #--------------------------------------------------------------------------

  # * Save Enchanted Weapon Data

  #     weapons : data for specific weapon

  #--------------------------------------------------------------------------

  def self.save_enchant(weapons)

    # If the file exists

    if FileTest.exist?('Data/EnchantedWeapons.rvdata')

      # Open the file and load the data

      file = File.open('Data/EnchantedWeapons.rvdata', "rb")

      weapon_data = Marshal.load(file)

      class_data = Marshal.load(file)

    else

      # Otherwise, default to nil

      weapon_data = [nil]

      class_data = [nil]

    end

    # Iterate through all weapons in the weapons variable

    for weapon in weapons

      # Push weapons into a temporary container

      weapon_data.push weapon

    end

    # Iterate through all classes in $data_classes

    for old_class in $data_classes

      # Skip if the current class it nil

      next if old_class == nil

      # Create new instance of extended class data

      new_class = RPG::ClassExtension.new

      # Add the ID and weapon set of the current class

      new_class.id         = old_class.id

      new_class.weapon_set = old_class.weapon_set

      # Push new class datas into a temporary container

      class_data.push new_class

    end

    # Open the enchants file and dump data

    file = File.open('Data/EnchantedWeapons.rvdata', "wb")

    Marshal.dump(weapon_data, file)

    Marshal.dump(class_data, file)

  end

  # Protect private class methods

  private_class_method :enchant_stats

  private_class_method :enchant_class

  private_class_method :save_enchant

end

 

#==============================================================================

# + RPG::BaseItem (module)

#==============================================================================

module RPG

  class BaseItem

    def initialize

      @id = 0

      @name = ""

      @icon_index = 0

      @description = ""

      @note = ""

      @atk = 0

      @def = 0

      @spi = 0

      @agi = 0

      @critical = 0

      @eva = 0

    end

    attr_accessor :id

    attr_accessor :name

    attr_accessor :icon_index

    attr_accessor :description

    attr_accessor :note

    attr_accessor :atk

    attr_accessor :def

    attr_accessor :spi

    attr_accessor :agi

    attr_accessor :critical

    attr_accessor :eva

  end

  #============================================================================

  # + RPG::ClassExtension (module)

  #============================================================================

  class ClassExtension

    def initialize

      @id = 0

      @weapon_set = []

    end

    attr_accessor :id

    attr_accessor :weapon_set

  end

end

 

#==============================================================================

# ** Game_Actor

#------------------------------------------------------------------------------

#  This class handles actors. It's used within the Game_Actors class

# ($game_actors) and referenced by the Game_Party class ($game_party).

#==============================================================================

 

class Game_Actor < Game_Battler

  #--------------------------------------------------------------------------

  # * Get Evasion Rate

  #--------------------------------------------------------------------------

  def eva

    n = 5

    # Add the enchanted weapon's evasion stat

    for item in equips.compact do n += item.eva end

    return n

  end

  #--------------------------------------------------------------------------

  # * Get Critical Ratio

  #--------------------------------------------------------------------------

  def cri

    n = 4

    n += 4 if actor.critical_bonus

    for weapon in weapons.compact

      n += 4 if weapon.critical_bonus

      # Add the enchanted weapon's critical ratio

      n += weapon.critical

    end

    return n

  end

end

 

#==============================================================================

# ** Game_Party

#------------------------------------------------------------------------------

#  This class handles the party. It includes information on amount of gold 

# and items. The instance of this class is referenced by $game_party.

#==============================================================================

 

class Game_Party < Game_Unit

  #--------------------------------------------------------------------------

  # * Alias Methods

  #--------------------------------------------------------------------------

  alias scriptastic_rand_enchant_game_party_gain_item gain_item

  #--------------------------------------------------------------------------

  # * Gain Items (or lose)

  #     item          : Item

  #     n             : Number

  #     include_equip : Include equipped items

  #--------------------------------------------------------------------------

  def gain_item(item, n, include_equip = false)

    # Reaplace item data with that of an enchanted item instead, if applicable

    item = Enchants::enchant(item) if item.is_a? RPG::Weapon

    # Call the original method

    scriptastic_rand_enchant_game_party_gain_item(item, n, include_equip)

  end

end

 

#==============================================================================

# ** Scene_Title (Data Conversion)

#------------------------------------------------------------------------------

# This script converts RGSS data structure to RGSS2 to increase compatibility.

#==============================================================================

 

class Scene_Title < Scene_Base

  #--------------------------------------------------------------------------

  # * Alias Methods

  #--------------------------------------------------------------------------

  alias scriptastic_rand_enchant_scene_title_load_database load_database

  alias scriptastic_rand_enchant_scene_title_load_bt_database load_bt_database

  #--------------------------------------------------------------------------

  # * Load Database

  #--------------------------------------------------------------------------

  def load_database

    # Call the original method

    scriptastic_rand_enchant_scene_title_load_database

    # Load normal weapon data

    $data_weapons       = load_weapons

    # Load enchanted weapon data

    load_enchants

  end

  #--------------------------------------------------------------------------

  # * Load Battle Test Database

  #--------------------------------------------------------------------------

  def load_bt_database

    # Call the original method

    scriptastic_rand_enchant_scene_title_load_bt_database

    # Load normal weapon data

    $data_weapons       = load_weapons

    # Load enchanted weapon data

    load_enchants

  end

  #--------------------------------------------------------------------------

  # * Load and Convert Weapons

  #--------------------------------------------------------------------------

  def load_weapons

    # Load base weapon data

    if $BTEST

      default_data = load_data("Data/BT_Weapons.rvdata")

    else

      default_data = load_data("Data/Weapons.rvdata")

    end

    # Add base for new $data_weapons array

    new_data = [nil]

    # Iterate through all weapons

    for weapon in default_data

      # Skip if weapon is nil

      next if weapon == nil

      # Create new instance of RPG::Weapon, with new data included

      new_weapon = RPG::Weapon.new

      # Add data from old weapon to the new weapon

      new_weapon.id                         = weapon.id

      new_weapon.name                       = weapon.name

      new_weapon.icon_index                 = weapon.icon_index

      new_weapon.description                = weapon.description

      new_weapon.note                       = weapon.note

      new_weapon.animation_id               = weapon.animation_id

      new_weapon.price                      = weapon.price

      new_weapon.hit                        = weapon.hit

      new_weapon.atk                        = weapon.atk

      new_weapon.def                        = weapon.def

      new_weapon.spi                        = weapon.spi

      new_weapon.agi                        = weapon.agi

      new_weapon.two_handed                 = weapon.two_handed

      new_weapon.fast_attack                = weapon.fast_attack

      new_weapon.dual_attack                = weapon.dual_attack

      new_weapon.critical_bonus             = weapon.critical_bonus

      new_weapon.element_set                = weapon.element_set

      new_weapon.state_set                  = weapon.state_set

      # Push the new weapon into the new_data array

      new_data.push new_weapon

    end

    # return new_data

    return new_data

  end

  #--------------------------------------------------------------------------

  # * Load Enchanted Weapons

  #--------------------------------------------------------------------------

  def load_enchants

    # Check for enchanted weapon file

    if FileTest.exist?('Data/EnchantedWeapons.rvdata')

      # If in debug mode

      if $TEST

        # Delete and recompile enchanted weapon file

        File.delete('Data/EnchantedWeapons.rvdata')

        Enchants::create_enchants

      else

        # Open and load enchanted weapon file

        file = File.open('Data/EnchantedWeapons.rvdata', "rb")

        weapon_data = Marshal.load(file)

        class_data = Marshal.load(file)

        # Iterate through all enchanted weapons

        for weapon in weapon_data

          # Skip if the weapon is nil

          next if weapon == nil

          # Add new weapon to $data_weapons

          $data_weapons.push weapon

        end

        # Iterate through all loaded classes

        for new_class in class_data

          # Skip if class is nil

          next if new_class == nil

          # Skip if original class is nil

          next if $data_classes[new_class.id] == nil

          # Replace original weapon set with updated one

          $data_classes[new_class.id].weapon_set = new_class.weapon_set

        end

      end

    else

      # Create new enchant file

      Enchants::create_enchants

    end

  end

end
 
Scriptastic":2z7trvlt said:
All right. We're finished with your request. We actually decided to finish it exactly as you requested, including defining all the enchantments you specified. If you wish to change the enchantments, the instructions are in the header. Otherwise, this script will work exactly like you asked.

Code:
#==============================================================================

# ** Team Scriptastic's Random Enchants

#    Version 0.10

#------------------------------------------------------------------------------

#  This script will give pre-specifiec weapons a random enchant when they are

#  given to the player.

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# * Version History

#  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -

#   Version [0.10] --------------------------------------------- ([2009-07-02])

#     -Initial version

#     -Author: Glitchfinder

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# * Instructions

#  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -

#  Place this script above Main, and below the default scripts. (I realize this

#  is obvious to most, but some people don't get it.)

#

#  This script requires a slight knowledge of scripting.  To enable an item to

#  have random variants, simply add a tag to the notes box, that looks like the

#  following:

#    <random +10>

#  Where 10 is the value you want added to the stats enchanted.

#

#  Below is a list of all possible enchantments.  An enchantment looks like the

#  following hash:

#

#  @enchants['of Strength'] =      {'atk' => true, 'def' => false,

#                                  'spi' => false, 'agi' => false,

#                                  'critical' => false, 'eva' => false}

#

#  Where 'of Strength' is the string added to the name of the base item.  This

#  also serves as an identifier for this enchantment, so two enchantments with

#  the same name will result in only the latter occuring.

#

#  'atk' => false        | if this is true, it would add XX from <random +xx>

#                        | to the weapon's attack stat.

#  'def' => false        | if this is true, it would add XX from <random +xx>

#                        | to the weapon's defense stat.

#  'spi' => false        | if this is true, it would add XX from <random +xx>

#                        | to the weapon's spirit stat.

#  'agi' => false        | if this is true, it would add XX from <random +xx>

#                        | to the weapon's agility stat.

#  'eva' => false        | if this is true, it would add XX from <random +xx>

#                        | to the weapon's evasion stat.

#  'critical' => false   | if this is true, it would add XX from <random +xx>

#                        | to the weapon's critical hit ratio.

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

# * Terms of Service

#  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -

#  This script is free for use in private or commercial applications, under the

#  condition that this header remains intact and credit to Team Scriptastic and

#  all authors mentioned in the version history is mentioned in an appropriate

#  section visible to users of the product.

#==============================================================================

 

#==============================================================================

# + Enchants (Module)

#==============================================================================

module Enchants

  @enchants = {}

  @enchants.default =             {'atk' => false, 'def' => false,

                                  'spi' => false, 'agi' => false,

                                  'critical' => false, 'eva' => false}

  @enchants['of Strength'] =      {'atk' => true, 'def' => false,

                                  'spi' => false, 'agi' => false,

                                  'critical' => false, 'eva' => false}

  @enchants['of Defense'] =       {'atk' => false, 'def' => true,

                                  'spi' => false, 'agi' => false,

                                  'critical' => false, 'eva' => false}

  @enchants['of Intellect'] =     {'atk' => false, 'def' => false,

                                  'spi' => true, 'agi' => false,

                                  'critical' => false, 'eva' => false}

  @enchants['of Agility'] =       {'atk' => false, 'def' => false,

                                  'spi' => false, 'agi' => true,

                                  'critical' => false, 'eva' => false}

  @enchants['of the Warrior'] =   {'atk' => true, 'def' => true,

                                  'spi' => false, 'agi' => false,

                                  'critical' => false, 'eva' => false}

  @enchants['of the Sorcerer'] =  {'atk' => true, 'def' => false,

                                  'spi' => true, 'agi' => false,

                                  'critical' => false, 'eva' => false}

  @enchants['of the Berserker'] = {'atk' => true, 'def' => false,

                                  'spi' => false, 'agi' => true,

                                  'critical' => false, 'eva' => false}

  @enchants['of the Medic'] =     {'atk' => false, 'def' => true,

                                  'spi' => true, 'agi' => false,

                                  'critical' => false, 'eva' => false}

  @enchants['of the Monk'] =      {'atk' => false, 'def' => true,

                                  'spi' => false, 'agi' => true,

                                  'critical' => false, 'eva' => false}

  @enchants['of the Focused'] =   {'atk' => false, 'def' => false,

                                  'spi' => true, 'agi' => true,

                                  'critical' => false, 'eva' => false}

  @enchants['of the Cruel'] =     {'atk' => true, 'def' => false,

                                  'spi' => false, 'agi' => false,

                                  'critical' => true, 'eva' => false}

  @enchants['of the Cunning'] =   {'atk' => false, 'def' => false,

                                  'spi' => false, 'agi' => true,

                                  'critical' => true, 'eva' => false}

  @enchants['of the Sly'] =       {'atk' => false, 'def' => false,

                                  'spi' => false, 'agi' => true,

                                  'critical' => false, 'eva' => true}

  #--------------------------------------------------------------------------

  # * Enchant Weapon

  #     weapon : data for specific weapon

  #--------------------------------------------------------------------------

  def self.enchant(weapon)

    # Check if the random tag is in the notes box

    if weapon.note.include?('<random +')

      # If so, load the default weapon index

      weapon_data = load_data("Data/Weapons.rvdata")

      # Check which id the weapon is based on

      weapon_id = weapon.id * rand(@enchants.keys.size) + weapon_data.size

      # Return this weapon data

      weapon = $data_weapons[weapon_id]

    end

    return weapon

  end

  #--------------------------------------------------------------------------

  # * Create Enchanted Weapons

  #     weapon : data for specific weapon

  #--------------------------------------------------------------------------

  def self.create_enchants

    # Create blank weapon array

    weapons = []

    # Iterate through all weapons in $data_weapons

    for weapon in $data_weapons

      # Skip if the current weapon is nil

      next if weapon == nil

      # If the weapon includes the random tag

      if weapon.note.include?('<random +')

        # Add the enchanted weapons to the appropriate classes

        enchant_class(weapon)

        # Iterate through all enchants

        for enchant in @enchants.keys

          # Clone and enchant current weapon

          new_weapon = weapon.clone

          new_weapon = enchant_stats(new_weapon, enchant)

          # Remove the random tag from the enchanted weapon

          new_weapon.note = new_weapon.note.gsub(/<random \+([0-9]+)>/) {''}

          # Push new weapon data to $data_weapons

          $data_weapons.push new_weapon

          # Push new weapons into weapons array

          weapons.push new_weapon

        end

      end

    end

    # Save the new data using the weapons array

    save_enchant(weapons) unless weapons.empty?

  end

  #--------------------------------------------------------------------------

  # * Create Enchant

  #     weapon : data for specific weapon

  #     str    : name of specific enchant

  #--------------------------------------------------------------------------

  def self.enchant_stats(weapon, str)

    # Set new weapon id

    weapon.id = $data_weapons.size

    # Set new weapon name

    weapon.name += ' ' + str

    # Remove the tag from the weapon note

    value = weapon.note.slice(/<random \+([0-9]+)>/)

    value.gsub!(/<random \+/) {''}

    value.gsub!(/>/) {''}

    # Convert the number within the tag string to an integer

    value = value.to_i

    # Add the value to the appropriate stat

    weapon.atk += value if @enchants[str]['atk']

    weapon.def += value if @enchants[str]['def']

    weapon.spi += value if @enchants[str]['spi']

    weapon.agi += value if @enchants[str]['agi']

    weapon.critical += value if @enchants[str]['critical']

    weapon.eva += value if @enchants[str]['eva']

    # Return the weapon data

    return weapon

  end

  #--------------------------------------------------------------------------

  # * Add Enchanted Weapon to Class Lists

  #     weapon : data for specific weapon

  #--------------------------------------------------------------------------

  def self.enchant_class(weapon)

    # Create base class array

    classes = [nil]

    # Iterate through all classes

    for new_class in $data_classes

      # Skip if the current class is nil

      next if new_class == nil

      # If the class used the base weapon 

      if new_class.weapon_set.include? weapon.id

        # Iterate through all IDs for the enchanted version

        for id in [email=0...@enchants.keys.size]0...@enchants.keys.size[/email]

          # Add the new IDs to the useable weapons array

          new_class.weapon_set.push $data_weapons.size + id

        end

      end

      # Add the modified class to the classes array

      classes.push new_class

    end

    # Replace the default classes array with the modified one

    $data_classes = classes

  end      

  #--------------------------------------------------------------------------

  # * Save Enchanted Weapon Data

  #     weapons : data for specific weapon

  #--------------------------------------------------------------------------

  def self.save_enchant(weapons)

    # If the file exists

    if FileTest.exist?('Data/EnchantedWeapons.rvdata')

      # Open the file and load the data

      file = File.open('Data/EnchantedWeapons.rvdata', "rb")

      weapon_data = Marshal.load(file)

      class_data = Marshal.load(file)

    else

      # Otherwise, default to nil

      weapon_data = [nil]

      class_data = [nil]

    end

    # Iterate through all weapons in the weapons variable

    for weapon in weapons

      # Push weapons into a temporary container

      weapon_data.push weapon

    end

    # Iterate through all classes in $data_classes

    for old_class in $data_classes

      # Skip if the current class it nil

      next if old_class == nil

      # Create new instance of extended class data

      new_class = RPG::ClassExtension.new

      # Add the ID and weapon set of the current class

      new_class.id         = old_class.id

      new_class.weapon_set = old_class.weapon_set

      # Push new class datas into a temporary container

      class_data.push new_class

    end

    # Open the enchants file and dump data

    file = File.open('Data/EnchantedWeapons.rvdata', "wb")

    Marshal.dump(weapon_data, file)

    Marshal.dump(class_data, file)

  end

  # Protect private class methods

  private_class_method :enchant_stats

  private_class_method :enchant_class

  private_class_method :save_enchant

end

 

#==============================================================================

# + RPG::BaseItem (module)

#==============================================================================

module RPG

  class BaseItem

    def initialize

      @id = 0

      @name = ""

      @icon_index = 0

      @description = ""

      @note = ""

      @atk = 0

      @def = 0

      @spi = 0

      @agi = 0

      @critical = 0

      @eva = 0

    end

    attr_accessor :id

    attr_accessor :name

    attr_accessor :icon_index

    attr_accessor :description

    attr_accessor :note

    attr_accessor :atk

    attr_accessor :def

    attr_accessor :spi

    attr_accessor :agi

    attr_accessor :critical

    attr_accessor :eva

  end

  #============================================================================

  # + RPG::ClassExtension (module)

  #============================================================================

  class ClassExtension

    def initialize

      @id = 0

      @weapon_set = []

    end

    attr_accessor :id

    attr_accessor :weapon_set

  end

end

 

#==============================================================================

# ** Game_Actor

#------------------------------------------------------------------------------

#  This class handles actors. It's used within the Game_Actors class

# ($game_actors) and referenced by the Game_Party class ($game_party).

#==============================================================================

 

class Game_Actor < Game_Battler

  #--------------------------------------------------------------------------

  # * Get Evasion Rate

  #--------------------------------------------------------------------------

  def eva

    n = 5

    # Add the enchanted weapon's evasion stat

    for item in equips.compact do n += item.eva end

    return n

  end

  #--------------------------------------------------------------------------

  # * Get Critical Ratio

  #--------------------------------------------------------------------------

  def cri

    n = 4

    n += 4 if actor.critical_bonus

    for weapon in weapons.compact

      n += 4 if weapon.critical_bonus

      # Add the enchanted weapon's critical ratio

      n += weapon.critical

    end

    return n

  end

end

 

#==============================================================================

# ** Game_Party

#------------------------------------------------------------------------------

#  This class handles the party. It includes information on amount of gold 

# and items. The instance of this class is referenced by $game_party.

#==============================================================================

 

class Game_Party < Game_Unit

  #--------------------------------------------------------------------------

  # * Alias Methods

  #--------------------------------------------------------------------------

  alias scriptastic_rand_enchant_game_party_gain_item gain_item

  #--------------------------------------------------------------------------

  # * Gain Items (or lose)

  #     item          : Item

  #     n             : Number

  #     include_equip : Include equipped items

  #--------------------------------------------------------------------------

  def gain_item(item, n, include_equip = false)

    # Reaplace item data with that of an enchanted item instead, if applicable

    item = Enchants::enchant(item) if item.is_a? RPG::Weapon

    # Call the original method

    scriptastic_rand_enchant_game_party_gain_item(item, n, include_equip)

  end

end

 

#==============================================================================

# ** Scene_Title (Data Conversion)

#------------------------------------------------------------------------------

# This script converts RGSS data structure to RGSS2 to increase compatibility.

#==============================================================================

 

class Scene_Title < Scene_Base

  #--------------------------------------------------------------------------

  # * Alias Methods

  #--------------------------------------------------------------------------

  alias scriptastic_rand_enchant_scene_title_load_database load_database

  alias scriptastic_rand_enchant_scene_title_load_bt_database load_bt_database

  #--------------------------------------------------------------------------

  # * Load Database

  #--------------------------------------------------------------------------

  def load_database

    # Call the original method

    scriptastic_rand_enchant_scene_title_load_database

    # Load normal weapon data

    $data_weapons       = load_weapons

    # Load enchanted weapon data

    load_enchants

  end

  #--------------------------------------------------------------------------

  # * Load Battle Test Database

  #--------------------------------------------------------------------------

  def load_bt_database

    # Call the original method

    scriptastic_rand_enchant_scene_title_load_bt_database

    # Load normal weapon data

    $data_weapons       = load_weapons

    # Load enchanted weapon data

    load_enchants

  end

  #--------------------------------------------------------------------------

  # * Load and Convert Weapons

  #--------------------------------------------------------------------------

  def load_weapons

    # Load base weapon data

    if $BTEST

      default_data = load_data("Data/BT_Weapons.rvdata")

    else

      default_data = load_data("Data/Weapons.rvdata")

    end

    # Add base for new $data_weapons array

    new_data = [nil]

    # Iterate through all weapons

    for weapon in default_data

      # Skip if weapon is nil

      next if weapon == nil

      # Create new instance of RPG::Weapon, with new data included

      new_weapon = RPG::Weapon.new

      # Add data from old weapon to the new weapon

      new_weapon.id                         = weapon.id

      new_weapon.name                       = weapon.name

      new_weapon.icon_index                 = weapon.icon_index

      new_weapon.description                = weapon.description

      new_weapon.note                       = weapon.note

      new_weapon.animation_id               = weapon.animation_id

      new_weapon.price                      = weapon.price

      new_weapon.hit                        = weapon.hit

      new_weapon.atk                        = weapon.atk

      new_weapon.def                        = weapon.def

      new_weapon.spi                        = weapon.spi

      new_weapon.agi                        = weapon.agi

      new_weapon.two_handed                 = weapon.two_handed

      new_weapon.fast_attack                = weapon.fast_attack

      new_weapon.dual_attack                = weapon.dual_attack

      new_weapon.critical_bonus             = weapon.critical_bonus

      new_weapon.element_set                = weapon.element_set

      new_weapon.state_set                  = weapon.state_set

      # Push the new weapon into the new_data array

      new_data.push new_weapon

    end

    # return new_data

    return new_data

  end

  #--------------------------------------------------------------------------

  # * Load Enchanted Weapons

  #--------------------------------------------------------------------------

  def load_enchants

    # Check for enchanted weapon file

    if FileTest.exist?('Data/EnchantedWeapons.rvdata')

      # If in debug mode

      if $TEST

        # Delete and recompile enchanted weapon file

        File.delete('Data/EnchantedWeapons.rvdata')

        Enchants::create_enchants

      else

        # Open and load enchanted weapon file

        file = File.open('Data/EnchantedWeapons.rvdata', "rb")

        weapon_data = Marshal.load(file)

        class_data = Marshal.load(file)

        # Iterate through all enchanted weapons

        for weapon in weapon_data

          # Skip if the weapon is nil

          next if weapon == nil

          # Add new weapon to $data_weapons

          $data_weapons.push weapon

        end

        # Iterate through all loaded classes

        for new_class in class_data

          # Skip if class is nil

          next if new_class == nil

          # Skip if original class is nil

          next if $data_classes[new_class.id] == nil

          # Replace original weapon set with updated one

          $data_classes[new_class.id].weapon_set = new_class.weapon_set

        end

      end

    else

      # Create new enchant file

      Enchants::create_enchants

    end

  end

end

This script doesn't seem to be working too well. The item doesn't seem to be created into my inventory. The only enchants that seems to be working is "Of Intellect", "Of the Sly", and "Of the Monk" but I have no idea why that's happening.
 
Phoenix":1vzvsuvs said:
This script doesn't seem to be working too well. The item doesn't seem to be created into my inventory. The only enchants that seems to be working is "Of Intellect", "Of the Sly", and "Of the Monk" but I have no idea why that's happening.

Would you be able to explain a little more clearly? I just tested it with a simple give weapon event, and this is what I ended up with:

enchantscreen.png


Please note that I have 24 longswords because those were used as a dummy to make sure it didn't enchant everything.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top