Advertisement
Guest User

Untitled

a guest
Dec 22nd, 2015
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================
  2. // SilvEventChance.js
  3. // Version: 1.00
  4. //=============================================================================
  5. /*:
  6.  * @plugindesc v1.00 Randomizes events through disabling specific marked events. <SilvEventChance>
  7.  * @author Silver
  8.  *
  9.  * @param -- General --
  10.  *
  11.  * @param Default Regular Section Chance
  12.  * @desc The chance for NOT disabling events for regular sections when no chance-notetag was found. Value must be between 0.0 and 1.0 (inclusive).
  13.  * @default 0.5
  14.  *
  15.  * @help
  16.  * Note that the exclusive-section is never disabled for the map if there is only 1 exclusive section for that map.
  17.  *
  18.  * Events disabled through self-switches and exclusive-events are saved between maps and saving&loading the game.
  19.  * Regular sections are randomized each time the player visits the map again or loads a savegame.
  20.  *
  21.  *--------------------------------------
  22.  * Event Notetags
  23.  *--------------------------------------
  24.  * <evc_chance:value> // value 0-100 or 0.0 - 1.0. both are valid and the same
  25.  * <evc_section:value>
  26.  * <evc_excl_section:value>
  27.  * <evc_disable_mode:value1, value2, value3> // value1: switch/erase, value2: A, B, C or D, value3: ON/OFF
  28.  *
  29.  * Examples:
  30.  * <evc_chance:50> // 50% chance to be disabled. If evc_section is included then this is the chance for the section instead. Does not apply to exclusive sections.
  31.  * <evc_section:towns> // Only disable when the town-section is disabled
  32.  * <evc_excl_section:towns> // only 1 exclusive section can be enabled at any time, all other events tagged with a different section will be disabled
  33.  * <evc_disable_mode:switch A> // switch can be either "switch A/B/C/D" or "erase" (default: erase)
  34.  * <evc_disable_mode:switch A ON> // The last value can be ON or OFF. If the 3rd value is omitted, it will default to "ON".
  35.  *
  36.  * Examples for an exclusive-section:
  37.  * <evc_excl_section:towns><evc_disable_mode:switch A>
  38.  * <evc_excl_section:towns><evc_disable_mode:switch B OFF>
  39.  *
  40.  * Examples for a regular section:
  41.  * <evc_section:towns1><evc_chance:90>
  42.  * <evc_section:towns3>
  43.  * <evc_section:towns4><evc_disable_mode:switch A ON>
  44.  *
  45.  * Example for chancing a single event:
  46.  * <evc_chance:75>
  47.  *--------------------------------------
  48.  * Version History:
  49.  *--------------------------------------
  50.  * v.1.00 (22 December 2015)
  51.  * - First Release
  52.  *
  53.  */
  54.  // Imported
  55. var Imported = Imported || {};
  56. Imported.SILV_EventChance = 1.00;
  57.  
  58. // #Parameters
  59. var Silv = Silv || {};
  60. Silv.EvChance = Silv.EvChance || {};
  61. Silv.Parameters = $plugins.filter(function(p) { return p.description.contains('<SilvEventChance>'); })[0].parameters;
  62. // General
  63. Silv.EvChance.DefaultRegularSectionChance = parseFloat(Silv.Parameters['Default Regular Section Chance']);
  64.  
  65. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  66. // Utilities
  67. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  68. // chance parameter: between 0.0 and 1.0
  69. Silv.EvChance.Chance = function(chance)
  70. {
  71.     return (Math.random() < chance);
  72. };
  73.  
  74. Silv.EvChance.DisableEv = function(mapId, event, storedEvent)
  75. {
  76.     if (storedEvent.disableMode === 'erase')
  77.     {
  78.         event.erase();
  79.     }
  80.     else
  81.     {
  82.         $gameSelfSwitches.setValue([mapId, storedEvent.id, storedEvent.disableModeLetter], storedEvent.disableModeValue);
  83.     }
  84. };
  85.  
  86. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  87. // Your Code
  88. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  89.  
  90. Game_Map.prototype.initEVCSections = function()
  91. {
  92.     this.sections                = {};
  93.     this.sections.regular        = {};
  94.     this.sections.regularArray   = [];
  95.     this.sections.exclusive      = {};
  96.     this.sections.exclusiveArray = [];
  97. };
  98.  
  99. var alias_silv_evChance_Game_Map_setup = Game_Map.prototype.setup;
  100. Game_Map.prototype.setup = function(mapId)
  101. {
  102.     // Call alias
  103.     alias_silv_evChance_Game_Map_setup.apply(this, arguments);
  104.    
  105.     // Init custom-map-variables
  106.     this.initEVCSections();
  107.    
  108.     // Loop through all map-events
  109.     var events = this.events();
  110.     for (var evIdx=0; evIdx<events.length; evIdx++)
  111.     {
  112.         var event = events[evIdx];
  113.         var storedEvent = { id:event._eventId, disableMode:'erase' };
  114.        
  115.         //------------------------------------------------------------------------------------------------------------------------------------
  116.         // Disable mode
  117.         //------------------------------------------------------------------------------------------------------------------------------------
  118.         if ('evc_disable_mode' in  $dataMap.events[storedEvent.id].meta)
  119.         {
  120.             var disableModeData = $dataMap.events[storedEvent.id].meta.evc_disable_mode.split(' ');
  121.  
  122.             // Sanity Check
  123.             if (disableModeData.length < 2) { throw new Error('Disablemode expects 2 values, the "switch" and the variable to switch (A, B, C or D). Exmaple: "switch A". Received value: ' + $dataMap.events[storedEvent.id].meta.evc_disable_mode); }
  124.  
  125.             // Check for switch-disable-mode
  126.             if (disableModeData[0].toLowerCase() === 'switch')
  127.             {
  128.                 storedEvent.disableMode = 'switch';
  129.                 storedEvent.disableModeLetter = disableModeData[1].toUpperCase();
  130.                
  131.                 if (disableModeData.length >= 3)
  132.                 {
  133.                     storedEvent.disableModeValue = (disableModeData[2].toUpperCase() === 'ON');
  134.                 }
  135.                 else
  136.                 {
  137.                     storedEvent.disableModeValue = true;
  138.                 }
  139.             }
  140.         }
  141.         //------------------------------------------------------------------------------------------------------------------------------------
  142.         // Process Notetags
  143.         //------------------------------------------------------------------------------------------------------------------------------------
  144.         if ('evc_excl_section' in  $dataMap.events[storedEvent.id].meta) // Exclusive section
  145.         {
  146.             var sectionName = $dataMap.events[storedEvent.id].meta.evc_excl_section;
  147.             if (typeof this.sections.exclusive[sectionName] === 'undefined')
  148.             {
  149.                 this.sections.exclusive[sectionName] = { enabled:false, events:[storedEvent] };
  150.                 this.sections.exclusiveArray.push(this.sections.exclusive[sectionName]);
  151.             }
  152.             else
  153.             {
  154.                 this.sections.exclusive[sectionName].events.push(storedEvent);
  155.             }
  156.         }
  157.         else if ('evc_section' in $dataMap.events[storedEvent.id].meta) // Regular section
  158.         {
  159.             var sectionName = $dataMap.events[storedEvent.id].meta.evc_section;
  160.             if (typeof this.sections.regular[sectionName] === 'undefined')
  161.             {
  162.                 this.sections.regular[sectionName] = { enabled:false, chance:Silv.EvChance.DefaultRegularSectionChance, events:[storedEvent] };
  163.                 this.sections.regularArray.push(this.sections.regular[sectionName]);
  164.             }
  165.             else
  166.             {
  167.                 this.sections.regular[sectionName].events.push(storedEvent);
  168.             }
  169.            
  170.             // Section Chance
  171.             if ('evc_chance' in $dataMap.events[storedEvent.id].meta)
  172.             {
  173.                 var chance = parseFloat( $dataMap.events[storedEvent.id].meta.evc_chance);
  174.                 this.sections.regular[sectionName].chance = ((chance <= 1) ? chance : chance / 100.0); // Only add a chance between 0.0 and 1.0. So convert values 1.01 - 100.0 to fit this range.
  175.             }
  176.         }
  177.         else if ('evc_chance' in $dataMap.events[storedEvent.id].meta) // Chance (no section)
  178.         {
  179.             var chance = parseFloat( $dataMap.events[storedEvent.id].meta.evc_chance);
  180.             var chance = ((chance <= 1) ? chance : chance / 100.0); // Only add a chance between 0.0 and 1.0. So convert values 1.01 - 100.0 to fit this range.
  181.             if (Silv.EvChance.Chance(chance)) { Silv.EvChance.DisableEv(mapId, event, storedEvent); }
  182.         }
  183.     };
  184.    
  185.     //------------------------------------------------------------------------------------------------------------------------------------
  186.     // Enable a random exclusive section
  187.     //------------------------------------------------------------------------------------------------------------------------------------
  188.     if (this.sections.exclusiveArray.length > 0)
  189.     {
  190.         var randomExclusiveIdx = Math.floor(Math.random() * this.sections.exclusiveArray.length);
  191.         this.sections.exclusiveArray[randomExclusiveIdx].enabled = true;
  192.        
  193.         // Now process the exclusive sections
  194.         for (var sectionIdx=0; sectionIdx<this.sections.exclusiveArray.length; sectionIdx++)
  195.         {
  196.             var section = this.sections.exclusiveArray[sectionIdx];
  197.             if (!section.enabled)
  198.             {
  199.                 for (var sectionEventIdx=0; sectionEventIdx<section.events.length; sectionEventIdx++)
  200.                 {
  201.                     var storedEvent = section.events[sectionEventIdx];
  202.                     Silv.EvChance.DisableEv(mapId, this.event(storedEvent.id), storedEvent);
  203.                 }
  204.             }
  205.         }
  206.     }
  207.    
  208.     //------------------------------------------------------------------------------------------------------------------------------------
  209.     // Now process the regular sections
  210.     //------------------------------------------------------------------------------------------------------------------------------------
  211.     for (var sectionIdx=0; sectionIdx<this.sections.regularArray.length; sectionIdx++)
  212.     {
  213.         var section = this.sections.regularArray[sectionIdx];
  214.         section.enabled = Silv.EvChance.Chance(section.chance);
  215.         if (!section.enabled)
  216.         {
  217.             for (var sectionEventIdx=0; sectionEventIdx<section.events.length; sectionEventIdx++)
  218.             {
  219.                 var storedEvent = section.events[sectionEventIdx];
  220.                 Silv.EvChance.DisableEv(mapId, this.event(storedEvent.id), storedEvent);
  221.             }
  222.         }
  223.     }
  224.     //------------------------------------------------------------------------------------------------------------------------------------
  225.     this.refreshTileEvents();
  226. };
  227. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  228. // This is the end of this awesome script!
  229. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement