The RPG Maker Resource Kit

RMRK RPG Maker Creation => VX Ace => VXA Scripts Database => Topic started by: modern algebra on December 30, 2012, 07:01:04 PM

Title: [VXA] Price Formulas
Post by: modern algebra on December 30, 2012, 07:01:04 PM
Price Formulas
Version: 1.0.1
Author: modern algebra
Date: 30 December 2012

Version History



Description


This script lets you calculate the standard price of an item by a formula. The most useful implementation for this is likely to implement a tax, where the price of the item varies depending on the value of the tax rate variable.

Naturally, this does not interact with any prices that are set directly in the Shop event itself.

Features


Instructions

Paste the script into its own slot in the Script Editor, above Main but below Materials.

For further instructions, please consult the comments in the header of the script.

Script


Code: [Select]
#==============================================================================
#    Price Formulas
#    Version: 1.0.1
#    Author: modern algebra (rmrk.net)
#    Date: 1 January 2013
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#  Description:
#   
#    This script lets you calculate the standard price of an item by a formula.
#   The most useful implementation for this is likely to implement a tax, where
#   the price of the item varies depending on the value of the tax rate
#   variable.
#
#    Naturally, this does not interact with any prices that are set directly
#   in the Shop event itself.
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#  Instructions:
#   
#    Paste this script into its own slot in the Script Editor, above Main but
#   below Materials.
#
#    To set the basic default formula that applies to all items, go to the
#   Editable Region at line xx and alter MAPF_DEFAULT_PRICE_FORMULA. You can
#   also give each item, weapon, or armor its own custom formula by using the
#   following code in a notebox:
#
#        \price_f { ... }
#
#   where you replace the ... with the custom formula. When setting it in the
#   notebox, DON'T use quotation marks.
#
#    As for the formula itself, the following codes are replaced:
#
#        \p    - This is replaced by the price value set in the database.
#        \v[n] - This is replaced with the value of the variable with ID n.
#        \s[n] - This is replaced by the value of the switch with ID n. It can
#               be used for conditionals.
#
#   Beyond those replacements, the code will run just as you input it, and as
#   such must obey ordinary syntax and it should output an integer.
#``````````````````````````````````````````````````````````````````````````````
#  Example Formulas:
#
#    (\p * (\v[4] / 100.0)).to_i
#      This formula multiplies the ordinary price of the item by the value of
#      variable 4 divided by 100. What that effectively means is that variable
#      4 is a percentage, and the price of the item will be modified by that
#      percentage. If, say the ordinary price is 120 and the value of variable
#      4 is 150, then the price of an item calculated by that formula would be
#      180 since that is 150% of 120. Similarly, if the value of variable 4 is
#      75, then the price would be 90, since that is 75% of 120. This would be
#      a good formula to use if you wanted to have a tax rate, since then all
#      you would need to do is adjust the value of variable 4 before every shop
#      and it would adjust the price of every item in the shop.
#
#    \p / (\s[24] ? 2 : 1)
#      This formula depends on the value of switch 24. If switch 24 is ON, then
#      the item is divided by 2 (half price). If it is OFF, then it is divided
#      by 1 (full price).
#==============================================================================

$imported = {} unless $imported
$imported[:MA_PriceFormulas] = true

#==============================================================================
# *** MA_PriceFormulas
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#  This module mixes in with RPG::Item and RPG::EquipItem to modify the price
# method.
#==============================================================================

module MA_PriceFormulas
  #\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
  #    BEGIN Editable Region
  #||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  #  MAPF_DEFAULT_PRICE_FORMULA - This is the default formula that is applied
  # to all items that do not use a custom formula in their noteboxes. '\p'
  # means that it will just be the ordinary price. However, this is a good
  # place to modify the formula for something that should apply to all items,
  # like a tax rate. It is necessary here to use quotation marks, though the
  # same is not true when you are setting it in a notebox.
  MAPF_DEFAULT_PRICE_FORMULA = '\p'
  #||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
  #    END Editable Region
  #//////////////////////////////////////////////////////////////////////////
  # Replace variable and switch codes
  MAPF_DEFAULT_PRICE_FORMULA.gsub!(/\\[Vv]\[\s*(\d+)\s*\]/) { "$game_variables[#{$1.to_i}]" }
  MAPF_DEFAULT_PRICE_FORMULA.gsub!(/\\[Ss]\[\s*(\d+)\s*\]/) { "$game_switches[#{$1.to_i}]" }
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  # * Price Formula
  #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  def mapf_price_formula
    if !@mapf_price_formula
      if note[/\\PRICE_F\s*\{(.+?)\}/i]
        @mapf_price_formula = $1.strip
        # Delete any quotation marks if user mistakenly inserts them
        @mapf_price_formula = $1 if @mapf_price_formula[/\A['"](.+?)['"]\z/]
        # Replace variable and switch codes
        @mapf_price_formula.gsub!(/\\[Vv]\[\s*(\d+)\s*\]/) { "$game_variables[#{$1.to_i}]" }
        @mapf_price_formula.gsub!(/\\[Ss]\[\s*(\d+)\s*\]/) { "$game_switches[#{$1.to_i}]" }
      else
        @mapf_price_formula = MAPF_DEFAULT_PRICE_FORMULA
      end
    end
    @mapf_price_formula
  end
end

#==============================================================================
# *** RPG
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#  Mix MA_PriceFormulas into RPG::Item and RPG::EquipItem
#==============================================================================

module RPG
  class EquipItem
    include MA_PriceFormulas # add mapf_price_formula method
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    # * Price
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    alias mapf_pric_3gh6 price
    def price(*args)
      standard_p = mapf_pric_3gh6(*args) # Run Original Method
      eval(mapf_price_formula.gsub(/\\[Pp]/, standard_p.to_s)).to_i # Calculate
    end
  end
  class Item
    include MA_PriceFormulas
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    # * Price
    #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    alias mapf_price_7bw2 price # add mapf_price_formula method
    def price(*args)
      standard_p = mapf_price_7bw2(*args) # Run Original Method
      eval(mapf_price_formula.gsub(/\\[Pp]/, standard_p.to_s)).to_i # Calculate
    end
  end
end

Credit



Support


Please post in this topic at RMRK.net if you have any questions, suggestions, error reports, or comments about this script. It is perfectly fine to post here even if this topic has not been posted in for a very long time.

Please do not message me privately, as any concerns you have are likely shared by others and they will benefit from our correspondence being public. Additionally, I am occasionally absent, and posting publically will allow other members to assist you when I am unable to do so quickly.

Known Compatibility Issues

I am currently unaware of any compatibility issues.

Demo


I do not believe a demo is necessary. However, if you are having trouble with the script, please post in the topic and I will try to help as best I can.

Terms of Use


I adopt RMRK's default Terms of Use (http://rmrk.net/index.php/topic,45481.0.html).
Title: Re: [VXA] Price Formulas
Post by: The Frontera on January 01, 2013, 01:26:07 PM
AWESOME, Man! Epic script, as always!
This is just what I needed for my project!
Title: Re: [VXA] Price Formulas
Post by: The Frontera on January 01, 2013, 01:40:20 PM
I've just tried out the script, and I have a question...
is there a way to set up and item that when in inventory changes the tax rate of all items in shops?
For example, you find a "Discount Card" and when you have it you get 20% discount on everything.
Title: Re: [VXA] Price Formulas
Post by: modern algebra on January 02, 2013, 02:20:20 AM
Yes, there is. All you'd need to do is set up a parallel process common event that is always on. Then you can just use a conditional branch that checks if the item is in the inventory, and if it is, turns on a switch. If it isn't, it turns that switch off. To avoid lag, you should probably put a 10 frame wait at the end of the common event.

Let's say you choose switch 6, then all you would need is your formula to read something like this:

Code: [Select]
a = %d; \s[6] ? a*0.8 : a

Of course, that whole common event is only necessary if the item can be gained randomly at any time and discarded once it is received. If the card is received directly through an event and cannot be discarded, then you would only need to turn the switch on there.

Anyway, that actually made me think that using %d is a bad idea.

EDIT::

I have changed the script to use version 1.0.1 - Now, %d should not be used, and the price will be substituted by the code: \p

So, if you update to 1.0.1 that above formula should actually be:

Code: [Select]
\s[6] ? \p*0.8 : \p
Title: Re: [VXA] Price Formulas
Post by: The Frontera on January 02, 2013, 01:22:23 PM
Thank you very much!
You are the best, modern algebra!