MZ Randomizing enemy troop contents and formation.

Forgive me for asking but I could not find one. Is there a plugin that randomizes enemy troops' formation and contents based on a preset list of possible enemies to appear?

Could be manually selected or have a tag classifying specific enemies as a "type" and battles have different types they can generate in the troop.

I have an AoE damage plugin I'm using and I'd like it so there's a lot of different combinations of monsters and positioning so I won't have to make a bunch of them myself or have a human-made amount where you can end up with a lot of encounters with the same strategy of skills to use.

Merry Christmas also 🙂
Randomizing enemies is possible without a plugin.
you just need a few hidden enemies (right-click and appear halfway) and a troop event running on turn 0.
the troop event first transforms the enemies into something else based on whatever checks you do in the troop event, then have them appear with the event command after the transformation.

however that does not change the position of the enemy sprite, for that you'll need the help of a plugin.
Randomizing enemies is possible without a plugin.
you just need a few hidden enemies (right-click and appear halfway) and a troop event running on turn 0.
the troop event first transforms the enemies into something else based on whatever checks you do in the troop event, then have them appear with the event command after the transformation.

however that does not change the position of the enemy sprite, for that you'll need the help of a plugin.
Any idea what function I should be overriding to generate the enemies then? I really need the randomized position. I'm not against coding it myself as it's just a lot of math to calculate where enemies already are so they don't overlap. But I have no idea where to start on getting the enemy IDs and battle generation.

Thank you for your reply 🙂
Hey,

The closest for this would probably be Game_Troop.prototype.setup (rmmz_objects.js).
Update: I've been working on this and realized that there should be existing troops to tell you what type of enemy (undead, plant, aquatic, etc) can generate for the specific area you want. Is there any way to read some sort of string data off troops? I noticed there's no notetag option like there is for enemies and stuff.

