Help with region based encounters

Hello, I am attempting to convert changes I made to MakeAMidget's encounter window script from RMXP into the script I wrote for RMMV based on it. I am running into problems, however attempting to figure out where MV is updating the map's encounters.

First I would like to know which of the following variables, or a different one if it is none of these, stores a map's encounters:
$gameMap.encounterList
$gamePlayer.$gameTroops
$gameTroop._enemies

Next, how does the game update the encounter list when regions are being employed?

What I am trying to accomplish is to have the encounter window not show up when there no troops loaded for the map but also account for maps where I am using region based troop encounters so that when the player enters one of these regions, the encounter window will be displayed.
From what I understand of your description, that is already default functionality.
The random encounters are part of the map properties, so if there are no encounters set in the map properties there won't be encounters on the map.

And if everything in the encounter table is conditioned to a Region ID, you have to be on that map and on a file with that Region ID for the encounter to happen.

So either you need to give a better description of what you want, or you misunderstood how random encounters work.
I believe it is encounterList.
it should store the target of the encounter, the region, and the rate.
*encounter*, as it is, it is an object.
*encounterList* is another object made up of *encounters*

$gamePlayer.$gameTroops is not even an object! XD

now, here I'm educatedly guessing, I believe when you move you always pass the tile you're standing on to the refresh process, and the refresh process gathers *all the tiles that have a region ID equal to that tile the player is standing on* and uses that to collect the relevant encounter from the list, based on it's rate.
I don't use that system, but I had to tinker with the ID part so that's what I remember.
$gameTroop is only of interest when a battle is going on.

$gameMap.encounterList() returns what is in $dataMap.encounterList so you could use either (it's probably more appropriate to use $gameMap.encounterList() as that's what Game_Player uses).

If you just want to know if the window should be displayed or not based on whether the player is in an encounter region, this code (from rpg_objects.js) will help:
Code:
Game_Player.prototype.makeEncounterTroopId = function() {
    var encounterList = [];
    var weightSum = 0;
    $gameMap.encounterList().forEach(function(encounter) {
        if (this.meetsEncounterConditions(encounter)) {
            encounterList.push(encounter);
            weightSum += encounter.weight;
        }
    }, this);
    if (weightSum > 0) {
        var value = Math.randomInt(weightSum);
        for (var i = 0; i < encounterList.length; i++) {
            value -= encounterList[i].weight;
            if (value < 0) {
                return encounterList[i].troopId;
            }
        }
    }
    return 0;
};

Game_Player.prototype.meetsEncounterConditions = function(encounter) {
    return (encounter.regionSet.length === 0 ||
            encounter.regionSet.contains(this.regionId()));
};

You could probably do something like this:
Code:
... = $gameMap.encounterList().some(function(encounter) {
  return $gamePlayer.meetsEncounterConditions(encounter);
});

where ... is the variable/property that will be updated with a true/false value based on whether the player is on a map or on a region where there could be encounters. You didn't provide any of the script you've already done, and I don't have a lot of experience with scenes and windows in MV. It could be something as simple as this in Scene_Map's update function:
Code:
this.encounterWindow.visible = ...
followed by the above.

Note - I have not tested this. I imagine it will probably need a little tweaking, but if you're a bit savvy with scripts, and it sounds like you are, it will at least be a decent starting point.
Thanks gstv87 and Shaz, you both helped me a ton in figuring out what was going on. What I had do to though was a bit different but what Shaz said was correct, the stuff you posted gave me the clues that led to the answer.

So, for future edification the makeEncounterTroopId only fires just before an encounter. Here is what I ended up with, I am just going to post the bits which control the visibility as I need to clean up a lot of crap code before I decide if I want to release the script or not.

Code that determine if the player is in an encounter region:
Code:
Game_Player.prototype.increaseSteps = function() {
       //Find a way to only run this is _regionList is empty and there are regions within encounterList objs
       $gameMap.encounterList().forEach(function(encounter) { $gamePlayer._regionList.push(encounter.regionSet); });
       Game_Character.prototype.increaseSteps.call(this);
       if (this.isNormal()) {
           $gameParty.increaseSteps();

           $gamePlayer._regionList.forEach(function(rgnSet){
               if (rgnSet.length === 0 || rgnSet.contains($gamePlayer.regionId())){
                   //alert("Entering a dangerous region!");
                    $gamePlayer._inEncRegion = true;
               } else{
                    $gamePlayer._inEncRegion = false;
               }
           });
       }
   };

_regionList and _inEncRegion are variables I added to the Game_Player object. Pretty simple, each time increaseSteps is called we want to see if the player has stepped into or out of an encounter region. One thing I still need to sort out is how to not make the regionList update/fill if it is not needed in order to save cpu time. On my test map it does not matter as there is only one region, but if you have a complex map using 100 or even all 255 of the regions I could see a large performance hit happening.

