From 2a3cf98850179cf5e9676bc0eb42a7ea597acd17 Mon Sep 17 00:00:00 2001 From: Vladimir Olteanu Date: Tue, 27 Apr 2021 23:32:50 +0300 Subject: [PATCH] Slight cleanup of dead{.h,.cpp} --- Source/dead.cpp | 44 ++++++++++++++++++++------------------------ Source/dead.h | 7 ++++--- Source/missiles.cpp | 2 +- Source/monster.cpp | 4 ++-- Source/monster.h | 3 ++- Source/msg.cpp | 4 ++-- Source/player.cpp | 2 +- test/dead_test.cpp | 4 ++-- 8 files changed, 34 insertions(+), 36 deletions(-) diff --git a/Source/dead.cpp b/Source/dead.cpp index 865459826..1f296a522 100644 --- a/Source/dead.cpp +++ b/Source/dead.cpp @@ -17,25 +17,22 @@ int8_t stonendx; void InitDead() { - int8_t mtypes[MAXMONSTERS]; - - for (int8_t &mtype : mtypes) - mtype = 0; + int8_t mtypes[MAXMONSTERS] = {}; int8_t nd = 0; for (int i = 0; i < nummtypes; i++) { - if (mtypes[Monsters[i].mtype] == 0) { - for (int d = 0; d < 8; d++) - dead[nd]._deadData[d] = Monsters[i].Anims[MA_DEATH].Data[d]; - dead[nd]._deadFrame = Monsters[i].Anims[MA_DEATH].Frames; - dead[nd]._deadWidth = Monsters[i].width; - dead[nd]._deadtrans = 0; - nd++; + if (mtypes[Monsters[i].mtype] != 0) + continue; - Monsters[i].mdeadval = nd; - mtypes[Monsters[i].mtype] = nd; - } + dead[nd]._deadData = Monsters[i].Anims[MA_DEATH].Data; + dead[nd]._deadFrame = Monsters[i].Anims[MA_DEATH].Frames; + dead[nd]._deadWidth = Monsters[i].width; + dead[nd]._deadtrans = 0; + nd++; + + Monsters[i].mdeadval = nd; + mtypes[Monsters[i].mtype] = nd; } for (auto &d : dead[nd]._deadData) @@ -58,8 +55,7 @@ void InitDead() for (int i = 0; i < nummonsters; i++) { int mi = monstactive[i]; if (monster[mi]._uniqtype != 0) { - for (int d = 0; d < 8; d++) - dead[nd]._deadData[d] = monster[mi].MType->Anims[MA_DEATH].Data[d]; + dead[nd]._deadData = monster[mi].MType->Anims[MA_DEATH].Data; dead[nd]._deadFrame = monster[mi].MType->Anims[MA_DEATH].Frames; dead[nd]._deadWidth = monster[mi].MType->width; dead[nd]._deadtrans = monster[mi]._uniqtrans + 4; @@ -72,21 +68,21 @@ void InitDead() assert(nd <= MAXDEAD); } -void AddDead(int dx, int dy, int8_t dv, direction ddir) +void AddDead(Point loc, int8_t dv, direction ddir) { - dDead[dx][dy] = (dv & 0x1F) + (ddir << 5); + dDead[loc.x][loc.y] = (dv & 0x1F) + (ddir << 5); } void SetDead() { for (int i = 0; i < nummonsters; i++) { int mi = monstactive[i]; - if (monster[mi]._uniqtype != 0) { - for (int dx = 0; dx < MAXDUNX; dx++) { - for (int dy = 0; dy < MAXDUNY; dy++) { - if ((dDead[dx][dy] & 0x1F) == monster[mi]._udeadval) - ChangeLightXY(monster[mi].mlid, dx, dy); - } + if (monster[mi]._uniqtype == 0) + continue; + for (int dx = 0; dx < MAXDUNX; dx++) { + for (int dy = 0; dy < MAXDUNY; dy++) { + if ((dDead[dx][dy] & 0x1F) == monster[mi]._udeadval) + ChangeLightXY(monster[mi].mlid, dx, dy); } } } diff --git a/Source/dead.h b/Source/dead.h index 3a2aef08b..e3808eda9 100644 --- a/Source/dead.h +++ b/Source/dead.h @@ -5,16 +5,17 @@ */ #pragma once +#include #include #include "engine.h" namespace devilution { -#define MAXDEAD 31 +static constexpr unsigned MAXDEAD = 31; struct DeadStruct { - uint8_t *_deadData[8]; + std::array _deadData; int _deadFrame; int _deadWidth; uint8_t _deadtrans; @@ -24,7 +25,7 @@ extern DeadStruct dead[MAXDEAD]; extern int8_t stonendx; void InitDead(); -void AddDead(int dx, int dy, int8_t dv, direction ddir); +void AddDead(Point loc, int8_t dv, direction ddir); void SetDead(); } // namespace devilution diff --git a/Source/missiles.cpp b/Source/missiles.cpp index 058dfeae2..48126f959 100644 --- a/Source/missiles.cpp +++ b/Source/missiles.cpp @@ -4619,7 +4619,7 @@ void MI_Stone(int i) if (monster[m]._mhitpoints > 0) monster[m]._mmode = (MON_MODE)missile[i]._miVar1; else - AddDead(monster[m].position.tile.x, monster[m].position.tile.y, stonendx, monster[m]._mdir); + AddDead(monster[m].position.tile, stonendx, monster[m]._mdir); } if (missile[i]._miAnimType == MFILE_SHATTER1) PutMissile(i); diff --git a/Source/monster.cpp b/Source/monster.cpp index 10374aad2..f0942e608 100644 --- a/Source/monster.cpp +++ b/Source/monster.cpp @@ -2637,9 +2637,9 @@ bool M_DoDeath(int i) PrepDoEnding(); } else if (monster[i]._mAnimFrame == monster[i]._mAnimLen) { if (monster[i]._uniqtype == 0) - AddDead(monster[i].position.tile.x, monster[i].position.tile.y, monster[i].MType->mdeadval, monster[i]._mdir); + AddDead(monster[i].position.tile, monster[i].MType->mdeadval, monster[i]._mdir); else - AddDead(monster[i].position.tile.x, monster[i].position.tile.y, monster[i]._udeadval, monster[i]._mdir); + AddDead(monster[i].position.tile, monster[i]._udeadval, monster[i]._mdir); dMonster[monster[i].position.tile.x][monster[i].position.tile.y] = 0; monster[i]._mDelFlag = true; diff --git a/Source/monster.h b/Source/monster.h index 2509bd90f..94e9395cc 100644 --- a/Source/monster.h +++ b/Source/monster.h @@ -6,6 +6,7 @@ #pragma once #include +#include #include "engine.h" #include "miniwin/miniwin.h" @@ -106,7 +107,7 @@ enum placeflag : uint8_t { struct AnimStruct { uint8_t *CMem; - uint8_t *Data[8]; + std::array Data; int Frames; int Rate; }; diff --git a/Source/msg.cpp b/Source/msg.cpp index 857c53dbe..7f424a44e 100644 --- a/Source/msg.cpp +++ b/Source/msg.cpp @@ -720,9 +720,9 @@ void DeltaLoadLevel() if (monster[i]._mAi != AI_DIABLO) { if (monster[i]._uniqtype == 0) { assert(monster[i].MType != nullptr); - AddDead(monster[i].position.tile.x, monster[i].position.tile.y, monster[i].MType->mdeadval, monster[i]._mdir); + AddDead(monster[i].position.tile, monster[i].MType->mdeadval, monster[i]._mdir); } else { - AddDead(monster[i].position.tile.x, monster[i].position.tile.y, monster[i]._udeadval, monster[i]._mdir); + AddDead(monster[i].position.tile, monster[i]._udeadval, monster[i]._mdir); } } monster[i]._mDelFlag = true; diff --git a/Source/player.cpp b/Source/player.cpp index 19708e66d..ae54bf881 100644 --- a/Source/player.cpp +++ b/Source/player.cpp @@ -2127,7 +2127,7 @@ void RemovePlrMissiles(int pnum) if (currlevel != 0 && pnum == myplr && (monster[myplr].position.tile.x != 1 || monster[myplr].position.tile.y != 0)) { M_StartKill(myplr, myplr); - AddDead(monster[myplr].position.tile.x, monster[myplr].position.tile.y, (monster[myplr].MType)->mdeadval, monster[myplr]._mdir); + AddDead(monster[myplr].position.tile, (monster[myplr].MType)->mdeadval, monster[myplr]._mdir); mx = monster[myplr].position.tile.x; my = monster[myplr].position.tile.y; dMonster[mx][my] = 0; diff --git a/test/dead_test.cpp b/test/dead_test.cpp index ca6c03841..c6f65db6c 100644 --- a/test/dead_test.cpp +++ b/test/dead_test.cpp @@ -8,12 +8,12 @@ using namespace devilution; TEST(Dead, AddDead) { - AddDead(21, 48, 8, DIR_W); + AddDead({21, 48}, 8, DIR_W); EXPECT_EQ(dDead[21][48], 8 + (DIR_W << 5)); } TEST(Dead, AddDead_OOB) { - AddDead(21, 48, MAXDEAD + 1, DIR_W); + AddDead({21, 48}, MAXDEAD + 1, DIR_W); EXPECT_EQ(dDead[21][48], 0 + (DIR_W << 5)); }