diff --git a/Source/missiles.cpp b/Source/missiles.cpp index c7950a5ec..4e0e090bc 100644 --- a/Source/missiles.cpp +++ b/Source/missiles.cpp @@ -3080,10 +3080,7 @@ void ProcessHorkSpawn(Missile &missile) if (spawnPosition) { auto facing = static_cast(missile.var1); - Monster *monster = AddMonster(*spawnPosition, facing, 1, true); - if (monster != nullptr) { - M_StartStand(*monster, facing); - } + SpawnMonster(*spawnPosition, facing, 1); } } else { missile._midist++; diff --git a/Source/monster.cpp b/Source/monster.cpp index 7f8873eb8..378268bc7 100644 --- a/Source/monster.cpp +++ b/Source/monster.cpp @@ -1538,7 +1538,7 @@ void MonsterPetrified(Monster &monster) } } -Monster *AddSkeleton(Point position, Direction dir, bool inMap) +std::optional GetRandomSkeletonTypeIndex() { int32_t typeCount = 0; size_t skeletonIndexes[SkeletonTypes.size()]; @@ -1549,18 +1549,20 @@ Monster *AddSkeleton(Point position, Direction dir, bool inMap) } if (typeCount == 0) { - return nullptr; + return {}; } const size_t typeIndex = skeletonIndexes[GenerateRnd(typeCount)]; - return AddMonster(position, dir, typeIndex, inMap); + return typeIndex; } -void SpawnSkeleton(Point position, Direction dir) +Monster *AddSkeleton(Point position, Direction dir, bool inMap) { - Monster *skeleton = AddSkeleton(position, dir, true); - if (skeleton != nullptr) - StartSpecialStand(*skeleton, dir); + auto typeIndex = GetRandomSkeletonTypeIndex(); + if (!typeIndex) + return nullptr; + + return AddMonster(position, dir, *typeIndex, inMap); } bool IsLineNotSolid(Point startPoint, Point endPoint) @@ -2295,7 +2297,10 @@ void LeoricAi(Monster &monster) && LineClearMissile(monster.position.tile, monster.enemyPosition)) { Point newPosition = monster.position.tile + md; if (IsTileAvailable(monster, newPosition) && ActiveMonsterCount < MaxMonsters) { - SpawnSkeleton(newPosition, md); + auto typeIndex = GetRandomSkeletonTypeIndex(); + if (typeIndex) { + SpawnMonster(newPosition, md, *typeIndex, true); + } StartSpecialStand(monster, md); } } else { @@ -3638,6 +3643,13 @@ Monster *AddMonster(Point position, Direction dir, size_t typeIndex, bool inMap) return nullptr; } +void SpawnMonster(Point position, Direction dir, size_t typeIndex, bool startSpecialStand /*= false*/) +{ + Monster *monster = AddMonster(position, dir, typeIndex, true); + if (startSpecialStand && monster != nullptr) + StartSpecialStand(*monster, dir); +} + void AddDoppelganger(Monster &monster) { Point target = { 0, 0 }; @@ -3649,7 +3661,7 @@ void AddDoppelganger(Monster &monster) } if (target != Point { 0, 0 }) { const size_t typeIndex = GetMonsterTypeIndex(monster.type().type); - AddMonster(target, monster.direction, typeIndex, true); + SpawnMonster(target, monster.direction, typeIndex); } } diff --git a/Source/monster.h b/Source/monster.h index e2dc9cfc9..bbb88c951 100644 --- a/Source/monster.h +++ b/Source/monster.h @@ -495,6 +495,7 @@ void InitGolems(); void InitMonsters(); void SetMapMonsters(const uint16_t *dunData, Point startPosition); Monster *AddMonster(Point position, Direction dir, size_t mtype, bool inMap); +void SpawnMonster(Point position, Direction dir, size_t typeIndex, bool startSpecialStand = false); void AddDoppelganger(Monster &monster); void ApplyMonsterDamage(DamageType damageType, Monster &monster, int damage); bool M_Talker(const Monster &monster);