Then this bit controls the window visibility and incrementing the encounter gauge:
Code:
Window_Encounter.prototype.update = function(){
       if ($gameMap.encounterList().length > 0 && $gamePlayer._inEncRegion){
           if (!this._IsVisible){
               this.show(); //If window is not active and we are in an enc region then activate it
               this._IsVisible = true;
           }

           var rate = $gamePlayer.getCurrEncSteps() / $gamePlayer.getTotalEncSteps();
           this.drawEncounter(0,0,this._gWidth,this._gHeight,rate,this._gColor1,this._gColor2);
       } else{
           if (this._IsVisible){
               this.hide(); //If window is active and we are not in an enc region then deactivate it
               this._IsVisible = false;
           }
       }
   };

Again, pretty simple. We check if $gameMap's encounterList has any elements because if it does not then we don't want the window to be displayed, however if there are encounters and the player is in an encounter region then we want the window to be displayed.

Now there is another problem here which is that this code assumes a region is being used for one of the encounter troops. I need to add something to state if there are not any regions within the map to ignore that condition but in the case of my project it's not an issue as all maps will use encounter regions if encounters are possible.

The script still works in this use case but the window only will show after the player has taken a step instead of being active from the get go due to the && operand.

But the info you guys gave me lead me to the right answers so thanks. I will credit you both in the plugin.
you don't need to credit me 😉

However, I'm not sure I understand why my suggestion didn't work? Did you try it?

Again, I don't have MV with me, but now that you've provided some code, I can give you something to try:

Code:
Window_Encounter.prototype.update = function() {
  var encounterPossible = $gameMap.encounterList().some(function(encounter) {
    return $gamePlayer.meetsEncounterConditions(encounter);
  });
  if (encounterPossible && !this._IsVisible) {
    this.show();
    this._isVisible = true;
  } else if (!encounterPossible && this._IsVisible) {
    this.hide();
    this._isVisible = false;
  }
};

I am assuming this._IsVisible is already a property of Window_Encounter.

The above should display or hide the window immediately when the map is loaded, and should also cater for no encounters set up for the map. Out of interest, what happened when you tried it?

I also wasn't saying to use makeEncounterTroopId - I was just pointing that out as something worth looking at, as that's what determines if the player is in a region specified in the map encounter list. The real magic happens in the function it calls - meetsEncounterConditions.
you don't need to credit me 😉

However, I'm not sure I understand why my suggestion didn't work? Did you try it?

Again, I don't have MV with me, but now that you've provided some code, I can give you something to try:

Code:
Window_Encounter.prototype.update = function() {
  var encounterPossible = $gameMap.encounterList().some(function(encounter) {
    return $gamePlayer.meetsEncounterConditions(encounter);
  });
  if (encounterPossible && !this._IsVisible) {
    this.show();
    this._isVisible = true;
  } else if (!encounterPossible && this._IsVisible) {
    this.hide();
    this._isVisible = false;
  }
};

I am assuming this._IsVisible is already a property of Window_Encounter.

The above should display or hide the window immediately when the map is loaded, and should also cater for no encounters set up for the map. Out of interest, what happened when you tried it?

I also wasn't saying to use makeEncounterTroopId - I was just pointing that out as something worth looking at, as that's what determines if the player is in a region specified in the map encounter list. The real magic happens in the function it calls - meetsEncounterConditions.

Your suggestion was almost correct, it was absolutely correct in that it showed how MV handles region based encounters it just was not being called at the right time for my purposes which lead me on the hunting expedition which found increaseSteps.

Instead I wanted it to update each time the player takes a step so that as soon as they enter a region, the window shows, and as soon as they leave a region to an area with no encounters, the window hides.

I will leave the entire code here in this post, maybe it will help clarify what was aiming to do with this plugin.

Code:
/*:
* @plugindesc Displays a meter when encounters are possible
* @author Geowil
*
*
*
* @help
* Credits:
* MakeAMidget - original RMXP script creator
* gstv87 - provided scripting help (rpgmakerweb.com)
* Shaz - provided scripting help (rpgmakerweb.com)
*/

const params = PluginManager.parameters('EncounterMeter');

function Window_Encounter() {
    this.initialize.apply(this, arguments);
}

