Guest User

ItemLog

a guest
Nov 24th, 2015
86
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================
  2. // SilvItemLog.js
  3. // Version: 1.03
  4. //=============================================================================
  5. /*:
  6.  * @plugindesc v1.03 Item Log. Displays the last looted items.
  7.    <SilverItemLog>
  8.  * @author Silver
  9.  *
  10.  * @param Auto Gain Loot
  11.  * @desc Automatically add the loot to the party's inventory? true/false
  12.  * @default true
  13.  *
  14.  * @param Auto Log Items
  15.  * @desc Automatically add items to the log window when added through the GainItem command? true/false
  16.  * @default true
  17.  *
  18.  * @param Item SFX
  19.  * @desc Automatically play this sound when picking up items, armors or weapons. Leave blank to disable.
  20.  * @default Item1
  21.  *
  22.  * @param Gold SFX
  23.  * @desc Automatically play this sound when picking up gold (currency). Leave blank to disable.
  24.  * @default Coin
  25.  *
  26.  * @param Window X
  27.  * @desc x-location of itemlog window. If window-alignment is set to Right, this will act as an offset value instead
  28.  * @default -2
  29.  *
  30.  * @param Window Y
  31.  * @desc y-location of itemlog window. If window-alignment is set to Top, this will act as an offset value instead
  32.  * @default 2
  33.  *
  34.  * @param Window Horizontal Alignment
  35.  * @desc Left/Right
  36.  * @default Right
  37.  *
  38.  * @param Window Vertical Alignment
  39.  * @desc Top/Bottom
  40.  * @default Top
  41.  *
  42.  * @param Window Width
  43.  * @desc width of the itemlog window
  44.  * @default 400
  45.  *
  46.  * @param Window Height
  47.  * @desc height of the itemlog window
  48.  * @default 160
  49.  *
  50.  * @param Font Size
  51.  * @desc Size of the font
  52.  * @default 24
  53.  *
  54.  * @param Text Offset X
  55.  * @desc Text offset X for the log entries
  56.  * @default 10
  57.  *
  58.  * @param Text Offset Y
  59.  * @desc Text offset Y for the log entries
  60.  * @default 6
  61.  *
  62.  * @param Standard Padding
  63.  * @desc Leave at default (it's basically an X and Y offset)
  64.  * @default 0
  65.  *
  66.  * @param Prefix Text
  67.  * @desc Prefix text
  68.  * @default Received
  69.  *
  70.  * @param Icon Y-offset
  71.  * @desc Extra Y-offset for the drawing of the icons to better align them with the sentences
  72.  * @default 4
  73.  *
  74.  * @param Fadeout Delay
  75.  * @desc How long before the window starts fading out (in frames)
  76.  * @default 240
  77.  *
  78.  * @param Fadeout Speed
  79.  * @desc How fast the window fades out
  80.  * @default 2
  81.  *
  82.  * @param Text Shading
  83.  * @desc Displays previously looted items in darker text. Set to 0 to disable.
  84.  * @default -0.15
  85.  *
  86.  * @param Gold IconIndex
  87.  * @desc iconindex for the gold/currency icon
  88.  * @default 163
  89.  *
  90.  * @param Window Skin
  91.  * @desc Name of the window skin to use for this window
  92.  * @default Window_ItemLog
  93.  *
  94.  * @help
  95.  * Plugin Command:
  96.  * ItemLog <loot type> <loot database-index> <amount> <optional: skip (override: does not actually add the item to the inventory)>
  97.  * example to loot 3 potions: ItemLog item 1 3
  98.  * example to loot 5 swords:  ItemLog weapon 1 5
  99.  * example to loot 6 axes:    ItemLog weapon 2 6
  100.  * example to log (but does not add to inventory regardless of the "Auto Gain Loot" parameter) 6 axes:    ItemLog weapon 2 6 skip
  101.  *
  102.  * The exception is gold, examples:
  103.  *                            ItemLog gold 123
  104.  *                            ItemLog gold 999
  105.  *
  106.  * Use the plugin command "ItemLog show" (w/o quotes) to show the window
  107.  * again without adding anything.
  108.  *
  109.  * Note that all commands & parameters are NOT case sensitive.
  110.  */
  111. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  112. // Utilities
  113. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  114. String.prototype.contains = function(it) { return this.indexOf(it) != -1; };
  115.  
  116. // Stack with limited size
  117. // Automatically removes the first element(s) if it exceeds it's maxsize
  118. function Queue_LSize(maxSize)
  119. {
  120.     this._maxSize = maxSize;
  121.     this.length = 0;
  122.     this._storage = [];
  123. }
  124.  
  125. Queue_LSize.prototype.itemByIdx = function(index)
  126. {
  127.     return this._storage[index];
  128. }
  129.  
  130. Queue_LSize.prototype.queue = function(data)
  131. {
  132.     this._storage[this.length] = data;
  133.     this.length++;
  134.     while (this.length > this._maxSize) { this.onAutoRemoval(this.dequeue()); }
  135. };
  136.  
  137. // For aliasing
  138. Queue_LSize.prototype.onAutoRemoval = function(removed_item) {}
  139.  
  140.  // Remove and return first item in array
  141. Queue_LSize.prototype.dequeue = function()
  142. {
  143.     if (this.length)
  144.     {
  145.         var deletedData = this._storage.shift();
  146.         this.length--;
  147.         return deletedData;
  148.     }
  149. };
  150. // Remove and return last item in array
  151. Queue_LSize.prototype.pop = function()
  152. {
  153.     if (this.length)
  154.     {
  155.         var deletedData = this._storage.pop();
  156.         this.length--;
  157.         return deletedData;
  158.     }
  159. };
  160. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  161. // Variables
  162. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  163. var Silv = Silv || {};
  164. Silv.Parameters = $plugins.filter(function(p) { return p.description.contains('<SilverItemLog>'); })[0].parameters;
  165. Silv.ItemLog = Silv.ItemLog || {};
  166. Silv.ItemLog.Window = null;
  167. Silv.ItemLog.AlreadyPlayedSFX = false;
  168. Silv.ItemLog.AutoLootGain = Silv.Parameters['Auto Gain Loot'].toLowerCase() == 'true';
  169. Silv.ItemLog.AutoLogItems = Silv.Parameters['Auto Log Items'].toLowerCase() == 'true';
  170. Silv.ItemLog.PickupSFXItem = Silv.Parameters['Item SFX'];
  171. Silv.ItemLog.PickupSFXGold = Silv.Parameters['Gold SFX'];
  172. Silv.ItemLog.Window_X = parseInt(Silv.Parameters['Window X']);
  173. Silv.ItemLog.Window_Y = parseInt(Silv.Parameters['Window Y']);
  174. Silv.ItemLog.WindowWidth = parseInt(Silv.Parameters['Window Width']);
  175. Silv.ItemLog.WindowHeight = parseInt(Silv.Parameters['Window Height']);
  176. Silv.ItemLog.WindowHorizontalAlignment = Silv.Parameters['Window Horizontal Alignment'].toLowerCase();
  177. Silv.ItemLog.WindowVerticalAlignment = Silv.Parameters['Window Vertical Alignment'].toLowerCase();
  178. Silv.ItemLog.FontSize = parseInt(Silv.Parameters['Font Size']);
  179. Silv.ItemLog.EntryTextOffsetX = parseInt(Silv.Parameters['Text Offset X']);
  180. Silv.ItemLog.EntryTextOffsetY = parseInt(Silv.Parameters['Text Offset Y']);
  181. Silv.ItemLog.StandardPadding = parseInt(Silv.Parameters['Standard Padding']);
  182. Silv.ItemLog.TextLines = Silv.ItemLog.TextLines || 'Error: Not yet set';
  183. Silv.ItemLog.PrefixText = Silv.Parameters['Prefix Text'];
  184. if (Silv.ItemLog.PrefixText.slice(-1) != ' ') { Silv.ItemLog.PrefixText += ' '; }
  185. Silv.ItemLog.IconOffsetY = parseInt(Silv.Parameters['Icon Y-offset']);
  186. Silv.ItemLog.FadeoutDelay = parseInt(Silv.Parameters['Fadeout Delay']);
  187. Silv.ItemLog.FadeoutSpeed = parseInt(Silv.Parameters['Fadeout Speed']);
  188. Silv.ItemLog.TextShadingValue = parseFloat(Silv.Parameters['Text Shading']);
  189. Silv.ItemLog.GoldIconIndex = parseInt(Silv.Parameters['Gold IconIndex']);
  190. Silv.ItemLog.WindowSkin = Silv.Parameters['Window Skin'];
  191.  
  192. (function()
  193. {
  194. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  195. // Window
  196. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  197. function Window_ItemLog() { this.initialize.apply(this, arguments); }
  198. // Inherit from base window
  199. Window_ItemLog.prototype = Object.create(Window_Base.prototype);
  200. // Set Constructor
  201. Window_ItemLog.prototype.constructor = Window_ItemLog;
  202.  
  203. Window_ItemLog.prototype.loadWindowskin = function() { this.windowskin = ImageManager.loadSystem(Silv.ItemLog.WindowSkin); }
  204. Window_ItemLog.prototype.standardPadding = function() { return Silv.ItemLog.StandardPadding; }
  205. Window_ItemLog.prototype.standardFontSize = function() { return Silv.ItemLog.FontSize; }
  206.  
  207. // Initialization
  208. Window_ItemLog.prototype.initialize = function(x, y, width, height)
  209. {
  210.     Window_Base.prototype.initialize.call(this, x, y, width, height);
  211.     this._helpWindow = null;
  212.     this._handlers = {};
  213.     this._touching = false;
  214.     this.deactivate();
  215.     var maxStackLines = parseInt((height - Silv.ItemLog.EntryTextOffsetY * 2) / parseFloat(Silv.ItemLog.FontSize));
  216.     Silv.ItemLog.TextLines = new Queue_LSize(maxStackLines);
  217.     this.fadeOutCnt = 0;
  218.     this.isFadingOut = false;
  219.     this.isFadedOut = false;
  220.    
  221.     this.update();
  222. }
  223.  
  224. // Update
  225. Window_ItemLog.prototype.update = function()
  226. {
  227.     Window_Base.prototype.update.call(this);
  228.    
  229.     Silv.ItemLog.AlreadyPlayedSFX = false;
  230.     this.updateFadeOut();
  231.     this.drawItemLogContents();
  232. }
  233.  
  234. // Update Fading Out
  235. Window_ItemLog.prototype.updateFadeOut = function()
  236. {
  237.     if (this.isFadedOut) { return; }
  238.    
  239.     if (!this.isFadingOut)
  240.     {
  241.         this.fadeOutCnt++;
  242.         if (this.fadeOutCnt > Silv.ItemLog.FadeoutDelay)
  243.         {
  244.             this.fadeOutCnt = 0;
  245.             this.isFadingOut = true;
  246.         }
  247.     }
  248.     else
  249.     {
  250.         this.fadeOut();
  251.     }
  252. }
  253.  
  254. Window_ItemLog.prototype.fadeOut = function()
  255. {
  256.     this.opacity = this.contentsOpacity -= Silv.ItemLog.FadeoutSpeed;
  257.     this.isFadedOut = (this.opacity <= 0);
  258. }
  259.  
  260. Window_ItemLog.prototype.resetFade = function()
  261. {
  262.     this.isFadingOut = false;
  263.     this.fadeOutCnt = 0;
  264.     this.opacity = this.contentsOpacity  = 255;
  265.     this.isFadedOut = false;
  266. }
  267.  
  268. Window_ItemLog.prototype.fadeOutInstantly = function()
  269. {
  270.     this.opacity = this.contentsOpacity = 0;
  271.     this.isFadedOut = true;
  272. }
  273.  
  274. // Drawing
  275. Window_ItemLog.prototype.drawItemLogContents = function()
  276. {
  277.     if (typeof Silv.ItemLog.TextLines === 'string') { return; }
  278.     this.contents.clear();
  279.     this.resetTextColor();
  280.    
  281.     var textLinesCnt = Silv.ItemLog.TextLines.length;
  282.     for (var i = textLinesCnt - 1; i >= 0; i--)
  283.     {
  284.         var line = Silv.ItemLog.TextLines.itemByIdx(i);
  285.         text = line.t1;
  286.         this.drawText(text, Silv.ItemLog.EntryTextOffsetX, Silv.ItemLog.EntryTextOffsetY + i * Silv.ItemLog.FontSize, 256, 'left');
  287.         var newOffsetX = Silv.ItemLog.EntryTextOffsetX + this.contents.measureTextWidth(line.t1);
  288.         this.drawLootIcon(line.iconIndex, newOffsetX, Silv.ItemLog.EntryTextOffsetY + i * Silv.ItemLog.FontSize + Silv.ItemLog.IconOffsetY);
  289.         newOffsetX += Silv.ItemLog.FontSize + 4;
  290.         text = line.t2;
  291.         this.drawText(text, newOffsetX, Silv.ItemLog.EntryTextOffsetY + i * Silv.ItemLog.FontSize, 256, 'left');
  292.        
  293.         this.contents.textColor = shadeColor(this.contents.textColor, Silv.ItemLog.TextShadingValue);
  294.     }
  295.    
  296.     function shadeColor(color, percent)
  297.     {  
  298.         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;
  299.         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);
  300.     }
  301. }
  302.  
  303. Window_ItemLog.prototype.drawLootIcon = function(iconIndex, x, y)
  304. {
  305.     var bitmap = ImageManager.loadSystem('IconSet');
  306.     var pw = Window_Base._iconWidth;
  307.     var ph = Window_Base._iconHeight;
  308.     var sx = iconIndex % 16 * pw;
  309.     var sy = Math.floor(iconIndex / 16) * ph;
  310.     this.contents.blt(bitmap, sx, sy, pw, ph, x, y, Silv.ItemLog.FontSize, Silv.ItemLog.FontSize);
  311. };
  312. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  313. // Create the ItemLog window
  314. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  315. var alias_silv_itemlog_createDisplayObjects = Scene_Map.prototype.createDisplayObjects;
  316. Scene_Map.prototype.createDisplayObjects = function()
  317. {
  318.     alias_silv_itemlog_createDisplayObjects.call(this);
  319.     this.createItemLogWindow();
  320. }
  321.  
  322. Scene_Map.prototype.createItemLogWindow = function()
  323. {
  324.     x = 0;
  325.     if (Silv.ItemLog.WindowHorizontalAlignment == 'right') { x = Graphics.width - Silv.ItemLog.WindowWidth; }
  326.     y = 0;
  327.     if (Silv.ItemLog.WindowVerticalAlignment == 'bottom') { y = Graphics.height - Silv.ItemLog.WindowHeight; }
  328.    
  329.     if (Silv.ItemLog.Window != null) { this.removeWindow(Silv.ItemLog.Window); }
  330.     Silv.ItemLog.Window = new Window_ItemLog(x + Silv.ItemLog.Window_X, y + Silv.ItemLog.Window_Y, Silv.ItemLog.WindowWidth, Silv.ItemLog.WindowHeight);
  331.     Silv.ItemLog.Window.fadeOutInstantly();
  332.     this.addChildAt(Silv.ItemLog.Window, 1);
  333. }
  334.  
  335. // Omg why does RPG Maker not have this method by default...
  336. Scene_Base.prototype.removeWindow = function(window)
  337. {
  338.     var index = this.children.indexOf(window);
  339.     if (index > -1) { this.children.splice(index, 1); }
  340. }
  341. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  342. // Saving & Loading
  343. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  344. /* Code below does not work for some reason. It does save the data but not load it properly.
  345. var alias_silv_ItemLog_makeSaveContents = DataManager.makeSaveContents;
  346. DataManager.makeSaveContents = function()
  347. {
  348.     contents = alias_silv_ItemLog_makeSaveContents.call(this);
  349.     contents.itemLogData = Silv.ItemLog.TextLines;
  350.     return contents;
  351. }
  352.  
  353. var alias_silv_ItemLog_extractSaveContents = DataManager.extractSaveContents;
  354. DataManager.extractSaveContents = function(contents)
  355. {
  356.     alias_silv_ItemLog_extractSaveContents.call(this, contents);
  357.     Silv = Silv || {};
  358.     Silv.ItemLog = Silv.ItemLog || {};
  359.     Silv.ItemLog.TextLines = contents.itemLogData;
  360. }
  361. */
  362. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  363. // Automatically add items
  364. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  365. // Change Gold
  366. var alias_silv_command125 = Game_Interpreter.prototype.command125;
  367. Game_Interpreter.prototype.command125 = function()
  368. {
  369.     var retVal = alias_silv_command125.apply(this, arguments);
  370.     if (Silv.ItemLog.AutoLogItems) { PluginCommand('ItemLog', ['gold', String(this._params[2]), 'skip']); }
  371.     return retVal;
  372. }
  373.  
  374. // Change Items
  375. var alias_silv_command126 = Game_Interpreter.prototype.command126;
  376. Game_Interpreter.prototype.command126 = function()
  377. {
  378.     var retVal = alias_silv_command126.apply(this, arguments);
  379.     if (Silv.ItemLog.AutoLogItems) { PluginCommand('ItemLog', ['item', this._params[0], this._params[3], 'skip']); }
  380.     return retVal;
  381. }
  382.  
  383. // Change Weapons
  384. var alias_silv_command127 = Game_Interpreter.prototype.command127;
  385. Game_Interpreter.prototype.command127 = function()
  386. {
  387.     var retVal = alias_silv_command127.apply(this, arguments);
  388.     if (Silv.ItemLog.AutoLogItems) { PluginCommand('ItemLog', ['weapon', this._params[0], this._params[3], 'skip']); }
  389.     return retVal;
  390. }
  391.  
  392. // Change Armors
  393. var alias_silv_command128 = Game_Interpreter.prototype.command128;
  394. Game_Interpreter.prototype.command128 = function()
  395. {
  396.     var retVal = alias_silv_command128.apply(this, arguments);
  397.     if (Silv.ItemLog.AutoLogItems) { PluginCommand('ItemLog', ['armor', this._params[0], this._params[3], 'skip']); }
  398.     return retVal;
  399. }
  400. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  401. // Plugin Command
  402. // Note: The items are separated by spaces. The command is the first word and any following words are args. args is an array.
  403. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  404. var _Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
  405. Game_Interpreter.prototype.pluginCommand = function(command, args)
  406. {
  407.     _Game_Interpreter_pluginCommand.call(this, command, args);
  408.     if (command.toLowerCase() == 'itemlog') { PluginCommand(command, args); }
  409. }
  410.  
  411. function PluginCommand(cmd, args)
  412. {
  413.     if (args[0].toLowerCase() == 'show')
  414.     {
  415.         Silv.ItemLog.Window.resetFade();
  416.         return;
  417.     }
  418.    
  419.     // arg: item 1 10
  420.     // arg: weapon 2 1
  421.     // arg: weapon 2 1 skip
  422.     // arg: gold 123
  423.     var db = getDatabase(args[0]);
  424.     var lootIdx = parseInt(args[1]);
  425.     var skipAddItemToInventory = (String(args[args.length - 1]).toLowerCase() == 'skip');
  426.     if (db != 'gold')
  427.     {
  428.         var amount = args[2];
  429.         if (amount.length == 1) { amount = ' ' + amount; }
  430.         var name = db[lootIdx].name;
  431.         var iconIndex = db[lootIdx].iconIndex;
  432.        
  433.         var t1 = Silv.ItemLog.PrefixText + amount + 'x ';
  434.         var t2 = name;             
  435.        
  436.         Silv.ItemLog.TextLines.queue({t1: t1, iconIndex: iconIndex, t2: t2});
  437.        
  438.         // Add loot
  439.         if (Silv.ItemLog.AutoLootGain && !skipAddItemToInventory)
  440.         {
  441.             $gameParty.gainItem(db[lootIdx], parseInt(args[2]));
  442.         }
  443.        
  444.         // sfx
  445.         if (!Silv.ItemLog.AlreadyPlayedSFX)
  446.         {
  447.             Play_SE(Silv.ItemLog.PickupSFXItem);
  448.             Silv.ItemLog.AlreadyPlayedSFX = true;
  449.         }
  450.     }
  451.     else
  452.     {
  453.         var amount = parseInt(args[1]);
  454.         Silv.ItemLog.TextLines.queue({t1: Silv.ItemLog.PrefixText + '    ', iconIndex: Silv.ItemLog.GoldIconIndex, t2: amount + ' ' + $dataSystem.currencyUnit});
  455.        
  456.         // Add loot
  457.         if (Silv.ItemLog.AutoLootGain && !skipAddItemToInventory)
  458.         {
  459.             $gameParty.gainGold(amount);
  460.         }
  461.        
  462.         // sfx
  463.         if (!Silv.ItemLog.AlreadyPlayedSFX)
  464.         {
  465.             Play_SE(Silv.ItemLog.PickupSFXGold);
  466.             Silv.ItemLog.AlreadyPlayedSFX = true;
  467.         }
  468.     }
  469.     Silv.ItemLog.Window.resetFade(); // Reset the fadeout, if any.
  470.    
  471.     function getDatabase(code)
  472.     {
  473.         code = code.toLowerCase();
  474.         if (code.contains('item') || code.contains('itm'))
  475.             return $dataItems;
  476.         if (code.contains('armor'))
  477.             return $dataArmors;
  478.         if (code.contains('weapon') || code.contains('wpn'))
  479.             return $dataWeapons;
  480.         if (code.contains('gold'))
  481.             return 'gold';
  482.        
  483.         throw('getDatabase('+ code + ') ERROR: invalid code.');
  484.     }
  485.    
  486.     function Play_SE(filename)
  487.     {
  488.         AudioManager.playSe({name: filename, volume: 90, pitch: 100, pan: 0});
  489.     }
  490. }
  491. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  492. })();
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×