Skip to content

Commit

Permalink
Fix debug spawning monsters
Browse files Browse the repository at this point in the history
  • Loading branch information
obligaron authored and AJenbo committed Dec 3, 2023
1 parent 16c814b commit 19b6e2b
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 42 deletions.
18 changes: 9 additions & 9 deletions Source/lua/modules/dev/monsters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ std::string DebugCmdSpawnUniqueMonster(std::string name, std::optional<unsigned>

int mtype = -1;
UniqueMonsterType uniqueIndex = UniqueMonsterType::None;
for (size_t i = 0; UniqueMonstersData.size(); ++i) {
auto mondata = UniqueMonstersData[i];
for (size_t i = 0; i < UniqueMonstersData.size(); ++i) {
const auto &mondata = UniqueMonstersData[i];
const std::string monsterName = AsciiStrToLower(std::string_view(mondata.mName));
if (monsterName.find(name) == std::string::npos)
continue;
Expand All @@ -55,11 +55,11 @@ std::string DebugCmdSpawnUniqueMonster(std::string name, std::optional<unsigned>
}

if (!found) {
if (LevelMonsterTypeCount == MaxLvlMTypes)
LevelMonsterTypeCount--; // we are running out of monster types, so override last used monster type
id = AddMonsterType(uniqueIndex, PLACE_SCATTER);
CMonster &monsterType = LevelMonsterTypes[id];
monsterType.type = static_cast<_monster_id>(mtype);
InitMonsterGFX(monsterType);
InitMonsterSND(monsterType);
monsterType.placeFlags |= PLACE_SCATTER;
monsterType.corpseId = 1;
}

Expand Down Expand Up @@ -104,7 +104,7 @@ std::string DebugCmdSpawnMonster(std::string name, std::optional<unsigned> count
int mtype = -1;

for (int i = 0; i < NUM_MTYPES; i++) {
auto mondata = MonstersData[i];
const auto &mondata = MonstersData[i];
const std::string monsterName = AsciiStrToLower(std::string_view(mondata.name));
if (monsterName.find(name) == std::string::npos)
continue;
Expand All @@ -127,11 +127,11 @@ std::string DebugCmdSpawnMonster(std::string name, std::optional<unsigned> count
}

if (!found) {
if (LevelMonsterTypeCount == MaxLvlMTypes)
LevelMonsterTypeCount--; // we are running out of monster types, so override last used monster type
id = AddMonsterType(static_cast<_monster_id>(mtype), PLACE_SCATTER);
CMonster &monsterType = LevelMonsterTypes[id];
monsterType.type = static_cast<_monster_id>(mtype);
InitMonsterGFX(monsterType);
InitMonsterSND(monsterType);
monsterType.placeFlags |= PLACE_SCATTER;
monsterType.corpseId = 1;
}

Expand Down
61 changes: 28 additions & 33 deletions Source/monster.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,39 +417,6 @@ void PlaceUniqueMonst(UniqueMonsterType uniqindex, size_t minionType, int bosspa
PrepareUniqueMonst(monster, uniqindex, minionType, bosspacksize, uniqueMonsterData);
}

size_t AddMonsterType(_monster_id type, placeflag placeflag)
{
const size_t typeIndex = GetMonsterTypeIndex(type);
CMonster &monsterType = LevelMonsterTypes[typeIndex];

if (typeIndex == LevelMonsterTypeCount) {
LevelMonsterTypeCount++;
monsterType.type = type;
const MonsterData &monsterData = MonstersData[type];
monstimgtot += monsterData.image;

const size_t numAnims = GetNumAnims(monsterData);
for (size_t i = 0; i < numAnims; ++i) {
AnimStruct &anim = monsterType.anims[i];
anim.frames = monsterData.frames[i];
if (monsterData.hasAnim(i)) {
anim.rate = monsterData.rate[i];
anim.width = monsterData.width;
}
}

InitMonsterSND(monsterType);
}

monsterType.placeFlags |= placeflag;
return typeIndex;
}

inline size_t AddMonsterType(UniqueMonsterType uniqueType, placeflag placeflag)
{
return AddMonsterType(UniqueMonstersData[static_cast<size_t>(uniqueType)].mtype, placeflag);
}

void ClearMVars(Monster &monster)
{
monster.var1 = 0;
Expand Down Expand Up @@ -3165,6 +3132,34 @@ MonsterSpritesData LoadMonsterSpritesData(const MonsterData &monsterData)

} // namespace

size_t AddMonsterType(_monster_id type, placeflag placeflag)
{
const size_t typeIndex = GetMonsterTypeIndex(type);
CMonster &monsterType = LevelMonsterTypes[typeIndex];

if (typeIndex == LevelMonsterTypeCount) {
LevelMonsterTypeCount++;
monsterType.type = type;
const MonsterData &monsterData = MonstersData[type];
monstimgtot += monsterData.image;

const size_t numAnims = GetNumAnims(monsterData);
for (size_t i = 0; i < numAnims; ++i) {
AnimStruct &anim = monsterType.anims[i];
anim.frames = monsterData.frames[i];
if (monsterData.hasAnim(i)) {
anim.rate = monsterData.rate[i];
anim.width = monsterData.width;
}
}

InitMonsterSND(monsterType);
}

monsterType.placeFlags |= placeflag;
return typeIndex;
}

void InitTRNForUniqueMonster(Monster &monster)
{
char filestr[64];
Expand Down
5 changes: 5 additions & 0 deletions Source/monster.h
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,11 @@ extern bool sgbSaveSoundOn;
void PrepareUniqueMonst(Monster &monster, UniqueMonsterType monsterType, size_t miniontype, int bosspacksize, const UniqueMonsterData &uniqueMonsterData);
void InitLevelMonsters();
void GetLevelMTypes();
size_t AddMonsterType(_monster_id type, placeflag placeflag);
inline size_t AddMonsterType(UniqueMonsterType uniqueType, placeflag placeflag)
{
return AddMonsterType(UniqueMonstersData[static_cast<size_t>(uniqueType)].mtype, placeflag);
}
void InitMonsterSND(CMonster &monsterType);
void InitMonsterGFX(CMonster &monsterType, MonsterSpritesData &&spritesData = {});
void InitAllMonsterGFX();
Expand Down

0 comments on commit 19b6e2b

Please sign in to comment.