(function(_) {
   "use strict";
   /* plugin vars */

   /* Scene Map */
   var Encounter_Scene_Map_start  = Scene_Map.prototype.start;
   Scene_Map.prototype.start = function(){
       Encounter_Scene_Map_start.call(this);
       this.createEncounterWindow();
   };

   Scene_Map.prototype.createEncounterWindow = function(){
       //only add if not existing
       this._encounterWind = new Window_Encounter(0,60);
       this.addWindow(this._encounterWind);  
   };


   /* Window Encounter */
   Window_Encounter.prototype = Object.create(Window_Base.prototype);
   Window_Encounter.prototype.constructor = Window_Encounter;

   Window_Encounter.prototype.initialize = function(x,y){
       var width= $gamePlayer.getTotalEncSteps();
       if (width < 200){
           width = 200;
       }
       var height=80;
       Window_Base.prototype.initialize.call(this,x,y,width,height);
      
       this._gColor1 = "#F0F";
       this._gColor2 = "#C0C";
       this._gWidth = Math.max(9,width);
       this._gHeight = 5;
       this.contents.fontSize = 18;
       this.drawEncounter(0,0,this._gWidth,this._gHeight,0,this._gColor1,this._gColor2);
       this.setOpacity();
       this._IsVisible = true;
   };

   Window_Encounter.prototype.update = function(){
       if ($gameMap.encounterList().length > 0 && $gamePlayer._inEncRegion){
           if (!this._IsVisible){
               this.show(); //If window is not active and we are in an enc region then activate it
               this._IsVisible = true;
           }

           var rate = $gamePlayer.getCurrEncSteps() / $gamePlayer.getTotalEncSteps();
           this.drawEncounter(0,0,this._gWidth,this._gHeight,rate,this._gColor1,this._gColor2);
       } else{
           if (this._IsVisible){
               this.hide(); //If window is active and we are not in an enc region then deactivate it
               this._IsVisible = false;
           }
       }
   };

   Window_Encounter.prototype.setOpacity = function(){
       this.opacity = 255;
   }

   Window_Encounter.prototype.drawEncounter = function(x,y,width,height,rate,color1,color2){   
       var gColorBk = "#000";
       var fillWidth1 = Math.floor(width*rate).clamp(0,width);
       var fillWidth2 =width;
       var encText = "Encounter Meter";
       this.drawText(encText,x,y,100,'left');
       this.contents.fillRect(x,y+35,fillWidth2,height,gColorBk);
       this.contents.gradientFillRect(x,y+35,fillWidth1,height,color1,color2);
   };


   /* Encounter Game Player */
   var Game_Player_InitMembers = Game_Player.prototype.initMembers;
   Game_Player.prototype.initMembers = function(){
       Game_Player_InitMembers.call(this);
       this._totalEncSteps = 0;
       this._inEncRegion = false;

       this._regionList = [];
   };

   Game_Player.prototype.makeEncounterCount = function(){
       var n = $gameMap.encounterStep();
       this._encounterCount = Math.randomInt(n) + Math.randomInt(n) + 1;
       this._totalEncSteps = this._encounterCount;
   };

   Game_Player.prototype.increaseSteps = function() {
       //Find a way to only run this if _regionList is empty and there are regions within encounterList objs
       $gameMap.encounterList().forEach(function(encounter) { $gamePlayer._regionList.push(encounter.regionSet); });
       Game_Character.prototype.increaseSteps.call(this);
       if (this.isNormal()) {
           $gameParty.increaseSteps();

           $gamePlayer._regionList.forEach(function(rgnSet){
               if (rgnSet.length === 0 || rgnSet.contains($gamePlayer.regionId())){
                   //alert("Entering a dangerous region!");
                    $gamePlayer._inEncRegion = true;
               } else{
                    $gamePlayer._inEncRegion = false;
               }
           });
       }
   };

   Game_Player.prototype.getTotalEncSteps = function() { return this._totalEncSteps; };
   Game_Player.prototype.getCurrEncSteps = function() { return this._totalEncSteps - this._encounterCount; };
   Game_Player.prototype.getXLocal = function() { return this.x; };
   Game_Player.prototype.getYLocal = function(){ return this.y; };
})();
so, you're trying to inform the player about nearby encounters?
doesn't that defeat the purpose of the encounters system?
so, you're trying to inform the player about nearby encounters?
doesn't that defeat the purpose of the encounters system?

It is kind of like the encounter system in Legend of Dragoon where the crystal changes colors as you get closer to a chance of an encounter. The idea is that it does not inform the player that a fight is going to happen as soon as the meter is full but that at the point it fills a battle is imminent.

It will also be used as a non-interactive way of telling the player when they are in safe zones that are "protected" by npcs.
Oh - I know what I did wrong - that should have been called in Scene_Map.update, or Game_Player.update, not in the window's update function. In those two cases, it should happen right away (not on every step though).

Original thread: https://forums.rpgmakerweb.com/threads/help-with-region-based-encounters.87513/