Advertisement
Maliki79

MalMZ_EncounterConditions

Aug 6th, 2022
1,316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //=============================================================================
  2. // RPG Maker MZ - Maliki's Encounter Conditions v1.0
  3. //=============================================================================
  4.  
  5. /*:
  6.  * @target MZ
  7.  * @plugindesc Provides deeper control on what enemy troops are encountered by the player
  8.  * @author Maliki79
  9.  * 
  10.  * @help MalMZ_EncounterConditions.js
  11.  *
  12.  * This plugin enables devs to set conditional evals for random encounter troop battles.
  13.  *
  14.  * To start, set up encounters normally.
  15.  * Next, in the Map Notes, use Notetag <encounterEvals: x, code >>>
  16.  * noting the specific spaces and the end of the tag MUST have 3 > signs as shown and the space after the :
  17.  * Let x be the id number of the troop.  
  18.  * code is the specific javascript code you are using.  
  19.  * Be sure you use code that can eval to true or false.
  20.  *
  21.  * You can use multiple lines to check for a troop.
  22.  * You can also use s[x] or v[x] for game switches and variables respectively.
  23.  *
  24.  * Once set up, when the player starts a random battle, the plugin will check for any evels that are set for the specific Troop Id.
  25.  * If ALL evals for that troop are true, it is then added to the list of POSSIBLE encounters based on your setup in Map Properties.
  26.  * You can have different conditions for the same troop on different maps.
  27.  *
  28.  * Example: <encounterEvals: 7, $gameParty.gold() > 200 >>>
  29.  * Troop Id 7 will only appear if the party has more than 200.
  30.  *
  31.  * Example 2: (Separate lines on the same map property notes)
  32.  * <encounterEvals: 10, s[27] >>>
  33.  * <encounterEvals: 10, $gameParty.highestLevel() < 5 >>>
  34.  * Troop 10 will only appear if game switch 27 is on AND the party's highest level is lower than 5.
  35.  *
  36.  */
  37.  
  38. Game_Player.prototype.makeEncounterTroopId = function() {
  39.     const encounterList = [];
  40.     let weightSum = 0;
  41.     for (const encounter of $gameMap.encounterList()) {
  42.         if (this.malEncounterCond(encounter)) {
  43.         if (this.meetsEncounterConditions(encounter)) {
  44.             encounterList.push(encounter);
  45.             weightSum += encounter.weight;
  46.         }
  47.     }
  48.     }
  49.     if (weightSum > 0) {
  50.         let value = Math.randomInt(weightSum);
  51.         for (const encounter of encounterList) {
  52.             value -= encounter.weight;
  53.             if (value < 0) {
  54.                 return encounter.troopId;
  55.             }
  56.         }
  57.     }
  58.     return 0;
  59. };
  60.  
  61. Game_Player.prototype.malEncounterCond = function(encounter) {
  62.     var troopId = encounter.troopId;
  63.     var cond = this.encounterCond;
  64.     var s = $gameSwitches._data;   
  65.     var v = $gameVariables._data;
  66.     for (var i = 0; i < cond.length; i++) {
  67.         var cond2 = cond[i];
  68.         if (cond2[0] == troopId) {
  69.             var ecEval = cond2[1];
  70.             if(eval(ecEval) == false) return false;
  71.         }
  72.     }
  73.     return true;
  74. };
  75.  
  76. var malSMStart = Scene_Map.prototype.start
  77. Scene_Map.prototype.start = function() {
  78.     malSMStart.call(this);
  79.     $gamePlayer.setEncountercond();
  80. };
  81.  
  82. Game_Player.prototype.setEncountercond = function() {
  83.     this.encounterCond = [];   
  84.     var noteread = $dataMap.note;  
  85.     while(noteread.indexOf("encounterEvals") > -1) 
  86.     {  
  87.         var notereg = noteread.split("<encounterEvals: "); 
  88.         var match = notereg[1].split(">>>");   
  89.         var match2 = match[0].split(",");  
  90.         match2[0] = Number(match2[0]);
  91.         this.encounterCond.push(match2);   
  92.         noteread = noteread.replace("<encounterEvals: ", " "); 
  93.     }  
  94. }; 
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement