Advertisement
Guest User

Untitled

a guest
Dec 25th, 2015
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================
  2. // SilvEventChance.js
  3. // Version: 1.01
  4. //=============================================================================
  5. /*:
  6.  * @plugindesc v1.01 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 3 exclusive-sections. Note that EITHER "towns 1" or "towns 2" or "towns C" are kept, the other 2 sections (and all of their events) are erased/switched.
  37.  * <evc_excl_section:towns 1><evc_disable_mode:switch A>
  38.  * <evc_excl_section:towns 1><evc_disable_mode:switch B OFF>
  39.  * <evc_excl_section:towns 2>
  40.  * <evc_excl_section:towns C>
  41.  *
  42.  * Examples for a regular section:
  43.  * <evc_section:towns1><evc_chance:90>
  44.  * <evc_section:towns3>
  45.  * <evc_section:towns4><evc_disable_mode:switch A ON>
  46.  *
  47.  * Example for chancing a single event:
  48.  * <evc_chance:75>
  49.  *--------------------------------------
  50.  * Version History:
  51.  *--------------------------------------
  52.  * v1.01 (25 December 2015)
  53.  * - Now supports unlimited (instead of just 1) exclusive section per map.
  54.  * - Applied my new coding standards.
  55.  *
  56.  * v.1.00 (22 December 2015)
  57.  * - First Release.
  58.  *
  59.  */
  60.  // Imported
  61. var Imported = Imported || {};
  62. Imported.SILV_EventChance = 1.01;
  63.  
  64. // #Parameters
  65. var Silv = Silv || {};
  66. Silv.EvChance = Silv.EvChance || {};
  67. Silv.Parameters = $plugins.filter(function(p) { return p.description.contains('<SilvEventChance>'); })[0].parameters;
  68. // General
  69. Silv.EvChance.DefaultRegularSectionChance = parseFloat(Silv.Parameters['Default Regular Section Chance']);
  70.  
  71. // Alias
  72. Silv.Alias = Silv.Alias || {};
  73. if (!Silv.AddAlias)
  74. {
  75.     Silv.AddAlias = function(alias, original_method)
  76.     {
  77.         if (Silv.Alias[alias]) { throw new Error('Alias already exists: ' + alias); }
  78.         Silv.Alias[alias] = original_method;
  79.     };
  80. }
  81.  
  82. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  83. // Utilities
  84. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  85. // chance parameter: between 0.0 and 1.0
  86. Silv.EvChance.Chance = function(chance)
  87. {
  88.     return (Math.random() < chance);
  89. };
  90.  
  91. Silv.EvChance.DisableEv = function(mapId, event, storedEvent)
  92. {
  93.     if (storedEvent.disableMode === 'erase')
  94.     {
  95.         event.erase();
  96.     }
  97.     else
  98.     {
  99.         $gameSelfSwitches.setValue([mapId, storedEvent.id, storedEvent.disableModeLetter], storedEvent.disableModeValue);
  100.     }
  101. };
  102.  
  103. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  104. // Game Map
  105. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  106.  
  107. Game_Map.prototype.initEVCSections = function()
  108. {
  109.     this.sections                = {};
  110.     this.sections.regular        = {};
  111.     this.sections.regularArray   = [];
  112.     this.sections.exclusives     = {};
  113. };
  114.  
  115. Silv.AddAlias('evChance_Game_Map_setup', Game_Map.prototype.setup);
  116. Game_Map.prototype.setup = function(mapId)
  117. {
  118.     Silv.Alias.evChance_Game_Map_setup.apply(this, arguments);
  119.     this.setupEventChance(mapId);
  120. };
  121.  
  122. Game_Map.prototype.setupEventChance = function(mapId)
  123. {
  124.     // Init custom-map-variables
  125.     this.initEVCSections();
  126.    
  127.     // Loop through all map-events
  128.     var events = this.events();
  129.     for (var evIdx=0; evIdx<events.length; evIdx++)
  130.     {
  131.         var event = events[evIdx];
  132.         var storedEvent = { id:event._eventId, disableMode:'erase' };
  133.        
  134.         //------------------------------------------------------------------------------------------------------------------------------------
  135.         // Disable mode
  136.         //------------------------------------------------------------------------------------------------------------------------------------
  137.         if ('evc_disable_mode' in  $dataMap.events[storedEvent.id].meta)
  138.         {
  139.             var disableModeData = $dataMap.events[storedEvent.id].meta.evc_disable_mode.split(' ');
  140.  
  141.             // Sanity Check
  142.             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); }
  143.  
  144.             // Check for switch-disable-mode
  145.             if (disableModeData[0].toLowerCase() === 'switch')
  146.             {
  147.                 storedEvent.disableMode = 'switch';
  148.                 storedEvent.disableModeLetter = disableModeData[1].toUpperCase();
  149.                
  150.                 if (disableModeData.length >= 3)
  151.                 {
  152.                     storedEvent.disableModeValue = (disableModeData[2].toUpperCase() === 'ON');
  153.                 }
  154.                 else
  155.                 {
  156.                     storedEvent.disableModeValue = true;
  157.                 }
  158.             }
  159.         }
  160.         //------------------------------------------------------------------------------------------------------------------------------------
  161.         // Process Notetags
  162.         //------------------------------------------------------------------------------------------------------------------------------------
  163.         if ('evc_excl_section' in  $dataMap.events[storedEvent.id].meta) // Exclusive section
  164.         {
  165.             var tagSplitted = $dataMap.events[storedEvent.id].meta.evc_excl_section.toLowerCase().split(' ');
  166.             var exclusiveCategory;
  167.             var exclusiveSection;
  168.            
  169.             if (tagSplitted.length == 1)
  170.             {
  171.                 exclusiveCategory = 'default';
  172.                 exclusiveSection = tagSplitted[0];             
  173.             }
  174.             else
  175.             {
  176.                 exclusiveCategory = tagSplitted[0];
  177.                 exclusiveSection = tagSplitted[1];
  178.             }
  179.            
  180.             if (typeof this.sections.exclusives[exclusiveCategory] === 'undefined') { this.sections.exclusives[exclusiveCategory] = {}; }
  181.             if (typeof this.sections.exclusives[exclusiveCategory][exclusiveSection] === "undefined") { this.sections.exclusives[exclusiveCategory][exclusiveSection] = []; }
  182.             this.sections.exclusives[exclusiveCategory][exclusiveSection].push(storedEvent); // $gameMap.sections.exclusives.dungeon.a = [storedevent]
  183.         }
  184.         else if ('evc_section' in $dataMap.events[storedEvent.id].meta) // Regular section
  185.         {
  186.             var sectionName = $dataMap.events[storedEvent.id].meta.evc_section;
  187.             if (typeof this.sections.regular[sectionName] === 'undefined')
  188.             {
  189.                 this.sections.regular[sectionName] = { enabled:false, chance:Silv.EvChance.DefaultRegularSectionChance, events:[storedEvent] };
  190.                 this.sections.regularArray.push(this.sections.regular[sectionName]);
  191.             }
  192.             else
  193.             {
  194.                 this.sections.regular[sectionName].events.push(storedEvent);
  195.             }
  196.            
  197.             // Section Chance
  198.             if ('evc_chance' in $dataMap.events[storedEvent.id].meta)
  199.             {
  200.                 var chance = parseFloat( $dataMap.events[storedEvent.id].meta.evc_chance);
  201.                 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.
  202.             }
  203.         }
  204.         else if ('evc_chance' in $dataMap.events[storedEvent.id].meta) // Chance (no section)
  205.         {
  206.             var chance = parseFloat( $dataMap.events[storedEvent.id].meta.evc_chance);
  207.             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.
  208.             if (Silv.EvChance.Chance(chance)) { Silv.EvChance.DisableEv(mapId, event, storedEvent); }
  209.         }
  210.     };
  211.    
  212.     //------------------------------------------------------------------------------------------------------------------------------------
  213.     // Enable a random exclusive section in each category
  214.     //------------------------------------------------------------------------------------------------------------------------------------
  215.    
  216.     for (var cat in this.sections.exclusives)
  217.     {
  218.         if (this.sections.exclusives.hasOwnProperty(cat)) // cat content examples: dungeons, towns
  219.         {
  220.             var category = this.sections.exclusives[cat];
  221.             var sections = [];
  222.             for (var section in category)
  223.             {
  224.                 var section = category[section];
  225.                 sections.push(section);
  226.             }
  227.            
  228.             if (sections.length > 1) // do not erase exclusive sections that have only 1 entry
  229.             {
  230.                 var randomExclusiveIdx = Math.floor(Math.random() * sections.length);
  231.                 for (var sectionIdx=0; sectionIdx<sections.length; sectionIdx++)
  232.                 {
  233.                     var section = sections[sectionIdx];
  234.                     if (sectionIdx !== randomExclusiveIdx)
  235.                     {
  236.                         for (var storedEventIdx=0; storedEventIdx<sections[sectionIdx].length; storedEventIdx++)
  237.                         {
  238.                             var storedEvent = section[storedEventIdx];
  239.                             Silv.EvChance.DisableEv(mapId, this.event(storedEvent.id), storedEvent);
  240.                             this.event(storedEvent.id).erase();
  241.                         }
  242.                     }
  243.                 }
  244.             }
  245.         }
  246.     }
  247.     //------------------------------------------------------------------------------------------------------------------------------------
  248.     // Now process the regular sections
  249.     //------------------------------------------------------------------------------------------------------------------------------------
  250.     for (var sectionIdx=0; sectionIdx<this.sections.regularArray.length; sectionIdx++)
  251.     {
  252.         var section = this.sections.regularArray[sectionIdx];
  253.         section.enabled = Silv.EvChance.Chance(section.chance);
  254.         if (!section.enabled)
  255.         {
  256.             for (var sectionEventIdx=0; sectionEventIdx<section.events.length; sectionEventIdx++)
  257.             {
  258.                 var storedEvent = section.events[sectionEventIdx];
  259.                 Silv.EvChance.DisableEv(mapId, this.event(storedEvent.id), storedEvent);
  260.             }
  261.         }
  262.     }
  263.     //------------------------------------------------------------------------------------------------------------------------------------
  264.     this.refreshTileEvents();
  265. };
  266.  
  267. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  268. // This is the end of this awesome script!
  269. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement