PT_ShopPercentages

ptietz

Warper
Member
Joined
Jul 11, 2018
Messages
2
Reaction score
6
First Language
German
Primarily Uses
RMMV
I've made another plugin I find very useful.
So I thought, I'd like to share it with you guys.
Hope it helps :)

What does it do?
This plugin changes the way, user-specified prices in the shop windows work.
They will now be treated as percentage values rather than absolute prices.

Features
  • decide whether to use your input as total percentage or discount values.
  • you can also use variables for your special prices
  • also, special prices for ALL items can be set.
How to use
Copy the source code (from below) and save it as
"YOUR_PROJECT_PATH/js/plugins/PT_ShopPercentages.js".

The plugin is plug & play and will work just fine with default values.
However, you can manipulate its behavior using the plugin parameters.
See the documentation contained in the plugin itself for details.

If you already work with special prices a lot already in your project,
it is not recommended to install this plugin or you might have a lot of work before you.
If you're starting a new project, however, this plugin will help make thinks
a lot easier in terms of in-game financial balancing.

Terms of use
You may freely use, copy and modify this script
in any commercial or non-commercial game.

Re-distribution of the source code as is, is prohibited.
You may link to the source, you got it from, though.
The distribution of your modified code is allowed,
as long as the original author is kept visible.

Crediting the author in the game, however,
is not required, although much appreciated.

Screenshots packed with this plugin are for
demonstrational purpose only.

Credits for the original software
"RPG Maker MV" go to
Copyright (c) 2015 KADOKAWA CORPORATION / YOJI OJIMA

The plugin
Code:
/*:
 * @plugindesc v1.1 Use percentages for special shop prices
 * @author Patrick Tietz <ptietz.official[at]gmail.com>
 *
 * @param rounding method
 * @desc how to round results
 * (round, floor, ceil)
 * @type select
 * @option round
 * @option floor
 * @option ceil
 * @default round
 *
 * @param treat as discount
 * @desc treat input percentages as discount
 * @type boolean
 * @default false
 *
 * @param use variables
 * @desc use price input as variable index
 * @type boolean
 * @default false
 *
 * @param overall variable
 * @desc apply to all prices
 * @type variable
 * @default 0
 *
 *
 * @help
 *
 * ######################################
 * # Welcome                            #
 * ######################################
 *
 * Thank you for using ShopPercentages.
 *
 * This Plugin treats special prices in the shop processing
 * as percentages rather than absolute prices.
 *
 * This way, you won't have to go over all the NPCs
 * that sell a particular item, whenever you change
 * and balance its price.
 *
 * Have fun making games! :)
 *
 *
 * ######################################
 * # General Instructions               #
 * ######################################
 *
 * This plugin is plug & play and will perfectly work
 * from the moment you install and enable it
 * from within the plugin manager.
 *
 * Please, keep in mind that this plugin
 * overrides the defaul shop behavior for special prices.
 * Installing this plugin midway through your project
 * will probably have a significant impact on
 * already set special prices in your game.
 * This might ruin your financial balance
 * and you will have to edit every shop again.
 *
 * The plugin does not add any plugin commands.
 *
 *
 * ######################################
 * # Parameters                         #
 * ######################################
 *
 * rounding method
 * ===============
 * You can define how you want float number results
 * to be rounded. Methods are "round", "ceil" and "floor".
 *
 *   Example:
 *   Let's assume we have an item for sale that costs 210 by default.
 *   We want to have it sold by 90% of its original value.
 *   That would give us a total of 189.81 which is not
 *   possible in RPG Maker MV. Therefore we must round.
 *
 *
 * treat as discount
 * =================
 * If enabled, you special price inputs will be treated
 * as discount rather than total percentage values.
 * This affects variables as well.
 *
 * This parameter's behavior adjusts to your
 * "treat as discount" setting.
 *
 *   Example:
 *   Usually, when you have an item of 200 value
 *   sold for 180, you would have to enter "90"
 *   as 180 is 90% of 200.
 *   With this parameter enabled, you would now
 *   have to enter "10" as 180 is a 10% discount
 *   of the original 200.
 *
 *
 * use variables
 * =============
 * If you enable "use variables", your special prices will no longer
 * be treated as direct percentage inputs but as variable indizes.
 *
 *   Example:
 *   Maybe, you want a 10% discount on all swords.
 *   To get there, you would have to set a value
 *   for a variable, depending on your "treat as discount" setting.
 *   Then you would write the index of that variable
 *   as a special price on each shop's swords.
 *   Of course, there are many other use cases thinkable.
 *
 *
 * overall variable
 * ================
 * Here, you can specify a variable whose value is applied to
 * all shop prices (even default ones).
 *
 * Be aware that this variable is applied AFTER
 * everything else. This means, that even an item
 * that already has a special price, will still
 * be affected by this setting.
 *
 * This parameter's behavior adjusts to your
 * "treat as discount" setting.
 *
 *   Example:
 *   Let's say your player has some sort of bargaining skill
 *   which affects all shop prices.
 *   This can be achieved by writing the current discount,
 *   calculated by the particular skill, into a variable
 *   and then specifying that variable in this plugin.
 *
 *
 * ######################################
 * # Terms of use                       #
 * ######################################
 *
 * You may freely use, copy and modify this script
 * in any commercial or non-commercial game.
 *
 * Re-distribution of the source code as is, is prohibited.
 * You may link to the source, you got it from, though.
 * The distribution of your modified code is allowed,
 * as long as the original author is kept visible.
 *
 * Crediting the author in the game, however,
 * is not required, although much appreciated.
 *
 * Screenshots packed with this plugin are for
 * demonstrational purpose only.
 *
 * Credits for the original software
 * "RPG Maker MV" go to
 * Copyright (c) 2015 KADOKAWA CORPORATION / YOJI OJIMA
 */

// get parameters
PT_ShopPercentages_ParamsRaw = {
    roundingMethod: String( PluginManager.parameters( 'PT_ShopPercentages' )[ 'rounding method' ] ),
    treatAsDiscount: String( PluginManager.parameters( 'PT_ShopPercentages' )[ 'treat as discount' ] ),
    useVars: String( PluginManager.parameters( 'PT_ShopPercentages' )[ 'use variables' ] ),
    overallVariable: parseInt( PluginManager.parameters( 'PT_ShopPercentages' )[ 'overall variable' ] ),
};

// validate parameters
PT_ShopPercentages_Params = {
    roundingMethod      : PT_ShopPercentages_ParamsRaw.roundingMethod === 'ceil'
                            || PT_ShopPercentages_ParamsRaw.roundingMethod === 'floor'
                            ? PT_ShopPercentages_ParamsRaw.roundingMethod
                            : 'round',
    treatAsDiscount     : PT_ShopPercentages_ParamsRaw.treatAsDiscount === 'true'
                            || PT_ShopPercentages_ParamsRaw.treatAsDiscount === '1'
                            || PT_ShopPercentages_ParamsRaw.treatAsDiscount === 'yes'
                            ? true
                            : false,
    useVars: PT_ShopPercentages_ParamsRaw.useVars === 'true'
                            || PT_ShopPercentages_ParamsRaw.useVars === '1'
                            || PT_ShopPercentages_ParamsRaw.useVars === 'yes'
                            ? true
                            : false,
    overallVariable: PT_ShopPercentages_ParamsRaw.overallVariable
};


/**
 * Calculate item price
 *
 * @param {number} origItemPrice original item price (from database)
 * @param {number} rawPriceInput raw price input from shop window, optional
 * @param {boolean} isOverall for internal use only
 * @returns {number}
 */
Window_ShopBuy.prototype.calcItemPrice = function( origItemPrice, rawPriceInput, isOverall = false )
{
    var result = origItemPrice;
   
    if ( arguments.length > 1 && typeof rawPriceInput !== 'undefined' )
    {
        // get price modifier depending "use variables" setting
        var input = !isOverall && PT_ShopPercentages_Params.useVars
            ? parseInt( $gameVariables.value( rawPriceInput ) )
            : rawPriceInput;
           
        // calculate price depending on "treatAsDiscount" setting
        var percentage = PT_ShopPercentages_Params.treatAsDiscount
            ? origItemPrice - origItemPrice * input / 100
            : origItemPrice * ( input / 100 );
           
        // round price depending on "rounding message" setting
        result = parseInt( Math[ PT_ShopPercentages_Params.roundingMethod ]( percentage ) );
    }
   
    // if "overall variable" is set
    if ( !isOverall && PT_ShopPercentages_Params.overallVariable > 0 )
    {
        // redo the whole thing
        result = this.calcItemPrice(
            result,
            $gameVariables.value( PT_ShopPercentages_Params.overallVariable ),
            true
        );
    }
   
    // return result
    return result;
};


/**
 * Make item list for buying window
 *
 * @override
 * @returns {undefined}
 */
Window_ShopBuy.prototype.makeItemList = function()
{
    this._data = [];
    this._price = [];
   
    this._shopGoods.forEach( function( goods )
    {
        var item = null;
       
        switch ( goods[0] )
        {
            case 0:
                item = $dataItems[ goods[1] ];
                break;
            case 1:
                item = $dataWeapons[ goods[1] ];
                break;
            case 2:
                item = $dataArmors[ goods[1] ];
                break;
        }
       
        if ( item )
        {
            this._data.push( item );
            this._price.push(
                goods[2] === 0
                ? this.calcItemPrice( item.price )
                : this.calcItemPrice( item.price, goods[3] )
            );
        }
       
    }, this );
   
};

Happy game making! :)
 
Last edited:

Creative Ed

Veteran
Veteran
Joined
Sep 16, 2013
Messages
304
Reaction score
321
First Language
Portuguese
Primarily Uses
RMMV
Thank you for sharing! :)
 

Espilonarge

Veteran
Veteran
Joined
Apr 24, 2016
Messages
128
Reaction score
60
First Language
English
Primarily Uses
I grabbed your plugin but the console keeps reporting an error on Line 195.

SyntaxError: Unexpected token =
Line 195 is as follows.

Window_ShopBuy.prototype.calcItemPrice = function( origItemPrice, rawPriceInput, isOverall = false )
Just to be sure, I've tested it in a new project to make sure it wasn't due to a compatibility issue.

EDIT

Removing "= false" after "isOverall" seems to fix the issue, however, after doing more testing, if you're using Hime's Shop Manager plugin, they will be incompatible (regardless if PT_ShopPercentages is above or below it).
 
Last edited:

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Stream will be live shortly! Going to do another art stream tonight so that I can finish my Fauna Focus piece~ Feel free to drop by!
Humans are fallible creatures, we all know that. It's when we're forced to look at ourselves that we start to disagree. :p
Expectation: Working on boss battle.
Reality: "Look at how awful are these code and database organization. Let me clean this mess", *yet, casually adding more modules that I have no idea if I would use it later*
Trying to learn to read Japanese so I read my manga. I like to buy them without all the edits made during translation.
The only thing getting me through today so far is coffee. And lots of it.

Forum statistics

Threads
97,852
Messages
947,384
Members
129,079
Latest member
DerLaPux
Top