I can't do it off events I think because the positioning/randomly generating is done before the battle starts per se.
Is there any way to read some sort of string data off troops? I noticed there's no notetag option like there is for enemies and stuff.
  1. Install and enable my SFG_Utils (make sure it's version 1.7 or later).
  2. Make sure the first page of your troop is set to Don't Run.
  3. Add a comment node to that first page, and put note tags in the comment.
  4. Now those note tags can be read from $gameTroops[i].meta just like standard note tags.
  1. Install and enable my SFG_Utils (make sure it's version 1.7 or later).
  2. Make sure the first page of your troop is set to Don't Run.
  3. Add a comment node to that first page, and put note tags in the comment.
  4. Now those note tags can be read from $gameTroops[i].meta just like standard note tags.
Thank you so much !!!! 😀
//check if monster is already at coordinates
function checkEnemyCoordinates(x,y,bitmap,enemies){

if(enemies.length <= 0) return true;
//get values of new enemy
const width = bitmap.width;
const height = bitmap.height;

for(let i = 0; i < enemies.length; i++){

//set values
const checker = enemies;
const top = checker.y - checker.height;
const bottom = checker.y;
const left = checker.x - checker.width/2;
const right = checker.x + checker.width/2;
//if overlap return false
if(y > top && y - height < bottom && x + width/2 > left && x - width/2 < right)
{
return false;
}
}

return true;
}

//override function
const gameTroopSetup = Game_Troop.prototype.setup;
Game_Troop.prototype.setup = function(troopId) {

//if disabled run as normal
if(!isRandom){
gameTroopSetup.call(this, troopId);
return;
}
//if enable, run as normal for bosses
const troop = $dataTroops[troopId];
if (troop.name.toLowerCase().includes("boss")) {
gameTroopSetup.call(this, troopId);
return;
}

const numEnemies = Math.floor(Math.random() * (maxEnemies - minEnemies + 1) + minEnemies);
this.clear();
this._troopId = troopId;
this._enemies = [];
const existingEnemies = [];
const tries = 5;
for (let i = 0; i < numEnemies; i++) {
const enemyFromGroup = $dataEnemies[1 + Math.floor(Math.random() * ($dataEnemies.length - 1))];
if(readEnemyType(enemyFromGroup) == "Boss"){
//if lands on boss, skip and generate another enemy
i -= 1;
continue;
}
const enemyId = enemyFromGroup.id;
let x = Math.floor(Math.random() * 400) + 50;
let y = Math.floor(Math.random() * 300) + 200;
const enemyBitmap = ImageManager.loadBitmap(enemyFromGroup.battlerName);
//if overlap, generate new coordinates
//try up to 5 times
for(let j = 0; i < tries; j++){
if (checkEnemyCoordinates(x,y,enemyBitmap,existingEnemies)){
break;
}
else{
x = Math.floor(Math.random() * 400) + 50;
y = Math.floor(Math.random() * 300) + 200;
}
}
if(!checkEnemyCoordinates(x,y,enemyBitmap,existingEnemies)){
continue;
}
const enemy = new Game_Enemy(enemyId, x, y);
existingEnemies.push({x :x, y: y, width: enemyBitmap.width, height: enemyBitmap.height});
this._enemies.push(enemy);
}
this.makeUniqueNames();
}

}

This ^ is the code I have so far (5 tries and x,y randomizer still pending, currently just checking if sprites overlap). I'm doing this on the default RPGmaker project with no other plugins except Art_VerboseErrorScreen. I apologize if the collision check is inverted but I tried every possibility of leftright/updown and all of them still caused collisions. Any idea what I'm doing wrong here? ^_^"
Please put your code in [code] tags (if using the visual editor, select it and press the </> button). Without that, it becomes extremely hard to read.
Please put your code in [code] tags (if using the visual editor, select it and press the </> button). Without that, it becomes extremely hard to read.
Oh my bad 🙂 I'm new here I didn't know you could do that. Here it is.

Code:
//check if monster is already at coordinates

function checkEnemyCoordinates(x,y,bitmap,enemies){

    

    if(enemies.length <= 0) return true;

    //get values of new enemy

    const width = bitmap.width;

    const height = bitmap.height;

    

    for(let i = 0; i < enemies.length; i++){

        

        //set values

        const checker = enemies;

         const top = checker.y - checker.height;
         const bottom = checker.y;
         const left = checker.x - checker.width/2;
         const right = checker.x + checker.width/2;
         //if overlap return false
         if(y > top && y - height < bottom && x + width/2 > left && x - width/2 < right)
         {
             return false;
         }
     }
    
     return true;
 }

 //override function
 const gameTroopSetup =  Game_Troop.prototype.setup;
 Game_Troop.prototype.setup = function(troopId) {
    
     //if disabled run as normal
     if(!isRandom){
         gameTroopSetup.call(this, troopId);
         return;
     }
     //if enable, run as normal for bosses
     const troop = $dataTroops[troopId];
     if (troop.name.toLowerCase().includes("boss")) {
         gameTroopSetup.call(this, troopId);
         return;
     }
    
    const numEnemies = Math.floor(Math.random() * (maxEnemies - minEnemies + 1) + minEnemies);
    this.clear();
    this._troopId = troopId;
    this._enemies = [];
    const existingEnemies = [];
    const tries = 5;
    for (let i = 0; i < numEnemies; i++) {
          const enemyFromGroup = $dataEnemies[1 + Math.floor(Math.random() * ($dataEnemies.length - 1))];
          if(readEnemyType(enemyFromGroup) == "Boss"){
              //if lands on boss, skip and generate another enemy
              i -= 1;
              continue;
          }
          const enemyId = enemyFromGroup.id;
          let x = Math.floor(Math.random() * 400) + 50;
          let y = Math.floor(Math.random() * 300) + 200;
          const enemyBitmap = ImageManager.loadBitmap(enemyFromGroup.battlerName);
          //if overlap, generate new coordinates
          //try up to 5 times
          for(let j = 0; i < tries; j++){
              if (checkEnemyCoordinates(x,y,enemyBitmap,existingEnemies)){
                  break;
              }
              else{
                  x = Math.floor(Math.random() * 400) + 50;
                  y = Math.floor(Math.random() * 300) + 200;
              }
          }
          if(!checkEnemyCoordinates(x,y,enemyBitmap,existingEnemies)){
              continue;
          }
          const enemy = new Game_Enemy(enemyId, x, y);
          existingEnemies.push({x :x, y: y, width: enemyBitmap.width, height: enemyBitmap.height});
          this._enemies.push(enemy);
        }
    this.makeUniqueNames();
    }
I think you selected your previous post (without pressing Edit) and copy-pasted it into the new post, because this line:

JavaScript:
const checker = enemies;

should look like this:

JavaScript:
const checker = enemies[i];

In the previous post, the [i] was interpreted as "start italics" and thus made invisible.

Side note: You can also make it look even better by using [code=javascript] instead of just [code]

Other than that, I don't know what your purpose is in posting the code. Is it just to show off, or do you have some specific questions or issues?
They wrote at the end that the battlers collided.

I didn't read all that, but doesn't it take time for the bitmaps to load? So you can't immediately get their width and height? You can put the width and height as enemy notetags.

Don't know if that's the problem though LOL.
It does seem like a plausible problem. You could probably use asynchronous code instead of putting the width and height in note tags, but I'm not entirely sure how that would work when you're aliasing Game_Troop.prototype.setup. At the very least you'd also need to alias Scene_Battle.prototype.isReady…?
Have not looked too closely into the code, aside from the feature that you only do 5 attempts you only seem to test whether the new enemy is completely covered by one of the existing ones, but not the other way around? (if an existing one is completely covered by the new enemy).
It might also be possible that multiple enemies cover up another together maybe?

Interesting concept either way.

Edit: Did not think of the way the ImageManager works, the bitmaps not yet being loaded would make sense I think.
Thank you guys for your help 🙂 the ImageManager not loading in time is likely the problem. And I'm sorry if posting the code made it seem like I was showing off lol, didn't mean to come off like that. Take care and happy holidays!
And I'm sorry if posting the code made it seem like I was showing off lol
I didn't mean showing off as in "bragging". I meant it more like "here's what I did".

Original thread: https://forums.rpgmakerweb.com/threads/randomizing-enemy-troop-contents-and-formation.181613/