Advertisement
Maliki79

MalEncounterRateOptions

May 13th, 2016 (edited)
595
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.6a
  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.  *  SCRIPT CALLS
  82.  *
  83.  * $gameSystem.lockEncounterRate();
  84.  * This call will LOCK the Encounter Rate setting at whatever number it was when called.
  85.  * You can add a number in the () to set the rate while locking it.
  86.  * Note: Unless EncounterRateShow is set to 0, the Encounter Rate will not show on the Option
  87.  * Menu while locked.
  88.  *
  89.  * $gameSystem.unlockEncounterRate();
  90.  * This call will allow players to change the Encounter Rate again after it hs been locked.
  91.  * You can add a number in the () to set the rate while unlocking it.
  92.  *
  93.  * $gameSystem.setEncounterRate(x);
  94.  * This call will change the Encounter Rate to any positive integer.
  95.  * Note that the player can change the number if the Encounter Rate is not locked.
  96.  *
  97.  * $gameSystem.getEncounterRate();
  98.  * This call will return the current Encounter Rate.
  99.  */
  100.  
  101. ConfigManager.commandEncounter = 100;
  102.  
  103. var MalEncounterTitle = Scene_Title.prototype.create;
  104. Scene_Title.prototype.create = function() {
  105. MalEncounterTitle.call(this);
  106. $gameSystem._encountersLocked = true;
  107. }
  108.  
  109. var MalEncounterInitialize = Game_System.prototype.initialize;
  110. Game_System.prototype.initialize = function() {
  111. MalEncounterInitialize.call(this);
  112. this._encountersLocked = false;
  113. }
  114.  
  115. Game_System.prototype.lockEncounterRate = function(setting) {
  116. $gameSystem._encountersLocked = true;
  117. if (setting) this.setEncounterRate(setting);
  118. }
  119.  
  120. Game_System.prototype.unlockEncounterRate = function(setting) {
  121. $gameSystem._encountersLocked = false;
  122. if (setting) this.setEncounterRate(setting);
  123. }
  124.  
  125. Game_System.prototype.getEncounterRate = function() {
  126. return $gameSystem._encounterRate;
  127. }
  128.  
  129. Game_System.prototype.setEncounterRate = function(setting) {
  130. var numb = Number(eval(setting)) || 100;
  131. if (numb < 0) numb = 0;
  132. ConfigManager['commandEncounterVolume'] = numb;
  133. $gameSystem._encounterRate = numb;
  134. }
  135.  
  136. Object.defineProperty(ConfigManager, 'encounterRate', {
  137.     get: function() {
  138.         return ConfigManager.commandEncounterVolume;
  139.     },
  140.     set: function(value) {
  141.         ConfigManager.commandEncounterVolume = value;
  142.     },
  143.     configurable: true
  144. });
  145.  
  146. var Mal_Config_MakeData = ConfigManager.makeData;
  147. ConfigManager.makeData = function() {
  148.     var config = Mal_Config_MakeData.call(this);
  149.     config.encounterRate = this.encounterRate; 
  150.     return config;
  151. };
  152.  
  153. var Mal_Config_applyData = ConfigManager.applyData;
  154. ConfigManager.applyData = function(config) {
  155.     Mal_Config_applyData.call(this, config);
  156.     this.encounterRate = this.readEncounter(config, 'encounterRate');
  157. };
  158.  
  159. ConfigManager.readEncounter = function(config, name) {
  160.     var value = config[name];
  161.     if (value !== undefined) {
  162.         return Number(value);
  163.     } else {
  164.         return 100;
  165.     }
  166. };
  167.  
  168. Window_Options.prototype.encounterOffset = function() {
  169.     var num = Number(PluginManager.parameters('MalEncounterRateOptions')['EncounterOffset']) || 50;
  170.     if (num !== undefined) {
  171.     return Math.floor(num);
  172.     } else {
  173.     return 50;
  174. }  
  175. };
  176.  
  177. var Mal_Window_addGeneralOptions = Window_Options.prototype.addGeneralOptions;
  178. Window_Options.prototype.addGeneralOptions = function() {
  179.     Mal_Window_addGeneralOptions.call(this);
  180.     if (PluginManager.parameters('MalEncounterRateOptions')['EncounterRateShow'] == 0 || $gameSystem._encountersLocked == false ) this.addCommand("Encounter Rate", 'commandEncounterVolume');
  181.     };
  182.    
  183. Game_Player.prototype.encounterProgressValue = function() {
  184.     var value = $gameMap.isBush(this.x, this.y) ? 2 : 1;
  185.     var val2 = ConfigManager.encounterRate;
  186.     if ($gameParty.hasEncounterHalf()) {
  187.         value *= 0.5;
  188.     }
  189.     if (this.isInShip()) {
  190.         value *= 0.5;
  191.     }
  192.     return value * (val2 / 100.0);
  193. };
  194.  
  195. ConfigManager.readEncounter = function(config, name) {
  196.     var value = config[name];
  197.     var limit = Number(PluginManager.parameters('MalEncounterRateOptions')['RateUpperLimit']);
  198.     var lowLimit = Number(PluginManager.parameters('MalEncounterRateOptions')['RateLowerLimit']);
  199.     if (limit < 100) limit = 100;
  200.     if (lowLimit < 0) lowLimit = 0;
  201.     if (value !== undefined) {
  202.         return Number(value).clamp(lowLimit, limit);
  203.     } else {
  204.         return 100;
  205.     }
  206. };
  207.  
  208. Window_Options.prototype.isEncounterSymbol = function(symbol) {
  209.     return symbol.contains('Encounter');
  210. };
  211.  
  212. var Mal_Win_Options_processOk = Window_Options.prototype.processOk
  213. Window_Options.prototype.processOk = function() {
  214.     var index = this.index();
  215.     var symbol = this.commandSymbol(index);
  216.     if (this.isEncounterSymbol(symbol) ) {
  217.         var value = this.getConfigValue(symbol);
  218.         var offset = this.encounterOffset();
  219.         var limit = Math.floor(Number(PluginManager.parameters('MalEncounterRateOptions')['RateUpperLimit'])) || 100;
  220.         var lowLimit = Math.floor(Number(PluginManager.parameters('MalEncounterRateOptions')['RateLowerLimit'])) || 0;
  221.         if (limit < 100) limit = 100;
  222.         if ($gameSystem._encountersLocked == false){
  223.         if (value + offset > limit && value != limit) {
  224.         value = limit;
  225.         } else {
  226.         value += offset;
  227.         }
  228.         if (value > limit) value = lowLimit;
  229.         this.changeValue(symbol, value);
  230.         $gameSystem._encounterRate = value;
  231.         } else {
  232.         SoundManager.playBuzzer();
  233.         }
  234.         } else {
  235.             Mal_Win_Options_processOk.call(this);
  236.         }
  237. };
  238.  
  239. var Mal_Win_Options_cursorRight = Window_Options.prototype.cursorRight
  240. Window_Options.prototype.cursorRight = function(wrap) {
  241.     var index = this.index();
  242.     var symbol = this.commandSymbol(index);
  243.     if (this.isEncounterSymbol(symbol)) {
  244.         var value = this.getConfigValue(symbol);
  245.         var offset = this.encounterOffset();
  246.         var limit = Math.floor(Number(PluginManager.parameters('MalEncounterRateOptions')['RateUpperLimit'])) || 100;
  247.         var lowLimit = Math.floor(Number(PluginManager.parameters('MalEncounterRateOptions')['RateLowerLimit'])) || 0;
  248.         if (limit < 100) limit = 100;
  249.         if ($gameSystem._encountersLocked == false){
  250.         if (value + offset > limit && value != limit) {
  251.         value = limit;
  252.         } else {
  253.         value += offset;
  254.         }
  255.         if (value > limit) value = lowLimit;
  256.         this.changeValue(symbol, value);
  257.         $gameSystem._encounterRate = value;
  258.         } else {
  259.         SoundManager.playBuzzer();
  260.         }
  261.         } else {
  262.             Mal_Win_Options_cursorRight.call(this, wrap);
  263.         }
  264. };
  265.  
  266. var Mal_Win_Options_cursorLeft = Window_Options.prototype.cursorLeft
  267. Window_Options.prototype.cursorLeft = function(wrap) {
  268.     var index = this.index();
  269.     var symbol = this.commandSymbol(index);
  270.     if (this.isEncounterSymbol(symbol)) {
  271.         var value = this.getConfigValue(symbol);
  272.         var offset = this.encounterOffset();
  273.         var limit = Math.floor(Number(PluginManager.parameters('MalEncounterRateOptions')['RateUpperLimit'])) || 100;
  274.         var lowLimit = Math.floor(Number(PluginManager.parameters('MalEncounterRateOptions')['RateLowerLimit'])) || 0;
  275.         if (limit < 100) limit = 100;
  276.         if ($gameSystem._encountersLocked == false){
  277.         if (value - offset < lowLimit && value != lowLimit) {
  278.         value = lowLimit;
  279.         } else {
  280.         value -= offset;
  281.         }
  282.         if (value < lowLimit) value = limit;
  283.         this.changeValue(symbol, value);
  284.         $gameSystem._encounterRate = value;
  285.         } else {
  286.         SoundManager.playBuzzer();
  287.         }
  288.         } else {
  289.             Mal_Win_Options_cursorLeft.call(this, wrap);
  290.         }
  291. };
  292.  
  293. var MalEncounter_onLoadSuccess = Scene_Load.prototype.onLoadSuccess;
  294. Scene_Load.prototype.onLoadSuccess = function() {
  295. MalEncounter_onLoadSuccess.call(this);
  296. ConfigManager['commandEncounterVolume'] = $gameSystem._encounterRate || 100;
  297. //$gamePlayer.encounterTick = 0;
  298. }
  299.  
  300. //var MalEncounter_updateNonmoving = Game_Player.prototype.updateNonmoving;
  301. Game_Player.prototype.updateNonmoving = function(wasMoving) {
  302.     if (!$gamePlayer.encounterTick) $gamePlayer.encounterTick = 0;
  303.     if (!$gameMap.isEventRunning()) {
  304.         if (wasMoving) {
  305.             $gameParty.onPlayerWalk();
  306.             this._firstStep = false;
  307.             this.checkEventTriggerHere([1,2]);
  308.             if ($gameMap.setupStartingEvent()) {
  309.                 return;
  310.             }
  311.         }
  312.         if (this.triggerAction()) {
  313.             return;
  314.         }
  315.         if ($gamePlayer.encounterTick >= 1000) {
  316.             this.updateEncounterCount();
  317.             $gamePlayer.encounterTick = 0;
  318.         } else {
  319.             if(!this._firstStep) $gamePlayer.encounterTick += 1 * Math.floor(Number(PluginManager.parameters('MalEncounterRateOptions')['EncounterSpeed']));
  320.             //console.log($gamePlayer.encounterTick);
  321.         }
  322.         if (wasMoving) {
  323.             this.updateEncounterCount();
  324.         } else {
  325.             $gameTemp.clearDestination();
  326.         }
  327.     }
  328. };
  329.  
  330. var MalEncounterCount = Game_Player.prototype.makeEncounterCount;
  331. Game_Player.prototype.makeEncounterCount = function() {
  332.     MalEncounterCount.call(this);
  333.     if(PluginManager.parameters('MalEncounterRateOptions')['EncounterSpeed'] != 0) this._encounterCount += 3;
  334.     this._firstStep = true;
  335. };
  336.  
  337. //TESTING
  338. var malEncounterGMSetup = Game_Map.prototype.setup;
  339. Game_Map.prototype.setup = function(mapId) {
  340.     if (!$dataMap) {
  341.         throw new Error('The map data is not available');
  342.     }
  343.     malEncounterGMSetup.call(this, mapId);
  344.     this.setupEncounterAdjusts();
  345. };
  346.  
  347. Game_Map.prototype.setupEncounterAdjusts = function() {
  348.     this._encounterAdjusts = [];
  349. var noteread = $dataMap.note;
  350. while(noteread.indexOf("malEncAdj") > -1)
  351. {
  352.     var notereg = noteread.split("<malEncAdj: ");
  353.     var match = notereg[1].split(">>>");
  354.     match = match[0].split(", ");
  355.     match[0] = parseInt(match[0]);
  356.     //match[1] = parseInt(match[1]);
  357.     this._encounterAdjusts.push(match);
  358.     noteread = noteread.replace("<malEncAdj: ", " ");
  359. }
  360. };
  361.  
  362. Game_Player.prototype.makeEncounterTroopId = function() {
  363.     var encounterList = [];
  364.     var weightSum = 0;
  365.     $gameMap.encounterList().forEach(function(encounter) {
  366.         if (this.meetsEncounterConditions(encounter)) {
  367.             var encounterCopy = encounter;
  368.             encounterCopy = this.malEncounterAdjust(encounterCopy);
  369.             if (encounterCopy.weight > 0) {
  370.                 encounterList.push(encounterCopy);
  371.                 weightSum += encounterCopy.weight;
  372.             }
  373.         }
  374.     }, this);
  375.     if (weightSum > 0) {
  376.         var value = Math.randomInt(weightSum);
  377.         for (var i = 0; i < encounterList.length; i++) {
  378.             value -= encounterList[i].weight;
  379.             if (value < 0) {
  380.                 return encounterList[i].troopId;
  381.             }
  382.         }
  383.     }
  384.     return 0;
  385. };
  386.  
  387. Game_Player.prototype.malEncounterAdjust = function (encounter) {
  388.     var ec = encounter;
  389.     var id = ec.troopId;
  390.     var ea = $gameMap._encounterAdjusts;
  391.     for (var i = 0; i < ea.length; i++) {
  392.         var line = ea[i];
  393.         var value = Number(eval(line[1])) || 0;
  394.         var check = eval(line[2]);
  395.         if ((line[0] == id || line[0] == -1) && check) ec.weight += value;
  396.     }
  397.     return ec;
  398. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement