Maliki79

MalEncounterRateOptions

May 13th, 2016 (edited)
749
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================
  2. // Maliki's Random Encounter Step Options
  3. // MalEncounterRateOptions.js
  4. // version 1.7
  5. //=============================================================================
  6. /*:  
  7. * @plugindesc ver1.6a - Allows players to set a general random encounter rate in the Options Menu.  
  8.  * @author Maliki79
  9.  *
  10.  * @param RateUpperLimit
  11.  * @desc The highest percentage the Encounter Rate can reach.  This number MUST be at or above 100.
  12.  * Default: 100
  13.  * @default 100
  14.  *
  15.  * @param RateLowerLimit
  16.  * @desc The lowest percentage the Encounter Rate can reach.  This number MUST be at or below 100.
  17.  * Default: 0
  18.  * @default 0
  19.  *
  20.  * @param EncounterOffset
  21.  * @desc The value that the Encounter Rate will change when a direction or ok is pressed.
  22.  * Default: 50
  23.  * @default 50
  24.  *
  25.  * @param EncounterRateShow
  26.  * @desc Forces Encounter rate to be shown in Options even when locked. 0 = true 1 = false
  27.  * Default: 0
  28.  * @default 0
  29.  *
  30.  * @param EncounterSpeed
  31.  * @desc Enter an Interger higher than 0 to allow encounters to occur even when standing still.
  32.  * Default: 0
  33.  * @default 0
  34.  *
  35.  * @help Allows players to set a general random encounter rate in the Options Menu.  
  36.  * This rate can be reduced to 0 to eliminate ALL random encounters.
  37.  * Also allows developers to lock/unlock the encounter rate directly and set
  38.  * values to whatever they want.
  39.  *
  40.  * Once installed, run your game and go to Options to find the
  41.  * Encounter Rate setting. (If EncounterRateShow is NOT set to 0, the encounter rate will NOT appear
  42.  * on the Title Screen's Option Menu as the rate is considered locked when on the Title Screen.)
  43.  *
  44.  * Optional: You can set the Upper limit of the Encounter rate to any number over 100.
  45.  * If set below, or to a non-number, it will default to 100.
  46.  *
  47.  * Optional: You can set the amount the Encounter Rate rises or falls via the Encounter Offset Param.
  48.  *
  49.  * You can set a timer to allow encounters while standing still.
  50.  * If the param EncounterSpeed is set to a number higher than 0, encounters will be able to occur
  51.  * even while players are not moving!
  52.  * Increasing the number will speed up the enocounter rate.
  53.  * Leaving it at 0 will disable this option.
  54.  * (Other encounter rate option will still be in effect)
  55.  *
  56.  * This plugin now also allows you to adjust the "weight" of random encounters by enemy troop Id.
  57.  * In map notes, you can add the notetag:
  58.  *
  59.  * <malEncAdj: troopId, amount, condition >>>
  60.  * with troopId being the troop Id number in the database,
  61.  * amount being the amount the weight will be adjusted (this can be negative, but it must be an integer.)
  62.  * and condition can be a javascript eval that returns true or false.
  63.  * (And note the 3 >>> at the end of the tag.)
  64.  *
  65.  * For example, the tag <malEncAdj: 1, 10, $gameSwitches.value(1)>>>
  66.  * will increase the weight of all instances of troop one on that map by 10 if switch 1 is turned ON.
  67.  *
  68.  * <malEncAdj: 4, -25, $gameVariables.value(1) > 10>>>
  69.  * will decrease the weight of all instances of troop 4 on that map by 25 if variable 1 is greater than 10.
  70.  *
  71.  * <malEncAdj: 7, $gameVariables.value(2) * 5, $gameVariables.value(2) > 0>>>
  72.  * will increase the weight of all instances of troop 7 on that map by 5 * variable 2 if variable 2 is greater than 0.
  73.  *
  74.  * If you set the troop ID to -1, it will affect ALL troops on that map if the conditions are met.
  75.  *
  76.  * weights are cumulative, meaning you could potentially have a troop affected by multiple tages.
  77.  * Note that any weight values that change to a value below 0 will NOT be encountered at all.
  78.  * Conversely, any troops initially set to 0 in the database but brought above 1 or higher CAN be encountered.
  79.  * (You can eval weight to an integer)
  80.  *
  81.  * <malNulRegionEnc: regionID, condition >>>
  82.  * with region ID being the region ID of the tile the player is standing on.  
  83.  * (Using -1 will effect ALL regions AND regionless areas (basically the entire map)
  84.  *
  85.  * This note tag will REMOVE ALL encounters on the specified region ID on that map if the conditions are true.
  86.  *
  87.  * Note that this tag supersides the malEncAdj tag meaning if you negate all encounters, their weight will hot matter.
  88.  *
  89.  *  SCRIPT CALLS
  90.  *
  91.  * $gameSystem.lockEncounterRate();
  92.  * This call will LOCK the Encounter Rate setting at whatever number it was when called.
  93.  * You can add a number in the () to set the rate while locking it.
  94.  * Note: Unless EncounterRateShow is set to 0, the Encounter Rate will not show on the Option
  95.  * Menu while locked.
  96.  *
  97.  * $gameSystem.unlockEncounterRate();
  98.  * This call will allow players to change the Encounter Rate again after it hs been locked.
  99.  * You can add a number in the () to set the rate while unlocking it.
  100.  *
  101.  * $gameSystem.setEncounterRate(x);
  102.  * This call will change the Encounter Rate to any positive integer.
  103.  * Note that the player can change the number if the Encounter Rate is not locked.
  104.  *
  105.  * $gameSystem.getEncounterRate();
  106.  * This call will return the current Encounter Rate.
  107.  */
  108.  
  109. ConfigManager.commandEncounter = 100;
  110.  
  111. var MalEncounterTitle = Scene_Title.prototype.create;
  112. Scene_Title.prototype.create = function() {
  113. MalEncounterTitle.call(this);
  114. $gameSystem._encountersLocked = true;
  115. }
  116.  
  117. var MalEncounterInitialize = Game_System.prototype.initialize;
  118. Game_System.prototype.initialize = function() {
  119. MalEncounterInitialize.call(this);
  120. this._encountersLocked = false;
  121. }
  122.  
  123. Game_System.prototype.lockEncounterRate = function(setting) {
  124. $gameSystem._encountersLocked = true;
  125. if (setting) this.setEncounterRate(setting);
  126. }
  127.  
  128. Game_System.prototype.unlockEncounterRate = function(setting) {
  129. $gameSystem._encountersLocked = false;
  130. if (setting) this.setEncounterRate(setting);
  131. }
  132.  
  133. Game_System.prototype.getEncounterRate = function() {
  134. return $gameSystem._encounterRate;
  135. }
  136.  
  137. Game_System.prototype.setEncounterRate = function(setting) {
  138. var numb = Number(eval(setting)) || 100;
  139. if (numb < 0) numb = 0;
  140. ConfigManager['commandEncounterVolume'] = numb;
  141. $gameSystem._encounterRate = numb;
  142. }
  143.  
  144. Object.defineProperty(ConfigManager, 'encounterRate', {
  145.     get: function() {
  146.         return ConfigManager.commandEncounterVolume;
  147.     },
  148.     set: function(value) {
  149.         ConfigManager.commandEncounterVolume = value;
  150.     },
  151.     configurable: true
  152. });
  153.  
  154. var Mal_Config_MakeData = ConfigManager.makeData;
  155. ConfigManager.makeData = function() {
  156.     var config = Mal_Config_MakeData.call(this);
  157.     config.encounterRate = this.encounterRate; 
  158.     return config;
  159. };
  160.  
  161. var Mal_Config_applyData = ConfigManager.applyData;
  162. ConfigManager.applyData = function(config) {
  163.     Mal_Config_applyData.call(this, config);
  164.     this.encounterRate = this.readEncounter(config, 'encounterRate');
  165. };
  166.  
  167. ConfigManager.readEncounter = function(config, name) {
  168.     var value = config[name];
  169.     if (value !== undefined) {
  170.         return Number(value);
  171.     } else {
  172.         return 100;
  173.     }
  174. };
  175.  
  176. Window_Options.prototype.encounterOffset = function() {
  177.     var num = Number(PluginManager.parameters('MalEncounterRateOptions')['EncounterOffset']) || 50;
  178.     if (num !== undefined) {
  179.     return Math.floor(num);
  180.     } else {
  181.     return 50;
  182. }  
  183. };
  184.  
  185. var Mal_Window_addGeneralOptions = Window_Options.prototype.addGeneralOptions;
  186. Window_Options.prototype.addGeneralOptions = function() {
  187.     Mal_Window_addGeneralOptions.call(this);
  188.     if (PluginManager.parameters('MalEncounterRateOptions')['EncounterRateShow'] == 0 || $gameSystem._encountersLocked == false ) this.addCommand("Encounter Rate", 'commandEncounterVolume');
  189.     };
  190.    
  191. Game_Player.prototype.encounterProgressValue = function() {
  192.     var value = $gameMap.isBush(this.x, this.y) ? 2 : 1;
  193.     var val2 = ConfigManager.encounterRate;
  194.     if ($gameParty.hasEncounterHalf()) {
  195.         value *= 0.5;
  196.     }
  197.     if (this.isInShip()) {
  198.         value *= 0.5;
  199.     }
  200.     return value * (val2 / 100.0);
  201. };
  202.  
  203. ConfigManager.readEncounter = function(config, name) {
  204.     var value = config[name];
  205.     var limit = Number(PluginManager.parameters('MalEncounterRateOptions')['RateUpperLimit']);
  206.     var lowLimit = Number(PluginManager.parameters('MalEncounterRateOptions')['RateLowerLimit']);
  207.     if (limit < 100) limit = 100;
  208.     if (lowLimit < 0) lowLimit = 0;
  209.     if (value !== undefined) {
  210.         return Number(value).clamp(lowLimit, limit);
  211.     } else {
  212.         return 100;
  213.     }
  214. };
  215.  
  216. Window_Options.prototype.isEncounterSymbol = function(symbol) {
  217.     return symbol.contains('Encounter');
  218. };
  219.  
  220. var Mal_Win_Options_processOk = Window_Options.prototype.processOk
  221. Window_Options.prototype.processOk = function() {
  222.     var index = this.index();
  223.     var symbol = this.commandSymbol(index);
  224.     if (this.isEncounterSymbol(symbol) ) {
  225.         var value = this.getConfigValue(symbol);
  226.         var offset = this.encounterOffset();
  227.         var limit = Math.floor(Number(PluginManager.parameters('MalEncounterRateOptions')['RateUpperLimit'])) || 100;
  228.         var lowLimit = Math.floor(Number(PluginManager.parameters('MalEncounterRateOptions')['RateLowerLimit'])) || 0;
  229.         if (limit < 100) limit = 100;
  230.         if ($gameSystem._encountersLocked == false){
  231.         if (value + offset > limit && value != limit) {
  232.         value = limit;
  233.         } else {
  234.         value += offset;
  235.         }
  236.         if (value > limit) value = lowLimit;
  237.         this.changeValue(symbol, value);
  238.         $gameSystem._encounterRate = value;
  239.         } else {
  240.         SoundManager.playBuzzer();
  241.         }
  242.         } else {
  243.             Mal_Win_Options_processOk.call(this);
  244.         }
  245. };
  246.  
  247. var Mal_Win_Options_cursorRight = Window_Options.prototype.cursorRight
  248. Window_Options.prototype.cursorRight = function(wrap) {
  249.     var index = this.index();
  250.     var symbol = this.commandSymbol(index);
  251.     if (this.isEncounterSymbol(symbol)) {
  252.         var value = this.getConfigValue(symbol);
  253.         var offset = this.encounterOffset();
  254.         var limit = Math.floor(Number(PluginManager.parameters('MalEncounterRateOptions')['RateUpperLimit'])) || 100;
  255.         var lowLimit = Math.floor(Number(PluginManager.parameters('MalEncounterRateOptions')['RateLowerLimit'])) || 0;
  256.         if (limit < 100) limit = 100;
  257.         if ($gameSystem._encountersLocked == false){
  258.         if (value + offset > limit && value != limit) {
  259.         value = limit;
  260.         } else {
  261.         value += offset;
  262.         }
  263.         if (value > limit) value = lowLimit;
  264.         this.changeValue(symbol, value);
  265.         $gameSystem._encounterRate = value;
  266.         } else {
  267.         SoundManager.playBuzzer();
  268.         }
  269.         } else {
  270.             Mal_Win_Options_cursorRight.call(this, wrap);
  271.         }
  272. };
  273.  
  274. var Mal_Win_Options_cursorLeft = Window_Options.prototype.cursorLeft
  275. Window_Options.prototype.cursorLeft = function(wrap) {
  276.     var index = this.index();
  277.     var symbol = this.commandSymbol(index);
  278.     if (this.isEncounterSymbol(symbol)) {
  279.         var value = this.getConfigValue(symbol);
  280.         var offset = this.encounterOffset();
  281.         var limit = Math.floor(Number(PluginManager.parameters('MalEncounterRateOptions')['RateUpperLimit'])) || 100;
  282.         var lowLimit = Math.floor(Number(PluginManager.parameters('MalEncounterRateOptions')['RateLowerLimit'])) || 0;
  283.         if (limit < 100) limit = 100;
  284.         if ($gameSystem._encountersLocked == false){
  285.         if (value - offset < lowLimit && value != lowLimit) {
  286.         value = lowLimit;
  287.         } else {
  288.         value -= offset;
  289.         }
  290.         if (value < lowLimit) value = limit;
  291.         this.changeValue(symbol, value);
  292.         $gameSystem._encounterRate = value;
  293.         } else {
  294.         SoundManager.playBuzzer();
  295.         }
  296.         } else {
  297.             Mal_Win_Options_cursorLeft.call(this, wrap);
  298.         }
  299. };
  300.  
  301. var MalEncounter_onLoadSuccess = Scene_Load.prototype.onLoadSuccess;
  302. Scene_Load.prototype.onLoadSuccess = function() {
  303. MalEncounter_onLoadSuccess.call(this);
  304. ConfigManager['commandEncounterVolume'] = $gameSystem._encounterRate || 100;
  305. //$gamePlayer.encounterTick = 0;
  306. }
  307.  
  308. //var MalEncounter_updateNonmoving = Game_Player.prototype.updateNonmoving;
  309. Game_Player.prototype.updateNonmoving = function(wasMoving) {
  310.     if (!$gamePlayer.encounterTick) $gamePlayer.encounterTick = 0;
  311.     if (!$gameMap.isEventRunning()) {
  312.         if (wasMoving) {
  313.             $gameParty.onPlayerWalk();
  314.             this._firstStep = false;
  315.             this.checkEventTriggerHere([1,2]);
  316.             if ($gameMap.setupStartingEvent()) {
  317.                 return;
  318.             }
  319.         }
  320.         if (this.triggerAction()) {
  321.             return;
  322.         }
  323.         if ($gamePlayer.encounterTick >= 1000) {
  324.             this.updateEncounterCount();
  325.             $gamePlayer.encounterTick = 0;
  326.         } else {
  327.             if(!this._firstStep) $gamePlayer.encounterTick += 1 * Math.floor(Number(PluginManager.parameters('MalEncounterRateOptions')['EncounterSpeed']));
  328.             //console.log($gamePlayer.encounterTick);
  329.         }
  330.         if (wasMoving) {
  331.             this.updateEncounterCount();
  332.         } else {
  333.             $gameTemp.clearDestination();
  334.         }
  335.     }
  336. };
  337.  
  338. var MalEncounterCount = Game_Player.prototype.makeEncounterCount;
  339. Game_Player.prototype.makeEncounterCount = function() {
  340.     MalEncounterCount.call(this);
  341.     if(PluginManager.parameters('MalEncounterRateOptions')['EncounterSpeed'] != 0) this._encounterCount += 3;
  342.     this._firstStep = true;
  343. };
  344.  
  345. var malEncounterGMSetup = Game_Map.prototype.setup;
  346. Game_Map.prototype.setup = function(mapId) {
  347.     if (!$dataMap) {
  348.         throw new Error('The map data is not available');
  349.     }
  350.     malEncounterGMSetup.call(this, mapId);
  351.     this.setupEncounterAdjusts();
  352. };
  353.  
  354. Game_Map.prototype.setupEncounterAdjusts = function() {
  355.     this._encounterAdjusts = [];
  356.     this._regionCond = [];  
  357. var noteread = $dataMap.note;
  358. while(noteread.indexOf("malEncAdj") > -1)
  359. {
  360.     var notereg = noteread.split("<malEncAdj: ");
  361.     var match = notereg[1].split(">>>");
  362.     match = match[0].split(", ");
  363.     match[0] = parseInt(match[0]);
  364.     //match[1] = parseInt(match[1]);
  365.     this._encounterAdjusts.push(match);
  366.     noteread = noteread.replace("<malEncAdj: ", " ");
  367. }
  368.  
  369.      
  370.     var noteread = $dataMap.note;  
  371.     while(noteread.indexOf("malNulRegionEnc") > -1)  
  372.     {  
  373.         var notereg = noteread.split("<malNulRegionEnc: ");  
  374.         var match = notereg[1].split(">>>");    
  375.         var match2 = match[0].split(",");  
  376.         match2[0] = Number(match2[0]);
  377.         this._regionCond.push(match2);    
  378.         noteread = noteread.replace("<malNulRegionEnc: ", " ");  
  379.     }  
  380. };
  381.  
  382. Game_Player.prototype.makeEncounterTroopId = function() {
  383.     var encounterList = [];
  384.     var weightSum = 0;
  385.     $gameMap.encounterList().forEach(function(encounter) {
  386.         if (this.meetsEncounterConditions(encounter)) {
  387.             var encounterCopy = encounter;
  388.             encounterCopy = this.malEncounterAdjust(encounterCopy);
  389.             if (encounterCopy.weight > 0) {
  390.                 encounterList.push(encounterCopy);
  391.                 weightSum += encounterCopy.weight;
  392.             }
  393.         }
  394.     }, this);
  395.     if (weightSum > 0) {
  396.         var value = Math.randomInt(weightSum);
  397.         for (var i = 0; i < encounterList.length; i++) {
  398.             value -= encounterList[i].weight;
  399.             if (value < 0) {
  400.                 return encounterList[i].troopId;
  401.             }
  402.         }
  403.     }
  404.     return 0;
  405. };
  406.  
  407. Game_Player.prototype.malEncounterAdjust = function (encounter) {
  408.     var ec = encounter;
  409.     var id = ec.troopId;
  410.     var ea = $gameMap._encounterAdjusts;
  411.     for (var i = 0; i < ea.length; i++) {
  412.         var line = ea[i];
  413.         var value = Number(eval(line[1])) || 0;
  414.         var check = eval(line[2]);
  415.         if ((line[0] == id || line[0] == -1) && check) ec.weight += value;
  416.     }
  417.     return ec;
  418. }
  419.  
  420. var MalGPMeetsEncCond = Game_Player.prototype.meetsEncounterConditions
  421. Game_Player.prototype.meetsEncounterConditions = function(encounter) {
  422.     if ($gameMap.nulRegion(this.regionId())) return false;
  423.     return MalGPMeetsEncCond.call(this, encounter);
  424. };
  425.  
  426. Game_Map.prototype.nulRegion = function(region) {
  427.     if (this._regionCond.length < 1) return false;
  428.     var region = Number(region);
  429.      for (var i = 0; i < this._regionCond.length; i++) {
  430.         if ((this._regionCond[i][0] == region || this._regionCond[i][0] == -1) && eval(this._regionCond[i][1])) return true;
  431.     }
  432.     return false;
  433. };
Advertisement
Add Comment
Please, Sign In to add comment