Advertisement
Guest User

Untitled

a guest
Apr 1st, 2016
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================
  2. // SilvStamina.js
  3. // Version: 1.07a
  4. //=============================================================================
  5. /*:
  6.  * @plugindesc v1.07a Basic dashing stamina script.
  7.    <SilverStamina>
  8.  * @author Silver
  9.  *
  10.  * @param -- General --
  11.  *
  12.  * @param Show Stamina Window
  13.  * @desc true/false
  14.  * @default true
  15.  *
  16.  * @param -- Positioning & Size --
  17.  *
  18.  * @param Window X
  19.  * @desc X-location of stamina window. If window-alignment is set to Right, this will act as an offset value instead
  20.  * @default 10
  21.  *
  22.  * @param Window Y
  23.  * @desc Y-location of stamina window. If window-alignment is set to Top, this will act as an offset value instead
  24.  * @default 10
  25.  *
  26.  * @param Window Width
  27.  * @desc width of the stamina window
  28.  * @default 170
  29.  *
  30.  * @param Window Height
  31.  * @desc height of the stamina window
  32.  * @default 72
  33.  *
  34.  * @param Window Horizontal Alignment
  35.  * @desc Left/Right
  36.  * @default Left
  37.  *
  38.  * @param Window Vertical Alignment
  39.  * @desc Top/Bottom
  40.  * @default Top
  41.  *
  42.  * @param Stamina Gauge Rectangle
  43.  * @desc The gauge rectangle. Format: x y width height
  44.  * @default 0 -20 132 24
  45.  *
  46.  * @param -- Stamina Pool --
  47.  *
  48.  * @param Stamina Decrease Mode
  49.  * @desc Use "Default" to decrease while moving. Use "Tile" to decrease stamina whenever the player enters a new tile.
  50.  * @default Default
  51.  *
  52.  * @param Stamina Decrease
  53.  * @desc Amount of stamina subtracted per update (use a positive number)
  54.  * @default 1
  55.  *
  56.  * @param Stamina Max
  57.  * @desc Maximum amount of stamina
  58.  * @default 300
  59.  *
  60.  * @param Stamina Recovery Delay
  61.  * @desc delay in update-calls before recovering stamina when not dashing
  62.  * @default 180
  63.  *
  64.  * @param Stamina Recovery Rate
  65.  * @desc How fast stamina is recovered (only when recovering)
  66.  * @default 0.3
  67.  *
  68.  * @param Stamina AutoDash Threshold
  69.  * @desc Do not automatically dash again before stamina is above this threshold (%) when recovering stamina. (0-100)
  70.  * @default 40
  71.  *
  72.  * @param Use Custom Stamina Regen Formula?
  73.  * @desc Use a custom stamina regeneration formula? If true, then the parameter "Stamina Regen Formula" is used as the formula. When false the default formula is used (which executes slightly faster).
  74.  * @default false
  75.  *
  76.  * @param Stamina Regen Formula
  77.  * @desc The formula for how fast to regenerate stamina over longer period of time. "Base" is the recovery rate. Case sensitive!
  78.  * @default base + Math.sqrt(x/50);
  79.  *
  80.  * @param -- Visuals --
  81.  *
  82.  * @param Stamina Gauge Color 1
  83.  * @desc Bar gradient color1 (start of bar) in hex
  84.  * @default #009900
  85.  *
  86.  * @param Stamina Gauge Color 2
  87.  * @desc Bar gradient color2 (end of bar) in hex
  88.  * @default #CC0000
  89.  *
  90.  * @param Draw Stamina Value
  91.  * @desc Draw text in stamina bar? Accepted values: absolute/percentage/both/none
  92.  * @default percentage
  93.  *
  94.  * @param Font Size
  95.  * @desc Size for stamina value
  96.  * @default 20
  97.  *
  98.  * @param Auto Hide Stamina Window
  99.  * @desc Automatically hide the stamina window if it's at max stamina for a specific period of time? true/false
  100.  * @default true
  101.  *
  102.  * @param -- Window Prefix Text--
  103.  *
  104.  * @param Window Text
  105.  * @desc Use :none to disable this
  106.  * @default :none
  107.  *
  108.  * @param Window Text Offset Y
  109.  * @desc y-coordinate for the text
  110.  * @default 19
  111.  *
  112.  * @param Window Text Spacing X
  113.  * @desc amount of room in coordinates between text and gauge
  114.  * @default 4
  115.  *
  116.  * @param -- Window --
  117.  *
  118.  * @param Hide Stamina Window Delay
  119.  * @desc After how many updates the stamina window should hide itself (if it remains at max stamina)
  120.  * @default 160
  121.  *
  122.  * @param Stamina Window Opacity
  123.  * @desc Stamina window opacity. Set to 0 to hide the window (will still show the bar)
  124.  * @default 255
  125.  *
  126.  * @param Window Slideout Direction
  127.  * @desc What direction to slide the stamina window out to. NoSlide/Left/Top/Right/Bottom
  128.  * @default Left
  129.  *
  130.  * @param Window Slideout Speed
  131.  * @desc How fast the window slides in&out
  132.  * @default 2
  133.  *
  134.  * @param -- Advanced --
  135.  *
  136.  * @param Common Events At Stamina Values
  137.  * @desc Example to run common event 15 at 0 stamina and common event 17 at 100% stamina: 0 15 100% 17.
  138.  *
  139.  * @param Common Event Stamina Value Activation Delta
  140.  * @desc How big the difference in stamina value must be between now and the last time a common event was ran. A value of 0 will run the Common Event every frame (if it matches the stamina value).
  141.  * @default 5
  142.  *
  143.  * @param Disable Stamina Consumption GameSwitch
  144.  * @desc The gameswitch to use to disable stamina consumption (or -1 to use none). ON = disabled
  145.  * @default -1
  146.  *
  147.  * @param Window Z-Index
  148.  * @desc Window Z-Index. Value must be > 0.
  149.  * @default 1
  150.  *
  151.  * @param Plugin Command Identifier
  152.  * @desc Do not change if you do not know what this is!
  153.  * @default stamina
  154.  *
  155.  * @help
  156.  * -------------------------------------
  157.  * Plugin Commands (not case sensitive):
  158.  * -------------------------------------
  159.  *
  160.  * Stamina Refill
  161.  * Instantly refills all of your stamina.
  162.  *
  163.  * Stamina Set <value>
  164.  * Instantly sets your stamina to the specified percentage (0-100).
  165.  * Example to set your stamina bar to 64: Stamina Set 64
  166.  *
  167.  * Stamina SetVar <variableIndex>
  168.  * Instantly sets your stamina to the specified percentage (0-100).
  169.  * Example to set the stamina using game-variable #10: Stamina SetVar 10
  170.  *
  171.  * Stamina SetEval <eval>
  172.  * Instantly sets your stamina to the specified percentage (0-100).
  173.  * Example: Example Stamina SetEval ($gameVariables.value(10) + 1) / 2
  174.  *
  175.  * Stamina Deplete
  176.  * Instantly sets your stamina to 0.
  177.  *
  178.  * Stamina ShowWindow
  179.  * Shows the stamina window. Does not work if you disabled the stamina window.
  180.  *
  181.  * Stamina HideWindow
  182.  * Hides the stamina window. Does not work if you disabled the stamina window.
  183.  * Also does not work if the stamina is currently regenerating.
  184.  *
  185.  * Stamina RefillHide
  186.  * Instantly refills all of your stamina and also hides the stamina window.
  187.  *
  188.  * Stamina SetMax <value>
  189.  * Sets a new max-stamina value.
  190.  *
  191.  * Stamina SetMaxVar <variableIndex>
  192.  * Sets a new max-stamina value.
  193.  * Example to set the max-stamina using game-variable #10: Stamina SetMaxVar 10
  194.  *
  195.  * Stamina SetMaxEval <eval>
  196.  * Sets a new max-stamina value. But this time it evaluates the code you enter as <eva>.
  197.  * Example: Stamina SetMaxEval ($gameVariables.value(10) + 1) / 2
  198.  * The above example assigns a new max-stamina using the game-variable 10 and then adding a 1 and then dividing it all by 2.
  199.  *
  200.  * Stamina IncreaseMax <value>
  201.  * Increases max stamina by the specified value. You can also use negative values.
  202.  *
  203.  * Stamina EnableDashing <true/false/toggle>
  204.  * Allows or prohibits dashing on the map.
  205.  * Examples:
  206.  * Stamina EnableDashing true
  207.  * Stamina EnableDashing false
  208.  * Stamina EnableDashing toggle
  209.  * -------------------------------------
  210.  * Map Notetags
  211.  * -------------------------------------
  212.  * <dstam_disable>
  213.  * Prevents stamina consumption on this map. Not that if the "Disable Stamina Consumption GameSwitch"
  214.  * is turned ON then you consume no stamina anyway.
  215.  *
  216.  * <disable_dashing>
  217.  * Prevents dashing on the map (with or without stamina).
  218.  *
  219.  * -------------------------------------
  220.  * Item Notetags (not case sensitive)
  221.  * -------------------------------------
  222.  * <dash_stamina:command (value)>
  223.  * Available commands:
  224.  *  - Add (may have a negative value)
  225.  *  - Refill
  226.  *  - Deplete
  227.  *  - IncreaseMax (may have a negative value)
  228.  * Examples:
  229.  * <dash_stamina:Add 10>
  230.  * <dash_stamina:Add -10>
  231.  * <dash_stamina:Refill>
  232.  * <dash_stamina:Deplete>
  233.  * <dash_stamina:IncreaseMax 1500>
  234.  * Note that those only work from the menu, not from battle
  235.  *
  236.  *--------------------------------------
  237.  * Version History:
  238.  *--------------------------------------
  239.  * v1.07a (1 April 2016) [Parameters Changed]
  240.  * - Added a new parameter to run common events at certain stamina values/percentages.
  241.  * - Added a new parameter "Use Custom Stamina Regen Formula?" for those who need more optimization. By default it does NOT use the custom formula (faster).
  242.  * - Added more comments to the code.
  243.  *
  244.  * v1.06 (26 March 2016)
  245.  * - Added new plugin commands: SetVar, SetEval, SetMaxEval & SetMaxVar.
  246.  * - The plugin command "Stamina EnableDashing" now accepts a third value: toggle.
  247.  *
  248.  * v1.05 (21 March 2016)
  249.  * - Added a new parameter "Stamina Decrease Mode". Now stamina can also be decreased on a per-tile-basis instead of only on a per-update-cycle-basis.
  250.  * - Added new functions and a new alias to accomodate the above new feature.
  251.  *
  252.  * v1.04 (03 February 2016)
  253.  * - Fixed a crash when transferring between maps using an autorun event with manual fading in or out (autorun stops execution of other events and some scripts like this one, which caused the crash).
  254.  *
  255.  * v1.03 (01 January 2016)
  256.  * - Used my new coding standards & refactored.
  257.  * - Switched to the Imported variable.
  258.  * - Fixed an accidental duplicate alias.
  259.  * - Fixed a bug in Scene_Map.prototype.updateMain() (which just so happened to cause no side-effects).
  260.  *
  261.  * v1.02 (12 December 2015)
  262.  * - Dashing for the current map can now be entirely disabled with a map-notetag and switched with a plugin command at any time.
  263.  * - Created a simple plugin addon to store the player-stamina in a global game-variable.
  264.  * - Enabled custom text to be drawn in front of the Stamina Window.
  265.  * - New feature: items can replenish, lower, refill and deplete stamina (only in the menu, not in battle).
  266.  *
  267.  * v1.01 (1 December 2015)
  268.  * - Removed strict-mode because... Possible bug in RPG Maker...
  269.  * - Refactored and fixed semicolons and missing var-keywords.
  270.  *
  271.  * v1.00 (26 November 2015)
  272.  * - First release.
  273.  * Alpha (November 2015)
  274.  * - First alpha release.
  275.  *
  276.  */
  277. // Imported
  278. var Imported = Imported || {};
  279. Imported.Silv_DashStamina = 1.07;
  280.  
  281. // #Parameters
  282. var Silv = Silv || {};
  283. Silv.Parameters = $plugins.filter(function(p) { return p.description.contains('<SilverStamina>'); })[0].parameters;
  284. Silv.DashStamina = Silv.DashStamina || {};
  285. // Non-parameters
  286. Silv.DashStamina.Window = null;
  287. Silv.DashStamina.ScreenIsFading = false;
  288. Silv.DashStamina.DashingDisabled = false;
  289. Silv.DashStamina.CommonEvents = [];
  290. Silv.DashStamina.HasCommonEvents = false;
  291. // General
  292. Silv.DashStamina.ShowWindow = Silv.Parameters['Show Stamina Window'].toLowerCase() === 'true';
  293. // Positioning & Size
  294. Silv.DashStamina.Window_X = parseInt(Silv.Parameters['Window X']);
  295. Silv.DashStamina.Window_Y = parseInt(Silv.Parameters['Window Y']);
  296. Silv.DashStamina.WindowWidth = parseInt(Silv.Parameters['Window Width']);
  297. Silv.DashStamina.WindowHeight = parseInt(Silv.Parameters['Window Height']);
  298. Silv.DashStamina.WindowHorizontalAlignment = (Silv.Parameters['Window Horizontal Alignment']).toLowerCase();
  299. Silv.DashStamina.WindowVerticalAlignment = (Silv.Parameters['Window Vertical Alignment']).toLowerCase();
  300. Silv.DashStamina.StaminaGaugeRectangle = {x: parseInt(Silv.Parameters['Stamina Gauge Rectangle'].split(' ')[0]), y: parseInt(Silv.Parameters['Stamina Gauge Rectangle'].split(' ')[1]), width: parseInt(Silv.Parameters['Stamina Gauge Rectangle'].split(' ')[2]), height: parseInt(Silv.Parameters['Stamina Gauge Rectangle'].split(' ')[3])};
  301. // Stamina Pool
  302. Silv.DashStamina.DecreaseMode = Silv.Parameters['Stamina Decrease Mode'].toLowerCase();
  303. Silv.DashStamina.StaminaDecrease = parseInt(Silv.Parameters['Stamina Decrease']);
  304. Silv.DashStamina.StaminaMax = parseInt(Silv.Parameters['Stamina Max']);
  305. Silv.DashStamina.StaminaRecoveryDelay = parseInt(Silv.Parameters['Stamina Recovery Delay']);
  306. Silv.DashStamina.StaminaRecoveryRate = parseInt(Silv.Parameters['Stamina Recovery Rate']);
  307. Silv.DashStamina.StaminaAutoDashThreshold = parseInt(Silv.Parameters['Stamina AutoDash Threshold']);
  308. Silv.DashStamina.UseCustomRegenFormula = Silv.Parameters['Use Custom Stamina Regen Formula?'].toLowerCase() === 'true';
  309. Silv.DashStamina.RegenFormula = Silv.Parameters['Stamina Regen Formula'];
  310. // Visuals
  311. Silv.DashStamina.StaminaGaugeColor1 = (Silv.Parameters['Stamina Gauge Color 1']).toUpperCase();
  312. Silv.DashStamina.StaminaGaugeColor2 = (Silv.Parameters['Stamina Gauge Color 2']).toUpperCase();
  313. Silv.DashStamina.DrawStaminaValue = Silv.Parameters['Draw Stamina Value'].toLowerCase();
  314. Silv.DashStamina.FontSize = parseInt(Silv.Parameters['Font Size']);
  315. // Window Prefix Text
  316. Silv.DashStamina.WindowText = Silv.Parameters['Window Text'];
  317. Silv.DashStamina.WindowTextOffsetY = parseInt(Silv.Parameters['Window Text Offset Y']);
  318. Silv.DashStamina.WindowTextGaugeSpacingX = parseInt(Silv.Parameters['Window Text Spacing X']);
  319. // Window
  320. Silv.DashStamina.AutoHideStaminaWindow = Silv.Parameters['Auto Hide Stamina Window'].toLowerCase() === 'true';
  321. Silv.DashStamina.HideStaminaWindowDelay = parseInt(Silv.Parameters['Hide Stamina Window Delay']);
  322. Silv.DashStamina.WindowOpacity = parseInt(Silv.Parameters['Stamina Window Opacity']);
  323. Silv.DashStamina.WindowSlideOutDir = Silv.Parameters['Window Slideout Direction'].toLowerCase();
  324. Silv.DashStamina.WindowSlideOutSpeed = parseFloat(Silv.Parameters['Window Slideout Speed']);
  325. // Advanced
  326. Silv.DashStamina.CE_Delta = parseInt(Silv.Parameters['Common Event Stamina Value Activation Delta']);
  327. Silv.DashStamina.DisableGameSwitch = parseInt(Silv.Parameters['Disable Stamina Consumption GameSwitch']);
  328. Silv.DashStamina.Window_Z = parseInt(Silv.Parameters['Window Z-Index']);
  329. Silv.DashStamina.PluginCmdId = Silv.Parameters['Plugin Command Identifier'];
  330.  
  331. // Alias
  332. Silv.Alias = Silv.Alias || {};
  333. if (!Silv.AddAlias)
  334. {
  335.     Silv.AddAlias = function(alias, original_method)
  336.     {
  337.         if (Silv.Alias[alias]) { throw new Error('Alias already exists: ' + alias); }
  338.         Silv.Alias[alias] = original_method;
  339.     };
  340. }
  341.  
  342.  
  343. // Convert&store data for running common events at certain stamina percentages
  344. (function()
  345. {
  346.     var splitted = Silv.Parameters['Common Events At Stamina Values'].split(' ');
  347.     for (var ceIdx=0; ceIdx<splitted.length; ceIdx+=2)
  348.     {
  349.         var threshold = splitted[ceIdx];
  350.         var thresholdIsPerc = false;
  351.         if (~threshold.indexOf('%'))
  352.         {
  353.             thresholdIsPerc = true;
  354.             threshold = parseInt(threshold.slice(0, -1)); // Remove % character
  355.         }
  356.         else
  357.         {
  358.             threshold = parseInt(threshold);
  359.         }
  360.         var commonEventID = parseInt(splitted[ceIdx + 1]);
  361.        
  362.         Silv.DashStamina.CommonEvents[ceIdx / 2] = { threshold:threshold, thresholdIsPerc:thresholdIsPerc, commonEventID:commonEventID, lastRunStaminaAmount:null };
  363.     }
  364.     Silv.DashStamina.HasCommonEvents = (Silv.DashStamina.CommonEvents.length > 0);
  365. })();
  366.  
  367. // Sanity checks
  368. (function()
  369. {
  370.     if ((Silv.DashStamina.DecreaseMode !== 'default') && (Silv.DashStamina.DecreaseMode !== 'tile')) { throw new Error('Invalid parameter-value for "Decrease Mode": ' + Silv.DashStamina.DecreaseMode + '.'); }
  371. })();
  372. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  373. (function()
  374. {
  375. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  376. // Utilities
  377. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  378.  
  379. // Usage: alert( hexToRgb("#0033ff").g ); // "51";
  380. function hexToRgb(hex)
  381. {
  382.     var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
  383.     return result ? {
  384.         r: parseInt(result[1], 16),
  385.         g: parseInt(result[2], 16),
  386.         b: parseInt(result[3], 16)
  387.     } : null;
  388. }
  389.  
  390. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  391. // Game Player
  392. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  393. //------------------------------------------------------------------------------------------------------------------------------------
  394. // Can the player possibly consume stamina?
  395. //------------------------------------------------------------------------------------------------------------------------------------
  396. Game_Player.prototype.hasStaminaConsumption = function()
  397. {
  398.     if ($gameSwitches.value(Silv.DashStamina.DisableGameSwitch)) { return false; }
  399.     if (!this.mapConsumeStamina) { return false; }
  400.     return true;
  401. };
  402.  
  403. //------------------------------------------------------------------------------------------------------------------------------------
  404. // Alias for whenever the player entered a new tile
  405. //------------------------------------------------------------------------------------------------------------------------------------
  406. Silv.AddAlias('dashStamina_Game_Player_updateMove', Game_Player.prototype.updateMove);
  407. Game_Player.prototype.updateMove = function()
  408. {
  409.     Silv.Alias.dashStamina_Game_Player_updateMove.apply(this, arguments);
  410.     if ((Silv.DashStamina.DecreaseMode === 'tile') && !this.isMoving() && this.isDashing() && this.hasStaminaConsumption())
  411.     {
  412.         // Consume stamina for the tile-decrease-mode
  413.         this.dashStamina -= Silv.DashStamina.StaminaDecrease;
  414.         this.postDecreaseStaminaHandling();
  415.     }
  416. };
  417.  
  418. //------------------------------------------------------------------------------------------------------------------------------------
  419. // Is&can the player consume stamina?
  420. //------------------------------------------------------------------------------------------------------------------------------------
  421. Game_Player.prototype.isConsumingStamina = function()
  422. {
  423.     return (this.isDashing() && this.isMoving() && this.hasStaminaConsumption());
  424. };
  425.  
  426. //------------------------------------------------------------------------------------------------------------------------------------
  427. // Call this method after decreasing the stamina
  428. //------------------------------------------------------------------------------------------------------------------------------------
  429. Game_Player.prototype.postDecreaseStaminaHandling = function()
  430. {
  431.     if (this.dashStamina < 0) { this.dashStamina = 0; }
  432.     this.isRecoveringStamina = false;
  433.     if (Silv.DashStamina.ShowWindow && !Silv.DashStamina.ScreenIsFading) { Silv.DashStamina.Window.showMe(); }
  434. };
  435.  
  436. //------------------------------------------------------------------------------------------------------------------------------------
  437. // Check if #Common Events (CE) need running and do so if applicable
  438. //------------------------------------------------------------------------------------------------------------------------------------
  439. Game_Player.prototype.staminaEventChecks = function()
  440. {
  441.     if (Silv.DashStamina.HasCommonEvents)
  442.     {
  443.         var staminaPercRounded = Math.round(this.dashStaminaPerc * 100);
  444.         for (var ceIdx=0; ceIdx<Silv.DashStamina.CommonEvents.length; ceIdx++)
  445.         {
  446.             if (Silv.DashStamina.CommonEvents[ceIdx].lastRunStaminaAmount === null)
  447.             {
  448.                 if (Silv.DashStamina.CommonEvents[ceIdx].thresholdIsPerc)
  449.                 {
  450.                     if (staminaPercRounded === Silv.DashStamina.CommonEvents[ceIdx].threshold) { this.staminaRunCE(ceIdx); }
  451.                 }
  452.                 else
  453.                 {
  454.                     if (this.dashStamina === Silv.DashStamina.CommonEvents[ceIdx].threshold) { this.staminaRunCE(ceIdx); }
  455.                 }
  456.             }
  457.             else
  458.             {
  459.                 if (Math.abs(Silv.DashStamina.CommonEvents[ceIdx].lastRunStaminaAmount - this.dashStamina) >= (Silv.DashStamina.CE_Delta - 1))
  460.                 {
  461.                     Silv.DashStamina.CommonEvents[ceIdx].lastRunStaminaAmount = null; // Allow this CE to be run again next time
  462.                 }
  463.             }
  464.         }
  465.     }
  466. };
  467.  
  468. Game_Player.prototype.staminaRunCE = function(ceIdx)
  469. {
  470.     $gameTemp.reserveCommonEvent(Silv.DashStamina.CommonEvents[ceIdx].commonEventID);
  471.     Silv.DashStamina.CommonEvents[ceIdx].lastRunStaminaAmount = this.dashStamina;
  472. };
  473.  
  474. //------------------------------------------------------------------------------------------------------------------------------------
  475. // #Update Stamina
  476. //------------------------------------------------------------------------------------------------------------------------------------
  477. Game_Player.prototype.updateStamina = function()
  478. {
  479.     this.oldDashStamina = this.dashStamina;
  480.    
  481.     var isConsumingStamina = this.isConsumingStamina();
  482.     if (isConsumingStamina)
  483.     {
  484.         // Only consume stamina here if the decrease-mode is set to default
  485.         if (Silv.DashStamina.DecreaseMode === 'default') { this.dashStamina -= Silv.DashStamina.StaminaDecrease; }
  486.         this.postDecreaseStaminaHandling();
  487.     }
  488.     else // not currently consuming stamina
  489.     {      
  490.         if (this.isRecoveringStamina)
  491.         {
  492.             this.staminaRecoveryTimeCnt++;
  493.             if (this.dashStamina < this.dashStaminaMax) // Recover the stamina
  494.             {
  495.                 // Recover stamina
  496.                 this.dashStamina += this.calculateStaminaRegen(Silv.DashStamina.StaminaRecoveryRate);
  497.                 Silv.DashStamina.clampMaxStamina();
  498.                
  499.                 if (Silv.DashStamina.ShowWindow && !Silv.DashStamina.ScreenIsFading) { Silv.DashStamina.Window.showMe(); }
  500.             }
  501.             else // Already at max stamina, find out when to hide the window if applicable and do so if possible
  502.             {
  503.                 // If the stamina window is used, attempt to autohide it if applicable
  504.                 if (Silv.DashStamina.ShowWindow && Silv.DashStamina.AutoHideStaminaWindow)
  505.                 {
  506.                     this.hideStaminaWindowDelayCnt += 1;
  507.                     if (this.hideStaminaWindowDelayCnt >= Silv.DashStamina.HideStaminaWindowDelay)
  508.                     {
  509.                         Silv.DashStamina.Window.hideMe();
  510.                         this.hideStaminaWindowDelayCnt = 0;
  511.                     }
  512.                 }
  513.                    
  514.             }
  515.         }
  516.         else // not currently recovering stamina, so find out when to start recovering it and do so if required
  517.         {
  518.             this.staminaRecoveryTimeCnt = 0;
  519.             this.staminaRecoveryDelayCnt += 1;
  520.             if (this.staminaRecoveryDelayCnt >= Silv.DashStamina.StaminaRecoveryDelay)
  521.             {
  522.                 this.staminaRecoveryDelayCnt = 0;
  523.                 this.isRecoveringStamina = true;
  524.             }
  525.         }
  526.     }
  527.  
  528.     // Stamina Percentage
  529.     this.dashStaminaPerc = this.dashStamina / parseFloat(this.dashStaminaMax);
  530.  
  531.     // Threshold
  532.     if (!isConsumingStamina)
  533.     {
  534.         this.requiresThresholdAmount = this.dashStaminaPerc * 100 < Silv.DashStamina.StaminaAutoDashThreshold;
  535.     }
  536.    
  537.     this.staminaEventChecks();
  538. };
  539.  
  540. //------------------------------------------------------------------------------------------------------------------------------------
  541. // Regeneration Formula
  542. //------------------------------------------------------------------------------------------------------------------------------------
  543. Game_Player.prototype.calculateStaminaRegen = function()
  544. {
  545.     if (!Silv.DashStamina.UseCustomRegenFormula) { return this.calculateStaminaRegenEval(); }
  546.     else { return this.calculateStaminaRegenNoEval();}
  547. };
  548.    
  549. Game_Player.prototype.calculateStaminaRegenEval = function()
  550. {
  551.     var base = Silv.DashStamina.StaminaRecoveryRate;
  552.     var x = this.staminaRecoveryTimeCnt;
  553.     return eval(Silv.DashStamina.RegenFormula);
  554. };
  555.  
  556. Game_Player.prototype.calculateStaminaRegenNoEval = function()
  557. {
  558.     return Silv.DashStamina.StaminaRecoveryRate + Math.sqrt(this.staminaRecoveryTimeCnt / 50);
  559. };
  560.  
  561. //------------------------------------------------------------------------------------------------------------------------------------
  562. // #Initialize
  563. //------------------------------------------------------------------------------------------------------------------------------------
  564. Silv.AddAlias('dashStamina_Game_Player_initialize', Game_Player.prototype.initialize);
  565. Game_Player.prototype.initialize = function()
  566. {
  567.     Silv.Alias.dashStamina_Game_Player_initialize.apply(this, arguments);
  568.  
  569.     this.dashStamina = this.dashStaminaMax = Silv.DashStamina.StaminaMax;
  570.     this.staminaRecoveryDelayCnt = 0; // counter for when to start recovering stamina
  571.     this.staminaRecoveryTimeCnt = 0; // counter for how long the player has been recovering stamina (in frames)
  572.     this.isRecoveringStamina = false;
  573.     this.dashStaminaPerc = 1.0;
  574.     this.hideStaminaWindowDelayCnt = 0;
  575.     this.requiresThresholdAmount = false;
  576.     this.wasDashing = false;
  577. };
  578.  
  579. //------------------------------------------------------------------------------------------------------------------------------------
  580. // Is the player currently allowed to dash?
  581. //------------------------------------------------------------------------------------------------------------------------------------
  582. Game_Player.prototype.dashingAllowed = function()
  583. {
  584.     if (this.dashStamina === 0 ||
  585.        (!this.wasDashing && this.requiresThresholdAmount)) // Do not allow to dash if the player was not dashing the previous frame AND if the threshold was passed.
  586.     {
  587.         return false;
  588.     }
  589.    
  590.     return true;
  591. };
  592.  
  593. //------------------------------------------------------------------------------------------------------------------------------------
  594. // Alias for disallowing dashing
  595. //------------------------------------------------------------------------------------------------------------------------------------
  596. Silv.AddAlias('dashStamina_Game_Player_updateDashing', Game_Player.prototype.updateDashing);
  597. Game_Player.prototype.updateDashing = function()
  598. {
  599.     Silv.Alias.dashStamina_Game_Player_updateDashing.apply(this, arguments);
  600.    
  601.     if (Silv.DashStamina.DashingDisabled)
  602.     {
  603.         this._dashing = false;
  604.         return;
  605.     }
  606.    
  607.     if (!this.dashingAllowed()) { this._dashing = false; }
  608.     this.wasDashing = this._dashing;
  609. };
  610.  
  611. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  612. // Game Screen
  613. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  614. // Do not just show the minimap on top of a faded-out screen.
  615. Silv.AddAlias('dashStamina_Game_Screen_updateFadeOut', Game_Screen.prototype.updateFadeOut);
  616. Game_Screen.prototype.updateFadeOut = function()
  617. {
  618.     Silv.Alias.dashStamina_Game_Screen_updateFadeOut.apply(this, arguments);
  619.    
  620.     if (this._brightness < 255) // (this._fadeOutDuration > 0)
  621.     {
  622.         if (Silv.DashStamina.Window !== null) { Silv.DashStamina.Window.visible = false; }
  623.         Silv.DashStamina.ScreenIsFading = true;
  624.     }
  625.     else
  626.     {
  627.         Silv.DashStamina.ScreenIsFading = false;
  628.     }
  629. };
  630.  
  631. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  632. // Scene Base
  633. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  634. Silv.AddAlias('dashStamina_Scene_Base_startFadeIn', Scene_Base.prototype.startFadeIn);
  635. Scene_Base.prototype.startFadeIn = function()
  636. {
  637.     Silv.Alias.dashStamina_Scene_Base_startFadeIn.apply(this, arguments);
  638.     if (Silv.DashStamina.Window !== null) { Silv.DashStamina.Window.visible = false; }
  639. };
  640.  
  641. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  642. // Scene Map
  643. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  644. // Hook into the main loop
  645. Silv.AddAlias('dashStamina_Scene_Map_updateMain', Scene_Map.prototype.updateMain);
  646. Scene_Map.prototype.updateMain = function()
  647. {
  648.     Silv.Alias.dashStamina_Scene_Map_updateMain.apply(this, arguments);
  649.     $gamePlayer.updateStamina();
  650.     if (Silv.DashStamina.ShowWindow) { Silv.DashStamina.Window.update(); }
  651. };
  652.  
  653. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  654. // Game Map
  655. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  656. // Disable Dashing?
  657. Silv.AddAlias('dashStamina_Game_Map_setup', Game_Map.prototype.setup);
  658. Game_Map.prototype.setup = function(mapId)
  659. {
  660.     Silv.Alias.dashStamina_Game_Map_setup.apply(this, arguments);
  661.     Silv.DashStamina.DashingDisabled = ('disable_dashing' in $dataMap.meta);
  662. };
  663.  
  664. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  665. // Stamina Regen/Deplete #Items
  666. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  667. Silv.AddAlias('dashStamina_Scene_ItemBase_useItem', Scene_ItemBase.prototype.useItem);
  668. Scene_ItemBase.prototype.useItem = function()
  669. {
  670.     var item = this.item();
  671.     if ('dash_stamina' in item.meta)
  672.     {
  673.         var args = item.meta.dash_stamina.split(' ');
  674.         switch(args[0].toLowerCase())
  675.         {
  676.             case 'add':
  677.                 if (args.length < 2) { throw 'Item-add-command is missing the value-argument.'; }
  678.                 $gamePlayer.dashStamina += parseInt(args[1]);
  679.                 if ($gamePlayer.dashStamina > $gamePlayer.dashStaminaMax) { $gamePlayer.dashStamina = $gamePlayer.dashStaminaMax; }
  680.                 else if ($gamePlayer.dashStamina < 0) { $gamePlayer.dashStamina = 0; }
  681.                 break;
  682.             case 'refill':
  683.                 $gamePlayer.dashStamina = $gamePlayer.dashStaminaMax;
  684.                 break;
  685.             case 'deplete':
  686.                 $gamePlayer.dashStamina = 0;
  687.                 break;
  688.             case 'increasemax':
  689.                 if (args.length < 2) { throw 'Item-increasemax-command is missing the value-argument.'; }
  690.                 $gamePlayer.dashStaminaMax += parseInt(args[1]);
  691.                 if ($gamePlayer.dashStaminaMax < 1 ) { $gamePlayer.dashStaminaMax = 1; }
  692.                 break;
  693.             default:
  694.                 throw 'Unknown dash_stamina itemnotetag: ' + item.meta;
  695.         }
  696.        
  697.     }
  698.    
  699.     Silv.Alias.dashStamina_Scene_ItemBase_useItem.apply(this, arguments);
  700. };
  701.  
  702. Silv.AddAlias('dashStamina_Game_Action_testApply', Game_Action.prototype.testApply);
  703. Game_Action.prototype.testApply = function(target)
  704. {
  705.     if ('dash_stamina' in this.item().meta)
  706.     {
  707.         return true;
  708.     }
  709.     else
  710.     {
  711.         return Silv.Alias.dashStamina_Game_Action_testApply.apply(this, arguments);
  712.     }
  713. };
  714. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  715. // Stamina #Window
  716. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  717. function Window_DashStamina() { this.initialize.apply(this, arguments); }
  718. Window_DashStamina.prototype = Object.create(Window_Base.prototype);
  719. Window_DashStamina.prototype.constructor = Window_DashStamina;
  720. // Font Size
  721. Window_DashStamina.prototype.standardFontSize = function() { return Silv.DashStamina.FontSize; };
  722.  
  723. // #Initialize
  724. Window_DashStamina.prototype.initialize = function(x, y, width, height)
  725. {
  726.     Window_Base.prototype.initialize.call(this, x, y, width, height);
  727.     this._helpWindow = null;
  728.     this._handlers = {};
  729.     this._touching = false;
  730.     this.deactivate();
  731.    
  732.     this.opacity = Silv.DashStamina.WindowOpacity;
  733.     this.slideDirection = {x: 0, y: 0 };
  734.     this.originalWinLoc = {x: 0, y: 0 };
  735.     this.sliding = 'none';
  736.     this.isFullySlidedOut = false;
  737.     this.isFullySlidedIn = true;
  738.     this.originalWinLoc.x = x; // for some reason "this." is not allowed here
  739.     this.originalWinLoc.y = y; // for some reason "this." is not allowed here
  740.    
  741.     if (Silv.DashStamina.WindowText != ':none')
  742.     {
  743.         this.windowText = Silv.DashStamina.WindowText;
  744.         this.windowTextWidth = this.contents.measureTextWidth(this.windowText);
  745.         this.windowTextGaugeSpacingX = Silv.DashStamina.WindowTextGaugeSpacingX;
  746.     }
  747.     else
  748.     {
  749.         this.windowText = null;
  750.         this.windowTextWidth = 0;
  751.         this.windowTextGaugeSpacingX = Silv.DashStamina.WindowTextGaugeSpacingX;
  752.     }
  753.    
  754.     this.update();
  755. };
  756.  
  757. //------------------------------------------------------------------------------------------------------------------------------------
  758. // Stamina Window Update
  759. //------------------------------------------------------------------------------------------------------------------------------------
  760. Window_DashStamina.prototype.update = function()
  761. {
  762.     if (Silv.DashStamina.ScreenIsFading)
  763.     {
  764.         this.visible = false;
  765.     }
  766.     else
  767.     {
  768.         Window_Base.prototype.update.call(this);
  769.         this.drawStaminaWindow(0, 0, Silv.DashStamina.StaminaGaugeRectangle.width);
  770.         this.updateSliding();
  771.     }
  772. };
  773.  
  774. //------------------------------------------------------------------------------------------------------------------------------------
  775. // Stamina Window #Drawing
  776. //------------------------------------------------------------------------------------------------------------------------------------
  777. Window_DashStamina.prototype.drawStaminaGauge = function(x, y, width, height, rate)
  778. {
  779.     var fillW = Math.floor(width * rate);
  780.     var gaugeY = y + this.lineHeight() - 8;
  781.     this.contents.fillRect(x, gaugeY, width, height, this.gaugeBackColor());
  782.     this.contents.fillRect(x, gaugeY, fillW, height, this.CalculateBarColour());
  783. };
  784.  
  785. Window_DashStamina.prototype.drawStaminaWindow = function(x, y, width)
  786. {
  787.     this.contents.clear();
  788.    
  789.     // Draw text before the gauge
  790.     if (this.windowText !== null)
  791.     {
  792.         this.contents.drawText(this.windowText, 0, Silv.DashStamina.WindowTextOffsetY, this.windowTextWidth, 0);
  793.     }
  794.  
  795.     // Draw gauge
  796.     this.drawStaminaGauge(Silv.DashStamina.StaminaGaugeRectangle.x + this.windowTextWidth + this.windowTextGaugeSpacingX, Silv.DashStamina.StaminaGaugeRectangle.y, Silv.DashStamina.StaminaGaugeRectangle.width, Silv.DashStamina.StaminaGaugeRectangle.height, $gamePlayer.dashStaminaPerc);
  797.     // Draw text on top of the gauge
  798.     var text;
  799.     switch(Silv.DashStamina.DrawStaminaValue) // allowed values: absolute/percentage/both/none
  800.     {
  801.         case 'absolute':
  802.             text = parseInt($gamePlayer.dashStamina) + '/' + parseInt($gamePlayer.dashStaminaMax);
  803.             break;
  804.         case 'percentage':
  805.             text = Math.round($gamePlayer.dashStaminaPerc * 100) + '%';
  806.             break;
  807.         case 'both':
  808.             text = parseInt($gamePlayer.dashStamina) + '/' + parseInt($gamePlayer.dashStaminaMax) + ' (' + Math.round($gamePlayer.dashStaminaPerc * 100) + '%)';
  809.             break;
  810.         case 'none':
  811.             return;
  812.         default:
  813.             throw 'ERROR: drawStaminaWindow missing case-statement or incorrect Silv.DashStamina.DrawStaminaValue value. Value: ' + Silv.DashStamina.DrawStaminaValue;
  814.     }
  815.    
  816.     this.resetTextColor();
  817.     this.drawText(text, x + this.windowTextWidth + this.windowTextGaugeSpacingX, y + 1, width, 'center');
  818. };
  819.  
  820. // Calculate what colour the gauge should be between the two colors depending on the percentage value of the current-stamina value.
  821. Window_DashStamina.prototype.CalculateBarColour = function()
  822. {
  823.     var c1 = hexToRgb(Silv.DashStamina.StaminaGaugeColor1);
  824.     var c2 = hexToRgb(Silv.DashStamina.StaminaGaugeColor2);
  825.        
  826.     var ratio = $gamePlayer.dashStaminaPerc;
  827.     var hex = function(x) {
  828.         x = x.toString(16);
  829.         return (x.length === 1) ? '0' + x : x;
  830.     };
  831.  
  832.     var r = Math.ceil(c1.r * ratio + c2.r * (1-ratio));
  833.     var g = Math.ceil(c1.g * ratio + c2.g * (1-ratio));
  834.     var b = Math.ceil(c1.b * ratio + c2.b * (1-ratio));
  835.  
  836.     var middle = '#' + hex(r) + hex(g) + hex(b);
  837.     return middle;
  838. };
  839.  
  840. //------------------------------------------------------------------------------------------------------------------------------------
  841. // Stamina Window #Show & #Hide
  842. //------------------------------------------------------------------------------------------------------------------------------------
  843. Window_DashStamina.prototype.showMe = function()
  844. {
  845.     //if (this.visible) { return; };
  846.     if (this.isFullySlidedIn)
  847.     {
  848.         this.visible = true;
  849.         return;
  850.     }
  851.    
  852.     if (Silv.DashStamina.WindowSlideOutDir === 'noslide' || this.visible === false) { this.visible = true; }
  853.     else
  854.     {
  855.         this.sliding = 'in';
  856.         switch (Silv.DashStamina.WindowSlideOutDir)
  857.         {
  858.             case 'top':
  859.                 this.slideDirection.x = 0;
  860.                 this.slideDirection.y = 1;
  861.                 break;
  862.             case 'left':
  863.                 this.slideDirection.x = 1;
  864.                 this.slideDirection.y = 0;
  865.                 break;
  866.             case 'right':
  867.                 this.slideDirection.x = -1;
  868.                 this.slideDirection.y = 0;
  869.                 break;
  870.             case 'bottom':
  871.                 this.slideDirection.x = 0;
  872.                 this.slideDirection.y = -1;
  873.                 break;
  874.             default:
  875.                 throw 'Window_DashStamina.prototype.HideMe: Unknown switch value: ' + Silv.DashStamina.WindowSlideOutDir;
  876.         }
  877.     }
  878. };
  879.  
  880. Window_DashStamina.prototype.hideMe = function()
  881. {
  882.     if (!this.visible || this.isFullySlidedOut) { return; }
  883.    
  884.     if (Silv.DashStamina.WindowSlideOutDir === 'noslide') { this.visible = false; }
  885.     else
  886.     {
  887.         this.sliding = 'out';
  888.         switch (Silv.DashStamina.WindowSlideOutDir)
  889.         {
  890.             case 'top':
  891.                 this.slideDirection.x = 0;
  892.                 this.slideDirection.y = -1;
  893.                 break;
  894.             case 'left':
  895.                 this.slideDirection.x = -1;
  896.                 this.slideDirection.y = 0;
  897.                 break;
  898.             case 'right':
  899.                 this.slideDirection.x = 1;
  900.                 this.slideDirection.y = 0;
  901.                 break;
  902.             case 'bottom':
  903.                 this.slideDirection.x = 0;
  904.                 this.slideDirection.y = 1;
  905.                 break;
  906.             default:
  907.                 throw 'Window_DashStamina.prototype.HideMe: Unknown switch value: ' + Silv.DashStamina.WindowSlideOutDir;
  908.         }
  909.     }
  910. };
  911.  
  912. //------------------------------------------------------------------------------------------------------------------------------------
  913. // Stamina Window #Sliding of the Window
  914. //------------------------------------------------------------------------------------------------------------------------------------
  915. Window_DashStamina.prototype.handleSlidingEnd = function()
  916. {
  917.     if (this.sliding === 'in')
  918.     {
  919.         // Stop sliding in
  920.         if (this.slideDirection.x  === 1 && this.x > this.originalWinLoc.x ||
  921.             this.slideDirection.x  === -1 && this.x < this.originalWinLoc.x ||
  922.             this.slideDirection.y  === 1 && this.y > this.originalWinLoc.y ||
  923.             this.slideDirection.y  === -1 && this.y < this.originalWinLoc.y)
  924.         {
  925.             this.sliding = 'none';
  926.             this.isFullySlidedIn = true;
  927.         }
  928.     }
  929.     else
  930.     {
  931.         // Stop sliding out
  932.         if (this.x < -this.width || this.x > Graphics._width + this.width ||
  933.             this.y < -this.height || this.x > Graphics._height + this.height)
  934.         {
  935.             this.sliding = 'none';
  936.             this.isFullySlidedOut = true;
  937.         }
  938.     }
  939. };
  940.  
  941. Window_DashStamina.prototype.updateSliding = function()
  942. {
  943.     if (this.sliding === 'none') { return; }
  944.     this.x += this.slideDirection.x * Silv.DashStamina.WindowSlideOutSpeed;
  945.     this.y += this.slideDirection.y * Silv.DashStamina.WindowSlideOutSpeed;
  946.    
  947.     this.isFullySlidedOut = false;
  948.     this.isFullySlidedIn = false;
  949.    
  950.     this.handleSlidingEnd();
  951. };
  952.  
  953. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  954. // #Create Stamina Window
  955. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  956. Scene_Map.prototype.createDashWindow = function()
  957. {
  958.     // Dispose the old window, if any
  959.     if (Silv.DashStamina.Window !== null) { this.removeWindow(Silv.DashStamina.Window); }
  960.    
  961.     // Does the map consume stamina?
  962.     $gamePlayer.mapConsumeStamina = !('dstam_disable' in $dataMap.meta);
  963.     if(Silv.DashStamina.ShowWindow)
  964.     {
  965.         var x = 0;
  966.         if (Silv.DashStamina.WindowHorizontalAlignment === 'right') { x = Graphics.width - Silv.DashStamina.WindowWidth; }
  967.         var y = 0;
  968.         if (Silv.DashStamina.WindowVerticalAlignment === 'bottom') { y = Graphics.height - Silv.DashStamina.WindowHeight; }
  969.        
  970.         Silv.DashStamina.Window = new Window_DashStamina(x + Silv.DashStamina.Window_X, y + Silv.DashStamina.Window_Y, Silv.DashStamina.WindowWidth, Silv.DashStamina.WindowHeight);
  971.        
  972.         this.addChild(Silv.DashStamina.Window, Silv.DashStamina.Window_Z);
  973.         if (Silv.DashStamina.AutoHideStaminaWindow) { Silv.DashStamina.Window.visible = false; }
  974.     }
  975. };
  976.  
  977. // Omg why does RPG Maker not have this method by default...
  978. Scene_Base.prototype.removeWindow = function(window)
  979. {
  980.     var index = this.children.indexOf(window);
  981.     if (index > -1) { this.children.splice(index, 1); }
  982. };
  983.  
  984. Silv.AddAlias('dashStamina_Scene_Map_createDisplayObjects', Scene_Map.prototype.createDisplayObjects);
  985. Scene_Map.prototype.createDisplayObjects = function()
  986. {
  987.     Silv.Alias.dashStamina_Scene_Map_createDisplayObjects.apply(this, arguments);
  988.     this.createDashWindow();
  989. };
  990.  
  991. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  992. // Saving & Loading
  993. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  994. Silv.AddAlias('dashStamina_DataManager_makeSaveContents', DataManager.makeSaveContents);
  995. DataManager.makeSaveContents = function()
  996. {
  997.     contents = Silv.Alias.dashStamina_DataManager_makeSaveContents.apply(this, arguments);
  998.     contents.dashStamina = $gamePlayer.dashStamina;
  999.     contents.dashStaminaMax = $gamePlayer.dashStaminaMax;
  1000.     return contents;
  1001. };
  1002.  
  1003. Silv.AddAlias('dashStamina_DataManager_extractSaveContents', DataManager.extractSaveContents);
  1004. DataManager.extractSaveContents = function(contents)
  1005. {
  1006.     Silv.Alias.dashStamina_DataManager_extractSaveContents.apply(this, arguments);
  1007.     $gamePlayer.dashStamina = contents.dashStamina;
  1008.     $gamePlayer.dashStaminaMax = contents.dashStaminaMax;
  1009. };
  1010.  
  1011. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1012. // Plugin Command
  1013. // Note: The items are separated by spaces. The command is the first word and any following words are args. args is an array.
  1014. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1015. Silv.AddAlias('dashStamina_Game_Interpreter_pluginCommand', Game_Interpreter.prototype.pluginCommand);
  1016. Game_Interpreter.prototype.pluginCommand = function(command, args)
  1017. {
  1018.     Silv.Alias.dashStamina_Game_Interpreter_pluginCommand.apply(this, arguments);
  1019.     if (command.toLowerCase() === Silv.DashStamina.PluginCmdId) { Silv.DashStamina.PluginCommand(command, args); }
  1020. };
  1021.  
  1022. // Concatenate the 'arguments' starting at a specific index because they are all part of an eval.
  1023. Silv.DashStamina.concatArgs = function(args, startIdx)
  1024. {
  1025.     var evalstr = '';
  1026.     for (var argIdx = startIdx; argIdx < args.length; argIdx++)
  1027.     {
  1028.         evalstr += args[argIdx] + ' ';
  1029.     }
  1030.     return evalstr;
  1031. };
  1032.  
  1033. // newValue must be between 0 - 100.
  1034. Silv.DashStamina.assignNewStaminaValue = function(newValue)
  1035. {
  1036.     var perc = Math.max(0, Math.min(100, newValue)); // clamp value between 0-100
  1037.     $gamePlayer.dashStamina = $gamePlayer.dashStaminaMax * (perc / 100.0);
  1038. };
  1039.  
  1040. Silv.DashStamina.clampMaxStamina = function()
  1041. {
  1042.     if ($gamePlayer.dashStamina > $gamePlayer.dashStaminaMax) { $gamePlayer.dashStamina = $gamePlayer.dashStaminaMax; }
  1043. };
  1044.  
  1045. Silv.DashStamina.PluginCommand = function(cmd, args)
  1046. {
  1047.     switch(args[0].toLowerCase())
  1048.     {
  1049.         case 'refill':
  1050.             $gamePlayer.dashStamina = $gamePlayer.dashStaminaMax;
  1051.             break;
  1052.         case 'deplete':
  1053.             $gamePlayer.dashStamina = 0;
  1054.             break;
  1055.         case 'set':
  1056.             Silv.DashStamina.assignNewStaminaValue(parseInt(args[1]));
  1057.         break;
  1058.         case 'setvar':
  1059.             Silv.DashStamina.assignNewStaminaValue(parseInt($gameVariables.value(args[1])));
  1060.         break;
  1061.         case 'seteval':
  1062.             var perc = parseInt(eval(Silv.DashStamina.concatArgs(args, 1)));
  1063.             if (isNaN(perc)) { throw new Error('Plugin command: "Stamina SetEval" evaled to a NaN value: ' + perc + '.'); }
  1064.             Silv.DashStamina.assignNewStaminaValue(perc);
  1065.         break;
  1066.         case 'showwindow':
  1067.             if (Silv.DashStamina.Window !== null)
  1068.             {
  1069.                 $gamePlayer.hideStaminaWindowDelayCnt = 0;
  1070.                 Silv.DashStamina.Window.visible = true;
  1071.             }
  1072.             break;
  1073.         case 'refillhide':
  1074.             $gamePlayer.dashStamina = $gamePlayer.dashStaminaMax;
  1075.             // NO break-statement here! We want to hide the window as well!
  1076.         case 'hidewindow':
  1077.             if (Silv.DashStamina.Window !== null)
  1078.             {
  1079.                 $gamePlayer.hideStaminaWindowDelayCnt = Silv.DashStamina.HideStaminaWindowDelay;
  1080.                 Silv.DashStamina.Window.visible = false;
  1081.             }
  1082.             break;
  1083.         case 'setmax':
  1084.             $gamePlayer.dashStaminaMax = Math.max(1, parseInt(args[1]));
  1085.             Silv.DashStamina.clampMaxStamina();
  1086.             break;
  1087.         case 'setmaxeval':
  1088.             $gamePlayer.dashStaminaMax = Math.max(1, parseInt(eval(Silv.DashStamina.concatArgs(args, 1))));
  1089.             Silv.DashStamina.clampMaxStamina();
  1090.             break;
  1091.         case 'setmaxvar':
  1092.             $gamePlayer.dashStaminaMax = Math.max(1, parseInt($gameVariables.value(args[1])));
  1093.             Silv.DashStamina.clampMaxStamina();
  1094.             break;
  1095.         case 'increasemax':
  1096.             $gamePlayer.dashStaminaMax += parseInt(args[1]);
  1097.             if ($gamePlayer.dashStaminaMax < 1) { $gamePlayer.dashStaminaMax = 1; }
  1098.             if ($gamePlayer.dashStamina > $gamePlayer.dashStaminaMax) { $gamePlayer.dashStamina = $gamePlayer.dashStaminaMax; }
  1099.             break;
  1100.         case 'enabledashing':
  1101.             switch(args[1].toLowerCase())
  1102.             {
  1103.                 case 'true':
  1104.                     Silv.DashStamina.DashingDisabled = false;
  1105.                     break;
  1106.                 case 'false':
  1107.                     Silv.DashStamina.DashingDisabled = true;
  1108.                     break;
  1109.                 case 'toggle':
  1110.                     Silv.DashStamina.DashingDisabled = !Silv.DashStamina.DashingDisabled;
  1111.                     break;
  1112.                 default:
  1113.                     throw new Error('"Stamina EnableDashing" received an unknown argument: ' + args[1] + '. Expected: True, False or Toggle.');
  1114.             }
  1115.             break;
  1116.         default:
  1117.             throw 'Stamina PluginCommand invalid command: ' + args[0];
  1118.     }
  1119. };
  1120.  
  1121. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1122. })();
  1123. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  1124. // This is the end of this awesome script!
  1125. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement