/*:
* @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 );
};