Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //=============================================================================
- // SilvItemLog.js
- // Version: 1.06a
- // License: https://creativecommons.org/licenses/by/4.0/
- //=============================================================================
- /*:
- * @plugindesc v1.06a Item Log. Displays the last looted items.
- <SilverItemLog>
- * @author Silver
- *
- * @param Auto Gain Loot
- * @desc Automatically add the loot to the party's inventory? true/false
- * @default true
- *
- * @param Auto Log Items
- * @desc Automatically add items to the log window when added through the GainItem command? true/false
- * @default true
- *
- * @param Item SFX
- * @desc Automatically play this sound when picking up items, armors or weapons. Leave blank to disable.
- * @default Item1
- *
- * @param Gold SFX
- * @desc Automatically play this sound when picking up gold (currency). Leave blank to disable.
- * @default Coin
- *
- * @param Window X
- * @desc x-location of itemlog window. If window-alignment is set to Right, this will act as an offset value instead
- * @default -2
- *
- * @param Window Y
- * @desc y-location of itemlog window. If window-alignment is set to Top, this will act as an offset value instead
- * @default 2
- *
- * @param Window Horizontal Alignment
- * @desc Left/Right
- * @default Right
- *
- * @param Window Vertical Alignment
- * @desc Top/Bottom
- * @default Top
- *
- * @param Window Width
- * @desc width of the itemlog window
- * @default 400
- *
- * @param Window Height
- * @desc height of the itemlog window
- * @default 160
- *
- * @param Font Size
- * @desc Size of the font
- * @default 24
- *
- * @param Text Offset X
- * @desc Text offset X for the log entries
- * @default 10
- *
- * @param Text Offset Y
- * @desc Text offset Y for the log entries
- * @default 6
- *
- * @param Standard Padding
- * @desc Leave at default (it's basically an X and Y offset)
- * @default 0
- *
- * @param Positive Prefix Text
- * @desc Prefix text
- * @default Received
- *
- * @param Negative Prefix Text
- * @desc Prefix text
- * @default Lost
- *
- * @param Icon Y-offset
- * @desc Extra Y-offset for the drawing of the icons to better align them with the sentences
- * @default 4
- *
- * @param Fadeout Delay
- * @desc How long before the window starts fading out (in frames)
- * @default 240
- *
- * @param Fadeout Speed
- * @desc How fast the window fades out
- * @default 2
- *
- * @param Text Shading
- * @desc Displays previously looted items in darker text. Set to 0 to disable.
- * @default -0.15
- *
- * @param Gold IconIndex
- * @desc iconindex for the gold/currency icon
- * @default 163
- *
- * @param Window Skin
- * @desc Name of the window skin to use for this window
- * @default Window_ItemLog
- *
- * @help
- * Plugin Command:
- * ItemLog <loot type> <loot database-index> <amount> <optional: skip (override: does not actually add the item to the inventory)>
- * example to loot 3 potions: ItemLog item 1 3
- * example to loot 5 swords: ItemLog weapon 1 5
- * example to loot 6 axes: ItemLog weapon 2 6
- * example to log (but does not add to inventory regardless of the "Auto Gain Loot" parameter) 6 axes: ItemLog weapon 2 6 skip
- *
- * The exception is gold, examples:
- * ItemLog gold 123
- * ItemLog gold 999
- *
- * Use the plugin command "ItemLog show" (w/o quotes) to show the window
- * again without adding anything.
- *
- * Note that all commands & parameters are NOT case sensitive.
- *
- *
- *--------------------------------------
- * Version History:
- *--------------------------------------
- * v1.06a (13 December 2015) [parameters changed]
- * - Now supports losing items/gold.
- * - Fixed a minor alignment issue when gaining < 10 items, armors or weapons.
- * - Minor refactoring of the 1.06 code.
- *
- * v1.05 (12 December 2015)
- * - Added a version history.
- * - Fixed a bug that used the wrong amount of gold/item/etc. when using a game-variable instead of a fixed amount.
- * - Added this plugin to Silv.Plugins.
- * - Changed license to https://creativecommons.org/licenses/by/4.0/
- *
- * v1.00 - v1.04 (November 2015)
- */
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // Utilities
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- String.prototype.contains = function(it) { return this.indexOf(it) != -1; };
- // Stack with limited size
- // Automatically removes the first element(s) if it exceeds it's maxsize
- function Queue_LSize(maxSize)
- {
- this._maxSize = maxSize;
- this.length = 0;
- this._storage = [];
- }
- Queue_LSize.prototype.itemByIdx = function(index)
- {
- return this._storage[index];
- };
- Queue_LSize.prototype.queue = function(data)
- {
- this._storage[this.length] = data;
- this.length++;
- while (this.length > this._maxSize) { this.onAutoRemoval(this.dequeue()); }
- };
- // For aliasing
- Queue_LSize.prototype.onAutoRemoval = function(removed_item) {};
- // Remove and return first item in array
- Queue_LSize.prototype.dequeue = function()
- {
- if (this.length)
- {
- var deletedData = this._storage.shift();
- this.length--;
- return deletedData;
- }
- };
- // Remove and return last item in array
- Queue_LSize.prototype.pop = function()
- {
- if (this.length)
- {
- var deletedData = this._storage.pop();
- this.length--;
- return deletedData;
- }
- };
- function lpad(word, padStr, length)
- {
- word = String(word);
- while (word.length < length) word = padStr + word;
- return word;
- }
- function rpad(word, padStr, length)
- {
- word = String(word);
- while (word.length < length) word += padStr;
- return word;
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // Variables
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- var Silv = Silv || {};
- Silv.Plugins = Silv.Plugins || {};
- Silv.Plugins.ItemLog = 1.06;
- Silv.Parameters = $plugins.filter(function(p) { return p.description.contains('<SilverItemLog>'); })[0].parameters;
- Silv.ItemLog = Silv.ItemLog || {};
- Silv.ItemLog.Window = null;
- Silv.ItemLog.AlreadyPlayedSFX = false;
- Silv.ItemLog.AutoLootGain = Silv.Parameters['Auto Gain Loot'].toLowerCase() == 'true';
- Silv.ItemLog.AutoLogItems = Silv.Parameters['Auto Log Items'].toLowerCase() == 'true';
- Silv.ItemLog.PickupSFXItem = Silv.Parameters['Item SFX'];
- Silv.ItemLog.PickupSFXGold = Silv.Parameters['Gold SFX'];
- Silv.ItemLog.Window_X = parseInt(Silv.Parameters['Window X']);
- Silv.ItemLog.Window_Y = parseInt(Silv.Parameters['Window Y']);
- Silv.ItemLog.WindowWidth = parseInt(Silv.Parameters['Window Width']);
- Silv.ItemLog.WindowHeight = parseInt(Silv.Parameters['Window Height']);
- Silv.ItemLog.WindowHorizontalAlignment = Silv.Parameters['Window Horizontal Alignment'].toLowerCase();
- Silv.ItemLog.WindowVerticalAlignment = Silv.Parameters['Window Vertical Alignment'].toLowerCase();
- Silv.ItemLog.FontSize = parseInt(Silv.Parameters['Font Size']);
- Silv.ItemLog.EntryTextOffsetX = parseInt(Silv.Parameters['Text Offset X']);
- Silv.ItemLog.EntryTextOffsetY = parseInt(Silv.Parameters['Text Offset Y']);
- Silv.ItemLog.StandardPadding = parseInt(Silv.Parameters['Standard Padding']);
- Silv.ItemLog.TextLines = Silv.ItemLog.TextLines || 'Error: Not yet set';
- Silv.ItemLog.PositivePrefixText = Silv.Parameters['Positive Prefix Text'];
- if (Silv.ItemLog.PositivePrefixText.slice(-1) != ' ') { Silv.ItemLog.PositivePrefixText += ' '; }
- Silv.ItemLog.NegativePrefixText = Silv.Parameters['Negative Prefix Text'];
- if (Silv.ItemLog.NegativePrefixText.slice(-1) != ' ') { Silv.ItemLog.NegativePrefixText += ' '; }
- Silv.ItemLog.IconOffsetY = parseInt(Silv.Parameters['Icon Y-offset']);
- Silv.ItemLog.FadeoutDelay = parseInt(Silv.Parameters['Fadeout Delay']);
- Silv.ItemLog.FadeoutSpeed = parseInt(Silv.Parameters['Fadeout Speed']);
- Silv.ItemLog.TextShadingValue = parseFloat(Silv.Parameters['Text Shading']);
- Silv.ItemLog.GoldIconIndex = parseInt(Silv.Parameters['Gold IconIndex']);
- Silv.ItemLog.WindowSkin = Silv.Parameters['Window Skin'];
- // Non Parameters
- Silv.ItemLog.PrefixLength = Math.max(Silv.ItemLog.PositivePrefixText.length, Silv.ItemLog.NegativePrefixText.length);
- (function()
- {
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // Window
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- function Window_ItemLog() { this.initialize.apply(this, arguments); }
- // Inherit from base window
- Window_ItemLog.prototype = Object.create(Window_Base.prototype);
- // Set Constructor
- Window_ItemLog.prototype.constructor = Window_ItemLog;
- Window_ItemLog.prototype.loadWindowskin = function() { this.windowskin = ImageManager.loadSystem(Silv.ItemLog.WindowSkin); };
- Window_ItemLog.prototype.standardPadding = function() { return Silv.ItemLog.StandardPadding; };
- Window_ItemLog.prototype.standardFontSize = function() { return Silv.ItemLog.FontSize; };
- // Initialization
- Window_ItemLog.prototype.initialize = function(x, y, width, height)
- {
- Window_Base.prototype.initialize.call(this, x, y, width, height);
- this._helpWindow = null;
- this._handlers = {};
- this._touching = false;
- this.deactivate();
- var maxStackLines = parseInt((height - Silv.ItemLog.EntryTextOffsetY * 2) / parseFloat(Silv.ItemLog.FontSize));
- Silv.ItemLog.TextLines = new Queue_LSize(maxStackLines);
- this.fadeOutCnt = 0;
- this.isFadingOut = false;
- this.isFadedOut = false;
- this.update();
- };
- // Update
- Window_ItemLog.prototype.update = function()
- {
- Window_Base.prototype.update.call(this);
- Silv.ItemLog.AlreadyPlayedSFX = false;
- this.updateFadeOut();
- this.drawItemLogContents();
- };
- // Update Fading Out
- Window_ItemLog.prototype.updateFadeOut = function()
- {
- if (this.isFadedOut) { return; }
- if (!this.isFadingOut)
- {
- this.fadeOutCnt++;
- if (this.fadeOutCnt > Silv.ItemLog.FadeoutDelay)
- {
- this.fadeOutCnt = 0;
- this.isFadingOut = true;
- }
- }
- else
- {
- this.fadeOut();
- }
- };
- Window_ItemLog.prototype.fadeOut = function()
- {
- this.opacity = this.contentsOpacity -= Silv.ItemLog.FadeoutSpeed;
- this.isFadedOut = (this.opacity <= 0);
- };
- Window_ItemLog.prototype.resetFade = function()
- {
- this.isFadingOut = false;
- this.fadeOutCnt = 0;
- this.opacity = this.contentsOpacity = 255;
- this.isFadedOut = false;
- };
- Window_ItemLog.prototype.fadeOutInstantly = function()
- {
- this.opacity = this.contentsOpacity = 0;
- this.isFadedOut = true;
- };
- // Drawing
- Window_ItemLog.prototype.drawItemLogContents = function()
- {
- if (typeof Silv.ItemLog.TextLines === 'string') { return; }
- this.contents.clear();
- this.resetTextColor();
- var textLinesCnt = Silv.ItemLog.TextLines.length;
- var text = null;
- for (var i = textLinesCnt - 1; i >= 0; i--)
- {
- var line = Silv.ItemLog.TextLines.itemByIdx(i);
- text = line.t1;
- this.drawText(text, Silv.ItemLog.EntryTextOffsetX, Silv.ItemLog.EntryTextOffsetY + i * Silv.ItemLog.FontSize, 256, 'left');
- var newOffsetX = Silv.ItemLog.EntryTextOffsetX + this.contents.measureTextWidth(line.t1);
- this.drawLootIcon(line.iconIndex, newOffsetX, Silv.ItemLog.EntryTextOffsetY + i * Silv.ItemLog.FontSize + Silv.ItemLog.IconOffsetY);
- newOffsetX += Silv.ItemLog.FontSize + 4;
- text = line.t2;
- this.drawText(text, newOffsetX, Silv.ItemLog.EntryTextOffsetY + i * Silv.ItemLog.FontSize, 256, 'left');
- this.contents.textColor = shadeColor(this.contents.textColor, Silv.ItemLog.TextShadingValue);
- }
- function shadeColor(color, percent)
- {
- var f=parseInt(color.slice(1),16),t=percent<0?0:255,p=percent<0?percent*-1:percent,R=f>>16,G=f>>8&0x00FF,B=f&0x0000FF;
- return "#"+(0x1000000+(Math.round((t-R)*p)+R)*0x10000+(Math.round((t-G)*p)+G)*0x100+(Math.round((t-B)*p)+B)).toString(16).slice(1);
- }
- };
- Window_ItemLog.prototype.drawLootIcon = function(iconIndex, x, y)
- {
- var bitmap = ImageManager.loadSystem('IconSet');
- var pw = Window_Base._iconWidth;
- var ph = Window_Base._iconHeight;
- var sx = iconIndex % 16 * pw;
- var sy = Math.floor(iconIndex / 16) * ph;
- this.contents.blt(bitmap, sx, sy, pw, ph, x, y, Silv.ItemLog.FontSize, Silv.ItemLog.FontSize);
- };
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // Create the ItemLog window
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- var alias_silv_itemlog_createDisplayObjects = Scene_Map.prototype.createDisplayObjects;
- Scene_Map.prototype.createDisplayObjects = function()
- {
- alias_silv_itemlog_createDisplayObjects.call(this);
- this.createItemLogWindow();
- };
- Scene_Map.prototype.createItemLogWindow = function()
- {
- var x = 0;
- if (Silv.ItemLog.WindowHorizontalAlignment == 'right') { x = Graphics.width - Silv.ItemLog.WindowWidth; }
- var y = 0;
- if (Silv.ItemLog.WindowVerticalAlignment == 'bottom') { y = Graphics.height - Silv.ItemLog.WindowHeight; }
- if (Silv.ItemLog.Window !== null) { this.removeWindow(Silv.ItemLog.Window); }
- Silv.ItemLog.Window = new Window_ItemLog(x + Silv.ItemLog.Window_X, y + Silv.ItemLog.Window_Y, Silv.ItemLog.WindowWidth, Silv.ItemLog.WindowHeight);
- Silv.ItemLog.Window.fadeOutInstantly();
- this.addChildAt(Silv.ItemLog.Window, 1);
- };
- // Omg why does RPG Maker not have this method by default...
- Scene_Base.prototype.removeWindow = function(window)
- {
- var index = this.children.indexOf(window);
- if (index > -1) { this.children.splice(index, 1); }
- };
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // Saving & Loading
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- /* Code below does not work for some reason. It does save the data but not load it properly.
- var alias_silv_ItemLog_makeSaveContents = DataManager.makeSaveContents;
- DataManager.makeSaveContents = function()
- {
- contents = alias_silv_ItemLog_makeSaveContents.call(this);
- contents.itemLogData = Silv.ItemLog.TextLines;
- return contents;
- }
- var alias_silv_ItemLog_extractSaveContents = DataManager.extractSaveContents;
- DataManager.extractSaveContents = function(contents)
- {
- alias_silv_ItemLog_extractSaveContents.call(this, contents);
- Silv = Silv || {};
- Silv.ItemLog = Silv.ItemLog || {};
- Silv.ItemLog.TextLines = contents.itemLogData;
- }
- */
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // Automatically add items
- //------------------------
- //
- // Explanation of the this._params:
- //
- // Gold
- // [0] = 0: increase, 1: decrease
- // [1] = 0: value, 1: $gameVariable was used to determine amount of gold
- // [2] = it is a direct amount of gold but if [1] == 1, then this is the gameVariable number that contains the gold-amount instead.
- //
- //
- // Items, Weapons & Armor
- // [0] = armor id matching the $dataArmors index
- // [1] = 0: increase, 1: decrease
- // [2] = 0: value, 1: $gameVariable was used to determine amount of armors
- // [3] = it is a direct amount of gold but if [2] == 1, then this is the gameVariable number that contains the gold-amount instead.
- // [4] = include equipment? true/false
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- Silv.ItemLog.getAmount = function(params) // does not apply to gold! Gold has different parameters.. Yay for RPG Maker...
- {
- return ((params[2] == 0) ? params[3] : $gameVariables.value(params[3])) * (params[1] == 1 ? -1 : 1);
- };
- // Change Gold
- var alias_silv_command125 = Game_Interpreter.prototype.command125;
- Game_Interpreter.prototype.command125 = function()
- {
- var amount = (this._params[1] == 0) ? this._params[2] : $gameVariables.value(this._params[2]);
- if (this._params[0] == 1) { amount *= -1; }
- var retVal = alias_silv_command125.apply(this, arguments);
- if (Silv.ItemLog.AutoLogItems) { PluginCommand('ItemLog', ['gold', String(amount), 'skip']); }
- return retVal;
- };
- // Change Items
- var alias_silv_command126 = Game_Interpreter.prototype.command126;
- Game_Interpreter.prototype.command126 = function()
- {
- var retVal = alias_silv_command126.apply(this, arguments);
- if (Silv.ItemLog.AutoLogItems) { PluginCommand('ItemLog', ['item', this._params[0], Silv.ItemLog.getAmount(this._params), 'skip']); }
- return retVal;
- };
- // Change Weapons
- var alias_silv_command127 = Game_Interpreter.prototype.command127;
- Game_Interpreter.prototype.command127 = function()
- {
- var retVal = alias_silv_command127.apply(this, arguments);
- if (Silv.ItemLog.AutoLogItems) { PluginCommand('ItemLog', ['weapon', this._params[0], Silv.ItemLog.getAmount(this._params), 'skip']); }
- return retVal;
- };
- // Change Armors
- var alias_silv_command128 = Game_Interpreter.prototype.command128;
- Game_Interpreter.prototype.command128 = function()
- {
- var retVal = alias_silv_command128.apply(this, arguments);
- if (Silv.ItemLog.AutoLogItems) { PluginCommand('ItemLog', ['armor', this._params[0], Silv.ItemLog.getAmount(this._params), 'skip']); }
- return retVal;
- };
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // Plugin Command
- // Note: The items are separated by spaces. The command is the first word and any following words are args. args is an array.
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- Silv.ItemLog.getPrefixText = function(amount)
- {
- var prefixText = rpad((amount >= 0) ? Silv.ItemLog.PositivePrefixText : Silv.ItemLog.NegativePrefixText, ' ', Silv.ItemLog.PrefixLength);
- return prefixText;
- };
- var _Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
- Game_Interpreter.prototype.pluginCommand = function(command, args)
- {
- _Game_Interpreter_pluginCommand.call(this, command, args);
- if (command.toLowerCase() == 'itemlog') { PluginCommand(command, args); }
- };
- function PluginCommand(cmd, args)
- {
- if (args[0].toLowerCase() == 'show')
- {
- Silv.ItemLog.Window.resetFade();
- return;
- }
- // arg: item 1 10
- // arg: weapon 2 1
- // arg: weapon 2 1 skip
- // arg: gold 123
- var db = getDatabase(args[0]);
- var lootIdx = parseInt(args[1]);
- var skipAddItemToInventory = (String(args[args.length - 1]).toLowerCase() == 'skip');
- if (db != 'gold')
- {
- var amount = parseInt(args[2]);
- var amountStr = String(args[2]);
- var name = db[lootIdx].name;
- var iconIndex = db[lootIdx].iconIndex;
- var t1 = Silv.ItemLog.getPrefixText(amount) + lpad(Math.abs(amountStr), ' ', 2) + 'x ';
- var t2 = name;
- Silv.ItemLog.TextLines.queue({t1: t1, iconIndex: iconIndex, t2: t2});
- // Add loot
- if (Silv.ItemLog.AutoLootGain && !skipAddItemToInventory)
- {
- $gameParty.gainItem(db[lootIdx], amount);
- }
- // sfx
- if (!Silv.ItemLog.AlreadyPlayedSFX)
- {
- Play_SE(Silv.ItemLog.PickupSFXItem);
- Silv.ItemLog.AlreadyPlayedSFX = true;
- }
- }
- else // Gold
- {
- var amount = parseInt(args[1]);
- Silv.ItemLog.TextLines.queue({t1: Silv.ItemLog.getPrefixText(amount) + ' ', iconIndex: Silv.ItemLog.GoldIconIndex, t2: Math.abs(amount) + ' ' + $dataSystem.currencyUnit});
- // Add loot
- if (Silv.ItemLog.AutoLootGain && !skipAddItemToInventory)
- {
- $gameParty.gainGold(amount);
- }
- // sfx
- if (!Silv.ItemLog.AlreadyPlayedSFX)
- {
- Play_SE(Silv.ItemLog.PickupSFXGold);
- Silv.ItemLog.AlreadyPlayedSFX = true;
- }
- }
- Silv.ItemLog.Window.resetFade(); // Reset the fadeout, if any.
- function getDatabase(code)
- {
- code = code.toLowerCase();
- if (code.contains('item') || code.contains('itm'))
- return $dataItems;
- if (code.contains('armor'))
- return $dataArmors;
- if (code.contains('weapon') || code.contains('wpn'))
- return $dataWeapons;
- if (code.contains('gold'))
- return 'gold';
- throw('getDatabase('+ code + ') ERROR: invalid code.');
- }
- function Play_SE(filename)
- {
- AudioManager.playSe({name: filename, volume: 90, pitch: 100, pan: 0});
- }
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement