Advertisement
Guest User

Untitled

a guest
Dec 1st, 2015
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================
  2. // SilvItemLog.js
  3. // Version: 1.04
  4. //=============================================================================
  5. /*:
  6.  * @plugindesc v1.04 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.     var text = null;
  283.     for (var i = textLinesCnt - 1; i >= 0; i--)
  284.     {
  285.         var line = Silv.ItemLog.TextLines.itemByIdx(i);
  286.         text = line.t1;
  287.         this.drawText(text, Silv.ItemLog.EntryTextOffsetX, Silv.ItemLog.EntryTextOffsetY + i * Silv.ItemLog.FontSize, 256, 'left');
  288.         var newOffsetX = Silv.ItemLog.EntryTextOffsetX + this.contents.measureTextWidth(line.t1);
  289.         this.drawLootIcon(line.iconIndex, newOffsetX, Silv.ItemLog.EntryTextOffsetY + i * Silv.ItemLog.FontSize + Silv.ItemLog.IconOffsetY);
  290.         newOffsetX += Silv.ItemLog.FontSize + 4;
  291.         text = line.t2;
  292.         this.drawText(text, newOffsetX, Silv.ItemLog.EntryTextOffsetY + i * Silv.ItemLog.FontSize, 256, 'left');
  293.        
  294.         this.contents.textColor = shadeColor(this.contents.textColor, Silv.ItemLog.TextShadingValue);
  295.     }
  296.    
  297.     function shadeColor(color, percent)
  298.     {  
  299.         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;
  300.         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);
  301.     }
  302. };
  303.  
  304. Window_ItemLog.prototype.drawLootIcon = function(iconIndex, x, y)
  305. {
  306.     var bitmap = ImageManager.loadSystem('IconSet');
  307.     var pw = Window_Base._iconWidth;
  308.     var ph = Window_Base._iconHeight;
  309.     var sx = iconIndex % 16 * pw;
  310.     var sy = Math.floor(iconIndex / 16) * ph;
  311.     this.contents.blt(bitmap, sx, sy, pw, ph, x, y, Silv.ItemLog.FontSize, Silv.ItemLog.FontSize);
  312. };
  313. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  314. // Create the ItemLog window
  315. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  316. var alias_silv_itemlog_createDisplayObjects = Scene_Map.prototype.createDisplayObjects;
  317. Scene_Map.prototype.createDisplayObjects = function()
  318. {
  319.     alias_silv_itemlog_createDisplayObjects.call(this);
  320.     this.createItemLogWindow();
  321. };
  322.  
  323. Scene_Map.prototype.createItemLogWindow = function()
  324. {
  325.     var x = 0;
  326.     if (Silv.ItemLog.WindowHorizontalAlignment == 'right') { x = Graphics.width - Silv.ItemLog.WindowWidth; }
  327.     var y = 0;
  328.     if (Silv.ItemLog.WindowVerticalAlignment == 'bottom') { y = Graphics.height - Silv.ItemLog.WindowHeight; }
  329.    
  330.     if (Silv.ItemLog.Window !== null) { this.removeWindow(Silv.ItemLog.Window); }
  331.     Silv.ItemLog.Window = new Window_ItemLog(x + Silv.ItemLog.Window_X, y + Silv.ItemLog.Window_Y, Silv.ItemLog.WindowWidth, Silv.ItemLog.WindowHeight);
  332.     Silv.ItemLog.Window.fadeOutInstantly();
  333.     this.addChildAt(Silv.ItemLog.Window, 1);
  334. };
  335.  
  336. // Omg why does RPG Maker not have this method by default...
  337. Scene_Base.prototype.removeWindow = function(window)
  338. {
  339.     var index = this.children.indexOf(window);
  340.     if (index > -1) { this.children.splice(index, 1); }
  341. };
  342. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  343. // Saving & Loading
  344. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  345. /* Code below does not work for some reason. It does save the data but not load it properly.
  346. var alias_silv_ItemLog_makeSaveContents = DataManager.makeSaveContents;
  347. DataManager.makeSaveContents = function()
  348. {
  349.     contents = alias_silv_ItemLog_makeSaveContents.call(this);
  350.     contents.itemLogData = Silv.ItemLog.TextLines;
  351.     return contents;
  352. }
  353.  
  354. var alias_silv_ItemLog_extractSaveContents = DataManager.extractSaveContents;
  355. DataManager.extractSaveContents = function(contents)
  356. {
  357.     alias_silv_ItemLog_extractSaveContents.call(this, contents);
  358.     Silv = Silv || {};
  359.     Silv.ItemLog = Silv.ItemLog || {};
  360.     Silv.ItemLog.TextLines = contents.itemLogData;
  361. }
  362. */
  363. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  364. // Automatically add items
  365. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  366. // Change Gold
  367. var alias_silv_command125 = Game_Interpreter.prototype.command125;
  368. Game_Interpreter.prototype.command125 = function()
  369. {
  370.     var retVal = alias_silv_command125.apply(this, arguments);
  371.     if (Silv.ItemLog.AutoLogItems) { PluginCommand('ItemLog', ['gold', String(this._params[2]), 'skip']); }
  372.     return retVal;
  373. };
  374.  
  375. // Change Items
  376. var alias_silv_command126 = Game_Interpreter.prototype.command126;
  377. Game_Interpreter.prototype.command126 = function()
  378. {
  379.     var retVal = alias_silv_command126.apply(this, arguments);
  380.     if (Silv.ItemLog.AutoLogItems) { PluginCommand('ItemLog', ['item', this._params[0], this._params[3], 'skip']); }
  381.     return retVal;
  382. };
  383.  
  384. // Change Weapons
  385. var alias_silv_command127 = Game_Interpreter.prototype.command127;
  386. Game_Interpreter.prototype.command127 = function()
  387. {
  388.     var retVal = alias_silv_command127.apply(this, arguments);
  389.     if (Silv.ItemLog.AutoLogItems) { PluginCommand('ItemLog', ['weapon', this._params[0], this._params[3], 'skip']); }
  390.     return retVal;
  391. };
  392.  
  393. // Change Armors
  394. var alias_silv_command128 = Game_Interpreter.prototype.command128;
  395. Game_Interpreter.prototype.command128 = function()
  396. {
  397.     var retVal = alias_silv_command128.apply(this, arguments);
  398.     if (Silv.ItemLog.AutoLogItems) { PluginCommand('ItemLog', ['armor', this._params[0], this._params[3], 'skip']); }
  399.     return retVal;
  400. };
  401. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  402. // Plugin Command
  403. // Note: The items are separated by spaces. The command is the first word and any following words are args. args is an array.
  404. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  405. var _Game_Interpreter_pluginCommand = Game_Interpreter.prototype.pluginCommand;
  406. Game_Interpreter.prototype.pluginCommand = function(command, args)
  407. {
  408.     _Game_Interpreter_pluginCommand.call(this, command, args);
  409.     if (command.toLowerCase() == 'itemlog') { PluginCommand(command, args); }
  410. };
  411.  
  412. function PluginCommand(cmd, args)
  413. {
  414.     if (args[0].toLowerCase() == 'show')
  415.     {
  416.         Silv.ItemLog.Window.resetFade();
  417.         return;
  418.     }
  419.    
  420.     // arg: item 1 10
  421.     // arg: weapon 2 1
  422.     // arg: weapon 2 1 skip
  423.     // arg: gold 123
  424.     var db = getDatabase(args[0]);
  425.     var lootIdx = parseInt(args[1]);
  426.     var skipAddItemToInventory = (String(args[args.length - 1]).toLowerCase() == 'skip');
  427.     if (db != 'gold')
  428.     {
  429.         var amount = args[2];
  430.         if (amount.length == 1) { amount = ' ' + amount; }
  431.         var name = db[lootIdx].name;
  432.         var iconIndex = db[lootIdx].iconIndex;
  433.        
  434.         var t1 = Silv.ItemLog.PrefixText + amount + 'x ';
  435.         var t2 = name;             
  436.        
  437.         Silv.ItemLog.TextLines.queue({t1: t1, iconIndex: iconIndex, t2: t2});
  438.        
  439.         // Add loot
  440.         if (Silv.ItemLog.AutoLootGain && !skipAddItemToInventory)
  441.         {
  442.             $gameParty.gainItem(db[lootIdx], parseInt(args[2]));
  443.         }
  444.        
  445.         // sfx
  446.         if (!Silv.ItemLog.AlreadyPlayedSFX)
  447.         {
  448.             Play_SE(Silv.ItemLog.PickupSFXItem);
  449.             Silv.ItemLog.AlreadyPlayedSFX = true;
  450.         }
  451.     }
  452.     else
  453.     {
  454.         var amount = parseInt(args[1]);
  455.         Silv.ItemLog.TextLines.queue({t1: Silv.ItemLog.PrefixText + '    ', iconIndex: Silv.ItemLog.GoldIconIndex, t2: amount + ' ' + $dataSystem.currencyUnit});
  456.        
  457.         // Add loot
  458.         if (Silv.ItemLog.AutoLootGain && !skipAddItemToInventory)
  459.         {
  460.             $gameParty.gainGold(amount);
  461.         }
  462.        
  463.         // sfx
  464.         if (!Silv.ItemLog.AlreadyPlayedSFX)
  465.         {
  466.             Play_SE(Silv.ItemLog.PickupSFXGold);
  467.             Silv.ItemLog.AlreadyPlayedSFX = true;
  468.         }
  469.     }
  470.     Silv.ItemLog.Window.resetFade(); // Reset the fadeout, if any.
  471.    
  472.     function getDatabase(code)
  473.     {
  474.         code = code.toLowerCase();
  475.         if (code.contains('item') || code.contains('itm'))
  476.             return $dataItems;
  477.         if (code.contains('armor'))
  478.             return $dataArmors;
  479.         if (code.contains('weapon') || code.contains('wpn'))
  480.             return $dataWeapons;
  481.         if (code.contains('gold'))
  482.             return 'gold';
  483.        
  484.         throw('getDatabase('+ code + ') ERROR: invalid code.');
  485.     }
  486.    
  487.     function Play_SE(filename)
  488.     {
  489.         AudioManager.playSe({name: filename, volume: 90, pitch: 100, pan: 0});
  490.     }
  491. }
  492. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  493. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement