EvaisaDev/LethalLib

Registering an enemy with rarity table method does not add the enemy to the spawn list

Closed this issue · 0 comments

Heya! I seem to have spotted an issue where the non-default overloads for registering an enemy, specifically with rarity tables does not add an enemy after all if it hasn't already been registered. I don't think this is intended functionality?

public static void RegisterEnemy(EnemyType enemy, SpawnType spawnType, Dictionary<Levels.LevelTypes, int>? levelRarities = null, Dictionary<string, int>? customLevelRarities = null, TerminalNode infoNode = null, TerminalKeyword infoKeyword = null)
{
// if already registered, add rarity to levelRarities
var spawnableEnemy = spawnableEnemies.FirstOrDefault(x => x.enemy == enemy && x.spawnType == spawnType);
if (spawnableEnemy != null)
{
if (levelRarities != null)
{
foreach (var level in levelRarities)
{
spawnableEnemy.levelRarities.Add(level.Key, level.Value);
}
}
if (customLevelRarities != null)
{
foreach (var level in customLevelRarities)
{
spawnableEnemy.customLevelRarities.Add(level.Key, level.Value);
}
}
return;
}
spawnableEnemy = new SpawnableEnemy(enemy, spawnType, levelRarities, customLevelRarities);
spawnableEnemy.terminalNode = infoNode;
spawnableEnemy.infoKeyword = infoKeyword;
var callingAssembly = Assembly.GetCallingAssembly();
var modDLL = callingAssembly.GetName().Name;
spawnableEnemy.modName = modDLL;
}

At the end of this method call compared to the the base rarity overload method, the enemy is not added/registered to the list of enemies via spawnableEnemies.Add(spawnableEnemy).

This means that to register an enemy, it must always be done through the non rarity table enabled method first. A possible workaround for this can be seen below.

// Register our enemy with no levels so it shows up in the debug menu.
// This is a bug/issue in LethalLib that doesn't register the enemy correctly outside of the base overload
// https://github.com/EvaisaDev/LethalLib/blob/main/LethalLib/Modules/Enemies.cs#L372-L405
// Reference the fact that spawnableEnemies.Add(spawnableEnemy) is not called at the end of this method.
Enemies.RegisterEnemy(
    lockerEnemy,
    Locker.Config.LockerSpawnWeight.Value,
    Levels.LevelTypes.None,
    Enemies.SpawnType.Default,
    lockerTerminalNode,
    lockerTerminalKeyword
);

// Need to do this to not keep the none level type registered.
Enemies.RemoveEnemyFromLevels(lockerEnemy, Levels.LevelTypes.None);

// Register our enemy and set spawn options from the config.
Enemies.RegisterEnemy(
    lockerEnemy,
    Enemies.SpawnType.Default,
    new Dictionary<Levels.LevelTypes, int>
    {
        [Levels.LevelTypes.ExperimentationLevel] = Locker.Config.LockerSpawnWeight.Value
    },
    levelWeightOverrides,
    lockerTerminalNode,
    lockerTerminalKeyword
);

As a proposed solution, we should check if the enemy with that name/identifier has already been registered. If not, it should be registered at the end of the method.