Variable Element Resistance and Damage

Discussion in 'RGSS3 Script Requests' started by Vis_Mage, Nov 19, 2019.

  1. Vis_Mage

    Vis_Mage Novice Magician Veteran

    Messages:
    403
    Likes Received:
    142
    Location:
    Ridge Island
    First Language:
    English
    Primarily Uses:
    RMVXA
    Hello! :kaohi:

    Would anyone be able to help me with a script that would allow element rates given by a state be tied to a variable?

    For example, perhaps there'd be the notetag <ElementRateVariable Fire 20>, that would make the afflicted actor's Element Rate for the Fire element equal to [Variable 20]%.

    I hope this actually makes sense. :kaoswt2: Let me know if there's anything you'd like me to try and explain better.

    Thank you!

    Edit: After a bit more thought, I realized I was over-complicating this, and cleaned up the request.
     
    Last edited: Nov 19, 2019
    #1
  2. A-Moonless-Night

    A-Moonless-Night WINTER IS COMING Veteran

    Messages:
    524
    Likes Received:
    372
    Location:
    New Zealand
    First Language:
    English
    Primarily Uses:
    RMVXA
    This should be easy enough to do. Does that variable's element rate overwrite the base element rate, or do they multiply together? E.g. if I had an item that changed Fire to 20% and a state with a variable that said it was 50%, the total element rate would be 10%.
     
    #2
  3. Vis_Mage

    Vis_Mage Novice Magician Veteran

    Messages:
    403
    Likes Received:
    142
    Location:
    Ridge Island
    First Language:
    English
    Primarily Uses:
    RMVXA
    Assuming I'm understanding how elemental rates work right, having it multiply instead of overwrite would probably allow it to play nice alongside other element rates.
     
    #3
  4. Rinobi

    Rinobi Veteran Veteran

    Messages:
    570
    Likes Received:
    213
    First Language:
    English
    Primarily Uses:
    RMVXA
    Having an element rate tied to a global variable seems like a mess to me.
    Just so that we understand, could you give an example of how you intend to use this script?
    I may be able to whip something up after work, if someone else hasn't already by then.
     
    #4
  5. A-Moonless-Night

    A-Moonless-Night WINTER IS COMING Veteran

    Messages:
    524
    Likes Received:
    372
    Location:
    New Zealand
    First Language:
    English
    Primarily Uses:
    RMVXA
    You can try this; I haven't tested it yet:
    Code:
    =begin
    #==============================================================================#
    #   AMN Element Variables
    #   Version   1.01
    #   Author:   AMoonlessNight
    #   Date:     19 Nov 2019
    #   Latest:   19 Nov 2019
    #==============================================================================#
    #   UPDATE LOG
    #------------------------------------------------------------------------------#
    # 19 Nov 2019 - created the script
    #==============================================================================#
    #   TERMS OF USE
    #------------------------------------------------------------------------------#
    # - Please credit AMoonlessNight or A-Moonless-Night
    # - Free for non-commercial use; contact for commercial use
    # - If you would like to edit/adjust this script, please make edits as a new
    #   script (do not edit this script directly), and make sure you provide proper
    #   credit
    # - I'd love to see your game if you end up using one of my scripts
    #==============================================================================#
    
    This script makes it so that element rates can be tied to a game variable set
    via notetags in the item's note field.
    
    You can use notetags in the following note fields:
      Actor
      Class
      Weapon
      Armour
      State
      Enemy
    
    <ElementRateVariable x y>
      where x is the ID of the element and y is the ID of the variable
        OR
      where x is the name of the element and y is the ID of the variable
     
    <ElementRateVariable 3 20>
      would set element 3 to be the value of game variable 20
     
    <ElementRateVariable Ice 3>
      would set the element called Ice in the database to be the value of
      game variable 3
    
     
    Variables should be set to an integer, e.g. 20 for 20%, 100 for 100%, 5 for 5%
    
    NOTE: You will need to set your variables to the correct amounts at the start,
    or else you may have issues with elements not working or returning zero values,
    (because the automatic value for variables is 0).
    
    =end
    
    module AMN_ElementVars
      VARS_REGEX = /<ElementRateVariable\s+(\d+|.+)\s+(\d+)>/i
    end
    
    class RPG::BaseItem
      attr_accessor :element_rate_vars
    
      def load_element_vars_notetags
        @element_rate_vars = {}
        self.note.split(/[\r\n]+/).each { |line|
        case line
        when AMN_ElementVars::VARS_REGEX
          ele, var = $1, $2
          is_integer = ele.split("").all?{|i| i =~ /\d/}
          ele = is_integer ? ele.to_i : $data_system.elements.index(ele)
          @element_rate_vars[ele] = var.to_i unless ele.nil?
        end
        }
      end
     
    end
    
    module DataManager
      class <<self; alias :amn_elevars_datamgr_loaddbase :load_database; end
     
      def self.load_database
        amn_elevars_datamgr_loaddbase
        load_element_vars_notetags
      end
    
      def self.load_element_vars_notetags
        groups = [$data_actors, $data_classes, $data_weapons, $data_armors,
          $data_enemies, $data_states]
        for group in groups
          for obj in group
            next if obj.nil?
            obj.load_element_vars_notetags
          end
        end
      end
     
    end
    
    class Game_BattlerBase
    
      def objects_element_rate_vars(element)
        feature_objects.inject(1.0) { |r, obj|
        r *= ($game_variables[obj.element_rate_vars[element]].to_f / 100.0) }
      end
     
    end
    
    class Game_Battler < Game_BattlerBase
     
      alias amn_elevars_gamebattler_itemelerate item_element_rate
      def item_element_rate(user, item)
        rate = amn_elevars_gamebattler_itemelerate(user, item)
        if item.damage.element_id < 0
          result = user.atk_elements.empty? ? 1.0 : objects_element_rate_vars(user.atk_elements)
        else
          result = objects_element_rate_vars(item.damage.element_id)
        end
        return rate * result
      end
     
    end
     
    #5
  6. Vis_Mage

    Vis_Mage Novice Magician Veteran

    Messages:
    403
    Likes Received:
    142
    Location:
    Ridge Island
    First Language:
    English
    Primarily Uses:
    RMVXA
    Thank you for taking on my request :)

    The script doesn't crash on startup, however upon attacking, I get the following error:

    Game_Variables:19:in `[]': no implicit conversion from nil to integer, TypeError

    In my test, I had a state given to an enemy, with the tag <ElementRateVariable Ice 30>, with variable 30 being 200 for double damage.

    Also Rinobi, I'll be sure to edit a more in-depth explanation of what exactly I'm trying to use it for later. Afraid I only had about 10 minutes to test it out before having to go.
     
    #6
  7. A-Moonless-Night

    A-Moonless-Night WINTER IS COMING Veteran

    Messages:
    524
    Likes Received:
    372
    Location:
    New Zealand
    First Language:
    English
    Primarily Uses:
    RMVXA
    Code:
    =begin
    #==============================================================================#
    #   AMN Element Variables
    #   Version   1.02
    #   Author:   AMoonlessNight
    #   Date:     19 Nov 2019
    #   Latest:   20 Nov 2019
    #==============================================================================#
    #   UPDATE LOG
    #------------------------------------------------------------------------------#
    # 19 Nov 2019 - created the script
    # 20 Nov 2019 - fixed issue with element damage calculation
    #==============================================================================#
    #   TERMS OF USE
    #------------------------------------------------------------------------------#
    # - Please credit AMoonlessNight or A-Moonless-Night
    # - Free for non-commercial use; contact for commercial use
    # - If you would like to edit/adjust this script, please make edits as a new
    #   script (do not edit this script directly), and make sure you provide proper
    #   credit
    # - I'd love to see your game if you end up using one of my scripts
    #==============================================================================#
    
    This script makes it so that element rates can be tied to a game variable set
    via notetags in the item's note field.
    
    You can use notetags in the following note fields:
      Actor
      Class
      Weapon
      Armour
      State
      Enemy
    
    <ElementRateVariable x y>
      where x is the ID of the element and y is the ID of the variable
        OR
      where x is the name of the element and y is the ID of the variable
     
    <ElementRateVariable 3 20>
      would set element 3 to be the value of game variable 20
     
    <ElementRateVariable Ice 3>
      would set the element called Ice in the database to be the value of
      game variable 3
    
     
    Variables should be set to an integer, e.g. 20 for 20%, 100 for 100%, 5 for 5%
    
    NOTE: You will need to set your variables to the correct amounts at the start,
    or else you may have issues with elements not working or returning zero values,
    (because the automatic value for variables is 0).
    
    =end
    
    module AMN_ElementVars
      VARS_REGEX = /<ElementRateVariable\s+(\d+|.+)\s+(\d+)>/i
    end
    
    class RPG::BaseItem
      attr_accessor :element_rate_vars
    
      def load_element_vars_notetags
        @element_rate_vars = {}
        self.note.split(/[\r\n]+/).each { |line|
        case line
        when AMN_ElementVars::VARS_REGEX
          ele, var = $1, $2
          is_integer = ele.split("").all?{|i| i =~ /\d/}
          ele = is_integer ? ele.to_i : $data_system.elements.index(ele)
          @element_rate_vars[ele] = var.to_i unless ele.nil?
          puts "Variable #{var} is tied to element #{ele}"
        end
        }
      end
     
    end
    
    module DataManager
      class <<self; alias :amn_elevars_datamgr_loaddbase :load_database; end
     
      def self.load_database
        amn_elevars_datamgr_loaddbase
        load_element_vars_notetags
      end
    
      def self.load_element_vars_notetags
        groups = [$data_actors, $data_classes, $data_weapons, $data_armors,
          $data_enemies, $data_states]
        for group in groups
          for obj in group
            next if obj.nil?
            obj.load_element_vars_notetags
          end
        end
      end
     
    end
    
    class Game_BattlerBase
    
      def objects_with_element_rate_vars(element)
        feature_objects.select { |obj| !obj.element_rate_vars.empty? && obj.element_rate_vars.key?(element) }
      end
     
      def objects_element_rate_vars(element)
        objects_with_element_rate_vars(element).inject(1.0) { |r, obj|
        r *= ($game_variables[obj.element_rate_vars[element]].to_f / 100.0) }
      end
     
    end
    
    class Game_Battler < Game_BattlerBase
     
      alias amn_elevars_gamebattler_itemelerate item_element_rate
      def item_element_rate(user, item)
        rate = amn_elevars_gamebattler_itemelerate(user, item)
        puts "Rate prior to variable check: #{rate}"
        if item.damage.element_id < 0
          result = user.atk_elements.empty? ? 1.0 : objects_element_rate_vars(user.atk_elements)
        else
          result = objects_element_rate_vars(item.damage.element_id)
        end
        puts "Rate post variable check: #{result}"
        puts "Final rate: #{rate * result}"
        return rate * result
      end
     
    end
    Should work this time.
     
    #7
    Vis_Mage likes this.
  8. Vis_Mage

    Vis_Mage Novice Magician Veteran

    Messages:
    403
    Likes Received:
    142
    Location:
    Ridge Island
    First Language:
    English
    Primarily Uses:
    RMVXA
    Everything seems to be working great now! Thank you!

    Seriously though, you've really helped me out with a lot of different features now. Thank you so much! :kaothx:
     
    #8
    A-Moonless-Night likes this.

Share This Page