Help with Yanfly Engine Ace Enemy Levels

I am having a tough time getting this script to have randomized enemy levels. I have min at 1, max level at 5, random fluctuation of 5 and default level type of 1. It works well up until my characters hit >5. At this point every single enemy is always level 5. Ive even tried disabling PARTY_LEVEL_TO_ENEMY_LEVEL and no such luck. I am also using falcao abs. Just to reiterate, when my party is <5 I get the good variability however after 5, every enemy is level 5
So what you want is for the enemy level to be random?
That's not really what the script does.
The script provides a level to the enemy and will adjust it based on the party levels.
If all the members in the party is level 5 or above and you set the enemy to a max of level 5, then it will give you all level 5.
after that it does a randomization, adding then subtracting.
But the final gives you a value that is the max value possible.
Ruby:
  def set_level_type
    @level = $game_party.match_party_level(enemy.level_type)
    @level += rand(enemy.level_rand+1)
    @level -= rand(enemy.level_rand+1)
    @level = [[@level, enemy.level_max].min, enemy.level_min].max
  end
so what you can see here is that if the @level was returned as 5
5 + random(RANDOM_FLUCTUATION + 1) which is 2 by default + 1 so rand(3)
(5+(rand(3)) - random(RANDOM_FLUCTUATION + 1) so rand(3)
@level = [ [ (5+(rand(3))-rand(3) , enemy max level] minimum, level minimum ] maximum

So it all depends on the random. You would have to get a low + and a high - to get something lower than 5. but it would also have to ignore your party level.

We could maybe add in another constant that would lower the level by if your party level is higher than the enemy max level then let the randomizations occur as normal.

This may work for you (untested)
Ruby:
# use note tag for random enemy levels when party level is higher than enemy level
# <level party random: 2>

module YEA
  module ENEMY_LEVEL
  
    # If your party level is higher than the enemy max level, this will add some
    # randomness to the enemy levels so that they are not always at max level
    RANDOM_PARTY_FLUCTUATION = 2
  end
end

module YEA
  module REGEXP
  module ENEMY
    LEVEL_PARTY_RAND = /<(?:LEVEL_PARTY_RANDOM|level party random):[ ](\d+)>/i
  end
  end
end

class RPG::Enemy < RPG::BaseItem
 
  #--------------------------------------------------------------------------
  # public instance variables
  #--------------------------------------------------------------------------
  attr_accessor :level_party_rand
 
  #--------------------------------------------------------------------------
  # common cache: load_notetags_elv
  #--------------------------------------------------------------------------
  alias r2_party_random_enemy_level_notetag load_notetags_elv
  def load_notetags_elv
    @level_party_rand = YEA::ENEMY_LEVEL::RANDOM_PARTY_FLUCTUATION
    r2_party_random_enemy_level_notetag
    self.note.split(/[\r\n]+/).each { |line|
      case line
      #---
      when YEA::REGEXP::ENEMY::LEVEL_PARTY_RAND
        @level_party_rand = $1.to_i
      end
    }
  end
end

class Game_Enemy < Game_Battler
  #--------------------------------------------------------------------------
  # new method: set_level_type
  #--------------------------------------------------------------------------
  def set_level_type
    @level = $game_party.match_party_level(enemy.level_type)
    if @level >= enemy.level_max
      @level = enemy.level_max
      @level -= enemy.level_party_rand
    end
    @level += rand(enemy.level_rand+1)
    @level -= rand(enemy.level_rand+1)
    @level = [[@level, enemy.level_max].min, enemy.level_min].max
  end
end
So what you want is for the enemy level to be random?
That's not really what the script does.
The script provides a level to the enemy and will adjust it based on the party levels.
If all the members in the party is level 5 or above and you set the enemy to a max of level 5, then it will give you all level 5.
after that it does a randomization, adding then subtracting.
But the final gives you a value that is the max value possible.
Ruby:
  def set_level_type
    @level = $game_party.match_party_level(enemy.level_type)
    @level += rand(enemy.level_rand+1)
    @level -= rand(enemy.level_rand+1)
    @level = [[@level, enemy.level_max].min, enemy.level_min].max
  end
so what you can see here is that if the @level was returned as 5
5 + random(RANDOM_FLUCTUATION + 1) which is 2 by default + 1 so rand(3)
(5+(rand(3)) - random(RANDOM_FLUCTUATION + 1) so rand(3)
@level = [ [ (5+(rand(3))-rand(3) , enemy max level] minimum, level minimum ] maximum

So it all depends on the random. You would have to get a low + and a high - to get something lower than 5. but it would also have to ignore your party level.

We could maybe add in another constant that would lower the level by if your party level is higher than the enemy max level then let the randomizations occur as normal.

This may work for you (untested)
Ruby:
# use note tag for random enemy levels when party level is higher than enemy level
# <level party random: 2>

module YEA
  module ENEMY_LEVEL
  
    # If your party level is higher than the enemy max level, this will add some
    # randomness to the enemy levels so that they are not always at max level
    RANDOM_PARTY_FLUCTUATION = 2
  end
end

module YEA
  module REGEXP
  module ENEMY
    LEVEL_PARTY_RAND = /<(?:LEVEL_PARTY_RANDOM|level party random):[ ](\d+)>/i
  end
  end
end

class RPG::Enemy < RPG::BaseItem
 
  #--------------------------------------------------------------------------
  # public instance variables
  #--------------------------------------------------------------------------
  attr_accessor :level_party_rand
 
  #--------------------------------------------------------------------------
  # common cache: load_notetags_elv
  #--------------------------------------------------------------------------
  alias r2_party_random_enemy_level_notetag load_notetags_elv
  def load_notetags_elv
    @level_party_rand = YEA::ENEMY_LEVEL::RANDOM_PARTY_FLUCTUATION
    r2_party_random_enemy_level_notetag
    self.note.split(/[\r\n]+/).each { |line|
      case line
      #---
      when YEA::REGEXP::ENEMY::LEVEL_PARTY_RAND
        @level_party_rand = $1.to_i
      end
    end
  end
end

class Game_Enemy < Game_Battler
  #--------------------------------------------------------------------------
  # new method: set_level_type
  #--------------------------------------------------------------------------
  def set_level_type
    @level = $game_party.match_party_level(enemy.level_type)
    if @level >= enemy.level_max
      @level = enemy.level_max
      @level -= enemy.level_party_rand
    end
    @level += rand(enemy.level_rand+1)
    @level -= rand(enemy.level_rand+1)
    @level = [[@level, enemy.level_max].min, enemy.level_min].max
  end
end
That makes sense as to why the enemies are all high level. I was hoping that with any enemy they would be a level from 1-5 or else you get the oblivion effect where leveling doesn’t mean much. I’ll try out your code and let you know! Thanks as always!
Finally got to try it, got this error while loading game:
error.png


In error log got this:
Game_Interpreter:1412:in `eval': uninitialized constant Game_Interpreter::Scene_Class, NameError
from Game_Interpreter:1412:in `eval'
from Game_Interpreter:1412:in `command_355'
from Game_Interpreter:198:in `execute_command'
from Game_Interpreter:103:in `run'
from tsut:906:in `run'
from Game_Interpreter:60:in `block in create_fiber'
Finally got to try it, got this error while loading game:
Yes typo on my part. fixed above.

Original thread: https://forums.rpgmakerweb.com/threads/help-with-yanfly-engine-ace-enemy-levels.170674/